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