10d6c4bdd65c26405af4c13d4fd66bef92241c0f
[moodle.git] / mod / scorm / report.php
1 <?php
3 // This script uses installed report plugins to print quiz reports
5     require_once("../../config.php");
6     require_once('locallib.php');
8     $id = optional_param('id', '', PARAM_INT);    // Course Module ID, or
9     $a = optional_param('a', '', PARAM_INT);     // SCORM ID
10     $b = optional_param('b', '', PARAM_INT);     // SCO ID
11     $user = optional_param('user', '', PARAM_INT);  // User ID
12     $attempt = optional_param('attempt', '1', PARAM_INT);  // attempt number
13     $action     = optional_param('action', '', PARAM_ALPHA);
14     $attemptids = optional_param('attemptid', array(), PARAM_RAW); //get array of responses to delete.
16     $url = new moodle_url('/mod/scorm/report.php');
17     if ($user !== '') {
18         $url->param('user', $user);
19     }
20     if ($attempt !== '1') {
21         $url->param('attempt', $attempt);
22     }
23     if ($action !== '') {
24         $url->param('action', $action);
25     }
27     if (!empty($id)) {
28         $url->param('id', $id);
29         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
30             print_error('invalidcoursemodule');
31         }
32         if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
33             print_error('coursemisconf');
34         }
35         if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
36             print_error('invalidcoursemodule');
37         }
38     } else {
39         if (!empty($b)) {
40             $url->param('b', $b);
41             if (! $sco = $DB->get_record('scorm_scoes', array('id'=>$b))) {
42                 print_error('invalidactivity', 'scorm');
43             }
44             $a = $sco->scorm;
45         }
46         if (!empty($a)) {
47             $url->param('a', $a);
48             if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
49                 print_error('invalidcoursemodule');
50             }
51             if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
52                 print_error('coursemisconf');
53             }
54             if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
55                 print_error('invalidcoursemodule');
56             }
57         }
58     }
59     $PAGE->set_url($url);
61     require_login($course->id, false, $cm);
63     $contextmodule = get_context_instance(CONTEXT_MODULE,$cm->id);
65     require_capability('mod/scorm:viewreport', $contextmodule);
67     add_to_log($course->id, 'scorm', 'report', 'report.php?id='.$cm->id, $scorm->id, $cm->id);
69     if (!empty($user)) {
70         $userdata = scorm_get_user_data($user);
71     } else {
72         $userdata = null;
73     }
75 /// Print the page header
76     if (empty($noheader)) {
78         $strscorms = get_string('modulenameplural', 'scorm');
79         $strscorm  = get_string('modulename', 'scorm');
80         $strreport  = get_string('report', 'scorm');
81         $strattempt  = get_string('attempt', 'scorm');
82         $strname  = get_string('name');
84         $PAGE->set_title("$course->shortname: ".format_string($scorm->name));
85         $PAGE->set_heading($course->fullname);
86         $PAGE->navbar->add($strreport, new moodle_url('/mod/scorm/report.php', array('id'=>$cm->id)));
88         if (empty($b)) {
89             if (!empty($a)) {
90                 $PAGE->navbar->add("$strattempt $attempt - ".fullname($userdata));
91             }
92         } else {
93             $PAGE->navbar->add("$strattempt $attempt - ".fullname($userdata), new moodle_url('/mod/scorm/report.php', array('a'=>$a, 'user'=>$user, 'attempt'=>$attempt)));
94             $PAGE->navbar->add($sco->title);
95         }
96         echo $OUTPUT->header();
97         echo $OUTPUT->heading(format_string($scorm->name));
98     }
100     if ($action == 'delete' && has_capability('mod/scorm:deleteresponses',$contextmodule)) {
101         if (scorm_delete_responses($attemptids, $scorm->id)) { //delete responses.
102             echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
103         }
104     }
106     if (empty($b)) {
107         if (empty($a)) {
108             // No options, show the global scorm report
110             if (!empty($cm->groupingid)) {
111                 $sql = "SELECT st.userid, st.scormid
112                         FROM {scorm_scoes_track} st
113                             INNER JOIN {groups_members} gm ON st.userid = gm.userid
114                             INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
115                         WHERE st.scormid = ? AND gg.groupingid = ?
116                         GROUP BY st.userid,st.scormid
117                         ";
118                 $params = array($scorm->id, $cm->groupingid);
119             } else {
120                 $sql = "SELECT st.userid, st.scormid
121                         FROM {scorm_scoes_track} st
122                         WHERE st.scormid = ?
123                         GROUP BY st.userid,st.scormid
124                         ";
125                 $params = array($scorm->id);
126             }
128             if ($scousers=$DB->get_records_sql($sql, $params)) {
129                 $table = new html_table();
130                 $table->head = array();
131                 $table->width = '100%';
132                 if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
133                     $table->head[]  = '&nbsp;';
134                     $table->align[] = 'center';
135                     $table->wrap[]  = 'nowrap';
136                     $table->size[]  = '10';
137                 }
139                 $table->head[]  = '&nbsp;';
140                 $table->align[] = 'center';
141                 $table->wrap[]  = 'nowrap';
142                 $table->size[]  = '10';
144                 $table->head[]  = get_string('name');
145                 $table->align[] = 'left';
146                 $table->wrap[]  = 'nowrap';
147                 $table->size[]  = '*';
149                 $table->head[]= get_string('attempt','scorm');
150                 $table->align[] = 'center';
151                 $table->wrap[] = 'nowrap';
152                 $table->size[] = '*';
154                 $table->head[]= get_string('started','scorm');
155                 $table->align[] = 'center';
156                 $table->wrap[] = 'nowrap';
157                 $table->size[] = '*';
159                 $table->head[]= get_string('last','scorm');
160                 $table->align[] = 'center';
161                 $table->wrap[] = 'nowrap';
162                 $table->size[] = '*';
164                 $table->head[]= get_string('score','scorm');
165                 $table->align[] = 'center';
166                 $table->wrap[] = 'nowrap';
167                 $table->size[] = '*';
169                 foreach($scousers as $scouser){
170                     $userdata = scorm_get_user_data($scouser->userid);
171                     $attempt = scorm_get_last_attempt($scorm->id,$scouser->userid);
172                     for ($a = 1; $a<=$attempt; $a++) {
173                         $row = array();
174                         if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
175                             $row[] = '<input type="checkbox" name="attemptid[]" value="'. $scouser->userid . ':' . $a . '" />';
176                         }
177                         //TODO: fetch the user details elsewhere - this is a performance problem!!
178                         $user = (object)array('id'=>$scouser->userid);
179                         $row[] = $OUTPUT->user_picture($user, array('courseid'=>$course->id));
180                         $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&amp;course='.$course->id.'">'.
181                                  fullname($userdata).'</a>';
182                         $row[] = '<a href="report.php?a='.$scorm->id.'&amp;user='.$scouser->userid.'&amp;attempt='.$a.'">'.$a.'</a>';
183                         $select = 'scormid = ? and userid = ? and attempt = ?';
184                         $params = array($scorm->id, $scouser->userid, $a);
185                         $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $a);
186                         // jump out here if this attempt doesnt exist
187                         if (!$timetracks->start) {
188                             continue;
189                         }
190                         $row[] = userdate($timetracks->start, get_string('strftimedaydatetime'));
191                         $row[] = userdate($timetracks->finish, get_string('strftimedaydatetime'));
193                         $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $a);
194                         $table->data[] = $row;
195                     }
196                 }
197                 echo '<div id="scormtablecontainer">';
198                 if (has_capability('mod/scorm:deleteresponses',$contextmodule)) {
199                     echo '<form id="attemptsform" method="post" action="'.$FULLSCRIPT.'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes_js(get_string('deleteattemptcheck','quiz')).'\' : true);">';
200                     echo '<input type="hidden" name="id" value="'.$id.'">';
201                     echo html_writer::table($table);
202                     echo '<a href="javascript:select_all_in(\'DIV\',null,\'scormtablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
203                     echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'scormtablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
204                     echo '&nbsp;&nbsp;';
205                     echo html_writer::tag('label', get_string('withselected', 'quiz'), array('for'=>'menuaction'));
206                     echo html_writer::select(array('delete' => get_string('delete')), 'action', '', array(''=>'choosedots'), array('id'=>'menuaction'));
207                     $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('attemptsform', 'menuaction', ''));
208                     echo '<noscript id="noscriptmenuaction" style="disaply:inline">';
209                     echo '<div>';
210                     echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';
211                     echo '</form>';
212                 } else {
213                     echo html_writer::table($table);
214                 }
215                 echo '</div>';
216             } else {
217                 echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
218             }
219         } else {
220             if (!empty($user)) {
221                 // User SCORM report
222                 if ($scoes = $DB->get_records_select('scorm_scoes',"scorm=? ORDER BY id", array($scorm->id))) {
223                     if (!empty($userdata)) {
224                         echo $OUTPUT->box_start('generalbox boxaligncenter');
225                         echo '<div class="mdl-align">'."\n";
226                         $userrec = (object)array('id'=>$user);
227                         echo $OUTPUT->user_picture($userrec, array('courseid'=>$course->id));
228                         echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&amp;course=$course->id\">".
229                              "$userdata->firstname $userdata->lastname</a><br />";
230                         echo get_string('attempt','scorm').': '.$attempt;
231                         echo '</div>'."\n";
232                         echo $OUTPUT->box_end();
234                         // Print general score data
235                         $table = new html_table();
236                         $table->head = array(get_string('title','scorm'),
237                                              get_string('status','scorm'),
238                                              get_string('time','scorm'),
239                                              get_string('score','scorm'),
240                                              '');
241                         $table->align = array('left', 'center','center','right','left');
242                         $table->wrap = array('nowrap', 'nowrap','nowrap','nowrap','nowrap');
243                         $table->width = '80%';
244                         $table->size = array('*', '*', '*', '*', '*');
245                         foreach ($scoes as $sco) {
246                             if ($sco->launch!='') {
247                                 $row = array();
248                                 $score = '&nbsp;';
249                                 if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
250                                     if ($trackdata->score_raw != '') {
251                                         $score = $trackdata->score_raw;
252                                     }
253                                     if ($trackdata->status == '') {
254                                         $trackdata->status = 'notattempted';
255                                     }
256                                     $detailslink = '<a href="report.php?b='.$sco->id.'&amp;user='.$user.'&amp;attempt='.$attempt.'" title="'.
257                                                     get_string('details','scorm').'">'.get_string('details','scorm').'</a>';
258                                 } else {
259                                     $trackdata->status = 'notattempted';
260                                     $trackdata->total_time = '&nbsp;';
261                                     $detailslink = '&nbsp;';
262                                 }
263                                 $strstatus = get_string($trackdata->status,'scorm');
264                                 $row[] = '<img src="'.$OUTPUT->pix_url('pix/' . $trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
265                                          $strstatus.'" />&nbsp;'.format_string($sco->title);
266                                 $row[] = get_string($trackdata->status,'scorm');
267                                 $row[] = scorm_format_date_time($trackdata->total_time);
268                                 $row[] = $score;
269                                 $row[] = $detailslink;
270                             } else {
271                                 $row = array(format_string($sco->title), '&nbsp;', '&nbsp;', '&nbsp;', '&nbsp;');
272                             }
273                             $table->data[] = $row;
274                         }
275                         echo html_writer::table($table);
276                     }
277                 }
278             } else {
279                 notice('No users to report');
280             }
281         }
282     } else {
283         // User SCO report
284         if (!empty($userdata)) {
285             echo $OUTPUT->box_start('generalbox boxaligncenter');
286             //print_heading(format_string($sco->title));
287             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>');
288             echo '<div class="mdl-align">'."\n";
289             $userrec = (object)array('id'=>$user);
290             echo $OUTPUT->user_picture($userrec, array('courseid'=>$course->id));
291             echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&amp;course=$course->id\">".
292                  "$userdata->firstname $userdata->lastname</a><br />";
293             $scoreview = '';
294             if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
295                 if ($trackdata->score_raw != '') {
296                     $scoreview = get_string('score','scorm').':&nbsp;'.$trackdata->score_raw;
297                 }
298                 if ($trackdata->status == '') {
299                     $trackdata->status = 'notattempted';
300                 }
301             } else {
302                 $trackdata->status = 'notattempted';
303                 $trackdata->total_time = '';
304             }
305             $strstatus = get_string($trackdata->status,'scorm');
306             echo '<img src="'.$$OUTPUT->pix_url('pix/'.$trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
307             $strstatus.'" />&nbsp;'.scorm_format_date_time($trackdata->total_time).'<br />'.$scoreview.'<br />';
308             echo '</div>'."\n";
309             echo '<hr /><h2>'.get_string('details','scorm').'</h2>';
311             // Print general score data
312             $table = new html_table();
313             $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
314             $table->align = array('left', 'left');
315             $table->wrap = array('nowrap', 'nowrap');
316             $table->width = '100%';
317             $table->size = array('*', '*');
319             $existelements = false;
320             if ($scorm->version == 'SCORM_1.3') {
321                 $elements = array('raw' => 'cmi.score.raw',
322                                   'min' => 'cmi.score.min',
323                                   'max' => 'cmi.score.max',
324                                   'status' => 'cmi.completion_status',
325                                   'time' => 'cmi.total_time');
326             } else {
327                 $elements = array('raw' => 'cmi.core.score.raw',
328                                   'min' => 'cmi.core.score.min',
329                                   'max' => 'cmi.core.score.max',
330                                   'status' => 'cmi.core.lesson_status',
331                                   'time' => 'cmi.core.total_time');
332             }
333             $printedelements = array();
334             foreach ($elements as $key => $element) {
335                 if (isset($trackdata->$element)) {
336                     $existelements = true;
337                     $printedelements[]=$element;
338                     $row = array();
339                     $row[] = get_string($key,'scorm');
340                     if ($key == 'time') {
341                         $row[] = s(scorm_format_date_time($trackdata->$element));
342                     } else {
343                         $row[] = s($trackdata->$element);
344                     }
345                     $table->data[] = $row;
346                 }
347             }
348             if ($existelements) {
349                 echo '<h3>'.get_string('general','scorm').'</h3>';
350                 echo html_writer::table($table);
351             }
353             // Print Interactions data
354             $table = new html_table();
355             $table->head = array(get_string('identifier','scorm'),
356                                  get_string('type','scorm'),
357                                  get_string('result','scorm'),
358                                  get_string('student_response','scorm'));
359             $table->align = array('center', 'center', 'center', 'center');
360             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
361             $table->width = '100%';
362             $table->size = array('*', '*', '*', '*', '*');
364             $existinteraction = false;
366             $i = 0;
367             $interactionid = 'cmi.interactions.'.$i.'.id';
369             while (isset($trackdata->$interactionid)) {
370                 $existinteraction = true;
371                 $printedelements[]=$interactionid;
372                 $elements = array($interactionid,
373                                   'cmi.interactions.'.$i.'.type',
374                                   'cmi.interactions.'.$i.'.result',
375                                   'cmi.interactions.'.$i.'.learner_response');
376                 $row = array();
377                 foreach ($elements as $element) {
378                     if (isset($trackdata->$element)) {
379                         $row[] = s($trackdata->$element);
380                         $printedelements[]=$element;
381                     } else {
382                         $row[] = '&nbsp;';
383                     }
384                 }
385                 $table->data[] = $row;
387                 $i++;
388                 $interactionid = 'cmi.interactions.'.$i.'.id';
389             }
390             if ($existinteraction) {
391                 echo '<h3>'.get_string('interactions','scorm').'</h3>';
392                 echo html_writer::table($table);
393             }
395             // Print Objectives data
396             $table = new html_table();
397             $table->head = array(get_string('identifier','scorm'),
398                                  get_string('status','scorm'),
399                                  get_string('raw','scorm'),
400                                  get_string('min','scorm'),
401                                  get_string('max','scorm'));
402             $table->align = array('center', 'center', 'center', 'center', 'center');
403             $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
404             $table->width = '100%';
405             $table->size = array('*', '*', '*', '*', '*');
407             $existobjective = false;
409             $i = 0;
410             $objectiveid = 'cmi.objectives.'.$i.'.id';
412             while (isset($trackdata->$objectiveid)) {
413                 $existobjective = true;
414                 $printedelements[]=$objectiveid;
415                 $elements = array($objectiveid,
416                                   'cmi.objectives.'.$i.'.status',
417                                   'cmi.objectives.'.$i.'.score.raw',
418                                   'cmi.objectives.'.$i.'.score.min',
419                                   'cmi.objectives.'.$i.'.score.max');
420                 $row = array();
421                 foreach ($elements as $element) {
422                     if (isset($trackdata->$element)) {
423                         $row[] = s($trackdata->$element);
424                         $printedelements[]=$element;
425                     } else {
426                         $row[] = '&nbsp;';
427                     }
428                 }
429                 $table->data[] = $row;
431                 $i++;
432                 $objectiveid = 'cmi.objectives.'.$i.'.id';
433             }
434             if ($existobjective) {
435                 echo '<h3>'.get_string('objectives','scorm').'</h3>';
436                 echo html_writer::table($table);
437             }
438             $table = new html_table();
439             $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
440             $table->align = array('left', 'left');
441             $table->wrap = array('nowrap', 'wrap');
442             $table->width = '100%';
443             $table->size = array('*', '*');
445             $existelements = false;
447             foreach($trackdata as $element => $value) {
448                 if (substr($element,0,3) == 'cmi') {
449                     if (!(in_array ($element, $printedelements))) {
450                         $existelements = true;
451                         $row = array();
452                         $row[] = get_string($element,'scorm') != '[['.$element.']]' ? get_string($element,'scorm') : $element;
453                         if (strpos($element, '_time') === false) {
454                             $row[] = s($value);
455                         } else {
456                             $row[] = s(scorm_format_date_time($value));
457                         }
458                         $table->data[] = $row;
459                     }
460                 }
461             }
462             if ($existelements) {
463                 echo '<h3>'.get_string('othertracks','scorm').'</h3>';
464                 echo html_writer::table($table);
465             }
466             echo $OUTPUT->box_end();
467         } else {
468             print_error('missingparameter');
469         }
470     }
473     if (empty($noheader)) {
474         echo $OUTPUT->footer();
475     }