Merge branch 'm20_MDL-26176_cleanup' of github.com:danmarsden/moodle
[moodle.git] / mod / scorm / datamodel.php
CommitLineData
03f5a0f8 1<?php
2 require_once('../../config.php');
86996ffe 3 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
e5dd8e3b 4
03f5a0f8 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
2347a351 8 $attempt = required_param('attempt', PARAM_INT); // attempt number
9// $attempt = $SESSION->scorm_attempt;
03f5a0f8 10
11
12 if (!empty($id)) {
4be90930 13 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
08b56f93 14 print_error('invalidcoursemodule');
03f5a0f8 15 }
bf347041 16 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
08b56f93 17 print_error('coursemisconf');
03f5a0f8 18 }
bf347041 19 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
08b56f93 20 print_error('invalidcoursemodule');
03f5a0f8 21 }
22 } else if (!empty($a)) {
bf347041 23 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
08b56f93 24 print_error('invalidcoursemodule');
03f5a0f8 25 }
bf347041 26 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
08b56f93 27 print_error('coursemisconf');
03f5a0f8 28 }
29 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
08b56f93 30 print_error('invalidcoursemodule');
03f5a0f8 31 }
32 } else {
08b56f93 33 print_error('missingparameter');
03f5a0f8 34 }
35
a6855934 36 $PAGE->set_url('/mod/scorm/datamodel.php', array('scoid'=>$scoid,'attempt'=>$attempt, 'id'=>$cm->id));
5c508e3f 37
03f5a0f8 38 require_login($course->id, false, $cm);
39
40 if (confirm_sesskey() && (!empty($scoid))) {
41 $result = true;
2347a351 42 $request = null;
03f5a0f8 43 if (has_capability('mod/scorm:savetrack', get_context_instance(CONTEXT_MODULE,$cm->id))) {
83eceb6c 44 foreach (data_submitted() as $element => $value) {
2347a351 45 $element = str_replace('__','.',$element);
03f5a0f8 46 if (substr($element,0,3) == 'cmi') {
b0758a40 47 $netelement = preg_replace('/\.N(\d+)\./',"\.\$1\.",$element);
6381fa56 48 $result = scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, $element, $value, $scorm->forcecompleted) && $result;
03f5a0f8 49 }
2347a351 50 if (substr($element,0,15) == 'adl.nav.request') {
51 // SCORM 2004 Sequencing Request
86996ffe 52 require_once($CFG->dirroot.'/mod/scorm/datamodels/sequencinglib.php');
2347a351 53
54 $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@', '@exit@', '@exitAll@', '@abandon@', '@abandonAll@');
55 $replace = array('continue_', 'previous_', '\1', 'exit_', 'exitall_', 'abandon_', 'abandonall');
56 $action = preg_replace($search, $replace, $value);
57
58 if ($action != $value) {
59 // Evaluating navigation request
6d75dcf5 60 $valid = scorm_seq_overall ($scoid,$USER->id,$action,$attempt);
b0758a40 61 $valid = 'true';
2347a351 62
63 // Set valid request
64 $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@');
65 $replace = array('true', 'true', 'true');
66 $matched = preg_replace($search, $replace, $value);
67 if ($matched == 'true') {
68 $request = 'adl.nav.request_valid["'.$action.'"] = "'.$valid.'";';
69 }
70 }
71 }
e341f9c1 72 // Log every datamodel update requested
73 if (substr($element,0,15) == 'adl.nav.request' || substr($element,0,3) == 'cmi') {
74 if (scorm_debugging($scorm)) {
75 add_to_log($course->id, 'scorm', 'trk: '.trim($scorm->name).' at: '.$attempt, 'view.php?id='.$cm->id, "$element => $value", $cm->id);
76 }
77 }
03f5a0f8 78 }
79 }
80 if ($result) {
81 echo "true\n0";
82 } else {
83 echo "false\n101";
84 }
2347a351 85 if ($request != null) {
86 echo "\n".$request;
87 }
03f5a0f8 88 }
e5dd8e3b 89