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