Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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