Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / scorm / datamodels / scormlib.php
blob8d29f02fa79263ff91dd87d2ec6e5d9dbccc0dbd
1 <?php
2 function scorm_get_resources($blocks) {
3 $resources = array();
4 foreach ($blocks as $block) {
5 if ($block['name'] == 'RESOURCES') {
6 foreach ($block['children'] as $resource) {
7 if ($resource['name'] == 'RESOURCE') {
8 $resources[addslashes($resource['attrs']['IDENTIFIER'])] = $resource['attrs'];
13 return $resources;
16 function scorm_get_manifest($blocks,$scoes) {
17 static $parents = array();
18 static $resources;
20 static $manifest;
21 static $organization;
23 if (count($blocks) > 0) {
24 foreach ($blocks as $block) {
25 switch ($block['name']) {
26 case 'METADATA':
27 if (isset($block['children'])) {
28 foreach ($block['children'] as $metadata) {
29 if ($metadata['name'] == 'SCHEMAVERSION') {
30 if (empty($scoes->version)) {
31 if (isset($metadata['tagData']) && (preg_match("/^(1\.2)$|^(CAM )?(1\.3)$/",$metadata['tagData'],$matches))) {
32 $scoes->version = 'SCORM_'.$matches[count($matches)-1];
33 } else {
34 if (isset($metadata['tagData']) && (preg_match("/^2004 3rd Edition$/",$metadata['tagData'],$matches))) {
35 $scoes->version = 'SCORM_1.3';
36 } else {
37 $scoes->version = 'SCORM_1.2';
44 break;
45 case 'MANIFEST':
46 $manifest = addslashes($block['attrs']['IDENTIFIER']);
47 $organization = '';
48 $resources = array();
49 $resources = scorm_get_resources($block['children']);
50 $scoes = scorm_get_manifest($block['children'],$scoes);
51 if (count($scoes->elements) <= 0) {
52 foreach ($resources as $item => $resource) {
53 if (!empty($resource['HREF'])) {
54 $sco = new stdClass();
55 $sco->identifier = $item;
56 $sco->title = $item;
57 $sco->parent = '/';
58 $sco->launch = addslashes($resource['HREF']);
59 $sco->scormtype = addslashes($resource['ADLCP:SCORMTYPE']);
60 $scoes->elements[$manifest][$organization][$item] = $sco;
64 break;
65 case 'ORGANIZATIONS':
66 if (!isset($scoes->defaultorg) && isset($block['attrs']['DEFAULT'])) {
67 $scoes->defaultorg = addslashes($block['attrs']['DEFAULT']);
69 $scoes = scorm_get_manifest($block['children'],$scoes);
70 break;
71 case 'ORGANIZATION':
72 $identifier = addslashes($block['attrs']['IDENTIFIER']);
73 $organization = '';
74 $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
75 $scoes->elements[$manifest][$organization][$identifier]->parent = '/';
76 $scoes->elements[$manifest][$organization][$identifier]->launch = '';
77 $scoes->elements[$manifest][$organization][$identifier]->scormtype = '';
79 $parents = array();
80 $parent = new stdClass();
81 $parent->identifier = $identifier;
82 $parent->organization = $organization;
83 array_push($parents, $parent);
84 $organization = $identifier;
86 $scoes = scorm_get_manifest($block['children'],$scoes);
88 array_pop($parents);
89 break;
90 case 'ITEM':
91 $parent = array_pop($parents);
92 array_push($parents, $parent);
94 $identifier = addslashes($block['attrs']['IDENTIFIER']);
95 $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
96 $scoes->elements[$manifest][$organization][$identifier]->parent = $parent->identifier;
97 if (!isset($block['attrs']['ISVISIBLE'])) {
98 $block['attrs']['ISVISIBLE'] = 'true';
100 $scoes->elements[$manifest][$organization][$identifier]->isvisible = addslashes($block['attrs']['ISVISIBLE']);
101 if (!isset($block['attrs']['PARAMETERS'])) {
102 $block['attrs']['PARAMETERS'] = '';
104 $scoes->elements[$manifest][$organization][$identifier]->parameters = addslashes($block['attrs']['PARAMETERS']);
105 if (!isset($block['attrs']['IDENTIFIERREF'])) {
106 $scoes->elements[$manifest][$organization][$identifier]->launch = '';
107 $scoes->elements[$manifest][$organization][$identifier]->scormtype = 'asset';
108 } else {
109 $idref = addslashes($block['attrs']['IDENTIFIERREF']);
110 $base = '';
111 if (isset($resources[$idref]['XML:BASE'])) {
112 $base = $resources[$idref]['XML:BASE'];
114 $scoes->elements[$manifest][$organization][$identifier]->launch = addslashes($base.$resources[$idref]['HREF']);
115 if (empty($resources[$idref]['ADLCP:SCORMTYPE'])) {
116 $resources[$idref]['ADLCP:SCORMTYPE'] = 'asset';
118 $scoes->elements[$manifest][$organization][$identifier]->scormtype = addslashes($resources[$idref]['ADLCP:SCORMTYPE']);
121 $parent = new stdClass();
122 $parent->identifier = $identifier;
123 $parent->organization = $organization;
124 array_push($parents, $parent);
126 $scoes = scorm_get_manifest($block['children'],$scoes);
128 array_pop($parents);
129 break;
130 case 'TITLE':
131 $parent = array_pop($parents);
132 array_push($parents, $parent);
133 if (!isset($block['tagData'])) {
134 $block['tagData'] = '';
136 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->title = addslashes($block['tagData']);
137 break;
138 case 'ADLCP:PREREQUISITES':
139 if ($block['attrs']['TYPE'] == 'aicc_script') {
140 $parent = array_pop($parents);
141 array_push($parents, $parent);
142 if (!isset($block['tagData'])) {
143 $block['tagData'] = '';
145 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->prerequisites = addslashes($block['tagData']);
147 break;
148 case 'ADLCP:MAXTIMEALLOWED':
149 $parent = array_pop($parents);
150 array_push($parents, $parent);
151 if (!isset($block['tagData'])) {
152 $block['tagData'] = '';
154 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->maxtimeallowed = addslashes($block['tagData']);
155 break;
156 case 'ADLCP:TIMELIMITACTION':
157 $parent = array_pop($parents);
158 array_push($parents, $parent);
159 if (!isset($block['tagData'])) {
160 $block['tagData'] = '';
162 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->timelimitaction = addslashes($block['tagData']);
163 break;
164 case 'ADLCP:DATAFROMLMS':
165 $parent = array_pop($parents);
166 array_push($parents, $parent);
167 if (!isset($block['tagData'])) {
168 $block['tagData'] = '';
170 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->datafromlms = addslashes($block['tagData']);
171 break;
172 case 'ADLCP:MASTERYSCORE':
173 $parent = array_pop($parents);
174 array_push($parents, $parent);
175 if (!isset($block['tagData'])) {
176 $block['tagData'] = '';
178 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->masteryscore = addslashes($block['tagData']);
179 break;
180 case 'ADLCP:COMPLETIONTHRESHOLD':
181 $parent = array_pop($parents);
182 array_push($parents, $parent);
183 if (!isset($block['tagData'])) {
184 $block['tagData'] = '';
186 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->threshold = addslashes($block['tagData']);
187 break;
188 case 'ADLNAV:PRESENTATION':
189 $parent = array_pop($parents);
190 array_push($parents, $parent);
191 if (!empty($block['children'])) {
192 foreach ($block['children'] as $adlnav) {
193 if ($adlnav['name'] == 'ADLNAV:NAVIGATIONINTERFACE') {
194 foreach ($adlnav['children'] as $adlnavInterface) {
195 if ($adlnavInterface['name'] == 'ADLNAV:HIDELMSUI') {
196 if ($adlnavInterface['tagData'] == 'continue') {
197 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidecontinue = 1;
199 if ($adlnavInterface['tagData'] == 'previous') {
200 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideprevious = 1;
202 if ($adlnavInterface['tagData'] == 'exit') {
203 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexit = 1;
205 if ($adlnavInterface['tagData'] == 'exitAll') {
206 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexitall = 1;
208 if ($adlnavInterface['tagData'] == 'abandon') {
209 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandon = 1;
211 if ($adlnavInterface['tagData'] == 'abandonAll') {
212 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandonall = 1;
214 if ($adlnavInterface['tagData'] == 'suspendAll') {
215 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidesuspendall = 1;
222 break;
223 case 'IMSSS:SEQUENCING':
224 $parent = array_pop($parents);
225 array_push($parents, $parent);
226 if (!empty($block['children'])) {
227 foreach ($block['children'] as $sequencing) {
228 if ($sequencing['name']=='IMSSS:CONTROLMODE') {
229 if (isset($sequencing['attrs']['CHOICE'])) {
230 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choice = $sequencing['attrs']['CHOICE'] == 'true'?1:0;
232 if (isset($sequencing['attrs']['CHOICEEXIT'])) {
233 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choiceexit = $sequencing['attrs']['CHOICEEXIT'] == 'true'?1:0;
235 if (isset($sequencing['attrs']['FLOW'])) {
236 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->flow = $sequencing['attrs']['FLOW'] == 'true'?1:0;
238 if (isset($sequencing['attrs']['FORWARDONLY'])) {
239 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->forwardonly = $sequencing['attrs']['FORWARDONLY'] == 'true'?1:0;
241 if (isset($sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'])) {
242 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptobjectinfo = $sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'] == 'true'?1:0;
244 if (isset($sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'])) {
245 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptprogressinfo = $sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'] == 'true'?1:0;
248 if ($sequencing['name']=='ADLSEQ:CONSTRAINEDCHOICECONSIDERATIONS') {
249 if (isset($sequencing['attrs']['CONSTRAINCHOICE'])) {
250 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->constrainChoice = $sequencing['attrs']['CONSTRAINCHOICE'] == 'true'?1:0;
252 if (isset($sequencing['attrs']['PREVENTACTIVATION'])) {
253 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->preventactivation = $sequencing['attrs']['PREVENTACTIVATION'] == 'true'?1:0;
256 if ($sequencing['name']=='IMSSS:OBJECTIVES') {
257 $objectives = array();
258 foreach ($sequencing['children'] as $objective) {
259 $objectivedata = new stdClass();
260 $objectivedata->primaryobj = 0;
261 switch ($objective['name']) {
262 case 'IMSSS:PRIMARYOBJECTIVE':
263 $objectivedata->primaryobj = 1;
264 case 'IMSSS:OBJECTIVE':
265 $objectivedata->satisfiedbymeasure = 0;
266 if (isset($objective['attrs']['SATISFIEDBYMEASURE'])) {
267 $objectivedata->satisfiedbymeasure = $objective['attrs']['SATISFIEDBYMEASURE']== 'true'?1:0;
269 $objectivedata->objectiveid = '';
270 if (isset($objective['attrs']['OBJECTIVEID'])) {
271 $objectivedata->objectiveid = $objective['attrs']['OBJECTIVEID'];
273 $objectivedata->minnormalizedmeasure = 1.0;
274 if (!empty($objective['children'])) {
275 $mapinfos = array();
276 foreach ($objective['children'] as $objectiveparam) {
277 if ($objectiveparam['name']=='IMSSS:MINNORMALIZEDMEASURE') {
278 if (isset($objectiveparam['tagData'])) {
279 $objectivedata->minnormalizedmeasure = $objectiveparam['tagData'];
280 } else {
281 $objectivedata->minnormalizedmeasure = 0;
284 if ($objectiveparam['name']=='IMSSS:MAPINFO') {
285 $mapinfo = new stdClass();
286 $mapinfo->targetobjectiveid = '';
287 if (isset($objectiveparam['attrs']['TARGETOBJECTIVEID'])) {
288 $mapinfo->targetobjectiveid = $objectiveparam['attrs']['TARGETOBJECTIVEID'];
290 $mapinfo->readsatisfiedstatus = 1;
291 if (isset($objectiveparam['attrs']['READSATISFIEDSTATUS'])) {
292 $mapinfo->readsatisfiedstatus = $objectiveparam['attrs']['READSATISFIEDSTATUS'] == 'true'?1:0;
294 $mapinfo->writesatisfiedstatus = 0;
295 if (isset($objectiveparam['attrs']['WRITESATISFIEDSTATUS'])) {
296 $mapinfo->writesatisfiedstatus = $objectiveparam['attrs']['WRITESATISFIEDSTATUS'] == 'true'?1:0;
298 $mapinfo->readnormalizemeasure = 1;
299 if (isset($objectiveparam['attrs']['READNORMALIZEDMEASURE'])) {
300 $mapinfo->readnormalizemeasure = $objectiveparam['attrs']['READNORMALIZEDMEASURE'] == 'true'?1:0;
302 $mapinfo->writenormalizemeasure = 0;
303 if (isset($objectiveparam['attrs']['WRITENORMALIZEDMEASURE'])) {
304 $mapinfo->writenormalizemeasure = $objectiveparam['attrs']['WRITENORMALIZEDMEASURE'] == 'true'?1:0;
306 array_push($mapinfos,$mapinfo);
309 if (!empty($mapinfos)) {
310 $objectivesdata->mapinfos = $mapinfos;
313 break;
315 //print_object($objectivedata);
316 array_push($objectives,$objectivedata);
318 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectives = $objectives;
320 if ($sequencing['name']=='IMSSS:LIMITCONDITIONS') {
321 if (isset($sequencing['attrs']['ATTEMPTLIMIT'])) {
322 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptLimit = $sequencing['attrs']['ATTEMPTLIMIT'];
324 if (isset($sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'])) {
325 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptAbsoluteDurationLimit = $sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'];
328 if ($sequencing['name']=='IMSSS:ROLLUPRULES') {
329 if (isset($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'])) {
330 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupobjectivesatisfied = $sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'] == 'true'?1:0;;
332 if (isset($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'])) {
333 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupprogresscompletion = $sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'] == 'true'?1:0;
335 if (isset($sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'])) {
336 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivemeasureweight = $sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'];
339 if (!empty($sequencing['children'])){
340 $rolluprules = array();
341 foreach ($sequencing['children'] as $sequencingrolluprule) {
342 if ($sequencingrolluprule['name']=='IMSSS:ROLLUPRULE' ) {
343 $rolluprule = new stdClass();
344 $rolluprule->childactivityset = 'all';
345 if (isset($sequencingrolluprule['attrs']['CHILDACTIVITYSET'])) {
346 $rolluprule->childactivityset = $sequencingrolluprule['attrs']['CHILDACTIVITYSET'];
348 $rolluprule->minimumcount = 0;
349 if (isset($sequencingrolluprule['attrs']['MINIMUMCOUNT'])) {
350 $rolluprule->minimumcount = $sequencingrolluprule['attrs']['MINIMUMCOUNT'];
352 $rolluprule->minimumpercent = 0.0000;
353 if (isset($sequencingrolluprule['attrs']['MINIMUMPERCENT'])) {
354 $rolluprule->minimumpercent = $sequencingrolluprule['attrs']['MINIMUMPERCENT'];
356 if (!empty($sequencingrolluprule['children'])) {
357 foreach ($sequencingrolluprule['children'] as $rolluproleconditions) {
358 if ($rolluproleconditions['name']=='IMSSS:ROLLUPCONDITIONS') {
359 $conditions = array();
360 $rolluprule->conditioncombination = 'all';
361 if (isset($rolluproleconditions['attrs']['CONDITIONCOMBINATION'])) {
362 $rolluprule->conditioncombination = $rolluproleconditions['attrs']['CONDITIONCOMBINATION'];
364 foreach ($rolluproleconditions['children'] as $rolluprulecondition) {
365 if ($rolluprulecondition['name']=='IMSSS:ROLLUPCONDITION') {
366 $condition = new stdClass();
367 if (isset($rolluprulecondition['attrs']['CONDITION'])) {
368 $condition->cond = $rolluprulecondition['attrs']['CONDITION'];
370 $condition->operator = 'noOp';
371 if (isset($rolluprulecondition['attrs']['OPERATOR'])) {
372 $condition->operator = $rolluprulecondition['attrs']['OPERATOR'];
374 array_push($conditions,$condition);
377 $rolluprule->conditions = $conditions;
379 if ($rolluproleconditions['name']=='IMSSS:ROLLUPACTION') {
380 $rolluprule->rollupruleaction = $rolluproleconditions['attrs']['ACTION'];
384 array_push($rolluprules, $rolluprule);
387 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rolluprules = $rolluprules;
391 if ($sequencing['name']=='IMSSS:SEQUENCINGRULES') {
392 if (!empty($sequencing['children'])) {
393 $sequencingrules = array();
394 foreach ($sequencing['children'] as $conditionrules) {
395 $conditiontype = -1;
396 switch($conditionrules['name']) {
397 case 'IMSSS:PRECONDITIONRULE':
398 $conditiontype = 0;
399 break;
400 case 'IMSSS:POSTCONDITIONRULE':
401 $conditiontype = 1;
402 break;
403 case 'IMSSS:EXITCONDITIONRULE':
404 $conditiontype = 2;
405 break;
407 if (!empty($conditionrules['children'])) {
408 $sequencingrule = new stdClass();
409 foreach ($conditionrules['children'] as $conditionrule) {
410 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
411 $ruleconditions = array();
412 $sequencingrule->conditioncombination = 'all';
413 if (isset($conditionrule['attrs']['CONDITIONCOMBINATION'])) {
414 $sequencingrule->conditioncombination = $conditionrule['attrs']['CONDITIONCOMBINATION'];
416 foreach ($conditionrule['children'] as $rulecondition) {
417 if ($rulecondition['name']=='IMSSS:RULECONDITION') {
418 $condition = new stdClass();
419 if (isset($rulecondition['attrs']['CONDITION'])) {
420 $condition->cond = $rulecondition['attrs']['CONDITION'];
422 $condition->operator = 'noOp';
423 if (isset($rulecondition['attrs']['OPERATOR'])) {
424 $condition->operator = $rulecondition['attrs']['OPERATOR'];
426 $condition->measurethreshold = 0.0000;
427 if (isset($rulecondition['attrs']['MEASURETHRESHOLD'])) {
428 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
430 $condition->referencedobjective = '';
431 if (isset($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
432 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
434 array_push($ruleconditions,$condition);
437 $sequencingrule->ruleconditions = $ruleconditions;
439 if ($conditionrule['name']=='IMSSS:RULEACTION') {
440 $sequencingrule->action = $conditionrule['attrs']['ACTION'];
442 $sequencingrule->type = $conditiontype;
444 array_push($sequencingrules,$sequencingrule);
447 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->sequencingrules = $sequencingrules;
452 break;
456 return $scoes;
459 function scorm_parse_scorm($pkgdir,$scormid) {
460 global $CFG;
462 $launch = 0;
463 $manifestfile = $pkgdir.'/imsmanifest.xml';
465 if (is_file($manifestfile)) {
467 $xmltext = file_get_contents($manifestfile);
469 $pattern = '/&(?!\w{2,6};)/';
470 $replacement = '&amp;';
471 $xmltext = preg_replace($pattern, $replacement, $xmltext);
473 $objXML = new xml2Array();
474 $manifests = $objXML->parse($xmltext);
475 //print_object($manifests);
476 $scoes = new stdClass();
477 $scoes->version = '';
478 $scoes = scorm_get_manifest($manifests,$scoes);
479 //print_object($scoes);
480 if (count($scoes->elements) > 0) {
481 $olditems = get_records('scorm_scoes','scorm',$scormid);
482 foreach ($scoes->elements as $manifest => $organizations) {
483 foreach ($organizations as $organization => $items) {
484 foreach ($items as $identifier => $item) {
485 // This new db mngt will support all SCORM future extensions
486 $newitem = new stdClass();
487 $newitem->scorm = $scormid;
488 $newitem->manifest = $manifest;
489 $newitem->organization = $organization;
490 $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
491 foreach ($standarddatas as $standarddata) {
492 if (isset($item->$standarddata)) {
493 $newitem->$standarddata = addslashes($item->$standarddata);
497 // Insert the new SCO, and retain the link between the old and new for later adjustment
498 $id = insert_record('scorm_scoes',$newitem);
499 if (!empty($olditems) && ($olditemid = scorm_array_search('identifier',$newitem->identifier,$olditems))) {
500 $olditems[$olditemid]->newid = $id;
503 if ($optionaldatas = scorm_optionals_data($item,$standarddatas)) {
504 $data = new stdClass();
505 $data->scoid = $id;
506 foreach ($optionaldatas as $optionaldata) {
507 if (isset($item->$optionaldata)) {
508 $data->name = $optionaldata;
509 $data->value = addslashes($item->$optionaldata);
510 $dataid = insert_record('scorm_scoes_data',$data);
515 if (isset($item->sequencingrules)) {
516 foreach($item->sequencingrules as $sequencingrule) {
517 $rule = new stdClass();
518 $rule->scoid = $id;
519 $rule->ruletype = $sequencingrule->type;
520 $rule->conditioncombination = $sequencingrule->conditioncombination;
521 $rule->action = $sequencingrule->action;
522 $ruleid = insert_record('scorm_seq_ruleconds',$rule);
523 if (isset($sequencingrule->ruleconditions)) {
524 foreach($sequencingrule->ruleconditions as $rulecondition) {
525 $rulecond = new stdClass();
526 $rulecond->scoid = $id;
527 $rulecond->ruleconditionsid = $ruleid;
528 $rulecond->referencedobjective = $rulecondition->referencedobjective;
529 $rulecond->measurethreshold = $rulecondition->measurethreshold;
530 $rulecond->cond = $rulecondition->cond;
531 $rulecondid = insert_record('scorm_seq_rulecond',$rulecond);
537 if (isset($item->rolluprules)) {
538 foreach($item->rolluprules as $rolluprule) {
539 $rollup = new stdClass();
540 $rollup->scoid = $id;
541 $rollup->childactivityset = $rolluprule->childactivityset;
542 $rollup->minimumcount = $rolluprule->minimumcount;
543 $rollup->minimumpercent = $rolluprule->minimumpercent;
544 $rollup->rollupruleaction = $rolluprule->rollupruleaction;
545 $rollup->conditioncombination = $rolluprule->conditioncombination;
547 $rollupruleid = insert_record('scorm_seq_rolluprule',$rollup);
548 if (isset($rollup->conditions)) {
549 foreach($rollup->conditions as $condition){
550 $cond = new stdClass();
551 $cond->scoid = $rollup->scoid;
552 $cond->rollupruleid = $rollupruleid;
553 $cond->operator = $condition->operator;
554 $cond->cond = $condition->cond;
555 $conditionid = insert_record('scorm_seq_rolluprulecond',$cond);
561 if (isset($item->objectives)) {
562 foreach($item->objectives as $objective) {
563 $obj = new stdClass();
564 $obj->scoid = $id;
565 $obj->primaryobj = $objective->primaryobj;
566 $obj->satisfiedbumeasure = $objective->satisfiedbymeasure;
567 $obj->objectiveid = $objective->objectiveid;
568 $obj->minnormalizedmeasure = $objective->minnormalizedmeasure;
569 $objectiveid = insert_record('scorm_seq_objective',$obj);
570 if (isset($objective->mapinfos)) {
571 //print_object($objective->mapinfos);
572 foreach($objective->mapinfos as $objmapinfo) {
573 $mapinfo = new stdClass();
574 $mapinfo->scoid = $id;
575 $mapinfo->objectiveid = $objectiveid;
576 $mapinfo->targetobjectiveid = $objmapinfo->targetobjectiveid;
577 $mapinfo->readsatisfiedstatus = $objmapinfo->readsatisfiedstatus;
578 $mapinfo->writesatisfiedstatus = $objmapinfo->writesatisfiedstatus;
579 $mapinfo->readnormalizedmeasure = $objmapinfo->readnormalizedmeasure;
580 $mapinfo->writenormalizedmeasure = $objmapinfo->writenormalizedmeasure;
581 $mapinfoid = insert_record('scorm_seq_mapinfo',$mapinfo);
586 //print_object($item);
587 if (($launch == 0) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
588 $launch = $id;
593 if (!empty($olditems)) {
594 foreach($olditems as $olditem) {
595 delete_records('scorm_scoes','id',$olditem->id);
596 delete_records('scorm_scoes_data','scoid',$olditem->id);
597 if (isset($olditem->newid)) {
598 set_field('scorm_scoes_track', 'scoid', $olditem->newid, 'scoid', $olditem->id);
600 delete_records('scorm_scoes_track','scoid',$olditem->id);
601 delete_records('scorm_seq_objective','scoid',$olditem->id);
602 delete_records('scorm_seq_mapinfo','scoid',$olditem->id);
603 delete_records('scorm_seq_ruleconds','scoid',$olditem->id);
604 delete_records('scorm_seq_rulecond','scoid',$olditem->id);
605 delete_records('scorm_seq_rolluprule','scoid',$olditem->id);
606 delete_records('scorm_seq_rolluprulecond','scoid',$olditem->id);
609 set_field('scorm','version',$scoes->version,'id',$scormid);
613 return $launch;
616 function scorm_optionals_data($item, $standarddata) {
617 $result = array();
618 $sequencingdata = array('sequencingrules','rolluprules','objectives');
619 foreach ($item as $element => $value) {
620 if (! in_array($element, $standarddata)) {
621 if (! in_array($element, $sequencingdata)) {
622 $result[] = $element;
626 return $result;
629 function scorm_is_leaf($sco) {
630 if (get_record('scorm_scoes','scorm',$sco->scorm,'parent',$sco->identifier)) {
631 return false;
633 return true;
636 function scorm_get_parent($sco) {
637 if ($sco->parent != '/') {
638 if ($parent = get_record('scorm_scoes','scorm',$sco->scorm,'identifier',$sco->parent)) {
639 return scorm_get_sco($parent->id);
642 return null;
645 function scorm_get_children($sco) {
646 if ($children = get_records('scorm_scoes','scorm',$sco->scorm,'parent',$sco->identifier)) {//originally this said parent instead of childrean
647 return $children;
649 return null;
652 function scorm_get_available_children($sco){
653 $res = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','availablechildren');
654 if(!$res || $res == null){
655 return false;
657 else{
658 return unserialize($res->value);
662 function scorm_get_available_descendent($descend = array(),$sco){
663 if($sco == null){
664 return $descend;
666 else{
667 $avchildren = scorm_get_available_children($sco);
668 foreach($avchildren as $avchild){
669 array_push($descend,$avchild);
671 foreach($avchildren as $avchild){
672 scorm_get_available_descendent($descend,$avchild);
677 function scorm_get_siblings($sco) {
678 if ($siblings = get_records('scorm_scoes','scorm',$sco->scorm,'parent',$sco->parent)) {
679 unset($siblings[$sco->id]);
680 if (!empty($siblings)) {
681 return $siblings;
684 return null;
687 function scorm_get_ancestors($sco) {
688 if ($sco->parent != '/') {
689 return array_push(scorm_get_ancestors(scorm_get_parent($sco)));
690 } else {
691 return $sco;
695 function scorm_get_preorder($preorder=array(),$sco) {
698 if ($sco != null) {
699 array_push($preorder,$sco);
700 $children = scorm_get_children($sco);
701 foreach ($children as $child){
702 scorm_get_preorder($sco);
704 } else {
705 return $preorder;
709 function scorm_find_common_ancestor($ancestors, $sco) {
710 $pos = scorm_array_search('identifier',$sco->parent,$ancestors);
711 if ($sco->parent != '/') {
712 if ($pos === false) {
713 return scorm_find_common_ancestor($ancestors,scorm_get_parent($sco));
716 return $pos;
719 /* Usage
720 Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
722 $objXML = new xml2Array();
723 $arrOutput = $objXML->parse($strYourXML);
724 print_r($arrOutput); //print it out, or do whatever!
727 class xml2Array {
729 var $arrOutput = array();
730 var $resParser;
731 var $strXmlData;
734 * Convert a utf-8 string to html entities
736 * @param string $str The UTF-8 string
737 * @return string
739 function utf8_to_entities($str) {
740 global $CFG;
742 $entities = '';
743 $values = array();
744 $lookingfor = 1;
746 return $str;
750 * Parse an XML text string and create an array tree that rapresent the XML structure
752 * @param string $strInputXML The XML string
753 * @return array
755 function parse($strInputXML) {
756 $this->resParser = xml_parser_create ('UTF-8');
757 xml_set_object($this->resParser,$this);
758 xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
760 xml_set_character_data_handler($this->resParser, "tagData");
762 $this->strXmlData = xml_parse($this->resParser,$strInputXML );
763 if(!$this->strXmlData) {
764 die(sprintf("XML error: %s at line %d",
765 xml_error_string(xml_get_error_code($this->resParser)),
766 xml_get_current_line_number($this->resParser)));
769 xml_parser_free($this->resParser);
771 return $this->arrOutput;
774 function tagOpen($parser, $name, $attrs) {
775 $tag=array("name"=>$name,"attrs"=>$attrs);
776 array_push($this->arrOutput,$tag);
779 function tagData($parser, $tagData) {
780 if(trim($tagData)) {
781 if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
782 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $this->utf8_to_entities($tagData);
783 } else {
784 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $this->utf8_to_entities($tagData);
789 function tagClosed($parser, $name) {
790 $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
791 array_pop($this->arrOutput);