/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @param object $quiz the data that came from the form. * @return mixed true on success, false or a string error message on failure. */ function quiz_update_instance($quiz, $mform) { global $CFG, $DB; require_once $CFG->dirroot . '/mod/quiz/locallib.php'; // Process the options from the form. $result = quiz_process_options($quiz); if ($result && is_string($result)) { return $result; } // Get the current value, so we can see what changed. $oldquiz = $DB->get_record('quiz', array('id' => $quiz->instance)); // We need two values from the existing DB record that are not in the form, // in some of the function calls below. $quiz->sumgrades = $oldquiz->sumgrades; $quiz->grade = $oldquiz->grade; // Update the database. $quiz->id = $quiz->instance; $DB->update_record('quiz', $quiz); // Do the processing required after an add or an update. quiz_after_add_or_update($quiz); if ($oldquiz->grademethod != $quiz->grademethod) { quiz_update_all_final_grades($quiz); quiz_update_grades($quiz); } $quizdateschanged = $oldquiz->timelimit != $quiz->timelimit || $oldquiz->timeclose != $quiz->timeclose || $oldquiz->graceperiod != $quiz->graceperiod; if ($quizdateschanged) { quiz_update_open_attempts(array('quizid' => $quiz->id)); } // Delete any previous preview attempts. quiz_delete_previews($quiz); // Repaginate, if asked to. if (!empty($quiz->repaginatenow)) { quiz_repaginate_questions($quiz->id, $quiz->questionsperpage); } return true; }
/** * Test get_attempt_data */ public function test_get_attempt_data() { global $DB; // Create a new quiz with one attempt started. list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true); $quizobj = $attemptobj->get_quizobj(); $quizobj->preload_questions(); $quizobj->load_questions(); $questions = $quizobj->get_questions(); $this->setUser($this->student); // We receive one question per page. $result = mod_quiz_external::get_attempt_data($attempt->id, 0); $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result); $this->assertEquals($attempt, (object) $result['attempt']); $this->assertEquals(1, $result['nextpage']); $this->assertCount(0, $result['messages']); $this->assertCount(1, $result['questions']); $this->assertEquals(1, $result['questions'][0]['slot']); $this->assertEquals(1, $result['questions'][0]['number']); $this->assertEquals('numerical', $result['questions'][0]['type']); $this->assertEquals('todo', $result['questions'][0]['state']); $this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']); $this->assertFalse($result['questions'][0]['flagged']); $this->assertEquals(0, $result['questions'][0]['page']); $this->assertEmpty($result['questions'][0]['mark']); $this->assertEquals(1, $result['questions'][0]['maxmark']); // Now try the last page. $result = mod_quiz_external::get_attempt_data($attempt->id, 1); $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result); $this->assertEquals($attempt, (object) $result['attempt']); $this->assertEquals(-1, $result['nextpage']); $this->assertCount(0, $result['messages']); $this->assertCount(1, $result['questions']); $this->assertEquals(2, $result['questions'][0]['slot']); $this->assertEquals(2, $result['questions'][0]['number']); $this->assertEquals('numerical', $result['questions'][0]['type']); $this->assertEquals('todo', $result['questions'][0]['state']); $this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']); $this->assertFalse($result['questions'][0]['flagged']); $this->assertEquals(1, $result['questions'][0]['page']); // Finish previous attempt. $attemptobj->process_finish(time(), false); // Change setting and expect two pages. $quiz->questionsperpage = 4; $DB->update_record('quiz', $quiz); quiz_repaginate_questions($quiz->id, $quiz->questionsperpage); // Start with new attempt with the new layout. $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); $timenow = time(); $attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow); quiz_attempt_save_started($quizobj, $quba, $attempt); // We receive two questions per page. $result = mod_quiz_external::get_attempt_data($attempt->id, 0); $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result); $this->assertCount(2, $result['questions']); $this->assertEquals(-1, $result['nextpage']); // Check questions looks good. $found = 0; foreach ($questions as $question) { foreach ($result['questions'] as $rquestion) { if ($rquestion['slot'] == $question->slot) { $this->assertTrue(strpos($rquestion['html'], "qid={$question->id}") !== false); $found++; } } } $this->assertEquals(2, $found); }
$selectedslots = array(); $params = (array) data_submitted(); foreach ($params as $key => $value) { if (preg_match('!^s([0-9]+)$!', $key, $matches)) { $selectedslots[] = $matches[1]; } } $afteractionurl = new moodle_url($thispageurl); if ($scrollpos) { $afteractionurl->param('scrollpos', $scrollpos); } if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) { // Re-paginate the quiz. $structure->check_can_be_edited(); $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT); quiz_repaginate_questions($quiz->id, $questionsperpage); quiz_delete_previews($quiz); redirect($afteractionurl); } if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) { // Add a single question to the current quiz. $structure->check_can_be_edited(); quiz_require_question_use($addquestion); $addonpage = optional_param('addonpage', 0, PARAM_INT); quiz_add_quiz_question($addquestion, $quiz, $addonpage); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); $thispageurl->param('lastchanged', $addquestion); redirect($afteractionurl); } if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {