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