From 37d7820816fa96edd4f5738064b3f3bb930bda99 Mon Sep 17 00:00:00 2001 From: Ilya Tregubov Date: Tue, 24 Mar 2020 09:36:57 +1100 Subject: [PATCH] MDL-68203 restore: Decode quiz links when restore into existing course or duplicating. --- backup/moodle2/restore_qtype_plugin.class.php | 14 +++ .../tests/quiz_restore_decode_links_test.php | 97 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 backup/tests/quiz_restore_decode_links_test.php diff --git a/backup/moodle2/restore_qtype_plugin.class.php b/backup/moodle2/restore_qtype_plugin.class.php index 7520aa31910..49bd8a18e30 100644 --- a/backup/moodle2/restore_qtype_plugin.class.php +++ b/backup/moodle2/restore_qtype_plugin.class.php @@ -175,6 +175,20 @@ abstract class restore_qtype_plugin extends restore_plugin { } } + $rules = restore_course_task::define_decode_rules(); + $rulesactivity = restore_quiz_activity_task::define_decode_rules(); + $rules = array_merge($rules, $rulesactivity); + + $decoder = $this->task->get_decoder(); + foreach ($rules as $rule) { + $decoder->add_rule($rule); + } + + $contentdecoded = $decoder->decode_content($data->answertext); + if ($contentdecoded) { + $data->answertext = $contentdecoded; + } + if (!isset($this->questionanswercache[$data->answertext])) { // If we haven't found the matching answer, something has gone really wrong, the question in the DB // is missing answers, throw an exception. diff --git a/backup/tests/quiz_restore_decode_links_test.php b/backup/tests/quiz_restore_decode_links_test.php new file mode 100644 index 00000000000..19a142d4077 --- /dev/null +++ b/backup/tests/quiz_restore_decode_links_test.php @@ -0,0 +1,97 @@ +. + +/** + * Decode links quiz restore tests. + * + * @package core_backup + * @copyright 2020 Ilya Tregubov + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff. +global $CFG; +require_once($CFG->dirroot . '/course/lib.php'); +require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); +require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); + +/** + * restore_decode tests (both rule and content) + */ +class restore_quiz_decode_testcase extends \core_privacy\tests\provider_testcase { + + /** + * Test restore_decode_rule class + */ + public function test_restore_quiz_decode_links() { + global $DB, $CFG, $USER; + + $this->resetAfterTest(true); + $this->setAdminUser(); + + $generator = $this->getDataGenerator(); + $course = $generator->create_course( + array('format' => 'topics', 'numsections' => 3, + 'enablecompletion' => COMPLETION_ENABLED), + array('createsections' => true)); + $quiz = $generator->create_module('quiz', array( + 'course' => $course->id)); + + // Create questions. + + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $context = context_course::instance($course->id); + $cat = $questiongenerator->create_question_category(array('contextid' => $context->id)); + $question = $questiongenerator->create_question('multichoice', null, array('category' => $cat->id)); + + // Add to the quiz. + quiz_add_quiz_question($question->id, $quiz); + + $questiondata = question_bank::load_question_data($question->id); + + $firstanswer = array_shift($questiondata->options->answers); + $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/course/view.php?id=' . $course->id, + ['id' => $firstanswer->id]); + + $secondanswer = array_shift($questiondata->options->answers); + $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, + ['id' => $secondanswer->id]); + + $thirdanswer = array_shift($questiondata->options->answers); + $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid, + ['id' => $thirdanswer->id]); + + $fourthanswer = array_shift($questiondata->options->answers); + $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid, + ['id' => $fourthanswer->id]); + + $newcm = duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid)); + + $sql = "SELECT qa.answer + FROM {quiz} q + LEFT JOIN {quiz_slots} qs ON qs.quizid = q.id + LEFT JOIN {question_answers} qa ON qa.question = qs.questionid + WHERE q.id = :quizid"; + $params = array('quizid' => $newcm->instance); + $answers = $DB->get_fieldset_sql($sql, $params); + $this->assertEquals($CFG->wwwroot . '/course/view.php?id=' . $course->id, $answers[0]); + $this->assertEquals($CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, $answers[1]); + $this->assertEquals($CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid, $answers[2]); + $this->assertEquals($CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid, $answers[3]); + } +}