MDL-45339 Libraries: Add a note to docblock of get_config, set_config, and unset_conf...
[moodle.git] / mod / scorm / datamodels / scorm_13.js.php
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.
13 //
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($CFG->dirroot.'/mod/scorm/locallib.php');
19 if (isset($userdata->status)) {
20     if (!isset($userdata->{'cmi.exit'}) || (($userdata->{'cmi.exit'} == 'time-out') || ($userdata->{'cmi.exit'} == 'normal'))) {
21             $userdata->entry = 'ab-initio';
22     } else {
23         if (isset($userdata->{'cmi.exit'}) && (($userdata->{'cmi.exit'} == 'suspend') || ($userdata->{'cmi.exit'} == 'logout'))) {
24             $userdata->entry = 'resume';
25         } else {
26             $userdata->entry = '';
27         }
28     }
29 }
30 if (!isset($currentorg)) {
31     $currentorg = '';
32 }
33 ?>
35 // Used need to debug cmi content (if you uncomment this, you must comment the definition inside SCORMapi1_3)
36 //var cmi = new Object();
38 //
39 // SCORM 1.3 API Implementation
40 //
41 function SCORMapi1_3() {
42     // Standard Data Type Definition
44     // language key has to be checked for language dependent strings
45     var validLanguages = {'aa':'aa', 'ab':'ab', 'ae':'ae', 'af':'af', 'ak':'ak', 'am':'am', 'an':'an', 'ar':'ar', 'as':'as', 'av':'av', 'ay':'ay', 'az':'az',
46                           'ba':'ba', 'be':'be', 'bg':'bg', 'bh':'bh', 'bi':'bi', 'bm':'bm', 'bn':'bn', 'bo':'bo', 'br':'br', 'bs':'bs',
47                           'ca':'ca', 'ce':'ce', 'ch':'ch', 'co':'co', 'cr':'cr', 'cs':'cs', 'cu':'cu', 'cv':'cv', 'cy':'cy',
48                           'da':'da', 'de':'de', 'dv':'dv', 'dz':'dz', 'ee':'ee', 'el':'el', 'en':'en', 'eo':'eo', 'es':'es', 'et':'et', 'eu':'eu',
49                           'fa':'fa', 'ff':'ff', 'fi':'fi', 'fj':'fj', 'fo':'fo', 'fr':'fr', 'fy':'fy', 'ga':'ga', 'gd':'gd', 'gl':'gl', 'gn':'gn', 'gu':'gu', 'gv':'gv',
50                           'ha':'ha', 'he':'he', 'hi':'hi', 'ho':'ho', 'hr':'hr', 'ht':'ht', 'hu':'hu', 'hy':'hy', 'hz':'hz',
51                           'ia':'ia', 'id':'id', 'ie':'ie', 'ig':'ig', 'ii':'ii', 'ik':'ik', 'io':'io', 'is':'is', 'it':'it', 'iu':'iu',
52                           'ja':'ja', 'jv':'jv', 'ka':'ka', 'kg':'kg', 'ki':'ki', 'kj':'kj', 'kk':'kk', 'kl':'kl', 'km':'km', 'kn':'kn', 'ko':'ko', 'kr':'kr', 'ks':'ks', 'ku':'ku', 'kv':'kv', 'kw':'kw', 'ky':'ky',
53                           'la':'la', 'lb':'lb', 'lg':'lg', 'li':'li', 'ln':'ln', 'lo':'lo', 'lt':'lt', 'lu':'lu', 'lv':'lv',
54                           'mg':'mg', 'mh':'mh', 'mi':'mi', 'mk':'mk', 'ml':'ml', 'mn':'mn', 'mo':'mo', 'mr':'mr', 'ms':'ms', 'mt':'mt', 'my':'my',
55                           'na':'na', 'nb':'nb', 'nd':'nd', 'ne':'ne', 'ng':'ng', 'nl':'nl', 'nn':'nn', 'no':'no', 'nr':'nr', 'nv':'nv', 'ny':'ny',
56                           'oc':'oc', 'oj':'oj', 'om':'om', 'or':'or', 'os':'os', 'pa':'pa', 'pi':'pi', 'pl':'pl', 'ps':'ps', 'pt':'pt',
57                           'qu':'qu', 'rm':'rm', 'rn':'rn', 'ro':'ro', 'ru':'ru', 'rw':'rw',
58                           'sa':'sa', 'sc':'sc', 'sd':'sd', 'se':'se', 'sg':'sg', 'sh':'sh', 'si':'si', 'sk':'sk', 'sl':'sl', 'sm':'sm', 'sn':'sn', 'so':'so', 'sq':'sq', 'sr':'sr', 'ss':'ss', 'st':'st', 'su':'su', 'sv':'sv', 'sw':'sw',
59                           'ta':'ta', 'te':'te', 'tg':'tg', 'th':'th', 'ti':'ti', 'tk':'tk', 'tl':'tl', 'tn':'tn', 'to':'to', 'tr':'tr', 'ts':'ts', 'tt':'tt', 'tw':'tw', 'ty':'ty',
60                           'ug':'ug', 'uk':'uk', 'ur':'ur', 'uz':'uz', 've':'ve', 'vi':'vi', 'vo':'vo',
61                           'wa':'wa', 'wo':'wo', 'xh':'xh', 'yi':'yi', 'yo':'yo', 'za':'za', 'zh':'zh', 'zu':'zu',
62                           'aar':'aar', 'abk':'abk', 'ave':'ave', 'afr':'afr', 'aka':'aka', 'amh':'amh', 'arg':'arg', 'ara':'ara', 'asm':'asm', 'ava':'ava', 'aym':'aym', 'aze':'aze',
63                           'bak':'bak', 'bel':'bel', 'bul':'bul', 'bih':'bih', 'bis':'bis', 'bam':'bam', 'ben':'ben', 'tib':'tib', 'bod':'bod', 'bre':'bre', 'bos':'bos',
64                           'cat':'cat', 'che':'che', 'cha':'cha', 'cos':'cos', 'cre':'cre', 'cze':'cze', 'ces':'ces', 'chu':'chu', 'chv':'chv', 'wel':'wel', 'cym':'cym',
65                           'dan':'dan', 'ger':'ger', 'deu':'deu', 'div':'div', 'dzo':'dzo', 'ewe':'ewe', 'gre':'gre', 'ell':'ell', 'eng':'eng', 'epo':'epo', 'spa':'spa', 'est':'est', 'baq':'baq', 'eus':'eus', 'per':'per',
66                           'fas':'fas', 'ful':'ful', 'fin':'fin', 'fij':'fij', 'fao':'fao', 'fre':'fre', 'fra':'fra', 'fry':'fry', 'gle':'gle', 'gla':'gla', 'glg':'glg', 'grn':'grn', 'guj':'guj', 'glv':'glv',
67                           'hau':'hau', 'heb':'heb', 'hin':'hin', 'hmo':'hmo', 'hrv':'hrv', 'hat':'hat', 'hun':'hun', 'arm':'arm', 'hye':'hye', 'her':'her',
68                           'ina':'ina', 'ind':'ind', 'ile':'ile', 'ibo':'ibo', 'iii':'iii', 'ipk':'ipk', 'ido':'ido', 'ice':'ice', 'isl':'isl', 'ita':'ita', 'iku':'iku',
69                           'jpn':'jpn', 'jav':'jav', 'geo':'geo', 'kat':'kat', 'kon':'kon', 'kik':'kik', 'kua':'kua', 'kaz':'kaz', 'kal':'kal', 'khm':'khm', 'kan':'kan', 'kor':'kor', 'kau':'kau', 'kas':'kas', 'kur':'kur', 'kom':'kom', 'cor':'cor', 'kir':'kir',
70                           'lat':'lat', 'ltz':'ltz', 'lug':'lug', 'lim':'lim', 'lin':'lin', 'lao':'lao', 'lit':'lit', 'lub':'lub', 'lav':'lav',
71                           'mlg':'mlg', 'mah':'mah', 'mao':'mao', 'mri':'mri', 'mac':'mac', 'mkd':'mkd', 'mal':'mal', 'mon':'mon', 'mol':'mol', 'mar':'mar', 'may':'may', 'msa':'msa', 'mlt':'mlt', 'bur':'bur', 'mya':'mya',
72                           'nau':'nau', 'nob':'nob', 'nde':'nde', 'nep':'nep', 'ndo':'ndo', 'dut':'dut', 'nld':'nld', 'nno':'nno', 'nor':'nor', 'nbl':'nbl', 'nav':'nav', 'nya':'nya',
73                           'oci':'oci', 'oji':'oji', 'orm':'orm', 'ori':'ori', 'oss':'oss', 'pan':'pan', 'pli':'pli', 'pol':'pol', 'pus':'pus', 'por':'por', 'que':'que',
74                           'roh':'roh', 'run':'run', 'rum':'rum', 'ron':'ron', 'rus':'rus', 'kin':'kin', 'san':'san', 'srd':'srd', 'snd':'snd', 'sme':'sme', 'sag':'sag', 'slo':'slo', 'sin':'sin', 'slk':'slk', 'slv':'slv', 'smo':'smo', 'sna':'sna', 'som':'som', 'alb':'alb', 'sqi':'sqi', 'srp':'srp', 'ssw':'ssw', 'sot':'sot', 'sun':'sun', 'swe':'swe', 'swa':'swa',
75                           'tam':'tam', 'tel':'tel', 'tgk':'tgk', 'tha':'tha', 'tir':'tir', 'tuk':'tuk', 'tgl':'tgl', 'tsn':'tsn', 'ton':'ton', 'tur':'tur', 'tso':'tso', 'tat':'tat', 'twi':'twi', 'tah':'tah',
76                           'uig':'uig', 'ukr':'ukr', 'urd':'urd', 'uzb':'uzb', 'ven':'ven', 'vie':'vie', 'vol':'vol', 'wln':'wln', 'wol':'wol', 'xho':'xho', 'yid':'yid', 'yor':'yor', 'zha':'zha', 'chi':'chi', 'zho':'zho', 'zul':'zul'};
78     var CMIString200 = '^[\\u0000-\\uFFFF]{0,200}$';
79     var CMIString250 = '^[\\u0000-\\uFFFF]{0,250}$';
80     var CMIString1000 = '^[\\u0000-\\uFFFF]{0,1000}$';
81     var CMIString4000 = '^[\\u0000-\\uFFFF]{0,4000}$';
82     var CMIString64000 = '^[\\u0000-\\uFFFF]{0,64000}$';
83     var CMILang = '^([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?$|^$';
84     var CMILangString250 = '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?([^\{].{0,250}$)?';
85     var CMILangcr = '^((\{lang=([a-zA-Z]{2,3}|i|x)?(\-[a-zA-Z0-9\-]{2,8})?\}))(.*?)$';
86     var CMILangString250cr = '^((\{lang=([a-zA-Z]{2,3}|i|x)?(\-[a-zA-Z0-9\-]{2,8})?\})?(.{0,250})?)?$';
87     var CMILangString4000 = '^(\{lang=([a-zA-Z]{2,3}|i|x)(\-[a-zA-Z0-9\-]{2,8})?\})?([^\{].{0,4000}$)?';
88     var CMITime = '^(19[7-9]{1}[0-9]{1}|20[0-2]{1}[0-9]{1}|203[0-8]{1})((-(0[1-9]{1}|1[0-2]{1}))((-(0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1}))(T([0-1]{1}[0-9]{1}|2[0-3]{1})((:[0-5]{1}[0-9]{1})((:[0-5]{1}[0-9]{1})((\\.[0-9]{1,2})((Z|([+|-]([0-1]{1}[0-9]{1}|2[0-3]{1})))(:[0-5]{1}[0-9]{1})?)?)?)?)?)?)?)?$';
89     var CMITimespan = '^P(\\d+Y)?(\\d+M)?(\\d+D)?(T(((\\d+H)(\\d+M)?(\\d+(\.\\d{1,2})?S)?)|((\\d+M)(\\d+(\.\\d{1,2})?S)?)|((\\d+(\.\\d{1,2})?S))))?$';
90     var CMIInteger = '^\\d+$';
91     var CMISInteger = '^-?([0-9]+)$';
92     var CMIDecimal = '^-?([0-9]{1,5})(\\.[0-9]{1,18})?$';
93     var CMIIdentifier = '^\\S{1,250}[a-zA-Z0-9]$';
94     var CMIShortIdentifier = '^[\\w\.]{1,250}$';
95     var CMILongIdentifier = '^(?:(?!urn:)\\S{1,4000}|urn:[A-Za-z0-9-]{1,31}:\\S{1,4000})$';
96     var CMIFeedback = '^.*$'; // This must be redefined
97     var CMIIndex = '[._](\\d+).';
98     var CMIIndexStore = '.N(\\d+).';
99     // Vocabulary Data Type Definition
100     var CMICStatus = '^completed$|^incomplete$|^not attempted$|^unknown$';
101     var CMISStatus = '^passed$|^failed$|^unknown$';
102     var CMIExit = '^time-out$|^suspend$|^logout$|^normal$|^$';
103     var CMIType = '^true-false$|^choice$|^(long-)?fill-in$|^matching$|^performance$|^sequencing$|^likert$|^numeric$|^other$';
104     var CMIResult = '^correct$|^incorrect$|^unanticipated$|^neutral$|^-?([0-9]{1,4})(\\.[0-9]{1,18})?$';
105     var NAVEvent = '^previous$|^continue$|^exit$|^exitAll$|^abandon$|^abandonAll$|^suspendAll$|^\{target=\\S{0,200}[a-zA-Z0-9]\}choice|jump$';
106     var NAVBoolean = '^unknown$|^true$|^false$';
107     var NAVTarget = '^previous$|^continue$|^choice.{target=\\S{0,200}[a-zA-Z0-9]}$'
108     // Children lists
109     var cmi_children = '_version,comments_from_learner,comments_from_lms,completion_status,credit,entry,exit,interactions,launch_data,learner_id,learner_name,learner_preference,location,max_time_allowed,mode,objectives,progress_measure,scaled_passing_score,score,session_time,success_status,suspend_data,time_limit_action,total_time';
110     var comments_children = 'comment,timestamp,location';
111     var score_children = 'max,raw,scaled,min';
112     var objectives_children = 'progress_measure,completion_status,success_status,description,score,id';
113     var correct_responses_children = 'pattern';
114     var student_data_children = 'mastery_score,max_time_allowed,time_limit_action';
115     var student_preference_children = 'audio_level,audio_captioning,delivery_speed,language';
116     var interactions_children = 'id,type,objectives,timestamp,correct_responses,weighting,learner_response,result,latency,description';
117     // Data ranges
118     var scaled_range = '-1#1';
119     var audio_range = '0#*';
120     var speed_range = '0#*';
121     var text_range = '-1#1';
122     var progress_range = '0#1';
123     var learner_response = {
124         'true-false':{'format':'^true$|^false$', 'max':1, 'delimiter':'', 'unique':false},
125         'choice':{'format':CMIShortIdentifier, 'max':36, 'delimiter':'[,]', 'unique':true},
126         'fill-in':{'format':CMILangString250, 'max':10, 'delimiter':'[,]', 'unique':false},
127         'long-fill-in':{'format':CMILangString4000, 'max':1, 'delimiter':'', 'unique':false},
128         'matching':{'format':CMIShortIdentifier, 'format2':CMIShortIdentifier, 'max':36, 'delimiter':'[,]', 'delimiter2':'[.]', 'unique':false},
129         'performance':{'format':'^$|'+CMIShortIdentifier, 'format2':CMIDecimal+'|^$|'+CMIShortIdentifier, 'max':250, 'delimiter':'[,]', 'delimiter2':'[.]', 'unique':false},
130         'sequencing':{'format':CMIShortIdentifier, 'max':36, 'delimiter':'[,]', 'unique':false},
131         'likert':{'format':CMIShortIdentifier, 'max':1, 'delimiter':'', 'unique':false},
132         'numeric':{'format':CMIDecimal, 'max':1, 'delimiter':'', 'unique':false},
133         'other':{'format':CMIString4000, 'max':1, 'delimiter':'', 'unique':false}
134     }
136     var correct_responses = {
137         'true-false':{'pre':'', 'max':1, 'delimiter':'', 'unique':false, 'duplicate':false,
138                       'format':'^true$|^false$',
139                       'limit':1},
140         'choice':{'pre':'', 'max':36, 'delimiter':'[,]', 'unique':true, 'duplicate':false,
141                   'format':CMIShortIdentifier},
142 //        'fill-in':{'pre':'^(((\{case_matters=(true|false)\})(\{order_matters=(true|false)\})?)|((\{order_matters=(true|false)\})(\{case_matters=(true|false)\})?))(.*?)$',
143         'fill-in':{'pre':'',
144                    'max':10, 'delimiter':'[,]', 'unique':false, 'duplicate':false,
145                    'format':CMILangString250cr},
146         'long-fill-in':{'pre':'^(\{case_matters=(true|false)\})?', 'max':1, 'delimiter':'', 'unique':false, 'duplicate':true,
147                         'format':CMILangString4000},
148         'matching':{'pre':'', 'max':36, 'delimiter':'[,]', 'delimiter2':'[.]', 'unique':false, 'duplicate':false,
149                     'format':CMIShortIdentifier, 'format2':CMIShortIdentifier},
150         'performance':{'pre':'^(\{order_matters=(true|false)\})?',
151                        'max':250, 'delimiter':'[,]', 'delimiter2':'[.]', 'unique':false, 'duplicate':false,
152                        'format':'^$|'+CMIShortIdentifier, 'format2':CMIDecimal+'|^$|'+CMIShortIdentifier},
153         'sequencing':{'pre':'', 'max':36, 'delimiter':'[,]', 'unique':false, 'duplicate':false,
154                       'format':CMIShortIdentifier},
155         'likert':{'pre':'', 'max':1, 'delimiter':'', 'unique':false, 'duplicate':false,
156                   'format':CMIShortIdentifier,
157                   'limit':1},
158         'numeric':{'pre':'', 'max':2, 'delimiter':'[:]', 'unique':false, 'duplicate':false,
159                    'format':CMIDecimal,
160                    'limit':1},
161         'other':{'pre':'', 'max':1, 'delimiter':'', 'unique':false, 'duplicate':false,
162                  'format':CMIString4000,
163                  'limit':1}
164     }
166     // The SCORM 1.3 data model
167     var datamodel =  {
168         'cmi._children':{'defaultvalue':cmi_children, 'mod':'r'},
169         'cmi._version':{'defaultvalue':'1.0', 'mod':'r'},
170         'cmi.comments_from_learner._children':{'defaultvalue':comments_children, 'mod':'r'},
171         'cmi.comments_from_learner._count':{'mod':'r', 'defaultvalue':'0'},
172         'cmi.comments_from_learner.n.comment':{'format':CMILangString4000, 'mod':'rw'},
173         'cmi.comments_from_learner.n.location':{'format':CMIString250, 'mod':'rw'},
174         'cmi.comments_from_learner.n.timestamp':{'format':CMITime, 'mod':'rw'},
175         'cmi.comments_from_lms._children':{'defaultvalue':comments_children, 'mod':'r'},
176         'cmi.comments_from_lms._count':{'mod':'r', 'defaultvalue':'0'},
177         'cmi.comments_from_lms.n.comment':{'format':CMILangString4000, 'mod':'r'},
178         'cmi.comments_from_lms.n.location':{'format':CMIString250, 'mod':'r'},
179         'cmi.comments_from_lms.n.timestamp':{'format':CMITime, 'mod':'r'},
180         'cmi.completion_status':{'defaultvalue':'<?php echo !empty($userdata->{'cmi.completion_status'})?$userdata->{'cmi.completion_status'}:'unknown' ?>', 'format':CMICStatus, 'mod':'rw'},
181         'cmi.completion_threshold':{'defaultvalue':<?php echo !empty($userdata->threshold)?'\''.$userdata->threshold.'\'':'null' ?>, 'mod':'r'},
182         'cmi.credit':{'defaultvalue':'<?php echo !empty($userdata->credit)?$userdata->credit:'' ?>', 'mod':'r'},
183         'cmi.entry':{'defaultvalue':'<?php echo $userdata->entry ?>', 'mod':'r'},
184         'cmi.exit':{'defaultvalue':'<?php echo !empty($userdata->{'cmi.exit'})?$userdata->{'cmi.exit'}:'' ?>', 'format':CMIExit, 'mod':'w'},
185         'cmi.interactions._children':{'defaultvalue':interactions_children, 'mod':'r'},
186         'cmi.interactions._count':{'mod':'r', 'defaultvalue':'0'},
187         'cmi.interactions.n.id':{'pattern':CMIIndex, 'format':CMILongIdentifier, 'mod':'rw'},
188         'cmi.interactions.n.type':{'pattern':CMIIndex, 'format':CMIType, 'mod':'rw'},
189         'cmi.interactions.n.objectives._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0'},
190         'cmi.interactions.n.objectives.n.id':{'pattern':CMIIndex, 'format':CMILongIdentifier, 'mod':'rw'},
191         'cmi.interactions.n.timestamp':{'pattern':CMIIndex, 'format':CMITime, 'mod':'rw'},
192         'cmi.interactions.n.correct_responses._count':{'defaultvalue':'0', 'pattern':CMIIndex, 'mod':'r'},
193         'cmi.interactions.n.correct_responses.n.pattern':{'pattern':CMIIndex, 'format':'CMIFeedback', 'mod':'rw'},
194         'cmi.interactions.n.weighting':{'pattern':CMIIndex, 'format':CMIDecimal, 'mod':'rw'},
195         'cmi.interactions.n.learner_response':{'pattern':CMIIndex, 'format':'CMIFeedback', 'mod':'rw'},
196         'cmi.interactions.n.result':{'pattern':CMIIndex, 'format':CMIResult, 'mod':'rw'},
197         'cmi.interactions.n.latency':{'pattern':CMIIndex, 'format':CMITimespan, 'mod':'rw'},
198         'cmi.interactions.n.description':{'pattern':CMIIndex, 'format':CMILangString250, 'mod':'rw'},
199         'cmi.launch_data':{'defaultvalue':<?php echo !empty($userdata->datafromlms)?'\''.$userdata->datafromlms.'\'':'null' ?>, 'mod':'r'},
200         'cmi.learner_id':{'defaultvalue':'<?php echo $userdata->student_id ?>', 'mod':'r'},
201         'cmi.learner_name':{'defaultvalue':'<?php echo $userdata->student_name ?>', 'mod':'r'},
202         'cmi.learner_preference._children':{'defaultvalue':student_preference_children, 'mod':'r'},
203         'cmi.learner_preference.audio_level':{'defaultvalue':<?php echo !empty($userdata->{'cmi.learner_preference.audio_level'})?'\''.$userdata->{'cmi.learner_preference.audio_level'}.'\'':'\'1\'' ?>, 'format':CMIDecimal, 'range':audio_range, 'mod':'rw'},
204         'cmi.learner_preference.language':{'defaultvalue':<?php echo !empty($userdata->{'cmi.learner_preference.language'})?'\''.$userdata->{'cmi.learner_preference.language'}.'\'':'\'\'' ?>, 'format':CMILang, 'mod':'rw'},
205         'cmi.learner_preference.delivery_speed':{'defaultvalue':<?php echo !empty($userdata->{'cmi.learner_preference.delivery_speed'})?'\''.$userdata->{'cmi.learner_preference.delivery_speed'}.'\'':'\'1\'' ?>, 'format':CMIDecimal, 'range':speed_range, 'mod':'rw'},
206         'cmi.learner_preference.audio_captioning':{'defaultvalue':<?php echo !empty($userdata->{'cmi.learner_preference.audio_captioning'})?'\''.$userdata->{'cmi.learner_preference.audio_captioning'}.'\'':'\'0\'' ?>, 'format':CMISInteger, 'range':text_range, 'mod':'rw'},
207         'cmi.location':{'defaultvalue':<?php echo !empty($userdata->{'cmi.location'})?'\''.$userdata->{'cmi.location'}.'\'':'null' ?>, 'format':CMIString1000, 'mod':'rw'},
208         'cmi.max_time_allowed':{'defaultvalue':<?php echo !empty($userdata->attemptAbsoluteDurationLimit)?'\''.$userdata->attemptAbsoluteDurationLimit.'\'':'null' ?>, 'mod':'r'},
209         'cmi.mode':{'defaultvalue':'<?php echo $userdata->mode ?>', 'mod':'r'},
210         'cmi.objectives._children':{'defaultvalue':objectives_children, 'mod':'r'},
211         'cmi.objectives._count':{'mod':'r', 'defaultvalue':'0'},
212         'cmi.objectives.n.id':{'pattern':CMIIndex, 'format':CMILongIdentifier, 'mod':'rw'},
213         'cmi.objectives.n.score._children':{'defaultvalue':score_children, 'pattern':CMIIndex, 'mod':'r'},
214         'cmi.objectives.n.score.scaled':{'defaultvalue':null, 'pattern':CMIIndex, 'format':CMIDecimal, 'range':scaled_range, 'mod':'rw'},
215         'cmi.objectives.n.score.raw':{'defaultvalue':null, 'pattern':CMIIndex, 'format':CMIDecimal, 'mod':'rw'},
216         'cmi.objectives.n.score.min':{'defaultvalue':null, 'pattern':CMIIndex, 'format':CMIDecimal, 'mod':'rw'},
217         'cmi.objectives.n.score.max':{'defaultvalue':null, 'pattern':CMIIndex, 'format':CMIDecimal, 'mod':'rw'},
218         'cmi.objectives.n.success_status':{'defaultvalue':'unknown', 'pattern':CMIIndex, 'format':CMISStatus, 'mod':'rw'},
219         'cmi.objectives.n.completion_status':{'defaultvalue':'unknown', 'pattern':CMIIndex, 'format':CMICStatus, 'mod':'rw'},
220         'cmi.objectives.n.progress_measure':{'defaultvalue':null, 'format':CMIDecimal, 'range':progress_range, 'mod':'rw'},
221         'cmi.objectives.n.description':{'pattern':CMIIndex, 'format':CMILangString250, 'mod':'rw'},
222         'cmi.progress_measure':{'defaultvalue':<?php echo !empty($userdata->{'cmi.progress_measure'})?'\''.$userdata->{'cmi.progress_measure'}.'\'':'null' ?>, 'format':CMIDecimal, 'range':progress_range, 'mod':'rw'},
223         'cmi.scaled_passing_score':{'defaultvalue':<?php echo !empty($userdata->{'cmi.scaled_passing_score'})?'\''.$userdata->{'cmi.scaled_passing_score'}.'\'':'null' ?>, 'format':CMIDecimal, 'range':scaled_range, 'mod':'r'},
224         'cmi.score._children':{'defaultvalue':score_children, 'mod':'r'},
225         'cmi.score.scaled':{'defaultvalue':<?php echo !empty($userdata->{'cmi.score.scaled'})?'\''.$userdata->{'cmi.score.scaled'}.'\'':'null' ?>, 'format':CMIDecimal, 'range':scaled_range, 'mod':'rw'},
226         'cmi.score.raw':{'defaultvalue':<?php echo !empty($userdata->{'cmi.score.raw'})?'\''.$userdata->{'cmi.score.raw'}.'\'':'null' ?>, 'format':CMIDecimal, 'mod':'rw'},
227         'cmi.score.min':{'defaultvalue':<?php echo !empty($userdata->{'cmi.score.min'})?'\''.$userdata->{'cmi.score.min'}.'\'':'null' ?>, 'format':CMIDecimal, 'mod':'rw'},
228         'cmi.score.max':{'defaultvalue':<?php echo !empty($userdata->{'cmi.score.max'})?'\''.$userdata->{'cmi.score.max'}.'\'':'null' ?>, 'format':CMIDecimal, 'mod':'rw'},
229         'cmi.session_time':{'format':CMITimespan, 'mod':'w', 'defaultvalue':'PT0H0M0S'},
230         'cmi.success_status':{'defaultvalue':'<?php echo !empty($userdata->{'cmi.success_status'})?$userdata->{'cmi.success_status'}:'unknown' ?>', 'format':CMISStatus, 'mod':'rw'},
231         'cmi.suspend_data':{'defaultvalue':<?php echo !empty($userdata->{'cmi.suspend_data'})?'\''.$userdata->{'cmi.suspend_data'}.'\'':'null' ?>, 'format':CMIString64000, 'mod':'rw'},
232         'cmi.time_limit_action':{'defaultvalue':<?php echo !empty($userdata->timelimitaction)?'\''.$userdata->timelimitaction.'\'':'null' ?>, 'mod':'r'},
233         'cmi.total_time':{'defaultvalue':'<?php echo !empty($userdata->{'cmi.total_time'})?$userdata->{'cmi.total_time'}:'PT0H0M0S' ?>', 'mod':'r'},
234         'adl.nav.request':{'defaultvalue':'_none_', 'format':NAVEvent, 'mod':'rw'}
235     };
236     //
237     // Datamodel inizialization
238     //
239         var cmi = new Object();
240         cmi.comments_from_learner = new Object();
241         cmi.comments_from_learner._count = 0;
242         cmi.comments_from_lms = new Object();
243         cmi.comments_from_lms._count = 0;
244         cmi.interactions = new Object();
245         cmi.interactions._count = 0;
246         cmi.learner_preference = new Object();
247         cmi.objectives = new Object();
248         cmi.objectives._count = 0;
249         cmi.score = new Object();
251     // Navigation Object
252     var adl = new Object();
253         adl.nav = new Object();
254         adl.nav.request_valid = new Array();
256     for (element in datamodel) {
257         if (element.match(/\.n\./) == null) {
258             if ((typeof eval('datamodel["'+element+'"].defaultvalue')) != 'undefined') {
259                 eval(element+' = datamodel["'+element+'"].defaultvalue;');
260             } else {
261                 eval(element+' = "";');
262             }
263         }
264     }
266 <?php
267     // reconstitute objectives, comments_from_learner and comments_from_lms
268     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score'));
269     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.interactions', array('objectives', 'correct_responses'));
270     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.comments_from_learner', array());
271     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.comments_from_lms', array());
272 ?>
274     if (cmi.completion_status == '') {
275         cmi.completion_status = 'not attempted';
276     }
278     //
279     // API Methods definition
280     //
281     var Initialized = false;
282     var Terminated = false;
283     var diagnostic = "";
284     var errorCode = "0";
286     function Initialize (param) {
287         errorCode = "0";
288         if (param == "") {
289             if ((!Initialized) && (!Terminated)) {
290                 Initialized = true;
291                 errorCode = "0";
292                 <?php
293                     if (scorm_debugging($scorm)) {
294                         echo 'LogAPICall("Initialize", param, "", errorCode);';
295                     }
296                 ?>
297                 return "true";
298             } else {
299                 if (Initialized) {
300                     errorCode = "103";
301                 } else {
302                     errorCode = "104";
303                 }
304             }
305         } else {
306             errorCode = "201";
307         }
308         <?php
309             if (scorm_debugging($scorm)) {
310                 echo 'LogAPICall("Initialize", param, "", errorCode);';
311             }
312         ?>
313         return "false";
314     }
316     function Terminate (param) {
317         errorCode = "0";
318         if (param == "") {
319             if ((Initialized) && (!Terminated)) {
320                 var AJAXResult = StoreData(cmi,true);
321                 <?php
322                     if (scorm_debugging($scorm)) {
323                         echo 'LogAPICall("Terminate", "AJAXResult", AJAXResult, 0);';
324                     }
325                 ?>
326                 result = ('true' == AJAXResult) ? 'true' : 'false';
327                 errorCode = ('true' == result)? '0' : '101'; // General exception for any AJAX fault
328                 <?php
329                     if (scorm_debugging($scorm)) {
330                         echo 'LogAPICall("Terminate", "result", result, errorCode);';
331                     }
332                 ?>
333                 if ('true' == result) {
334                     Initialized = false;
335                     Terminated = true;
336                     if (adl.nav.request != '_none_') {
337                         switch (adl.nav.request) {
338                             case 'continue':
339                                 setTimeout('mod_scorm_launch_next_sco();',500);
340                             break;
341                             case 'previous':
342                                 setTimeout('mod_scorm_launch_prev_sco();',500);
343                             break;
344                             case 'choice':
345                             break;
346                             case 'exit':
347                             break;
348                             case 'exitAll':
349                             break;
350                             case 'abandon':
351                             break;
352                             case 'abandonAll':
353                             break;
354                         }
355                     } else {
356                         if (<?php echo $scorm->auto ?> == 1) {
357                             setTimeout('mod_scorm_launch_next_sco();',500);
358                         }
359                     }
360                     // trigger TOC update
361                     var sURL = "<?php echo $CFG->wwwroot; ?>" + "/mod/scorm/prereqs.php?a=<?php echo $scorm->id ?>&scoid=<?php echo $scoid ?>&attempt=<?php echo $attempt ?>&mode=<?php echo $mode ?>&currentorg=<?php echo $currentorg ?>&sesskey=<?php echo sesskey(); ?>";
362                     var callback = M.mod_scorm.connectPrereqCallback;
363                     YUI().use('io-base', function(Y) {
364                         Y.on('io:complete', callback.success, Y);
365                         Y.io(sURL);
366                     });
367                 } else {
368                     diagnostic = "Failure calling the Terminate remote callback: the server replied with HTTP Status " + AJAXResult;
369                 }
370                 return result;
371             } else {
372                 if (Terminated) {
373                     errorCode = "113";
374                 } else {
375                     errorCode = "112";
376                 }
377             }
378         } else {
379             errorCode = "201";
380         }
381         <?php
382             if (scorm_debugging($scorm)) {
383                 echo 'LogAPICall("Terminate", param, "", errorCode);';
384             }
385         ?>
386         return "false";
387     }
389     function GetValue (element) {
390         errorCode = "0";
391         diagnostic = "";
392         if ((Initialized) && (!Terminated)) {
393             if (element !="") {
394                 var expression = new RegExp(CMIIndex,'g');
395                 var elementmodel = String(element).replace(expression,'.n.');
396                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
397                     if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {
399                         element = String(element).replace(/\.(\d+)\./, ".N$1.");
400                         element = element.replace(/\.(\d+)\./, ".N$1.");
402                         var elementIndexes = element.split('.');
403                         var subelement = element.substr(0,3);
404                         var i = 1;
405                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
406                             subelement += '.'+elementIndexes[i++];
407                         }
409                         if (subelement == element) {
411                             if ((typeof eval(subelement) != "undefined") && (eval(subelement) != null)) {
412                                 errorCode = "0";
413                                 <?php
414                                     if (scorm_debugging($scorm)) {
415                                         echo 'LogAPICall("GetValue", element, eval(element), 0);';
416                                     }
417                                 ?>
418                                 return eval(element);
419                             } else {
420                                 errorCode = "403";
421                             }
422                         } else {
423                             errorCode = "301";
424                         }
425                     } else {
426                         //errorCode = eval('datamodel["'+elementmodel+'"].readerror');
427                         errorCode = "405";
428                     }
429                 } else {
430                     var childrenstr = '._children';
431                     var countstr = '._count';
432                     var parentmodel = '';
433                     if (elementmodel.substr(elementmodel.length-childrenstr.length,elementmodel.length) == childrenstr) {
434                         parentmodel = elementmodel.substr(0,elementmodel.length-childrenstr.length);
435                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
436                             errorCode = "301";
437                             diagnostic = "Data Model Element Does Not Have Children";
438                         } else {
439                             errorCode = "401";
440                         }
441                     } else if (elementmodel.substr(elementmodel.length-countstr.length,elementmodel.length) == countstr) {
442                         parentmodel = elementmodel.substr(0,elementmodel.length-countstr.length);
443                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
444                             errorCode = "301";
445                             diagnostic = "Data Model Element Cannot Have Count";
446                         } else {
447                             errorCode = "401";
448                         }
449                     } else {
450                         parentmodel = 'adl.nav.request_valid.';
451                         if (element.substr(0,parentmodel.length) == parentmodel) {
452                             if (element.substr(parentmodel.length).match(NAVTarget) == null) {
453                                 errorCode = "301";
454                             } else {
455                                 if (adl.nav.request == element.substr(parentmodel.length)) {
456                                     return "true";
457                                 } else if (adl.nav.request == '_none_') {
458                                     return "unknown";
459                                 } else {
460                                     return "false";
461                                 }
462                             }
463                         } else {
464                             errorCode = "401";
465                         }
466                     }
467                 }
468             } else {
469                 errorCode = "301";
470             }
471         } else {
472             if (Terminated) {
473                 errorCode = "123";
474             } else {
475                 errorCode = "122";
476             }
477         }
478         <?php
479             if (scorm_debugging($scorm)) {
480                 echo 'LogAPICall("GetValue", element, "", errorCode);';
481             }
482         ?>
483         return "";
484     }
486     function SetValue (element,value) {
487         errorCode = "0";
488         diagnostic = "";
489         if ((Initialized) && (!Terminated)) {
490             if (element != "") {
491                 var expression = new RegExp(CMIIndex,'g');
492                 var elementmodel = String(element).replace(expression,'.n.');
493                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
494                     if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
495                         if (eval('datamodel["'+elementmodel+'"].format') != 'CMIFeedback') {
496                             expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));
497                         } else {
498                             // cmi.interactions.n.type depending format accept everything at this stage
499                             expression = new RegExp(CMIFeedback);
500                         }
501                         value = value+'';
502                         var matches = value.match(expression);
503                         if ((matches != null) && ((matches.join('').length > 0) || (value.length == 0))) {
504                             // Value match dataelement format
506                             if (element != elementmodel) {
507                                 //This is a dynamic datamodel element
509                                 var elementIndexes = element.split('.');
510                                 var subelement = 'cmi';
511                                 var parentelement = 'cmi';
512                                 for (var i=1;(i < elementIndexes.length-1) && (errorCode=="0");i++) {
513                                     var elementIndex = elementIndexes[i];
514                                     if (elementIndexes[i+1].match(/^\d+$/)) {
515                                         if ((parseInt(elementIndexes[i+1]) > 0) && (elementIndexes[i+1].charAt(0) == 0)) {
516                                             // Index has a leading 0 (zero), this is not a number
517                                             errorCode = "351";
518                                         }
519                                         parentelement = subelement+'.'+elementIndex;
520                                         if ((typeof eval(parentelement) == "undefined") || (typeof eval(parentelement+'._count') == "undefined")) {
521                                             errorCode="408";
522                                         } else {
523                                             if (elementIndexes[i+1] > eval(parentelement+'._count')) {
524                                                 errorCode = "351";
525                                                 diagnostic = "Data Model Element Collection Set Out Of Order";
526                                             }
527                                             subelement = subelement.concat('.'+elementIndex+'.N'+elementIndexes[i+1]);
528                                             i++;
530                                             if (((typeof eval(subelement)) == "undefined") && (i < elementIndexes.length-2)) {
531                                                 errorCode="408";
532                                             }
533                                         }
534                                     } else {
535                                         subelement = subelement.concat('.'+elementIndex);
536                                     }
537                                 }
539                                 if (errorCode == "0") {
540                                     // Till now it's a real datamodel element
542                                     element = subelement.concat('.'+elementIndexes[elementIndexes.length-1]);
544                                     if ((typeof eval(subelement)) == "undefined") {
545                                         switch (elementmodel) {
546                                             case 'cmi.objectives.n.id':
547                                                 if (!duplicatedID(element,parentelement,value)) {
548                                                     if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
549                                                         eval(parentelement+'._count++;');
550                                                         eval(subelement+' = new Object();');
551                                                         var subobject = eval(subelement);
552                                                         subobject.success_status = datamodel["cmi.objectives.n.success_status"].defaultvalue;
553                                                         subobject.completion_status = datamodel["cmi.objectives.n.completion_status"].defaultvalue;
554                                                         subobject.progress_measure = datamodel["cmi.objectives.n.progress_measure"].defaultvalue;
555                                                         subobject.score = new Object();
556                                                         subobject.score._children = score_children;
557                                                         subobject.score.scaled = datamodel["cmi.objectives.n.score.scaled"].defaultvalue;
558                                                         subobject.score.raw = datamodel["cmi.objectives.n.score.raw"].defaultvalue;
559                                                         subobject.score.min = datamodel["cmi.objectives.n.score.min"].defaultvalue;
560                                                         subobject.score.max = datamodel["cmi.objectives.n.score.max"].defaultvalue;
561                                                     }
562                                                 } else {
563                                                     errorCode="351";
564                                                     diagnostic = "Data Model Element ID Already Exists";
565                                                 }
566                                             break;
567                                             case 'cmi.interactions.n.id':
568                                                 if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
569                                                     eval(parentelement+'._count++;');
570                                                     eval(subelement+' = new Object();');
571                                                     var subobject = eval(subelement);
572                                                     subobject.objectives = new Object();
573                                                     subobject.objectives._count = 0;
574                                                 }
575                                             break;
576                                             case 'cmi.interactions.n.objectives.n.id':
577                                                 if (typeof eval(parentelement) != "undefined") {
578                                                     if (!duplicatedID(element,parentelement,value)) {
579                                                         if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
580                                                             eval(parentelement+'._count++;');
581                                                             eval(subelement+' = new Object();');
582                                                         }
583                                                     } else {
584                                                         errorCode="351";
585                                                         diagnostic = "Data Model Element ID Already Exists";
586                                                     }
587                                                 } else {
588                                                     errorCode="408";
589                                                 }
590                                             break;
591                                             case 'cmi.interactions.n.correct_responses.n.pattern':
592                                                 if (typeof eval(parentelement) != "undefined") {
593                                                     // Use cmi.interactions.n.type value to check the right dataelement format
594                                                     if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
595                                                         var interactiontype = eval(String(parentelement).replace('correct_responses','type'));
596                                                         var interactioncount = eval(parentelement+'._count');
597                                                         // trap duplicate values, which is not allowed for type choice
598                                                         if (interactiontype == 'choice') {
599                                                             for (var i=0; (i < interactioncount) && (errorCode=="0"); i++) {
600                                                                if (eval(parentelement+'.N'+i+'.pattern') == value) {
601                                                                    errorCode = "351";
602                                                                }
603                                                             }
604                                                         }
605                                                         if ((typeof correct_responses[interactiontype].limit == 'undefined') ||
606                                                             (eval(parentelement+'._count') < correct_responses[interactiontype].limit)) {
607                                                             var nodes = new Array();
608                                                             if (correct_responses[interactiontype].delimiter != '') {
609                                                                 nodes = value.split(correct_responses[interactiontype].delimiter);
610                                                             } else {
611                                                                 nodes[0] = value;
612                                                             }
613                                                             if ((nodes.length > 0) && (nodes.length <= correct_responses[interactiontype].max)) {
614                                                                 errorCode = CRcheckValueNodes (element, interactiontype, nodes, value, errorCode);
615                                                             } else if (nodes.length > correct_responses[interactiontype].max) {
616                                                                 errorCode = "351";
617                                                                 diagnostic = "Data Model Element Pattern Too Long";
618                                                             }
619                                                             if ((errorCode == "0") && ((correct_responses[interactiontype].duplicate == false) ||
620                                                                (!duplicatedPA(element,parentelement,value))) || (errorCode == "0" && value == "")) {
621                                                                eval(parentelement+'._count++;');
622                                                                eval(subelement+' = new Object();');
623                                                             } else {
624                                                                 if (errorCode == "0") {
625                                                                     errorCode="351";
626                                                                     diagnostic = "Data Model Element Pattern Already Exists";
627                                                                 }
628                                                             }
629                                                         } else {
630                                                             errorCode="351";
631                                                             diagnostic = "Data Model Element Collection Limit Reached";
632                                                         }
633                                                     } else {
634                                                         errorCode="351";
635                                                         diagnostic = "Data Model Element Collection Set Out Of Order";
636                                                     }
637                                                 } else {
638                                                     errorCode="408";
639                                                 }
640                                             break;
641                                             default:
642                                                 if ((parentelement != 'cmi.objectives') && (parentelement != 'cmi.interactions') && (typeof eval(parentelement) != "undefined")) {
643                                                     if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
644                                                         eval(parentelement+'._count++;');
645                                                         eval(subelement+' = new Object();');
646                                                     } else {
647                                                         errorCode="351";
648                                                         diagnostic = "Data Model Element Collection Set Out Of Order";
649                                                     }
650                                                 } else {
651                                                     errorCode="408";
652                                                 }
653                                             break;
654                                         }
655                                     } else {
656                                         switch (elementmodel) {
657                                             case 'cmi.objectives.n.id':
658                                                 if (eval(element) != value) {
659                                                     errorCode = "351";
660                                                     diagnostic = "Write Once Violation";
661                                                 }
662                                             break;
663                                             case 'cmi.interactions.n.objectives.n.id':
664                                                 if (duplicatedID(element,parentelement,value)) {
665                                                     errorCode = "351";
666                                                     diagnostic = "Data Model Element ID Already Exists";
667                                                 }
668                                             break;
669                                             case 'cmi.interactions.n.type':
670                                                 var subobject = eval(subelement);
671                                                 subobject.correct_responses = new Object();
672                                                 subobject.correct_responses._count = 0;
673                                             break;
674                                             case 'cmi.interactions.n.learner_response':
675                                                 if (typeof eval(subelement+'.type') == "undefined") {
676                                                     errorCode="408";
677                                                 } else {
678                                                     // Use cmi.interactions.n.type value to check the right dataelement format
679                                                     interactiontype = eval(subelement+'.type');
680                                                     var nodes = new Array();
681                                                     if (learner_response[interactiontype].delimiter != '') {
682                                                         nodes = value.split(learner_response[interactiontype].delimiter);
683                                                     } else {
684                                                         nodes[0] = value;
685                                                     }
686                                                     if ((nodes.length > 0) && (nodes.length <= learner_response[interactiontype].max)) {
687                                                         expression = new RegExp(learner_response[interactiontype].format);
688                                                         for (var i=0; (i < nodes.length) && (errorCode=="0"); i++) {
689                                                             if (typeof learner_response[interactiontype].delimiter2 != 'undefined') {
690                                                                 values = nodes[i].split(learner_response[interactiontype].delimiter2);
691                                                                 if (values.length == 2) {
692                                                                     matches = values[0].match(expression);
693                                                                     if (matches == null) {
694                                                                         errorCode = "406";
695                                                                     } else {
696                                                                         var expression2 = new RegExp(learner_response[interactiontype].format2);
697                                                                         matches = values[1].match(expression2);
698                                                                         if (matches == null) {
699                                                                             errorCode = "406";
700                                                                         }
701                                                                     }
702                                                                 } else {
703                                                                     errorCode = "406";
704                                                                 }
705                                                             } else {
706                                                                 matches = nodes[i].match(expression);
707                                                                 if (matches == null) {
708                                                                     errorCode = "406";
709                                                                 } else {
710                                                                     if ((nodes[i] != '') && (learner_response[interactiontype].unique)) {
711                                                                         for (var j=0; (j<i) && (errorCode=="0"); j++) {
712                                                                             if (nodes[i] == nodes[j]) {
713                                                                                 errorCode = "406";
714                                                                             }
715                                                                         }
716                                                                     }
717                                                                 }
718                                                             }
719                                                         }
720                                                     } else if (nodes.length > learner_response[interactiontype].max) {
721                                                         errorCode = "351";
722                                                         diagnostic = "Data Model Element Pattern Too Long";
723                                                     }
724                                                 }
725                                              break;
726                                          case 'cmi.interactions.n.correct_responses.n.pattern':
727                                                  subel= subelement.split('.');
728                                              subel1= 'cmi.interactions.'+subel[2];
730                                                 if (typeof eval(subel1+'.type') == "undefined") {
731                                                     errorCode="408";
732                                                 } else {
733                                                     // Use cmi.interactions.n.type value to check the right //dataelement format
734                                                     var interactiontype = eval(subel1+'.type');
735                                                     var interactioncount = eval(parentelement+'._count');
736                                                     // trap duplicate values, which is not allowed for type choice
737                                                     if (interactiontype == 'choice') {
738                                                         for (var i=0; (i < interactioncount) && (errorCode=="0"); i++) {
739                                                            if (eval(parentelement+'.N'+i+'.pattern') == value) {
740                                                                errorCode = "351";
741                                                            }
742                                                         }
743                                                     }
744                                                     var nodes = new Array();
745                                                     if (correct_responses[interactiontype].delimiter != '') {
746                                                         nodes = value.split(correct_responses[interactiontype].delimiter);
747                                                     } else {
748                                                         nodes[0] = value;
749                                                     }
751                                                     if ((nodes.length > 0) && (nodes.length <= correct_responses[interactiontype].max)) {
752                                                         errorCode = CRcheckValueNodes (element, interactiontype, nodes, value, errorCode);
753                                                     } else if (nodes.length > correct_responses[interactiontype].max) {
754                                                         errorCode = "351";
755                                                         diagnostic = "Data Model Element Pattern Too Long";
756                                                     }
757                                                 }
758                                              break;
759                                         }
760                                     }
761                                 }
762                             }
763                             //Store data
764                             if (errorCode == "0") {
766                                 if ((typeof eval('datamodel["'+elementmodel+'"].range')) != "undefined") {
767                                     range = eval('datamodel["'+elementmodel+'"].range');
768                                     ranges = range.split('#');
769                                     value = value*1.0;
770                                     if (value >= ranges[0]) {
771                                         if ((ranges[1] == '*') || (value <= ranges[1])) {
772                                             eval(element+'=value;');
773                                             errorCode = "0";
774                                             <?php
775                                                 if (scorm_debugging($scorm)) {
776                                                     echo 'LogAPICall("SetValue", element, value, errorCode);';
777                                                 }
778                                             ?>
779                                             return "true";
780                                         } else {
781                                             errorCode = '407';
782                                         }
783                                     } else {
784                                         errorCode = '407';
785                                     }
786                                 } else {
787                                     eval(element+'=value;');
788                                     errorCode = "0";
789                                     <?php
790                                         if (scorm_debugging($scorm)) {
791                                             echo 'LogAPICall("SetValue", element, value, errorCode);';
792                                         }
793                                     ?>
794                                     return "true";
795                                 }
796                             }
797                         } else {
798                             errorCode = "406";
799                         }
800                     } else {
801                         errorCode = "404";
802                     }
803                 } else {
804                     errorCode = "401"
805                 }
806             } else {
807                 errorCode = "351";
808             }
809         } else {
810             if (Terminated) {
811                 errorCode = "133";
812             } else {
813                 errorCode = "132";
814             }
815         }
816         <?php
817             if (scorm_debugging($scorm)) {
818                 echo 'LogAPICall("SetValue", element, value, errorCode);';
819             }
820         ?>
821         return "false";
822     }
825     function CRremovePrefixes (node) {
826         // check for prefixes lang, case, order
827         // case and then order
828         var seenOrder = false;
829         var seenCase = false;
830         var seenLang = false;
831         var errorCode = "0";
832         while (matches = node.match('^(\{(lang|case_matters|order_matters)=([^\}]+)\})')) {
833             switch (matches[2]) {
834                 case 'lang':
835                     // check for language prefix on each node
836                     langmatches = node.match(CMILangcr);
837                     if (langmatches != null) {
838                         lang = langmatches[3];
839                         // check that language string definition is valid
840                         if (lang.length > 0 && lang != undefined) {
841                             if (validLanguages[lang.toLowerCase()] == undefined) {
842                                 errorCode = "406";
843                             }
844                         }
845                     }
846                     seenLang = true;
847                 break;
849                 case 'case_matters':
850                     // check for correct case answer
851                     if (! seenLang && ! seenOrder && ! seenCase) {
852                         if (matches[3] != 'true' && matches[3] != 'false') {
853                             errorCode = "406";
854                         }
855                     }
856                     seenCase = true;
857                 break;
859                 case 'order_matters':
860                     // check for correct case answer
861                     if (! seenCase && ! seenLang && ! seenOrder) {
862                         if (matches[3] != 'true' && matches[3] != 'false') {
863                             errorCode = "406";
864                         }
865                     }
866                     seenOrder = true;
867                 break;
869                 default:
870                 break;
871             }
872             node = node.substr(matches[1].length);
873         }
874         return {'errorCode': errorCode, 'node': node};
875     }
878     function CRcheckValueNodes(element, interactiontype, nodes, value, errorCode) {
879         expression = new RegExp(correct_responses[interactiontype].format);
880         for (var i=0; (i < nodes.length) && (errorCode=="0"); i++) {
881             if (interactiontype.match('^(fill-in|long-fill-in|matching|performance|sequencing)$')) {
882                 result = CRremovePrefixes(nodes[i]);
883                 errorCode = result.errorCode;
884                 nodes[i] = result.node;
885             }
887             // check for prefix on each node
888             if (correct_responses[interactiontype].pre != '') {
889                 matches = nodes[i].match(correct_responses[interactiontype].pre);
890                 if (matches != null) {
891                     nodes[i] = nodes[i].substr(matches[1].length);
892                 }
893             }
895             if (correct_responses[interactiontype].delimiter2 != undefined) {
896                 values = nodes[i].split(correct_responses[interactiontype].delimiter2);
897                 if (values.length == 2) {
898                     matches = values[0].match(expression);
899                     if (matches == null) {
900                         errorCode = "406";
901                     } else {
902                         var expression2 = new RegExp(correct_responses[interactiontype].format2);
903                         matches = values[1].match(expression2);
904                         if (matches == null) {
905                             errorCode = "406";
906                         }
907                     }
908                 } else {
909                      errorCode = "406";
910                 }
911             } else {
912                 matches = nodes[i].match(expression);
913                 //if ((matches == null) || (matches.join('').length == 0)) {
914                 if ((matches == null && value != "")||(matches == null && interactiontype=="true-false")){
915                     errorCode = "406";
916                 } else {
917                     // numeric range - left must be <= right
918                     if (interactiontype == 'numeric' && nodes.length > 1) {
919                         if (parseFloat(nodes[0]) > parseFloat(nodes[1])) {
920                             errorCode = "406";
921                         }
922                     } else {
923                         if ((nodes[i] != '') && (correct_responses[interactiontype].unique)) {
924                             for (var j=0; (j < i) && (errorCode=="0"); j++) {
925                                 if (nodes[i] == nodes[j]) {
926                                     errorCode = "406";
927                                 }
928                             }
929                         }
930                     }
931                 }
932             }
933         } // end of for each nodes
934         return errorCode;
935     }
938     function Commit (param) {
939         errorCode = "0";
940         if (param == "") {
941             if ((Initialized) && (!Terminated)) {
942                 var AJAXResult = StoreData(cmi,false);
943                 <?php
944                     if (scorm_debugging($scorm)) {
945                         echo 'LogAPICall("Commit", "AJAXResult", AJAXResult, 0);';
946                     }
947                 ?>
948                 var result = ('true' == AJAXResult) ? 'true' : 'false';
949                 errorCode = ('true' == result)? '0' : '101'; // General exception for any AJAX fault
950                 <?php
951                     if (scorm_debugging($scorm)) {
952                         echo 'LogAPICall("Commit", "result", result, errorCode);';
953                     }
954                 ?>
955                 if ('false' == result) {
956                     diagnostic = "Failure calling the Commit remote callback: the server replied with HTTP Status " + AJAXResult;
957                 }
958                 return result;
959             } else {
960                 if (Terminated) {
961                     errorCode = "143";
962                 } else {
963                     errorCode = "142";
964                 }
965             }
966         } else {
967             errorCode = "201";
968         }
969         <?php
970             if (scorm_debugging($scorm)) {
971                 echo 'LogAPICall("Commit", param, "", errorCode);';
972             }
973         ?>
974         return "false";
975     }
977     function GetLastError () {
978     <?php
979         if (scorm_debugging($scorm)) {
980             echo 'LogAPICall("GetLastError", "", "", errorCode);';
981         }
982     ?>
983         return errorCode;
984     }
986     function GetErrorString (param) {
987         if (param != "") {
988             var errorString = "";
989             switch(param) {
990                 case "0":
991                     errorString = "No error";
992                 break;
993                 case "101":
994                     errorString = "General exception";
995                 break;
996                 case "102":
997                     errorString = "General Inizialization Failure";
998                 break;
999                 case "103":
1000                     errorString = "Already Initialized";
1001                 break;
1002                 case "104":
1003                     errorString = "Content Instance Terminated";
1004                 break;
1005                 case "111":
1006                     errorString = "General Termination Failure";
1007                 break;
1008                 case "112":
1009                     errorString = "Termination Before Inizialization";
1010                 break;
1011                 case "113":
1012                     errorString = "Termination After Termination";
1013                 break;
1014                 case "122":
1015                     errorString = "Retrieve Data Before Initialization";
1016                 break;
1017                 case "123":
1018                     errorString = "Retrieve Data After Termination";
1019                 break;
1020                 case "132":
1021                     errorString = "Store Data Before Inizialization";
1022                 break;
1023                 case "133":
1024                     errorString = "Store Data After Termination";
1025                 break;
1026                 case "142":
1027                     errorString = "Commit Before Inizialization";
1028                 break;
1029                 case "143":
1030                     errorString = "Commit After Termination";
1031                 break;
1032                 case "201":
1033                     errorString = "General Argument Error";
1034                 break;
1035                 case "301":
1036                     errorString = "General Get Failure";
1037                 break;
1038                 case "351":
1039                     errorString = "General Set Failure";
1040                 break;
1041                 case "391":
1042                     errorString = "General Commit Failure";
1043                 break;
1044                 case "401":
1045                     errorString = "Undefinited Data Model";
1046                 break;
1047                 case "402":
1048                     errorString = "Unimplemented Data Model Element";
1049                 break;
1050                 case "403":
1051                     errorString = "Data Model Element Value Not Initialized";
1052                 break;
1053                 case "404":
1054                     errorString = "Data Model Element Is Read Only";
1055                 break;
1056                 case "405":
1057                     errorString = "Data Model Element Is Write Only";
1058                 break;
1059                 case "406":
1060                     errorString = "Data Model Element Type Mismatch";
1061                 break;
1062                 case "407":
1063                     errorString = "Data Model Element Value Out Of Range";
1064                 break;
1065                 case "408":
1066                     errorString = "Data Model Dependency Not Established";
1067                 break;
1068             }
1069             <?php
1070             if (scorm_debugging($scorm)) {
1071                 echo 'LogAPICall("GetErrorString", param,  errorString, 0);';
1072             }
1073              ?>
1074             return errorString;
1075         } else {
1076            <?php
1077             if (scorm_debugging($scorm)) {
1078                 echo 'LogAPICall("GetErrorString", param,  "No error string found!", 0);';
1079             }
1080              ?>
1081             return "";
1082         }
1083     }
1085     function GetDiagnostic (param) {
1086         if (diagnostic != "") {
1087             <?php
1088                 if (scorm_debugging($scorm)) {
1089                     echo 'LogAPICall("GetDiagnostic", param, diagnostic, 0);';
1090                 }
1091             ?>
1092             return diagnostic;
1093         }
1094         <?php
1095             if (scorm_debugging($scorm)) {
1096                 echo 'LogAPICall("GetDiagnostic", param, param, 0);';
1097             }
1098         ?>
1099         return param;
1100     }
1102     function duplicatedID (element, parent, value) {
1103         var found = false;
1104         var elements = eval(parent+'._count');
1105         for (var n=0;(n < elements) && (!found);n++) {
1106             if ((parent+'.N'+n+'.id' != element) && (eval(parent+'.N'+n+'.id') == value)) {
1107                 found = true;
1108             }
1109         }
1110         return found;
1111     }
1113     function duplicatedPA (element, parent, value) {
1114         var found = false;
1115         var elements = eval(parent+'._count');
1116         for (var n=0;(n < elements) && (!found);n++) {
1117             if ((parent+'.N'+n+'.pattern' != element) && (eval(parent+'.N'+n+'.pattern') == value)) {
1118                 found = true;
1119             }
1120         }
1121         return found;
1122     }
1124     function getElementModel(element) {
1125         if (typeof datamodel[element] != "undefined") {
1126             return element;
1127         } else {
1128             var expression = new RegExp(CMIIndex,'g');
1129             var elementmodel = String(element).replace(expression,'.n.');
1130             if (typeof datamodel[elementmodel] != "undefined") {
1131                 return elementmodel;
1132             }
1133         }
1134         return false;
1135     }
1137     function AddTime (first, second) {
1138         <?php
1139 //            if (scorm_debugging($scorm)) {
1140 //                echo 'alert("AddTime: "+first+" + "+second);';
1141 //            }
1142         ?>
1143         var timestring = 'P';
1144         var matchexpr = /^P((\d+)Y)?((\d+)M)?((\d+)D)?(T((\d+)H)?((\d+)M)?((\d+(\.\d{1,2})?)S)?)?$/;
1145         var firstarray = first.match(matchexpr);
1146         var secondarray = second.match(matchexpr);
1147         if ((firstarray != null) && (secondarray != null)) {
1148             var firstsecs=0;
1149             if(parseFloat(firstarray[13],10)>0){ firstsecs=parseFloat(firstarray[13],10); }
1150             var secondsecs=0;
1151             if(parseFloat(secondarray[13],10)>0){ secondsecs=parseFloat(secondarray[13],10); }
1152             var secs = firstsecs+secondsecs;  //Seconds
1153             var change = Math.floor(secs/60);
1154             secs = Math.round((secs-(change*60))*100)/100;
1155             var firstmins=0;
1156             if(parseInt(firstarray[11],10)>0){ firstmins=parseInt(firstarray[11],10); }
1157             var secondmins=0;
1158             if(parseInt(secondarray[11],10)>0){ secondmins=parseInt(secondarray[11],10); }
1159             var mins = firstmins+secondmins+change;   //Minutes
1160             change = Math.floor(mins / 60);
1161             mins = Math.round(mins-(change*60));
1162             var firsthours=0;
1163             if(parseInt(firstarray[9],10)>0){ firsthours=parseInt(firstarray[9],10); }
1164             var secondhours=0;
1165             if(parseInt(secondarray[9],10)>0){ secondhours=parseInt(secondarray[9],10); }
1166             var hours = firsthours+secondhours+change; //Hours
1167             change = Math.floor(hours/24);
1168             hours = Math.round(hours-(change*24));
1169             var firstdays=0;
1170             if(parseInt(firstarray[6],10)>0){ firstdays=parseInt(firstarray[6],10); }
1171             var seconddays=0;
1172             if(parseInt(secondarray[6],10)>0){ firstdays=parseInt(secondarray[6],10); }
1173             var days = Math.round(firstdays+seconddays+change); // Days
1174             var firstmonths=0;
1175             if(parseInt(firstarray[4],10)>0){ firstmonths=parseInt(firstarray[4],10); }
1176             var secondmonths=0;
1177             if(parseInt(secondarray[4],10)>0){ secondmonths=parseInt(secondarray[4],10); }
1178             var months = Math.round(firstmonths+secondmonths);
1179             var firstyears=0;
1180             if(parseInt(firstarray[2],10)>0){ firstyears=parseInt(firstarray[2],10); }
1181             var secondyears=0;
1182             if(parseInt(secondarray[2],10)>0){ secondyears=parseInt(secondarray[2],10); }
1183             var years = Math.round(firstyears+secondyears);
1184         }
1185         if (years > 0) {
1186             timestring += years + 'Y';
1187         }
1188         if (months > 0) {
1189             timestring += months + 'M';
1190         }
1191         if (days > 0) {
1192             timestring += days + 'D';
1193         }
1194         if ((hours > 0) || (mins > 0) || (secs > 0)) {
1195             timestring += 'T';
1196             if (hours > 0) {
1197                 timestring += hours + 'H';
1198             }
1199             if (mins > 0) {
1200                 timestring += mins + 'M';
1201             }
1202             if (secs > 0) {
1203                 timestring += secs + 'S';
1204             }
1205         }
1206         return timestring;
1207     }
1209     function TotalTime() {
1210         var total_time = AddTime(cmi.total_time, cmi.session_time);
1211         return '&'+underscore('cmi.total_time')+'='+encodeURIComponent(total_time);
1212     }
1214     function CollectData(data,parent) {
1215         var datastring = '';
1216         for (property in data) {
1217             if (typeof data[property] == 'object') {
1218                 datastring += CollectData(data[property],parent+'.'+property);
1219             } else {
1220                 var element = parent+'.'+property;
1221                 var expression = new RegExp(CMIIndexStore,'g');
1222                 var elementmodel = String(element).replace(expression,'.n.');
1223                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
1224                     if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
1225                         var elementstring = '&'+underscore(element)+'='+encodeURIComponent(data[property]);
1226                         if ((typeof eval('datamodel["'+elementmodel+'"].defaultvalue')) != "undefined") {
1227                             if (eval('datamodel["'+elementmodel+'"].defaultvalue') != data[property] || eval('typeof(datamodel["'+elementmodel+'"].defaultvalue)') != typeof(data[property])) {
1228                                 datastring += elementstring;
1229                             }
1230                         } else {
1231                             datastring += elementstring;
1232                         }
1233                     }
1234                 }
1235             }
1236         }
1237         return datastring;
1238     }
1240     function StoreData(data,storetotaltime) {
1241         var datastring = '';
1242         if (storetotaltime) {
1243             if (cmi.mode == 'normal') {
1244                 if (cmi.credit == 'credit') {
1245                     if ((cmi.completion_threshold != null) && (cmi.progress_measure != null)) {
1246                         if (cmi.progress_measure >= cmi.completion_threshold) {
1247                             cmi.completion_status = 'completed';
1248                         } else {
1249                             cmi.completion_status = 'incomplete';
1250                         }
1251                     }
1252                     if ((cmi.scaled_passed_score != null) && (cmi.score.scaled != '')) {
1253                         if (cmi.score.scaled >= cmi.scaled_passed_score) {
1254                             cmi.success_status = 'passed';
1255                         } else {
1256                             cmi.success_status = 'failed';
1257                         }
1258                     }
1259                 }
1260             }
1261             datastring += TotalTime();
1262         }
1263         datastring += CollectData(data,'cmi');
1264         var element = 'adl.nav.request';
1265         var navrequest = eval(element) != datamodel[element].defaultvalue ? '&'+underscore(element)+'='+encodeURIComponent(eval(element)) : '';
1266         datastring += navrequest;
1267         datastring += '&attempt=<?php echo $attempt ?>';
1268         datastring += '&scoid=<?php echo $scoid ?>';
1269         var myRequest = NewHttpReq();
1270         var result = DoRequest(myRequest,"<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php","id=<?php p($id) ?>&a=<?php p($a) ?>&sesskey=<?php echo sesskey() ?>"+datastring);
1271         var results = String(result).split('\n');
1272         if ((results.length > 2) && (navrequest != '')) {
1273             eval(results[2]);
1274         }
1275         errorCode = results[1];
1276         return results[0];
1277     }
1279     this.Initialize = Initialize;
1280     this.Terminate = Terminate;
1281     this.GetValue = GetValue;
1282     this.SetValue = SetValue;
1283     this.Commit = Commit;
1284     this.GetLastError = GetLastError;
1285     this.GetErrorString = GetErrorString;
1286     this.GetDiagnostic = GetDiagnostic;
1287     this.version = '1.0';
1290 var API_1484_11 = new SCORMapi1_3();
1292 <?php
1293 // pull in the debugging utilities
1294 if (scorm_debugging($scorm)) {
1295     include_once($CFG->dirroot.'/mod/scorm/datamodels/debug.js.php');
1296     echo 'AppendToLog("Moodle SCORM 1.3 API Loaded, Activity: '.$scorm->name.', SCO: '.$sco->identifier.'", 0);';