Updated the HEAD build version to 20100706
[moodle.git] / mod / scorm / lib.php
CommitLineData
28f672b2 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
e4aa175a 17
28f672b2 18/**
19 * @package mod-scorm
20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/** SCORM_TYPE_LOCAL = local */
9528568b 25define('SCORM_TYPE_LOCAL', 'local');
28f672b2 26/** SCORM_TYPE_LOCALSYNC = localsync */
9528568b 27define('SCORM_TYPE_LOCALSYNC', 'localsync');
28f672b2 28/** SCORM_TYPE_EXTERNAL = external */
9528568b 29define('SCORM_TYPE_EXTERNAL', 'external');
28f672b2 30/** SCORM_TYPE_IMSREPOSITORY = imsrepository */
9528568b 31define('SCORM_TYPE_IMSREPOSITORY', 'imsrepository');
32
33
e4aa175a 34/**
28f672b2 35 * Given an object containing all the necessary data,
36 * (defined by the form in mod_form.php) this function
37 * will create a new instance and return the id number
38 * of the new instance.
39 *
40 * @global stdClass
41 * @global object
42 * @uses CONTEXT_MODULE
43 * @uses SCORM_TYPE_LOCAL
44 * @uses SCORM_TYPE_LOCALSYNC
45 * @uses SCORM_TYPE_EXTERNAL
46 * @uses SCORM_TYPE_IMSREPOSITORY
47 * @param object $scorm Form data
48 * @param object $mform
49 * @return int new instance id
50 */
9528568b 51function scorm_add_instance($scorm, $mform=null) {
c18269c7 52 global $CFG, $DB;
a679d64d 53
86996ffe 54 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a679d64d 55
413900b4 56 if (empty($scorm->timeopen)) {
d54e2145 57 $scorm->timeopen = 0;
413900b4
DM
58 }
59 if (empty($scorm->timeclose)) {
d54e2145 60 $scorm->timeclose = 0;
61 }
9528568b 62 $cmid = $scorm->coursemodule;
63 $cmidnumber = $scorm->cmidnumber;
64 $courseid = $scorm->course;
76ea4fb4 65
9528568b 66 $context = get_context_instance(CONTEXT_MODULE, $cmid);
a679d64d 67
9528568b 68 $scorm = scorm_option2text($scorm);
69 $scorm->width = (int)str_replace('%', '', $scorm->width);
70 $scorm->height = (int)str_replace('%', '', $scorm->height);
e4aa175a 71
9528568b 72 if (!isset($scorm->whatgrade)) {
73 $scorm->whatgrade = 0;
74 }
75 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
b3659259 76
a8f3a651 77 $id = $DB->insert_record('scorm', $scorm);
e4aa175a 78
9528568b 79/// update course module record - from now on this instance properly exists and all function may be used
bf8e93d7 80 $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
e4aa175a 81
9528568b 82/// reload scorm instance
83 $scorm = $DB->get_record('scorm', array('id'=>$id));
84
85/// store the package and verify
86 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
87 if ($mform) {
88 $filename = $mform->get_new_filename('packagefile');
89 if ($filename !== false) {
90 $fs = get_file_storage();
64f93798
PS
91 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
92 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
9528568b 93 $scorm->reference = $filename;
8ba4f1e0 94 }
e4aa175a 95 }
96
9528568b 97 } else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
98 $scorm->reference = $scorm->packageurl;
e4aa175a 99
9528568b 100 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
101 $scorm->reference = $scorm->packageurl;
102
103 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
104 $scorm->reference = $scorm->packageurl;
531fa830 105
a679d64d 106 } else {
9528568b 107 return false;
e4aa175a 108 }
9528568b 109
110 // save reference
111 $DB->update_record('scorm', $scorm);
112
113
114/// extra fields required in grade related functions
115 $scorm->course = $courseid;
116 $scorm->cmidnumber = $cmidnumber;
117 $scorm->cmid = $cmid;
118
119 scorm_parse($scorm, true);
120
121 scorm_grade_item_update($scorm);
122
123 return $scorm->id;
e4aa175a 124}
125
126/**
28f672b2 127 * Given an object containing all the necessary data,
128 * (defined by the form in mod_form.php) this function
129 * will update an existing instance with new data.
130 *
131 * @global stdClass
132 * @global object
133 * @uses CONTEXT_MODULE
134 * @uses SCORM_TYPE_LOCAL
135 * @uses SCORM_TYPE_LOCALSYNC
136 * @uses SCORM_TYPE_EXTERNAL
137 * @uses SCORM_TYPE_IMSREPOSITORY
138 * @param object $scorm Form data
139 * @param object $mform
140 * @return bool
141 */
9528568b 142function scorm_update_instance($scorm, $mform=null) {
c18269c7 143 global $CFG, $DB;
e4aa175a 144
86996ffe 145 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
76ea4fb4 146
413900b4 147 if (empty($scorm->timeopen)) {
d54e2145 148 $scorm->timeopen = 0;
413900b4
DM
149 }
150 if (empty($scorm->timeclose)) {
d54e2145 151 $scorm->timeclose = 0;
152 }
153
9528568b 154 $cmid = $scorm->coursemodule;
155 $cmidnumber = $scorm->cmidnumber;
156 $courseid = $scorm->course;
157
158 $scorm->id = $scorm->instance;
159
160 $context = get_context_instance(CONTEXT_MODULE, $cmid);
161
162 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
163 if ($mform) {
164 $filename = $mform->get_new_filename('packagefile');
165 if ($filename !== false) {
166 $scorm->reference = $filename;
167 $fs = get_file_storage();
64f93798
PS
168 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
169 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
a679d64d 170 }
76ea4fb4 171 }
76ea4fb4 172
9528568b 173 } else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
174 $scorm->reference = $scorm->packageurl;
175
176 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
177 $scorm->reference = $scorm->packageurl;
178
179 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
180 $scorm->reference = $scorm->packageurl;
181
182 } else {
183 return false;
184 }
bfe8c2f0 185
e4aa175a 186 $scorm = scorm_option2text($scorm);
9528568b 187 $scorm->width = (int)str_replace('%','',$scorm->width);
188 $scorm->height = (int)str_replace('%','',$scorm->height);
189 $scorm->timemodified = time();
e4aa175a 190
b3659259 191 if (!isset($scorm->whatgrade)) {
192 $scorm->whatgrade = 0;
193 }
9528568b 194 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
a30b6819 195
a8c31db2 196 $DB->update_record('scorm', $scorm);
531fa830 197
9528568b 198 $scorm = $DB->get_record('scorm', array('id'=>$scorm->id));
199
200/// extra fields required in grade related functions
201 $scorm->course = $courseid;
202 $scorm->idnumber = $cmidnumber;
203 $scorm->cmid = $cmid;
204
205 scorm_parse($scorm, (bool)$scorm->updatefreq);
206
207 scorm_grade_item_update($scorm);
208
209 return true;
e4aa175a 210}
211
212/**
28f672b2 213 * Given an ID of an instance of this module,
214 * this function will permanently delete the instance
215 * and any data that depends on it.
216 *
217 * @global stdClass
218 * @global object
219 * @param int $id Scorm instance id
220 * @return boolean
221 */
e4aa175a 222function scorm_delete_instance($id) {
c18269c7 223 global $CFG, $DB;
e4aa175a 224
c18269c7 225 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
e4aa175a 226 return false;
227 }
228
229 $result = true;
230
e4aa175a 231 // Delete any dependent records
c18269c7 232 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
e4aa175a 233 $result = false;
234 }
c18269c7 235 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
b3659259 236 foreach ($scoes as $sco) {
c18269c7 237 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
b3659259 238 $result = false;
239 }
9528568b 240 }
c18269c7 241 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
b3659259 242 } else {
e4aa175a 243 $result = false;
244 }
c18269c7 245 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
e4aa175a 246 $result = false;
247 }
a30b6819 248
c18269c7 249 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
e4aa175a 250 $result = false;
251 }
c18269c7 252 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
e4aa175a 253 $result = false;
254 }
c18269c7 255 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
e4aa175a 256 $result = false;
257 }
c18269c7 258 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 259 $result = false;
260 }
c18269c7 261 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 262 $result = false;
263 }
c18269c7 264 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 265 $result = false;
266 }
c18269c7 267 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 268 $result = false;
9528568b 269 }*/
531fa830 270
c18269c7 271 scorm_grade_item_delete($scorm);
9528568b 272
e4aa175a 273 return $result;
274}
275
276/**
28f672b2 277 * Return a small object with summary information about what a
278 * user has done with a given particular instance of this module
279 * Used for user activity reports.
280 *
281 * @global stdClass
282 * @param int $course Course id
283 * @param int $user User id
284 * @param int $mod
285 * @param int $scorm The scorm id
286 * @return mixed
287 */
9528568b 288function scorm_user_outline($course, $user, $mod, $scorm) {
531fa830 289 global $CFG;
86996ffe 290 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a30b6819 291
1a96363a
NC
292 require_once("$CFG->libdir/gradelib.php");
293 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
294 if (!empty($grades->items[0]->grades)) {
295 $grade = reset($grades->items[0]->grades);
296 $result = new object();
297 $result->info = get_string('grade') . ': '. $grade->str_long_grade;
298 $result->time = $grade->dategraded;
299 return $result;
300 }
301 return null;
e4aa175a 302}
303
304/**
28f672b2 305 * Print a detailed representation of what a user has done with
306 * a given particular instance of this module, for user activity reports.
307 *
308 * @global stdClass
309 * @global object
310 * @param object $course
311 * @param object $user
312 * @param object $mod
313 * @param object $scorm
314 * @return boolean
315 */
e4aa175a 316function scorm_user_complete($course, $user, $mod, $scorm) {
d436d197 317 global $CFG, $DB, $OUTPUT;
1a96363a 318 require_once("$CFG->libdir/gradelib.php");
e4aa175a 319
320 $liststyle = 'structlist';
e4aa175a 321 $now = time();
322 $firstmodify = $now;
323 $lastmodify = 0;
324 $sometoreport = false;
325 $report = '';
9895302c 326
c86a91d5 327 // First Access and Last Access dates for SCOs
86996ffe 328 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
c86a91d5
PH
329 $timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
330 $firstmodify = $timetracks->start;
331 $lastmodify = $timetracks->finish;
9895302c 332
1a96363a
NC
333 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
334 if (!empty($grades->items[0]->grades)) {
335 $grade = reset($grades->items[0]->grades);
336 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
337 if ($grade->str_feedback) {
338 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
339 }
340 }
341
bf347041 342 if ($orgs = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id, 'organization'=>'', 'launch'=>''),'id','id,identifier,title')) {
e4aa175a 343 if (count($orgs) <= 1) {
344 unset($orgs);
345 $orgs[]->identifier = '';
346 }
347 $report .= '<div class="mod-scorm">'."\n";
348 foreach ($orgs as $org) {
bf347041 349 $conditions = array();
e4aa175a 350 $currentorg = '';
351 if (!empty($org->identifier)) {
352 $report .= '<div class="orgtitle">'.$org->title.'</div>';
353 $currentorg = $org->identifier;
bf347041 354 $conditions['organization'] = $currentorg;
e4aa175a 355 }
356 $report .= "<ul id='0' class='$liststyle'>";
bf347041 357 $conditions['scorm'] = $scorm->id;
358 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")){
9fb2de4e 359 // drop keys so that we can access array sequentially
9528568b 360 $scoes = array_values($scoes);
e4aa175a 361 $level=0;
362 $sublist=1;
363 $parents[$level]='/';
9fb2de4e 364 foreach ($scoes as $pos=>$sco) {
e4aa175a 365 if ($parents[$level]!=$sco->parent) {
366 if ($level>0 && $parents[$level-1]==$sco->parent) {
367 $report .= "\t\t</ul></li>\n";
368 $level--;
369 } else {
370 $i = $level;
371 $closelist = '';
372 while (($i > 0) && ($parents[$level] != $sco->parent)) {
373 $closelist .= "\t\t</ul></li>\n";
374 $i--;
375 }
376 if (($i == 0) && ($sco->parent != $currentorg)) {
377 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
378 $level++;
379 } else {
380 $report .= $closelist;
381 $level = $i;
382 }
383 $parents[$level]=$sco->parent;
384 }
385 }
386 $report .= "\t\t<li>";
9fb2de4e 387 if (isset($scoes[$pos+1])) {
388 $nextsco = $scoes[$pos+1];
389 } else {
390 $nextsco = false;
391 }
e4aa175a 392 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
393 $sublist++;
394 } else {
485f4ce6 395 $report .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
e4aa175a 396 }
397
398 if ($sco->launch) {
e4aa175a 399 $score = '';
400 $totaltime = '';
401 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
402 if ($usertrack->status == '') {
403 $usertrack->status = 'notattempted';
404 }
405 $strstatus = get_string($usertrack->status,'scorm');
485f4ce6 406 $report .= "<img src='".$OUTPUT->pix_url($usertrack->status, 'scorm')."' alt='$strstatus' title='$strstatus' />";
e4aa175a 407 } else {
408 if ($sco->scormtype == 'sco') {
485f4ce6 409 $report .= '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
e4aa175a 410 } else {
485f4ce6 411 $report .= '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
e4aa175a 412 }
413 }
414 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
415 if ($usertrack !== false) {
416 $sometoreport = true;
417 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
418 foreach($usertrack as $element => $value) {
419 if (substr($element,0,3) == 'cmi') {
420 $report .= '<li>'.$element.' => '.$value.'</li>';
421 }
422 }
423 $report .= "\t\t\t</ul></li>\n";
9528568b 424 }
e4aa175a 425 } else {
426 $report .= "&nbsp;$sco->title</li>\n";
427 }
428 }
429 for ($i=0;$i<$level;$i++) {
430 $report .= "\t\t</ul></li>\n";
431 }
432 }
433 $report .= "\t</ul><br />\n";
434 }
435 $report .= "</div>\n";
436 }
437 if ($sometoreport) {
438 if ($firstmodify < $now) {
439 $timeago = format_time($now - $firstmodify);
440 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
441 }
442 if ($lastmodify > 0) {
443 $timeago = format_time($now - $lastmodify);
444 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
445 }
446 echo get_string('report','scorm').":<br />\n";
447 echo $report;
448 } else {
dabfd0ed 449 print_string('noactivity','scorm');
e4aa175a 450 }
451
452 return true;
453}
454
e4aa175a 455/**
28f672b2 456 * Function to be run periodically according to the moodle cron
457 * This function searches for things that need to be done, such
458 * as sending out mail, toggling flags etc ...
459 *
460 * @global stdClass
461 * @global object
462 * @return boolean
463 */
e4aa175a 464function scorm_cron () {
bf347041 465 global $CFG, $DB;
a679d64d 466
86996ffe 467 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a679d64d 468
469 $sitetimezone = $CFG->timezone;
9528568b 470 /// Now see if there are any scorm updates to be done
471
bfe8c2f0 472 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
473 set_config('scorm_updatetimelast', 0);
474 }
475
a679d64d 476 $timenow = time();
c5b3ba6a 477 $updatetime = usergetmidnight($timenow, $sitetimezone);
a679d64d 478
479 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
480
bfe8c2f0 481 set_config('scorm_updatetimelast', $timenow);
e4aa175a 482
376c9c70 483 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 484
bf347041 485 $scormsupdate = $DB->get_records('scorm', array('updatefreq'=>UPDATE_EVERYDAY));
9528568b 486 foreach($scormsupdate as $scormupdate) {
487 scorm_parse($scormupdate, true);
a679d64d 488 }
489 }
490
e4aa175a 491 return true;
492}
493
494/**
531fa830 495 * Return grade for given user or all users.
496 *
28f672b2 497 * @global stdClass
498 * @global object
531fa830 499 * @param int $scormid id of scorm
500 * @param int $userid optional user id, 0 means all users
501 * @return array array of grades, false if none
502 */
503function scorm_get_user_grades($scorm, $userid=0) {
bf347041 504 global $CFG, $DB;
86996ffe 505 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
531fa830 506
507 $grades = array();
508 if (empty($userid)) {
bf347041 509 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
531fa830 510 foreach ($scousers as $scouser) {
511 $grades[$scouser->userid] = new object();
512 $grades[$scouser->userid]->id = $scouser->userid;
513 $grades[$scouser->userid]->userid = $scouser->userid;
514 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
515 }
516 } else {
517 return false;
518 }
e4aa175a 519
531fa830 520 } else {
bf347041 521 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
531fa830 522 return false; //no attempt yet
523 }
524 $grades[$userid] = new object();
525 $grades[$userid]->id = $userid;
526 $grades[$userid]->userid = $userid;
527 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
e4aa175a 528 }
e4aa175a 529
531fa830 530 return $grades;
531}
532
533/**
534 * Update grades in central gradebook
535 *
28f672b2 536 * @global stdClass
537 * @global object
775f811a 538 * @param object $scorm
531fa830 539 * @param int $userid specific user only, 0 mean all
28f672b2 540 * @param bool $nullifnone
531fa830 541 */
775f811a 542function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
bf347041 543 global $CFG, $DB;
775f811a 544 require_once($CFG->libdir.'/gradelib.php');
531fa830 545
775f811a 546 if ($grades = scorm_get_user_grades($scorm, $userid)) {
547 scorm_grade_item_update($scorm, $grades);
531fa830 548
775f811a 549 } else if ($userid and $nullifnone) {
550 $grade = new object();
551 $grade->userid = $userid;
552 $grade->rawgrade = NULL;
553 scorm_grade_item_update($scorm, $grade);
531fa830 554
e4aa175a 555 } else {
775f811a 556 scorm_grade_item_update($scorm);
557 }
558}
559
560/**
561 * Update all grades in gradebook.
28f672b2 562 *
563 * @global object
775f811a 564 */
565function scorm_upgrade_grades() {
566 global $DB;
567
568 $sql = "SELECT COUNT('x')
569 FROM {scorm} s, {course_modules} cm, {modules} m
570 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
571 $count = $DB->count_records_sql($sql);
572
573 $sql = "SELECT s.*, cm.idnumber AS cmidnumber, s.course AS courseid
574 FROM {scorm} s, {course_modules} cm, {modules} m
575 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
576 if ($rs = $DB->get_recordset_sql($sql)) {
775f811a 577 $pbar = new progress_bar('scormupgradegrades', 500, true);
578 $i=0;
579 foreach ($rs as $scorm) {
580 $i++;
581 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
582 scorm_update_grades($scorm, 0, false);
583 $pbar->update($i, $count, "Updating Scorm grades ($i/$count).");
531fa830 584 }
775f811a 585 $rs->close();
e4aa175a 586 }
531fa830 587}
e4aa175a 588
531fa830 589/**
590 * Update/create grade item for given scorm
591 *
28f672b2 592 * @global stdClass
593 * @global object
594 * @uses GRADE_TYPE_VALUE
595 * @uses GRADE_TYPE_NONE
531fa830 596 * @param object $scorm object with extra cmidnumber
28f672b2 597 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
531fa830 598 * @return object grade_item
599 */
0b5a80a1 600function scorm_grade_item_update($scorm, $grades=NULL) {
bf347041 601 global $CFG, $DB;
531fa830 602 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
603 require_once($CFG->libdir.'/gradelib.php');
604 }
605
7ef16bf9 606 $params = array('itemname'=>$scorm->name);
607 if (isset($scorm->cmidnumber)) {
608 $params['idnumber'] = $scorm->cmidnumber;
609 }
9528568b 610
531fa830 611 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
5b4b959b 612 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND launch <> ?', array($scorm->id, $DB->sql_empty()))) {
531fa830 613 $params['gradetype'] = GRADE_TYPE_VALUE;
614 $params['grademax'] = $maxgrade;
615 $params['grademin'] = 0;
616 } else {
617 $params['gradetype'] = GRADE_TYPE_NONE;
e4aa175a 618 }
531fa830 619 } else {
620 $params['gradetype'] = GRADE_TYPE_VALUE;
621 $params['grademax'] = $scorm->maxgrade;
622 $params['grademin'] = 0;
e4aa175a 623 }
531fa830 624
0b5a80a1 625 if ($grades === 'reset') {
626 $params['reset'] = true;
627 $grades = NULL;
628 }
629
630 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
531fa830 631}
632
633/**
634 * Delete grade item for given scorm
635 *
28f672b2 636 * @global stdClass
531fa830 637 * @param object $scorm object
638 * @return object grade_item
639 */
640function scorm_grade_item_delete($scorm) {
641 global $CFG;
642 require_once($CFG->libdir.'/gradelib.php');
643
644 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
e4aa175a 645}
646
28f672b2 647/**
648 * @return array
649 */
e4aa175a 650function scorm_get_view_actions() {
651 return array('pre-view','view','view all','report');
652}
653
28f672b2 654/**
655 * @return array
656 */
e4aa175a 657function scorm_get_post_actions() {
658 return array();
659}
660
28f672b2 661/**
662 * @param object $scorm
663 * @return object $scorm
664 */
e4aa175a 665function scorm_option2text($scorm) {
1adc77e6 666 $scorm_popoup_options = scorm_get_popup_options_array();
e5dd8e3b 667
e4aa175a 668 if (isset($scorm->popup)) {
76ea4fb4 669 if ($scorm->popup == 1) {
e4aa175a 670 $optionlist = array();
1adc77e6 671 foreach ($scorm_popoup_options as $name => $option) {
e4aa175a 672 if (isset($scorm->$name)) {
673 $optionlist[] = $name.'='.$scorm->$name;
674 } else {
675 $optionlist[] = $name.'=0';
676 }
9528568b 677 }
e4aa175a 678 $scorm->options = implode(',', $optionlist);
679 } else {
680 $scorm->options = '';
9528568b 681 }
e4aa175a 682 } else {
683 $scorm->popup = 0;
684 $scorm->options = '';
685 }
686 return $scorm;
687}
688
0b5a80a1 689/**
690 * Implementation of the function for printing the form elements that control
691 * whether the course reset functionality affects the scorm.
e5dd8e3b 692 *
28f672b2 693 * @param object $mform form passed by reference
0b5a80a1 694 */
695function scorm_reset_course_form_definition(&$mform) {
696 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
697 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
698}
699
700/**
701 * Course reset form defaults.
28f672b2 702 *
703 * @return array
0b5a80a1 704 */
705function scorm_reset_course_form_defaults($course) {
706 return array('reset_scorm'=>1);
707}
708
709/**
710 * Removes all grades from gradebook
28f672b2 711 *
712 * @global stdClass
713 * @global object
0b5a80a1 714 * @param int $courseid
715 * @param string optional type
716 */
717function scorm_reset_gradebook($courseid, $type='') {
bf347041 718 global $CFG, $DB;
0b5a80a1 719
720 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
bf347041 721 FROM {scorm} s, {course_modules} cm, {modules} m
722 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
0b5a80a1 723
bf347041 724 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 725 foreach ($scorms as $scorm) {
726 scorm_grade_item_update($scorm, 'reset');
727 }
728 }
729}
730
731/**
732 * Actual implementation of the rest coures functionality, delete all the
733 * scorm attempts for course $data->courseid.
28f672b2 734 *
735 * @global stdClass
736 * @global object
737 * @param object $data the data submitted from the reset course.
0b5a80a1 738 * @return array status array
739 */
740function scorm_reset_userdata($data) {
bf347041 741 global $CFG, $DB;
0b5a80a1 742
743 $componentstr = get_string('modulenameplural', 'scorm');
744 $status = array();
745
746 if (!empty($data->reset_scorm)) {
747 $scormssql = "SELECT s.id
bf347041 748 FROM {scorm} s
749 WHERE s.course=?";
0b5a80a1 750
bf347041 751 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
0b5a80a1 752
753 // remove all grades from gradebook
754 if (empty($data->reset_gradebook_grades)) {
755 scorm_reset_gradebook($data->courseid);
756 }
757
758 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
759 }
760
761 // no dates to shift here
762
763 return $status;
764}
765
f432bebf 766/**
767 * Returns all other caps used in module
28f672b2 768 *
769 * @return array
f432bebf 770 */
771function scorm_get_extra_capabilities() {
772 return array('moodle/site:accessallgroups');
773}
774
9528568b 775/**
776 * Lists all file areas current user may browse
28f672b2 777 *
778 * @param object $course
779 * @param object $cm
780 * @param object $context
781 * @return array
9528568b 782 */
783function scorm_get_file_areas($course, $cm, $context) {
784 $areas = array();
64f93798
PS
785 $areas['content'] = get_string('areacontent', 'scorm');
786 $areas['package'] = get_string('areapackage', 'scorm');
9528568b 787 return $areas;
788}
789
790/**
9895302c 791 * File browsing support for SCORM file areas
e5dd8e3b 792 *
9895302c
DC
793 * @param stdclass $browser
794 * @param stdclass $areas
795 * @param stdclass $course
796 * @param stdclass $cm
797 * @param stdclass $context
798 * @param string $filearea
799 * @param int $itemid
800 * @param string $filepath
801 * @param string $filename
802 * @return stdclass file_info instance or null if not found
9528568b 803 */
804function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
805 global $CFG;
806
807 if (!has_capability('moodle/course:managefiles', $context)) {
808 return null;
809 }
810
811 // no writing for now!
812
813 $fs = get_file_storage();
814
64f93798 815 if ($filearea === 'content') {
9528568b 816
817 $filepath = is_null($filepath) ? '/' : $filepath;
818 $filename = is_null($filename) ? '.' : $filename;
e5dd8e3b 819
9528568b 820 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 821 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
9528568b 822 if ($filepath === '/' and $filename === '.') {
64f93798 823 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
9528568b 824 } else {
825 // not found
826 return null;
827 }
828 }
64f93798 829 require_once("$CFG->dirroot/mod/scorm/locallib.php");
8546def3 830 return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
9528568b 831
64f93798 832 } else if ($filearea === 'package') {
9528568b 833 $filepath = is_null($filepath) ? '/' : $filepath;
834 $filename = is_null($filename) ? '.' : $filename;
e5dd8e3b 835
9528568b 836 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 837 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, $filepath, $filename)) {
9528568b 838 if ($filepath === '/' and $filename === '.') {
64f93798 839 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
9528568b 840 } else {
841 // not found
842 return null;
843 }
844 }
9895302c 845 return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
9528568b 846 }
847
848 // scorm_intro handled in file_browser
849
850 return false;
851}
852
853/**
854 * Serves scorm content, introduction images and packages. Implements needed access control ;-)
28f672b2 855 *
28f672b2 856 * @param object $course
64f93798 857 * @param object $cm
28f672b2 858 * @param object $context
859 * @param string $filearea
860 * @param array $args
98edf7b6 861 * @param bool $forcedownload
86900a93 862 * @return bool false if file not found, does not return if found - just send the file
9528568b 863 */
64f93798 864function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
9528568b 865 global $CFG;
866
64f93798 867 if ($context->contextlevel != CONTEXT_MODULE) {
0a8a7b6c 868 return false;
869 }
e5dd8e3b 870
0a8a7b6c 871 require_login($course, true, $cm);
872
64f93798
PS
873 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
874
875 if ($filearea === 'content') {
9528568b 876 $revision = (int)array_shift($args); // prevents caching problems - ignored here
64f93798 877 $relativepath = implode('/', $args);
4cec22dc 878 $fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
9528568b 879 // TODO: add any other access restrictions here if needed!
880
64f93798 881 } else if ($filearea === 'package') {
9528568b 882 if (!has_capability('moodle/course:manageactivities', $context)) {
883 return false;
884 }
64f93798
PS
885 $relativepath = implode('/', $args);
886 $fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
9528568b 887 $lifetime = 0; // no caching here
888
889 } else {
890 return false;
891 }
892
893 $fs = get_file_storage();
894 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
895 return false;
896 }
897
898 // finally send the file
899 send_stored_file($file, $lifetime, 0, false);
900}
901
42f103be 902/**
28f672b2 903 * @uses FEATURE_GROUPS
904 * @uses FEATURE_GROUPINGS
905 * @uses FEATURE_GROUPMEMBERSONLY
906 * @uses FEATURE_MOD_INTRO
907 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
908 * @uses FEATURE_GRADE_HAS_GRADE
909 * @uses FEATURE_GRADE_OUTCOMES
42f103be 910 * @param string $feature FEATURE_xx constant for requested feature
28f672b2 911 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 912 */
913function scorm_supports($feature) {
914 switch($feature) {
915 case FEATURE_GROUPS: return false;
916 case FEATURE_GROUPINGS: return false;
917 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 918 case FEATURE_MOD_INTRO: return true;
42f103be 919 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
920 case FEATURE_GRADE_HAS_GRADE: return true;
921 case FEATURE_GRADE_OUTCOMES: return true;
02488b86 922 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 923
924 default: return null;
925 }
926}
0a1f8f8f
SH
927
928/**
929 * This function extends the global navigaiton for the site.
930 * It is important to note that you should not rely on PAGE objects within this
931 * body of code as there is no guarantee that during an AJAX request they are
932 * available
933 *
934 * @param navigation_node $navigation The scorm node within the global navigation
935 * @param stdClass $course The course object returned from the DB
936 * @param stdClass $module The module object returned from the DB
937 * @param stdClass $cm The course module isntance returned from the DB
938 */
939function scorm_extend_navigation($navigation, $course, $module, $cm) {
940 /**
941 * This is currently just a stub so that it can be easily expanded upon.
942 * When expanding just remove this comment and the line below and then add
943 * you content.
944 */
945 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
02488b86 946}
169a204c
DM
947/**
948 * writes log output to a temp log file
949 *
950 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
951 * @param string $text - text to be written to file.
952 * @param integer $scoid - scoid of object this log entry is for.
953 */
954function scorm_write_log($type, $text, $scoid) {
955 global $CFG, $USER;
956
957 $debugenablelog = debugging('', DEBUG_DEVELOPER);
958 if (!$debugenablelog || empty($text)) {
959 return ;
960 }
961 if (make_upload_directory('temp/scormlogs/')) {
962 $logpath = $CFG->dataroot.'/temp/scormlogs';
963
964 $logfile = $logpath.'/'.$type.'debug_'.$USER->id.'_'.$scoid.'.log';
965 @file_put_contents($logfile, date('Y/m/d H:i:s O')." DEBUG $text\r\n", FILE_APPEND);
966 }
967}
d860e4f8
DM
968/**
969 * writes overview info for course_overview block - displays upcoming scorm objects that have a due date
970 *
971 * @param object $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
972 * @param array $htmlarray
64f93798 973 * @return mixed
d860e4f8
DM
974 */
975function scorm_print_overview($courses, &$htmlarray) {
976 global $USER, $CFG, $DB;
977
978 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
979 return array();
980 }
981
982 if (!$scorms = get_all_instances_in_courses('scorm',$courses)) {
983 return;
984 }
985
986 $scormids = array();
987
988 // Do scorm::isopen() here without loading the whole thing for speed
989 foreach ($scorms as $key => $scorm) {
990 $time = time();
991 if ($scorm->timeopen) {
992 $isopen = ($scorm->timeopen <= $time && $time <= $scorm->timeclose);
993 }
994 if (empty($isopen) || empty($scorm->timeclose)) {
995 unset($scorms[$key]);
996 }else{
997 $scormids[] = $scorm->id;
998 }
999 }
1000
1001 if(empty($scormids)){
1002 // no assigments to look at - we're done
1003 return true;
1004 }
1005 $strscorm = get_string('modulename', 'scorm');
1006 $strduedate = get_string('duedate', 'scorm');
1007
1008 foreach ($scorms as $scorm) {
1009 $str = '<div class="scorm overview"><div class="name">'.$strscorm. ': '.
1010 '<a '.($scorm->visible ? '':' class="dimmed"').
1011 'title="'.$strscorm.'" href="'.$CFG->wwwroot.
1012 '/mod/assignment/view.php?id='.$scorm->coursemodule.'">'.
1013 $scorm->name.'</a></div>';
1014 if ($scorm->timeclose) {
1015 $str .= '<div class="info">'.$strduedate.': '.userdate($scorm->timeclose).'</div>';
1016 }
1017
1018 $str .= '</div>';
1019 if (empty($htmlarray[$scorm->course]['scorm'])) {
1020 $htmlarray[$scorm->course]['scorm'] = $str;
1021 } else {
1022 $htmlarray[$scorm->course]['scorm'] .= $str;
1023 }
1024 }
1025}