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