d3a1ab9685991d5383f50d2c3cde0ce0cd800094
[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]->identifier = $identifier;
94                     $scoes->elements[$manifest][$organization][$identifier]->parent = '/';
95                     $scoes->elements[$manifest][$organization][$identifier]->launch = '';
96                     $scoes->elements[$manifest][$organization][$identifier]->scormtype = '';
98                     $parents = array();
99                     $parent = new stdClass();
100                     $parent->identifier = $identifier;
101                     $parent->organization = $organization;
102                     array_push($parents, $parent);
103                     $organization = $identifier;
105                     if (!empty($block['children'])) {
106                         $scoes = scorm_get_manifest($block['children'], $scoes);
107                     }
109                     array_pop($parents);
110                 break;
111                 case 'ITEM':
112                     $parent = array_pop($parents);
113                     array_push($parents, $parent);
115                     $identifier = $block['attrs']['IDENTIFIER'];
116                     $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
117                     $scoes->elements[$manifest][$organization][$identifier]->parent = $parent->identifier;
118                     if (!isset($block['attrs']['ISVISIBLE'])) {
119                         $block['attrs']['ISVISIBLE'] = 'true';
120                     }
121                     $scoes->elements[$manifest][$organization][$identifier]->isvisible = $block['attrs']['ISVISIBLE'];
122                     if (!isset($block['attrs']['PARAMETERS'])) {
123                         $block['attrs']['PARAMETERS'] = '';
124                     }
125                     $scoes->elements[$manifest][$organization][$identifier]->parameters = $block['attrs']['PARAMETERS'];
126                     if (!isset($block['attrs']['IDENTIFIERREF'])) {
127                         $scoes->elements[$manifest][$organization][$identifier]->launch = '';
128                         $scoes->elements[$manifest][$organization][$identifier]->scormtype = 'asset';
129                     } else {
130                         $idref = $block['attrs']['IDENTIFIERREF'];
131                         $base = '';
132                         if (isset($resources[$idref]['XML:BASE'])) {
133                             $base = $resources[$idref]['XML:BASE'];
134                         }
135                         if (!isset($resources[$idref])) {
136                             $manifestresourcesnotfound[] = $idref;
137                             $scoes->elements[$manifest][$organization][$identifier]->launch = '';
138                         } else {
139                             $scoes->elements[$manifest][$organization][$identifier]->launch = $base.$resources[$idref]['HREF'];
140                             if (empty($resources[$idref]['ADLCP:SCORMTYPE'])) {
141                                 $resources[$idref]['ADLCP:SCORMTYPE'] = 'asset';
142                             }
143                             $scoes->elements[$manifest][$organization][$identifier]->scormtype = $resources[$idref]['ADLCP:SCORMTYPE'];
144                         }
145                     }
147                     $parent = new stdClass();
148                     $parent->identifier = $identifier;
149                     $parent->organization = $organization;
150                     array_push($parents, $parent);
152                     if (!empty($block['children'])) {
153                         $scoes = scorm_get_manifest($block['children'], $scoes);
154                     }
156                     array_pop($parents);
157                 break;
158                 case 'TITLE':
159                     $parent = array_pop($parents);
160                     array_push($parents, $parent);
161                     if (!isset($block['tagData'])) {
162                         $block['tagData'] = '';
163                     }
164                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->title = $block['tagData'];
165                 break;
166                 case 'ADLCP:PREREQUISITES':
167                     if ($block['attrs']['TYPE'] == 'aicc_script') {
168                         $parent = array_pop($parents);
169                         array_push($parents, $parent);
170                         if (!isset($block['tagData'])) {
171                             $block['tagData'] = '';
172                         }
173                         $scoes->elements[$manifest][$parent->organization][$parent->identifier]->prerequisites = $block['tagData'];
174                     }
175                 break;
176                 case 'ADLCP:MAXTIMEALLOWED':
177                     $parent = array_pop($parents);
178                     array_push($parents, $parent);
179                     if (!isset($block['tagData'])) {
180                         $block['tagData'] = '';
181                     }
182                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->maxtimeallowed = $block['tagData'];
183                 break;
184                 case 'ADLCP:TIMELIMITACTION':
185                     $parent = array_pop($parents);
186                     array_push($parents, $parent);
187                     if (!isset($block['tagData'])) {
188                         $block['tagData'] = '';
189                     }
190                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->timelimitaction = $block['tagData'];
191                 break;
192                 case 'ADLCP:DATAFROMLMS':
193                     $parent = array_pop($parents);
194                     array_push($parents, $parent);
195                     if (!isset($block['tagData'])) {
196                         $block['tagData'] = '';
197                     }
198                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->datafromlms = $block['tagData'];
199                 break;
200                 case 'ADLCP:MASTERYSCORE':
201                     $parent = array_pop($parents);
202                     array_push($parents, $parent);
203                     if (!isset($block['tagData'])) {
204                         $block['tagData'] = '';
205                     }
206                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->masteryscore = $block['tagData'];
207                 break;
208                 case 'ADLCP:COMPLETIONTHRESHOLD':
209                     $parent = array_pop($parents);
210                     array_push($parents, $parent);
211                     if (!isset($block['attrs']['MINPROGRESSMEASURE'])) {
212                         $block['attrs']['MINPROGRESSMEASURE'] = '1.0';
213                     }
214                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->threshold = $block['attrs']['MINPROGRESSMEASURE'];
215                 break;
216                 case 'ADLNAV:PRESENTATION':
217                     $parent = array_pop($parents);
218                     array_push($parents, $parent);
219                     if (!empty($block['children'])) {
220                         foreach ($block['children'] as $adlnav) {
221                             if ($adlnav['name'] == 'ADLNAV:NAVIGATIONINTERFACE') {
222                                 foreach ($adlnav['children'] as $adlnavInterface) {
223                                     if ($adlnavInterface['name'] == 'ADLNAV:HIDELMSUI') {
224                                         if ($adlnavInterface['tagData'] == 'continue') {
225                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidecontinue = 1;
226                                         }
227                                         if ($adlnavInterface['tagData'] == 'previous') {
228                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideprevious = 1;
229                                         }
230                                         if ($adlnavInterface['tagData'] == 'exit') {
231                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexit = 1;
232                                         }
233                                         if ($adlnavInterface['tagData'] == 'exitAll') {
234                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexitall = 1;
235                                         }
236                                         if ($adlnavInterface['tagData'] == 'abandon') {
237                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandon = 1;
238                                         }
239                                         if ($adlnavInterface['tagData'] == 'abandonAll') {
240                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandonall = 1;
241                                         }
242                                         if ($adlnavInterface['tagData'] == 'suspendAll') {
243                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidesuspendall = 1;
244                                         }
245                                     }
246                                 }
247                             }
248                         }
249                     }
250                 break;
251                 case 'IMSSS:SEQUENCING':
252                     $parent = array_pop($parents);
253                     array_push($parents, $parent);
254                     if (!empty($block['children'])) {
255                         foreach ($block['children'] as $sequencing) {
256                             if ($sequencing['name']=='IMSSS:CONTROLMODE') {
257                                 if (isset($sequencing['attrs']['CHOICE'])) {
258                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choice = $sequencing['attrs']['CHOICE'] == 'true'?1:0;
259                                 }
260                                 if (isset($sequencing['attrs']['CHOICEEXIT'])) {
261                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choiceexit = $sequencing['attrs']['CHOICEEXIT'] == 'true'?1:0;
262                                 }
263                                 if (isset($sequencing['attrs']['FLOW'])) {
264                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->flow = $sequencing['attrs']['FLOW'] == 'true'?1:0;
265                                 }
266                                 if (isset($sequencing['attrs']['FORWARDONLY'])) {
267                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->forwardonly = $sequencing['attrs']['FORWARDONLY'] == 'true'?1:0;
268                                 }
269                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'])) {
270                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptobjectinfo = $sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'] == 'true'?1:0;
271                                 }
272                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'])) {
273                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptprogressinfo = $sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'] == 'true'?1:0;
274                                 }
275                             }
276                             if ($sequencing['name']=='ADLSEQ:CONSTRAINEDCHOICECONSIDERATIONS') {
277                                 if (isset($sequencing['attrs']['CONSTRAINCHOICE'])) {
278                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->constrainChoice = $sequencing['attrs']['CONSTRAINCHOICE'] == 'true'?1:0;
279                                 }
280                                 if (isset($sequencing['attrs']['PREVENTACTIVATION'])) {
281                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->preventactivation = $sequencing['attrs']['PREVENTACTIVATION'] == 'true'?1:0;
282                                 }
283                             }
284                             if ($sequencing['name']=='IMSSS:OBJECTIVES') {
285                                 $objectives = array();
286                                 foreach ($sequencing['children'] as $objective) {
287                                     $objectivedata = new stdClass();
288                                     $objectivedata->primaryobj = 0;
289                                     switch ($objective['name']) {
290                                         case 'IMSSS:PRIMARYOBJECTIVE':
291                                             $objectivedata->primaryobj = 1;
292                                         case 'IMSSS:OBJECTIVE':
293                                             $objectivedata->satisfiedbymeasure = 0;
294                                             if (isset($objective['attrs']['SATISFIEDBYMEASURE'])) {
295                                                 $objectivedata->satisfiedbymeasure = $objective['attrs']['SATISFIEDBYMEASURE']== 'true'?1:0;
296                                             }
297                                             $objectivedata->objectiveid = '';
298                                             if (isset($objective['attrs']['OBJECTIVEID'])) {
299                                                 $objectivedata->objectiveid = $objective['attrs']['OBJECTIVEID'];
300                                             }
301                                             $objectivedata->minnormalizedmeasure = 1.0;
302                                             if (!empty($objective['children'])) {
303                                                 $mapinfos = array();
304                                                 foreach ($objective['children'] as $objectiveparam) {
305                                                     if ($objectiveparam['name']=='IMSSS:MINNORMALIZEDMEASURE') {
306                                                         if (isset($objectiveparam['tagData'])) {
307                                                             $objectivedata->minnormalizedmeasure = $objectiveparam['tagData'];
308                                                         } else {
309                                                             $objectivedata->minnormalizedmeasure = 0;
310                                                         }
311                                                     }
312                                                     if ($objectiveparam['name']=='IMSSS:MAPINFO') {
313                                                         $mapinfo = new stdClass();
314                                                         $mapinfo->targetobjectiveid = '';
315                                                         if (isset($objectiveparam['attrs']['TARGETOBJECTIVEID'])) {
316                                                             $mapinfo->targetobjectiveid = $objectiveparam['attrs']['TARGETOBJECTIVEID'];
317                                                         }
318                                                         $mapinfo->readsatisfiedstatus = 1;
319                                                         if (isset($objectiveparam['attrs']['READSATISFIEDSTATUS'])) {
320                                                             $mapinfo->readsatisfiedstatus = $objectiveparam['attrs']['READSATISFIEDSTATUS'] == 'true'?1:0;
321                                                         }
322                                                         $mapinfo->writesatisfiedstatus = 0;
323                                                         if (isset($objectiveparam['attrs']['WRITESATISFIEDSTATUS'])) {
324                                                             $mapinfo->writesatisfiedstatus = $objectiveparam['attrs']['WRITESATISFIEDSTATUS'] == 'true'?1:0;
325                                                         }
326                                                         $mapinfo->readnormalizemeasure = 1;
327                                                         if (isset($objectiveparam['attrs']['READNORMALIZEDMEASURE'])) {
328                                                             $mapinfo->readnormalizemeasure = $objectiveparam['attrs']['READNORMALIZEDMEASURE'] == 'true'?1:0;
329                                                         }
330                                                         $mapinfo->writenormalizemeasure = 0;
331                                                         if (isset($objectiveparam['attrs']['WRITENORMALIZEDMEASURE'])) {
332                                                             $mapinfo->writenormalizemeasure = $objectiveparam['attrs']['WRITENORMALIZEDMEASURE'] == 'true'?1:0;
333                                                         }
334                                                         array_push($mapinfos, $mapinfo);
335                                                     }
336                                                 }
337                                                 if (!empty($mapinfos)) {
338                                                     $objectivesdata->mapinfos = $mapinfos;
339                                                 }
340                                             }
341                                         break;
342                                     }
343                                     array_push($objectives, $objectivedata);
344                                 }
345                                 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectives = $objectives;
346                             }
347                             if ($sequencing['name']=='IMSSS:LIMITCONDITIONS') {
348                                 if (isset($sequencing['attrs']['ATTEMPTLIMIT'])) {
349                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptLimit = $sequencing['attrs']['ATTEMPTLIMIT'];
350                                 }
351                                 if (isset($sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'])) {
352                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptAbsoluteDurationLimit = $sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'];
353                                 }
354                             }
355                             if ($sequencing['name']=='IMSSS:ROLLUPRULES') {
356                                 if (isset($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'])) {
357                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupobjectivesatisfied = $sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'] == 'true'?1:0;;
358                                 }
359                                 if (isset($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'])) {
360                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupprogresscompletion = $sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'] == 'true'?1:0;
361                                 }
362                                 if (isset($sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'])) {
363                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivemeasureweight = $sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'];
364                                 }
366                                 if (!empty($sequencing['children'])) {
367                                     $rolluprules = array();
368                                     foreach ($sequencing['children'] as $sequencingrolluprule) {
369                                         if ($sequencingrolluprule['name']=='IMSSS:ROLLUPRULE' ) {
370                                             $rolluprule = new stdClass();
371                                             $rolluprule->childactivityset = 'all';
372                                             if (isset($sequencingrolluprule['attrs']['CHILDACTIVITYSET'])) {
373                                                 $rolluprule->childactivityset = $sequencingrolluprule['attrs']['CHILDACTIVITYSET'];
374                                             }
375                                             $rolluprule->minimumcount = 0;
376                                             if (isset($sequencingrolluprule['attrs']['MINIMUMCOUNT'])) {
377                                                 $rolluprule->minimumcount = $sequencingrolluprule['attrs']['MINIMUMCOUNT'];
378                                             }
379                                             $rolluprule->minimumpercent = 0.0000;
380                                             if (isset($sequencingrolluprule['attrs']['MINIMUMPERCENT'])) {
381                                                 $rolluprule->minimumpercent = $sequencingrolluprule['attrs']['MINIMUMPERCENT'];
382                                             }
383                                             if (!empty($sequencingrolluprule['children'])) {
384                                                 foreach ($sequencingrolluprule['children'] as $rolluproleconditions) {
385                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPCONDITIONS') {
386                                                         $conditions = array();
387                                                         $rolluprule->conditioncombination = 'all';
388                                                         if (isset($rolluproleconditions['attrs']['CONDITIONCOMBINATION'])) {
389                                                             $rolluprule->conditioncombination = $rolluproleconditions['attrs']['CONDITIONCOMBINATION'];
390                                                         }
391                                                         foreach ($rolluproleconditions['children'] as $rolluprulecondition) {
392                                                             if ($rolluprulecondition['name']=='IMSSS:ROLLUPCONDITION') {
393                                                                 $condition = new stdClass();
394                                                                 if (isset($rolluprulecondition['attrs']['CONDITION'])) {
395                                                                     $condition->cond = $rolluprulecondition['attrs']['CONDITION'];
396                                                                 }
397                                                                 $condition->operator = 'noOp';
398                                                                 if (isset($rolluprulecondition['attrs']['OPERATOR'])) {
399                                                                     $condition->operator = $rolluprulecondition['attrs']['OPERATOR'];
400                                                                 }
401                                                                 array_push($conditions, $condition);
402                                                             }
403                                                         }
404                                                         $rolluprule->conditions = $conditions;
405                                                     }
406                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPACTION') {
407                                                         $rolluprule->rollupruleaction = $rolluproleconditions['attrs']['ACTION'];
408                                                     }
409                                                 }
410                                             }
411                                             array_push($rolluprules, $rolluprule);
412                                         }
413                                     }
414                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rolluprules = $rolluprules;
415                                 }
416                             }
418                             if ($sequencing['name']=='IMSSS:SEQUENCINGRULES') {
419                                 if (!empty($sequencing['children'])) {
420                                     $sequencingrules = array();
421                                     foreach ($sequencing['children'] as $conditionrules) {
422                                         $conditiontype = -1;
423                                         switch($conditionrules['name']) {
424                                             case 'IMSSS:PRECONDITIONRULE':
425                                                 $conditiontype = 0;
426                                             break;
427                                             case 'IMSSS:POSTCONDITIONRULE':
428                                                 $conditiontype = 1;
429                                             break;
430                                             case 'IMSSS:EXITCONDITIONRULE':
431                                                 $conditiontype = 2;
432                                             break;
433                                         }
434                                         if (!empty($conditionrules['children'])) {
435                                             $sequencingrule = new stdClass();
436                                             foreach ($conditionrules['children'] as $conditionrule) {
437                                                 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
438                                                     $ruleconditions = array();
439                                                     $sequencingrule->conditioncombination = 'all';
440                                                     if (isset($conditionrule['attrs']['CONDITIONCOMBINATION'])) {
441                                                         $sequencingrule->conditioncombination = $conditionrule['attrs']['CONDITIONCOMBINATION'];
442                                                     }
443                                                     foreach ($conditionrule['children'] as $rulecondition) {
444                                                         if ($rulecondition['name']=='IMSSS:RULECONDITION') {
445                                                             $condition = new stdClass();
446                                                             if (isset($rulecondition['attrs']['CONDITION'])) {
447                                                                 $condition->cond = $rulecondition['attrs']['CONDITION'];
448                                                             }
449                                                             $condition->operator = 'noOp';
450                                                             if (isset($rulecondition['attrs']['OPERATOR'])) {
451                                                                 $condition->operator = $rulecondition['attrs']['OPERATOR'];
452                                                             }
453                                                             $condition->measurethreshold = 0.0000;
454                                                             if (isset($rulecondition['attrs']['MEASURETHRESHOLD'])) {
455                                                                 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
456                                                             }
457                                                             $condition->referencedobjective = '';
458                                                             if (isset($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
459                                                                 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
460                                                             }
461                                                             array_push($ruleconditions, $condition);
462                                                         }
463                                                     }
464                                                     $sequencingrule->ruleconditions = $ruleconditions;
465                                                 }
466                                                 if ($conditionrule['name']=='IMSSS:RULEACTION') {
467                                                     $sequencingrule->action = $conditionrule['attrs']['ACTION'];
468                                                 }
469                                                 $sequencingrule->type = $conditiontype;
470                                             }
471                                             array_push($sequencingrules, $sequencingrule);
472                                         }
473                                     }
474                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->sequencingrules = $sequencingrules;
475                                 }
476                             }
477                         }
478                     }
479                 break;
480             }
481         }
482     }
483     if (!empty($manifestresourcesnotfound)) {
484         //throw warning to user to let them know manifest contains references to resources that don't appear to exist.
485         if (!defined('DEBUGGING_PRINTED')) { //prevent redirect and display warning
486             define('DEBUGGING_PRINTED', 1);
487         }
488         echo $OUTPUT->notification(get_string('invalidmanifestresource', 'scorm').' '. implode(', ',$manifestresourcesnotfound));
489     }
490     return $scoes;
493 function scorm_parse_scorm($scorm, $manifest) {
494     global $CFG, $DB;
496     // load manifest into string
497     if ($manifest instanceof stored_file) {
498         $xmltext = $manifest->get_content();
499     } else {
500         require_once("$CFG->libdir/filelib.php");
501         $xmltext = download_file_content($manifest);
502     }
504     $launch = 0;
506     $pattern = '/&(?!\w{2,6};)/';
507     $replacement = '&amp;';
508     $xmltext = preg_replace($pattern, $replacement, $xmltext);
510     $objXML = new xml2Array();
511     $manifests = $objXML->parse($xmltext);
512     $scoes = new stdClass();
513     $scoes->version = '';
514     $scoes = scorm_get_manifest($manifests, $scoes);
515     if (count($scoes->elements) > 0) {
516         $olditems = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id));
517         foreach ($scoes->elements as $manifest => $organizations) {
518             foreach ($organizations as $organization => $items) {
519                 foreach ($items as $identifier => $item) {
520                     // This new db mngt will support all SCORM future extensions
521                     $newitem = new stdClass();
522                     $newitem->scorm = $scorm->id;
523                     $newitem->manifest = $manifest;
524                     $newitem->organization = $organization;
525                     $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
526                     foreach ($standarddatas as $standarddata) {
527                         if (isset($item->$standarddata)) {
528                             $newitem->$standarddata = $item->$standarddata;
529                         }
530                     }
532                     // Insert the new SCO, and retain the link between the old and new for later adjustment
533                     $id = $DB->insert_record('scorm_scoes', $newitem);
534                     if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) {
535                         $olditems[$olditemid]->newid = $id;
536                     }
538                     if ($optionaldatas = scorm_optionals_data($item, $standarddatas)) {
539                         $data = new stdClass();
540                         $data->scoid = $id;
541                         foreach ($optionaldatas as $optionaldata) {
542                             if (isset($item->$optionaldata)) {
543                                 $data->name =  $optionaldata;
544                                 $data->value = $item->$optionaldata;
545                                 $dataid = $DB->insert_record('scorm_scoes_data', $data);
546                             }
547                         }
548                     }
550                     if (isset($item->sequencingrules)) {
551                         foreach ($item->sequencingrules as $sequencingrule) {
552                             $rule = new stdClass();
553                             $rule->scoid = $id;
554                             $rule->ruletype = $sequencingrule->type;
555                             $rule->conditioncombination = $sequencingrule->conditioncombination;
556                             $rule->action = $sequencingrule->action;
557                             $ruleid = $DB->insert_record('scorm_seq_ruleconds', $rule);
558                             if (isset($sequencingrule->ruleconditions)) {
559                                 foreach ($sequencingrule->ruleconditions as $rulecondition) {
560                                     $rulecond = new stdClass();
561                                     $rulecond->scoid = $id;
562                                     $rulecond->ruleconditionsid = $ruleid;
563                                     $rulecond->referencedobjective = $rulecondition->referencedobjective;
564                                     $rulecond->measurethreshold = $rulecondition->measurethreshold;
565                                     $rulecond->cond = $rulecondition->cond;
566                                     $rulecondid = $DB->insert_record('scorm_seq_rulecond', $rulecond);
567                                 }
568                             }
569                         }
570                     }
572                     if (isset($item->rolluprules)) {
573                         foreach ($item->rolluprules as $rolluprule) {
574                             $rollup = new stdClass();
575                             $rollup->scoid =  $id;
576                             $rollup->childactivityset = $rolluprule->childactivityset;
577                             $rollup->minimumcount = $rolluprule->minimumcount;
578                             $rollup->minimumpercent = $rolluprule->minimumpercent;
579                             $rollup->rollupruleaction = $rolluprule->rollupruleaction;
580                             $rollup->conditioncombination = $rolluprule->conditioncombination;
582                             $rollupruleid = $DB->insert_record('scorm_seq_rolluprule', $rollup);
583                             if (isset($rollup->conditions)) {
584                                 foreach ($rollup->conditions as $condition) {
585                                     $cond = new stdClass();
586                                     $cond->scoid = $rollup->scoid;
587                                     $cond->rollupruleid = $rollupruleid;
588                                     $cond->operator = $condition->operator;
589                                     $cond->cond = $condition->cond;
590                                     $conditionid = $DB->insert_record('scorm_seq_rolluprulecond', $cond);
591                                 }
592                             }
593                         }
594                     }
596                     if (isset($item->objectives)) {
597                         foreach ($item->objectives as $objective) {
598                             $obj = new stdClass();
599                             $obj->scoid = $id;
600                             $obj->primaryobj = $objective->primaryobj;
601                             $obj->satisfiedbumeasure = $objective->satisfiedbymeasure;
602                             $obj->objectiveid = $objective->objectiveid;
603                             $obj->minnormalizedmeasure = trim($objective->minnormalizedmeasure);
604                             $objectiveid = $DB->insert_record('scorm_seq_objective', $obj);
605                             if (isset($objective->mapinfos)) {
606                                 foreach ($objective->mapinfos as $objmapinfo) {
607                                     $mapinfo = new stdClass();
608                                     $mapinfo->scoid = $id;
609                                     $mapinfo->objectiveid = $objectiveid;
610                                     $mapinfo->targetobjectiveid = $objmapinfo->targetobjectiveid;
611                                     $mapinfo->readsatisfiedstatus = $objmapinfo->readsatisfiedstatus;
612                                     $mapinfo->writesatisfiedstatus = $objmapinfo->writesatisfiedstatus;
613                                     $mapinfo->readnormalizedmeasure = $objmapinfo->readnormalizedmeasure;
614                                     $mapinfo->writenormalizedmeasure = $objmapinfo->writenormalizedmeasure;
615                                     $mapinfoid = $DB->insert_record('scorm_seq_mapinfo', $mapinfo);
616                                 }
617                             }
618                         }
619                     }
620                     if (($launch == 0) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
621                         $launch = $id;
622                     }
623                 }
624             }
625         }
626         if (!empty($olditems)) {
627             foreach ($olditems as $olditem) {
628                 $DB->delete_records('scorm_scoes', array('id'=>$olditem->id));
629                 $DB->delete_records('scorm_scoes_data', array('scoid'=>$olditem->id));
630                 if (isset($olditem->newid)) {
631                     $DB->set_field('scorm_scoes_track', 'scoid', $olditem->newid, array('scoid' => $olditem->id));
632                 }
633                 $DB->delete_records('scorm_scoes_track', array('scoid'=>$olditem->id));
634                 $DB->delete_records('scorm_seq_objective', array('scoid'=>$olditem->id));
635                 $DB->delete_records('scorm_seq_mapinfo', array('scoid'=>$olditem->id));
636                 $DB->delete_records('scorm_seq_ruleconds', array('scoid'=>$olditem->id));
637                 $DB->delete_records('scorm_seq_rulecond', array('scoid'=>$olditem->id));
638                 $DB->delete_records('scorm_seq_rolluprule', array('scoid'=>$olditem->id));
639                 $DB->delete_records('scorm_seq_rolluprulecond', array('scoid'=>$olditem->id));
640             }
641         }
642         if (empty($scoes->version)) {
643             $scoes->version = 'SCORM_1.2';
644         }
645         $DB->set_field('scorm', 'version', $scoes->version, array('id'=>$scorm->id));
646         $scorm->version = $scoes->version;
647     }
649     $scorm->launch = $launch;
651     return true;
654 function scorm_optionals_data($item, $standarddata) {
655     $result = array();
656     $sequencingdata = array('sequencingrules', 'rolluprules', 'objectives');
657     foreach ($item as $element => $value) {
658         if (! in_array($element, $standarddata)) {
659             if (! in_array($element, $sequencingdata)) {
660                 $result[] = $element;
661             }
662         }
663     }
664     return $result;
667 function scorm_is_leaf($sco) {
668     global $DB;
670     if ($DB->get_record('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->identifier))) {
671         return false;
672     }
673     return true;
676 function scorm_get_parent($sco) {
677     global $DB;
679     if ($sco->parent != '/') {
680         if ($parent = $DB->get_record('scorm_scoes', array('scorm'=>$sco->scorm, 'identifier'=>$sco->parent))) {
681             return scorm_get_sco($parent->id);
682         }
683     }
684     return null;
687 function scorm_get_children($sco) {
688     global $DB;
690     if ($children = $DB->get_records('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->identifier))) {//originally this said parent instead of childrean
691         return $children;
692     }
693     return null;
696 function scorm_get_available_children($sco) {  // TODO: undefined vars!!!
697     global $DB;
699     $res = $DB->get_record('scorm_scoes_track', array('scoid'=>$scoid,
700                                                      'userid'=>$userid,
701                                                      'element'=>'availablechildren'));
702     if (!$res || $res == null) {
703         return false;
704     } else {
705         return unserialize($res->value);
706     }
709 function scorm_get_available_descendent($descend = array(), $sco) {
710     if ($sco == null) {
711         return $descend;
712     } else {
713         $avchildren = scorm_get_available_children($sco);
714         foreach ($avchildren as $avchild) {
715             array_push($descend, $avchild);
716         }
717         foreach ($avchildren as $avchild) {
718             scorm_get_available_descendent($descend, $avchild);
719         }
720     }
723 function scorm_get_siblings($sco) {
724     global $DB;
726     if ($siblings = $DB->get_records('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->parent))) {
727         unset($siblings[$sco->id]);
728         if (!empty($siblings)) {
729             return $siblings;
730         }
731     }
732     return null;
734 //get an array that contains all the parent scos for this sco.
735 function scorm_get_ancestors($sco) {
736     $ancestors = array();
737     $continue = true;
738     while ($continue) {
739         $ancestor = scorm_get_parent($sco);
740         if (!empty($ancestor) && $ancestor->id !== $sco->id) {
741             $sco = $ancestor;
742             $ancestors[] = $ancestor;
743             if ($sco->parent == '/') {
744                 $continue = false;
745             }
746         } else {
747             $continue = false;
748         }
749     }
750     return $ancestors;
753 function scorm_get_preorder($preorder=array(), $sco) {
754     if ($sco != null) {
755         array_push($preorder, $sco);
756         $children = scorm_get_children($sco);
757         foreach ($children as $child) {
758             scorm_get_preorder($sco);
759         }
760     } else {
761         return $preorder;
762     }
765 function scorm_find_common_ancestor($ancestors, $sco) {
766     $pos = scorm_array_search('identifier', $sco->parent, $ancestors);
767     if ($sco->parent != '/') {
768         if ($pos === false) {
769             return scorm_find_common_ancestor($ancestors, scorm_get_parent($sco));
770         }
771     }
772     return $pos;
775 /* Usage
776  Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
778  $objXML = new xml2Array();
779  $arrOutput = $objXML->parse($strYourXML);
780  print_r($arrOutput); //print it out, or do whatever!
782 */
783 class xml2Array {
785     var $arrOutput = array();
786     var $resParser;
787     var $strXmlData;
789     /**
790      * Convert a utf-8 string to html entities
791      *
792      * @param string $str The UTF-8 string
793      * @return string
794      */
795     function utf8_to_entities($str) {
796         global $CFG;
798         $entities = '';
799         $values = array();
800         $lookingfor = 1;
802         return $str;
803     }
805     /**
806      * Parse an XML text string and create an array tree that rapresent the XML structure
807      *
808      * @param string $strInputXML The XML string
809      * @return array
810      */
811     function parse($strInputXML) {
812         $this->resParser = xml_parser_create ('UTF-8');
813         xml_set_object($this->resParser, $this);
814         xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
816         xml_set_character_data_handler($this->resParser, "tagData");
818         $this->strXmlData = xml_parse($this->resParser, $strInputXML );
819         if (!$this->strXmlData) {
820             die(sprintf("XML error: %s at line %d",
821             xml_error_string(xml_get_error_code($this->resParser)),
822             xml_get_current_line_number($this->resParser)));
823         }
825         xml_parser_free($this->resParser);
827         return $this->arrOutput;
828     }
830     function tagOpen($parser, $name, $attrs) {
831         $tag=array("name"=>$name, "attrs"=>$attrs);
832         array_push($this->arrOutput, $tag);
833     }
835     function tagData($parser, $tagData) {
836         if (trim($tagData)) {
837             if (isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
838                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $this->utf8_to_entities($tagData);
839             } else {
840                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $this->utf8_to_entities($tagData);
841             }
842         }
843     }
845     function tagClosed($parser, $name) {
846         $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
847         array_pop($this->arrOutput);
848     }