MDL-18148 - AICC compliance fixes from Martin Holden.
[moodle.git] / mod / scorm / datamodel.php
1 <?php
2     require_once('../../config.php');
3     require_once('locallib.php');
4         
5     $id = optional_param('id', '', PARAM_INT);       // Course Module ID, or
6     $a = optional_param('a', '', PARAM_INT);         // scorm ID
7     $scoid = required_param('scoid', PARAM_INT);  // sco ID
8     $attempt = required_param('attempt', PARAM_INT);  // attempt number
9 //    $attempt = $SESSION->scorm_attempt;
12     if (!empty($id)) {
13         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
14             print_error('invalidcoursemodule');
15         }
16         if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
17             print_error('coursemisconf');
18         }
19         if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
20             print_error('invalidcoursemodule');
21         }
22     } else if (!empty($a)) {
23         if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
24             print_error('invalidcoursemodule');
25         }
26         if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
27             print_error('coursemisconf');
28         }
29         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
30             print_error('invalidcoursemodule');
31         }
32     } else {
33         print_error('missingparameter');
34     }
36     require_login($course->id, false, $cm);
38     if (confirm_sesskey() && (!empty($scoid))) {
39         $result = true;
40         $request = null;
41         if (has_capability('mod/scorm:savetrack', get_context_instance(CONTEXT_MODULE,$cm->id))) {
42             foreach (data_submitted() as $element => $value) {
43                 $element = str_replace('__','.',$element);
44                 if (substr($element,0,3) == 'cmi') {
45                     $netelement = preg_replace('/\.N(\d+)\./',"\.\$1\.",$element);
46                     $result = scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, $element, $value, $scorm->forcecompleted) && $result;
47                 }
48                 if (substr($element,0,15) == 'adl.nav.request') {
49                     // SCORM 2004 Sequencing Request
50                     require_once('datamodels/sequencinglib.php');
52                     $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@', '@exit@', '@exitAll@', '@abandon@', '@abandonAll@');
53                     $replace = array('continue_', 'previous_', '\1', 'exit_', 'exitall_', 'abandon_', 'abandonall');
54                     $action = preg_replace($search, $replace, $value);
56                     if ($action != $value) {
57                         // Evaluating navigation request
58                         $valid = scorm_seq_overall ($scoid,$USER->id,$action,$attempt);
59                         $valid = 'true';
61                         // Set valid request
62                         $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@');
63                         $replace = array('true', 'true', 'true');
64                         $matched = preg_replace($search, $replace, $value);
65                         if ($matched == 'true') {
66                             $request = 'adl.nav.request_valid["'.$action.'"] = "'.$valid.'";';
67                         }
68                     }
69                 }
70                 // Log every datamodel update requested
71                 if (substr($element,0,15) == 'adl.nav.request' || substr($element,0,3) == 'cmi') {
72                     if (scorm_debugging($scorm)) {
73                         add_to_log($course->id, 'scorm', 'trk: '.trim($scorm->name).' at: '.$attempt, 'view.php?id='.$cm->id, "$element => $value", $cm->id);
74                     }
75                 }
76             }
77         }
78         if ($result) {
79             echo "true\n0";
80         } else {
81             echo "false\n101";
82         }
83         if ($request != null) {
84             echo "\n".$request;
85         }
86     }
87 ?>