Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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 mod_quiz;
  18  
  19  use mod_quiz\question\bank\qbank_helper;
  20  use mod_quiz\quiz_settings;
  21  
  22  /**
  23   * Test the restore of random question tags.
  24   *
  25   * @package    mod_quiz
  26   * @category   test
  27   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  28   * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class tags_test extends \advanced_testcase {
  32      public function test_restore_random_question_by_tag() {
  33          global $CFG, $USER, $DB;
  34  
  35          require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  36          require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  37  
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40  
  41          $backupid = 'abc';
  42          $backuppath = make_backup_temp_directory($backupid);
  43          get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
  44                  __DIR__ . "/fixtures/random_by_tag_quiz.mbz", $backuppath);
  45  
  46          // Do the restore to new course with default settings.
  47          $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
  48          $newcourseid = \restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid);
  49          $rc = new \restore_controller($backupid, $newcourseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id,
  50                  \backup::TARGET_NEW_COURSE);
  51  
  52          $this->assertTrue($rc->execute_precheck());
  53          $rc->execute_plan();
  54          $rc->destroy();
  55  
  56          // Get the information about the resulting course and check that it is set up correctly.
  57          $modinfo = get_fast_modinfo($newcourseid);
  58          $quiz = array_values($modinfo->get_instances_of('quiz'))[0];
  59          $quizobj = quiz_settings::create($quiz->instance);
  60          $structure = structure::create_for_quiz($quizobj);
  61  
  62          // Are the correct slots returned?
  63          $slots = $structure->get_slots();
  64          $this->assertCount(1, $slots);
  65  
  66          $quizobj->preload_questions();
  67          $quizobj->load_questions();
  68          $questions = $quizobj->get_questions();
  69  
  70          $this->assertCount(1, $questions);
  71  
  72          $question = array_values($questions)[0];
  73  
  74          $tag1 = \core_tag_tag::get_by_name(0, 't1', 'id, name');
  75          $this->assertNotFalse($tag1);
  76  
  77          $tag2 = \core_tag_tag::get_by_name(0, 't2', 'id, name');
  78          $this->assertNotFalse($tag2);
  79  
  80          $tag3 = \core_tag_tag::get_by_name(0, 't3', 'id, name');
  81          $this->assertNotFalse($tag3);
  82  
  83          $slottags = $this->get_tags_for_slot($question->slotid);
  84          $slottags = reset($slottags);
  85          $slottags = explode(',', $slottags);
  86          $this->assertEquals("{$tag2->id},{$tag2->name}", "{$slottags[0]},{$slottags[1]}");
  87  
  88          $defaultcategory = question_get_default_category(\context_course::instance($newcourseid)->id);
  89          $this->assertEquals($defaultcategory->id, $question->category);
  90          $randomincludingsubcategories = $DB->get_record('question_set_references',
  91              ['itemid' => reset($slots)->id, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
  92          $filtercondition = json_decode($randomincludingsubcategories->filtercondition);
  93          $this->assertEquals(0, $filtercondition->includingsubcategories);
  94      }
  95  
  96      /**
  97       * Helper to get the random tags, if any, for a slot.
  98       *
  99       * @param int $slotid the id of the slot to get tags for.
 100       * @return array the tags.
 101       */
 102      protected function get_tags_for_slot(int $slotid): array {
 103          global $DB;
 104          $referencedata = $DB->get_record('question_set_references',
 105                  ['itemid' => $slotid, 'component' => 'mod_quiz', 'questionarea' => 'slot']);
 106          if (isset($referencedata->filtercondition)) {
 107              $filtercondition = json_decode($referencedata->filtercondition);
 108              if (isset($filtercondition->tags)) {
 109                  return $filtercondition->tags;
 110              }
 111          }
 112          return [];
 113      }
 114  }