Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 */
1441 ariadna 32
final 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
        // ---------------------------------------------------
1441 ariadna 97
        // 4  | 456    | 123  | 1       | null      | submitted
1 efrain 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
1441 ariadna 102
        // 6  | 456    | 234  | 1       | 100       | finished
103
        // 7  | 456    | 234  | 2       | null      | submitted
1 efrain 104
        // layout is only given because it has a not-null constraint.
105
        // uniqueid values are meaningless, but that column has a unique constraint.
1441 ariadna 106
        // user 123 will have their first attempt submitted, but not finished.
107
        // user 234 will have their last attempt submitted, but not finished.
1 efrain 108
 
109
        $fakeattempt->attempt = 3;
110
        $fakeattempt->sumgrades = 50;
111
        $fakeattempt->uniqueid = 13;
112
        $DB->insert_record('quiz_attempts', $fakeattempt);
113
 
114
        $fakeattempt->attempt = 2;
115
        $fakeattempt->sumgrades = 50;
116
        $fakeattempt->uniqueid = 26;
1441 ariadna 117
        $fakeattempt->state = quiz_attempt::FINISHED;
1 efrain 118
        $DB->insert_record('quiz_attempts', $fakeattempt);
119
 
120
        $fakeattempt->attempt = 4;
121
        $fakeattempt->sumgrades = null;
122
        $fakeattempt->uniqueid = 39;
123
        $fakeattempt->state = quiz_attempt::IN_PROGRESS;
124
        $DB->insert_record('quiz_attempts', $fakeattempt);
125
 
126
        $fakeattempt->attempt = 1;
1441 ariadna 127
        $fakeattempt->sumgrades = null;
1 efrain 128
        $fakeattempt->uniqueid = 52;
1441 ariadna 129
        $fakeattempt->state = quiz_attempt::SUBMITTED;
1 efrain 130
        $DB->insert_record('quiz_attempts', $fakeattempt);
131
 
132
        $fakeattempt->attempt = 1;
133
        $fakeattempt->userid = 1;
134
        $fakeattempt->sumgrades = 100;
135
        $fakeattempt->uniqueid = 65;
1441 ariadna 136
        $fakeattempt->state = quiz_attempt::FINISHED;
1 efrain 137
        $DB->insert_record('quiz_attempts', $fakeattempt);
138
 
1441 ariadna 139
        $fakeattempt->attempt = 1;
140
        $fakeattempt->userid = 234;
141
        $fakeattempt->sumgrades = 100;
142
        $fakeattempt->uniqueid = 99;
143
        $DB->insert_record('quiz_attempts', $fakeattempt);
144
 
145
        $fakeattempt->attempt = 2;
146
        $fakeattempt->userid = 234;
147
        $fakeattempt->sumgrades = null;
148
        $fakeattempt->uniqueid = 77;
149
        $fakeattempt->state = quiz_attempt::SUBMITTED;
150
        $DB->insert_record('quiz_attempts', $fakeattempt);
151
 
1 efrain 152
        $quiz = new \stdClass();
153
        $quiz->attempts = 10;
154
 
155
        $quiz->grademethod = QUIZ_ATTEMPTFIRST;
156
        $firstattempt = $DB->get_records_sql("
157
                SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
1441 ariadna 158
                . quiz_report_qm_filter_select($quiz), [123, 456]);
159
        $this->assertCount(1, $firstattempt);
1 efrain 160
        $firstattempt = reset($firstattempt);
161
        $this->assertEquals(1, $firstattempt->attempt);
1441 ariadna 162
        $this->assertEquals(quiz_attempt::SUBMITTED, $firstattempt->state);
1 efrain 163
 
1441 ariadna 164
        $firstattempt = $DB->get_records_sql("
165
                SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
166
                . quiz_report_qm_filter_select($quiz), [234, 456]);
167
        $this->assertCount(1, $firstattempt);
168
        $firstattempt = reset($firstattempt);
169
        $this->assertEquals(1, $firstattempt->attempt);
170
        $this->assertEquals(quiz_attempt::FINISHED, $firstattempt->state);
171
 
1 efrain 172
        $quiz->grademethod = QUIZ_ATTEMPTLAST;
173
        $lastattempt = $DB->get_records_sql("
174
                SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
175
                . quiz_report_qm_filter_select($quiz), [123, 456]);
1441 ariadna 176
        $this->assertCount(1, $lastattempt);
1 efrain 177
        $lastattempt = reset($lastattempt);
178
        $this->assertEquals(3, $lastattempt->attempt);
1441 ariadna 179
        $this->assertEquals(quiz_attempt::FINISHED, $lastattempt->state);
1 efrain 180
 
1441 ariadna 181
        $lastattempt = $DB->get_records_sql("
182
                SELECT * FROM {quiz_attempts} quiza WHERE userid = ? AND quiz = ? AND "
183
                . quiz_report_qm_filter_select($quiz), [234, 456]);
184
        $this->assertCount(1, $lastattempt);
185
        $lastattempt = reset($lastattempt);
186
        $this->assertEquals(2, $lastattempt->attempt);
187
        $this->assertEquals(quiz_attempt::SUBMITTED, $lastattempt->state);
188
 
1 efrain 189
        $quiz->attempts = 0;
190
        $quiz->grademethod = QUIZ_GRADEHIGHEST;
191
        $bestattempt = $DB->get_records_sql("
192
                SELECT * FROM {quiz_attempts} qa_alias WHERE userid = ? AND quiz = ? AND "
193
                . quiz_report_qm_filter_select($quiz, 'qa_alias'), [123, 456]);
1441 ariadna 194
        $this->assertCount(1, $bestattempt);
1 efrain 195
        $bestattempt = reset($bestattempt);
196
        $this->assertEquals(2, $bestattempt->attempt);
1441 ariadna 197
 
198
        $bestattempt = $DB->get_records_sql("
199
                SELECT * FROM {quiz_attempts} qa_alias WHERE userid = ? AND quiz = ? AND "
200
                . quiz_report_qm_filter_select($quiz, 'qa_alias'), [234, 456]);
201
        $this->assertCount(1, $bestattempt);
202
        $bestattempt = reset($bestattempt);
203
        $this->assertEquals(1, $bestattempt->attempt);
1 efrain 204
    }
205
 
11 efrain 206
    public function test_quiz_results_never_below_zero(): void {
1 efrain 207
        global $DB;
208
        $this->resetAfterTest();
209
 
210
        $quizid = 7;
211
        $fakegrade = new \stdClass();
212
        $fakegrade->quiz = $quizid;
213
 
214
        // Have 5 test grades.
215
        $fakegrade->userid = 10;
216
        $fakegrade->grade = 6.66667;
217
        $DB->insert_record('quiz_grades', $fakegrade);
218
 
219
        $fakegrade->userid = 11;
220
        $fakegrade->grade = -2.86;
221
        $DB->insert_record('quiz_grades', $fakegrade);
222
 
223
        $fakegrade->userid = 12;
224
        $fakegrade->grade = 10.0;
225
        $DB->insert_record('quiz_grades', $fakegrade);
226
 
227
        $fakegrade->userid = 13;
228
        $fakegrade->grade = -5.0;
229
        $DB->insert_record('quiz_grades', $fakegrade);
230
 
231
        $fakegrade->userid = 14;
232
        $fakegrade->grade = 33.33333;
233
        $DB->insert_record('quiz_grades', $fakegrade);
234
 
235
        $data = quiz_report_grade_bands(5, 20, $quizid);
236
        $this->assertGreaterThanOrEqual(0, min(array_keys($data)));
237
    }
238
}