MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / scorm / report.php
blob6be8b3c544b0b05abb239a3b8876c2dff5946054
1 <?php // $Id$
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
14 if (!empty($id)) {
15 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
16 error('Course Module ID was incorrect');
18 if (! $course = get_record('course', 'id', $cm->course)) {
19 error('Course is misconfigured');
21 if (! $scorm = get_record('scorm', 'id', $cm->instance)) {
22 error('Course module is incorrect');
24 } else {
25 if (!empty($b)) {
26 if (! $sco = get_record('scorm_scoes', 'id', $b)) {
27 error('Scorm activity is incorrect');
29 $a = $sco->scorm;
31 if (!empty($a)) {
32 if (! $scorm = get_record('scorm', 'id', $a)) {
33 error('Course module is incorrect');
35 if (! $course = get_record('course', 'id', $scorm->course)) {
36 error('Course is misconfigured');
38 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
39 error('Course Module ID was incorrect');
44 require_login($course->id, false, $cm);
46 if (!has_capability('mod/scorm:viewreport', get_context_instance(CONTEXT_MODULE,$cm->id))) {
47 error('You are not allowed to use this script');
50 add_to_log($course->id, 'scorm', 'report', 'report.php?id='.$cm->id, $scorm->id);
52 if (!empty($user)) {
53 $userdata = scorm_get_user_data($user);
54 } else {
55 $userdata = null;
58 /// Print the page header
59 if (empty($noheader)) {
61 $strscorms = get_string('modulenameplural', 'scorm');
62 $strscorm = get_string('modulename', 'scorm');
63 $strreport = get_string('report', 'scorm');
64 $strattempt = get_string('attempt', 'scorm');
65 $strname = get_string('name');
67 $navlinks = array();
68 $navlinks[] = array('name' => $strscorms, 'link' => "index.php?id=$course->id", 'type' => 'activity');
69 $navlinks[] = array('name' => format_string($scorm->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
71 if (empty($b)) {
72 if (empty($a)) {
73 $navigation = build_navigation($navlinks);
74 print_header("$course->shortname: ".format_string($scorm->name), $course->fullname,$navigation,
75 '', '', true);
76 } else {
78 $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');
79 $navlinks[] = array('name' => "$strattempt $attempt - ".fullname($userdata), 'link' => '', 'type' => 'title');
80 $navigation = build_navigation($navlinks);
82 print_header("$course->shortname: ".format_string($scorm->name), $course->fullname,
83 $navigation, '', '', true);
85 } else {
87 $navlinks[] = array('name' => $strreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');
88 $navlinks[] = array('name' => "$strattempt $attempt - ".fullname($userdata), 'link' => "report.php?a=$a&user=$user&attempt=$attempt", 'type' => 'title');
89 $navlinks[] = array('name' => $sco->title, 'link' => '', 'type' => 'title');
90 $navigation = build_navigation($navlinks);
92 print_header("$course->shortname: ".format_string($scorm->name), $course->fullname, $navigation,
93 '', '', true);
95 print_heading(format_string($scorm->name));
98 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
100 if (empty($b)) {
101 if (empty($a)) {
102 // No options, show the global scorm report
104 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
105 $sql = "SELECT st.userid, st.scormid
106 FROM {$CFG->prefix}scorm_scoes_track st
107 INNER JOIN {$CFG->prefix}groups_members gm ON st.userid = gm.userid
108 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
109 WHERE st.scormid = {$scorm->id} AND gg.groupingid = {$cm->groupingid}
110 GROUP BY st.userid,st.scormid
112 } else {
113 $sql = "SELECT st.userid, st.scormid
114 FROM {$CFG->prefix}scorm_scoes_track st
115 WHERE st.scormid = {$scorm->id}
116 GROUP BY st.userid,st.scormid
120 if ($scousers=get_records_sql($sql)) {
121 $table = new stdClass();
122 $table->head = array('&nbsp;', get_string('name'));
123 $table->align = array('center', 'left');
124 $table->wrap = array('nowrap', 'nowrap');
125 $table->width = '100%';
126 $table->size = array(10, '*');
128 $table->head[]= get_string('attempt','scorm');
129 $table->align[] = 'center';
130 $table->wrap[] = 'nowrap';
131 $table->size[] = '*';
133 $table->head[]= get_string('started','scorm');
134 $table->align[] = 'center';
135 $table->wrap[] = 'nowrap';
136 $table->size[] = '*';
138 $table->head[]= get_string('last','scorm');
139 $table->align[] = 'center';
140 $table->wrap[] = 'nowrap';
141 $table->size[] = '*';
143 $table->head[]= get_string('score','scorm');
144 $table->align[] = 'center';
145 $table->wrap[] = 'nowrap';
146 $table->size[] = '*';
148 foreach($scousers as $scouser){
149 $userdata = scorm_get_user_data($scouser->userid);
150 $attempt = scorm_get_last_attempt($scorm->id,$scouser->userid);
151 for ($a = 1; $a<=$attempt; $a++) {
152 $row = array();
153 $row[] = print_user_picture($scouser->userid, $course->id, $userdata->picture, false, true);
154 $row[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$scouser->userid.'&course='.$course->id.'">'.
155 fullname($userdata).'</a>';
156 $row[] = '<a href="report.php?a='.$scorm->id.'&user='.$scouser->userid.'&attempt='.$a.'">'.$a.'</a>';
157 $select = 'scormid = '.$scorm->id.' and userid = '.$scouser->userid.' and attempt = '.$a;
158 $timetracks = get_record_select('scorm_scoes_track', $select,'min(timemodified) as started, max(timemodified) as last');
159 $row[] = userdate($timetracks->started, get_string('strftimedaydatetime'));
160 $row[] = userdate($timetracks->last, get_string('strftimedaydatetime'));
162 $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $a);
163 $table->data[] = $row;
167 print_table($table);
168 } else {
169 if (!empty($user)) {
170 // User SCORM report
171 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' ORDER BY id")) {
172 if (!empty($userdata)) {
173 print_simple_box_start('center');
174 echo '<div align="center">'."\n";
175 print_user_picture($user, $course->id, $userdata->picture, false, false);
176 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&course=$course->id\">".
177 "$userdata->firstname $userdata->lastname</a><br />";
178 echo get_string('attempt','scorm').': '.$attempt;
179 echo '</div>'."\n";
180 print_simple_box_end();
182 // Print general score data
183 $table = new stdClass();
184 $table->head = array(get_string('title','scorm'),
185 get_string('status','scorm'),
186 get_string('time','scorm'),
187 get_string('score','scorm'),
188 '');
189 $table->align = array('left', 'center','center','right','left');
190 $table->wrap = array('nowrap', 'nowrap','nowrap','nowrap','nowrap');
191 $table->width = '80%';
192 $table->size = array('*', '*', '*', '*', '*');
193 foreach ($scoes as $sco) {
194 if ($sco->launch!='') {
195 $row = array();
196 $score = '&nbsp;';
197 if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
198 if ($trackdata->score_raw != '') {
199 $score = $trackdata->score_raw;
201 if ($trackdata->status == '') {
202 $trackdata->status = 'notattempted';
204 $detailslink = '<a href="report.php?b='.$sco->id.'&user='.$user.'&attempt='.$attempt.'" title="'.
205 get_string('details','scorm').'">'.get_string('details','scorm').'</a>';
206 } else {
207 $trackdata->status = 'notattempted';
208 $trackdata->total_time = '&nbsp;';
209 $detailslink = '&nbsp;';
211 $strstatus = get_string($trackdata->status,'scorm');
212 $row[] = '<img src="'.$scormpixdir.'/'.$trackdata->status.'.gif" alt="'.$strstatus.'" title="'.
213 $strstatus.'">&nbsp;'.format_string($sco->title);
214 $row[] = get_string($trackdata->status,'scorm');
215 $row[] = $trackdata->total_time;
216 $row[] = $score;
217 $row[] = $detailslink;
218 } else {
219 $row = array(format_string($sco->title), '&nbsp;', '&nbsp;', '&nbsp;', '&nbsp;');
221 $table->data[] = $row;
223 print_table($table);
226 } else {
227 notice('No users to report');
230 } else {
231 // User SCO report
232 if (!empty($userdata)) {
233 print_simple_box_start('center');
234 //print_heading(format_string($sco->title));
235 print_heading('<a href="'.$CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&mode=browse&scoid='.$sco->id.'" target="_new">'.format_string($sco->title).'</a>');
236 echo '<div align="center">'."\n";
237 print_user_picture($user, $course->id, $userdata->picture, false, false);
238 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user&course=$course->id\">".
239 "$userdata->firstname $userdata->lastname</a><br />";
240 $scoreview = '';
241 if ($trackdata = scorm_get_tracks($sco->id,$user,$attempt)) {
242 if ($trackdata->score_raw != '') {
243 $scoreview = get_string('score','scorm').':&nbsp;'.$trackdata->score_raw;
245 if ($trackdata->status == '') {
246 $trackdata->status = 'notattempted';
248 } else {
249 $trackdata->status = 'notattempted';
250 $trackdata->total_time = '';
252 $strstatus = get_string($trackdata->status,'scorm');
253 echo '<img src="'.$scormpixdir.'/'.$trackdata->status.'.gif" alt="'.$strstatus.'" title="'.
254 $strstatus.'">&nbsp;'.$trackdata->total_time.'<br />'.$scoreview.'<br />';
255 echo '</div>'."\n";
256 echo '<hr /><h2>'.get_string('details','scorm').'</h2>';
258 // Print general score data
259 $table = new stdClass();
260 $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
261 $table->align = array('left', 'left');
262 $table->wrap = array('nowrap', 'nowrap');
263 $table->width = '100%';
264 $table->size = array('*', '*');
266 $existelements = false;
267 if ($scorm->version == 'SCORM_1.3') {
268 $elements = array('raw' => 'cmi.score.raw',
269 'min' => 'cmi.score.min',
270 'max' => 'cmi.score.max',
271 'status' => 'cmi.completition_status',
272 'time' => 'cmi.total_time');
273 } else {
274 $elements = array('raw' => 'cmi.core.score.raw',
275 'min' => 'cmi.core.score.min',
276 'max' => 'cmi.core.score.max',
277 'status' => 'cmi.core.lesson_status',
278 'time' => 'cmi.core.total_time');
280 $printedelements = array();
281 foreach ($elements as $key => $element) {
282 if (isset($trackdata->$element)) {
283 $existelements = true;
284 $printedelements[]=$element;
285 $row = array();
286 $row[] = get_string($key,'scorm');
287 $row[] = s($trackdata->$element);
288 $table->data[] = $row;
291 if ($existelements) {
292 echo '<h3>'.get_string('general','scorm').'</h3>';
293 print_table($table);
296 // Print Interactions data
297 $table = new stdClass();
298 $table->head = array(get_string('identifier','scorm'),
299 get_string('type','scorm'),
300 get_string('result','scorm'),
301 get_string('student_response','scorm'));
302 $table->align = array('center', 'center', 'center', 'center');
303 $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap');
304 $table->width = '100%';
305 $table->size = array('*', '*', '*', '*', '*');
307 $existinteraction = false;
309 $i = 0;
310 $interactionid = 'cmi.interactions.'.$i.'.id';
312 while (isset($trackdata->$interactionid)) {
313 $existinteraction = true;
314 $printedelements[]=$interactionid;
315 $elements = array($interactionid,
316 'cmi.interactions.'.$i.'.type',
317 'cmi.interactions.'.$i.'.result',
318 'cmi.interactions.'.$i.'.student_response');
319 $row = array();
320 foreach ($elements as $element) {
321 if (isset($trackdata->$element)) {
322 $row[] = s($trackdata->$element);
323 $printedelements[]=$element;
324 } else {
325 $row[] = '&nbsp;';
328 $table->data[] = $row;
330 $i++;
331 $interactionid = 'cmi.interactions.'.$i.'.id';
333 if ($existinteraction) {
334 echo '<h3>'.get_string('interactions','scorm').'</h3>';
335 echo '<h3>'.get_string('interactions','scorm').'</h3>';
336 print_table($table);
339 // Print Objectives data
340 $table = new stdClass();
341 $table->head = array(get_string('identifier','scorm'),
342 get_string('status','scorm'),
343 get_string('raw','scorm'),
344 get_string('min','scorm'),
345 get_string('max','scorm'));
346 $table->align = array('center', 'center', 'center', 'center', 'center');
347 $table->wrap = array('nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap');
348 $table->width = '100%';
349 $table->size = array('*', '*', '*', '*', '*');
351 $existobjective = false;
353 $i = 0;
354 $objectiveid = 'cmi.objectives.'.$i.'.id';
356 while (isset($trackdata->$objectiveid)) {
357 $existobjective = true;
358 $printedelements[]=$objectiveid;
359 $elements = array($objectiveid,
360 'cmi.objectives.'.$i.'.status',
361 'cmi.objectives.'.$i.'.score.raw',
362 'cmi.objectives.'.$i.'.score.min',
363 'cmi.objectives.'.$i.'.score.max');
364 $row = array();
365 foreach ($elements as $element) {
366 if (isset($trackdata->$element)) {
367 $row[] = s($trackdata->$element);
368 $printedelements[]=$element;
369 } else {
370 $row[] = '&nbsp;';
373 $table->data[] = $row;
375 $i++;
376 $objectiveid = 'cmi.objectives.'.$i.'.id';
378 if ($existobjective) {
379 echo '<h3>'.get_string('objectives','scorm').'</h3>';
380 print_table($table);
382 $table = new stdClass();
383 $table->head = array(get_string('element','scorm'), get_string('value','scorm'));
384 $table->align = array('left', 'left');
385 $table->wrap = array('nowrap', 'wrap');
386 $table->width = '100%';
387 $table->size = array('*', '*');
389 $existelements = false;
391 foreach($trackdata as $element => $value) {
392 if (substr($element,0,3) == 'cmi') {
393 if (!(in_array ($element, $printedelements))) {
394 $existelements = true;
395 $row = array();
396 $row[] = get_string($element,'scorm') != '[['.$element.']]' ? get_string($element,'scorm') : $element;
397 $row[] = s($value);
398 $table->data[] = $row;
402 if ($existelements) {
403 echo '<h3>'.get_string('othertracks','scorm').'</h3>';
404 print_table($table);
406 print_simple_box_end();
407 } else {
408 error('Missing script parameter');
413 if (empty($noheader)) {
414 print_footer($course);