MDL-20636 Fix unit test that the recent code change broke. #382
[moodle.git] / mod / quiz / report / reportlib.php
CommitLineData
f33c438e 1<?php
2709ee45
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Helper functions for the quiz reports.
20 *
ba643847
TH
21 * @package mod
22 * @subpackage quiz
23 * @copyright 2008 Jamie Pratt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2709ee45
TH
25 */
26
a17b297d
TH
27
28defined('MOODLE_INTERNAL') || die();
29
530d9866 30require_once($CFG->dirroot . '/mod/quiz/lib.php');
446166a6 31require_once($CFG->libdir . '/filelib.php');
530d9866 32
f33c438e 33define('QUIZ_REPORT_DEFAULT_PAGE_SIZE', 30);
f17a34b5 34define('QUIZ_REPORT_DEFAULT_GRADING_PAGE_SIZE', 10);
4469159e 35
36define('QUIZ_REPORT_ATTEMPTS_ALL', 0);
37define('QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO', 1);
38define('QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH', 2);
39define('QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS', 3);
2709ee45 40
869309b8 41/**
42 * Takes an array of objects and constructs a multidimensional array keyed by
43 * the keys it finds on the object.
44 * @param array $datum an array of objects with properties on the object
45 * including the keys passed as the next param.
46 * @param array $keys Array of strings with the names of the properties on the
47 * objects in datum that you want to index the multidimensional array by.
f7970e3c 48 * @param bool $keysunique If there is not only one object for each
869309b8 49 * combination of keys you are using you should set $keysunique to true.
50 * Otherwise all the object will be added to a zero based array. So the array
51 * returned will have count($keys) + 1 indexs.
52 * @return array multidimensional array properly indexed.
53 */
2709ee45
TH
54function quiz_report_index_by_keys($datum, $keys, $keysunique = true) {
55 if (!$datum) {
56 return array();
98f38217 57 }
58 $key = array_shift($keys);
59 $datumkeyed = array();
2709ee45
TH
60 foreach ($datum as $data) {
61 if ($keys || !$keysunique) {
98f38217 62 $datumkeyed[$data->{$key}][]= $data;
63 } else {
64 $datumkeyed[$data->{$key}]= $data;
65 }
66 }
2709ee45
TH
67 if ($keys) {
68 foreach ($datumkeyed as $datakey => $datakeyed) {
869309b8 69 $datumkeyed[$datakey] = quiz_report_index_by_keys($datakeyed, $keys, $keysunique);
98f38217 70 }
71 }
72 return $datumkeyed;
73}
2709ee45
TH
74
75function quiz_report_unindex($datum) {
76 if (!$datum) {
869309b8 77 return $datum;
78 }
79 $datumunkeyed = array();
2709ee45
TH
80 foreach ($datum as $value) {
81 if (is_array($value)) {
869309b8 82 $datumunkeyed = array_merge($datumunkeyed, quiz_report_unindex($value));
83 } else {
84 $datumunkeyed[] = $value;
85 }
86 }
87 return $datumunkeyed;
88}
720be6f2 89
2badf2e6 90/**
2709ee45
TH
91 * Get the slots of real questions (not descriptions) in this quiz, in order.
92 * @param object $quiz the quiz.
0ff4bd08 93 * @return array of slot => $question object with fields ->slot, ->id, ->maxmark, ->number, ->length.
2badf2e6 94 */
cf3b6568 95function quiz_report_get_significant_questions($quiz) {
2709ee45
TH
96 global $DB;
97
cf3b6568 98 $questionids = quiz_questions_in_quiz($quiz->questions);
3c6185e9
TH
99 if (empty($questionids)) {
100 return array();
101 }
102
cf3b6568 103 list($usql, $params) = $DB->get_in_or_equal(explode(',', $questionids));
9cf4a18b 104 $params[] = $quiz->id;
2709ee45
TH
105 $questions = $DB->get_records_sql("
106SELECT
107 q.id,
108 q.length,
109 qqi.grade AS maxmark
110
111FROM {question} q
112JOIN {quiz_question_instances} qqi ON qqi.question = q.id
113
114WHERE
2709ee45 115 q.id $usql AND
cf3b6568 116 qqi.quiz = ? AND
2709ee45
TH
117 length > 0", $params);
118
119 $qsbyslot = array();
2badf2e6 120 $number = 1;
2709ee45
TH
121 foreach (explode(',', $questionids) as $key => $id) {
122 if (!array_key_exists($id, $questions)) {
123 continue;
2badf2e6 124 }
2709ee45
TH
125
126 $slot = $key + 1;
127 $question = $questions[$id];
128 $question->slot = $slot;
129 $question->number = $number;
130
131 $qsbyslot[$slot] = $question;
132
133 $number += $question->length;
2badf2e6 134 }
2709ee45
TH
135
136 return $qsbyslot;
2badf2e6 137}
2709ee45 138
4469159e 139/**
140 * Given the quiz grading method return sub select sql to find the id of the
9cf4a18b 141 * one attempt that will be graded for each user. Or return
4469159e 142 * empty string if all attempts contribute to final grade.
143 */
2709ee45 144function quiz_report_qm_filter_select($quiz, $quizattemptsalias = 'quiza') {
9b40c540 145 if ($quiz->attempts == 1) { // Only one attempt allowed on this quiz
b621e1a0 146 return '';
147 }
9b40c540 148
b621e1a0 149 switch ($quiz->grademethod) {
4469159e 150 case QUIZ_GRADEHIGHEST :
2709ee45
TH
151 return "$quizattemptsalias.id = (
152 SELECT MIN(qa2.id)
9b40c540
TH
153 FROM {quiz_attempts} qa2
154 WHERE qa2.quiz = $quizattemptsalias.quiz AND
155 qa2.userid = $quizattemptsalias.userid AND
2709ee45
TH
156 COALESCE(qa2.sumgrades, 0) = (
157 SELECT MAX(COALESCE(qa3.sumgrades, 0))
9b40c540
TH
158 FROM {quiz_attempts} qa3
159 WHERE qa3.quiz = $quizattemptsalias.quiz AND
160 qa3.userid = $quizattemptsalias.userid
2709ee45
TH
161 )
162 )";
163
4469159e 164 case QUIZ_GRADEAVERAGE :
2709ee45
TH
165 return '';
166
4469159e 167 case QUIZ_ATTEMPTFIRST :
2709ee45
TH
168 return "$quizattemptsalias.id = (
169 SELECT MIN(qa2.id)
170 FROM {quiz_attempts} qa2
9b40c540
TH
171 WHERE qa2.quiz = $quizattemptsalias.quiz AND
172 qa2.userid = $quizattemptsalias.userid)";
2709ee45 173
4469159e 174 case QUIZ_ATTEMPTLAST :
2709ee45
TH
175 return "$quizattemptsalias.id = (
176 SELECT MAX(qa2.id)
177 FROM {quiz_attempts} qa2
9b40c540
TH
178 WHERE qa2.quiz = $quizattemptsalias.quiz AND
179 qa2.userid = $quizattemptsalias.userid)";
4469159e 180 }
4469159e 181}
8b87ab00 182
2709ee45
TH
183/**
184 * Get the nuber of students whose score was in a particular band for this quiz.
185 * @param number $bandwidth the width of each band.
f7970e3c
TH
186 * @param int $bands the number of bands
187 * @param int $quizid the quiz id.
2709ee45
TH
188 * @param array $userids list of user ids.
189 * @return array band number => number of users with scores in that band.
190 */
191function quiz_report_grade_bands($bandwidth, $bands, $quizid, $userids = array()) {
446166a6 192 global $DB;
2709ee45
TH
193
194 if ($userids) {
2daffca5 195 list($usql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'u000000');
2709ee45 196 $usql = "qg.userid $usql AND";
8b2f8253 197 } else {
2daffca5 198 $usql = '';
8b2f8253 199 $params = array();
200 }
2709ee45
TH
201 $sql = "
202SELECT
2daffca5 203 FLOOR(qg.grade / :bandwidth1) AS band,
2709ee45
TH
204 COUNT(1) AS num
205
206FROM {quiz_grades} qg
207JOIN {quiz} q ON qg.quiz = q.id
208
209WHERE
210 $usql
2daffca5 211 qg.quiz = :quizid
2709ee45
TH
212
213GROUP BY
2daffca5 214 FLOOR(qg.grade / :bandwidth2)
2709ee45
TH
215
216ORDER BY
217 band";
2daffca5
TH
218
219 $params['quizid'] = $quizid;
220 $params['bandwidth1'] = $bandwidth;
221 $params['bandwidth2'] = $bandwidth;
2709ee45 222
9cf4a18b 223 $data = $DB->get_records_sql_menu($sql, $params);
2709ee45 224
8b87ab00 225 //need to create array elements with values 0 at indexes where there is no element
a5686531 226 $data = $data + array_fill(0, $bands+1, 0);
8b87ab00 227 ksort($data);
2709ee45 228
9cf4a18b 229 //place the maximum (prefect grade) into the last band i.e. make last
a5686531 230 //band for example 9 <= g <=10 (where 10 is the perfect grade) rather than
231 //just 9 <= g <10.
2709ee45 232 $data[$bands - 1] += $data[$bands];
a5686531 233 unset($data[$bands]);
2709ee45 234
8b87ab00 235 return $data;
236}
2709ee45
TH
237
238function quiz_report_highlighting_grading_method($quiz, $qmsubselect, $qmfilter) {
239 if ($quiz->attempts == 1) {
240 return '<p>' . get_string('onlyoneattemptallowed', 'quiz_overview') . '</p>';
241
242 } else if (!$qmsubselect) {
243 return '<p>' . get_string('allattemptscontributetograde', 'quiz_overview') . '</p>';
244
245 } else if ($qmfilter) {
246 return '<p>' . get_string('showinggraded', 'quiz_overview') . '</p>';
247
248 } else {
249 return '<p>' . get_string('showinggradedandungraded', 'quiz_overview',
250 '<span class="gradedattempt">' . quiz_get_grading_option_name($quiz->grademethod) .
251 '</span>') . '</p>';
b621e1a0 252 }
253}
aad5b0fc 254
aad5b0fc 255/**
256 * Get the feedback text for a grade on this quiz. The feedback is
257 * processed ready for display.
258 *
259 * @param float $grade a grade on this quiz.
f7970e3c 260 * @param int $quizid the id of the quiz object.
aad5b0fc 261 * @return string the comment that corresponds to this grade (empty string if there is not one.
262 */
8ad67658 263function quiz_report_feedback_for_grade($grade, $quizid, $context) {
446166a6 264 global $DB;
99d19c13 265
aad5b0fc 266 static $feedbackcache = array();
2709ee45
TH
267
268 if (!isset($feedbackcache[$quizid])) {
9cf4a18b 269 $feedbackcache[$quizid] = $DB->get_records('quiz_feedback', array('quizid' => $quizid));
aad5b0fc 270 }
2709ee45 271
aad5b0fc 272 $feedbacks = $feedbackcache[$quizid];
8ad67658 273 $feedbackid = 0;
aad5b0fc 274 $feedbacktext = '';
8ad67658 275 $feedbacktextformat = FORMAT_MOODLE;
aad5b0fc 276 foreach ($feedbacks as $feedback) {
277 if ($feedback->mingrade <= $grade && $grade < $feedback->maxgrade){
8ad67658 278 $feedbackid = $feedback->id;
aad5b0fc 279 $feedbacktext = $feedback->feedbacktext;
8ad67658 280 $feedbacktextformat = $feedback->feedbacktextformat;
aad5b0fc 281 break;
282 }
283 }
284
285 // Clean the text, ready for display.
0ff4bd08 286 $formatoptions = new stdClass();
aad5b0fc 287 $formatoptions->noclean = true;
8ad67658
EL
288 $feedbacktext = file_rewrite_pluginfile_urls($feedbacktext, 'pluginfile.php', $context->id, 'mod_quiz', 'feedback', $feedbackid);
289 $feedbacktext = format_text($feedbacktext, $feedbacktextformat, $formatoptions);
aad5b0fc 290
291 return $feedbacktext;
292}
0c1c764e 293
2709ee45
TH
294/**
295 * Format a number as a percentage out of $quiz->sumgrades
296 * @param number $rawgrade the mark to format.
297 * @param object $quiz the quiz settings
f7970e3c 298 * @param bool $round whether to round the results ot $quiz->decimalpoints.
2709ee45
TH
299 */
300function quiz_report_scale_summarks_as_percentage($rawmark, $quiz, $round = true) {
301 if ($quiz->sumgrades == 0) {
869309b8 302 return '';
0c1c764e 303 }
2709ee45
TH
304 if (!is_numeric($rawmark)) {
305 return $rawmark;
306 }
307
308 $mark = $rawmark * 100 / $quiz->sumgrades;
309 if ($round) {
310 $mark = quiz_format_grade($quiz, $mark);
311 }
312 return $mark . '%';
0c1c764e 313}
2709ee45 314
bbf4f440 315/**
316 * Returns an array of reports to which the current user has access to.
2709ee45 317 * @return array reports are ordered as they should be for display in tabs.
bbf4f440 318 */
a49cb927 319function quiz_report_list($context) {
bbf4f440 320 global $DB;
321 static $reportlist = null;
2709ee45 322 if (!is_null($reportlist)) {
bbf4f440 323 return $reportlist;
324 }
2709ee45 325
f2557823 326 $reports = $DB->get_records('quiz_reports', null, 'displayorder DESC', 'name, capability');
a49cb927 327 $reportdirs = get_plugin_list('quiz');
1ddfb914 328
329 // Order the reports tab in descending order of displayorder
bbf4f440 330 $reportcaps = array();
2709ee45
TH
331 foreach ($reports as $key => $report) {
332 if (array_key_exists($report->name, $reportdirs)) {
333 $reportcaps[$report->name] = $report->capability;
bbf4f440 334 }
335 }
336
2709ee45 337 // Add any other reports, which are on disc but not in the DB, on the end
1ddfb914 338 foreach ($reportdirs as $reportname => $notused) {
bbf4f440 339 if (!isset($reportcaps[$reportname])) {
1ddfb914 340 $reportcaps[$reportname] = null;
bbf4f440 341 }
342 }
343 $reportlist = array();
2709ee45
TH
344 foreach ($reportcaps as $name => $capability) {
345 if (empty($capability)) {
bbf4f440 346 $capability = 'mod/quiz:viewreports';
347 }
2709ee45 348 if (has_capability($capability, $context)) {
bbf4f440 349 $reportlist[] = $name;
350 }
351 }
352 return $reportlist;
353}
354
2709ee45
TH
355/**
356 * Create a filename for use when downloading data from a quiz report. It is
357 * expected that this will be passed to flexible_table::is_downloading, which
358 * cleans the filename of bad characters and adds the file extension.
359 * @param string $report the type of report.
360 * @param string $courseshortname the course shortname.
361 * @param string $quizname the quiz name.
362 * @return string the filename.
363 */
364function quiz_report_download_filename($report, $courseshortname, $quizname) {
365 return $courseshortname . '-' . format_string($quizname, true) . '-' . $report;
366}
367
a49cb927
TH
368/**
369 * Get the default report for the current user.
370 * @param object $context the quiz context.
371 */
372function quiz_report_default_report($context) {
373 return reset(quiz_report_list($context));
374}