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