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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [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 mod_quiz;
  18  
  19  use quiz_attempt;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/mod/quiz/attemptlib.php');
  25  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  26  
  27  /**
  28   * This class contains the test cases for the functions in reportlib.php.
  29   *
  30   * @package   mod_quiz
  31   * @category  test
  32   * @copyright 2008 Jamie Pratt me@jamiep.org
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  34   */
  35  class reportlib_test extends \advanced_testcase {
  36      public function test_quiz_report_index_by_keys() {
  37          $datum = array();
  38          $object = new \stdClass();
  39          $object->qid = 3;
  40          $object->aid = 101;
  41          $object->response = '';
  42          $object->grade = 3;
  43          $datum[] = $object;
  44  
  45          $indexed = quiz_report_index_by_keys($datum, array('aid', 'qid'));
  46  
  47          $this->assertEquals($indexed[101][3]->qid, 3);
  48          $this->assertEquals($indexed[101][3]->aid, 101);
  49          $this->assertEquals($indexed[101][3]->response, '');
  50          $this->assertEquals($indexed[101][3]->grade, 3);
  51  
  52          $indexed = quiz_report_index_by_keys($datum, array('aid', 'qid'), false);
  53  
  54          $this->assertEquals($indexed[101][3][0]->qid, 3);
  55          $this->assertEquals($indexed[101][3][0]->aid, 101);
  56          $this->assertEquals($indexed[101][3][0]->response, '');
  57          $this->assertEquals($indexed[101][3][0]->grade, 3);
  58      }
  59  
  60      public function test_quiz_report_scale_summarks_as_percentage() {
  61          $quiz = new \stdClass();
  62          $quiz->sumgrades = 10;
  63          $quiz->decimalpoints = 2;
  64  
  65          $this->assertEquals('12.34567%',
  66              quiz_report_scale_summarks_as_percentage(1.234567, $quiz, false));
  67          $this->assertEquals('12.35%',
  68              quiz_report_scale_summarks_as_percentage(1.234567, $quiz, true));
  69          $this->assertEquals('-',
  70              quiz_report_scale_summarks_as_percentage('-', $quiz, true));
  71      }
  72  
  73      public function test_quiz_report_qm_filter_select_only_one_attempt_allowed() {
  74          $quiz = new \stdClass();
  75          $quiz->attempts = 1;
  76          $this->assertSame('', quiz_report_qm_filter_select($quiz));
  77      }
  78  
  79      public function test_quiz_report_qm_filter_select_average() {
  80          $quiz = new \stdClass();
  81          $quiz->attempts = 10;
  82          $quiz->grademethod = QUIZ_GRADEAVERAGE;
  83          $this->assertSame('', quiz_report_qm_filter_select($quiz));
  84      }
  85  
  86      public function test_quiz_report_qm_filter_select_first_last_best() {
  87          global $DB;
  88          $this->resetAfterTest();
  89  
  90          $fakeattempt = new \stdClass();
  91          $fakeattempt->userid = 123;
  92          $fakeattempt->quiz = 456;
  93          $fakeattempt->layout = '1,2,0,3,4,0,5';
  94          $fakeattempt->state = quiz_attempt::FINISHED;
  95  
  96          // We intentionally insert these in a funny order, to test the SQL better.
  97          // The test data is:
  98          // id | quizid | user | attempt | sumgrades | state
  99          // ---------------------------------------------------
 100          // 4  | 456    | 123  | 1       | 30        | finished
 101          // 2  | 456    | 123  | 2       | 50        | finished
 102          // 1  | 456    | 123  | 3       | 50        | finished
 103          // 3  | 456    | 123  | 4       | null      | inprogress
 104          // 5  | 456    | 1    | 1       | 100       | finished
 105          // layout is only given because it has a not-null constraint.
 106          // uniqueid values are meaningless, but that column has a unique constraint.
 107  
 108          $fakeattempt->attempt = 3;
 109          $fakeattempt->sumgrades = 50;
 110          $fakeattempt->uniqueid = 13;
 111          $DB->insert_record('quiz_attempts', $fakeattempt);
 112  
 113          $fakeattempt->attempt = 2;
 114          $fakeattempt->sumgrades = 50;
 115          $fakeattempt->uniqueid = 26;
 116          $DB->insert_record('quiz_attempts', $fakeattempt);
 117  
 118          $fakeattempt->attempt = 4;
 119          $fakeattempt->sumgrades = null;
 120          $fakeattempt->uniqueid = 39;
 121          $fakeattempt->state = quiz_attempt::IN_PROGRESS;
 122          $DB->insert_record('quiz_attempts', $fakeattempt);
 123  
 124          $fakeattempt->attempt = 1;
 125          $fakeattempt->sumgrades = 30;
 126          $fakeattempt->uniqueid = 52;
 127          $fakeattempt->state = quiz_attempt::FINISHED;
 128          $DB->insert_record('quiz_attempts', $fakeattempt);
 129  
 130          $fakeattempt->attempt = 1;
 131          $fakeattempt->userid = 1;
 132          $fakeattempt->sumgrades = 100;
 133          $fakeattempt->uniqueid = 65;
 134          $DB->insert_record('quiz_attempts', $fakeattempt);
 135  
 136          $quiz = new \stdClass();
 137          $quiz->attempts = 10;
 138  
 139          $quiz->grademethod = QUIZ_ATTEMPTFIRST;
 140          $firstattempt = $DB->get_records_sql("
 141                  SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
 142                          . quiz_report_qm_filter_select($quiz), array(123, 456));
 143          $this->assertEquals(1, count($firstattempt));
 144          $firstattempt = reset($firstattempt);
 145          $this->assertEquals(1, $firstattempt->attempt);
 146  
 147          $quiz->grademethod = QUIZ_ATTEMPTLAST;
 148          $lastattempt = $DB->get_records_sql("
 149                  SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
 150                  . quiz_report_qm_filter_select($quiz), array(123, 456));
 151          $this->assertEquals(1, count($lastattempt));
 152          $lastattempt = reset($lastattempt);
 153          $this->assertEquals(3, $lastattempt->attempt);
 154  
 155          $quiz->attempts = 0;
 156          $quiz->grademethod = QUIZ_GRADEHIGHEST;
 157          $bestattempt = $DB->get_records_sql("
 158                  SELECT * FROM {quiz_attempts} qa_alias WHERE userid = ? AND quiz = ? AND "
 159                  . quiz_report_qm_filter_select($quiz, 'qa_alias'), array(123, 456));
 160          $this->assertEquals(1, count($bestattempt));
 161          $bestattempt = reset($bestattempt);
 162          $this->assertEquals(2, $bestattempt->attempt);
 163      }
 164  
 165      public function test_quiz_results_never_below_zero() {
 166          global $DB;
 167          $this->resetAfterTest();
 168  
 169          $quizid = 7;
 170          $fakegrade = new \stdClass();
 171          $fakegrade->quiz = $quizid;
 172  
 173          // Have 5 test grades.
 174          $fakegrade->userid = 10;
 175          $fakegrade->grade = 6.66667;
 176          $DB->insert_record('quiz_grades', $fakegrade);
 177  
 178          $fakegrade->userid = 11;
 179          $fakegrade->grade = -2.86;
 180          $DB->insert_record('quiz_grades', $fakegrade);
 181  
 182          $fakegrade->userid = 12;
 183          $fakegrade->grade = 10.0;
 184          $DB->insert_record('quiz_grades', $fakegrade);
 185  
 186          $fakegrade->userid = 13;
 187          $fakegrade->grade = -5.0;
 188          $DB->insert_record('quiz_grades', $fakegrade);
 189  
 190          $fakegrade->userid = 14;
 191          $fakegrade->grade = 33.33333;
 192          $DB->insert_record('quiz_grades', $fakegrade);
 193  
 194          $data = quiz_report_grade_bands(5, 20, $quizid);
 195          $this->assertGreaterThanOrEqual(0, min(array_keys($data)));
 196      }
 197  }