weekly release 2.2dev
[moodle.git] / mod / scorm / api.php
CommitLineData
5c2aa157
DM
1<?php
2
3 require_once("../../config.php");
4 require_once('locallib.php');
5
6 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
7 $a = optional_param('a', '', PARAM_INT); // scorm ID
8 $scoid = required_param('scoid', PARAM_INT); // sco ID
9 $mode = optional_param('mode', '', PARAM_ALPHA); // navigation mode
10 $attempt = required_param('attempt', PARAM_INT); // new attempt
11
12 //IE 6 Bug workaround
d1e3b5c2
DM
13 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
14 @ini_set('zlib.output_compression', 'Off');
15 @apache_setenv('no-gzip', 1);
16 header( 'Content-Type: application/javascript' );
5c2aa157
DM
17 }
18
19 if (!empty($id)) {
20 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
21 print_error('invalidcoursemodule');
22 }
23 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
24 print_error('coursemisconf');
25 }
26 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
27 print_error('invalidcoursemodule');
28 }
29 } else if (!empty($a)) {
30 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
31 print_error('coursemisconf');
32 }
33 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
34 print_error('coursemisconf');
35 }
36 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
37 print_error('invalidcoursemodule');
38 }
39 } else {
40 print_error('missingparameter');
41 }
42
43 $PAGE->set_url('/mod/scorm/api.php', array('scoid'=>$scoid, 'id'=>$cm->id));
44
45 require_login($course->id, false, $cm);
46
47 if ($usertrack = scorm_get_tracks($scoid,$USER->id,$attempt)) {
c5578163
DM
48 //according to SCORM 2004 spec(RTE V1, 4.2.8), only cmi.exit==suspend should allow previous datamodel elements on re-launch
49 if ($scorm->version != "SCORM_1.3" || (isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend'))) {
5c2aa157
DM
50 foreach ($usertrack as $key => $value) {
51 $userdata->$key = addslashes_js($value);
52 }
53 } else {
54 $userdata->status = '';
55 $userdata->score_raw = '';
56 }
57 } else {
58 $userdata->status = '';
59 $userdata->score_raw = '';
60 }
61 $userdata->student_id = addslashes_js($USER->username);
62 $userdata->student_name = addslashes_js($USER->lastname .', '. $USER->firstname);
63 $userdata->mode = 'normal';
64 if (isset($mode)) {
65 $userdata->mode = $mode;
66 }
67 if ($userdata->mode == 'normal') {
68 $userdata->credit = 'credit';
69 } else {
70 $userdata->credit = 'no-credit';
71 }
72 if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
73 foreach ($scodatas as $key => $value) {
74 $userdata->$key = addslashes_js($value);
75 }
76 } else {
8909cbdc 77 print_error('cannotfindsco', 'scorm');
5c2aa157
DM
78 }
79 if (!$sco = scorm_get_sco($scoid)) {
8909cbdc 80 print_error('cannotfindsco', 'scorm');
5c2aa157
DM
81 }
82 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
83 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php')) {
84 include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php');
85 } else {
86 include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.js.php');
87 }
88 // set the start time of this SCO
89 scorm_insert_track($USER->id,$scorm->id,$scoid,$attempt,'x.start.time',time());
90?>
91
92
93var errorCode = "0";
94function underscore(str) {
95 str = String(str).replace(/.N/g,".");
96 return str.replace(/\./g,"__");
97}