MDL-32507 SCORM fix some strict standard errors
[moodle.git] / mod / scorm / datamodels / scormlib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 function scorm_get_resources($blocks) {
18     $resources = array();
19     foreach ($blocks as $block) {
20         if ($block['name'] == 'RESOURCES' && isset($block['children'])) {
21             foreach ($block['children'] as $resource) {
22                 if ($resource['name'] == 'RESOURCE') {
23                     $resources[addslashes_js($resource['attrs']['IDENTIFIER'])] = $resource['attrs'];
24                 }
25             }
26         }
27     }
28     return $resources;
29 }
31 function scorm_get_manifest($blocks, $scoes) {
32     global $OUTPUT;
33     static $parents = array();
34     static $resources;
36     static $manifest;
37     static $organization;
39     $manifestresourcesnotfound = array();
40     if (count($blocks) > 0) {
41         foreach ($blocks as $block) {
42             switch ($block['name']) {
43                 case 'METADATA':
44                     if (isset($block['children'])) {
45                         foreach ($block['children'] as $metadata) {
46                             if ($metadata['name'] == 'SCHEMAVERSION') {
47                                 if (empty($scoes->version)) {
48                                     if (isset($metadata['tagData']) && (preg_match("/^(1\.2)$|^(CAM )?(1\.3)$/", $metadata['tagData'], $matches))) {
49                                         $scoes->version = 'SCORM_'.$matches[count($matches)-1];
50                                     } else {
51                                         if (isset($metadata['tagData']) && (preg_match("/^2004 (3rd|4th) Edition$/", $metadata['tagData'], $matches))) {
52                                             $scoes->version = 'SCORM_1.3';
53                                         } else {
54                                             $scoes->version = 'SCORM_1.2';
55                                         }
56                                     }
57                                 }
58                             }
59                         }
60                     }
61                 break;
62                 case 'MANIFEST':
63                     $manifest = $block['attrs']['IDENTIFIER'];
64                     $organization = '';
65                     $resources = array();
66                     $resources = scorm_get_resources($block['children']);
67                     $scoes = scorm_get_manifest($block['children'], $scoes);
68                     if (empty($scoes->elements) || count($scoes->elements) <= 0) {
69                         foreach ($resources as $item => $resource) {
70                             if (!empty($resource['HREF'])) {
71                                 $sco = new stdClass();
72                                 $sco->identifier = $item;
73                                 $sco->title = $item;
74                                 $sco->parent = '/';
75                                 $sco->launch = $resource['HREF'];
76                                 $sco->scormtype = $resource['ADLCP:SCORMTYPE'];
77                                 $scoes->elements[$manifest][$organization][$item] = $sco;
78                             }
79                         }
80                     }
81                 break;
82                 case 'ORGANIZATIONS':
83                     if (!isset($scoes->defaultorg) && isset($block['attrs']['DEFAULT'])) {
84                         $scoes->defaultorg = $block['attrs']['DEFAULT'];
85                     }
86                     if (!empty($block['children'])) {
87                         $scoes = scorm_get_manifest($block['children'], $scoes);
88                     }
89                 break;
90                 case 'ORGANIZATION':
91                     $identifier = $block['attrs']['IDENTIFIER'];
92                     $organization = '';
93                     $scoes->elements[$manifest][$organization][$identifier] = new stdClass();
94                     $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
95                     $scoes->elements[$manifest][$organization][$identifier]->parent = '/';
96                     $scoes->elements[$manifest][$organization][$identifier]->launch = '';
97                     $scoes->elements[$manifest][$organization][$identifier]->scormtype = '';
99                     $parents = array();
100                     $parent = new stdClass();
101                     $parent->identifier = $identifier;
102                     $parent->organization = $organization;
103                     array_push($parents, $parent);
104                     $organization = $identifier;
106                     if (!empty($block['children'])) {
107                         $scoes = scorm_get_manifest($block['children'], $scoes);
108                     }
110                     array_pop($parents);
111                 break;
112                 case 'ITEM':
113                     $parent = array_pop($parents);
114                     array_push($parents, $parent);
116                     $identifier = $block['attrs']['IDENTIFIER'];
117                     $scoes->elements[$manifest][$organization][$identifier] = new stdClass();
118                     $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
119                     $scoes->elements[$manifest][$organization][$identifier]->parent = $parent->identifier;
120                     if (!isset($block['attrs']['ISVISIBLE'])) {
121                         $block['attrs']['ISVISIBLE'] = 'true';
122                     }
123                     $scoes->elements[$manifest][$organization][$identifier]->isvisible = $block['attrs']['ISVISIBLE'];
124                     if (!isset($block['attrs']['PARAMETERS'])) {
125                         $block['attrs']['PARAMETERS'] = '';
126                     }
127                     $scoes->elements[$manifest][$organization][$identifier]->parameters = $block['attrs']['PARAMETERS'];
128                     if (!isset($block['attrs']['IDENTIFIERREF'])) {
129                         $scoes->elements[$manifest][$organization][$identifier]->launch = '';
130                         $scoes->elements[$manifest][$organization][$identifier]->scormtype = 'asset';
131                     } else {
132                         $idref = $block['attrs']['IDENTIFIERREF'];
133                         $base = '';
134                         if (isset($resources[$idref]['XML:BASE'])) {
135                             $base = $resources[$idref]['XML:BASE'];
136                         }
137                         if (!isset($resources[$idref])) {
138                             $manifestresourcesnotfound[] = $idref;
139                             $scoes->elements[$manifest][$organization][$identifier]->launch = '';
140                         } else {
141                             $scoes->elements[$manifest][$organization][$identifier]->launch = $base.$resources[$idref]['HREF'];
142                             if (empty($resources[$idref]['ADLCP:SCORMTYPE'])) {
143                                 $resources[$idref]['ADLCP:SCORMTYPE'] = 'asset';
144                             }
145                             $scoes->elements[$manifest][$organization][$identifier]->scormtype = $resources[$idref]['ADLCP:SCORMTYPE'];
146                         }
147                     }
149                     $parent = new stdClass();
150                     $parent->identifier = $identifier;
151                     $parent->organization = $organization;
152                     array_push($parents, $parent);
154                     if (!empty($block['children'])) {
155                         $scoes = scorm_get_manifest($block['children'], $scoes);
156                     }
158                     array_pop($parents);
159                 break;
160                 case 'TITLE':
161                     $parent = array_pop($parents);
162                     array_push($parents, $parent);
163                     if (!isset($block['tagData'])) {
164                         $block['tagData'] = '';
165                     }
166                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->title = $block['tagData'];
167                 break;
168                 case 'ADLCP:PREREQUISITES':
169                     if ($block['attrs']['TYPE'] == 'aicc_script') {
170                         $parent = array_pop($parents);
171                         array_push($parents, $parent);
172                         if (!isset($block['tagData'])) {
173                             $block['tagData'] = '';
174                         }
175                         $scoes->elements[$manifest][$parent->organization][$parent->identifier]->prerequisites = $block['tagData'];
176                     }
177                 break;
178                 case 'ADLCP:MAXTIMEALLOWED':
179                     $parent = array_pop($parents);
180                     array_push($parents, $parent);
181                     if (!isset($block['tagData'])) {
182                         $block['tagData'] = '';
183                     }
184                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->maxtimeallowed = $block['tagData'];
185                 break;
186                 case 'ADLCP:TIMELIMITACTION':
187                     $parent = array_pop($parents);
188                     array_push($parents, $parent);
189                     if (!isset($block['tagData'])) {
190                         $block['tagData'] = '';
191                     }
192                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->timelimitaction = $block['tagData'];
193                 break;
194                 case 'ADLCP:DATAFROMLMS':
195                     $parent = array_pop($parents);
196                     array_push($parents, $parent);
197                     if (!isset($block['tagData'])) {
198                         $block['tagData'] = '';
199                     }
200                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->datafromlms = $block['tagData'];
201                 break;
202                 case 'ADLCP:MASTERYSCORE':
203                     $parent = array_pop($parents);
204                     array_push($parents, $parent);
205                     if (!isset($block['tagData'])) {
206                         $block['tagData'] = '';
207                     }
208                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->masteryscore = $block['tagData'];
209                 break;
210                 case 'ADLCP:COMPLETIONTHRESHOLD':
211                     $parent = array_pop($parents);
212                     array_push($parents, $parent);
213                     if (!isset($block['attrs']['MINPROGRESSMEASURE'])) {
214                         $block['attrs']['MINPROGRESSMEASURE'] = '1.0';
215                     }
216                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->threshold = $block['attrs']['MINPROGRESSMEASURE'];
217                 break;
218                 case 'ADLNAV:PRESENTATION':
219                     $parent = array_pop($parents);
220                     array_push($parents, $parent);
221                     if (!empty($block['children'])) {
222                         foreach ($block['children'] as $adlnav) {
223                             if ($adlnav['name'] == 'ADLNAV:NAVIGATIONINTERFACE') {
224                                 foreach ($adlnav['children'] as $adlnavInterface) {
225                                     if ($adlnavInterface['name'] == 'ADLNAV:HIDELMSUI') {
226                                         if ($adlnavInterface['tagData'] == 'continue') {
227                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidecontinue = 1;
228                                         }
229                                         if ($adlnavInterface['tagData'] == 'previous') {
230                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideprevious = 1;
231                                         }
232                                         if ($adlnavInterface['tagData'] == 'exit') {
233                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexit = 1;
234                                         }
235                                         if ($adlnavInterface['tagData'] == 'exitAll') {
236                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexitall = 1;
237                                         }
238                                         if ($adlnavInterface['tagData'] == 'abandon') {
239                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandon = 1;
240                                         }
241                                         if ($adlnavInterface['tagData'] == 'abandonAll') {
242                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandonall = 1;
243                                         }
244                                         if ($adlnavInterface['tagData'] == 'suspendAll') {
245                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidesuspendall = 1;
246                                         }
247                                     }
248                                 }
249                             }
250                         }
251                     }
252                 break;
253                 case 'IMSSS:SEQUENCING':
254                     $parent = array_pop($parents);
255                     array_push($parents, $parent);
256                     if (!empty($block['children'])) {
257                         foreach ($block['children'] as $sequencing) {
258                             if ($sequencing['name']=='IMSSS:CONTROLMODE') {
259                                 if (isset($sequencing['attrs']['CHOICE'])) {
260                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choice = $sequencing['attrs']['CHOICE'] == 'true'?1:0;
261                                 }
262                                 if (isset($sequencing['attrs']['CHOICEEXIT'])) {
263                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choiceexit = $sequencing['attrs']['CHOICEEXIT'] == 'true'?1:0;
264                                 }
265                                 if (isset($sequencing['attrs']['FLOW'])) {
266                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->flow = $sequencing['attrs']['FLOW'] == 'true'?1:0;
267                                 }
268                                 if (isset($sequencing['attrs']['FORWARDONLY'])) {
269                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->forwardonly = $sequencing['attrs']['FORWARDONLY'] == 'true'?1:0;
270                                 }
271                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'])) {
272                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptobjectinfo = $sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'] == 'true'?1:0;
273                                 }
274                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'])) {
275                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptprogressinfo = $sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'] == 'true'?1:0;
276                                 }
277                             }
278                             if ($sequencing['name']=='ADLSEQ:CONSTRAINEDCHOICECONSIDERATIONS') {
279                                 if (isset($sequencing['attrs']['CONSTRAINCHOICE'])) {
280                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->constrainChoice = $sequencing['attrs']['CONSTRAINCHOICE'] == 'true'?1:0;
281                                 }
282                                 if (isset($sequencing['attrs']['PREVENTACTIVATION'])) {
283                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->preventactivation = $sequencing['attrs']['PREVENTACTIVATION'] == 'true'?1:0;
284                                 }
285                             }
286                             if ($sequencing['name']=='IMSSS:OBJECTIVES') {
287                                 $objectives = array();
288                                 foreach ($sequencing['children'] as $objective) {
289                                     $objectivedata = new stdClass();
290                                     $objectivedata->primaryobj = 0;
291                                     switch ($objective['name']) {
292                                         case 'IMSSS:PRIMARYOBJECTIVE':
293                                             $objectivedata->primaryobj = 1;
294                                         case 'IMSSS:OBJECTIVE':
295                                             $objectivedata->satisfiedbymeasure = 0;
296                                             if (isset($objective['attrs']['SATISFIEDBYMEASURE'])) {
297                                                 $objectivedata->satisfiedbymeasure = $objective['attrs']['SATISFIEDBYMEASURE']== 'true'?1:0;
298                                             }
299                                             $objectivedata->objectiveid = '';
300                                             if (isset($objective['attrs']['OBJECTIVEID'])) {
301                                                 $objectivedata->objectiveid = $objective['attrs']['OBJECTIVEID'];
302                                             }
303                                             $objectivedata->minnormalizedmeasure = 1.0;
304                                             if (!empty($objective['children'])) {
305                                                 $mapinfos = array();
306                                                 foreach ($objective['children'] as $objectiveparam) {
307                                                     if ($objectiveparam['name']=='IMSSS:MINNORMALIZEDMEASURE') {
308                                                         if (isset($objectiveparam['tagData'])) {
309                                                             $objectivedata->minnormalizedmeasure = $objectiveparam['tagData'];
310                                                         } else {
311                                                             $objectivedata->minnormalizedmeasure = 0;
312                                                         }
313                                                     }
314                                                     if ($objectiveparam['name']=='IMSSS:MAPINFO') {
315                                                         $mapinfo = new stdClass();
316                                                         $mapinfo->targetobjectiveid = '';
317                                                         if (isset($objectiveparam['attrs']['TARGETOBJECTIVEID'])) {
318                                                             $mapinfo->targetobjectiveid = $objectiveparam['attrs']['TARGETOBJECTIVEID'];
319                                                         }
320                                                         $mapinfo->readsatisfiedstatus = 1;
321                                                         if (isset($objectiveparam['attrs']['READSATISFIEDSTATUS'])) {
322                                                             $mapinfo->readsatisfiedstatus = $objectiveparam['attrs']['READSATISFIEDSTATUS'] == 'true'?1:0;
323                                                         }
324                                                         $mapinfo->writesatisfiedstatus = 0;
325                                                         if (isset($objectiveparam['attrs']['WRITESATISFIEDSTATUS'])) {
326                                                             $mapinfo->writesatisfiedstatus = $objectiveparam['attrs']['WRITESATISFIEDSTATUS'] == 'true'?1:0;
327                                                         }
328                                                         $mapinfo->readnormalizemeasure = 1;
329                                                         if (isset($objectiveparam['attrs']['READNORMALIZEDMEASURE'])) {
330                                                             $mapinfo->readnormalizemeasure = $objectiveparam['attrs']['READNORMALIZEDMEASURE'] == 'true'?1:0;
331                                                         }
332                                                         $mapinfo->writenormalizemeasure = 0;
333                                                         if (isset($objectiveparam['attrs']['WRITENORMALIZEDMEASURE'])) {
334                                                             $mapinfo->writenormalizemeasure = $objectiveparam['attrs']['WRITENORMALIZEDMEASURE'] == 'true'?1:0;
335                                                         }
336                                                         array_push($mapinfos, $mapinfo);
337                                                     }
338                                                 }
339                                                 if (!empty($mapinfos)) {
340                                                     $objectivesdata->mapinfos = $mapinfos;
341                                                 }
342                                             }
343                                         break;
344                                     }
345                                     array_push($objectives, $objectivedata);
346                                 }
347                                 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectives = $objectives;
348                             }
349                             if ($sequencing['name']=='IMSSS:LIMITCONDITIONS') {
350                                 if (isset($sequencing['attrs']['ATTEMPTLIMIT'])) {
351                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptLimit = $sequencing['attrs']['ATTEMPTLIMIT'];
352                                 }
353                                 if (isset($sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'])) {
354                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptAbsoluteDurationLimit = $sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'];
355                                 }
356                             }
357                             if ($sequencing['name']=='IMSSS:ROLLUPRULES') {
358                                 if (isset($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'])) {
359                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupobjectivesatisfied = $sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'] == 'true'?1:0;;
360                                 }
361                                 if (isset($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'])) {
362                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupprogresscompletion = $sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'] == 'true'?1:0;
363                                 }
364                                 if (isset($sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'])) {
365                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivemeasureweight = $sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'];
366                                 }
368                                 if (!empty($sequencing['children'])) {
369                                     $rolluprules = array();
370                                     foreach ($sequencing['children'] as $sequencingrolluprule) {
371                                         if ($sequencingrolluprule['name']=='IMSSS:ROLLUPRULE' ) {
372                                             $rolluprule = new stdClass();
373                                             $rolluprule->childactivityset = 'all';
374                                             if (isset($sequencingrolluprule['attrs']['CHILDACTIVITYSET'])) {
375                                                 $rolluprule->childactivityset = $sequencingrolluprule['attrs']['CHILDACTIVITYSET'];
376                                             }
377                                             $rolluprule->minimumcount = 0;
378                                             if (isset($sequencingrolluprule['attrs']['MINIMUMCOUNT'])) {
379                                                 $rolluprule->minimumcount = $sequencingrolluprule['attrs']['MINIMUMCOUNT'];
380                                             }
381                                             $rolluprule->minimumpercent = 0.0000;
382                                             if (isset($sequencingrolluprule['attrs']['MINIMUMPERCENT'])) {
383                                                 $rolluprule->minimumpercent = $sequencingrolluprule['attrs']['MINIMUMPERCENT'];
384                                             }
385                                             if (!empty($sequencingrolluprule['children'])) {
386                                                 foreach ($sequencingrolluprule['children'] as $rolluproleconditions) {
387                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPCONDITIONS') {
388                                                         $conditions = array();
389                                                         $rolluprule->conditioncombination = 'all';
390                                                         if (isset($rolluproleconditions['attrs']['CONDITIONCOMBINATION'])) {
391                                                             $rolluprule->conditioncombination = $rolluproleconditions['attrs']['CONDITIONCOMBINATION'];
392                                                         }
393                                                         foreach ($rolluproleconditions['children'] as $rolluprulecondition) {
394                                                             if ($rolluprulecondition['name']=='IMSSS:ROLLUPCONDITION') {
395                                                                 $condition = new stdClass();
396                                                                 if (isset($rolluprulecondition['attrs']['CONDITION'])) {
397                                                                     $condition->cond = $rolluprulecondition['attrs']['CONDITION'];
398                                                                 }
399                                                                 $condition->operator = 'noOp';
400                                                                 if (isset($rolluprulecondition['attrs']['OPERATOR'])) {
401                                                                     $condition->operator = $rolluprulecondition['attrs']['OPERATOR'];
402                                                                 }
403                                                                 array_push($conditions, $condition);
404                                                             }
405                                                         }
406                                                         $rolluprule->conditions = $conditions;
407                                                     }
408                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPACTION') {
409                                                         $rolluprule->rollupruleaction = $rolluproleconditions['attrs']['ACTION'];
410                                                     }
411                                                 }
412                                             }
413                                             array_push($rolluprules, $rolluprule);
414                                         }
415                                     }
416                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rolluprules = $rolluprules;
417                                 }
418                             }
420                             if ($sequencing['name']=='IMSSS:SEQUENCINGRULES') {
421                                 if (!empty($sequencing['children'])) {
422                                     $sequencingrules = array();
423                                     foreach ($sequencing['children'] as $conditionrules) {
424                                         $conditiontype = -1;
425                                         switch($conditionrules['name']) {
426                                             case 'IMSSS:PRECONDITIONRULE':
427                                                 $conditiontype = 0;
428                                             break;
429                                             case 'IMSSS:POSTCONDITIONRULE':
430                                                 $conditiontype = 1;
431                                             break;
432                                             case 'IMSSS:EXITCONDITIONRULE':
433                                                 $conditiontype = 2;
434                                             break;
435                                         }
436                                         if (!empty($conditionrules['children'])) {
437                                             $sequencingrule = new stdClass();
438                                             foreach ($conditionrules['children'] as $conditionrule) {
439                                                 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
440                                                     $ruleconditions = array();
441                                                     $sequencingrule->conditioncombination = 'all';
442                                                     if (isset($conditionrule['attrs']['CONDITIONCOMBINATION'])) {
443                                                         $sequencingrule->conditioncombination = $conditionrule['attrs']['CONDITIONCOMBINATION'];
444                                                     }
445                                                     foreach ($conditionrule['children'] as $rulecondition) {
446                                                         if ($rulecondition['name']=='IMSSS:RULECONDITION') {
447                                                             $condition = new stdClass();
448                                                             if (isset($rulecondition['attrs']['CONDITION'])) {
449                                                                 $condition->cond = $rulecondition['attrs']['CONDITION'];
450                                                             }
451                                                             $condition->operator = 'noOp';
452                                                             if (isset($rulecondition['attrs']['OPERATOR'])) {
453                                                                 $condition->operator = $rulecondition['attrs']['OPERATOR'];
454                                                             }
455                                                             $condition->measurethreshold = 0.0000;
456                                                             if (isset($rulecondition['attrs']['MEASURETHRESHOLD'])) {
457                                                                 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
458                                                             }
459                                                             $condition->referencedobjective = '';
460                                                             if (isset($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
461                                                                 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
462                                                             }
463                                                             array_push($ruleconditions, $condition);
464                                                         }
465                                                     }
466                                                     $sequencingrule->ruleconditions = $ruleconditions;
467                                                 }
468                                                 if ($conditionrule['name']=='IMSSS:RULEACTION') {
469                                                     $sequencingrule->action = $conditionrule['attrs']['ACTION'];
470                                                 }
471                                                 $sequencingrule->type = $conditiontype;
472                                             }
473                                             array_push($sequencingrules, $sequencingrule);
474                                         }
475                                     }
476                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->sequencingrules = $sequencingrules;
477                                 }
478                             }
479                         }
480                     }
481                 break;
482             }
483         }
484     }
485     if (!empty($manifestresourcesnotfound)) {
486         //throw warning to user to let them know manifest contains references to resources that don't appear to exist.
487         if (!defined('DEBUGGING_PRINTED')) { //prevent redirect and display warning
488             define('DEBUGGING_PRINTED', 1);
489         }
490         echo $OUTPUT->notification(get_string('invalidmanifestresource', 'scorm').' '. implode(', ',$manifestresourcesnotfound));
491     }
492     return $scoes;
495 function scorm_parse_scorm($scorm, $manifest) {
496     global $CFG, $DB;
498     // load manifest into string
499     if ($manifest instanceof stored_file) {
500         $xmltext = $manifest->get_content();
501     } else {
502         require_once("$CFG->libdir/filelib.php");
503         $xmltext = download_file_content($manifest);
504     }
506     $launch = 0;
508     $pattern = '/&(?!\w{2,6};)/';
509     $replacement = '&amp;';
510     $xmltext = preg_replace($pattern, $replacement, $xmltext);
512     $objXML = new xml2Array();
513     $manifests = $objXML->parse($xmltext);
514     $scoes = new stdClass();
515     $scoes->version = '';
516     $scoes = scorm_get_manifest($manifests, $scoes);
517     if (count($scoes->elements) > 0) {
518         $olditems = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id));
519         foreach ($scoes->elements as $manifest => $organizations) {
520             foreach ($organizations as $organization => $items) {
521                 foreach ($items as $identifier => $item) {
522                     // This new db mngt will support all SCORM future extensions
523                     $newitem = new stdClass();
524                     $newitem->scorm = $scorm->id;
525                     $newitem->manifest = $manifest;
526                     $newitem->organization = $organization;
527                     $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
528                     foreach ($standarddatas as $standarddata) {
529                         if (isset($item->$standarddata)) {
530                             $newitem->$standarddata = $item->$standarddata;
531                         }
532                     }
534                     // Insert the new SCO, and retain the link between the old and new for later adjustment
535                     $id = $DB->insert_record('scorm_scoes', $newitem);
536                     if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) {
537                         $olditems[$olditemid]->newid = $id;
538                     }
540                     if ($optionaldatas = scorm_optionals_data($item, $standarddatas)) {
541                         $data = new stdClass();
542                         $data->scoid = $id;
543                         foreach ($optionaldatas as $optionaldata) {
544                             if (isset($item->$optionaldata)) {
545                                 $data->name =  $optionaldata;
546                                 $data->value = $item->$optionaldata;
547                                 $dataid = $DB->insert_record('scorm_scoes_data', $data);
548                             }
549                         }
550                     }
552                     if (isset($item->sequencingrules)) {
553                         foreach ($item->sequencingrules as $sequencingrule) {
554                             $rule = new stdClass();
555                             $rule->scoid = $id;
556                             $rule->ruletype = $sequencingrule->type;
557                             $rule->conditioncombination = $sequencingrule->conditioncombination;
558                             $rule->action = $sequencingrule->action;
559                             $ruleid = $DB->insert_record('scorm_seq_ruleconds', $rule);
560                             if (isset($sequencingrule->ruleconditions)) {
561                                 foreach ($sequencingrule->ruleconditions as $rulecondition) {
562                                     $rulecond = new stdClass();
563                                     $rulecond->scoid = $id;
564                                     $rulecond->ruleconditionsid = $ruleid;
565                                     $rulecond->referencedobjective = $rulecondition->referencedobjective;
566                                     $rulecond->measurethreshold = $rulecondition->measurethreshold;
567                                     $rulecond->cond = $rulecondition->cond;
568                                     $rulecondid = $DB->insert_record('scorm_seq_rulecond', $rulecond);
569                                 }
570                             }
571                         }
572                     }
574                     if (isset($item->rolluprules)) {
575                         foreach ($item->rolluprules as $rolluprule) {
576                             $rollup = new stdClass();
577                             $rollup->scoid =  $id;
578                             $rollup->childactivityset = $rolluprule->childactivityset;
579                             $rollup->minimumcount = $rolluprule->minimumcount;
580                             $rollup->minimumpercent = $rolluprule->minimumpercent;
581                             $rollup->rollupruleaction = $rolluprule->rollupruleaction;
582                             $rollup->conditioncombination = $rolluprule->conditioncombination;
584                             $rollupruleid = $DB->insert_record('scorm_seq_rolluprule', $rollup);
585                             if (isset($rollup->conditions)) {
586                                 foreach ($rollup->conditions as $condition) {
587                                     $cond = new stdClass();
588                                     $cond->scoid = $rollup->scoid;
589                                     $cond->rollupruleid = $rollupruleid;
590                                     $cond->operator = $condition->operator;
591                                     $cond->cond = $condition->cond;
592                                     $conditionid = $DB->insert_record('scorm_seq_rolluprulecond', $cond);
593                                 }
594                             }
595                         }
596                     }
598                     if (isset($item->objectives)) {
599                         foreach ($item->objectives as $objective) {
600                             $obj = new stdClass();
601                             $obj->scoid = $id;
602                             $obj->primaryobj = $objective->primaryobj;
603                             $obj->satisfiedbumeasure = $objective->satisfiedbymeasure;
604                             $obj->objectiveid = $objective->objectiveid;
605                             $obj->minnormalizedmeasure = trim($objective->minnormalizedmeasure);
606                             $objectiveid = $DB->insert_record('scorm_seq_objective', $obj);
607                             if (isset($objective->mapinfos)) {
608                                 foreach ($objective->mapinfos as $objmapinfo) {
609                                     $mapinfo = new stdClass();
610                                     $mapinfo->scoid = $id;
611                                     $mapinfo->objectiveid = $objectiveid;
612                                     $mapinfo->targetobjectiveid = $objmapinfo->targetobjectiveid;
613                                     $mapinfo->readsatisfiedstatus = $objmapinfo->readsatisfiedstatus;
614                                     $mapinfo->writesatisfiedstatus = $objmapinfo->writesatisfiedstatus;
615                                     $mapinfo->readnormalizedmeasure = $objmapinfo->readnormalizedmeasure;
616                                     $mapinfo->writenormalizedmeasure = $objmapinfo->writenormalizedmeasure;
617                                     $mapinfoid = $DB->insert_record('scorm_seq_mapinfo', $mapinfo);
618                                 }
619                             }
620                         }
621                     }
622                     if (($launch == 0) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
623                         $launch = $id;
624                     }
625                 }
626             }
627         }
628         if (!empty($olditems)) {
629             foreach ($olditems as $olditem) {
630                 $DB->delete_records('scorm_scoes', array('id'=>$olditem->id));
631                 $DB->delete_records('scorm_scoes_data', array('scoid'=>$olditem->id));
632                 if (isset($olditem->newid)) {
633                     $DB->set_field('scorm_scoes_track', 'scoid', $olditem->newid, array('scoid' => $olditem->id));
634                 }
635                 $DB->delete_records('scorm_scoes_track', array('scoid'=>$olditem->id));
636                 $DB->delete_records('scorm_seq_objective', array('scoid'=>$olditem->id));
637                 $DB->delete_records('scorm_seq_mapinfo', array('scoid'=>$olditem->id));
638                 $DB->delete_records('scorm_seq_ruleconds', array('scoid'=>$olditem->id));
639                 $DB->delete_records('scorm_seq_rulecond', array('scoid'=>$olditem->id));
640                 $DB->delete_records('scorm_seq_rolluprule', array('scoid'=>$olditem->id));
641                 $DB->delete_records('scorm_seq_rolluprulecond', array('scoid'=>$olditem->id));
642             }
643         }
644         if (empty($scoes->version)) {
645             $scoes->version = 'SCORM_1.2';
646         }
647         $DB->set_field('scorm', 'version', $scoes->version, array('id'=>$scorm->id));
648         $scorm->version = $scoes->version;
649     }
651     $scorm->launch = $launch;
653     return true;
656 function scorm_optionals_data($item, $standarddata) {
657     $result = array();
658     $sequencingdata = array('sequencingrules', 'rolluprules', 'objectives');
659     foreach ($item as $element => $value) {
660         if (! in_array($element, $standarddata)) {
661             if (! in_array($element, $sequencingdata)) {
662                 $result[] = $element;
663             }
664         }
665     }
666     return $result;
669 function scorm_is_leaf($sco) {
670     global $DB;
672     if ($DB->get_record('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->identifier))) {
673         return false;
674     }
675     return true;
678 function scorm_get_parent($sco) {
679     global $DB;
681     if ($sco->parent != '/') {
682         if ($parent = $DB->get_record('scorm_scoes', array('scorm'=>$sco->scorm, 'identifier'=>$sco->parent))) {
683             return scorm_get_sco($parent->id);
684         }
685     }
686     return null;
689 function scorm_get_children($sco) {
690     global $DB;
692     if ($children = $DB->get_records('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->identifier))) {//originally this said parent instead of childrean
693         return $children;
694     }
695     return null;
698 function scorm_get_available_children($sco) {  // TODO: undefined vars!!!
699     global $DB;
701     $res = $DB->get_record('scorm_scoes_track', array('scoid'=>$scoid,
702                                                      'userid'=>$userid,
703                                                      'element'=>'availablechildren'));
704     if (!$res || $res == null) {
705         return false;
706     } else {
707         return unserialize($res->value);
708     }
711 function scorm_get_available_descendent($descend = array(), $sco) {
712     if ($sco == null) {
713         return $descend;
714     } else {
715         $avchildren = scorm_get_available_children($sco);
716         foreach ($avchildren as $avchild) {
717             array_push($descend, $avchild);
718         }
719         foreach ($avchildren as $avchild) {
720             scorm_get_available_descendent($descend, $avchild);
721         }
722     }
725 function scorm_get_siblings($sco) {
726     global $DB;
728     if ($siblings = $DB->get_records('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->parent))) {
729         unset($siblings[$sco->id]);
730         if (!empty($siblings)) {
731             return $siblings;
732         }
733     }
734     return null;
736 //get an array that contains all the parent scos for this sco.
737 function scorm_get_ancestors($sco) {
738     $ancestors = array();
739     $continue = true;
740     while ($continue) {
741         $ancestor = scorm_get_parent($sco);
742         if (!empty($ancestor) && $ancestor->id !== $sco->id) {
743             $sco = $ancestor;
744             $ancestors[] = $ancestor;
745             if ($sco->parent == '/') {
746                 $continue = false;
747             }
748         } else {
749             $continue = false;
750         }
751     }
752     return $ancestors;
755 function scorm_get_preorder($preorder=array(), $sco) {
756     if ($sco != null) {
757         array_push($preorder, $sco);
758         $children = scorm_get_children($sco);
759         foreach ($children as $child) {
760             scorm_get_preorder($sco);
761         }
762     } else {
763         return $preorder;
764     }
767 function scorm_find_common_ancestor($ancestors, $sco) {
768     $pos = scorm_array_search('identifier', $sco->parent, $ancestors);
769     if ($sco->parent != '/') {
770         if ($pos === false) {
771             return scorm_find_common_ancestor($ancestors, scorm_get_parent($sco));
772         }
773     }
774     return $pos;
777 /* Usage
778  Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
780  $objXML = new xml2Array();
781  $arrOutput = $objXML->parse($strYourXML);
782  print_r($arrOutput); //print it out, or do whatever!
784 */
785 class xml2Array {
787     var $arrOutput = array();
788     var $resParser;
789     var $strXmlData;
791     /**
792      * Convert a utf-8 string to html entities
793      *
794      * @param string $str The UTF-8 string
795      * @return string
796      */
797     function utf8_to_entities($str) {
798         global $CFG;
800         $entities = '';
801         $values = array();
802         $lookingfor = 1;
804         return $str;
805     }
807     /**
808      * Parse an XML text string and create an array tree that rapresent the XML structure
809      *
810      * @param string $strInputXML The XML string
811      * @return array
812      */
813     function parse($strInputXML) {
814         $this->resParser = xml_parser_create ('UTF-8');
815         xml_set_object($this->resParser, $this);
816         xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
818         xml_set_character_data_handler($this->resParser, "tagData");
820         $this->strXmlData = xml_parse($this->resParser, $strInputXML );
821         if (!$this->strXmlData) {
822             die(sprintf("XML error: %s at line %d",
823             xml_error_string(xml_get_error_code($this->resParser)),
824             xml_get_current_line_number($this->resParser)));
825         }
827         xml_parser_free($this->resParser);
829         return $this->arrOutput;
830     }
832     function tagOpen($parser, $name, $attrs) {
833         $tag=array("name"=>$name, "attrs"=>$attrs);
834         array_push($this->arrOutput, $tag);
835     }
837     function tagData($parser, $tagData) {
838         if (trim($tagData)) {
839             if (isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
840                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $this->utf8_to_entities($tagData);
841             } else {
842                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $this->utf8_to_entities($tagData);
843             }
844         }
845     }
847     function tagClosed($parser, $name) {
848         $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
849         array_pop($this->arrOutput);
850     }