Merge branch 'm20_MDL-26176_cleanup' of github.com:danmarsden/moodle
[moodle.git] / mod / scorm / report.php
1 <?php
3 // This script uses installed report plugins to print quiz reports
5     require_once("../../config.php");
6     require_once($CFG->libdir.'/tablelib.php');
7     require_once($CFG->dirroot.'/mod/scorm/locallib.php');
8     require_once($CFG->dirroot.'/mod/scorm/reportsettings_form.php');
9     require_once($CFG->libdir.'/formslib.php');
10     define('SCORM_REPORT_DEFAULT_PAGE_SIZE', 20);
11     define('SCORM_REPORT_ATTEMPTS_ALL_STUDENTS', 0);
12     define('SCORM_REPORT_ATTEMPTS_STUDENTS_WITH', 1);
13     define('SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO', 2);
15     $id = optional_param('id', '', PARAM_INT);    // Course Module ID, or
16     $a = optional_param('a', '', PARAM_INT);     // SCORM ID
17     $b = optional_param('b', '', PARAM_INT);     // SCO ID
18     $user = optional_param('user', '', PARAM_INT);  // User ID
19     $attempt = optional_param('attempt', '1', PARAM_INT);  // attempt number
20     $action     = optional_param('action', '', PARAM_ALPHA);
21     $attemptids = optional_param('attemptid', array(), PARAM_RAW);
22     $download = optional_param('download', '', PARAM_RAW);
24     $url = new moodle_url('/mod/scorm/report.php');
25     if ($user !== '') {
26         $url->param('user', $user);
27     }
28     if ($attempt !== '1') {
29         $url->param('attempt', $attempt);
30     }
31     if ($action !== '') {
32         $url->param('action', $action);
33     }
35     if (!empty($id)) {
36         $url->param('id', $id);
37         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
38             print_error('invalidcoursemodule');
39         }
40         if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
41             print_error('coursemisconf');
42         }
43         if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
44             print_error('invalidcoursemodule');
45         }
46     } else {
47         if (!empty($b)) {
48             $url->param('b', $b);
49             if (! $sco = $DB->get_record('scorm_scoes', array('id'=>$b))) {
50                 print_error('invalidactivity', 'scorm');
51             }
52             $a = $sco->scorm;
53         }
54         if (!empty($a)) {
55             $url->param('a', $a);
56             if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
57                 print_error('invalidcoursemodule');
58             }
59             if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
60                 print_error('coursemisconf');
61             }
62             if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
63                 print_error('invalidcoursemodule');
64             }
65         }
66     }
67     $PAGE->set_url($url);
69     require_login($course->id, false, $cm);
71     $contextmodule = get_context_instance(CONTEXT_MODULE,$cm->id);
73     require_capability('mod/scorm:viewreport', $contextmodule);
75     add_to_log($course->id, 'scorm', 'report', 'report.php?id='.$cm->id, $scorm->id, $cm->id);
77     if (!empty($user)) {
78         $userdata = scorm_get_user_data($user);
79     } else {
80         $userdata = null;
81     }
82     if (!empty($download)) {
83         $noheader = true;
84     }
85 /// Print the page header
86     if (empty($noheader)) {
88         $strscorms = get_string('modulenameplural', 'scorm');
89         $strscorm  = get_string('modulename', 'scorm');
90         $strreport  = get_string('report', 'scorm');
91         $strattempt  = get_string('attempt', 'scorm');
92         $strname  = get_string('name');
94         $PAGE->set_title("$course->shortname: ".format_string($scorm->name));
95         $PAGE->set_heading($course->fullname);
96         $PAGE->navbar->add($strreport, new moodle_url('/mod/scorm/report.php', array('id'=>$cm->id)));
98         if (empty($b)) {
99             if (!empty($a)) {
100                 $PAGE->navbar->add("$strattempt $attempt - ".fullname($userdata));
101             }
102         } else {
103             $PAGE->navbar->add("$strattempt $attempt - ".fullname($userdata), new moodle_url('/mod/scorm/report.php', array('a'=>$a, 'user'=>$user, 'attempt'=>$attempt)));
104             $PAGE->navbar->add($sco->title);
105         }
106         echo $OUTPUT->header();
107         $currenttab = 'reports';
108         require($CFG->dirroot . '/mod/scorm/tabs.php');
109         echo $OUTPUT->heading(format_string($scorm->name));
110     }
112     if ($action == 'delete' && has_capability('mod/scorm:deleteresponses',$contextmodule) && confirm_sesskey()) {
113         if (scorm_delete_responses($attemptids, $scorm)) { //delete responses.
114             add_to_log($course->id, 'scorm', 'delete attempts', 'report.php?id=' . $cm->id, implode(",", $attemptids), $cm->id);
115             echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
116         }
117     }
119     if (empty($b)) {
120         if (empty($a)) {
121             // No options, show the global scorm report
122                 $pageoptions = array();
123                 $pageoptions['id'] = $cm->id;
124                 $reporturl = new moodle_url($CFG->wwwroot.'/mod/scorm/report.php', $pageoptions);
126                 // find out current groups mode
127                 $currentgroup = groups_get_activity_group($cm, true);
129                 // detailed report
130                 $mform = new mod_scorm_report_settings( $reporturl, compact('currentgroup') );
131                 if ($fromform = $mform->get_data()) {
132                     $detailedrep = $fromform->detailedrep;
133                     $pagesize = $fromform->pagesize;
134                     $attemptsmode = $fromform->attemptsmode;
135                     set_user_preference('scorm_report_detailed', $detailedrep);
136                     set_user_preference('scorm_report_pagesize', $pagesize);
137                 } else {
138                     $detailedrep = get_user_preferences('scorm_report_detailed', false);
139                     $pagesize = get_user_preferences('scorm_report_pagesize', 0);
140                     $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_STUDENTS_WITH, PARAM_INT);
141                 }
142                 if ($pagesize < 1) {
143                     $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
144                 }
146                 // select group menu
147                 $displayoptions = array();
148                 $displayoptions['id'] = $cm->id;
149                 $displayoptions['attemptsmode'] = $attemptsmode;
150                 $reporturlwithdisplayoptions = new moodle_url($CFG->wwwroot.'/mod/scorm/report.php', $displayoptions);
152                 if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used
153                     if (!$download) {
154                         groups_print_activity_menu($cm, $reporturlwithdisplayoptions->out());
155                     }
156                 }
158                 // We only want to show the checkbox to delete attempts
159                 // if the user has permissions and if the report mode is showing attempts.
160                 $candelete = has_capability('mod/scorm:deleteresponses',$contextmodule)
161                         && ($attemptsmode!= SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO);
162                 // select the students
163                 $nostudents = false;
165                 if (empty($currentgroup)) {
166                     // all users who can attempt scoes
167                     if (!$students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack','','','','','','',false)){
168                         echo $OUTPUT->notification(get_string('nostudentsyet'));
169                         $nostudents = true;
170                         $allowedlist = '';
171                     } else {
172                         $allowedlist = join(',',array_keys($students));
173                     }
174                 } else {
175                     // all users who can attempt scoes and who are in the currently selected group
176                     if (!$groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack','','','','',$currentgroup,'',false)){
177                         echo $OUTPUT->notification(get_string('nostudentsingroup'));
178                         $nostudents = true;
179                         $groupstudents = array();
180                     }
181                     $allowedlist = join(',', array_keys($groupstudents));
182                 }
184                 if( !$nostudents ) {
186                     // Now check if asked download of data
187                     if ($download) {
188                         $filename = clean_filename("$course->shortname ".format_string($scorm->name,true));
189                     }
191                     // Define table columns
192                     $columns = array();
193                     $headers = array();
194                     if (!$download && $candelete) {
195                         $columns[]= 'checkbox';
196                         $headers[]= NULL;
197                     }
198                     if (!$download && $CFG->grade_report_showuserimage) {
199                         $columns[]= 'picture';
200                         $headers[]= '';
201                     }
202                     $columns[]= 'fullname';
203                     $headers[]= get_string('name');
204                     if ($CFG->grade_report_showuseridnumber) {
205                         $columns[]= 'idnumber';
206                         $headers[]= get_string('idnumber');
207                     }
208                     $columns[]= 'attempt';
209                     $headers[]= get_string('attempt', 'scorm');
210                     $columns[]= 'start';
211                     $headers[]= get_string('started','scorm');
212                     $columns[]= 'finish';
213                     $headers[]= get_string('last','scorm');
214                     $columns[]= 'score';
215                     $headers[]= get_string('score','scorm');
216                     if ($detailedrep && $scoes = $DB->get_records('scorm_scoes',array("scorm"=>$scorm->id),'id')) {
217                         foreach ($scoes as $sco) {
218                             if ($sco->launch!='') {
219                                 $columns[]= 'scograde'.$sco->id;
220                                 $headers[]= format_string($sco->title);
221                                 $table->head[]= format_string($sco->title);
222                             }
223                         }
224                     } else {
225                         $scoes = NULL;
226                     }
228                     if (!$download) {
229                         $table = new flexible_table('mod-scorm-report');
231                         $table->define_columns($columns);
232                         $table->define_headers($headers);
233                         $table->define_baseurl($reporturlwithdisplayoptions->out());
235                         $table->sortable(true);
236                         $table->collapsible(true);
238                         $table->column_suppress('picture');
239                         $table->column_suppress('fullname');
240                         $table->column_suppress('idnumber');
242                         $table->no_sorting('start');
243                         $table->no_sorting('finish');
244                         $table->no_sorting('score');
245                         if( $scoes ) {
246                             foreach ($scoes as $sco) {
247                                 if ($sco->launch!='') {
248                                     $table->no_sorting('scograde'.$sco->id);
249                                 }
250                             }
251                         }
253                         $table->column_class('picture', 'picture');
254                         $table->column_class('fullname', 'bold');
255                         $table->column_class('score', 'bold');
257                         $table->set_attribute('cellspacing', '0');
258                         $table->set_attribute('id', 'attempts');
259                         $table->set_attribute('class', 'generaltable generalbox');
261                         // Start working -- this is necessary as soon as the niceties are over
262                         $table->setup();
263                     } else if ($download =='ODS') {
264                         require_once("$CFG->libdir/odslib.class.php");
266                         $filename .= ".ods";
267                         // Creating a workbook
268                         $workbook = new MoodleODSWorkbook("-");
269                         // Sending HTTP headers
270                         $workbook->send($filename);
271                         // Creating the first worksheet
272                         $sheettitle = get_string('report','scorm');
273                         $myxls =& $workbook->add_worksheet($sheettitle);
274                         // format types
275                         $format =& $workbook->add_format();
276                         $format->set_bold(0);
277                         $formatbc =& $workbook->add_format();
278                         $formatbc->set_bold(1);
279                         $formatbc->set_align('center');
280                         $formatb =& $workbook->add_format();
281                         $formatb->set_bold(1);
282                         $formaty =& $workbook->add_format();
283                         $formaty->set_bg_color('yellow');
284                         $formatc =& $workbook->add_format();
285                         $formatc->set_align('center');
286                         $formatr =& $workbook->add_format();
287                         $formatr->set_bold(1);
288                         $formatr->set_color('red');
289                         $formatr->set_align('center');
290                         $formatg =& $workbook->add_format();
291                         $formatg->set_bold(1);
292                         $formatg->set_color('green');
293                         $formatg->set_align('center');
294                         // Here starts workshhet headers
296                         $colnum = 0;
297                         foreach ($headers as $item) {
298                             $myxls->write(0,$colnum,$item,$formatbc);
299                             $colnum++;
300                         }
301                         $rownum=1;
302                     } else if ($download =='Excel') {
303                         require_once("$CFG->libdir/excellib.class.php");
305                         $filename .= ".xls";
306                         // Creating a workbook
307                         $workbook = new MoodleExcelWorkbook("-");
308                         // Sending HTTP headers
309                         $workbook->send($filename);
310                         // Creating the first worksheet
311                         $sheettitle = get_string('report','scorm');
312                         $myxls =& $workbook->add_worksheet($sheettitle);
313                         // format types
314                         $format =& $workbook->add_format();
315                         $format->set_bold(0);
316                         $formatbc =& $workbook->add_format();
317                         $formatbc->set_bold(1);
318                         $formatbc->set_align('center');
319                         $formatb =& $workbook->add_format();
320                         $formatb->set_bold(1);
321                         $formaty =& $workbook->add_format();
322                         $formaty->set_bg_color('yellow');
323                         $formatc =& $workbook->add_format();
324                         $formatc->set_align('center');
325                         $formatr =& $workbook->add_format();
326                         $formatr->set_bold(1);
327                         $formatr->set_color('red');
328                         $formatr->set_align('center');
329                         $formatg =& $workbook->add_format();
330                         $formatg->set_bold(1);
331                         $formatg->set_color('green');
332                         $formatg->set_align('center');
334                         $colnum = 0;
335                         foreach ($headers as $item) {
336                             $myxls->write(0,$colnum,$item,$formatbc);
337                             $colnum++;
338                         }
339                         $rownum=1;
340                     } else if ($download=='CSV') {
341                         $filename .= ".txt";
342                         header("Content-Type: application/download\n");
343                         header("Content-Disposition: attachment; filename=\"$filename\"");
344                         header("Expires: 0");
345                         header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
346                         header("Pragma: public");
347                         echo implode("\t", $headers)." \n";
348                     }
350                     // Construct the SQL
351                     $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
352                     $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' .
353                             'u.id AS userid, u.idnumber, u.firstname, u.lastname, u.picture, u.imagealt, u.email ';
355                     // This part is the same for all cases - join users and scorm_scoes_track tables
356                     $from = 'FROM {user} u ';
357                     $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id;
358                     switch ($attemptsmode){
359                         case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
360                             // Show only students with attempts
361                             $where = ' WHERE u.id IN (' .$allowedlist. ') AND st.userid IS NOT NULL';
362                             break;
363                         case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
364                             // Show only students without attempts
365                             $where = ' WHERE u.id IN (' .$allowedlist. ') AND st.userid IS NULL';
366                             break;
367                         case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
368                             // Show all students with or without attempts
369                             $where = ' WHERE u.id IN (' .$allowedlist. ') AND (st.userid IS NOT NULL OR st.userid IS NULL)';
370                             break;
371                     }
373                     $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, ';
374                     $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'','st.attempt').')) AS nbattempts, ';
375                     $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
376                     $countsql .= $from.$where;
377                     $params = array();
379                     if (!$download) {
380                         $sort = $table->get_sql_sort();
381                     }
382                     else {
383                         $sort = '';
384                     }
385                     // Fix some wired sorting
386                     if (empty($sort)) {
387                         $sort = ' ORDER BY uniqueid';
388                     } else {
389                         $sort = ' ORDER BY '.$sort;
390                     }
392                     if (!$download) {
393                         // Add extra limits due to initials bar
394                         list($twhere, $tparams) = $table->get_sql_where();
395                         if ($twhere) {
396                             $where .= ' AND '.$twhere; //initial bar
397                             $params = array_merge($params, $tparams);
398                         }
400                         if (!empty($countsql)) {
401                             $count = $DB->get_record_sql($countsql);
402                             $totalinitials = $count->nbresults;
403                             if ($twhere) {
404                                 $countsql .= ' AND '.$twhere;
405                             }
406                             $count = $DB->get_record_sql($countsql, $params);
407                             $total  = $count->nbresults;
408                         }
410                         $table->pagesize($pagesize, $total);
412                         echo '<div class="quizattemptcounts">';
413                         if ( $count->nbresults == $count->nbattempts ) {
414                             echo get_string('reportcountattempts','scorm', $count);
415                         } else if( $count->nbattempts>0 ) {
416                             echo get_string('reportcountallattempts','scorm', $count);
417                         } else {
418                             echo $count->nbusers.' '.get_string('users');
419                         }
420                         echo '</div>';
421                     }
423                     // Fetch the attempts
424                     if (!$download) {
425                         $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params,
426                                                 $table->get_page_start(), $table->get_page_size());
427                         echo '<div id="scormtablecontainer">';
428                         if ($candelete) {
429                             // Start form
430                             $strreallydel  = addslashes_js(get_string('deleteattemptcheck','scorm'));
431                             echo '<form id="attemptsform" method="post" action="' . $reporturlwithdisplayoptions->out(true) .
432                                     '" onsubmit="return confirm(\''.$strreallydel.'\');">';
433                             echo '<input type="hidden" name="action" value="delete"/>';
434                             echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
435                             echo '<div style="display: none;">';
436                             echo html_writer::input_hidden_params($reporturlwithdisplayoptions);
437                             echo '</div>';
438                             echo '<div>';
439                         }
440                         $table->initialbars($totalinitials>20); // Build table rows
441                     } else {
442                         $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params);
443                     }
445                     if ($attempts) {
446                         foreach($attempts as $scouser){
447                             $row = array();
448                             if (!empty($scouser->attempt)) {
449                                 $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
450                             }
451                             if (in_array('checkbox', $columns)){
452                                 if ($candelete && !empty($timetracks->start)) {
453                                     $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $scouser->attempt . '" />';
454                                 } else if($candelete) {
455                                     $row[] = '';
456                                 }
457                             }
458                             if (in_array('picture', $columns)){
459                                 $user = (object)array('id'=>$scouser->userid,
460                                                       'picture'=>$scouser->picture,
461                                                       'imagealt'=>$scouser->imagealt,
462                                                       'email'=>$scouser->email,
463                                                       'firstname'=>$scouser->firstname,
464                                                       'lastname'=>$scouser->lastname);
465                                 $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
466                             }
467                             if (!$download){
468                                 $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&amp;course='.$course->id.'">'.fullname($scouser).'</a>';
469                             } else {
470                                 $row[] = fullname($scouser);
471                             }
472                             if (in_array('idnumber', $columns)){
473                                 $row[] = $scouser->idnumber;
474                             }
475                             if (empty($timetracks->start)) {
476                                 $row[] = '-';
477                                 $row[] = '-';
478                                 $row[] = '-';
479                                 $row[] = '-';
480                             } else {
481                                 if (!$download) $row[] = '<a href="report.php?a='.$scorm->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$scouser->attempt.'">'.$scouser->attempt.'</a>';
482                                 else $row[] = $scouser->attempt;
483                                 if ($download =='ODS' || $download =='Excel' ) $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
484                                 else $row[] = userdate($timetracks->start);
485                                 if ($download =='ODS' || $download =='Excel' ) $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
486                                 else $row[] = userdate($timetracks->finish);
488                                 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
489                             }
490                             // print out all scores of attempt
491                             if ($scoes) {
492                                 foreach ($scoes as $sco) {
493                                     if ($sco->launch!='') {
494                                         if ($trackdata = scorm_get_tracks($sco->id,$scouser->userid,$scouser->attempt)) {
495                                             if ($trackdata->status == '') {
496                                                 $trackdata->status = 'notattempted';
497                                             }
498                                             $strstatus = get_string($trackdata->status, 'scorm');
499                                             // if raw score exists, print it
500                                             if ($trackdata->score_raw != '') {
501                                                 $score = $trackdata->score_raw;
502                                                 // add max score if it exists
503                                                 if ($scorm->version == 'SCORM_1.3') {
504                                                     $maxkey = 'cmi.score.max';
505                                                 } else {
506                                                     $maxkey = 'cmi.core.score.max';
507                                                 }
508                                                 if (isset($trackdata->$maxkey)) {
509                                                     $score .= '/'.$trackdata->$maxkey;
510                                                 }
511                                             // else print out status
512                                             } else {
513                                                 $score = $strstatus;
514                                             }
515                                             if (!$download) {
516                                                 $row[] = '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>
517                                                           <a href="report.php?b='.$sco->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$scouser->attempt.
518                                                          '" title="'.get_string('details','scorm').'">'.$score.'</a>';
519                                             } else {
520                                                 $row[] = $score;
521                                             }
522                                         } else {
523                                             // if we don't have track data, we haven't attempted yet
524                                             $strstatus = get_string('notattempted', 'scorm');
525                                             if (!$download) {
526                                                 $row[] = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" /><br/>'.$strstatus;
527                                             } else {
528                                                 $row[] = $strstatus;
529                                             }
530                                         }
531                                     }
532                                 }
533                             }
535                             if (!$download) {
536                                 $table->add_data($row);
537                             } else if ($download == 'Excel' or $download == 'ODS') {
538                                 $colnum = 0;
539                                 foreach($row as $item){
540                                     $myxls->write($rownum,$colnum,$item,$format);
541                                     $colnum++;
542                                 }
543                                 $rownum++;
544                             } else if ($download=='CSV') {
545                                 $text = implode("\t", $row);
546                                 echo $text." \n";
547                             }
548                         }
549                         if (!$download) {
550                             $table->finish_output();
551                             if ($candelete) {
552                                 echo '<table id="commands">';
553                                 echo '<tr><td>';
554                                 echo '<a href="javascript:select_all_in(\'DIV\',null,\'scormtablecontainer\');">'.
555                                         get_string('selectall', 'scorm').'</a> / ';
556                                 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'scormtablecontainer\');">'.
557                                         get_string('selectnone', 'scorm').'</a> ';
558                                 echo '&nbsp;&nbsp;';
559                                 echo '<input type="submit" value="'.get_string('deleteselected', 'quiz_overview').'"/>';
560                                 echo '</td></tr></table>';
561                                 // Close form
562                                 echo '</div>';
563                                 echo '</form>';
564                             }
565                             echo '</div>';
566                             if (!empty($attempts)) {
567                                 echo '<table class="boxaligncenter"><tr>';
568                                 echo '<td>';
569                                 echo $OUTPUT->single_button(new moodle_url('/mod/scorm/report.php', $pageoptions + $displayoptions + array('download' => 'ODS')), get_string('downloadods'));
570                                 echo "</td>\n";
571                                 echo '<td>';
572                                 echo $OUTPUT->single_button(new moodle_url('/mod/scorm/report.php', $pageoptions + $displayoptions + array('download' => 'Excel')), get_string('downloadexcel'));
573                                 echo "</td>\n";
574                                 echo '<td>';
575                                 echo $OUTPUT->single_button(new moodle_url('/mod/scorm/report.php', $pageoptions + $displayoptions + array('download' => 'CSV')), get_string('downloadtext'));
576                                 echo "</td>\n";
577                                 echo "<td>";
578                                 echo "</td>\n";
579                                 echo '</tr></table>';
580                         }
581                     }
582                     if (!$download) {
583                         $mform->set_data($displayoptions + compact('detailedrep', 'pagesize'));
584                         $mform->display();
585                     }
586                 } else {
587                     echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
588                 }
589                 if ($download == 'Excel' or $download == 'ODS') {
590                     $workbook->close();
591                     exit;
592                 } else if ($download == 'CSV') {
593                     exit;
594                 }
595             } else {
596                 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
597             }
598         } else {
599             if (!empty($user)) {
600                 // User SCORM report
601                 if ($scoes = $DB->get_records_select('scorm_scoes',"scorm=? ORDER BY id", array($scorm->id))) {
602                     if (!empty($userdata)) {
603                         echo $OUTPUT->box_start('generalbox boxaligncenter');
604                         echo '<div class="mdl-align">'."\n";
605                         $userrec = (object)array('id'=>$user);
606                         echo $OUTPUT->user_picture($userrec, array('courseid'=>$course->id));
607                         echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&amp;course=$course->id\">".
608                              "$userdata->firstname $userdata->lastname</a><br />";
609                         echo get_string('attempt','scorm').': '.$attempt;
610                         echo '</div>'."\n";
611                         echo $OUTPUT->box_end();
613                         // Print general score data
614                         $table = new html_table();
615                         $table->head = array(get_string('title','scorm'),
616                                              get_string('status','scorm'),
617                                              get_string('time','scorm'),
618                                              get_string('score','scorm'),
619                                              '');
620                         $table->align = array('left', 'center','center','right','left');
621                         $table->wrap = array('nowrap', 'nowrap','nowrap','nowrap','nowrap');
622                         $table->width = '80%';
623                         $table->size = array('*', '*', '*', '*', '*');
624                         foreach ($scoes as $sco) {
625                             if ($sco->launch!='') {
626                                 $row = array();
627                                 $score = '&nbsp;';
628                                 if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
629                                     if ($trackdata->score_raw != '') {
630                                         $score = $trackdata->score_raw;
631                                     }
632                                     if ($trackdata->status == '') {
633                                         $trackdata->status = 'notattempted';
634                                     }
635                                     $detailslink = '<a href="report.php?b='.$sco->id.'&amp;user='.$user.'&amp;attempt='.$attempt.'" title="'.
636                                                     get_string('details','scorm').'">'.get_string('details','scorm').'</a>';
637                                 } else {
638                                     $trackdata->status = 'notattempted';
639                                     $trackdata->total_time = '&nbsp;';
640                                     $detailslink = '&nbsp;';
641                                 }
642                                 $strstatus = get_string($trackdata->status,'scorm');
643                                 $row[] = '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
644                                          $strstatus.'" />&nbsp;'.format_string($sco->title);
645                                 $row[] = get_string($trackdata->status,'scorm');
646                                 $row[] = scorm_format_duration($trackdata->total_time);
647                                 $row[] = $score;
648                                 $row[] = $detailslink;
649                             } else {
650                                 $row = array(format_string($sco->title), '&nbsp;', '&nbsp;', '&nbsp;', '&nbsp;');
651                             }
652                             $table->data[] = $row;
653                         }
654                         echo html_writer::table($table);
655                     }
656                 }
657             } else {
658                 notice('No users to report');
659             }
660         }
661     } else {
662         // User SCO report
663         if (!empty($userdata)) {
664             echo $OUTPUT->box_start('generalbox boxaligncenter');
665             //print_heading(format_string($sco->title));
666             echo $OUTPUT->heading('<a href="'.$CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;mode=browse&amp;scoid='.$sco->id.'" target="_new">'.format_string($sco->title).'</a>');
667             echo '<div class="mdl-align">'."\n";
668             $userrec = (object)array('id'=>$user);
669             echo $OUTPUT->user_picture($userrec, array('courseid'=>$course->id));
670             echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&amp;course=$course->id\">".
671                  "$userdata->firstname $userdata->lastname</a><br />";
672             $scoreview = '';
673             if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
674                 if ($trackdata->score_raw != '') {
675                     $scoreview = get_string('score','scorm').':&nbsp;'.$trackdata->score_raw;
676                 }
677                 if ($trackdata->status == '') {
678                     $trackdata->status = 'notattempted';
679                 }
680             } else {
681                 $trackdata->status = 'notattempted';
682                 $trackdata->total_time = '';
683             }
684             $strstatus = get_string($trackdata->status,'scorm');
685             echo '<img src="'.$OUTPUT->pix_url($trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
686             $strstatus.'" />&nbsp;'.scorm_format_duration($trackdata->total_time).'<br />'.$scoreview.'<br />';
687             echo '</div>'."\n";
688             echo '<hr /><h2>'.get_string('details','scorm').'</h2>';
690             // Print general score data
691             $table = new html_table();
692             $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
693             $table->align = array('left', 'left');
694             $table->wrap = array('nowrap', 'nowrap');
695             $table->width = '100%';
696             $table->size = array('*', '*');
698             $existelements = false;
699             if ($scorm->version == 'SCORM_1.3') {
700                 $elements = array('raw' => 'cmi.score.raw',
701                                   'min' => 'cmi.score.min',
702                                   'max' => 'cmi.score.max',
703                                   'status' => 'cmi.completion_status',
704                                   'time' => 'cmi.total_time');
705             } else {
706                 $elements = array('raw' => 'cmi.core.score.raw',
707                                   'min' => 'cmi.core.score.min',
708                                   'max' => 'cmi.core.score.max',
709                                   'status' => 'cmi.core.lesson_status',
710                                   'time' => 'cmi.core.total_time');
711             }
712             $printedelements = array();
713             foreach ($elements as $key => $element) {
714                 if (isset($trackdata->$element)) {
715                     $existelements = true;
716                     $printedelements[]=$element;
717                     $row = array();
718                     $row[] = get_string($key,'scorm');
719                     switch($key) {
720                         case 'status':
721                             $row[] = $strstatus;
722                         break;
723                         case 'time':
724                             $row[] = s(scorm_format_duration($trackdata->$element));
725                         break;
726                         default:
727                             s($trackdata->$element);
728                         break;
729                     }
730                     $table->data[] = $row;
731                 }
732             }
733             if ($existelements) {
734                 echo '<h3>'.get_string('general','scorm').'</h3>';
735                 echo html_writer::table($table);
736             }
738             // Print Interactions data
739             $table = new html_table();
740             $table->head = array(get_string('identifier','scorm'),
741                                  get_string('type','scorm'),
742                                  get_string('result','scorm'),
743                                  get_string('student_response','scorm'));
744             $table->align = array('center', 'center', 'center', 'center');
745             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
746             $table->width = '100%';
747             $table->size = array('*', '*', '*', '*', '*');
749             $existinteraction = false;
751             $i = 0;
752             $interactionid = 'cmi.interactions.'.$i.'.id';
754             while (isset($trackdata->$interactionid)) {
755                 $existinteraction = true;
756                 $printedelements[]=$interactionid;
757                 $elements = array($interactionid,
758                                   'cmi.interactions.'.$i.'.type',
759                                   'cmi.interactions.'.$i.'.result',
760                                   'cmi.interactions.'.$i.'.learner_response');
761                 $row = array();
762                 foreach ($elements as $element) {
763                     if (isset($trackdata->$element)) {
764                         $row[] = s($trackdata->$element);
765                         $printedelements[]=$element;
766                     } else {
767                         $row[] = '&nbsp;';
768                     }
769                 }
770                 $table->data[] = $row;
772                 $i++;
773                 $interactionid = 'cmi.interactions.'.$i.'.id';
774             }
775             if ($existinteraction) {
776                 echo '<h3>'.get_string('interactions','scorm').'</h3>';
777                 echo html_writer::table($table);
778             }
780             // Print Objectives data
781             $table = new html_table();
782             $table->head = array(get_string('identifier','scorm'),
783                                  get_string('status','scorm'),
784                                  get_string('raw','scorm'),
785                                  get_string('min','scorm'),
786                                  get_string('max','scorm'));
787             $table->align = array('center', 'center', 'center', 'center', 'center');
788             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
789             $table->width = '100%';
790             $table->size = array('*', '*', '*', '*', '*');
792             $existobjective = false;
794             $i = 0;
795             $objectiveid = 'cmi.objectives.'.$i.'.id';
797             while (isset($trackdata->$objectiveid)) {
798                 $existobjective = true;
799                 $printedelements[]=$objectiveid;
800                 $elements = array($objectiveid,
801                                   'cmi.objectives.'.$i.'.status',
802                                   'cmi.objectives.'.$i.'.score.raw',
803                                   'cmi.objectives.'.$i.'.score.min',
804                                   'cmi.objectives.'.$i.'.score.max');
805                 $row = array();
806                 foreach ($elements as $element) {
807                     if (isset($trackdata->$element)) {
808                         $row[] = s($trackdata->$element);
809                         $printedelements[]=$element;
810                     } else {
811                         $row[] = '&nbsp;';
812                     }
813                 }
814                 $table->data[] = $row;
816                 $i++;
817                 $objectiveid = 'cmi.objectives.'.$i.'.id';
818             }
819             if ($existobjective) {
820                 echo '<h3>'.get_string('objectives','scorm').'</h3>';
821                 echo html_writer::table($table);
822             }
823             $table = new html_table();
824             $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
825             $table->align = array('left', 'left');
826             $table->wrap = array('nowrap', 'wrap');
827             $table->width = '100%';
828             $table->size = array('*', '*');
830             $existelements = false;
832             foreach($trackdata as $element => $value) {
833                 if (substr($element,0,3) == 'cmi') {
834                     if (!(in_array ($element, $printedelements))) {
835                         $existelements = true;
836                         $row = array();
837                         $row[] = get_string($element,'scorm') != '[['.$element.']]' ? get_string($element,'scorm') : $element;
838                         if (strpos($element, '_time') === false) {
839                             $row[] = s($value);
840                         } else {
841                             $row[] = s(scorm_format_duration($value));
842                         }
843                         $table->data[] = $row;
844                     }
845                 }
846             }
847             if ($existelements) {
848                 echo '<h3>'.get_string('othertracks','scorm').'</h3>';
849                 echo html_writer::table($table);
850             }
851             echo $OUTPUT->box_end();
852         } else {
853             print_error('missingparameter');
854         }
855     }
858     if (empty($noheader)) {
859         echo $OUTPUT->footer();
860     }