Merge branch 'MDL-60494-stray_img_end-39' of https://github.com/leonstr/moodle into...
[moodle.git] / mod / scorm / report / objectives / classes / 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 objectives SCORM report plugin
18  * @package   scormreport_objectives
19  * @author    Dan Marsden <dan@danmarsden.com>
20  * @copyright 2013 Dan Marsden
21  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 namespace scormreport_objectives;
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/mod/scorm/report/objectives/responsessettings_form.php');
30 /**
31  * Objectives report class
32  *
33  * @copyright  2013 Dan Marsden
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class report extends \mod_scorm\report {
37     /**
38      * displays the full report
39      * @param \stdClass $scorm full SCORM object
40      * @param \stdClass $cm - full course_module object
41      * @param \stdClass $course - full course object
42      * @param string $download - type of download being requested
43      */
44     public function display($scorm, $cm, $course, $download) {
45         global $CFG, $DB, $OUTPUT, $PAGE;
47         $contextmodule = \context_module::instance($cm->id);
48         $action = optional_param('action', '', PARAM_ALPHA);
49         $attemptids = optional_param_array('attemptid', array(), PARAM_RAW);
50         $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT);
51         $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode)));
53         if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
54             if (scorm_delete_responses($attemptids, $scorm)) { // Delete responses.
55                 echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
56             }
57         }
58         // Find out current groups mode.
59         $currentgroup = groups_get_activity_group($cm, true);
61         // Detailed report.
62         $mform = new \mod_scorm_report_objectives_settings($PAGE->url, compact('currentgroup'));
63         if ($fromform = $mform->get_data()) {
64             $pagesize = $fromform->pagesize;
65             $showobjectivescore = $fromform->objectivescore;
66             set_user_preference('scorm_report_pagesize', $pagesize);
67             set_user_preference('scorm_report_objectives_score', $showobjectivescore);
68         } else {
69             $pagesize = get_user_preferences('scorm_report_pagesize', 0);
70             $showobjectivescore = get_user_preferences('scorm_report_objectives_score', 0);
71         }
72         if ($pagesize < 1) {
73             $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
74         }
76         // Select group menu.
77         $displayoptions = array();
78         $displayoptions['attemptsmode'] = $attemptsmode;
79         $displayoptions['objectivescore'] = $showobjectivescore;
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' => \context_course::instance($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;
95         list($allowedlistsql, $params) = get_enrolled_sql($contextmodule, 'mod/scorm:savetrack', (int) $currentgroup);
96         if (empty($currentgroup)) {
97             // All users who can attempt scoes.
98             if (!$DB->record_exists_sql($allowedlistsql, $params)) {
99                 echo $OUTPUT->notification(get_string('nostudentsyet'));
100                 $nostudents = true;
101             }
102         } else {
103             // All users who can attempt scoes and who are in the currently selected group.
104             if (!$DB->record_exists_sql($allowedlistsql, $params)) {
105                 echo $OUTPUT->notification(get_string('nostudentsingroup'));
106                 $nostudents = true;
107             }
108         }
109         if ( !$nostudents ) {
110             // Now check if asked download of data.
111             $coursecontext = \context_course::instance($course->id);
112             if ($download) {
113                 $filename = clean_filename("$course->shortname ".format_string($scorm->name, true, $formattextoptions));
114             }
116             // Define table columns.
117             $columns = array();
118             $headers = array();
119             if (!$download && $candelete) {
120                 $columns[] = 'checkbox';
121                 $headers[] = $this->generate_master_checkbox();
122             }
123             if (!$download && $CFG->grade_report_showuserimage) {
124                 $columns[] = 'picture';
125                 $headers[] = '';
126             }
127             $columns[] = 'fullname';
128             $headers[] = get_string('name');
130             $extrafields = get_extra_user_fields($coursecontext);
131             foreach ($extrafields as $field) {
132                 $columns[] = $field;
133                 $headers[] = get_user_field_name($field);
134             }
135             $columns[] = 'attempt';
136             $headers[] = get_string('attempt', 'scorm');
137             $columns[] = 'start';
138             $headers[] = get_string('started', 'scorm');
139             $columns[] = 'finish';
140             $headers[] = get_string('last', 'scorm');
141             $columns[] = 'score';
142             $headers[] = get_string('score', 'scorm');
143             $scoes = $DB->get_records('scorm_scoes', array("scorm" => $scorm->id), 'sortorder, id');
144             foreach ($scoes as $sco) {
145                 if ($sco->launch != '') {
146                     $columns[] = 'scograde'.$sco->id;
147                     $headers[] = format_string($sco->title, '', $formattextoptions);
148                 }
149             }
151             // Construct the SQL.
152             $select = 'SELECT DISTINCT '.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').' AS uniqueid, ';
153             $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' .
154                     \user_picture::fields('u', array('idnumber'), 'userid') .
155                     get_extra_user_fields_sql($coursecontext, 'u', '', array('email', 'idnumber')) . ' ';
157             // This part is the same for all cases - join users and scorm_scoes_track tables.
158             $from = 'FROM {user} u ';
159             $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = '.$scorm->id;
160             switch ($attemptsmode) {
161                 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
162                     // Show only students with attempts.
163                     $where = " WHERE u.id IN ({$allowedlistsql}) AND st.userid IS NOT NULL";
164                     break;
165                 case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
166                     // Show only students without attempts.
167                     $where = " WHERE u.id IN ({$allowedlistsql}) AND st.userid IS NULL";
168                     break;
169                 case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
170                     // Show all students with or without attempts.
171                     $where = " WHERE u.id IN ({$allowedlistsql}) AND (st.userid IS NOT NULL OR st.userid IS NULL)";
172                     break;
173             }
175             $countsql = 'SELECT COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)').')) AS nbresults, ';
176             $countsql .= 'COUNT(DISTINCT('.$DB->sql_concat('u.id', '\'#\'', 'st.attempt').')) AS nbattempts, ';
177             $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
178             $countsql .= $from.$where;
180             $nbmaincolumns = count($columns); // Get number of main columns used.
182             $objectives = get_scorm_objectives($scorm->id);
183             $nosort = array();
184             foreach ($objectives as $scoid => $sco) {
185                 foreach ($sco as $id => $objectivename) {
186                     $colid = $scoid.'objectivestatus' . $id;
187                     $columns[] = $colid;
188                     $nosort[] = $colid;
190                     if (!$displayoptions['objectivescore']) {
191                         // Display the objective name only.
192                         $headers[] = $objectivename;
193                     } else {
194                         // Display the objective status header with a "status" suffix to avoid confusion.
195                         $headers[] = $objectivename. ' '. get_string('status', 'scormreport_objectives');
197                         // Now print objective score headers.
198                         $colid = $scoid.'objectivescore' . $id;
199                         $columns[] = $colid;
200                         $nosort[] = $colid;
201                         $headers[] = $objectivename. ' '. get_string('score', 'scormreport_objectives');
202                     }
203                 }
204             }
206             $emptycell = ''; // Used when an empty cell is being printed - in html we add a space.
207             if (!$download) {
208                 $emptycell = '&nbsp;';
209                 $table = new \flexible_table('mod-scorm-report');
211                 $table->define_columns($columns);
212                 $table->define_headers($headers);
213                 $table->define_baseurl($PAGE->url);
215                 $table->sortable(true);
216                 $table->collapsible(true);
218                 // This is done to prevent redundant data, when a user has multiple attempts.
219                 $table->column_suppress('picture');
220                 $table->column_suppress('fullname');
221                 foreach ($extrafields as $field) {
222                     $table->column_suppress($field);
223                 }
224                 foreach ($nosort as $field) {
225                     $table->no_sorting($field);
226                 }
228                 $table->no_sorting('start');
229                 $table->no_sorting('finish');
230                 $table->no_sorting('score');
231                 $table->no_sorting('checkbox');
232                 $table->no_sorting('picture');
234                 foreach ($scoes as $sco) {
235                     if ($sco->launch != '') {
236                         $table->no_sorting('scograde'.$sco->id);
237                     }
238                 }
240                 $table->column_class('picture', 'picture');
241                 $table->column_class('fullname', 'bold');
242                 $table->column_class('score', 'bold');
244                 $table->set_attribute('cellspacing', '0');
245                 $table->set_attribute('id', 'attempts');
246                 $table->set_attribute('class', 'generaltable generalbox');
248                 // Start working -- this is necessary as soon as the niceties are over.
249                 $table->setup();
250             } else if ($download == 'ODS') {
251                 require_once("$CFG->libdir/odslib.class.php");
253                 $filename .= ".ods";
254                 // Creating a workbook.
255                 $workbook = new \MoodleODSWorkbook("-");
256                 // Sending HTTP headers.
257                 $workbook->send($filename);
258                 // Creating the first worksheet.
259                 $sheettitle = get_string('report', 'scorm');
260                 $myxls = $workbook->add_worksheet($sheettitle);
261                 // Format types.
262                 $format = $workbook->add_format();
263                 $format->set_bold(0);
264                 $formatbc = $workbook->add_format();
265                 $formatbc->set_bold(1);
266                 $formatbc->set_align('center');
267                 $formatb = $workbook->add_format();
268                 $formatb->set_bold(1);
269                 $formaty = $workbook->add_format();
270                 $formaty->set_bg_color('yellow');
271                 $formatc = $workbook->add_format();
272                 $formatc->set_align('center');
273                 $formatr = $workbook->add_format();
274                 $formatr->set_bold(1);
275                 $formatr->set_color('red');
276                 $formatr->set_align('center');
277                 $formatg = $workbook->add_format();
278                 $formatg->set_bold(1);
279                 $formatg->set_color('green');
280                 $formatg->set_align('center');
281                 // Here starts workshhet headers.
283                 $colnum = 0;
284                 foreach ($headers as $item) {
285                     $myxls->write(0, $colnum, $item, $formatbc);
286                     $colnum++;
287                 }
288                 $rownum = 1;
289             } else if ($download == 'Excel') {
290                 require_once("$CFG->libdir/excellib.class.php");
292                 $filename .= ".xls";
293                 // Creating a workbook.
294                 $workbook = new \MoodleExcelWorkbook("-");
295                 // Sending HTTP headers.
296                 $workbook->send($filename);
297                 // Creating the first worksheet.
298                 $sheettitle = get_string('report', 'scorm');
299                 $myxls = $workbook->add_worksheet($sheettitle);
300                 // Format types.
301                 $format = $workbook->add_format();
302                 $format->set_bold(0);
303                 $formatbc = $workbook->add_format();
304                 $formatbc->set_bold(1);
305                 $formatbc->set_align('center');
306                 $formatb = $workbook->add_format();
307                 $formatb->set_bold(1);
308                 $formaty = $workbook->add_format();
309                 $formaty->set_bg_color('yellow');
310                 $formatc = $workbook->add_format();
311                 $formatc->set_align('center');
312                 $formatr = $workbook->add_format();
313                 $formatr->set_bold(1);
314                 $formatr->set_color('red');
315                 $formatr->set_align('center');
316                 $formatg = $workbook->add_format();
317                 $formatg->set_bold(1);
318                 $formatg->set_color('green');
319                 $formatg->set_align('center');
321                 $colnum = 0;
322                 foreach ($headers as $item) {
323                     $myxls->write(0, $colnum, $item, $formatbc);
324                     $colnum++;
325                 }
326                 $rownum = 1;
327             } else if ($download == 'CSV') {
328                 $csvexport = new \csv_export_writer("tab");
329                 $csvexport->set_filename($filename, ".txt");
330                 $csvexport->add_data($headers);
331             }
333             if (!$download) {
334                 $sort = $table->get_sql_sort();
335             } else {
336                 $sort = '';
337             }
338             // Fix some wired sorting.
339             if (empty($sort)) {
340                 $sort = ' ORDER BY uniqueid';
341             } else {
342                 $sort = ' ORDER BY '.$sort;
343             }
345             if (!$download) {
346                 // Add extra limits due to initials bar.
347                 list($twhere, $tparams) = $table->get_sql_where();
348                 if ($twhere) {
349                     $where .= ' AND '.$twhere; // Initial bar.
350                     $params = array_merge($params, $tparams);
351                 }
353                 if (!empty($countsql)) {
354                     $count = $DB->get_record_sql($countsql, $params);
355                     $totalinitials = $count->nbresults;
356                     if ($twhere) {
357                         $countsql .= ' AND '.$twhere;
358                     }
359                     $count = $DB->get_record_sql($countsql, $params);
360                     $total  = $count->nbresults;
361                 }
363                 $table->pagesize($pagesize, $total);
365                 echo \html_writer::start_div('scormattemptcounts');
366                 if ( $count->nbresults == $count->nbattempts ) {
367                     echo get_string('reportcountattempts', 'scorm', $count);
368                 } else if ( $count->nbattempts > 0 ) {
369                     echo get_string('reportcountallattempts', 'scorm', $count);
370                 } else {
371                     echo $count->nbusers.' '.get_string('users');
372                 }
373                 echo \html_writer::end_div();
374             }
376             // Fetch the attempts.
377             if (!$download) {
378                 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params,
379                 $table->get_page_start(), $table->get_page_size());
380                 echo \html_writer::start_div('', array('id' => 'scormtablecontainer'));
381                 if ($candelete) {
382                     // Start form.
383                     $strreallydel  = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
384                     echo \html_writer::start_tag('form', array('id' => 'attemptsform', 'method' => 'post',
385                                                                 'action' => $PAGE->url->out(false),
386                                                                 'onsubmit' => 'return confirm("'.$strreallydel.'");'));
387                     echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete'));
388                     echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
389                     echo \html_writer::start_div('', array('style' => 'display: none;'));
390                     echo \html_writer::input_hidden_params($PAGE->url);
391                     echo \html_writer::end_div();
392                     echo \html_writer::start_div();
393                 }
394                 $table->initialbars($totalinitials > 20); // Build table rows.
395             } else {
396                 $attempts = $DB->get_records_sql($select.$from.$where.$sort, $params);
397             }
398             if ($attempts) {
399                 foreach ($attempts as $scouser) {
400                     $row = array();
401                     if (!empty($scouser->attempt)) {
402                         $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
403                     } else {
404                         $timetracks = '';
405                     }
406                     if (in_array('checkbox', $columns)) {
407                         if ($candelete && !empty($timetracks->start)) {
408                             $row[] = $this->generate_row_checkbox('attemptid[]', "{$scouser->userid}:{$scouser->attempt}");
409                         } else if ($candelete) {
410                             $row[] = '';
411                         }
412                     }
413                     if (in_array('picture', $columns)) {
414                         $user = new \stdClass();
415                         $additionalfields = explode(',', \user_picture::fields());
416                         $user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
417                         $user->id = $scouser->userid;
418                         $row[] = $OUTPUT->user_picture($user, array('courseid' => $course->id));
419                     }
420                     if (!$download) {
421                         $url = new \moodle_url('/user/view.php', array('id' => $scouser->userid, 'course' => $course->id));
422                         $row[] = \html_writer::link($url, fullname($scouser));
423                     } else {
424                         $row[] = fullname($scouser);
425                     }
426                     foreach ($extrafields as $field) {
427                         $row[] = s($scouser->{$field});
428                     }
429                     if (empty($timetracks->start)) {
430                         $row[] = '-';
431                         $row[] = '-';
432                         $row[] = '-';
433                         $row[] = '-';
434                     } else {
435                         if (!$download) {
436                             $url = new \moodle_url('/mod/scorm/report/userreport.php', array('id' => $cm->id,
437                                     'user' => $scouser->userid, 'attempt' => $scouser->attempt));
438                             $row[] = \html_writer::link($url, $scouser->attempt);
439                         } else {
440                             $row[] = $scouser->attempt;
441                         }
442                         if ($download == 'ODS' || $download == 'Excel' ) {
443                             $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
444                         } else {
445                             $row[] = userdate($timetracks->start);
446                         }
447                         if ($download == 'ODS' || $download == 'Excel' ) {
448                             $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
449                         } else {
450                             $row[] = userdate($timetracks->finish);
451                         }
452                         $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
453                     }
454                     // Print out all scores of attempt.
455                     foreach ($scoes as $sco) {
456                         if ($sco->launch != '') {
457                             if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
458                                 if ($trackdata->status == '') {
459                                     $trackdata->status = 'notattempted';
460                                 }
461                                 $strstatus = get_string($trackdata->status, 'scorm');
463                                 if ($trackdata->score_raw != '') { // If raw score exists, print it.
464                                     $score = $trackdata->score_raw;
465                                     // Add max score if it exists.
466                                     if (isset($trackdata->score_max)) {
467                                         $score .= '/'.$trackdata->score_max;
468                                     }
470                                 } else { // ...else print out status.
471                                     $score = $strstatus;
472                                 }
473                                 if (!$download) {
474                                     $url = new \moodle_url('/mod/scorm/report/userreporttracks.php', array('id' => $cm->id,
475                                         'scoid' => $sco->id, 'user' => $scouser->userid, 'attempt' => $scouser->attempt));
476                                     $row[] = $OUTPUT->pix_icon($trackdata->status, $strstatus, 'scorm') . '<br>' .
477                                         \html_writer::link($url, $score, array('title' => get_string('details', 'scorm')));
478                                 } else {
479                                     $row[] = $score;
480                                 }
481                                 // Iterate over tracks and match objective id against values.
482                                 $scorm2004 = false;
483                                 if (scorm_version_check($scorm->version, SCORM_13)) {
484                                     $scorm2004 = true;
485                                     $objectiveprefix = "cmi.objectives.";
486                                 } else {
487                                     $objectiveprefix = "cmi.objectives_";
488                                 }
490                                 $keywords = array(".id", $objectiveprefix);
491                                 $objectivestatus = array();
492                                 $objectivescore = array();
493                                 foreach ($trackdata as $name => $value) {
494                                     if (strpos($name, $objectiveprefix) === 0 && strrpos($name, '.id') !== false) {
495                                         $num = trim(str_ireplace($keywords, '', $name));
496                                         if (is_numeric($num)) {
497                                             if ($scorm2004) {
498                                                 $element = $objectiveprefix.$num.'.completion_status';
499                                             } else {
500                                                 $element = $objectiveprefix.$num.'.status';
501                                             }
502                                             if (isset($trackdata->$element)) {
503                                                 $objectivestatus[$value] = $trackdata->$element;
504                                             } else {
505                                                 $objectivestatus[$value] = '';
506                                             }
507                                             if ($displayoptions['objectivescore']) {
508                                                 $element = $objectiveprefix.$num.'.score.raw';
509                                                 if (isset($trackdata->$element)) {
510                                                     $objectivescore[$value] = $trackdata->$element;
511                                                 } else {
512                                                     $objectivescore[$value] = '';
513                                                 }
514                                             }
515                                         }
516                                     }
517                                 }
519                                 // Interaction data.
520                                 if (!empty($objectives[$trackdata->scoid])) {
521                                     foreach ($objectives[$trackdata->scoid] as $name) {
522                                         if (isset($objectivestatus[$name])) {
523                                             $row[] = s($objectivestatus[$name]);
524                                         } else {
525                                             $row[] = $emptycell;
526                                         }
527                                         if ($displayoptions['objectivescore']) {
528                                             if (isset($objectivescore[$name])) {
529                                                 $row[] = s($objectivescore[$name]);
530                                             } else {
531                                                 $row[] = $emptycell;
532                                             }
534                                         }
535                                     }
536                                 }
537                                 // End of interaction data.
538                             } else {
539                                 // If we don't have track data, we haven't attempted yet.
540                                 $strstatus = get_string('notattempted', 'scorm');
541                                 if (!$download) {
542                                     $row[] = $OUTPUT->pix_icon('notattempted', $strstatus, 'scorm') . '<br>' . $strstatus;
543                                 } else {
544                                     $row[] = $strstatus;
545                                 }
546                                 // Complete the empty cells.
547                                 for ($i = 0; $i < count($columns) - $nbmaincolumns; $i++) {
548                                     $row[] = $emptycell;
549                                 }
550                             }
551                         }
552                     }
554                     if (!$download) {
555                         $table->add_data($row);
556                     } else if ($download == 'Excel' or $download == 'ODS') {
557                         $colnum = 0;
558                         foreach ($row as $item) {
559                             $myxls->write($rownum, $colnum, $item, $format);
560                             $colnum++;
561                         }
562                         $rownum++;
563                     } else if ($download == 'CSV') {
564                         $csvexport->add_data($row);
565                     }
566                 }
567                 if (!$download) {
568                     $table->finish_output();
569                     if ($candelete) {
570                         echo \html_writer::start_tag('table', array('id' => 'commands'));
571                         echo \html_writer::start_tag('tr').\html_writer::start_tag('td');
572                         echo $this->generate_delete_selected_button();
573                         echo \html_writer::end_tag('td').\html_writer::end_tag('tr').\html_writer::end_tag('table');
574                         // Close form.
575                         echo \html_writer::end_tag('div');
576                         echo \html_writer::end_tag('form');
577                     }
578                     echo \html_writer::end_div();
579                     if (!empty($attempts)) {
580                         echo \html_writer::start_tag('table', array('class' => 'boxaligncenter')).\html_writer::start_tag('tr');
581                         echo \html_writer::start_tag('td');
582                         echo $OUTPUT->single_button(new \moodle_url($PAGE->url,
583                                                                    array('download' => 'ODS') + $displayoptions),
584                                                                    get_string('downloadods'),
585                                                                    'post',
586                                                                    ['class' => 'mt-1']);
587                         echo \html_writer::end_tag('td');
588                         echo \html_writer::start_tag('td');
589                         echo $OUTPUT->single_button(new \moodle_url($PAGE->url,
590                                                                    array('download' => 'Excel') + $displayoptions),
591                                                                    get_string('downloadexcel'),
592                                                                    'post',
593                                                                    ['class' => 'mt-1']);
594                         echo \html_writer::end_tag('td');
595                         echo \html_writer::start_tag('td');
596                         echo $OUTPUT->single_button(new \moodle_url($PAGE->url,
597                                                                    array('download' => 'CSV') + $displayoptions),
598                                                                    get_string('downloadtext'),
599                                                                    'post',
600                                                                    ['class' => 'mt-1']);
601                         echo \html_writer::end_tag('td');
602                         echo \html_writer::start_tag('td');
603                         echo \html_writer::end_tag('td');
604                         echo \html_writer::end_tag('tr').\html_writer::end_tag('table');
605                     }
606                 }
607             } else {
608                 if ($candelete && !$download) {
609                     echo \html_writer::end_div();
610                     echo \html_writer::end_tag('form');
611                     $table->finish_output();
612                 }
613                 echo \html_writer::end_div();
614             }
615             // Show preferences form irrespective of attempts are there to report or not.
616             if (!$download) {
617                 $mform->set_data(compact('pagesize', 'attemptsmode'));
618                 $mform->display();
619             }
620             if ($download == 'Excel' or $download == 'ODS') {
621                 $workbook->close();
622                 exit;
623             } else if ($download == 'CSV') {
624                 $csvexport->download_file();
625                 exit;
626             }
627         } else {
628             echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
629         }
630     }// Function ends.
633 /**
634  * Returns The maximum numbers of Objectives associated with an Scorm Pack
635  *
636  * @param int $scormid Scorm instance id
637  * @return array an array of possible objectives.
638  */
639 function get_scorm_objectives($scormid) {
640     global $DB;
641     $objectives = array();
642     $params = array();
643     $select = "scormid = ? AND ";
644     $select .= $DB->sql_like("element", "?", false);
645     $params[] = $scormid;
646     $params[] = "cmi.objectives%.id";
647     $value = $DB->sql_compare_text('value');
648     $rs = $DB->get_recordset_select("scorm_scoes_track", $select, $params, 'value', "DISTINCT $value AS value, scoid");
649     if ($rs->valid()) {
650         foreach ($rs as $record) {
651             $objectives[$record->scoid][] = $record->value;
652         }
653         // Now naturally sort the sco arrays.
654         foreach ($objectives as $scoid => $sco) {
655             natsort($objectives[$scoid]);
656         }
657     }
658     $rs->close();
659     return $objectives;