function addQuiz(Model_Quiz_Quiz $quiz) { $db = Zend_Registry::get('db'); $current = $this->getQuizzes(); try { $db->insert("sequence_quiz", array('quiz_id' => $quiz->getID(), 'sequence_id' => $this->id, 'position' => count($current) + 1)); return true; } catch (Exception $e) { My_Logger::log($e->getMessage()); return false; } }
public function getQuiz() { return Model_Quiz_Quiz::fromID($this->quizquiz_id); }
/** * This action is the quiz shell. * It ensures permissions, creates new attempts etc etc. * * @author Ben Evans */ public function attemptAction() { Model_Shell_Debug::getInstance()->log("User Entered the Attempt Action"); $identity = Zend_Auth::getInstance()->getIdentity(); $username = $identity->username; $auth_model = Model_Auth_General::getAuthModel(); /* Before we do anything, test to make sure we've passed a VALID QUIZ which WE ARE ENTITLED to sit. */ $quiz_id = $this->_getParam("quiz"); if (is_null($quiz_id)) { throw new Exception("No quiz was passed. Cannot continue."); } $vQuiz = Model_Quiz_Quiz::fromID($quiz_id); if ($vQuiz == null) { throw new Exception("Quiz ID passed was invalid. Cannot continue."); } $mFinished = false; $mMarking = false; //Permissions if ($auth_model->userInGroup($username, $vQuiz->getPermissions_group()) && $vQuiz->getOpen_date() <= strtotime("now")) { //Have we run out of attempts? $vAttempts = Model_Quiz_QuizAttempt::getAllFromUser($username, $vQuiz); if (sizeof($vAttempts) >= $vQuiz->getMax_attempts()) { //It is possible that we're on our last attempt, and that it's "in progress"...check $bInProgress = false; foreach ($vAttempts as $vAttempt) { if ($vAttempt->getDate_finished() == null) { $bInProgress = true; } } if (!$bInProgress) { throw new Exception("You've exceeded your maximum attempts for this quiz. Cannot continue"); } } } else { if (!$this->view->is_admin) { throw new Exception("Insufficient Permissions to take this quiz / Quiz not open yet"); } $vAttempts = Model_Quiz_QuizAttempt::getAllFromUser($username, $vQuiz); } /* Ok. We're allowed to TAKE the quiz. Are we resuming, or starting a new one? */ $mQuizAttempt = null; if (is_array($vAttempts)) { foreach ($vAttempts as $vAttempt) { if ($vAttempt->getDate_finished() == null) { $mQuizAttempt = $vAttempt; break; } } //End Foreach } //End If if ($mQuizAttempt == null) { $mQuizAttempt = Model_Quiz_QuizAttempt::fromScratch(strtotime("now"), $vQuiz, $username); } /* Calculate the total questions needed for this quiz */ $vTCs = $vQuiz->getTestedConcepts(); $vTotalQuestions = 0; foreach ($vTCs as $vTC) { $vTotalQuestions = $vTotalQuestions + $vTC->getNumber_tested(); } /* We have our quizAttempt ready to go. Now we look to see if we're resuming a question or not */ $mQuestionAttempt = $mQuizAttempt->getLastIncompleteQuestion(); if (is_object($mQuestionAttempt) && !$mQuestionAttempt->isValid()) { $mQuestionAttempt->destroy(); // Remove the Question attempt (Database was reinitialised or something) $mQuestionAttempt = null; } if ($mQuestionAttempt != null) { /* Are we getting an ANSWER for this question? */ if (array_key_exists("marking", $_POST) && $_POST['marking'] == "1") { /* Mark it */ $mMarking = true; } /* If we reach here, the page has probably been refreshed. We just re-display the last question */ } else { /* Have we finished this quiz? */ if ($mQuizAttempt->getQuestionAttemptCount() >= $vTotalQuestions) { //Close this attempt and display a result later on down the page $mQuizAttempt->setDate_finished(strtotime("now")); //Calculate and store the final score $mQuizAttempt->setTotal_score($mQuizAttempt->getTotal_score()); $mFinished = true; } else { /* QuizAttempt isn't finished... Fetch a questionBase */ $vQuestionBase = Model_Shell_QuestionChooser::select_next_question($mQuizAttempt, true); /* Make a GeneratedQuestion */ $vCounter = 0; //Make sure we don't get any fluke no-text answers while ($vCounter < 3) { Model_Shell_Debug::getInstance()->log("vQuestionBase: " . isset($vQuestionBase)); Model_Shell_Debug::getInstance()->log("Generating... from " . $vQuestionBase->getXml()); $vGen = Model_Quiz_GeneratedQuestion::fromQuestionBase($vQuestionBase); if ($vGen->getCorrect_answer() != "" && $vGen->getCorrect_answer() != "\r\n") { break; } else { $vGen->remove(); } $vCounter++; } if ($vGen->getCorrect_answer() == "" || $vGen->getCorrect_answer() == "\r\n") { throw new Exception("Error. While generating a question for you, blank answers appeared > 3 times. This should never happen. Either try to refresh this page, or consult your lecturer..."); } /* Make a QuestionAttempt */ $mQuestionAttempt = Model_Quiz_QuestionAttempt::fromScratch($vQuestionBase, strtotime("now"), strtotime("now"), $mQuizAttempt, $vGen); } //End-if_finished_quizAttempt } // Pass all relevant information to the view $this->view->quiz = $vQuiz; $this->view->question_attempt = $mQuestionAttempt; $this->view->finished = $mFinished; $this->view->marking = $mMarking; $this->view->mQuizAttempt = $mQuizAttempt; $this->view->vTotalQuestions = $vTotalQuestions; }
protected function findQuiz($quiz_id) { if (is_null($quiz_id)) { throw new Exception("No quiz was passed. Cannot continue."); } $quiz = Model_Quiz_Quiz::fromID($quiz_id); if ($quiz == null) { throw new Exception("Quiz ID passed was invalid. Cannot continue."); } return $quiz; }
/** * Shows Pass/Fail for a given class [Group] */ public function passfailAction() { $group = $this->_getParam("group"); $this->view->group = $group; // Pass ALL the groups to the view (to select) $all_groups = array(); $all_quizzes = Model_Quiz_Quiz::getAll(); foreach ($all_quizzes as $quiz) { $all_groups[] = $quiz->getPermissions_group(); } $all_groups = array_unique($all_groups); sort($all_groups); $this->view->all_groups = $all_groups; // If we've SELECTED a group... if (!is_null($group)) { $group = strtolower($group); // Group Members $members = Model_Auth_ActiveDirectory::getUsersFromGroup($group); $keyed_members = array(); foreach ($members as $member) { $username = $member; $member = Model_Auth_ActiveDirectory::getUserDetails($username); $member['username'] = strtolower($username); $keyed_members[$username] = $member; } // Find all Quizzes that are part of this group $all_quizzes = Model_Quiz_Quiz::getAll(); $valid_quizzes = array(); foreach ($all_quizzes as $quiz) { if (strtolower($quiz->getPermissions_group()) == $group) { $valid_quizzes[] = $quiz; } } // Now go and find all the results for each quiz $quiz_results = array(); // Key is the quiz ID foreach ($valid_quizzes as $quiz) { $set_result = array(); foreach ($keyed_members as $member) { //Did they pass? $highest_result = Model_Quiz_QuizAttempt::getHighestMarkQuiz($member['username'], $quiz); if (is_null($highest_result)) { $set_result[$member['username']] = "NA"; } else { if ($highest_result->getTotal_score() / $quiz->getTotalQuestions() * 100 >= $quiz->getPercentage_pass()) { $set_result[$member['username']] = "<span class='green'>P</span>"; } else { $set_result[$member['username']] = "<span class='red'>F</span>"; } } } $quiz_results[$quiz->getID()] = $set_result; } // Pass all info to the view $this->view->members = $keyed_members; $this->view->quizzes = $valid_quizzes; $this->view->quiz_results = $quiz_results; } }
function hasPendingPrerequisite($username) { $prereq = $this->getPrerequisite(); if (!$prereq) { return false; } $prereq_question = Model_Quiz_Quiz::fromID($prereq); // have the user attempted the prerequisites? $attempt = Model_Quiz_QuizAttempt::fromQuizAndUser($prereq, $username); if (!$attempt) { return true; //prerequisite has not even been attempted } // if yes, of all the attempts, has the best score passed the quiz requirement? if ($attempt->getDate_finished()) { $best_attempt = Model_Quiz_QuizAttempt::getHighestMarkQuiz($username, $prereq_question); return !$best_attempt->hasPassedQuiz(); } return true; /* if ($attempt->getDate_finished()==null){ $vQuizStatus = Model_Quiz_Quiz::QUIZ_INPROGRESS; if($vQuiz->getClose_date() < strtotime("now")){ echo "\t\t<td>In Progress (Late)</td>\n"; } else{ echo "\t\t<td>In Progress</td>\n"; } }else{ $vQuizStatus = Model_Quiz_Quiz::QUIZ_COMPLETED; echo "\t\t<td>Completed</td>\n"; } */ }
/** * This function Caches the local Usernames (and First/Last names) * from the designated Authentication source (eg. LDAP) */ public function syncusernamesAction() { $vGroups = array(); $vCounter = 0; //Get all the quizzes $vQuizzes = Model_Quiz_Quiz::getAll(); foreach ($vQuizzes as $vQuiz) { if (!in_array($vQuiz->getPermissions_group(), $vGroups)) { $vGroups[] = $vQuiz->getPermissions_group(); } } //So we have all groups now in the system foreach ($vGroups as $vGroup) { //Get the members of this group $vMembers = Model_Auth_ActiveDirectory::getUsersFromGroup($vGroup); if (is_array($vMembers) && sizeof($vMembers) > 0) { foreach ($vMembers as $vMember) { Model_Auth_ActiveDirectory::updateUser($vMember); $vCounter++; } } } $this->view->counter = $vCounter; }
function createQuiz($name, $permissions, $starts = false, $ends = false, $attempts = 50, $percentage = 100) { return Model_Quiz_Quiz::fromScratch($name, $permissions, $starts ?: date('Y-m-d', strtotime("-1 month")), $ends ?: date('Y-m-d', strtotime("+5 month")), $attempts, $percentage); }
/** * Shows the Hall of Fame for the Logged in User * * @return void * @author Ben Evans */ public function halloffameAction() { $this->view->title = "Hall of Fame"; $this->view->headTitle("Hall of Fame"); $identity = Zend_Auth::getInstance()->getIdentity(); $auth_model = Model_Auth_General::getAuthModel(); //Firstly, we have to get the quizzes that we have access to $vQuizzes = Model_Quiz_Quiz::getAll(true); $vAvailable = array(); /* Make sure you have permission */ foreach ($vQuizzes as $vQuiz) { if ($this->view->is_admin) { $vAvailable[] = $vQuiz; } else { if ($auth_model->userInGroup($identity->username, $vQuiz->getPermissions_group()) && $vQuiz->getOpen_date() <= strtotime("now")) { $vAvailable[] = $vQuiz; } } } /* Any Quizzes available? */ $quiz_rows = array(); if (sizeof($vAvailable) != 0) { foreach ($vAvailable as $vQuiz) { $quiz_row = array(); $quiz_row['name'] = $vQuiz->getName(); $vAttempts = $vQuiz->getQuizAttempts(); $vPassed = array(); if (sizeof($vAttempts) > 0) { foreach ($vAttempts as $vAttempt) { //Make sure we're only looking at people who've passed $vTotalScore = $vAttempt->getTotal_score(); //echo "Comparing " . $vTotalScore . "/". $vQuiz->getTotalQuestions() ." with " . $vQuiz->getPercentage_pass() . "% (".$vAttempt->getID().")<br/>"; if ($vTotalScore / $vQuiz->getTotalQuestions() * 100 >= $vQuiz->getPercentage_pass() && $vAttempt->getDate_finished() != null) { //echo "Added.<br/>"; $vPassed[] = $vAttempt; } } } //Did anyone pass? if (sizeof($vPassed) > 0) { //Sort results first usort($vPassed, "sort_by_score"); //Truncate the array if necessary if (sizeof($vPassed) > MAX_HALLOFFAME) { array_splice($vPassed, 0, MAX_HALLOFFAME); } // And now record the scores $quiz_row['scores'] = array(); //Now output the scores foreach ($vPassed as $vp) { $score_row = array("name" => $vp->getAd_user_cachesamaccountname(), "score" => $vp->getTotal_score(), "time" => $vp->getDate_finished() - $vp->getDate_started()); $quiz_row['scores'][] = $score_row; } } $quiz_rows[] = $quiz_row; } //End_foreach_quiz } $this->view->quiz_rows = $quiz_rows; }
public static function getAll($vOrder = false) { $db = Zend_Registry::get("db"); $vReturn = array(); $sql = "SELECT * FROM quiz"; if ($vOrder) { $sql .= " ORDER BY close_date"; } //echo "SQL: $sql<br/>"; $result = $db->query($sql); $rows = $result->fetchAll(); foreach ($rows as $row) { $vReturn[] = Model_Quiz_Quiz::fromID($row['quiz_id']); } return $vReturn; }