MDL-40259 - For SCORM reports limit fields returned from
[moodle.git] / mod / scorm / report / basic / report.php
CommitLineData
62b82cbc
AKA
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 * Core Report class of basic reporting plugin
5e85f7b5
PS
18 * @package scormreport
19 * @subpackage basic
20 * @author Dan Marsden and Ankit Kumar Agarwal
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62b82cbc
AKA
22 */
23
b2614417
DM
24defined('MOODLE_INTERNAL') || die();
25
62b82cbc
AKA
26class scorm_basic_report extends scorm_default_report {
27 /**
b2614417
DM
28 * displays the full report
29 * @param stdClass $scorm full SCORM object
30 * @param stdClass $cm - full course_module object
31 * @param stdClass $course - full course object
32 * @param string $download - type of download being requested
62b82cbc 33 */
b2614417
DM
34 function display($scorm, $cm, $course, $download) {
35 global $CFG, $DB, $OUTPUT, $PAGE;
2ceaefb4 36
62b82cbc 37 $contextmodule= get_context_instance(CONTEXT_MODULE, $cm->id);
b2614417 38 $action = optional_param('action', '', PARAM_ALPHA);
18bd7573 39 $attemptids = optional_param_array('attemptid', array(), PARAM_RAW);
b27917ee
DM
40 $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT);
41 $PAGE->set_url(new moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode)));
62b82cbc 42
b2614417
DM
43 if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
44 if (scorm_delete_responses($attemptids, $scorm)) { //delete responses.
45 add_to_log($course->id, 'scorm', 'delete attempts', 'report.php?id=' . $cm->id, implode(",", $attemptids), $cm->id);
46 echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
47 }
48 }
6073fcbe
AKA
49 // find out current groups mode
50 $currentgroup = groups_get_activity_group($cm, true);
62b82cbc
AKA
51
52 // detailed report
b2614417 53 $mform = new mod_scorm_report_settings($PAGE->url, compact('currentgroup'));
62b82cbc
AKA
54 if ($fromform = $mform->get_data()) {
55 $detailedrep = $fromform->detailedrep;
56 $pagesize = $fromform->pagesize;
62b82cbc
AKA
57 set_user_preference('scorm_report_detailed', $detailedrep);
58 set_user_preference('scorm_report_pagesize', $pagesize);
59 } else {
60 $detailedrep = get_user_preferences('scorm_report_detailed', false);
61 $pagesize = get_user_preferences('scorm_report_pagesize', 0);
62b82cbc
AKA
62 }
63 if ($pagesize < 1) {
64 $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
65 }
66
67 // select group menu
5e3014df
DM
68 $displayoptions = array();
69 $displayoptions['attemptsmode'] = $attemptsmode;
62b82cbc
AKA
70 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
71 if (!$download) {
5e3014df 72 groups_print_activity_menu($cm, new moodle_url($PAGE->url, $displayoptions));
62b82cbc
AKA
73 }
74 }
75
76 // We only want to show the checkbox to delete attempts
77 // if the user has permissions and if the report mode is showing attempts.
78 $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule)
79 && ($attemptsmode!= SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO);
80 // select the students
81 $nostudents = false;
82
83 if (empty($currentgroup)) {
84 // all users who can attempt scoes
66d00225 85 if (!$students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', '', '', false)) {
62b82cbc
AKA
86 echo $OUTPUT->notification(get_string('nostudentsyet'));
87 $nostudents = true;
88 $allowedlist = '';
89 } else {
f17d78ca 90 $allowedlist = array_keys($students);
62b82cbc 91 }
66d00225 92 unset($students);
62b82cbc
AKA
93 } else {
94 // all users who can attempt scoes and who are in the currently selected group
66d00225 95 if (!$groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', $currentgroup, '', false)) {
62b82cbc
AKA
96 echo $OUTPUT->notification(get_string('nostudentsingroup'));
97 $nostudents = true;
98 $groupstudents = array();
99 }
f17d78ca 100 $allowedlist = array_keys($groupstudents);
66d00225 101 unset($groupstudents);
62b82cbc
AKA
102 }
103
104 if ( !$nostudents ) {
105 // Now check if asked download of data
e4aec5be 106 $coursecontext = context_course::instance($course->id);
62b82cbc 107 if ($download) {
8ebbb06a
SH
108 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
109 $filename = clean_filename("$shortname ".format_string($scorm->name, true));
62b82cbc
AKA
110 }
111
112 // Define table columns
113 $columns = array();
114 $headers = array();
115 if (!$download && $candelete) {
116 $columns[]= 'checkbox';
117 $headers[]= null;
118 }
119 if (!$download && $CFG->grade_report_showuserimage) {
120 $columns[]= 'picture';
121 $headers[]= '';
122 }
123 $columns[]= 'fullname';
124 $headers[]= get_string('name');
e4aec5be 125 $extrafields = get_extra_user_fields($coursecontext);
126 foreach ($extrafields as $field) {
127 $columns[] = $field;
128 $headers[] = get_user_field_name($field);
62b82cbc 129 }
e4aec5be 130
62b82cbc
AKA
131 $columns[]= 'attempt';
132 $headers[]= get_string('attempt', 'scorm');
133 $columns[]= 'start';
134 $headers[]= get_string('started', 'scorm');
135 $columns[]= 'finish';
136 $headers[]= get_string('last', 'scorm');
137 $columns[]= 'score';
138 $headers[]= get_string('score', 'scorm');
139 if ($detailedrep && $scoes = $DB->get_records('scorm_scoes', array("scorm"=>$scorm->id), 'id')) {
140 foreach ($scoes as $sco) {
141 if ($sco->launch!='') {
142 $columns[]= 'scograde'.$sco->id;
143 $headers[]= format_string($sco->title);
62b82cbc
AKA
144 }
145 }
146 } else {
147 $scoes = null;
148 }
149
150 if (!$download) {
151 $table = new flexible_table('mod-scorm-report');
152
153 $table->define_columns($columns);
154 $table->define_headers($headers);
b2614417 155 $table->define_baseurl($PAGE->url);
62b82cbc
AKA
156
157 $table->sortable(true);
158 $table->collapsible(true);
159
e12459ba 160 // This is done to prevent redundant data, when a user has multiple attempts
62b82cbc
AKA
161 $table->column_suppress('picture');
162 $table->column_suppress('fullname');
e4aec5be 163 foreach ($extrafields as $field) {
164 $table->column_suppress($field);
165 }
62b82cbc
AKA
166
167 $table->no_sorting('start');
168 $table->no_sorting('finish');
169 $table->no_sorting('score');
170 if ( $scoes ) {
171 foreach ($scoes as $sco) {
172 if ($sco->launch!='') {
173 $table->no_sorting('scograde'.$sco->id);
174 }
175 }
176 }
177
178 $table->column_class('picture', 'picture');
179 $table->column_class('fullname', 'bold');
180 $table->column_class('score', 'bold');
181
182 $table->set_attribute('cellspacing', '0');
183 $table->set_attribute('id', 'attempts');
184 $table->set_attribute('class', 'generaltable generalbox');
185
186 // Start working -- this is necessary as soon as the niceties are over
187 $table->setup();
188 } else if ($download =='ODS') {
189 require_once("$CFG->libdir/odslib.class.php");
190
191 $filename .= ".ods";
192 // Creating a workbook
193 $workbook = new MoodleODSWorkbook("-");
194 // Sending HTTP headers
195 $workbook->send($filename);
196 // Creating the first worksheet
197 $sheettitle = get_string('report', 'scorm');
a530b69d 198 $myxls = $workbook->add_worksheet($sheettitle);
62b82cbc 199 // format types
a530b69d 200 $format = $workbook->add_format();
62b82cbc 201 $format->set_bold(0);
a530b69d 202 $formatbc = $workbook->add_format();
62b82cbc
AKA
203 $formatbc->set_bold(1);
204 $formatbc->set_align('center');
a530b69d 205 $formatb = $workbook->add_format();
62b82cbc 206 $formatb->set_bold(1);
a530b69d 207 $formaty = $workbook->add_format();
62b82cbc 208 $formaty->set_bg_color('yellow');
a530b69d 209 $formatc = $workbook->add_format();
62b82cbc 210 $formatc->set_align('center');
a530b69d 211 $formatr = $workbook->add_format();
62b82cbc
AKA
212 $formatr->set_bold(1);
213 $formatr->set_color('red');
214 $formatr->set_align('center');
a530b69d 215 $formatg = $workbook->add_format();
62b82cbc
AKA
216 $formatg->set_bold(1);
217 $formatg->set_color('green');
218 $formatg->set_align('center');
219 // Here starts workshhet headers
220
221 $colnum = 0;
222 foreach ($headers as $item) {
223 $myxls->write(0, $colnum, $item, $formatbc);
224 $colnum++;
225 }
226 $rownum=1;
227 } else if ($download =='Excel') {
228 require_once("$CFG->libdir/excellib.class.php");
229
230 $filename .= ".xls";
231 // Creating a workbook
232 $workbook = new MoodleExcelWorkbook("-");
233 // Sending HTTP headers
234 $workbook->send($filename);
235 // Creating the first worksheet
236 $sheettitle = get_string('report', 'scorm');
a530b69d 237 $myxls = $workbook->add_worksheet($sheettitle);
62b82cbc 238 // format types
a530b69d 239 $format = $workbook->add_format();
62b82cbc 240 $format->set_bold(0);
a530b69d 241 $formatbc = $workbook->add_format();
62b82cbc
AKA
242 $formatbc->set_bold(1);
243 $formatbc->set_align('center');
a530b69d 244 $formatb = $workbook->add_format();
62b82cbc 245 $formatb->set_bold(1);
a530b69d 246 $formaty = $workbook->add_format();
62b82cbc 247 $formaty->set_bg_color('yellow');
a530b69d 248 $formatc = $workbook->add_format();
62b82cbc 249 $formatc->set_align('center');
a530b69d 250 $formatr = $workbook->add_format();
62b82cbc
AKA
251 $formatr->set_bold(1);
252 $formatr->set_color('red');
253 $formatr->set_align('center');
a530b69d 254 $formatg = $workbook->add_format();
62b82cbc
AKA
255 $formatg->set_bold(1);
256 $formatg->set_color('green');
257 $formatg->set_align('center');
258
259 $colnum = 0;
260 foreach ($headers as $item) {
261 $myxls->write(0, $colnum, $item, $formatbc);
262 $colnum++;
263 }
264 $rownum=1;
265 } else if ($download=='CSV') {
266 $filename .= ".txt";
267 header("Content-Type: application/download\n");
268 header("Content-Disposition: attachment; filename=\"$filename\"");
269 header("Expires: 0");
270 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
271 header("Pragma: public");
272 echo implode("\t", $headers)." \n";
273 }
f17d78ca
AKA
274 $params = array();
275 list($usql, $params) = $DB->get_in_or_equal($allowedlist, SQL_PARAMS_NAMED);
62b82cbc
AKA
276 // Construct the SQL
277 $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
278 $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' .
e4aec5be 279 'u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, u.imagealt, u.email' .
2ceaefb4 280 get_extra_user_fields_sql($coursecontext, 'u', '', array('email', 'idnumber')) . ' ';
62b82cbc
AKA
281
282 // This part is the same for all cases - join users and scorm_scoes_track tables
283 $from = 'FROM {user} u ';
284 $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id;
285 switch ($attemptsmode) {
286 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
287 // Show only students with attempts
f17d78ca 288 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NOT NULL';
62b82cbc
AKA
289 break;
290 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
291 // Show only students without attempts
f17d78ca 292 $where = ' WHERE u.id ' .$usql. ' AND st.userid IS NULL';
62b82cbc
AKA
293 break;
294 case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
295 // Show all students with or without attempts
f17d78ca 296 $where = ' WHERE u.id ' .$usql. ' AND (st.userid IS NOT NULL OR st.userid IS NULL)';
62b82cbc
AKA
297 break;
298 }
299
300 $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, ';
301 $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'st.attempt').')) AS nbattempts, ';
302 $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
303 $countsql .= $from.$where;
62b82cbc
AKA
304
305 if (!$download) {
306 $sort = $table->get_sql_sort();
307 } else {
308 $sort = '';
309 }
310 // Fix some wired sorting
311 if (empty($sort)) {
312 $sort = ' ORDER BY uniqueid';
313 } else {
314 $sort = ' ORDER BY '.$sort;
315 }
316
317 if (!$download) {
318 // Add extra limits due to initials bar
319 list($twhere, $tparams) = $table->get_sql_where();
320 if ($twhere) {
321 $where .= ' AND '.$twhere; //initial bar
322 $params = array_merge($params, $tparams);
323 }
324
325 if (!empty($countsql)) {
f17d78ca 326 $count = $DB->get_record_sql($countsql, $params);
62b82cbc
AKA
327 $totalinitials = $count->nbresults;
328 if ($twhere) {
329 $countsql .= ' AND '.$twhere;
330 }
331 $count = $DB->get_record_sql($countsql, $params);
332 $total = $count->nbresults;
333 }
334
335 $table->pagesize($pagesize, $total);
336
337 echo '<div class="quizattemptcounts">';
338 if ( $count->nbresults == $count->nbattempts ) {
339 echo get_string('reportcountattempts', 'scorm', $count);
340 } else if ( $count->nbattempts>0 ) {
341 echo get_string('reportcountallattempts', 'scorm', $count);
342 } else {
343 echo $count->nbusers.' '.get_string('users');
344 }
345 echo '</div>';
346 }
347
348 // Fetch the attempts
349 if (!$download) {
350 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params,
351 $table->get_page_start(), $table->get_page_size());
352 echo '<div id="scormtablecontainer">';
353 if ($candelete) {
354 // Start form
355 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
5e3014df 356 echo '<form id="attemptsform" method="post" action="' . $PAGE->url->out(false) .
b2614417 357 '" onsubmit="return confirm(\''.$strreallydel.'\');">';
62b82cbc
AKA
358 echo '<input type="hidden" name="action" value="delete"/>';
359 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
360 echo '<div style="display: none;">';
b2614417 361 echo html_writer::input_hidden_params($PAGE->url);
62b82cbc
AKA
362 echo '</div>';
363 echo '<div>';
364 }
365 $table->initialbars($totalinitials>20); // Build table rows
366 } else {
367 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params);
368 }
369
370 if ($attempts) {
371 foreach ($attempts as $scouser) {
372 $row = array();
373 if (!empty($scouser->attempt)) {
374 $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
503dfba8
DM
375 } else {
376 $timetracks = '';
62b82cbc
AKA
377 }
378 if (in_array('checkbox', $columns)) {
379 if ($candelete && !empty($timetracks->start)) {
380 $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $scouser->attempt . '" />';
381 } else if ($candelete) {
382 $row[] = '';
383 }
384 }
385 if (in_array('picture', $columns)) {
386 $user = (object)array(
387 'id'=>$scouser->userid,
388 'picture'=>$scouser->picture,
389 'imagealt'=>$scouser->imagealt,
390 'email'=>$scouser->email,
391 'firstname'=>$scouser->firstname,
392 'lastname'=>$scouser->lastname);
393 $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
394 }
395 if (!$download) {
396 $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&amp;course='.$course->id.'">'.fullname($scouser).'</a>';
397 } else {
398 $row[] = fullname($scouser);
399 }
e4aec5be 400 foreach ($extrafields as $field) {
401 $row[] = s($scouser->{$field});
62b82cbc
AKA
402 }
403 if (empty($timetracks->start)) {
404 $row[] = '-';
405 $row[] = '-';
406 $row[] = '-';
407 $row[] = '-';
408 } else {
409 if (!$download) {
410 $row[] = '<a href="userreport.php?a='.$scorm->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$scouser->attempt.'">'.$scouser->attempt.'</a>';
411 } else {
412 $row[] = $scouser->attempt;
413 }
414 if ($download =='ODS' || $download =='Excel' ) {
415 $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
416 } else {
417 $row[] = userdate($timetracks->start);
418 }
419 if ($download =='ODS' || $download =='Excel' ) {
420 $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
421 } else {
422 $row[] = userdate($timetracks->finish);
423 }
424 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
425 }
426 // print out all scores of attempt
427 if ($scoes) {
428 foreach ($scoes as $sco) {
429 if ($sco->launch!='') {
430 if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
431 if ($trackdata->status == '') {
432 $trackdata->status = 'notattempted';
433 }
434 $strstatus = get_string($trackdata->status, 'scorm');
435 // if raw score exists, print it
436 if ($trackdata->score_raw != '') {
437 $score = $trackdata->score_raw;
438 // add max score if it exists
e6402b54 439 if (scorm_version_check($scorm->version, SCORM_13)) {
62b82cbc
AKA
440 $maxkey = 'cmi.score.max';
441 } else {
442 $maxkey = 'cmi.core.score.max';
443 }
444 if (isset($trackdata->$maxkey)) {
445 $score .= '/'.$trackdata->$maxkey;
446 }
447 // else print out status
448 } else {
449 $score = $strstatus;
450 }
451 if (!$download) {
452 $row[] = '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>
453 <a href="userreport.php?b='.$sco->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$scouser->attempt.
454 '" title="'.get_string('details', 'scorm').'">'.$score.'</a>';
455 } else {
456 $row[] = $score;
457 }
458 } else {
459 // if we don't have track data, we haven't attempted yet
460 $strstatus = get_string('notattempted', 'scorm');
461 if (!$download) {
462 $row[] = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>'.$strstatus;
463 } else {
464 $row[] = $strstatus;
465 }
466 }
467 }
468 }
469 }
470
471 if (!$download) {
472 $table->add_data($row);
473 } else if ($download == 'Excel' or $download == 'ODS') {
474 $colnum = 0;
475 foreach ($row as $item) {
476 $myxls->write($rownum, $colnum, $item, $format);
477 $colnum++;
478 }
479 $rownum++;
480 } else if ($download=='CSV') {
481 $text = implode("\t", $row);
482 echo $text." \n";
483 }
484 }
485 if (!$download) {
486 $table->finish_output();
487 if ($candelete) {
488 echo '<table id="commands">';
489 echo '<tr><td>';
490 echo '<a href="javascript:select_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
491 get_string('selectall', 'scorm').'</a> / ';
492 echo '<a href="javascript:deselect_all_in(\'DIV\', null, \'scormtablecontainer\');">'.
493 get_string('selectnone', 'scorm').'</a> ';
494 echo '&nbsp;&nbsp;';
495 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
496 echo '</td></tr></table>';
497 // Close form
498 echo '</div>';
499 echo '</form>';
500 }
501 echo '</div>';
502 if (!empty($attempts)) {
503 echo '<table class="boxaligncenter"><tr>';
504 echo '<td>';
5e3014df
DM
505 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
506 array('download'=>'ODS') + $displayoptions),
507 get_string('downloadods'));
62b82cbc
AKA
508 echo "</td>\n";
509 echo '<td>';
5e3014df
DM
510 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
511 array('download'=>'Excel') + $displayoptions),
512 get_string('downloadexcel'));
62b82cbc
AKA
513 echo "</td>\n";
514 echo '<td>';
5e3014df
DM
515 echo $OUTPUT->single_button(new moodle_url($PAGE->url,
516 array('download'=>'CSV') + $displayoptions),
517 get_string('downloadtext'));
62b82cbc
AKA
518 echo "</td>\n";
519 echo "<td>";
520 echo "</td>\n";
521 echo '</tr></table>';
522 }
523 }
62b82cbc 524 } else {
59666dae
AKA
525 if ($candelete && !$download) {
526 echo '</div>';
527 echo '</form>';
2af0e472 528 $table->finish_output();
59666dae
AKA
529 }
530 echo '</div>';
2af0e472
AKA
531 }
532 // Show preferences form irrespective of attempts are there to report or not
533 if (!$download) {
534 $mform->set_data(compact('detailedrep', 'pagesize', 'attemptsmode'));
535 $mform->display();
62b82cbc
AKA
536 }
537 if ($download == 'Excel' or $download == 'ODS') {
538 $workbook->close();
539 exit;
540 } else if ($download == 'CSV') {
541 exit;
542 }
543 } else {
544 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
545 }
62b82cbc 546 }// function ends
62b82cbc 547}