Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core_backup;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  // Include all the needed stuff.
  22  global $CFG;
  23  require_once($CFG->dirroot . '/course/lib.php');
  24  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  
  27  /**
  28   * Decode links quiz restore tests.
  29   *
  30   * @package    core_backup
  31   * @copyright  2020 Ilya Tregubov <mattp@catalyst-au.net>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class quiz_restore_decode_links_test extends \advanced_testcase {
  35  
  36      /**
  37       * Test restore_decode_rule class
  38       */
  39      public function test_restore_quiz_decode_links() {
  40          global $DB, $CFG, $USER;
  41  
  42          $this->resetAfterTest(true);
  43          $this->setAdminUser();
  44  
  45          $generator = $this->getDataGenerator();
  46          $course = $generator->create_course(
  47              array('format' => 'topics', 'numsections' => 3,
  48                  'enablecompletion' => COMPLETION_ENABLED),
  49              array('createsections' => true));
  50          $quiz = $generator->create_module('quiz', array(
  51              'course' => $course->id));
  52  
  53          // Create questions.
  54  
  55          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56          $context = \context_course::instance($course->id);
  57          $cat = $questiongenerator->create_question_category(array('contextid' => $context->id));
  58          $question = $questiongenerator->create_question('multichoice', null, array('category' => $cat->id));
  59  
  60          // Add to the quiz.
  61          quiz_add_quiz_question($question->id, $quiz);
  62          \mod_quiz\external\submit_question_version::execute(
  63                  $DB->get_field('quiz_slots', 'id', ['quizid' => $quiz->id, 'slot' => 1]), 1);
  64  
  65          $questiondata = \question_bank::load_question_data($question->id);
  66  
  67          $firstanswer = array_shift($questiondata->options->answers);
  68          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/course/view.php?id=' . $course->id,
  69              ['id' => $firstanswer->id]);
  70  
  71          $secondanswer = array_shift($questiondata->options->answers);
  72          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid,
  73              ['id' => $secondanswer->id]);
  74  
  75          $thirdanswer = array_shift($questiondata->options->answers);
  76          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid,
  77              ['id' => $thirdanswer->id]);
  78  
  79          $fourthanswer = array_shift($questiondata->options->answers);
  80          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid,
  81              ['id' => $fourthanswer->id]);
  82  
  83          $newcm = duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid));
  84  
  85          $quizquestions = \mod_quiz\question\bank\qbank_helper::get_question_structure(
  86                  $newcm->instance, \context_module::instance($newcm->id));
  87          $questionids = [];
  88          foreach ($quizquestions as $quizquestion) {
  89              if ($quizquestion->questionid) {
  90                  $questionids[] = $quizquestion->questionid;
  91              }
  92          }
  93          list($condition, $param) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'questionid');
  94          $condition = 'WHERE qa.question ' . $condition;
  95  
  96          $sql = "SELECT qa.id,
  97                         qa.answer
  98                    FROM {question_answers} qa
  99                    $condition";
 100          $answers = $DB->get_records_sql($sql, $param);
 101  
 102          $this->assertEquals($CFG->wwwroot . '/course/view.php?id=' . $course->id, $answers[$firstanswer->id]->answer);
 103          $this->assertEquals($CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, $answers[$secondanswer->id]->answer);
 104          $this->assertEquals($CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid, $answers[$thirdanswer->id]->answer);
 105          $this->assertEquals($CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid, $answers[$fourthanswer->id]->answer);
 106      }
 107  }