Commit | Line | Data |
---|---|---|
f5e42695 TH |
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 | /** | |
18 | * Base class for the table used by a {@link quiz_attempt_report}. | |
19 | * | |
20 | * @package mod_quiz | |
21 | * @copyright 2010 The Open University | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | require_once($CFG->libdir.'/tablelib.php'); | |
29 | ||
30 | ||
31 | /** | |
32 | * Base class for the table used by a {@link quiz_attempt_report}. | |
33 | * | |
34 | * @copyright 2010 The Open University | |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36 | */ | |
37 | abstract class quiz_attempt_report_table extends table_sql { | |
38 | public $useridfield = 'userid'; | |
39 | ||
40 | /** @var moodle_url the URL of this report. */ | |
41 | protected $reporturl; | |
42 | ||
43 | /** @var array the display options. */ | |
44 | protected $displayoptions; | |
45 | ||
46 | /** | |
47 | * @var array information about the latest step of each question. | |
48 | * Loaded by {@link load_question_latest_steps()}, if applicable. | |
49 | */ | |
50 | protected $lateststeps = null; | |
51 | ||
52 | protected $quiz; | |
53 | protected $context; | |
54 | protected $qmsubselect; | |
55 | protected $qmfilter; | |
56 | protected $attemptsmode; | |
57 | protected $groupstudents; | |
58 | protected $students; | |
59 | protected $questions; | |
60 | protected $includecheckboxes; | |
61 | ||
62 | public function __construct($uniqueid, $quiz, $context, $qmsubselect, $qmfilter, | |
63 | $attemptsmode, $groupstudents, $students, $questions, $includecheckboxes, | |
64 | $reporturl, $displayoptions) { | |
65 | parent::__construct($uniqueid); | |
66 | $this->quiz = $quiz; | |
67 | $this->context = $context; | |
68 | $this->qmsubselect = $qmsubselect; | |
69 | $this->qmfilter = $qmfilter; | |
70 | $this->attemptsmode = $attemptsmode; | |
71 | $this->groupstudents = $groupstudents; | |
72 | $this->students = $students; | |
73 | $this->questions = $questions; | |
74 | $this->includecheckboxes = $includecheckboxes; | |
75 | $this->reporturl = $reporturl; | |
76 | $this->displayoptions = $displayoptions; | |
77 | } | |
78 | ||
79 | public function col_checkbox($attempt) { | |
80 | if ($attempt->attempt) { | |
81 | return '<input type="checkbox" name="attemptid[]" value="'.$attempt->attempt.'" />'; | |
82 | } else { | |
83 | return ''; | |
84 | } | |
85 | } | |
86 | ||
87 | public function col_picture($attempt) { | |
88 | global $OUTPUT; | |
89 | $user = new stdClass(); | |
90 | $user->id = $attempt->userid; | |
91 | $user->lastname = $attempt->lastname; | |
92 | $user->firstname = $attempt->firstname; | |
93 | $user->imagealt = $attempt->imagealt; | |
94 | $user->picture = $attempt->picture; | |
95 | $user->email = $attempt->email; | |
96 | return $OUTPUT->user_picture($user); | |
97 | } | |
98 | ||
99 | public function col_fullname($attempt) { | |
100 | $html = parent::col_fullname($attempt); | |
101 | if ($this->is_downloading()) { | |
102 | return $html; | |
103 | } | |
104 | ||
105 | return $html . html_writer::empty_tag('br') . html_writer::link( | |
106 | new moodle_url('/mod/quiz/review.php', array('attempt' => $attempt->attempt)), | |
107 | get_string('reviewattempt', 'quiz'), array('class' => 'reviewlink')); | |
108 | } | |
109 | ||
110 | public function col_timestart($attempt) { | |
111 | if ($attempt->attempt) { | |
112 | return userdate($attempt->timestart, $this->strtimeformat); | |
113 | } else { | |
114 | return '-'; | |
115 | } | |
116 | } | |
117 | ||
118 | public function col_timefinish($attempt) { | |
119 | if ($attempt->attempt && $attempt->timefinish) { | |
120 | return userdate($attempt->timefinish, $this->strtimeformat); | |
121 | } else { | |
122 | return '-'; | |
123 | } | |
124 | } | |
125 | ||
126 | public function col_duration($attempt) { | |
127 | if ($attempt->timefinish) { | |
128 | return format_time($attempt->timefinish - $attempt->timestart); | |
129 | } else if ($attempt->timestart) { | |
130 | return get_string('unfinished', 'quiz'); | |
131 | } else { | |
132 | return '-'; | |
133 | } | |
134 | } | |
135 | ||
136 | public function col_feedbacktext($attempt) { | |
137 | if (!$attempt->timefinish) { | |
138 | return '-'; | |
139 | } | |
140 | ||
141 | $feedback = quiz_report_feedback_for_grade( | |
142 | quiz_rescale_grade($attempt->sumgrades, $this->quiz, false), | |
143 | $this->quiz->id, $this->context); | |
144 | ||
145 | if ($this->is_downloading()) { | |
146 | $feedback = strip_tags($feedback); | |
147 | } | |
148 | ||
149 | return $feedback; | |
150 | } | |
151 | ||
152 | public function get_row_class($attempt) { | |
153 | if ($this->qmsubselect && $attempt->gradedattempt) { | |
154 | return 'gradedattempt'; | |
155 | } else { | |
156 | return ''; | |
157 | } | |
158 | } | |
159 | ||
160 | /** | |
161 | * Make a link to review an individual question in a popup window. | |
162 | * | |
163 | * @param string $data HTML fragment. The text to make into the link. | |
164 | * @param object $attempt data for the row of the table being output. | |
165 | * @param int $slot the number used to identify this question within this usage. | |
166 | */ | |
167 | public function make_review_link($data, $attempt, $slot) { | |
168 | global $OUTPUT; | |
169 | ||
170 | $stepdata = $this->lateststeps[$attempt->usageid][$slot]; | |
171 | $state = question_state::get($stepdata->state); | |
172 | ||
173 | $flag = ''; | |
174 | if ($stepdata->flagged) { | |
175 | $flag = ' ' . $OUTPUT->pix_icon('i/flagged', get_string('flagged', 'question'), | |
176 | 'moodle', array('class' => 'questionflag')); | |
177 | } | |
178 | ||
179 | $feedbackimg = ''; | |
180 | if ($state->is_finished() && $state != question_state::$needsgrading) { | |
181 | $feedbackimg = ' ' . $this->icon_for_fraction($stepdata->fraction); | |
182 | } | |
183 | ||
184 | $output = html_writer::tag('span', html_writer::tag('span', | |
185 | $data . $feedbackimg . $flag, | |
186 | array('class' => $state->get_state_class(true))), array('class' => 'que')); | |
187 | ||
188 | $url = new moodle_url('/mod/quiz/reviewquestion.php', | |
189 | array('attempt' => $attempt->attempt, 'slot' => $slot)); | |
190 | $output = $OUTPUT->action_link($url, $output, | |
191 | new popup_action('click', $url, 'reviewquestion', | |
192 | array('height' => 450, 'width' => 650)), | |
193 | array('title' => get_string('reviewresponse', 'quiz'))); | |
194 | ||
195 | return $output; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Return an appropriate icon (green tick, red cross, etc.) for a grade. | |
200 | * @param float $fraction grade on a scale 0..1. | |
201 | * @return string html fragment. | |
202 | */ | |
203 | protected function icon_for_fraction($fraction) { | |
204 | global $OUTPUT; | |
205 | ||
206 | $state = question_state::graded_state_for_fraction($fraction); | |
207 | if ($state == question_state::$gradedright) { | |
208 | $icon = 'i/tick_green_big'; | |
209 | } else if ($state == question_state::$gradedpartial) { | |
210 | $icon = 'i/tick_amber_big'; | |
211 | } else { | |
212 | $icon = 'i/cross_red_big'; | |
213 | } | |
214 | ||
215 | return $OUTPUT->pix_icon($icon, get_string($state->get_feedback_class(), 'question'), | |
216 | 'moodle', array('class' => 'icon')); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Load information about the latest state of selected questions in selected attempts. | |
221 | * | |
222 | * The results are returned as an two dimensional array $qubaid => $slot => $dataobject | |
223 | * | |
224 | * @param qubaid_condition $qubaids used to restrict which usages are included | |
225 | * in the query. See {@link qubaid_condition}. | |
226 | * @param array $slots A list of slots for the questions you want to konw about. | |
227 | * @return array of records. See the SQL in this function to see the fields available. | |
228 | */ | |
229 | protected function load_question_latest_steps(qubaid_condition $qubaids) { | |
230 | $dm = new question_engine_data_mapper(); | |
231 | $latesstepdata = $dm->load_questions_usages_latest_steps( | |
232 | $qubaids, array_keys($this->questions)); | |
233 | ||
234 | $lateststeps = array(); | |
235 | foreach ($latesstepdata as $step) { | |
236 | $lateststeps[$step->questionusageid][$step->slot] = $step; | |
237 | } | |
238 | ||
239 | return $lateststeps; | |
240 | } | |
241 | ||
242 | /** | |
243 | * @return bool should {@link query_db()} call {@link load_question_latest_steps}? | |
244 | */ | |
245 | protected function requires_latest_steps_loaded() { | |
246 | return false; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Is this a column that depends on joining to the latest state information? | |
251 | * If so, return the corresponding slot. If not, return false. | |
252 | * @param string $column a column name | |
253 | * @return int false if no, else a slot. | |
254 | */ | |
255 | protected function is_latest_step_column($column) { | |
256 | return false; | |
257 | } | |
258 | ||
259 | /** | |
260 | * Get any fields that might be needed when sorting on date for a particular slot. | |
261 | * @param int $slot the slot for the column we want. | |
262 | * @param string $alias the table alias for latest state information relating to that slot. | |
263 | */ | |
264 | protected function get_required_latest_state_fields($slot, $alias) { | |
265 | return ''; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Contruct all the parts of the main database query. | |
270 | * @param array $reportstudents list if userids of users to include in the report. | |
271 | * @return array with 4 elements ($fields, $from, $where, $params) that can be used to | |
272 | * build the actual database query. | |
273 | */ | |
274 | public function base_sql($reportstudents) { | |
275 | global $DB; | |
276 | ||
277 | $fields = $DB->sql_concat('u.id', "'#'", 'COALESCE(quiza.attempt, 0)') . ' AS uniqueid,'; | |
278 | ||
279 | if ($this->qmsubselect) { | |
280 | $fields .= "\n(CASE WHEN $this->qmsubselect THEN 1 ELSE 0 END) AS gradedattempt,"; | |
281 | } | |
282 | ||
283 | $extrafields = get_extra_user_fields_sql($this->context, 'u', '', | |
284 | array('id', 'idnumber', 'firstname', 'lastname', 'picture', | |
285 | 'imagealt', 'institution', 'department', 'email')); | |
286 | $fields .= ' | |
287 | quiza.uniqueid AS usageid, | |
288 | quiza.id AS attempt, | |
289 | u.id AS userid, | |
290 | u.idnumber, | |
291 | u.firstname, | |
292 | u.lastname, | |
293 | u.picture, | |
294 | u.imagealt, | |
295 | u.institution, | |
296 | u.department, | |
297 | u.email' . $extrafields . ', | |
298 | quiza.sumgrades, | |
299 | quiza.timefinish, | |
300 | quiza.timestart, | |
301 | CASE WHEN quiza.timefinish = 0 THEN null | |
302 | WHEN quiza.timefinish > quiza.timestart THEN quiza.timefinish - quiza.timestart | |
303 | ELSE 0 END AS duration'; | |
304 | // To explain that last bit, in MySQL, qa.timestart and qa.timefinish | |
305 | // are unsigned. Since MySQL 5.5.5, when they introduced strict mode, | |
306 | // subtracting a larger unsigned int from a smaller one gave an error. | |
307 | // Therefore, we avoid doing that. timefinish can be non-zero and less | |
308 | // than timestart when you have two load-balanced servers with very | |
309 | // badly synchronised clocks, and a student does a really quick attempt.'; | |
310 | ||
311 | // This part is the same for all cases - join users and quiz_attempts tables | |
312 | $from = "\n{user} u"; | |
313 | $from .= "\nLEFT JOIN {quiz_attempts} quiza ON | |
314 | quiza.userid = u.id AND quiza.quiz = :quizid"; | |
315 | $params = array('quizid' => $this->quiz->id); | |
316 | ||
317 | if ($this->qmsubselect && $this->qmfilter) { | |
318 | $from .= " AND $this->qmsubselect"; | |
319 | } | |
320 | switch ($this->attemptsmode) { | |
321 | case QUIZ_REPORT_ATTEMPTS_ALL: | |
322 | // Show all attempts, including students who are no longer in the course | |
323 | $where = 'quiza.id IS NOT NULL AND quiza.preview = 0'; | |
324 | break; | |
325 | case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH: | |
326 | // Show only students with attempts | |
327 | list($usql, $uparams) = $DB->get_in_or_equal( | |
328 | $reportstudents, SQL_PARAMS_NAMED, 'u'); | |
329 | $params += $uparams; | |
330 | $where = "u.id $usql AND quiza.preview = 0 AND quiza.id IS NOT NULL"; | |
331 | break; | |
332 | case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO: | |
333 | // Show only students without attempts | |
334 | list($usql, $uparams) = $DB->get_in_or_equal( | |
335 | $reportstudents, SQL_PARAMS_NAMED, 'u'); | |
336 | $params += $uparams; | |
337 | $where = "u.id $usql AND quiza.id IS NULL"; | |
338 | break; | |
339 | case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS: | |
340 | // Show all students with or without attempts | |
341 | list($usql, $uparams) = $DB->get_in_or_equal( | |
342 | $reportstudents, SQL_PARAMS_NAMED, 'u'); | |
343 | $params += $uparams; | |
344 | $where = "u.id $usql AND (quiza.preview = 0 OR quiza.preview IS NULL)"; | |
345 | break; | |
346 | } | |
347 | ||
348 | return array($fields, $from, $where, $params); | |
349 | } | |
350 | ||
351 | /** | |
352 | * Add the information about the latest state of the question with slot | |
353 | * $slot to the query. | |
354 | * | |
355 | * The extra information is added as a join to a | |
356 | * 'table' with alias qa$slot, with columns that are a union of | |
357 | * the columns of the question_attempts and question_attempts_states tables. | |
358 | * | |
359 | * @param int $slot the question to add information for. | |
360 | */ | |
361 | protected function add_latest_state_join($slot) { | |
362 | $alias = 'qa' . $slot; | |
363 | ||
364 | $fields = $this->get_required_latest_state_fields($slot, $alias); | |
365 | if (!$fields) { | |
366 | return; | |
367 | } | |
368 | ||
369 | // This condition roughly filters the list of attempts to be considered. | |
370 | // It is only used in a subselect to help crappy databases (see MDL-30122) | |
371 | // therefore, it is better to use a very simple join, which may include | |
372 | // too many records, than to do a super-accurate join. | |
373 | $qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid", | |
374 | "{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid'])); | |
375 | ||
376 | $dm = new question_engine_data_mapper(); | |
377 | list($inlineview, $viewparams) = $dm->question_attempt_latest_state_view($alias, $qubaids); | |
378 | ||
379 | $this->sql->fields .= ",\n$fields"; | |
380 | $this->sql->from .= "\nLEFT JOIN $inlineview ON " . | |
381 | "$alias.questionusageid = quiza.uniqueid AND $alias.slot = :{$alias}slot"; | |
382 | $this->sql->params[$alias . 'slot'] = $slot; | |
383 | $this->sql->params = array_merge($this->sql->params, $viewparams); | |
384 | } | |
385 | ||
386 | /** | |
387 | * Get an appropriate qubaid_condition for loading more data about the | |
388 | * attempts we are displaying. | |
389 | * @return qubaid_condition | |
390 | */ | |
391 | protected function get_qubaids_condition() { | |
392 | if (is_null($this->rawdata)) { | |
393 | throw new coding_exception( | |
394 | 'Cannot call get_qubaids_condition until the main data has been loaded.'); | |
395 | } | |
396 | ||
397 | if ($this->is_downloading()) { | |
398 | // We want usages for all attempts. | |
399 | return new qubaid_join($this->sql->from, 'quiza.uniqueid', | |
400 | $this->sql->where, $this->sql->params); | |
401 | } | |
402 | ||
403 | $qubaids = array(); | |
404 | foreach ($this->rawdata as $attempt) { | |
405 | if ($attempt->usageid > 0) { | |
406 | $qubaids[] = $attempt->usageid; | |
407 | } | |
408 | } | |
409 | ||
410 | return new qubaid_list($qubaids); | |
411 | } | |
412 | ||
413 | public function query_db($pagesize, $useinitialsbar = true) { | |
414 | $doneslots = array(); | |
415 | foreach ($this->get_sort_columns() as $column => $notused) { | |
416 | $slot = $this->is_latest_step_column($column); | |
417 | if ($slot && !in_array($slot, $doneslots)) { | |
418 | $this->add_latest_state_join($slot); | |
419 | $doneslots[] = $slot; | |
420 | } | |
421 | } | |
422 | ||
423 | parent::query_db($pagesize, $useinitialsbar); | |
424 | ||
425 | if ($this->requires_latest_steps_loaded()) { | |
426 | $qubaids = $this->get_qubaids_condition(); | |
427 | $this->lateststeps = $this->load_question_latest_steps($qubaids); | |
428 | } | |
429 | } | |
430 | ||
431 | public function get_sort_columns() { | |
432 | // Add attemptid as a final tie-break to the sort. This ensures that | |
433 | // Attempts by the same student appear in order when just sorting by name. | |
434 | $sortcolumns = parent::get_sort_columns(); | |
435 | $sortcolumns['quiza.id'] = SORT_ASC; | |
436 | return $sortcolumns; | |
437 | } | |
438 | ||
439 | public function wrap_html_start() { | |
440 | if ($this->is_downloading() || !$this->includecheckboxes) { | |
441 | return; | |
442 | } | |
443 | ||
444 | $url = new moodle_url($this->reporturl, $this->displayoptions); | |
445 | $url->param('sesskey', sesskey()); | |
446 | ||
447 | echo '<div id="tablecontainer">'; | |
448 | echo '<form id="attemptsform" method="post" action="' . $url->out_omit_querystring() . '">'; | |
449 | ||
450 | echo html_writer::input_hidden_params($url); | |
451 | echo '<div>'; | |
452 | } | |
453 | ||
454 | public function wrap_html_finish() { | |
455 | if ($this->is_downloading() || !$this->includecheckboxes) { | |
456 | return; | |
457 | } | |
458 | ||
459 | echo '<div id="commands">'; | |
460 | echo '<a href="javascript:select_all_in(\'DIV\', null, \'tablecontainer\');">' . | |
461 | get_string('selectall', 'quiz') . '</a> / '; | |
462 | echo '<a href="javascript:deselect_all_in(\'DIV\', null, \'tablecontainer\');">' . | |
463 | get_string('selectnone', 'quiz') . '</a> '; | |
464 | echo ' '; | |
465 | $this->submit_buttons(); | |
466 | echo '</div>'; | |
467 | // Close form | |
468 | echo '</div>'; | |
469 | echo '</form></div>'; | |
470 | } | |
471 | ||
472 | /** | |
473 | * Output any submit buttons required by the $this->includecheckboxes form. | |
474 | */ | |
475 | protected function submit_buttons() { | |
476 | global $PAGE; | |
477 | if (has_capability('mod/quiz:deleteattempts', $this->context)) { | |
478 | echo '<input type="submit" id="deleteattemptsbutton" name="delete" value="' . | |
479 | get_string('deleteselected', 'quiz_overview') . '"/>'; | |
480 | $PAGE->requires->event_handler('#deleteattemptsbutton', 'click', 'M.util.show_confirm_dialog', | |
481 | array('message' => get_string('deleteattemptcheck', 'quiz'))); | |
482 | } | |
483 | } | |
484 | } |