Merge branch 'MDL-30175' of git://github.com/stronk7/moodle
[moodle.git] / mod / scorm / api.php
blob88875ced7ca8a11bc154f5ae5777bee5e1a60750
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 require_once("../../config.php");
18 require_once('locallib.php');
20 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
21 $a = optional_param('a', '', PARAM_INT); // scorm ID
22 $scoid = required_param('scoid', PARAM_INT); // sco ID
23 $mode = optional_param('mode', '', PARAM_ALPHA); // navigation mode
24 $attempt = required_param('attempt', PARAM_INT); // new attempt
26 //IE 6 Bug workaround
27 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
28 @ini_set('zlib.output_compression', 'Off');
29 @apache_setenv('no-gzip', 1);
30 header( 'Content-Type: application/javascript' );
33 if (!empty($id)) {
34 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
35 print_error('invalidcoursemodule');
37 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
38 print_error('coursemisconf');
40 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
41 print_error('invalidcoursemodule');
43 } else if (!empty($a)) {
44 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
45 print_error('coursemisconf');
47 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
48 print_error('coursemisconf');
50 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
51 print_error('invalidcoursemodule');
53 } else {
54 print_error('missingparameter');
57 $PAGE->set_url('/mod/scorm/api.php', array('scoid'=>$scoid, 'id'=>$cm->id));
59 require_login($course->id, false, $cm);
61 if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
62 //according to SCORM 2004 spec(RTE V1, 4.2.8), only cmi.exit==suspend should allow previous datamodel elements on re-launch
63 if (!scorm_version_check($scorm->version, SCORM_13) || (isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend'))) {
64 foreach ($usertrack as $key => $value) {
65 $userdata->$key = addslashes_js($value);
67 } else {
68 $userdata->status = '';
69 $userdata->score_raw = '';
71 } else {
72 $userdata->status = '';
73 $userdata->score_raw = '';
75 $userdata->student_id = addslashes_js($USER->username);
76 $userdata->student_name = addslashes_js($USER->lastname .', '. $USER->firstname);
77 $userdata->mode = 'normal';
78 if (isset($mode)) {
79 $userdata->mode = $mode;
81 if ($userdata->mode == 'normal') {
82 $userdata->credit = 'credit';
83 } else {
84 $userdata->credit = 'no-credit';
86 if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
87 foreach ($scodatas as $key => $value) {
88 $userdata->$key = addslashes_js($value);
90 } else {
91 print_error('cannotfindsco', 'scorm');
93 if (!$sco = scorm_get_sco($scoid)) {
94 print_error('cannotfindsco', 'scorm');
96 if (scorm_version_check($scorm->version, SCORM_13)) {
97 $userdata->{'cmi.scaled_passing_score'} = $DB->get_field('scorm_seq_objective', 'minnormalizedmeasure', array('scoid'=>$scoid));
99 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
100 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php')) {
101 include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php');
102 } else {
103 include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.js.php');
105 // set the start time of this SCO
106 scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());
110 var errorCode = "0";
111 function underscore(str) {
112 str = String(str).replace(/.N/g,".");
113 return str.replace(/\./g,"__");