MDL-66946 quiz reports: incorrectly show teachers sometimes
[moodle.git] / mod / quiz / report / attemptsreport.php
CommitLineData
9b40c540 1<?php
9b40c540
TH
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/**
f5e42695
TH
18 * The file defines a base class that can be used to build a report like the
19 * overview or responses report, that has one row per attempt.
9b40c540 20 *
f5e42695
TH
21 * @package mod_quiz
22 * @copyright 2010 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9b40c540
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
9b40c540
TH
29require_once($CFG->libdir.'/tablelib.php');
30
31
32/**
33 * Base class for quiz reports that are basically a table with one row for each attempt.
34 *
f5e42695
TH
35 * @copyright 2010 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9b40c540 37 */
ac4d9157 38abstract class quiz_attempts_report extends quiz_default_report {
5bf8a169
TH
39 /** @var int default page size for reports. */
40 const DEFAULT_PAGE_SIZE = 30;
41
863872e3
TH
42 /** @var string constant used for the options, means all users with attempts. */
43 const ALL_WITH = 'all_with';
44 /** @var string constant used for the options, means only enrolled users with attempts. */
45 const ENROLLED_WITH = 'enrolled_with';
46 /** @var string constant used for the options, means only enrolled users without attempts. */
47 const ENROLLED_WITHOUT = 'enrolled_without';
48 /** @var string constant used for the options, means all enrolled users. */
49 const ENROLLED_ALL = 'enrolled_any';
5bf8a169 50
303aa3b8
TH
51 /** @var string the mode this report is. */
52 protected $mode;
53
9b40c540
TH
54 /** @var object the quiz context. */
55 protected $context;
56
ac4d9157 57 /** @var mod_quiz_attempts_report_form The settings form to use. */
303aa3b8
TH
58 protected $form;
59
60 /** @var string SQL fragment for selecting the attempt that gave the final grade,
61 * if applicable. */
62 protected $qmsubselect;
63
9b40c540
TH
64 /** @var boolean caches the results of {@link should_show_grades()}. */
65 protected $showgrades = null;
66
303aa3b8
TH
67 /**
68 * Initialise various aspects of this report.
69 *
70 * @param string $mode
71 * @param string $formclass
72 * @param object $quiz
73 * @param object $cm
74 * @param object $course
10c4fce5
JB
75 * @return array with four elements:
76 * 0 => integer the current group id (0 for none).
77 * 1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
78 * 2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
79 * 3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
80 * Will be the same as either element 1 or 2.
303aa3b8
TH
81 */
82 protected function init($mode, $formclass, $quiz, $cm, $course) {
83 $this->mode = $mode;
84
85 $this->context = context_module::instance($cm->id);
86
10c4fce5
JB
87 list($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins) = $this->get_students_joins(
88 $cm, $course);
303aa3b8
TH
89
90 $this->qmsubselect = quiz_report_qm_filter_select($quiz);
91
92 $this->form = new $formclass($this->get_base_url(),
6dd9362e 93 array('quiz' => $quiz, 'currentgroup' => $currentgroup, 'context' => $this->context));
303aa3b8 94
10c4fce5 95 return array($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins);
303aa3b8
TH
96 }
97
98 /**
9d58dae3
TH
99 * Get the base URL for this report.
100 * @return moodle_url the URL.
303aa3b8
TH
101 */
102 protected function get_base_url() {
103 return new moodle_url('/mod/quiz/report.php',
104 array('id' => $this->context->instanceid, 'mode' => $this->mode));
105 }
106
9b40c540 107 /**
10c4fce5
JB
108 * Get sql fragments (joins) which can be used to build queries that
109 * will select an appropriate set of students to show in the reports.
110 *
111 * @param object $cm the course module.
e4977ba5 112 * @param object $course the course settings.
6dd9362e 113 * @return array with four elements:
9b40c540 114 * 0 => integer the current group id (0 for none).
10c4fce5
JB
115 * 1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
116 * 2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
117 * 3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
118 * Will be the same as either element 1 or 2.
9b40c540 119 */
10c4fce5 120 protected function get_students_joins($cm, $course = null) {
e4977ba5
TH
121 $currentgroup = $this->get_current_group($cm, $course, $this->context);
122
10c4fce5 123 $empty = new \core\dml\sql_join();
e4977ba5 124 if ($currentgroup == self::NO_GROUPS_ALLOWED) {
10c4fce5 125 return array($currentgroup, $empty, $empty, $empty);
e4977ba5 126 }
9b40c540 127
4d325589
TH
128 $studentsjoins = get_enrolled_with_capabilities_join($this->context, '',
129 array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'));
9b40c540
TH
130
131 if (empty($currentgroup)) {
10c4fce5 132 return array($currentgroup, $studentsjoins, $empty, $studentsjoins);
9b40c540
TH
133 }
134
135 // We have a currently selected group.
10c4fce5
JB
136 $groupstudentsjoins = get_enrolled_with_capabilities_join($this->context, '',
137 array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'), $currentgroup);
9b40c540 138
10c4fce5 139 return array($currentgroup, $studentsjoins, $groupstudentsjoins, $groupstudentsjoins);
9b40c540
TH
140 }
141
eefa2c88
TH
142 /**
143 * Outputs the things you commonly want at the top of a quiz report.
144 *
145 * Calls through to {@link print_header_and_tabs()} and then
146 * outputs the standard group selector, number of attempts summary,
147 * and messages to cover common cases when the report can't be shown.
148 *
149 * @param stdClass $cm the course_module information.
ade8dd5d 150 * @param stdClass $course the course settings.
eefa2c88
TH
151 * @param stdClass $quiz the quiz settings.
152 * @param mod_quiz_attempts_report_options $options the current report settings.
153 * @param int $currentgroup the current group.
154 * @param bool $hasquestions whether there are any questions in the quiz.
155 * @param bool $hasstudents whether there are any relevant students.
156 */
157 protected function print_standard_header_and_messages($cm, $course, $quiz,
158 $options, $currentgroup, $hasquestions, $hasstudents) {
159 global $OUTPUT;
160
161 $this->print_header_and_tabs($cm, $course, $quiz, $this->mode);
162
163 if (groups_get_activity_groupmode($cm)) {
164 // Groups are being used, so output the group selector if we are not downloading.
165 groups_print_activity_menu($cm, $options->get_url());
166 }
167
168 // Print information on the number of existing attempts.
169 if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
170 echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
171 }
172
173 if (!$hasquestions) {
174 echo quiz_no_questions_message($quiz, $cm, $this->context);
ade8dd5d
TH
175 } else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
176 echo $OUTPUT->notification(get_string('notingroup'));
eefa2c88
TH
177 } else if (!$hasstudents) {
178 echo $OUTPUT->notification(get_string('nostudentsyet'));
179 } else if ($currentgroup && !$this->hasgroupstudents) {
180 echo $OUTPUT->notification(get_string('nostudentsingroup'));
181 }
182 }
183
9b40c540
TH
184 /**
185 * Add all the user-related columns to the $columns and $headers arrays.
186 * @param table_sql $table the table being constructed.
187 * @param array $columns the list of columns. Added to.
188 * @param array $headers the columns headings. Added to.
189 */
190 protected function add_user_columns($table, &$columns, &$headers) {
191 global $CFG;
192 if (!$table->is_downloading() && $CFG->grade_report_showuserimage) {
193 $columns[] = 'picture';
194 $headers[] = '';
195 }
196 if (!$table->is_downloading()) {
197 $columns[] = 'fullname';
198 $headers[] = get_string('name');
199 } else {
200 $columns[] = 'lastname';
201 $headers[] = get_string('lastname');
202 $columns[] = 'firstname';
203 $headers[] = get_string('firstname');
204 }
205
3a33d514 206 $extrafields = get_extra_user_fields($this->context);
e4aec5be 207 foreach ($extrafields as $field) {
208 $columns[] = $field;
209 $headers[] = get_user_field_name($field);
9b40c540 210 }
9b40c540
TH
211 }
212
213 /**
214 * Set the display options for the user-related columns in the table.
215 * @param table_sql $table the table being constructed.
216 */
217 protected function configure_user_columns($table) {
218 $table->column_suppress('picture');
219 $table->column_suppress('fullname');
12a725be
TH
220 $extrafields = get_extra_user_fields($this->context);
221 foreach ($extrafields as $field) {
222 $table->column_suppress($field);
223 }
9b40c540
TH
224
225 $table->column_class('picture', 'picture');
226 $table->column_class('lastname', 'bold');
227 $table->column_class('firstname', 'bold');
228 $table->column_class('fullname', 'bold');
229 }
230
c547514a
TH
231 /**
232 * Add the state column to the $columns and $headers arrays.
233 * @param array $columns the list of columns. Added to.
234 * @param array $headers the columns headings. Added to.
235 */
236 protected function add_state_column(&$columns, &$headers) {
237 $columns[] = 'state';
238 $headers[] = get_string('attemptstate', 'quiz');
239 }
240
9b40c540
TH
241 /**
242 * Add all the time-related columns to the $columns and $headers arrays.
243 * @param array $columns the list of columns. Added to.
244 * @param array $headers the columns headings. Added to.
245 */
246 protected function add_time_columns(&$columns, &$headers) {
247 $columns[] = 'timestart';
248 $headers[] = get_string('startedon', 'quiz');
249
250 $columns[] = 'timefinish';
25a03faa 251 $headers[] = get_string('timecompleted', 'quiz');
9b40c540
TH
252
253 $columns[] = 'duration';
254 $headers[] = get_string('attemptduration', 'quiz');
255 }
256
257 /**
258 * Add all the grade and feedback columns, if applicable, to the $columns
259 * and $headers arrays.
260 * @param object $quiz the quiz settings.
9e67e357 261 * @param bool $usercanseegrades whether the user is allowed to see grades for this quiz.
9b40c540
TH
262 * @param array $columns the list of columns. Added to.
263 * @param array $headers the columns headings. Added to.
6d54b0f7 264 * @param bool $includefeedback whether to include the feedbacktext columns
9b40c540 265 */
9e67e357
TH
266 protected function add_grade_columns($quiz, $usercanseegrades, &$columns, &$headers, $includefeedback = true) {
267 if ($usercanseegrades) {
9b40c540
TH
268 $columns[] = 'sumgrades';
269 $headers[] = get_string('grade', 'quiz') . '/' .
270 quiz_format_grade($quiz, $quiz->grade);
271 }
272
6d54b0f7 273 if ($includefeedback && quiz_has_feedback($quiz)) {
9b40c540
TH
274 $columns[] = 'feedbacktext';
275 $headers[] = get_string('feedback', 'quiz');
276 }
277 }
278
279 /**
280 * Set up the table.
281 * @param table_sql $table the table being constructed.
282 * @param array $columns the list of columns.
283 * @param array $headers the columns headings.
284 * @param moodle_url $reporturl the URL of this report.
e97d60ad 285 * @param mod_quiz_attempts_report_options $options the display options.
f7970e3c 286 * @param bool $collapsible whether to allow columns in the report to be collapsed.
9b40c540 287 */
25a03faa 288 protected function set_up_table_columns($table, $columns, $headers, $reporturl,
e97d60ad 289 mod_quiz_attempts_report_options $options, $collapsible) {
9b40c540
TH
290 $table->define_columns($columns);
291 $table->define_headers($headers);
292 $table->sortable(true, 'uniqueid');
293
e97d60ad 294 $table->define_baseurl($options->get_url());
9b40c540
TH
295
296 $this->configure_user_columns($table);
297
298 $table->no_sorting('feedbacktext');
299 $table->column_class('sumgrades', 'bold');
300
301 $table->set_attribute('id', 'attempts');
302
303 $table->collapsible($collapsible);
304 }
305
361cf27d
TH
306 /**
307 * Process any submitted actions.
308 * @param object $quiz the quiz settings.
a558acf9 309 * @param object $cm the cm object for the quiz.
361cf27d 310 * @param int $currentgroup the currently selected group.
10c4fce5
JB
311 * @param \core\dml\sql_join $groupstudentsjoins (joins, wheres, params) the students in the current group.
312 * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) the users whose attempt this user is allowed to modify.
a558acf9 313 * @param moodle_url $redirecturl where to redircet to after a successful action.
361cf27d 314 */
10c4fce5
JB
315 protected function process_actions($quiz, $cm, $currentgroup, \core\dml\sql_join $groupstudentsjoins,
316 \core\dml\sql_join $allowedjoins, $redirecturl) {
317 if (empty($currentgroup) || $this->hasgroupstudents) {
361cf27d
TH
318 if (optional_param('delete', 0, PARAM_BOOL) && confirm_sesskey()) {
319 if ($attemptids = optional_param_array('attemptid', array(), PARAM_INT)) {
320 require_capability('mod/quiz:deleteattempts', $this->context);
10c4fce5 321 $this->delete_selected_attempts($quiz, $cm, $attemptids, $allowedjoins);
a558acf9 322 redirect($redirecturl);
361cf27d
TH
323 }
324 }
325 }
326 }
327
9b40c540
TH
328 /**
329 * Delete the quiz attempts
330 * @param object $quiz the quiz settings. Attempts that don't belong to
331 * this quiz are not deleted.
332 * @param object $cm the course_module object.
333 * @param array $attemptids the list of attempt ids to delete.
10c4fce5 334 * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) This list of userids that are visible in the report.
9b40c540
TH
335 * Users can only delete attempts that they are allowed to see in the report.
336 * Empty means all users.
337 */
10c4fce5 338 protected function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\sql_join $allowedjoins) {
2daffca5
TH
339 global $DB;
340
9b40c540 341 foreach ($attemptids as $attemptid) {
10c4fce5
JB
342 if (empty($allowedjoins->joins)) {
343 $sql = "SELECT quiza.*
344 FROM {quiz_attempts} quiza
345 JOIN {user} u ON u.id = quiza.userid
346 WHERE quiza.id = :attemptid";
347 } else {
348 $sql = "SELECT quiza.*
349 FROM {quiz_attempts} quiza
350 JOIN {user} u ON u.id = quiza.userid
351 {$allowedjoins->joins}
352 WHERE {$allowedjoins->wheres} AND quiza.id = :attemptid";
9b40c540 353 }
10c4fce5
JB
354 $params = $allowedjoins->params + array('attemptid' => $attemptid);
355 $attempt = $DB->get_record_sql($sql, $params);
356 if (!$attempt || $attempt->quiz != $quiz->id || $attempt->preview != 0) {
357 // Ensure the attempt exists, belongs to this quiz and belongs to
358 // a student included in the report. If not skip.
9b40c540
TH
359 continue;
360 }
7dad3d95 361
a97a7585
MN
362 // Set the course module id before calling quiz_delete_attempt().
363 $quiz->cmid = $cm->id;
9b40c540
TH
364 quiz_delete_attempt($attempt, $quiz);
365 }
366 }
10c4fce5
JB
367
368 /**
369 * Get information about which students to show in the report.
370 * @param object $cm the coures module.
371 * @param object $course the course settings.
372 * @return array with four elements:
373 * 0 => integer the current group id (0 for none).
374 * 1 => array ids of all the students in this course.
375 * 2 => array ids of all the students in the current group.
376 * 3 => array ids of all the students to show in the report. Will be the
377 * same as either element 1 or 2.
378 * @deprecated since Moodle 3.2 Please use get_students_joins() instead.
379 */
380 protected function load_relevant_students($cm, $course = null) {
381 $msg = 'The function load_relevant_students() is deprecated. Please use get_students_joins() instead.';
382 throw new coding_exception($msg);
383 }
9b40c540 384}