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 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 qbank_usage;
  18  
  19  use mod_quiz\quiz_attempt;
  20  
  21  /**
  22   * Tests for the data of question usage from differnet areas like helper or usage table.
  23   *
  24   * @package    qbank_usage
  25   * @copyright  2021 Catalyst IT Australia Pty Ltd
  26   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @coversDefaultClass \qbank_usage\tables\question_usage_table
  29   * @covers \qbank_usage_output_fragment_question_usage
  30   */
  31  class question_usage_test extends \advanced_testcase {
  32  
  33      /**
  34       * Test question usage data.
  35       */
  36      public function test_question_usage() {
  37          global $PAGE;
  38          $this->resetAfterTest(true);
  39          $layout = '1,2,0';
  40          // Make a user to do the quiz.
  41          $user = $this->getDataGenerator()->create_user();
  42          $course = $this->getDataGenerator()->create_course();
  43          // Make a quiz.
  44          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  45          $quiz = $quizgenerator->create_instance(['course' => $course->id,
  46              'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
  47  
  48          $quizobj = \mod_quiz\quiz_settings::create($quiz->id, $user->id);
  49  
  50          $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
  51          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
  52  
  53          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  54          $cat = $questiongenerator->create_question_category();
  55  
  56          $questions = [];
  57          $page = 1;
  58          foreach (explode(',', $layout) as $slot) {
  59              if ($slot == 0) {
  60                  $page += 1;
  61                  continue;
  62              }
  63  
  64              $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
  65              quiz_add_quiz_question($question->id, $quiz, $page);
  66              $questions[] = $question;
  67          }
  68  
  69          $timenow = time();
  70          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
  71          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
  72          quiz_attempt_save_started($quizobj, $quba, $attempt);
  73          $attemptdata = quiz_attempt::create($attempt->id);
  74  
  75          $this->setAdminUser();
  76          $PAGE->set_url(new \moodle_url('/'));
  77          foreach ($questions as $question) {
  78              $questionusagetable = qbank_usage_output_fragment_question_usage(['questionid' => $question->id]);
  79              // Test usage table contains the quiz data which was attempted.
  80              $this->assertStringContainsString($quiz->name, $questionusagetable);
  81  
  82              // Test usage table contains the course data where the quiz was attempted.
  83              $this->assertStringContainsString($course->fullname, $questionusagetable);
  84          }
  85      }
  86  }