public function test_random_questions_not_returned() { $this->resetAfterTest(); $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(); $course = $this->getDataGenerator()->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course)); quiz_add_random_questions($quiz, 1, $cat->id, 1, false); $loader = new \core_question\bank\random_question_loader(new qubaid_list(array())); $this->assertNull($loader->get_next_question_id($cat->id, 0)); }
$key = $matches[1]; quiz_require_question_use($key); quiz_add_quiz_question($key, $quiz); } } quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); redirect($afteractionurl); } if (optional_param('addrandom', false, PARAM_BOOL) && confirm_sesskey()) { // Add random questions to the quiz. $recurse = optional_param('recurse', 0, PARAM_BOOL); $addonpage = optional_param('addonpage', 0, PARAM_INT); $categoryid = required_param('categoryid', PARAM_INT); $randomcount = required_param('randomcount', PARAM_INT); quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); redirect($afteractionurl); } if (optional_param('addnewpagesafterselected', null, PARAM_CLEAN) && !empty($selectedquestionids) && confirm_sesskey()) { foreach ($selectedquestionids as $questionid) { $quiz->questions = quiz_add_page_break_after($quiz->questions, $questionid); } $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id)); quiz_delete_previews($quiz); redirect($afteractionurl); } $addpage = optional_param('addpage', false, PARAM_INT); if ($addpage !== false && confirm_sesskey()) { $quiz->questions = quiz_add_page_break_at($quiz->questions, $addpage);
/** * Test deleting a quiz instance. */ public function test_quiz_delete_instance() { global $SITE, $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup a quiz with 1 standard and 1 random question. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); quiz_add_quiz_question($standardq->id, $quiz); quiz_add_random_questions($quiz, 0, $cat->id, 1, false); // Get the random question. $randomq = $DB->get_record('question', array('qtype' => 'random')); quiz_delete_instance($quiz->id); // Check that the random question was deleted. $count = $DB->count_records('question', array('id' => $randomq->id)); $this->assertEquals(0, $count); // Check that the standard question was not deleted. $count = $DB->count_records('question', array('id' => $standardq->id)); $this->assertEquals(1, $count); // Check that all the slots were removed. $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id)); $this->assertEquals(0, $count); // Check that the quiz was removed. $count = $DB->count_records('quiz', array('id' => $quiz->id)); $this->assertEquals(0, $count); }
if (!empty($data->existingcategory)) { list($categoryid) = explode(',', $data->category); $includesubcategories = !empty($data->includesubcategories); $returnurl->param('cat', $data->category); } else { if (!empty($data->newcategory)) { list($parentid, $contextid) = explode(',', $data->parent); $categoryid = $qcobject->add_category($data->parent, $data->name, '', true); $includesubcategories = 0; add_to_log($quiz->course, 'quiz', 'addcategory', 'view.php?id=' . $cm->id, $categoryid, $cm->id); $returnurl->param('cat', $categoryid . ',' . $contextid); } else { throw new coding_exception('It seems a form was submitted without any button being pressed???'); } } quiz_add_random_questions($quiz, $addonpage, $categoryid, 1, $includesubcategories); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); redirect($returnurl); } $mform->set_data(array('addonpage' => $addonpage, 'returnurl' => $returnurl, 'cmid' => $cm->id, 'category' => $category)); // Setup $PAGE. $streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz')); $PAGE->navbar->add($streditingquiz); $PAGE->set_title($streditingquiz); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); if (!($quizname = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance)))) { print_error('invalidcoursemodule'); } echo $OUTPUT->heading(get_string('addrandomquestiontoquiz', 'quiz', $quizname), 2);
if ($data = $mform->get_data()) { if (!empty($data->existingcategory)) { list($categoryid) = explode(',', $data->category); $includesubcategories = !empty($data->includesubcategories); $returnurl->param('cat', $data->category); } else { if (!empty($data->newcategory)) { list($parentid, $contextid) = explode(',', $data->parent); $categoryid = $qcobject->add_category($data->parent, $data->name, '', true); $includesubcategories = 0; $returnurl->param('cat', $categoryid . ',' . $contextid); } else { throw new coding_exception('It seems a form was submitted without any button being pressed???'); } } quiz_add_random_questions($quiz, $addonpage, $categoryid, $data->numbertoadd, $includesubcategories); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); redirect($returnurl); } $mform->set_data(array('addonpage' => $addonpage, 'returnurl' => $returnurl, 'cmid' => $cm->id, 'category' => $category)); // Setup $PAGE. $streditingquiz = get_string('editinga', 'moodle', get_string('modulename', 'quiz')); $PAGE->navbar->add($streditingquiz); $PAGE->set_title($streditingquiz); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); if (!($quizname = $DB->get_field($cm->modname, 'name', array('id' => $cm->instance)))) { print_error('invalidcoursemodule'); } echo $OUTPUT->heading(get_string('addrandomquestiontoquiz', 'quiz', $quizname), 2);
public function test_quiz_removing_a_random_question_deletes_the_question() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $quizobj = $this->create_test_quiz(array(array('TF1', 1, 'truefalse'))); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); quiz_add_random_questions($quizobj->get_quiz(), 1, $cat->id, 1, false); $structure = \mod_quiz\structure::create_for_quiz($quizobj); $randomq = $DB->get_record('question', array('qtype' => 'random')); $structure->remove_slot(2); $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array(array('TF1', 1, 'truefalse')), $structure); $this->assertFalse($DB->record_exists('question', array('id' => $randomq->id))); }
/** * Create a quiz with a random as well as other questions and walk through quiz attempts. */ public function test_quiz_with_random_question_attempt_walkthrough() { global $SITE; $this->resetAfterTest(true); question_bank::get_qtype('random')->clear_caches_before_testing(); $this->setAdminUser(); // Make a quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 2, 'grade' => 100.0, 'sumgrades' => 4)); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); // Add two questions to question category. $cat = $questiongenerator->create_question_category(); $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); // Add random question to the quiz. quiz_add_random_questions($quiz, 0, $cat->id, 1, false); // Make another category. $cat2 = $questiongenerator->create_question_category(); $match = $questiongenerator->create_question('match', null, array('category' => $cat->id)); quiz_add_quiz_question($match->id, $quiz, 0); $multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', array('category' => $cat->id)); quiz_add_quiz_question($multichoicemulti->id, $quiz, 0); $multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', array('category' => $cat->id)); quiz_add_quiz_question($multichoicesingle->id, $quiz, 0); foreach (array($saq->id => 'frog', $numq->id => '3.14') as $randomqidtoselect => $randqanswer) { // Make a new user to do the quiz each loop. $user1 = $this->getDataGenerator()->create_user(); $this->setUser($user1); $quizobj = quiz::create($quiz->id, $user1->id); // Start the attempt. $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, 1, false, $timenow); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow, array(1 => $randomqidtoselect)); $this->assertEquals('1,2,0,3,4,0', $attempt->layout); quiz_attempt_save_started($quizobj, $quba, $attempt); // Process some responses from the student. $attemptobj = quiz_attempt::create($attempt->id); $tosubmit = array(); $selectedquestionid = $quba->get_question_attempt(1)->get_question()->id; $tosubmit[1] = array('answer' => $randqanswer); $tosubmit[2] = array( 'frog' => 'amphibian', 'cat' => 'mammal', 'newt' => 'amphibian'); $tosubmit[3] = array('One' => '1', 'Two' => '0', 'Three' => '1', 'Four' => '0'); // First and third choice. $tosubmit[4] = array('answer' => 'One'); // The first choice. $attemptobj->process_submitted_actions($timenow, false, $tosubmit); // Finish the attempt. $attemptobj = quiz_attempt::create($attempt->id); $attemptobj->process_finish($timenow, false); // Re-load quiz attempt data. $attemptobj = quiz_attempt::create($attempt->id); // Check that results are stored as expected. $this->assertEquals(1, $attemptobj->get_attempt_number()); $this->assertEquals(4, $attemptobj->get_sum_marks()); $this->assertEquals(true, $attemptobj->is_finished()); $this->assertEquals($timenow, $attemptobj->get_submitted_date()); $this->assertEquals($user1->id, $attemptobj->get_userid()); // Check quiz grades. $grades = quiz_get_user_grades($quiz, $user1->id); $grade = array_shift($grades); $this->assertEquals(100.0, $grade->rawgrade); // Check grade book. $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id); $gradebookitem = array_shift($gradebookgrades->items); $gradebookgrade = array_shift($gradebookitem->grades); $this->assertEquals(100, $gradebookgrade->grade); } }
/** * Test removing slots from a quiz. */ public function test_quiz_remove_slot() { global $SITE, $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Setup a quiz with 1 standard and 1 random question. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); $cm = get_coursemodule_from_instance('quiz', $quiz->id, $SITE->id); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); quiz_add_quiz_question($standardq->id, $quiz); quiz_add_random_questions($quiz, 0, $cat->id, 1, false); // Get the random question. $randomq = $DB->get_record('question', array('qtype' => 'random')); $quizobj = new quiz($quiz, $cm, $SITE); $structure = \mod_quiz\structure::create_for_quiz($quizobj); // Check that the setup looks right. $this->assertEquals(2, $structure->get_question_count()); $this->assertEquals($standardq->id, $structure->get_question_in_slot(1)->questionid); $this->assertEquals($randomq->id, $structure->get_question_in_slot(2)->questionid); // Remove the standard question. $structure->remove_slot($quiz, 1); $alteredstructure = \mod_quiz\structure::create_for_quiz($quizobj); // Check the new ordering, and that the slot number was updated. $this->assertEquals(1, $alteredstructure->get_question_count()); $this->assertEquals($randomq->id, $alteredstructure->get_question_in_slot(1)->questionid); // Check that the ordinary question was not deleted. $this->assertTrue($DB->record_exists('question', array('id' => $standardq->id))); // Remove the random question. $structure->remove_slot($quiz, 1); $alteredstructure = \mod_quiz\structure::create_for_quiz($quizobj); // Check that new ordering. $this->assertEquals(0, $alteredstructure->get_question_count()); // Check that the random question was deleted. $this->assertFalse($DB->record_exists('question', array('id' => $randomq->id))); }
public function create_quiz($quizsettings, $qs) { global $SITE, $DB; $this->setAdminUser(); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $slots = array(); $qidsbycat = array(); $sumofgrades = 0; for ($rowno = 0; $rowno < $qs->getRowCount(); $rowno++) { $q = $this->explode_dot_separated_keys_to_make_subindexs($qs->getRow($rowno)); $catname = array('name' => $q['cat']); if (!($cat = $DB->get_record('question_categories', array('name' => $q['cat'])))) { $cat = $questiongenerator->create_question_category($catname); } $q['catid'] = $cat->id; foreach (array('which' => null, 'overrides' => array()) as $key => $default) { if (empty($q[$key])) { $q[$key] = $default; } } if ($q['type'] !== 'random') { // Don't actually create random questions here. $overrides = array('category' => $cat->id, 'defaultmark' => $q['mark']) + $q['overrides']; $question = $questiongenerator->create_question($q['type'], $q['which'], $overrides); $q['id'] = $question->id; if (!isset($qidsbycat[$q['cat']])) { $qidsbycat[$q['cat']] = array(); } if (!empty($q['which'])) { $name = $q['type'] . '_' . $q['which']; } else { $name = $q['type']; } $qidsbycat[$q['catid']][$name] = $q['id']; } if (!empty($q['slot'])) { $slots[$q['slot']] = $q; $sumofgrades += $q['mark']; } } ksort($slots); // Make a quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); // Settings from param override defaults. $aggregratedsettings = $quizsettings + array('course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => $sumofgrades); $this->quiz = $quizgenerator->create_instance($aggregratedsettings); $this->randqids = array(); foreach ($slots as $slotno => $slotquestion) { if ($slotquestion['type'] !== 'random') { quiz_add_quiz_question($slotquestion['id'], $this->quiz, 0, $slotquestion['mark']); } else { quiz_add_random_questions($this->quiz, 0, $slotquestion['catid'], 1, 0); $this->randqids[$slotno] = $qidsbycat[$slotquestion['catid']]; } } }