MDL-44753 SCORM: make correct selection of first sco - also fixes MDL-44896
[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 /**
18  * functions used by SCORM 1.2/2004 packages.
19  *
20  * @package    mod_scorm
21  * @copyright 1999 onwards Roberto Pinna
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 function scorm_get_resources($blocks) {
26     $resources = array();
27     foreach ($blocks as $block) {
28         if ($block['name'] == 'RESOURCES' && isset($block['children'])) {
29             foreach ($block['children'] as $resource) {
30                 if ($resource['name'] == 'RESOURCE') {
31                     $resources[addslashes_js($resource['attrs']['IDENTIFIER'])] = $resource['attrs'];
32                 }
33             }
34         }
35     }
36     return $resources;
37 }
39 function scorm_get_manifest($blocks, $scoes) {
40     global $OUTPUT;
41     static $parents = array();
42     static $resources;
44     static $manifest;
45     static $organization;
47     $manifestresourcesnotfound = array();
48     if (count($blocks) > 0) {
49         foreach ($blocks as $block) {
50             switch ($block['name']) {
51                 case 'METADATA':
52                     if (isset($block['children'])) {
53                         foreach ($block['children'] as $metadata) {
54                             if ($metadata['name'] == 'SCHEMAVERSION') {
55                                 if (empty($scoes->version)) {
56                                     if (isset($metadata['tagData']) && (preg_match("/^(1\.2)$|^(CAM )?(1\.3)$/", $metadata['tagData'], $matches))) {
57                                         $scoes->version = 'SCORM_'.$matches[count($matches)-1];
58                                     } else {
59                                         if (isset($metadata['tagData']) && (preg_match("/^2004 (3rd|4th) Edition$/", $metadata['tagData'], $matches))) {
60                                             $scoes->version = 'SCORM_1.3';
61                                         } else {
62                                             $scoes->version = 'SCORM_1.2';
63                                         }
64                                     }
65                                 }
66                             }
67                         }
68                     }
69                 break;
70                 case 'MANIFEST':
71                     $manifest = $block['attrs']['IDENTIFIER'];
72                     $organization = '';
73                     $resources = array();
74                     $resources = scorm_get_resources($block['children']);
75                     $scoes = scorm_get_manifest($block['children'], $scoes);
76                     if (empty($scoes->elements) || count($scoes->elements) <= 0) {
77                         foreach ($resources as $item => $resource) {
78                             if (!empty($resource['HREF'])) {
79                                 $sco = new stdClass();
80                                 $sco->identifier = $item;
81                                 $sco->title = $item;
82                                 $sco->parent = '/';
83                                 $sco->launch = $resource['HREF'];
84                                 $sco->scormtype = $resource['ADLCP:SCORMTYPE'];
85                                 $scoes->elements[$manifest][$organization][$item] = $sco;
86                             }
87                         }
88                     }
89                 break;
90                 case 'ORGANIZATIONS':
91                     if (!isset($scoes->defaultorg) && isset($block['attrs']['DEFAULT'])) {
92                         $scoes->defaultorg = $block['attrs']['DEFAULT'];
93                     }
94                     if (!empty($block['children'])) {
95                         $scoes = scorm_get_manifest($block['children'], $scoes);
96                     }
97                 break;
98                 case 'ORGANIZATION':
99                     $identifier = $block['attrs']['IDENTIFIER'];
100                     $organization = '';
101                     $scoes->elements[$manifest][$organization][$identifier] = new stdClass();
102                     $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
103                     $scoes->elements[$manifest][$organization][$identifier]->parent = '/';
104                     $scoes->elements[$manifest][$organization][$identifier]->launch = '';
105                     $scoes->elements[$manifest][$organization][$identifier]->scormtype = '';
107                     $parents = array();
108                     $parent = new stdClass();
109                     $parent->identifier = $identifier;
110                     $parent->organization = $organization;
111                     array_push($parents, $parent);
112                     $organization = $identifier;
114                     if (!empty($block['children'])) {
115                         $scoes = scorm_get_manifest($block['children'], $scoes);
116                     }
118                     array_pop($parents);
119                 break;
120                 case 'ITEM':
121                     $parent = array_pop($parents);
122                     array_push($parents, $parent);
124                     $identifier = $block['attrs']['IDENTIFIER'];
125                     $scoes->elements[$manifest][$organization][$identifier] = new stdClass();
126                     $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
127                     $scoes->elements[$manifest][$organization][$identifier]->parent = $parent->identifier;
128                     if (!isset($block['attrs']['ISVISIBLE'])) {
129                         $block['attrs']['ISVISIBLE'] = 'true';
130                     }
131                     $scoes->elements[$manifest][$organization][$identifier]->isvisible = $block['attrs']['ISVISIBLE'];
132                     if (!isset($block['attrs']['PARAMETERS'])) {
133                         $block['attrs']['PARAMETERS'] = '';
134                     }
135                     $scoes->elements[$manifest][$organization][$identifier]->parameters = $block['attrs']['PARAMETERS'];
136                     if (!isset($block['attrs']['IDENTIFIERREF'])) {
137                         $scoes->elements[$manifest][$organization][$identifier]->launch = '';
138                         $scoes->elements[$manifest][$organization][$identifier]->scormtype = 'asset';
139                     } else {
140                         $idref = $block['attrs']['IDENTIFIERREF'];
141                         $base = '';
142                         if (isset($resources[$idref]['XML:BASE'])) {
143                             $base = $resources[$idref]['XML:BASE'];
144                         }
145                         if (!isset($resources[$idref])) {
146                             $manifestresourcesnotfound[] = $idref;
147                             $scoes->elements[$manifest][$organization][$identifier]->launch = '';
148                         } else {
149                             $scoes->elements[$manifest][$organization][$identifier]->launch = $base.$resources[$idref]['HREF'];
150                             if (empty($resources[$idref]['ADLCP:SCORMTYPE'])) {
151                                 $resources[$idref]['ADLCP:SCORMTYPE'] = 'asset';
152                             }
153                             $scoes->elements[$manifest][$organization][$identifier]->scormtype = $resources[$idref]['ADLCP:SCORMTYPE'];
154                         }
155                     }
157                     $parent = new stdClass();
158                     $parent->identifier = $identifier;
159                     $parent->organization = $organization;
160                     array_push($parents, $parent);
162                     if (!empty($block['children'])) {
163                         $scoes = scorm_get_manifest($block['children'], $scoes);
164                     }
166                     array_pop($parents);
167                 break;
168                 case 'TITLE':
169                     $parent = array_pop($parents);
170                     array_push($parents, $parent);
171                     if (!isset($block['tagData'])) {
172                         $block['tagData'] = '';
173                     }
174                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->title = $block['tagData'];
175                 break;
176                 case 'ADLCP:PREREQUISITES':
177                     if ($block['attrs']['TYPE'] == 'aicc_script') {
178                         $parent = array_pop($parents);
179                         array_push($parents, $parent);
180                         if (!isset($block['tagData'])) {
181                             $block['tagData'] = '';
182                         }
183                         $scoes->elements[$manifest][$parent->organization][$parent->identifier]->prerequisites = $block['tagData'];
184                     }
185                 break;
186                 case 'ADLCP:MAXTIMEALLOWED':
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]->maxtimeallowed = $block['tagData'];
193                 break;
194                 case 'ADLCP:TIMELIMITACTION':
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]->timelimitaction = $block['tagData'];
201                 break;
202                 case 'ADLCP:DATAFROMLMS':
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]->datafromlms = $block['tagData'];
209                 break;
210                 case 'ADLCP:MASTERYSCORE':
211                     $parent = array_pop($parents);
212                     array_push($parents, $parent);
213                     if (!isset($block['tagData'])) {
214                         $block['tagData'] = '';
215                     }
216                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->masteryscore = $block['tagData'];
217                 break;
218                 case 'ADLCP:COMPLETIONTHRESHOLD':
219                     $parent = array_pop($parents);
220                     array_push($parents, $parent);
221                     if (!isset($block['attrs']['MINPROGRESSMEASURE'])) {
222                         $block['attrs']['MINPROGRESSMEASURE'] = '1.0';
223                     }
224                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->threshold = $block['attrs']['MINPROGRESSMEASURE'];
225                 break;
226                 case 'ADLNAV:PRESENTATION':
227                     $parent = array_pop($parents);
228                     array_push($parents, $parent);
229                     if (!empty($block['children'])) {
230                         foreach ($block['children'] as $adlnav) {
231                             if ($adlnav['name'] == 'ADLNAV:NAVIGATIONINTERFACE') {
232                                 foreach ($adlnav['children'] as $adlnavInterface) {
233                                     if ($adlnavInterface['name'] == 'ADLNAV:HIDELMSUI') {
234                                         if ($adlnavInterface['tagData'] == 'continue') {
235                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidecontinue = 1;
236                                         }
237                                         if ($adlnavInterface['tagData'] == 'previous') {
238                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideprevious = 1;
239                                         }
240                                         if ($adlnavInterface['tagData'] == 'exit') {
241                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexit = 1;
242                                         }
243                                         if ($adlnavInterface['tagData'] == 'exitAll') {
244                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideexitall = 1;
245                                         }
246                                         if ($adlnavInterface['tagData'] == 'abandon') {
247                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandon = 1;
248                                         }
249                                         if ($adlnavInterface['tagData'] == 'abandonAll') {
250                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hideabandonall = 1;
251                                         }
252                                         if ($adlnavInterface['tagData'] == 'suspendAll') {
253                                             $scoes->elements[$manifest][$parent->organization][$parent->identifier]->hidesuspendall = 1;
254                                         }
255                                     }
256                                 }
257                             }
258                         }
259                     }
260                 break;
261                 case 'IMSSS:SEQUENCING':
262                     $parent = array_pop($parents);
263                     array_push($parents, $parent);
264                     if (!empty($block['children'])) {
265                         foreach ($block['children'] as $sequencing) {
266                             if ($sequencing['name']=='IMSSS:CONTROLMODE') {
267                                 if (isset($sequencing['attrs']['CHOICE'])) {
268                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choice = $sequencing['attrs']['CHOICE'] == 'true'?1:0;
269                                 }
270                                 if (isset($sequencing['attrs']['CHOICEEXIT'])) {
271                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choiceexit = $sequencing['attrs']['CHOICEEXIT'] == 'true'?1:0;
272                                 }
273                                 if (isset($sequencing['attrs']['FLOW'])) {
274                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->flow = $sequencing['attrs']['FLOW'] == 'true'?1:0;
275                                 }
276                                 if (isset($sequencing['attrs']['FORWARDONLY'])) {
277                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->forwardonly = $sequencing['attrs']['FORWARDONLY'] == 'true'?1:0;
278                                 }
279                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'])) {
280                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptobjectinfo = $sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'] == 'true'?1:0;
281                                 }
282                                 if (isset($sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'])) {
283                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptprogressinfo = $sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'] == 'true'?1:0;
284                                 }
285                             }
286                             if ($sequencing['name'] == 'IMSSS:DELIVERYCONTROLS') {
287                                 if (isset($sequencing['attrs']['TRACKED'])) {
288                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->tracked = $sequencing['attrs']['TRACKED'] == 'true'?1:0;
289                                 }
290                                 if (isset($sequencing['attrs']['COMPLETIONSETBYCONTENT'])) {
291                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->completionsetbycontent = $sequencing['attrs']['COMPLETIONSETBYCONTENT'] == 'true'?1:0;
292                                 }
293                                 if (isset($sequencing['attrs']['OBJECTIVESETBYCONTENT'])) {
294                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivesetbycontent = $sequencing['attrs']['OBJECTIVESETBYCONTENT'] == 'true'?1:0;
295                                 }
296                             }
297                             if ($sequencing['name']=='ADLSEQ:CONSTRAINEDCHOICECONSIDERATIONS') {
298                                 if (isset($sequencing['attrs']['CONSTRAINCHOICE'])) {
299                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->constrainChoice = $sequencing['attrs']['CONSTRAINCHOICE'] == 'true'?1:0;
300                                 }
301                                 if (isset($sequencing['attrs']['PREVENTACTIVATION'])) {
302                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->preventactivation = $sequencing['attrs']['PREVENTACTIVATION'] == 'true'?1:0;
303                                 }
304                             }
305                             if ($sequencing['name']=='IMSSS:OBJECTIVES') {
306                                 $objectives = array();
307                                 foreach ($sequencing['children'] as $objective) {
308                                     $objectivedata = new stdClass();
309                                     $objectivedata->primaryobj = 0;
310                                     switch ($objective['name']) {
311                                         case 'IMSSS:PRIMARYOBJECTIVE':
312                                             $objectivedata->primaryobj = 1;
313                                         case 'IMSSS:OBJECTIVE':
314                                             $objectivedata->satisfiedbymeasure = 0;
315                                             if (isset($objective['attrs']['SATISFIEDBYMEASURE'])) {
316                                                 $objectivedata->satisfiedbymeasure = $objective['attrs']['SATISFIEDBYMEASURE']== 'true'?1:0;
317                                             }
318                                             $objectivedata->objectiveid = '';
319                                             if (isset($objective['attrs']['OBJECTIVEID'])) {
320                                                 $objectivedata->objectiveid = $objective['attrs']['OBJECTIVEID'];
321                                             }
322                                             $objectivedata->minnormalizedmeasure = 1.0;
323                                             if (!empty($objective['children'])) {
324                                                 $mapinfos = array();
325                                                 foreach ($objective['children'] as $objectiveparam) {
326                                                     if ($objectiveparam['name']=='IMSSS:MINNORMALIZEDMEASURE') {
327                                                         if (isset($objectiveparam['tagData'])) {
328                                                             $objectivedata->minnormalizedmeasure = $objectiveparam['tagData'];
329                                                         } else {
330                                                             $objectivedata->minnormalizedmeasure = 0;
331                                                         }
332                                                     }
333                                                     if ($objectiveparam['name']=='IMSSS:MAPINFO') {
334                                                         $mapinfo = new stdClass();
335                                                         $mapinfo->targetobjectiveid = '';
336                                                         if (isset($objectiveparam['attrs']['TARGETOBJECTIVEID'])) {
337                                                             $mapinfo->targetobjectiveid = $objectiveparam['attrs']['TARGETOBJECTIVEID'];
338                                                         }
339                                                         $mapinfo->readsatisfiedstatus = 1;
340                                                         if (isset($objectiveparam['attrs']['READSATISFIEDSTATUS'])) {
341                                                             $mapinfo->readsatisfiedstatus = $objectiveparam['attrs']['READSATISFIEDSTATUS'] == 'true'?1:0;
342                                                         }
343                                                         $mapinfo->writesatisfiedstatus = 0;
344                                                         if (isset($objectiveparam['attrs']['WRITESATISFIEDSTATUS'])) {
345                                                             $mapinfo->writesatisfiedstatus = $objectiveparam['attrs']['WRITESATISFIEDSTATUS'] == 'true'?1:0;
346                                                         }
347                                                         $mapinfo->readnormalizemeasure = 1;
348                                                         if (isset($objectiveparam['attrs']['READNORMALIZEDMEASURE'])) {
349                                                             $mapinfo->readnormalizemeasure = $objectiveparam['attrs']['READNORMALIZEDMEASURE'] == 'true'?1:0;
350                                                         }
351                                                         $mapinfo->writenormalizemeasure = 0;
352                                                         if (isset($objectiveparam['attrs']['WRITENORMALIZEDMEASURE'])) {
353                                                             $mapinfo->writenormalizemeasure = $objectiveparam['attrs']['WRITENORMALIZEDMEASURE'] == 'true'?1:0;
354                                                         }
355                                                         array_push($mapinfos, $mapinfo);
356                                                     }
357                                                 }
358                                                 if (!empty($mapinfos)) {
359                                                     $objectivesdata->mapinfos = $mapinfos;
360                                                 }
361                                             }
362                                         break;
363                                     }
364                                     array_push($objectives, $objectivedata);
365                                 }
366                                 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectives = $objectives;
367                             }
368                             if ($sequencing['name']=='IMSSS:LIMITCONDITIONS') {
369                                 if (isset($sequencing['attrs']['ATTEMPTLIMIT'])) {
370                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptLimit = $sequencing['attrs']['ATTEMPTLIMIT'];
371                                 }
372                                 if (isset($sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'])) {
373                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptAbsoluteDurationLimit = $sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'];
374                                 }
375                             }
376                             if ($sequencing['name']=='IMSSS:ROLLUPRULES') {
377                                 if (isset($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'])) {
378                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupobjectivesatisfied = $sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'] == 'true'?1:0;
379                                 }
380                                 if (isset($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'])) {
381                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupprogresscompletion = $sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'] == 'true'?1:0;
382                                 }
383                                 if (isset($sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'])) {
384                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivemeasureweight = $sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'];
385                                 }
387                                 if (!empty($sequencing['children'])) {
388                                     $rolluprules = array();
389                                     foreach ($sequencing['children'] as $sequencingrolluprule) {
390                                         if ($sequencingrolluprule['name']=='IMSSS:ROLLUPRULE' ) {
391                                             $rolluprule = new stdClass();
392                                             $rolluprule->childactivityset = 'all';
393                                             if (isset($sequencingrolluprule['attrs']['CHILDACTIVITYSET'])) {
394                                                 $rolluprule->childactivityset = $sequencingrolluprule['attrs']['CHILDACTIVITYSET'];
395                                             }
396                                             $rolluprule->minimumcount = 0;
397                                             if (isset($sequencingrolluprule['attrs']['MINIMUMCOUNT'])) {
398                                                 $rolluprule->minimumcount = $sequencingrolluprule['attrs']['MINIMUMCOUNT'];
399                                             }
400                                             $rolluprule->minimumpercent = 0.0000;
401                                             if (isset($sequencingrolluprule['attrs']['MINIMUMPERCENT'])) {
402                                                 $rolluprule->minimumpercent = $sequencingrolluprule['attrs']['MINIMUMPERCENT'];
403                                             }
404                                             if (!empty($sequencingrolluprule['children'])) {
405                                                 foreach ($sequencingrolluprule['children'] as $rolluproleconditions) {
406                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPCONDITIONS') {
407                                                         $conditions = array();
408                                                         $rolluprule->conditioncombination = 'all';
409                                                         if (isset($rolluproleconditions['attrs']['CONDITIONCOMBINATION'])) {
410                                                             $rolluprule->conditioncombination = $rolluproleconditions['attrs']['CONDITIONCOMBINATION'];
411                                                         }
412                                                         foreach ($rolluproleconditions['children'] as $rolluprulecondition) {
413                                                             if ($rolluprulecondition['name']=='IMSSS:ROLLUPCONDITION') {
414                                                                 $condition = new stdClass();
415                                                                 if (isset($rolluprulecondition['attrs']['CONDITION'])) {
416                                                                     $condition->cond = $rolluprulecondition['attrs']['CONDITION'];
417                                                                 }
418                                                                 $condition->operator = 'noOp';
419                                                                 if (isset($rolluprulecondition['attrs']['OPERATOR'])) {
420                                                                     $condition->operator = $rolluprulecondition['attrs']['OPERATOR'];
421                                                                 }
422                                                                 array_push($conditions, $condition);
423                                                             }
424                                                         }
425                                                         $rolluprule->conditions = $conditions;
426                                                     }
427                                                     if ($rolluproleconditions['name']=='IMSSS:ROLLUPACTION') {
428                                                         $rolluprule->rollupruleaction = $rolluproleconditions['attrs']['ACTION'];
429                                                     }
430                                                 }
431                                             }
432                                             array_push($rolluprules, $rolluprule);
433                                         }
434                                     }
435                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rolluprules = $rolluprules;
436                                 }
437                             }
439                             if ($sequencing['name']=='IMSSS:SEQUENCINGRULES') {
440                                 if (!empty($sequencing['children'])) {
441                                     $sequencingrules = array();
442                                     foreach ($sequencing['children'] as $conditionrules) {
443                                         $conditiontype = -1;
444                                         switch($conditionrules['name']) {
445                                             case 'IMSSS:PRECONDITIONRULE':
446                                                 $conditiontype = 0;
447                                             break;
448                                             case 'IMSSS:POSTCONDITIONRULE':
449                                                 $conditiontype = 1;
450                                             break;
451                                             case 'IMSSS:EXITCONDITIONRULE':
452                                                 $conditiontype = 2;
453                                             break;
454                                         }
455                                         if (!empty($conditionrules['children'])) {
456                                             $sequencingrule = new stdClass();
457                                             foreach ($conditionrules['children'] as $conditionrule) {
458                                                 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
459                                                     $ruleconditions = array();
460                                                     $sequencingrule->conditioncombination = 'all';
461                                                     if (isset($conditionrule['attrs']['CONDITIONCOMBINATION'])) {
462                                                         $sequencingrule->conditioncombination = $conditionrule['attrs']['CONDITIONCOMBINATION'];
463                                                     }
464                                                     foreach ($conditionrule['children'] as $rulecondition) {
465                                                         if ($rulecondition['name']=='IMSSS:RULECONDITION') {
466                                                             $condition = new stdClass();
467                                                             if (isset($rulecondition['attrs']['CONDITION'])) {
468                                                                 $condition->cond = $rulecondition['attrs']['CONDITION'];
469                                                             }
470                                                             $condition->operator = 'noOp';
471                                                             if (isset($rulecondition['attrs']['OPERATOR'])) {
472                                                                 $condition->operator = $rulecondition['attrs']['OPERATOR'];
473                                                             }
474                                                             $condition->measurethreshold = 0.0000;
475                                                             if (isset($rulecondition['attrs']['MEASURETHRESHOLD'])) {
476                                                                 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
477                                                             }
478                                                             $condition->referencedobjective = '';
479                                                             if (isset($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
480                                                                 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
481                                                             }
482                                                             array_push($ruleconditions, $condition);
483                                                         }
484                                                     }
485                                                     $sequencingrule->ruleconditions = $ruleconditions;
486                                                 }
487                                                 if ($conditionrule['name']=='IMSSS:RULEACTION') {
488                                                     $sequencingrule->action = $conditionrule['attrs']['ACTION'];
489                                                 }
490                                                 $sequencingrule->type = $conditiontype;
491                                             }
492                                             array_push($sequencingrules, $sequencingrule);
493                                         }
494                                     }
495                                     $scoes->elements[$manifest][$parent->organization][$parent->identifier]->sequencingrules = $sequencingrules;
496                                 }
497                             }
498                         }
499                     }
500                 break;
501             }
502         }
503     }
504     if (!empty($manifestresourcesnotfound)) {
505         //throw warning to user to let them know manifest contains references to resources that don't appear to exist.
506         if (!defined('DEBUGGING_PRINTED')) { //prevent redirect and display warning
507             define('DEBUGGING_PRINTED', 1);
508         }
509         echo $OUTPUT->notification(get_string('invalidmanifestresource', 'scorm').' '. implode(', ',$manifestresourcesnotfound));
510     }
511     return $scoes;
514 /**
515  * Sets up SCORM 1.2/2004 packages using the manifest file.
516  * Called whenever SCORM changes
517  * @param object $scorm instance - fields are updated and changes saved into database
518  * @param stored_file|string $manifest - path to manifest file or stored_file.
519  * @return bool
520  */
521 function scorm_parse_scorm(&$scorm, $manifest) {
522     global $CFG, $DB;
524     // load manifest into string
525     if ($manifest instanceof stored_file) {
526         $xmltext = $manifest->get_content();
527     } else {
528         require_once("$CFG->libdir/filelib.php");
529         $xmltext = download_file_content($manifest);
530     }
532     $defaultorgid = 0;
533     $firstinorg = 0;
535     $pattern = '/&(?!\w{2,6};)/';
536     $replacement = '&amp;';
537     $xmltext = preg_replace($pattern, $replacement, $xmltext);
539     $objXML = new xml2Array();
540     $manifests = $objXML->parse($xmltext);
541     $scoes = new stdClass();
542     $scoes->version = '';
543     $scoes = scorm_get_manifest($manifests, $scoes);
544     $newscoes = array();
545     $sortorder = 0;
546     if (count($scoes->elements) > 0) {
547         $olditems = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id));
548         foreach ($scoes->elements as $manifest => $organizations) {
549             foreach ($organizations as $organization => $items) {
550                 foreach ($items as $identifier => $item) {
551                     $sortorder++;
552                     // This new db mngt will support all SCORM future extensions
553                     $newitem = new stdClass();
554                     $newitem->scorm = $scorm->id;
555                     $newitem->manifest = $manifest;
556                     $newitem->organization = $organization;
557                     $newitem->sortorder = $sortorder;
558                     $standarddatas = array('parent', 'identifier', 'launch', 'scormtype', 'title');
559                     foreach ($standarddatas as $standarddata) {
560                         if (isset($item->$standarddata)) {
561                             $newitem->$standarddata = $item->$standarddata;
562                         } else {
563                             $newitem->$standarddata = '';
564                         }
565                     }
567                     if (!empty($defaultorgid) && empty($firstinorg) && $newitem->parent == $scoes->defaultorg) {
568                         $firstinorg = $sortorder;
569                     }
571                     if (!empty($olditems) && ($olditemid = scorm_array_search('identifier', $newitem->identifier, $olditems))) {
572                         $newitem->id = $olditemid;
573                         // Update the Sco sortorder but keep id so that user tracks are kept against the same ids.
574                         $DB->update_record('scorm_scoes', $newitem);
575                         $id = $olditemid;
576                         // Remove all old data so we don't duplicate it.
577                         $DB->delete_records('scorm_scoes_data', array('scoid'=>$olditemid));
578                         $DB->delete_records('scorm_seq_objective', array('scoid'=>$olditemid));
579                         $DB->delete_records('scorm_seq_mapinfo', array('scoid'=>$olditemid));
580                         $DB->delete_records('scorm_seq_ruleconds', array('scoid'=>$olditemid));
581                         $DB->delete_records('scorm_seq_rulecond', array('scoid'=>$olditemid));
582                         $DB->delete_records('scorm_seq_rolluprule', array('scoid'=>$olditemid));
583                         $DB->delete_records('scorm_seq_rolluprulecond', array('scoid'=>$olditemid));
585                         // Now remove this SCO from the olditems object as we have dealt with it.
586                         unset($olditems[$olditemid]);
587                     } else {
588                         // Insert the new SCO, and retain the link between the old and new for later adjustment
589                         $id = $DB->insert_record('scorm_scoes', $newitem);
590                     }
591                     $newscoes[$id] = $newitem; // Save this sco in memory so we can use it later.
593                     if ($optionaldatas = scorm_optionals_data($item, $standarddatas)) {
594                         $data = new stdClass();
595                         $data->scoid = $id;
596                         foreach ($optionaldatas as $optionaldata) {
597                             if (isset($item->$optionaldata)) {
598                                 $data->name =  $optionaldata;
599                                 $data->value = $item->$optionaldata;
600                                 $dataid = $DB->insert_record('scorm_scoes_data', $data);
601                             }
602                         }
603                     }
605                     if (isset($item->sequencingrules)) {
606                         foreach ($item->sequencingrules as $sequencingrule) {
607                             $rule = new stdClass();
608                             $rule->scoid = $id;
609                             $rule->ruletype = $sequencingrule->type;
610                             $rule->conditioncombination = $sequencingrule->conditioncombination;
611                             $rule->action = $sequencingrule->action;
612                             $ruleid = $DB->insert_record('scorm_seq_ruleconds', $rule);
613                             if (isset($sequencingrule->ruleconditions)) {
614                                 foreach ($sequencingrule->ruleconditions as $rulecondition) {
615                                     $rulecond = new stdClass();
616                                     $rulecond->scoid = $id;
617                                     $rulecond->ruleconditionsid = $ruleid;
618                                     $rulecond->referencedobjective = $rulecondition->referencedobjective;
619                                     $rulecond->measurethreshold = $rulecondition->measurethreshold;
620                                     $rulecond->operator = $rulecondition->operator;
621                                     $rulecond->cond = $rulecondition->cond;
622                                     $rulecondid = $DB->insert_record('scorm_seq_rulecond', $rulecond);
623                                 }
624                             }
625                         }
626                     }
628                     if (isset($item->rolluprules)) {
629                         foreach ($item->rolluprules as $rolluprule) {
630                             $rollup = new stdClass();
631                             $rollup->scoid =  $id;
632                             $rollup->childactivityset = $rolluprule->childactivityset;
633                             $rollup->minimumcount = $rolluprule->minimumcount;
634                             $rollup->minimumpercent = $rolluprule->minimumpercent;
635                             $rollup->rollupruleaction = $rolluprule->rollupruleaction;
636                             $rollup->conditioncombination = $rolluprule->conditioncombination;
638                             $rollupruleid = $DB->insert_record('scorm_seq_rolluprule', $rollup);
639                             if (isset($rollup->conditions)) {
640                                 foreach ($rollup->conditions as $condition) {
641                                     $cond = new stdClass();
642                                     $cond->scoid = $rollup->scoid;
643                                     $cond->rollupruleid = $rollupruleid;
644                                     $cond->operator = $condition->operator;
645                                     $cond->cond = $condition->cond;
646                                     $conditionid = $DB->insert_record('scorm_seq_rolluprulecond', $cond);
647                                 }
648                             }
649                         }
650                     }
652                     if (isset($item->objectives)) {
653                         foreach ($item->objectives as $objective) {
654                             $obj = new stdClass();
655                             $obj->scoid = $id;
656                             $obj->primaryobj = $objective->primaryobj;
657                             $obj->satisfiedbumeasure = $objective->satisfiedbymeasure;
658                             $obj->objectiveid = $objective->objectiveid;
659                             $obj->minnormalizedmeasure = trim($objective->minnormalizedmeasure);
660                             $objectiveid = $DB->insert_record('scorm_seq_objective', $obj);
661                             if (isset($objective->mapinfos)) {
662                                 foreach ($objective->mapinfos as $objmapinfo) {
663                                     $mapinfo = new stdClass();
664                                     $mapinfo->scoid = $id;
665                                     $mapinfo->objectiveid = $objectiveid;
666                                     $mapinfo->targetobjectiveid = $objmapinfo->targetobjectiveid;
667                                     $mapinfo->readsatisfiedstatus = $objmapinfo->readsatisfiedstatus;
668                                     $mapinfo->writesatisfiedstatus = $objmapinfo->writesatisfiedstatus;
669                                     $mapinfo->readnormalizedmeasure = $objmapinfo->readnormalizedmeasure;
670                                     $mapinfo->writenormalizedmeasure = $objmapinfo->writenormalizedmeasure;
671                                     $mapinfoid = $DB->insert_record('scorm_seq_mapinfo', $mapinfo);
672                                 }
673                             }
674                         }
675                     }
676                     if (empty($defaultorgid) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
677                         $defaultorgid = $id;
678                     }
679                 }
680             }
681         }
682         if (!empty($olditems)) {
683             foreach ($olditems as $olditem) {
684                 $DB->delete_records('scorm_scoes', array('id'=>$olditem->id));
685                 $DB->delete_records('scorm_scoes_data', array('scoid'=>$olditem->id));
686                 $DB->delete_records('scorm_scoes_track', array('scoid'=>$olditem->id));
687                 $DB->delete_records('scorm_seq_objective', array('scoid'=>$olditem->id));
688                 $DB->delete_records('scorm_seq_mapinfo', array('scoid'=>$olditem->id));
689                 $DB->delete_records('scorm_seq_ruleconds', array('scoid'=>$olditem->id));
690                 $DB->delete_records('scorm_seq_rulecond', array('scoid'=>$olditem->id));
691                 $DB->delete_records('scorm_seq_rolluprule', array('scoid'=>$olditem->id));
692                 $DB->delete_records('scorm_seq_rolluprulecond', array('scoid'=>$olditem->id));
693             }
694         }
695         if (empty($scoes->version)) {
696             $scoes->version = 'SCORM_1.2';
697         }
698         $DB->set_field('scorm', 'version', $scoes->version, array('id' => $scorm->id));
699         $scorm->version = $scoes->version;
700     }
701     $scorm->launch = 0;
702     // Check launch sco is valid.
703     if (!empty($defaultorgid) && isset($newscoes[$defaultorgid]) && !empty($newscoes[$defaultorgid]->launch)) {
704         // Launch param is valid - do nothing.
705         $scorm->launch = $defaultorgid;
706     } else if (!empty($defaultorgid) && isset($newscoes[$defaultorgid]) && empty($newscoes[$defaultorgid]->launch)) {
707         // The launch is probably the default org so we need to find the first launchable item inside this org.
708         $sqlselect = 'scorm = ? AND sortorder >= ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
709         // We use get_records here as we need to pass a limit in the query that works cross db.
710         $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id, $firstinorg), 'sortorder', 'id', 0, 1);
711         if (!empty($scoes)) {
712             $sco = reset($scoes); // We only care about the first record - the above query only returns one.
713             $scorm->launch = $sco->id;
714         }
715     }
716     if (empty($scorm->launch)) {
717         // No valid Launch is specified - find the first launchable sco instead.
718         $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
719         // We use get_records here as we need to pass a limit in the query that works cross db.
720         $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
721         if (!empty($scoes)) {
722             $sco = reset($scoes); // We only care about the first record - the above query only returns one.
723             $scorm->launch = $sco->id;
724         }
725     }
727     return true;
730 function scorm_optionals_data($item, $standarddata) {
731     $result = array();
732     $sequencingdata = array('sequencingrules', 'rolluprules', 'objectives');
733     foreach ($item as $element => $value) {
734         if (! in_array($element, $standarddata)) {
735             if (! in_array($element, $sequencingdata)) {
736                 $result[] = $element;
737             }
738         }
739     }
740     return $result;
743 function scorm_is_leaf($sco) {
744     global $DB;
746     if ($DB->record_exists('scorm_scoes', array('scorm' => $sco->scorm, 'parent' => $sco->identifier))) {
747         return false;
748     }
749     return true;
752 function scorm_get_parent($sco) {
753     global $DB;
755     if ($sco->parent != '/') {
756         if ($parent = $DB->get_record('scorm_scoes', array('scorm'=>$sco->scorm, 'identifier'=>$sco->parent))) {
757             return scorm_get_sco($parent->id);
758         }
759     }
760     return null;
763 function scorm_get_children($sco) {
764     global $DB;
766     $children = $DB->get_records('scorm_scoes', array('scorm' => $sco->scorm, 'parent' => $sco->identifier), 'sortorder, id');
767     if (!empty($children)) {
768         return $children;
769     }
770     return null;
773 function scorm_get_available_children($sco) {
774     global $DB;
776     $res = $DB->get_records('scorm_scoes', array('scorm' => $sco->scorm, 'parent' => $sco->identifier), 'sortorder, id');
777     if (!$res || $res == null) {
778         return false;
779     } else {
780         foreach ($res as $sco) {
781             $result[] = $sco;
782         }
783         return $result;
784     }
787 function scorm_get_available_descendent($descend = array(), $sco) {
788     if ($sco == null) {
789         return $descend;
790     } else {
791         $avchildren = scorm_get_available_children($sco);
792         foreach ($avchildren as $avchild) {
793             array_push($descend, $avchild);
794         }
795         foreach ($avchildren as $avchild) {
796             scorm_get_available_descendent($descend, $avchild);
797         }
798     }
801 function scorm_get_siblings($sco) {
802     global $DB;
804     if ($siblings = $DB->get_records('scorm_scoes', array('scorm'=>$sco->scorm, 'parent'=>$sco->parent), 'sortorder, id')) {
805         unset($siblings[$sco->id]);
806         if (!empty($siblings)) {
807             return $siblings;
808         }
809     }
810     return null;
812 //get an array that contains all the parent scos for this sco.
813 function scorm_get_ancestors($sco) {
814     $ancestors = array();
815     $continue = true;
816     while ($continue) {
817         $ancestor = scorm_get_parent($sco);
818         if (!empty($ancestor) && $ancestor->id !== $sco->id) {
819             $sco = $ancestor;
820             $ancestors[] = $ancestor;
821             if ($sco->parent == '/') {
822                 $continue = false;
823             }
824         } else {
825             $continue = false;
826         }
827     }
828     return $ancestors;
831 function scorm_get_preorder(&$preorder = array(), $sco = null) {
832     if ($sco != null) {
833         array_push($preorder, $sco);
834         if ($children = scorm_get_children($sco)) {
835             foreach ($children as $child) {
836                 scorm_get_preorder($preorder, $child);
837             }
838         }
839     }
840     return $preorder;
843 function scorm_find_common_ancestor($ancestors, $sco) {
844     $pos = scorm_array_search('identifier', $sco->parent, $ancestors);
845     if ($sco->parent != '/') {
846         if ($pos === false) {
847             return scorm_find_common_ancestor($ancestors, scorm_get_parent($sco));
848         }
849     }
850     return $pos;
853 /* Usage
854  Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
856  $objXML = new xml2Array();
857  $arrOutput = $objXML->parse($strYourXML);
858  print_r($arrOutput); //print it out, or do whatever!
860 */
861 class xml2Array {
863     var $arrOutput = array();
864     var $resParser;
865     var $strXmlData;
867     /**
868      * Convert a utf-8 string to html entities
869      *
870      * @param string $str The UTF-8 string
871      * @return string
872      */
873     function utf8_to_entities($str) {
874         global $CFG;
876         $entities = '';
877         $values = array();
878         $lookingfor = 1;
880         return $str;
881     }
883     /**
884      * Parse an XML text string and create an array tree that rapresent the XML structure
885      *
886      * @param string $strInputXML The XML string
887      * @return array
888      */
889     function parse($strInputXML) {
890         $this->resParser = xml_parser_create ('UTF-8');
891         xml_set_object($this->resParser, $this);
892         xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
894         xml_set_character_data_handler($this->resParser, "tagData");
896         $this->strXmlData = xml_parse($this->resParser, $strInputXML );
897         if (!$this->strXmlData) {
898             die(sprintf("XML error: %s at line %d",
899             xml_error_string(xml_get_error_code($this->resParser)),
900             xml_get_current_line_number($this->resParser)));
901         }
903         xml_parser_free($this->resParser);
905         return $this->arrOutput;
906     }
908     function tagOpen($parser, $name, $attrs) {
909         $tag=array("name"=>$name, "attrs"=>$attrs);
910         array_push($this->arrOutput, $tag);
911     }
913     function tagData($parser, $tagData) {
914         if (trim($tagData)) {
915             if (isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
916                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $this->utf8_to_entities($tagData);
917             } else {
918                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $this->utf8_to_entities($tagData);
919             }
920         }
921     }
923     function tagClosed($parser, $name) {
924         $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
925         array_pop($this->arrOutput);
926     }