MDL-76898 quiz: final deprecation of things deprecated long ago
[moodle.git] / mod / quiz / classes / local / reports / attempts_report.php
blob9f246fd21b4e35b074bb9930579d85763ec7a840
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.
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/>.
17 namespace mod_quiz\local\reports;
19 use coding_exception;
20 use context_module;
21 use moodle_url;
22 use stdClass;
23 use table_sql;
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/tablelib.php');
30 /**
31 * Base class for quiz reports that are basically a table with one row for each attempt.
33 * @package mod_quiz
34 * @copyright 2010 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 abstract class attempts_report extends report_base {
38 /** @var int default page size for reports. */
39 const DEFAULT_PAGE_SIZE = 30;
41 /** @var string constant used for the options, means all users with attempts. */
42 const ALL_WITH = 'all_with';
43 /** @var string constant used for the options, means only enrolled users with attempts. */
44 const ENROLLED_WITH = 'enrolled_with';
45 /** @var string constant used for the options, means only enrolled users without attempts. */
46 const ENROLLED_WITHOUT = 'enrolled_without';
47 /** @var string constant used for the options, means all enrolled users. */
48 const ENROLLED_ALL = 'enrolled_any';
50 /** @var string the mode this report is. */
51 protected $mode;
53 /** @var context_module the quiz context. */
54 protected $context;
56 /** @var attempts_report_options_form The settings form to use. */
57 protected $form;
59 /** @var string SQL fragment for selecting the attempt that gave the final grade,
60 * if applicable. */
61 protected $qmsubselect;
63 /** @var boolean caches the results of {@see should_show_grades()}. */
64 protected $showgrades = null;
66 /**
67 * Initialise various aspects of this report.
69 * @param string $mode
70 * @param string $formclass
71 * @param object $quiz
72 * @param object $cm
73 * @param object $course
74 * @return array with four elements:
75 * 0 => integer the current group id (0 for none).
76 * 1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
77 * 2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
78 * 3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
79 * Will be the same as either element 1 or 2.
81 public function init($mode, $formclass, $quiz, $cm, $course): array {
82 $this->mode = $mode;
84 $this->context = context_module::instance($cm->id);
86 [$currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins] = $this->get_students_joins(
87 $cm, $course);
89 $this->qmsubselect = quiz_report_qm_filter_select($quiz);
91 $this->form = new $formclass($this->get_base_url(),
92 ['quiz' => $quiz, 'currentgroup' => $currentgroup, 'context' => $this->context]);
94 return [$currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins];
97 /**
98 * Get the base URL for this report.
99 * @return moodle_url the URL.
101 protected function get_base_url() {
102 return new moodle_url('/mod/quiz/report.php',
103 ['id' => $this->context->instanceid, 'mode' => $this->mode]);
107 * Get sql fragments (joins) which can be used to build queries that
108 * will select an appropriate set of students to show in the reports.
110 * @param object $cm the course module.
111 * @param object $course the course settings.
112 * @return array with four elements:
113 * 0 => integer the current group id (0 for none).
114 * 1 => \core\dml\sql_join Contains joins, wheres, params for all the students in this course.
115 * 2 => \core\dml\sql_join Contains joins, wheres, params for all the students in the current group.
116 * 3 => \core\dml\sql_join Contains joins, wheres, params for all the students to show in the report.
117 * Will be the same as either element 1 or 2.
119 protected function get_students_joins($cm, $course = null) {
120 $currentgroup = $this->get_current_group($cm, $course, $this->context);
122 $empty = new \core\dml\sql_join();
123 if ($currentgroup == self::NO_GROUPS_ALLOWED) {
124 return [$currentgroup, $empty, $empty, $empty];
127 $studentsjoins = get_enrolled_with_capabilities_join($this->context, '',
128 ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
130 if (empty($currentgroup)) {
131 return [$currentgroup, $studentsjoins, $empty, $studentsjoins];
134 // We have a currently selected group.
135 $groupstudentsjoins = get_enrolled_with_capabilities_join($this->context, '',
136 ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts'], $currentgroup);
138 return [$currentgroup, $studentsjoins, $groupstudentsjoins, $groupstudentsjoins];
142 * Outputs the things you commonly want at the top of a quiz report.
144 * Calls through to {@see print_header_and_tabs()} and then
145 * outputs the standard group selector, number of attempts summary,
146 * and messages to cover common cases when the report can't be shown.
148 * @param stdClass $cm the course_module information.
149 * @param stdClass $course the course settings.
150 * @param stdClass $quiz the quiz settings.
151 * @param attempts_report_options $options the current report settings.
152 * @param int $currentgroup the current group.
153 * @param bool $hasquestions whether there are any questions in the quiz.
154 * @param bool $hasstudents whether there are any relevant students.
156 protected function print_standard_header_and_messages($cm, $course, $quiz,
157 $options, $currentgroup, $hasquestions, $hasstudents) {
158 global $OUTPUT;
160 $this->print_header_and_tabs($cm, $course, $quiz, $this->mode);
162 if (groups_get_activity_groupmode($cm)) {
163 // Groups are being used, so output the group selector if we are not downloading.
164 groups_print_activity_menu($cm, $options->get_url());
167 // Print information on the number of existing attempts.
168 if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
169 echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
172 if (!$hasquestions) {
173 echo quiz_no_questions_message($quiz, $cm, $this->context);
174 } else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
175 echo $OUTPUT->notification(get_string('notingroup'));
176 } else if (!$hasstudents) {
177 echo $OUTPUT->notification(get_string('nostudentsyet'));
178 } else if ($currentgroup && !$this->hasgroupstudents) {
179 echo $OUTPUT->notification(get_string('nostudentsingroup'));
184 * Add all the user-related columns to the $columns and $headers arrays.
185 * @param table_sql $table the table being constructed.
186 * @param array $columns the list of columns. Added to.
187 * @param array $headers the columns headings. Added to.
189 protected function add_user_columns($table, &$columns, &$headers) {
190 global $CFG;
191 if (!$table->is_downloading() && $CFG->grade_report_showuserimage) {
192 $columns[] = 'picture';
193 $headers[] = '';
195 if (!$table->is_downloading()) {
196 $columns[] = 'fullname';
197 $headers[] = get_string('name');
198 } else {
199 $columns[] = 'lastname';
200 $headers[] = get_string('lastname');
201 $columns[] = 'firstname';
202 $headers[] = get_string('firstname');
205 $extrafields = \core_user\fields::get_identity_fields($this->context);
206 foreach ($extrafields as $field) {
207 $columns[] = $field;
208 $headers[] = \core_user\fields::get_display_name($field);
213 * Set the display options for the user-related columns in the table.
214 * @param table_sql $table the table being constructed.
216 protected function configure_user_columns($table) {
217 $table->column_suppress('picture');
218 $table->column_suppress('fullname');
220 $extrafields = \core_user\fields::get_identity_fields($this->context);
221 foreach ($extrafields as $field) {
222 $table->column_suppress($field);
225 $table->column_class('picture', 'picture');
226 $table->column_class('lastname', 'bold');
227 $table->column_class('firstname', 'bold');
228 $table->column_class('fullname', 'bold');
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.
236 protected function add_state_column(&$columns, &$headers) {
237 $columns[] = 'state';
238 $headers[] = get_string('attemptstate', 'quiz');
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.
246 protected function add_time_columns(&$columns, &$headers) {
247 $columns[] = 'timestart';
248 $headers[] = get_string('startedon', 'quiz');
250 $columns[] = 'timefinish';
251 $headers[] = get_string('timecompleted', 'quiz');
253 $columns[] = 'duration';
254 $headers[] = get_string('attemptduration', 'quiz');
258 * Add all the grade and feedback columns, if applicable, to the $columns
259 * and $headers arrays.
260 * @param object $quiz the quiz settings.
261 * @param bool $usercanseegrades whether the user is allowed to see grades for this quiz.
262 * @param array $columns the list of columns. Added to.
263 * @param array $headers the columns headings. Added to.
264 * @param bool $includefeedback whether to include the feedbacktext columns
266 protected function add_grade_columns($quiz, $usercanseegrades, &$columns, &$headers, $includefeedback = true) {
267 if ($usercanseegrades) {
268 $columns[] = 'sumgrades';
269 $headers[] = get_string('grade', 'quiz') . '/' .
270 quiz_format_grade($quiz, $quiz->grade);
273 if ($includefeedback && quiz_has_feedback($quiz)) {
274 $columns[] = 'feedbacktext';
275 $headers[] = get_string('feedback', 'quiz');
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.
285 * @param attempts_report_options $options the display options.
286 * @param bool $collapsible whether to allow columns in the report to be collapsed.
288 protected function set_up_table_columns($table, $columns, $headers, $reporturl,
289 attempts_report_options $options, $collapsible) {
290 $table->define_columns($columns);
291 $table->define_headers($headers);
292 $table->sortable(true, 'uniqueid');
294 $table->define_baseurl($options->get_url());
296 $this->configure_user_columns($table);
298 $table->no_sorting('feedbacktext');
299 $table->column_class('sumgrades', 'bold');
301 $table->set_attribute('id', 'attempts');
303 $table->collapsible($collapsible);
307 * Process any submitted actions.
308 * @param object $quiz the quiz settings.
309 * @param object $cm the cm object for the quiz.
310 * @param int $currentgroup the currently selected group.
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.
313 * @param moodle_url $redirecturl where to redircet to after a successful action.
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) {
318 if (optional_param('delete', 0, PARAM_BOOL) && confirm_sesskey()) {
319 if ($attemptids = optional_param_array('attemptid', [], PARAM_INT)) {
320 require_capability('mod/quiz:deleteattempts', $this->context);
321 $this->delete_selected_attempts($quiz, $cm, $attemptids, $allowedjoins);
322 redirect($redirecturl);
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.
334 * @param \core\dml\sql_join $allowedjoins (joins, wheres, params) This list of userids that are visible in the report.
335 * Users can only delete attempts that they are allowed to see in the report.
336 * Empty means all users.
338 protected function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\sql_join $allowedjoins) {
339 global $DB;
341 foreach ($attemptids as $attemptid) {
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";
354 $params = $allowedjoins->params + ['attemptid' => $attemptid];
355 $attempt = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE);
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.
359 continue;
362 // Set the course module id before calling quiz_delete_attempt().
363 $quiz->cmid = $cm->id;
364 quiz_delete_attempt($attempt, $quiz);