public function test_update_question_dependency() { $quizobj = $this->create_test_quiz(array(array('TF1', 1, 'truefalse'), array('TF2', 1, 'truefalse'))); $structure = \mod_quiz\structure::create_for_quiz($quizobj); // Test adding a dependency. $slotid = $structure->get_slot_id_for_slot(2); $structure->update_question_dependency($slotid, true); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assertEquals(1, $structure->is_question_dependent_on_previous_slot(2)); // Test removing a dependency. $structure->update_question_dependency($slotid, false); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assertEquals(0, $structure->is_question_dependent_on_previous_slot(2)); }
/** * Get an instance of the {@link \mod_quiz\structure} class for this quiz. * @return \mod_quiz\structure describes the questions in the quiz. */ public function get_structure() { return \mod_quiz\structure::create_for_quiz($this); }
/** * Create a quiz, add five questions to the quiz * which are all on one page and return the quiz object. */ private function get_quiz_object() { global $SITE; $this->resetAfterTest(true); // Make a quiz. $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); $quiz = $quizgenerator->create_instance(array( 'course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2)); // Create five questions. $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $questiongenerator->create_question_category(); $shortanswer = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id)); $numerical = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); $essay = $questiongenerator->create_question('essay', null, array('category' => $cat->id)); $truefalse = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id)); $match = $questiongenerator->create_question('match', null, array('category' => $cat->id)); // Add them to the quiz. quiz_add_quiz_question($shortanswer->id, $quiz); quiz_add_quiz_question($numerical->id, $quiz); quiz_add_quiz_question($essay->id, $quiz); quiz_add_quiz_question($truefalse->id, $quiz); quiz_add_quiz_question($match->id, $quiz); // Return the quiz object. return \mod_quiz\structure::create_for($quiz); }
/** * HTML for a page, with ids stripped, so it can be used as a javascript template. * * @param structure $structure object containing the structure of the quiz. * @param \stdClass $quiz the quiz settings. * @return string HTML for a new icon */ protected function add_page_icon_template(structure $structure, $quiz) { if (!$structure->has_questions()) { return ''; } $question = $structure->get_question_in_slot(1); $html = $this->page_split_join_button($quiz, $question, true); return str_replace('&slot=1&', '&slot=%%SLOT%%&', $html); }
/** * Test updating pagebreaks in the quiz. */ public function test_update_page_break() { // Create a test quiz with 8 questions. list($quiz, $cm, $course) = $this->prepare_quiz_data(); $this->add_eight_questions_to_the_quiz($quiz); $quizobj = new quiz($quiz, $cm, $course); $structure = \mod_quiz\structure::create_for_quiz($quizobj); // Store the original order of slots, so we can assert what has changed. $originalslotids = array(); foreach ($structure->get_slots() as $slot) { $originalslotids[$slot->slot] = $slot->id; } // Test removing a page break. $slotid = $structure->get_question_in_slot(2)->slotid; $type = \mod_quiz\repaginate::LINK; $slots = $structure->update_page_break($quiz, $slotid, $type); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array($originalslotids[1] => 1, $originalslotids[2] => 1, $originalslotids[3] => 1, $originalslotids[4] => 1, $originalslotids[5] => 1, $originalslotids[6] => 1, $originalslotids[7] => 2, $originalslotids[8] => 3), $structure); // Test adding a page break. $slotid = $structure->get_question_in_slot(2)->slotid; $type = \mod_quiz\repaginate::UNLINK; $slots = $structure->update_page_break($quiz, $slotid, $type); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assert_quiz_layout(array($originalslotids[1] => 1, $originalslotids[2] => 2, $originalslotids[3] => 2, $originalslotids[4] => 2, $originalslotids[5] => 2, $originalslotids[6] => 2, $originalslotids[7] => 3, $originalslotids[8] => 4), $structure); }
/** * Test updating pagebreaks in the quiz. */ public function test_update_question_dependency() { // Create a test quiz with 8 questions. list($quiz, $cm, $course) = $this->prepare_quiz_data(); $this->add_eight_questions_to_the_quiz($quiz); $quizobj = new quiz($quiz, $cm, $course); $structure = \mod_quiz\structure::create_for_quiz($quizobj); // Store the original order of slots, so we can assert what has changed. $originalslotids = array(); foreach ($structure->get_slots() as $slot) { $originalslotids[$slot->slot] = $slot->id; } // Test adding a dependency. $slotid = $structure->get_slot_id_for_slot(3); $structure->update_question_dependency($slotid, true); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assertEquals(1, $structure->is_question_dependent_on_previous_slot(3)); // Test removing a dependency. $structure->update_question_dependency($slotid, false); // Having called update page break, we need to reload $structure. $structure = \mod_quiz\structure::create_for_quiz($quizobj); $this->assertEquals(0, $structure->is_question_dependent_on_previous_slot(3)); }
/** * Renders html to display a random question the link to edit the configuration * and also to see that category in the question bank. * * @param structure $structure object containing the structure of the quiz. * @param \stdClass $question data from the question and quiz_slots tables. * @param \moodle_url $pageurl the canonical URL of this page. * @return string HTML to output. */ public function random_question(structure $structure, $question, $pageurl) { $editurl = new \moodle_url('/question/question.php', array('returnurl' => $pageurl->out_as_local_url(), 'cmid' => $structure->get_cmid(), 'id' => $question->id)); $temp = clone $question; $temp->questiontext = ''; $instancename = quiz_question_tostring($temp); $configuretitle = get_string('configurerandomquestion', 'quiz'); $qtype = \question_bank::get_qtype($question->qtype, false); $namestr = $qtype->local_name(); $icon = $this->pix_icon('icon', $namestr, $qtype->plugin_name(), array('title' => $namestr, 'class' => 'icon activityicon', 'alt' => ' ', 'role' => 'presentation')); $editicon = $this->pix_icon('t/edit', $configuretitle, 'moodle', array('title' => '')); // If this is a random question, display a link to show the questions // selected from in the question bank. $qbankurl = new \moodle_url('/question/edit.php', array('cmid' => $structure->get_cmid(), 'cat' => $question->category . ',' . $question->contextid, 'recurse' => !empty($question->questiontext))); $qbanklink = ' ' . \html_writer::link($qbankurl, get_string('seequestions', 'quiz'), array('class' => 'mod_quiz_random_qbank_link')); return html_writer::link($editurl, $icon . $editicon, array('title' => $configuretitle)) . ' ' . $instancename . ' ' . $qbanklink; }