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