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