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