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