public function test_render_missing() { $records = new question_test_recordset(array(array('questionattemptid', 'contextid', 'questionusageid', 'slot', 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged', 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, '_order', '1,2,3'), array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 2, 1, 'complete', 0.5, 1256233705, 1, '-submit', '1'), array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 2, 1, 'complete', 0.5, 1256233705, 1, 'choice0', '1'))); $question = test_question_maker::make_question('truefalse', 'true'); $question->id = -1; question_bank::start_unit_test(); question_bank::load_test_question_data($question); $qa = question_attempt::load_from_records($records, 1, new question_usage_null_observer(), 'deferredfeedback'); question_bank::end_unit_test(); $this->assertEquals(2, $qa->get_num_steps()); $step = $qa->get_step(0); $this->assertEquals(question_state::$todo, $step->get_state()); $this->assertNull($step->get_fraction()); $this->assertEquals(1256233700, $step->get_timecreated()); $this->assertEquals(1, $step->get_user_id()); $this->assertEquals(array('_order' => '1,2,3'), $step->get_all_data()); $step = $qa->get_step(1); $this->assertEquals(question_state::$complete, $step->get_state()); $this->assertEquals(0.5, $step->get_fraction()); $this->assertEquals(1256233705, $step->get_timecreated()); $this->assertEquals(1, $step->get_user_id()); $this->assertEquals(array('-submit' => '1', 'choice0' => '1'), $step->get_all_data()); $output = $qa->render(new question_display_options(), '1'); $this->assertRegExp('/' . preg_quote($qa->get_question()->questiontext, '/') . '/', $output); $this->assertRegExp('/' . preg_quote(get_string('questionusedunknownmodel', 'qbehaviour_missing'), '/') . '/', $output); $this->assertTag(array('tag' => 'div', 'attributes' => array('class' => 'warning')), $output); }
public function test_load_with_unnecessary_autosaved_data() { // The point here is that the somehow (probably due to two things // happening concurrently, we have autosaved data in the database that // has already been superceded by real data, so it should be ignored. // There is also a second lot of redundant data to delete. $records = new question_test_recordset(array(array('questionattemptid', 'contextid', 'questionusageid', 'slot', 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged', 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 5, -2, 'complete', null, 1256233715, 1, 'answer', '0'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 4, -1, 'complete', null, 1256233715, 1, 'answer', '0'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 0, '', '', '', 1256233790, 2, 1, 'complete', null, 1256233705, 1, 'answer', '1'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 1.0, 1, '', '', '', 1256233790, 3, 2, 'complete', null, 1256233710, 1, 'answer', '0'))); $question = test_question_maker::make_question('truefalse', 'true'); $question->id = -1; question_bank::start_unit_test(); question_bank::load_test_question_data($question); $observer = new testable_question_engine_unit_of_work(question_engine::make_questions_usage_by_activity('unit_test', context_system::instance())); $qa = question_attempt::load_from_records($records, 1, $observer, 'deferredfeedback'); question_bank::end_unit_test(); $this->assertEquals($question->questiontext, $qa->get_question()->questiontext); $this->assertEquals(3, $qa->get_num_steps()); $this->assertFalse($qa->has_autosaved_step()); $step = $qa->get_step(0); $this->assertEquals(question_state::$todo, $step->get_state()); $this->assertNull($step->get_fraction()); $this->assertEquals(1256233700, $step->get_timecreated()); $this->assertEquals(1, $step->get_user_id()); $this->assertEquals(array(), $step->get_all_data()); $step = $qa->get_step(1); $this->assertEquals(question_state::$complete, $step->get_state()); $this->assertNull($step->get_fraction()); $this->assertEquals(1256233705, $step->get_timecreated()); $this->assertEquals(1, $step->get_user_id()); $this->assertEquals(array('answer' => '1'), $step->get_all_data()); $step = $qa->get_step(2); $this->assertEquals(question_state::$complete, $step->get_state()); $this->assertNull($step->get_fraction()); $this->assertEquals(1256233710, $step->get_timecreated()); $this->assertEquals(1, $step->get_user_id()); $this->assertEquals(array('answer' => '0'), $step->get_all_data()); $this->assertEquals(2, count($observer->get_steps_deleted())); }
/** * Load a {@link question_attempt} from the database, including all its * steps. * @param int $questionid the question to load all the attempts fors. * @param qubaid_condition $qubaids used to restrict which usages are included * in the query. See {@link qubaid_condition}. * @return array of question_attempts. */ public function load_attempts_at_question($questionid, qubaid_condition $qubaids) { $params = $qubaids->from_where_params(); $params['questionid'] = $questionid; $records = $this->db->get_recordset_sql("\nSELECT\n quba.contextid,\n quba.preferredbehaviour,\n qa.id AS questionattemptid,\n qa.questionusageid,\n qa.slot,\n qa.behaviour,\n qa.questionid,\n qa.variant,\n qa.maxmark,\n qa.minfraction,\n qa.maxfraction,\n qa.flagged,\n qa.questionsummary,\n qa.rightanswer,\n qa.responsesummary,\n qa.timemodified,\n qas.id AS attemptstepid,\n qas.sequencenumber,\n qas.state,\n qas.fraction,\n qas.timecreated,\n qas.userid,\n qasd.name,\n qasd.value\n\nFROM {$qubaids->from_question_attempts('qa')}\nJOIN {question_usages} quba ON quba.id = qa.questionusageid\nLEFT JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id\nLEFT JOIN {question_attempt_step_data} qasd ON qasd.attemptstepid = qas.id\n\nWHERE\n {$qubaids->where()} AND\n qa.questionid = :questionid\n\nORDER BY\n quba.id,\n qa.id,\n qas.sequencenumber\n ", $params); $questionattempts = array(); while ($records->valid()) { $record = $records->current(); $questionattempts[$record->questionattemptid] = question_attempt::load_from_records($records, $record->questionattemptid, new question_usage_null_observer(), $record->preferredbehaviour); } $records->close(); return $questionattempts; }
/** * Create a question_usage_by_activity from records loaded from the database. * * For internal use only. * * @param Iterator $records Raw records loaded from the database. * @param int $questionattemptid The id of the question_attempt to extract. * @return question_usage_by_activity The newly constructed usage. */ public static function load_from_records($records, $qubaid) { $record = $records->current(); while ($record->qubaid != $qubaid) { $records->next(); if (!$records->valid()) { throw new coding_exception("Question usage $qubaid not found in the database."); } $record = $records->current(); } $quba = new question_usage_by_activity($record->component, context::instance_by_id($record->contextid, IGNORE_MISSING)); $quba->set_id_from_database($record->qubaid); $quba->set_preferred_behaviour($record->preferredbehaviour); $quba->observer = new question_engine_unit_of_work($quba); // If slot is null then the current pointer in $records will not be // advanced in the while loop below, and we get stuck in an infinite loop, // since this method is supposed to always consume at least one record. // Therefore, in this case, advance the record here. if (is_null($record->slot)) { $records->next(); } while ($record && $record->qubaid == $qubaid && !is_null($record->slot)) { $quba->questionattempts[$record->slot] = question_attempt::load_from_records($records, $record->questionattemptid, $quba->observer, $quba->get_preferred_behaviour()); if ($records->valid()) { $record = $records->current(); } else { $record = false; } } return $quba; }
public function test_load_missing_question() { $records = new test_recordset(array(array('questionattemptid', 'contextid', 'questionusageid', 'slot', 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged', 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction', 'timecreated', 'userid', 'name', 'value'), array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0, 0.0, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null))); question_bank::start_unit_test(); $qa = question_attempt::load_from_records($records, 1, new question_usage_null_observer(), 'deferredfeedback'); question_bank::end_unit_test(); $missingq = question_bank::get_qtype('missingtype')->make_deleted_instance(-1, 2); $this->assertEqual($missingq, $qa->get_question()); $this->assertEqual(1, $qa->get_num_steps()); $step = $qa->get_step(0); $this->assertEqual(question_state::$todo, $step->get_state()); $this->assertNull($step->get_fraction()); $this->assertEqual(1256233700, $step->get_timecreated()); $this->assertEqual(1, $step->get_user_id()); $this->assertEqual(array(), $step->get_all_data()); }
/** * Create a question_usage_by_activity from records loaded from the database. * * For internal use only. * * @param Iterator $records Raw records loaded from the database. * @param int $questionattemptid The id of the question_attempt to extract. * @return question_usage_by_activity The newly constructed usage. */ public static function load_from_records($records, $qubaid) { $record = $records->current(); while ($record->qubaid != $qubaid) { $records->next(); if (!$records->valid()) { throw new coding_exception("Question usage {$qubaid} not found in the database."); } $record = $records->current(); } $quba = new offlinequiz_question_usage_by_activity($record->component, context::instance_by_id($record->contextid)); $quba->set_id_from_database($record->qubaid); $quba->set_preferred_behaviour($record->preferredbehaviour); $quba->observer = new question_engine_unit_of_work($quba); while ($record && $record->qubaid == $qubaid && !is_null($record->slot)) { $quba->questionattempts[$record->slot] = question_attempt::load_from_records($records, $record->questionattemptid, $quba->observer, $quba->get_preferred_behaviour()); if ($records->valid()) { $record = $records->current(); } else { $record = false; } } return $quba; }