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