Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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   * Helper test.
  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\helper
  29   */
  30  class helper_test extends \advanced_testcase {
  31  
  32      /**
  33       * @var \stdClass $quiz
  34       */
  35      protected $quiz;
  36  
  37      /**
  38       * @var array $questions
  39       */
  40      protected $questions = [];
  41  
  42      /**
  43       * Test setup.
  44       */
  45      public function setup(): void {
  46          $this->resetAfterTest();
  47          $layout = '1,2,0';
  48          // Make a user to do the quiz.
  49          $user = $this->getDataGenerator()->create_user();
  50          $course = $this->getDataGenerator()->create_course();
  51          // Make a quiz.
  52          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  53          $this->quiz = $quizgenerator->create_instance(['course' => $course->id,
  54              'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
  55  
  56          $quizobj = \mod_quiz\quiz_settings::create($this->quiz->id, $user->id);
  57  
  58          $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
  59          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
  60  
  61          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  62          $cat = $questiongenerator->create_question_category();
  63  
  64          $page = 1;
  65          foreach (explode(',', $layout) as $slot) {
  66              if ($slot == 0) {
  67                  $page += 1;
  68                  continue;
  69              }
  70  
  71              $question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
  72              quiz_add_quiz_question($question->id, $this->quiz, $page);
  73              $this->questions [] = $question;
  74          }
  75  
  76          $timenow = time();
  77          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
  78          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
  79          quiz_attempt_save_started($quizobj, $quba, $attempt);
  80          quiz_attempt::create($attempt->id);
  81      }
  82  
  83      /**
  84       * Test question attempt count.
  85       *
  86       * @covers ::get_question_attempts_count_in_quiz
  87       */
  88      public function test_get_question_attempts_count_in_quiz() {
  89          foreach ($this->questions as $question) {
  90              $questionattemptcount = helper::get_question_attempts_count_in_quiz($question->id, $this->quiz->id);
  91              // Test the attempt count matches the usage count, each question should have one count.
  92              $this->assertEquals(1, $questionattemptcount);
  93          }
  94      }
  95  
  96      /**
  97       * Test test usage data.
  98       *
  99       * @covers ::get_question_entry_usage_count
 100       */
 101      public function test_get_question_entry_usage_count() {
 102          foreach ($this->questions as $question) {
 103              $count = helper::get_question_entry_usage_count(\question_bank::load_question($question->id));
 104              // Test that the attempt data matches the usage data for the count.
 105              $this->assertEquals(1, $count);
 106          }
 107      }
 108  }