MDL-15810 whitelist _blank target in htmlpurifier cleaning; merged from MOODLE_19_STABLE
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
9528568b 3require_once("$CFG->dirroot/mod/scorm/lib.php");
4
a30b6819 5/// Constants and settings for module scorm
a679d64d 6define('UPDATE_NEVER', '0');
7define('UPDATE_ONCHANGE', '1');
8define('UPDATE_EVERYDAY', '2');
9define('UPDATE_EVERYTIME', '3');
10
b3659259 11define('SCO_ALL', 0);
12define('SCO_DATA', 1);
13define('SCO_ONLY', 2);
a30b6819 14
15define('GRADESCOES', '0');
16define('GRADEHIGHEST', '1');
17define('GRADEAVERAGE', '2');
18define('GRADESUM', '3');
a30b6819 19
20define('HIGHESTATTEMPT', '0');
21define('AVERAGEATTEMPT', '1');
22define('FIRSTATTEMPT', '2');
23define('LASTATTEMPT', '3');
a30b6819 24
a30b6819 25
9528568b 26/// Local Library of functions for module scorm
a30b6819 27
9528568b 28/**
29 * Extracts scrom package, sets up all variables.
30 * Called whenever scorm changes
31 * @param object $scorm instance - fields are updated and changes saved into database
32 * @param bool $full force full update if true
33 * @return void
34 */
35function scorm_parse($scorm, $full) {
36 global $CFG, $DB;
8aee93f1 37
9528568b 38 if (!isset($scorm->cmid)) {
39 $cm = get_coursemodule_from_instance('scorm', $scorm->id);
40 $scorm->cmid = $cm->id;
41 }
42 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
43 $newhash = $scorm->sha1hash;
a30b6819 44
9528568b 45 if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
a30b6819 46
9528568b 47 $fs = get_file_storage();
48 $packagefile = false;
f69db63e 49
9528568b 50 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
51 if ($packagefile = $fs->get_file($context->id, 'scorm_package', 0, '/', $scorm->reference)) {
52 $newhash = $packagefile->get_contenthash();
53 } else {
54 $newhash = null;
55 }
56 } else {
57 if (!$CFG->scorm_allowtypelocalsync) {
58 // sorry - localsync disabled
59 return;
60 }
61 if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
62 $fs->delete_area_files($context->id, 'scorm_package');
63 $file_record = array('contextid'=>$context->id, 'filearea'=>'scorm_package', 'itemid'=>0, 'filepath'=>'/');
64 if ($packagefile = $fs->create_file_from_url($file_record, $scorm->reference)) {
65 $newhash = sha1($scorm->reference);
5c1ac70c 66 } else {
9528568b 67 $newhash = null;
5c1ac70c 68 }
f69db63e 69 }
70 }
f69db63e 71
9528568b 72 if ($packagefile) {
73 if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
74 if (strpos($scorm->version, 'SCORM') !== false) {
75 if ($fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
76 // no need to update
77 return;
e4aa175a 78 }
9528568b 79 } else if (strpos($scorm->version, 'AICC') !== false) {
80 // TODO: add more sanity checks - something really exists in scorm_content area
81 return;
82 }
83 }
84
85 // now extract files
86 $fs->delete_area_files($context->id, 'scorm_content');
87
88 $packer = get_file_packer('application/zip');
89 $packagefile->extract_to_storage($packer, $context->id, 'scorm_content', 0, '/');
90
91 } else if (!$full) {
92 return;
93 }
94
95
96 if ($manifest = $fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
97 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
98 // SCORM
99 if (!scorm_parse_scorm($scorm, $manifest)) {
100 $scorm->version = 'ERROR';
101 }
102 } else {
103 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
104 // AICC
105 if (!scorm_parse_aicc($scorm)) {
106 $scorm->version = 'ERROR';
107 }
108 }
109
110 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $CFG->scorm_allowtypeexternal) {
111 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
112 return;
113 }
114 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
115 // SCORM only, AICC can not be external
116 if (!scorm_parse_scorm($scorm, $scorm->reference)) {
117 $scorm->version = 'ERROR';
118 }
119 $newhash = sha1($scorm->reference);
120
121 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY and !empty($CFG->repositoryactivate) and $CFG->scorm_allowtypeimsrepository) {
122 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
123 return;
124 }
125 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
126 if (!scorm_parse_scorm($scorm, $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml')) {
127 $scorm->version = 'ERROR';
128 }
129 $newhash = sha1($scorm->reference);
130
e4aa175a 131 } else {
9528568b 132 // sorry, disabled type
133 return;
e4aa175a 134 }
9528568b 135
136 $scorm->revision++;
137 $scorm->sha1hash = $newhash;
138 $DB->update_record('scorm', $scorm);
e4aa175a 139}
140
9528568b 141
2b3447c3 142function scorm_array_search($item, $needle, $haystacks, $strict=false) {
143 if (!empty($haystacks)) {
144 foreach ($haystacks as $key => $element) {
145 if ($strict) {
146 if ($element->{$item} === $needle) {
147 return $key;
148 }
149 } else {
150 if ($element->{$item} == $needle) {
151 return $key;
e4aa175a 152 }
153 }
e4aa175a 154 }
155 }
2b3447c3 156 return false;
e4aa175a 157}
158
2b3447c3 159function scorm_repeater($what, $times) {
160 if ($times <= 0) {
161 return null;
162 }
163 $return = '';
164 for ($i=0; $i<$times;$i++) {
165 $return .= $what;
166 }
167 return $return;
168}
e4aa175a 169
2b3447c3 170function scorm_external_link($link) {
171// check if a link is external
172 $result = false;
173 $link = strtolower($link);
174 if (substr($link,0,7) == 'http://') {
175 $result = true;
176 } else if (substr($link,0,8) == 'https://') {
177 $result = true;
178 } else if (substr($link,0,4) == 'www.') {
179 $result = true;
180 }
181 return $result;
e4aa175a 182}
183
b3659259 184/**
185* Returns an object containing all datas relative to the given sco ID
186*
187* @param integer $id The sco ID
188* @return mixed (false if sco id does not exists)
189*/
bd3523a5 190
b3659259 191function scorm_get_sco($id,$what=SCO_ALL) {
bf347041 192 global $DB;
193
194 if ($sco = $DB->get_record('scorm_scoes', array('id'=>$id))) {
b3659259 195 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
bf347041 196 if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id)))) {
b3659259 197 foreach ($scodatas as $scodata) {
c31f631b 198 $sco->{$scodata->name} = $scodata->value;
b3659259 199 }
bf347041 200 } else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id))))) {
9528568b 201 $sco->parameters = '';
b3659259 202 }
203 return $sco;
204 } else {
205 return false;
206 }
207}
82605bea 208
209/**
210* Returns an object (array) containing all the scoes data related to the given sco ID
211*
212* @param integer $id The sco ID
213* @param integer $organisation an organisation ID - defaults to false if not required
214* @return mixed (false if there are no scoes or an array)
215*/
216
217function scorm_get_scoes($id,$organisation=false) {
b44c704f 218 global $DB;
219
82605bea 220 $organizationsql = '';
b44c704f 221 $queryarray = array('scorm'=>$id);
82605bea 222 if (!empty($organisation)) {
b44c704f 223 $queryarray['organization'] = $organisation;
82605bea 224 }
b44c704f 225 if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'id ASC')) {
82605bea 226 // drop keys so that it is a simple array as expected
227 $scoes = array_values($scoes);
228 foreach ($scoes as $sco) {
b44c704f 229 if ($scodatas = $DB->get_records('scorm_scoes_data',array('scoid'=>$sco->id))) {
82605bea 230 foreach ($scodatas as $scodata) {
2fd0e9fe 231 $sco->{$scodata->name} = $scodata->value;
82605bea 232 }
233 }
234 }
235 return $scoes;
236 } else {
237 return false;
238 }
239}
240
6381fa56 241function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value,$forcecompleted=false) {
bf347041 242 global $DB;
243
e4aa175a 244 $id = null;
6381fa56 245
246 if ($forcecompleted) {
247 //TODO - this could be broadened to encompass SCORM 2004 in future
248 if (($element == 'cmi.core.lesson_status') && ($value == 'incomplete')) {
249 if ($track = $DB->get_record_select('scorm_scoes_track','userid=? AND scormid=? AND scoid=? AND attempt=? AND element=\'cmi.core.score.raw\'', array($userid, $scormid, $scoid, $attempt))) {
250 $value = 'completed';
251 }
252 }
253 if ($element == 'cmi.core.score.raw') {
254 if ($tracktest = $DB->get_record_select('scorm_scoes_track','userid=? AND scormid=? AND scoid=? AND attempt=? AND element=\'cmi.core.lesson_status\'', array($userid, $scormid, $scoid, $attempt))) {
255 if ($tracktest->value == "incomplete") {
256 $tracktest->value = "completed";
257 $idtest = $DB->update_record('scorm_scoes_track',$tracktest);
258 }
259 }
260 }
261 }
262
bf347041 263 if ($track = $DB->get_record('scorm_scoes_track',array('userid'=>$userid, 'scormid'=>$scormid, 'scoid'=>$scoid, 'attempt'=>$attempt, 'element'=>$element))) {
e4aa175a 264 $track->value = $value;
265 $track->timemodified = time();
bf347041 266 $id = $DB->update_record('scorm_scoes_track',$track);
e4aa175a 267 } else {
268 $track->userid = $userid;
269 $track->scormid = $scormid;
270 $track->scoid = $scoid;
271 $track->attempt = $attempt;
272 $track->element = $element;
bf347041 273 $track->value = $value;
e4aa175a 274 $track->timemodified = time();
bf347041 275 $id = $DB->insert_record('scorm_scoes_track',$track);
e4aa175a 276 }
9528568b 277
d23121ab 278 // MDL-9552, update the gradebook everything raw score is sent
a0b36684 279 // Scoring by learning objects also needs to be included in the gradebook update
9528568b 280 if (strstr($element, '.score.raw') ||
a0b36684 281 (($element == 'cmi.core.lesson_status' || $element == 'cmi.completion_status') && ($track->value == 'completed' || $track->value == 'passed'))) {
282 $scorm = $DB->get_record('scorm', array('id' => $scormid));
283 $grademethod = $scorm->grademethod % 10;
284 if (strstr($element, '.score.raw') || $grademethod == GRADESCOES) {
285 include_once('lib.php');
286 scorm_update_grades($scorm, $userid);
287 }
d23121ab 288 }
9528568b 289
e4aa175a 290 return $id;
291}
292
e4aa175a 293function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 294/// Gets all tracks of specified sco and user
bf347041 295 global $CFG, $DB;
e4aa175a 296
297 if (empty($attempt)) {
bf347041 298 if ($scormid = $DB->get_field('scorm_scoes','scorm', array('id'=>$scoid))) {
e4aa175a 299 $attempt = scorm_get_last_attempt($scormid,$userid);
300 } else {
301 $attempt = 1;
302 }
303 }
bf347041 304 if ($tracks = $DB->get_records('scorm_scoes_track', array('userid'=>$userid, 'scoid'=>$scoid, 'attempt'=>$attempt),'element ASC')) {
e4aa175a 305 $usertrack->userid = $userid;
9528568b 306 $usertrack->scoid = $scoid;
a30b6819 307 // Defined in order to unify scorm1.2 and scorm2004
e4aa175a 308 $usertrack->score_raw = '';
e4aa175a 309 $usertrack->status = '';
e4aa175a 310 $usertrack->total_time = '00:00:00';
311 $usertrack->session_time = '00:00:00';
312 $usertrack->timemodified = 0;
313 foreach ($tracks as $track) {
314 $element = $track->element;
315 $usertrack->{$element} = $track->value;
316 switch ($element) {
f69db63e 317 case 'cmi.core.lesson_status':
318 case 'cmi.completion_status':
319 if ($track->value == 'not attempted') {
320 $track->value = 'notattempted';
9528568b 321 }
f69db63e 322 $usertrack->status = $track->value;
9528568b 323 break;
e4aa175a 324 case 'cmi.core.score.raw':
325 case 'cmi.score.raw':
326 $usertrack->score_raw = $track->value;
9528568b 327 break;
e4aa175a 328 case 'cmi.core.session_time':
329 case 'cmi.session_time':
330 $usertrack->session_time = $track->value;
9528568b 331 break;
e4aa175a 332 case 'cmi.core.total_time':
333 case 'cmi.total_time':
334 $usertrack->total_time = $track->value;
9528568b 335 break;
336 }
e4aa175a 337 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
338 $usertrack->timemodified = $track->timemodified;
9528568b 339 }
3505e82b 340 }
9528568b 341 if (is_array($usertrack)) {
07b905ae 342 ksort($usertrack);
343 }
e4aa175a 344 return $usertrack;
345 } else {
346 return false;
347 }
348}
349
2b3447c3 350function scorm_get_user_data($userid) {
bf347041 351 global $DB;
2b3447c3 352/// Gets user info required to display the table of scorm results
353/// for report.php
e4aa175a 354
bf347041 355 return $DB->get_record('user', array('id'=>$userid),'firstname, lastname, picture');
2b3447c3 356}
e4aa175a 357
a30b6819 358function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
bf347041 359 global $DB;
9528568b 360 $attemptscore = NULL;
a30b6819 361 $attemptscore->scoes = 0;
362 $attemptscore->values = 0;
363 $attemptscore->max = 0;
364 $attemptscore->sum = 0;
365 $attemptscore->lastmodify = 0;
9528568b 366
bf347041 367 if (!$scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
a30b6819 368 return NULL;
e4aa175a 369 }
e4aa175a 370
7ce6eb87 371 // this treatment is necessary as the whatgrade field was not in the DB
372 // and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
373 // and 1s are grademethod
a30b6819 374 $grademethod = $scorm->grademethod % 10;
375
9528568b 376 foreach ($scoes as $sco) {
2b3447c3 377 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
378 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
a30b6819 379 $attemptscore->scoes++;
9528568b 380 }
2b3447c3 381 if (!empty($userdata->score_raw)) {
a30b6819 382 $attemptscore->values++;
383 $attemptscore->sum += $userdata->score_raw;
384 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
385 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
386 $attemptscore->lastmodify = $userdata->timemodified;
387 } else {
388 $attemptscore->lastmodify = 0;
389 }
9528568b 390 }
391 }
e4aa175a 392 }
2b3447c3 393 switch ($grademethod) {
a30b6819 394 case GRADEHIGHEST:
395 $score = $attemptscore->max;
9528568b 396 break;
a30b6819 397 case GRADEAVERAGE:
398 if ($attemptscore->values > 0) {
399 $score = $attemptscore->sum/$attemptscore->values;
5c1ac70c 400 } else {
a30b6819 401 $score = 0;
9528568b 402 }
403 break;
a30b6819 404 case GRADESUM:
405 $score = $attemptscore->sum;
9528568b 406 break;
a30b6819 407 case GRADESCOES:
408 $score = $attemptscore->scoes;
7ef16bf9 409 break;
410 default:
411 $score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default
5c1ac70c 412 }
a30b6819 413
414 if ($time) {
415 $result = new stdClass();
416 $result->score = $score;
417 $result->time = $attemptscore->lastmodify;
418 } else {
419 $result = $score;
420 }
421
422 return $result;
423}
424
425function scorm_grade_user($scorm, $userid, $time=false) {
7ce6eb87 426 // this treatment is necessary as the whatgrade field was not in the DB
427 // and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
428 // and 1s are grademethod
a30b6819 429 $whatgrade = intval($scorm->grademethod / 10);
430
7ce6eb87 431 // insure we dont grade user beyond $scorm->maxattempt settings
432 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
433 if($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt){
434 $lastattempt = $scorm->maxattempt;
435 }
436
a30b6819 437 switch ($whatgrade) {
438 case FIRSTATTEMPT:
439 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
9528568b 440 break;
a30b6819 441 case LASTATTEMPT:
442 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
443 break;
444 case HIGHESTATTEMPT:
a30b6819 445 $maxscore = 0;
446 $attempttime = 0;
447 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
448 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
449 if ($time) {
450 if ($attemptscore->score > $maxscore) {
451 $maxscore = $attemptscore->score;
452 $attempttime = $attemptscore->time;
453 }
454 } else {
455 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
456 }
457 }
458 if ($time) {
459 $result = new stdClass();
460 $result->score = $maxscore;
461 $result->time = $attempttime;
462 return $result;
463 } else {
464 return $maxscore;
465 }
466 break;
467 case AVERAGEATTEMPT:
468 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
469 $sumscore = 0;
470 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
471 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
472 if ($time) {
473 $sumscore += $attemptscore->score;
474 } else {
475 $sumscore += $attemptscore;
476 }
477 }
478
479 if ($lastattempt > 0) {
480 $score = $sumscore / $lastattempt;
481 } else {
482 $score = 0;
483 }
484
485 if ($time) {
486 $result = new stdClass();
487 $result->score = $score;
488 $result->time = $attemptscore->time;
489 return $result;
490 } else {
491 return $score;
492 }
493 break;
494 }
e4aa175a 495}
496
8e45ba45 497function scorm_count_launchable($scormid,$organization='') {
bf347041 498 global $DB;
499
500 $sqlorganization = '';
501 $params = array($scormid);
8e45ba45 502 if (!empty($organization)) {
bf347041 503 $sqlorganization = " AND organization=?";
504 $params[] = $organization;
8e45ba45 505 }
6280b17a 506 $params []= ''; // empty launch
507 return $DB->count_records_select('scorm_scoes',"scorm = ? $sqlorganization AND launch <> ?", $params);
e4aa175a 508}
509
2b3447c3 510function scorm_get_last_attempt($scormid, $userid) {
bf347041 511 global $DB;
512
2b3447c3 513/// Find the last attempt number for the given user id and scorm id
bf347041 514 if ($lastattempt = $DB->get_record('scorm_scoes_track', array('userid'=>$userid, 'scormid'=>$scormid), 'max(attempt) as a')) {
2b3447c3 515 if (empty($lastattempt->a)) {
516 return '1';
517 } else {
518 return $lastattempt->a;
e4aa175a 519 }
6381fa56 520 } else {
521 return false;
e4aa175a 522 }
e4aa175a 523}
524
e4aa175a 525function scorm_course_format_display($user,$course) {
bf347041 526 global $CFG, $DB;
e4aa175a 527
528 $strupdate = get_string('update');
529 $strmodule = get_string('modulename','scorm');
77bf0c29 530 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 531
532 echo '<div class="mod-scorm">';
533 if ($scorms = get_all_instances_in_course('scorm', $course)) {
9528568b 534 // The module SCORM activity with the least id is the course
e4aa175a 535 $scorm = current($scorms);
536 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
08b56f93 537 print_error('invalidcoursemodule');
e4aa175a 538 }
539 $colspan = '';
540 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 541 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 542 if (isediting($course->id)) {
543 // Display update icon
544 $path = $CFG->wwwroot.'/course';
545 $headertext .= '<span class="commands">'.
546 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
0d905d9f 547 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
e4aa175a 548 }
549 $headertext .= '</td>';
550 // Display report link
bf347041 551 $trackedusers = $DB->get_record('scorm_scoes_track', array('scormid'=>$scorm->id), 'count(distinct(userid)) as c');
e4aa175a 552 if ($trackedusers->c > 0) {
553 $headertext .= '<td class="reportlink">'.
fa738731 554 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
e4aa175a 555 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
556 } else {
557 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
558 }
559 $colspan = ' colspan="2"';
9528568b 560 }
e4aa175a 561 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
562 print_simple_box($headertext,'','100%');
563 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
564 } else {
0d699c24 565 if (has_capability('moodle/course:update', $context)) {
e4aa175a 566 // Create a new activity
2b3447c3 567 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 568 } else {
569 notify('Could not find a scorm course here');
570 }
571 }
572 echo '</div>';
573}
574
2b3447c3 575function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
534792cd 576 global $CFG, $DB;
ab3b00e1 577
9528568b 578 if ($scorm->updatefreq == UPDATE_EVERYTIME) {
579 scorm_parse($scorm, false);
ab3b00e1 580 }
581
e4aa175a 582 $organization = optional_param('organization', '', PARAM_INT);
583
6381fa56 584 if($scorm->displaycoursestructure == 1) {
585 print_simple_box_start('center',$boxwidth);
e4aa175a 586?>
29a43013 587 <div class="structurehead"><?php print_string('contents','scorm') ?></div>
e4aa175a 588<?php
6381fa56 589 }
e4aa175a 590 if (empty($organization)) {
591 $organization = $scorm->launch;
592 }
07b905ae 593 if ($orgs = $DB->get_records_menu('scorm_scoes', array('scorm'=>$scorm->id, 'organization'=>'', 'launch'=>''), 'id', 'id,title')) {
e4aa175a 594 if (count($orgs) > 1) {
595 ?>
52a9a9b5 596 <div class='scorm-center'>
e4aa175a 597 <?php print_string('organizations','scorm') ?>
b7dc2256 598 <form id='changeorg' method='post' action='<?php echo $action ?>'>
e4aa175a 599 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
600 </form>
601 </div>
602<?php
603 }
604 }
605 $orgidentifier = '';
b3659259 606 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
607 if (($sco->organization == '') && ($sco->launch == '')) {
608 $orgidentifier = $sco->identifier;
e4aa175a 609 } else {
b3659259 610 $orgidentifier = $sco->organization;
e4aa175a 611 }
612 }
28ffbff3 613
614/*
615 $orgidentifier = '';
bf347041 616 if ($org = $DB->get_record('scorm_scoes', array('id'=>$organization))) {
28ffbff3 617 if (($org->organization == '') && ($org->launch == '')) {
618 $orgidentifier = $org->identifier;
619 } else {
620 $orgidentifier = $org->organization;
621 }
622 }*/
623
2b3447c3 624 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
dbe7e6f6 625 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
626 $scorm->version = 'scorm_12';
627 }
2b3447c3 628 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
629
e4aa175a 630 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
631 $incomplete = $result->incomplete;
28ffbff3 632
6381fa56 633 // do we want the TOC to be displayed?
634 if($scorm->displaycoursestructure == 1) {
635 echo $result->toc;
636 print_simple_box_end();
637 }
638
639 // is this the first attempt ?
640 $attemptcount = scorm_get_attempt_count($user, $scorm);
641
642 // do not give the player launch FORM if the SCORM object is locked after the final attempt
643 if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) {
e4aa175a 644?>
52a9a9b5 645 <div class="scorm-center">
4675994a 646 <form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php">
e4aa175a 647 <?php
e4aa175a 648 if ($scorm->hidebrowse == 0) {
8949f8df 649 print_string('mode','scorm');
e4aa175a 650 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
76ea4fb4 651 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
e4aa175a 652 } else {
76ea4fb4 653 echo '<input type="hidden" name="mode" value="normal" />'."\n";
e4aa175a 654 }
6381fa56 655 if ($scorm->forcenewattempt == 1) {
656 if ($incomplete === false) {
657 echo '<input type="hidden" name="newattempt" value="on" />'."\n";
658 }
659 } elseif ($attemptcount != 0 && ($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
e4aa175a 660?>
6381fa56 661 <br />
662 <input type="checkbox" id="a" name="newattempt" />
663 <label for="a"><?php print_string('newattempt','scorm') ?></label>
e4aa175a 664<?php
665 }
666 ?>
667 <br />
4675994a 668 <input type="hidden" name="scoid"/>
df7ba610 669 <input type="hidden" name="id" value="<?php echo $cm->id ?>"/>
e4aa175a 670 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
2b600d16 671 <input type="submit" value="<?php print_string('enter','scorm') ?>" />
e4aa175a 672 </form>
673 </div>
674<?php
6381fa56 675 }
e4aa175a 676}
6280b17a 677
28ffbff3 678function scorm_simple_play($scorm,$user) {
bf347041 679 global $DB;
680
28ffbff3 681 $result = false;
9528568b 682
5b4b959b 683 $scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND launch <> ?', array($scorm->id, $DB->sql_empty()));
9528568b 684
fa6eb1dc 685 if ($scoes && (count($scoes) == 1)) {
28ffbff3 686 if ($scorm->skipview >= 1) {
687 $sco = current($scoes);
688 if (scorm_get_tracks($sco->id,$user->id) === false) {
45698041 689 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
28ffbff3 690 $result = true;
691 } else if ($scorm->skipview == 2) {
45698041 692 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
28ffbff3 693 $result = true;
694 }
695 }
696 }
28ffbff3 697 return $result;
698}
699/*
8e45ba45 700function scorm_simple_play($scorm,$user) {
bf347041 701 global $DB;
8e45ba45 702 $result = false;
bf347041 703 if ($scoes = $DB->get_records_select('scorm_scoes','scorm=? AND launch<>""', array($scorm->id))) {
76ea4fb4 704 if (count($scoes) == 1) {
705 if ($scorm->skipview >= 1) {
706 $sco = current($scoes);
707 if (scorm_get_tracks($sco->id,$user->id) === false) {
708 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
709 $result = true;
710 } else if ($scorm->skipview == 2) {
711 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
712 $result = true;
713 }
8e45ba45 714 }
715 }
716 }
717 return $result;
718}
28ffbff3 719*/
d8c9d8a1 720
721function scorm_get_count_users($scormid, $groupingid=null) {
bf347041 722 global $CFG, $DB;
9528568b 723
d8c9d8a1 724 if (!empty($CFG->enablegroupings) && !empty($groupingid)) {
725 $sql = "SELECT COUNT(DISTINCT st.userid)
bf347041 726 FROM {scorm_scoes_track} st
727 INNER JOIN {groups_members} gm ON st.userid = gm.userid
9528568b 728 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
bf347041 729 WHERE st.scormid = ? AND gg.groupingid = ?
d8c9d8a1 730 ";
bf347041 731 $params = array($scormid, $groupingid);
d8c9d8a1 732 } else {
733 $sql = "SELECT COUNT(DISTINCT st.userid)
9528568b 734 FROM {scorm_scoes_track} st
bf347041 735 WHERE st.scormid = ?
d8c9d8a1 736 ";
bf347041 737 $params = array($scormid);
d8c9d8a1 738 }
9528568b 739
bf347041 740 return ($DB->count_records_sql($sql, $params));
d8c9d8a1 741}
742
527af457 743/**
744* Build up the JavaScript representation of an array element
745*
746* @param string $sversion SCORM API version
747* @param array $userdata User track data
748* @param string $element_name Name of array element to get values for
749* @param array $children list of sub elements of this array element that also need instantiating
750* @return None
751*/
752function scorm_reconstitute_array_element($sversion, $userdata, $element_name, $children) {
753 // reconstitute comments_from_learner and comments_from_lms
754 $current = '';
755 $count = 0;
756
757 // filter out the ones we want
758 $element_list = array();
759 foreach($userdata as $element => $value){
760 if (substr($element,0,strlen($element_name)) == $element_name) {
761 $element_list[$element] = $value;
762 }
763 }
764
765 // sort elements in .n array order
766 uksort($element_list, "scorm_element_cmp");
767
768 // generate JavaScript
769 foreach($element_list as $element => $value){
770 if ($sversion == 'scorm_13') {
771 $element = preg_replace('/\.(\d+)\./', ".N\$1.", $element);
772 preg_match('/\.(N\d+)\./', $element, $matches);
773 } else {
774 $element = preg_replace('/\.(\d+)\./', "_\$1.", $element);
775 preg_match('/\_(\d+)\./', $element, $matches);
776 }
777 if (count($matches) > 0 && $current != $matches[1]) {
778 $current = $matches[1];
779 $count++;
780 $end = strpos($element,$matches[1])+strlen($matches[1]);
781 $subelement = substr($element,0,$end);
782 echo ' '.$subelement." = new Object();\n";
783 // now add the children
784 foreach ($children as $child) {
785 echo ' '.$subelement.".".$child." = new Object();\n";
786 echo ' '.$subelement.".".$child."._children = ".$child."_children;\n";
787 }
788 }
789 echo ' '.$element.' = \''.$value."';\n";
790 }
791 if ($count > 0) {
792 echo ' '.$element_name.'._count = '.$count.";\n";
793 }
794}
795
796/**
797* Build up the JavaScript representation of an array element
798*
799* @param string $a left array element
800* @param string $b right array element
801* @return comparator - 0,1,-1
802*/
803function scorm_element_cmp($a, $b) {
804 preg_match('/(\d+)\./', $a, $matches);
805 $left = intval($matches[1]);
806 preg_match('/(\d+)\./', $b, $matches);
807 $right = intval($matches[1]);
808 if ($left < $right) {
809 return -1; // smaller
810 } elseif ($left > $right) {
811 return 1; // bigger
812 } else {
813 return 0; // equal to
814 }
815}
6381fa56 816
817/**
818* Generate the user attempt status string
819*
820* @param object $user Current context user
821* @param object $scorm a moodle scrom object - mdl_scorm
822* @return string - Attempt status string
823*/
824function scorm_get_attempt_status($user, $scorm) {
825 global $DB;
826
827 $attempts = $DB->get_records_select('scorm_scoes_track',"element='cmi.core.score.raw' AND userid=? AND scormid=?", array($user->id, $scorm->id),'attempt','attempt AS attemptnumber, value AS grade');
828 if(empty($attempts)) {
829 $attemptcount = 0;
830 } else {
831 $attemptcount = count($attempts);
832 }
833
834 $result = '<p>'.get_string('noattemptsallowed', 'scorm').': ';
835 if ($scorm->maxattempt > 0) {
836 $result .= $scorm->maxattempt . '<BR>';
837 } else {
838 $result .= get_string('unlimited').'<BR>';
839 }
840 $result .= get_string('noattemptsmade', 'scorm').': ' . $attemptcount . '<BR>';
841
842 $gradereported = 0;
843 $gradesum = 0;
844 switch ($scorm->grademethod) {
845 case GRADEHIGHEST:
846 $grademethod = get_string('gradehighest', 'scorm');
847 break;
848 case GRADEAVERAGE:
849 $grademethod = get_string('gradeaverage', 'scorm');
850 break;
851 case GRADESUM:
852 $grademethod = get_string('gradesum', 'scorm');
853 break;
854 case GRADESCOES:
855 $grademethod = get_string('gradescoes', 'scorm');
856 break;
857 }
858
859 if(!empty($attempts)) {
860 foreach($attempts as $attempt) {
861 $gradereported = scorm_grade_user_attempt($scorm, $user->id, $attempt->attemptnumber);
862 $result .= get_string('gradeforattempt', 'scorm').' ' . $attempt->attemptnumber . ': ' . $attempt->grade .'%<BR>';
863 }
864 }
865
866 $result .= get_string('grademethod', 'scorm'). ': ' . $grademethod;
867 if(empty($attempts)) {
868 $result .= '<BR>' . get_string('gradereported','scorm') . ': ' . get_string('none') . '<BR>';
869 } else {
870 $result .= '<BR>' . get_string('gradereported','scorm') . ': ' . $gradereported . ($scorm->grademethod == GRADESCOES ? '' : '%') .'<BR>';
871 }
872 $result .= '</p>';
873 if ($attemptcount >= $scorm->maxattempt and $scorm->maxattempt > 0) {
874 $result .= '<p><font color="#cc0000">'.get_string('exceededmaxattempts','scorm').'</font></p>';
875 }
876 return $result;
877}
878
879/**
880* Get SCORM attempt count
881*
882* @param object $user Current context user
883* @param object $scorm a moodle scrom object - mdl_scorm
884* @return int - no. of attempts so far
885*/
886function scorm_get_attempt_count($user, $scorm) {
887 global $DB;
888 $attemptcount = 0;
889 $element = 'cmi.core.score.raw';
890 if ($scorm->version == 'scorm1_3') {
891 $element = 'cmi.score.raw';
892 }
893 $attempts = $DB->get_records_select('scorm_scoes_track',"element=? AND userid=? AND scormid=?", array($element, $user->id, $scorm->id),'attempt','attempt AS attemptnumber, value AS grade');
894 if(!empty($attempts)) {
895 $attemptcount = count($attempts);
896 }
897 return $attemptcount;
898}
1881df27 899
900/**
901* Figure out with this is a debug situation
902*
903* @param object $scorm a moodle scrom object - mdl_scorm
904* @return boolean - debugging true/false
905*/
906function scorm_debugging($scorm) {
907 global $CFG, $USER;
908 if (!$CFG->scorm_allowapidebug) {
909 return false;
910 }
911 $identifier = $USER->username.':'.$scorm->name;
912 $test = $CFG->scorm_apidebugmask;
913 // check the regex is only a short list of safe characters
914 if (!preg_match('/^[\w\s\*\.\?\+\:\_\\\]+$/', $test)) {
915 return false;
916 }
917 $res = false;
918 eval('$res = preg_match(\'/^'.$test.'/\', $identifier) ? true : false;');
919 return $res;
920}
527af457 921?>