MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / scorm / datamodels / sequencinglib.php
blob0ced1826a81043fa9b5c768f075e9bec958b589f
1 <?php // $Id$
2 require ('scormlib.php');
4 function scorm_seq_evaluate($scoid,$usertracks) {
5 return true;
8 function scorm_seq_overall ($scoid,$userid,$request,$attempt) {
9 $seq = scorm_seq_navigation($scoid,$userid,$request,$attempt);
10 if ($seq->navigation) {
11 if ($seq->termination != null) {
12 $seq = scorm_seq_termination($scoid,$userid,$seq);
14 if ($seq->sequencing != null) {
15 $seq = scorm_seq_sequencing($scoid,$userid,$seq);
16 if($seq->sequencing == 'exit'){//return the control to the LTS
17 return 'true';
20 if ($seq->delivery != null) {
21 $seq = scorm_sequencing_delivery($scoid,$userid,$seq);
22 $seq = scorm_content_delivery_environment ($seq,$userid);
25 if ($seq->exception != null) {
26 $seq = scorm_sequencing_exception($seq);
28 return 'true';
32 function scorm_seq_navigation ($scoid,$userid,$request,$attempt=0) {
33 /// Sequencing structure
34 $seq = new stdClass();
35 $seq->currentactivity = scorm_get_sco($scoid);
36 $seq->traversaldir = null;
37 $seq->nextactivity = null;
38 $seq->deliveryvalid = null;
39 $seq->attempt = $attempt;
41 $seq->identifiedactivity = null;
42 $seq->delivery = null;
43 $seq->deliverable = false;
44 $seq->active = scorm_seq_is('active',$scoid,$userid);
45 $seq->suspended = scorm_seq_is('suspended',$scoid,$userid);
46 $seq->navigation = null;
47 $seq->termination = null;
48 $seq->sequencing = null;
49 $seq->target = null;
50 $seq->endsession = null;
51 $seq->exception = null;
52 $seq->reachable = true;
53 $seq->prevact = true;
55 switch ($request) {
56 case 'start_':
57 if (empty($seq->currentactivity)) {
58 $seq->navigation = true;
59 $seq->sequencing = 'start';
60 } else {
61 $seq->exception = 'NB.2.1-1'; /// Sequencing session already begun
63 break;
64 case 'resumeall_':
65 if (empty($seq->currentactivity)) {
66 if ($track = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','suspendedactivity')) {//I think it's suspend instead of suspendedactivity
67 $seq->navigation = true;
68 $seq->sequencing = 'resumeall';
69 } else {
70 $seq->exception = 'NB.2.1-3'; /// No suspended activity found
72 } else {
73 $seq->exception = 'NB.2.1-1'; /// Sequencing session already begun
75 break;
76 case 'continue_':
77 case 'previous_':
78 if (!empty($seq->currentactivity)) {
79 $sco = $seq->currentactivity;
80 if ($sco->parent != '/') {
81 if ($parentsco = scorm_get_parent($sco)) {
83 if (isset($parentsco->flow) && ($parentsco->flow == true)) {//I think it's parentsco
84 // Current activity is active !
85 if (scorm_seq_is('active',$sco->id,$userid)) {
86 if ($request == 'continue_') {
87 $seq->navigation = true;
88 $seq->termination = 'exit';
89 $seq->sequencing = 'continue';
90 } else {
91 if (!isset($parentsco->forwardonly) || ($parentsco->forwardonly == false)) {
92 $seq->navigation = true;
93 $seq->termination = 'exit';
94 $seq->sequencing = 'previous';
95 } else {
96 $seq->exception = 'NB.2.1-5'; /// Violates control mode
104 } else {
105 $seq->exception = 'NB.2.1-2'; /// Current activity not defined
107 break;
108 case 'forward_':
109 case 'backward_':
110 $seq->exception = 'NB.2.1-7' ; /// None to be done, behavior not defined
111 break;
112 case 'exit_':
113 case 'abandon_':
114 if (!empty($seq->currentactivity)) {
115 // Current activity is active !
116 $seq->navigation = true;
117 $seq->termination = substr($request,0,-1);
118 $seq->sequencing = 'exit';
119 } else {
120 $seq->exception = 'NB.2.1-2'; /// Current activity not defined
122 case 'exitall_':
123 case 'abandonall_':
124 case 'suspendall_':
125 if (!empty($seq->currentactivity)) {
126 $seq->navigation = true;
127 $seq->termination = substr($request,0,-1);
128 $seq->sequencing = 'exit';
129 } else {
130 $seq->exception = 'NB.2.1-2'; /// Current activity not defined
132 break;
133 default: /// {target=<STRING>}choice
134 if ($targetsco = get_record('scorm_scoes','scorm',$sco->scorm,'identifier',$request)) {
135 if ($targetsco->parent != '/') {
136 $seq->target = $request;
137 } else {
138 if ($parentsco = scorm_get_parent($targetsco)) {
139 if (!isset($parentsco->choice) || ($parent->choice == true)) {
140 $seq->target = $request;
144 if ($seq->target != null) {
145 if (empty($seq->currentactivity)) {
146 $seq->navigation = true;
147 $seq->sequencing = 'choice';
148 } else {
149 if (!$sco = scorm_get_sco($scoid)) {
150 return $seq;
152 if ($sco->parent != $target->parent) {
153 $ancestors = scorm_get_ancestors($sco);
154 $commonpos = scorm_find_common_ancestor($ancestors,$targetsco);
155 if ($commonpos !== false) {
156 if ($activitypath = array_slice($ancestors,0,$commonpos)) {
157 foreach ($activitypath as $activity) {
158 if (scorm_seq_is('active',$activity->id,$userid) && (isset($activity->choiceexit) && ($activity->choiceexit == false))) {
159 $seq->navigation = false;
160 $seq->termination = null;
161 $seq->sequencing = null;
162 $seq->target = null;
163 $seq->exception = 'NB.2.1-8'; /// Violates control mode
164 return $seq;
167 } else {
168 $seq->navigation = false;
169 $seq->termination = null;
170 $seq->sequencing = null;
171 $seq->target = null;
172 $seq->exception = 'NB.2.1-9';
176 // Current activity is active !
177 $seq->navigation = true;
178 $seq->sequencing = 'choice';
180 } else {
181 $seq->exception = 'NB.2.1-10'; /// Violates control mode
183 } else {
184 $seq->exception = 'NB.2.1-11'; /// Target activity does not exists
186 break;
188 return $seq;
191 function scorm_seq_termination ($seq,$userid) {
192 if (empty($seq->currentactivity)) {
193 $seq->termination = false;
194 $seq->exception = 'TB.2.3-1';
195 return $seq;
198 $sco = $seq->currentactivity;
200 if ((($seq->termination == 'exit') || ($seq->termination == 'abandon')) && !$seq->active) {
201 $seq->termination = false;
202 $seq->exception = 'TB.2.3-2';
203 return $seq;
205 switch ($seq->termination) {
206 case 'exit':
207 scorm_seq_end_attempt($sco,$userid,$seq);
208 $seq = scorm_seq_exit_action_rules($seq,$userid);
209 do {
210 $exit = false;// I think this is false. Originally this was true
211 $seq = scorm_seq_post_cond_rules($seq,$userid);
212 if ($seq->termination == 'exitparent') {
213 if ($sco->parent != '/') {
214 $sco = scorm_get_parent($sco);
215 $seq->currentactivity = $sco;
216 $seq->active = scorm_seq_is('active',$sco->id,$userid);
217 scorm_seq_end_attempt($sco,$userid,$seq);
218 $exit = true;//I think it's true. Originally this was false
219 } else {
220 $seq->termination = false;
221 $seq->exception = 'TB.2.3-4';
222 return $seq;
225 } while (($exit == false) && ($seq->termination == 'exit'));
226 if ($seq->termination == 'exit') {
227 $seq->termination = true;
228 return $seq;
230 case 'exitall':
231 if ($seq->active) {
232 scorm_seq_end_attempt($sco,$userid,$seq);
234 /// Terminate Descendent Attempts Process
237 if ($ancestors = scorm_get_ancestors($sco)) {
238 foreach ($ancestors as $ancestor) {
239 scorm_seq_end_attempt($ancestor,$userid,$seq);
240 $seq->currentactivity = $ancestor;
244 $seq->active = scorm_seq_is('active',$seq->currentactivity->id,$userid);
245 $seq->termination = true;
246 $seq->sequencing = exit;
247 break;
248 case 'suspendall':
249 if (($seq->active) || ($seq->suspended)) {
250 scorm_seq_set('suspended',$sco->id,$userid);
251 } else {
252 if ($sco->parent != '/') {
253 $parentsco = scorm_get_parent($sco);
254 scorm_seq_set('suspended',$parentsco->id,$userid);
255 } else {
256 $seq->termination = false;
257 $seq->exception = 'TB.2.3-3';
258 // return $seq;
261 if ($ancestors = scorm_get_ancestors($sco)) {
262 foreach ($ancestors as $ancestor) {
263 scorm_seq_set('active',$ancestor->id,$userid,false);
264 scorm_seq_set('suspended',$ancestor->id,$userid);
265 $seq->currentactivity = $ancestor;
267 $seq->termination = true;
268 $seq->sequencing = 'exit';
269 } else {
270 $seq->termination = false;
271 $seq->exception = 'TB.2.3-5';
273 break;
274 case 'abandon':
275 scorm_seq_set('active',$sco->id,$userid,false);
276 $seq->active = null;
277 $seq->termination = true;
278 break;
279 case 'abandonall':
280 if ($ancestors = scorm_get_ancestors($sco)) {
281 foreach ($ancestors as $ancestor) {
282 scorm_seq_set('active',$ancestor->id,$userid,false);
283 $seq->currentactivity = $ancestor;
285 $seq->termination = true;
286 $seq->sequencing = 'exit';
287 } else {
288 $seq->termination = false;
289 $seq->exception = 'TB.2.3-6';
291 break;
292 default:
293 $seq->termination = false;
294 $seq->exception = 'TB.2.3-7';
295 break;
297 return $seq;
300 function scorm_seq_end_attempt($sco,$userid,$seq) {
301 if (scorm_is_leaf($sco)) {
302 if (!isset($sco->tracked) || ($sco->tracked == 1)) {
303 if (!scorm_seq_is('suspended',$sco->id,$userid)) {
304 if (!isset($sco->completionsetbycontent) || ($sco->completionsetbycontent == 0)) {
305 if (!scorm_seq_is('attemptprogressstatus',$sco->id,$userid,$seq->attempt)) {
306 // if (!scorm_seq_is('attemptprogressstatus',$sco->id,$userid)) {
307 scorm_seq_set('attemptprogressstatus',$sco->id,$userid);
308 scorm_seq_set('attemptcompletionstatus',$sco->id,$userid);
311 if (!isset($sco->objectivesetbycontent) || ($sco->objectivesetbycontent == 0)) {
312 if ($objectives = get_records('scorm_seq_objective','scoid',$sco->id)) {
313 foreach ($objectives as $objective) {
314 if ($objective->primaryobj) {
315 //if (!scorm_seq_objective_progress_status($sco,$userid,$objective)) {
316 if (!scorm_seq_is('objectiveprogressstatus',$sco->id,$userid)) {
317 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid);
318 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid);
326 } else {
327 if ($children = scorm_get_children($sco)) {
328 $suspended = false;
329 foreach ($children as $child) {
330 if (scorm_seq_is('suspended',$child,$userid)) {
331 $suspended = true;
332 break;
335 if ($suspended) {
336 scorm_seq_set('suspended',$sco,$userid);
337 } else {
338 scorm_seq_set('suspended',$sco,$userid,false);
342 scorm_seq_set('active',$sco,$userid,0,false);
343 scorm_seq_overall_rollup($sco,$userid);
346 function scorm_seq_is($what, $scoid, $userid, $attempt=0) {
348 /// Check if passed activity $what is active
349 $active = false;
350 if ($track = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'attempt',$attempt,'element',$what)) {
351 $active = true;
353 return $active;
356 function scorm_seq_set($what, $scoid, $userid, $attempt=0, $value='true') {
357 $sco = scorm_get_sco($scoid);
359 /// set passed activity to active or not
360 if ($value == false) {
361 delete_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'attempt',$attempt,'element',$what);
362 } else {
363 scorm_insert_track($userid, $sco->scorm, $sco->id, 0, $what, $value);
366 // update grades in gradebook
367 $scorm = get_record('scorm', 'id', $sco->scorm);
368 scorm_update_grades($scorm, $userid, true);
371 function scorm_seq_exit_action_rules($seq,$userid) {
372 $sco = $seq->currentactivity;
373 $ancestors = scorm_get_ancestors($sco);
374 $exittarget = null;
375 foreach (array_reverse($ancestors) as $ancestor) {
376 if (scorm_seq_rules_check($ancestor,'exit') != null) {
377 $exittarget = $ancestor;
378 break;
381 if ($exittarget != null) {
382 $commons = array_slice($ancestors,0,scorm_find_common_ancestor($ancestors,$exittarget));
384 /// Terminate Descendent Attempts Process
385 if ($commons) {
386 foreach ($commons as $ancestor) {
388 scorm_seq_end_attempt($ancestor,$userid,$seq->attempt);
389 $seq->currentactivity = $ancestor;
393 return $seq;
396 function scorm_seq_post_cond_rules($seq,$userid) {
397 $sco = $seq->currentactivity;
398 if (!$seq->suspended) {
399 if ($action = scorm_seq_rules_check($sco,'post') != null) {
400 switch($action) {
401 case 'retry':
402 case 'continue':
403 case 'previous':
404 $seq->sequencing = $action;
405 break;
406 case 'exitparent':
407 case 'exitall':
408 $seq->termination = $action;
409 break;
410 case 'retryall':
411 $seq->termination = 'exitall';
412 $seq->sequencing = 'retry';
413 break;
417 return $seq;
420 function scorm_seq_rules_check ($sco, $action){
421 $act = null;
422 if($rules = get_records('scorm_seq_ruleconds','scoid',$sco->id,'action',$action)){
423 foreach ($rules as $rule){
424 if($act = scorm_seq_rule_check($sco,$rule)){
425 return $act;
429 return $act;
433 function scorm_seq_rule_check ($sco, $rule){
434 $bag = Array();
435 $cond = '';
436 $ruleconds = get_records('scorm_seq_rulecond','scoid',$sco->id,'ruleconditionsid',$rule->id);
437 foreach ($ruleconds as $rulecond){
438 if ($rulecond->operator = 'not') {
439 if ($rulecond->cond != 'unknown' ){
440 $rulecond->cond = 'not'.$rulecond;
443 $bag [$rule->id] = $rulecond->cond;
446 if (empty($bag)){
447 $cond = 'unknown';
448 return $cond;
451 $size= sizeof($bag);
452 $i=0;
454 if ($rule->conditioncombination = 'all'){
455 foreach ($bag as $con){
456 $cond = $cond.' and '.$con;
460 else{
461 foreach ($bag as $con){
462 $cond = $cond.' or '.$con;
465 return $cond;
469 function scorm_seq_overall_rollup($sco,$userid){//Carlos
471 if ($ancestors = scorm_get_ancestors($sco)) {
472 foreach ($ancestors as $ancestor) {
473 if(!scorm_is_leaf($ancestor)){
474 scorm_seq_measure_rollup($sco,$userid);
476 scorm_seq_objective_rollup($sco,$userid);
477 scorm_seq_activity_progress_rollup($sco,$userid);
484 /* For this next function I have defined measure weight and measure status as records with the attempt = 0 on the scorm_scoes_track table. According to the page 89 of the SeqNav.pdf those datas give us some information about the progress of the objective*/
486 function scorm_seq_measure_rollup($sco,$userid){
488 $totalmeasure = 0; //Check if there is something similar in the database
489 $valid = false;//Same as in the last line
490 $countedmeasures = 0;//Same too
491 $targetobjective = null;
492 $readable = true;//to check if status and measure weight are readable
493 $objectives = get_records('scorm_seq_objective','scoid',$sco->id);
495 foreach ($objective as $objective){
497 if ($objective->primaryobj == true){//Objective contributes to rollup I'm using primaryobj field, but not
498 $targetobjective = $objective;
499 break;
504 if ($targetobjective != null){
505 $children = scorm_get_children($sco);
506 foreach ($children as $child){
507 $child = scorm_get_sco ($child);
508 if (!isset($child->tracked) || ($child->tracked == 1)){
510 $rolledupobjective = null;// we set the rolled up activity to undefined
511 $objectives = get_records('scorm_seq_objective','scoid',$child->id);
512 foreach ($objective as $objective){
513 if ($objective->primaryobj == true){//Objective contributes to rollup I'm using primaryobj field, but not
514 $rolledupobjective = $objective;
515 break;
518 if ($rolledupobjective != null){
519 $child = scorm_get_sco($child->id);
521 $countedmeasures = $countedmeasures + ($child->measureweight);
522 if (!scorm_seq_is('objectivemeasurestatus',$sco->id,$userid)) {
523 $normalizedmeasure = get_record('scorm_scoes_track','scoid',$child->id,'userid',$userid,'element','objectivenormalizedmeasure');
524 $totalmeasure = $totalmeasure + (($normalizedmeasure->value) * ($child->measureweight));
525 $valid = true;
535 if(!$valid){
537 scorm_seq_set('objectivemeasurestatus',$sco->id,$userid,false);
540 else{
541 if($countedmeasures>0){
542 scorm_seq_set('objectivemeasurestatus',$sco->id,$userid);
543 $val=$totalmeasure/$countedmeasure;
544 scorm_seq_set('objectivenormalizedmeasure',$sco->id,$userid,$val);
547 else{
548 scorm_seq_set('objectivemeasurestatus',$sco->id,$userid,false);
557 function scorm_seq_objective_rollup($sco,$userid){
559 scorm_seq_objective_rollup_measure($sco,$userid);
560 scorm_seq_objective_rollup_rules($sco,$userid);
561 scorm_seq_objective_rollup_default($sco,$userid);
564 if($targetobjective->satisfiedbymeasure){
565 scorm_seq_objective_rollup_measure($sco,$userid);
567 else{
568 if ((scorm_seq_rollup_rule_check($sco,$userid,'incomplete'))|| (scorm_seq_rollup_rule_check($sco,$userid,'completed'))){
569 scorm_seq_objective_rollup_rules($sco,$userid);
571 else{
573 $rolluprules = get_record('scorm_seq_rolluprule','scoid',$sco->id,'userid',$userid);
574 foreach($rolluprules as $rolluprule){
575 $rollupruleconds = get_records('scorm_seq_rolluprulecond','rollupruleid',$rolluprule->id);
576 foreach($rollupruleconds as $rolluprulecond){
578 switch ($rolluprulecond->cond!='satisfied' && $rolluprulecond->cond!='completed' && $rolluprulecond->cond!='attempted'){
580 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid, false);
582 break;
592 function scorm_seq_objective_rollup_measure($sco,$userid){
593 $targetobjective = null;
596 $objectives = get_records('scorm_seq_objective','scoid',$sco->id);
597 foreach ($objectives as $objective){
598 if ($objective->primaryobj == true){
599 $targetobjective = $objective;
600 break;
603 if ($targetobjective != null){
605 if($targetobjective->satisfiedbymeasure){
608 if (!scorm_seq_is('objectiveprogressstatus',$sco->id,$userid)) {
610 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid,false);
614 else{
615 if (scorm_seq_is('active',$sco->id,$userid)) {
616 $isactive = true;
618 else{
619 $isactive = false;
622 $normalizedmeasure = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','objectivenormalizedmeasure');
624 $sco = scorm_get_sco ($sco->id);
626 if (!$isactive || ($isactive && (!isset($sco->measuresatisfactionifactive) || $sco->measuresatisfactionifactive == true))){
627 if($normalizedmeasure->value >= $targetobjective->minnormalizedmeasure){
628 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid);
629 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid);
631 else{
632 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid);
633 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid,false);
636 else{
638 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid,false);
647 function scorm_seq_objective_rollup_default($sco,$userid){
648 if (!(scorm_seq_rollup_rule_check($sco,$userid,'incomplete')) && !(scorm_seq_rollup_rule_check($sco,$userid,'completed'))){
650 $rolluprules = get_record('scorm_seq_rolluprule','scoid',$sco->id,'userid',$userid);
651 foreach($rolluprules as $rolluprule){
652 $rollupruleconds = get_records('scorm_seq_rolluprulecond','rollupruleid',$rolluprule->id);
653 foreach($rollupruleconds as $rolluprulecond){
655 if ($rolluprulecond->cond!='satisfied' && $rolluprulecond->cond!='completed' && $rolluprulecond->cond!='attempted'){
657 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid, false);
659 break;
670 function scorm_seq_objective_rollup_rules($sco,$userid){
671 $targetobjective = null;
673 $objectives = get_records('scorm_seq_objective','scoid',$sco->id);
674 foreach ($objective as $objective){
675 if ($objective->primaryobj == true){//Objective contributes to rollup I'm using primaryobj field, but not
676 $targetobjective = $objective;
677 break;
680 if ($targetobjective != null){
684 if(scorm_seq_rollup_rule_check($sco,$userid,'notsatisfied')){//with not satisfied rollup for the activity
687 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid);
688 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid,false);
690 if(scorm_seq_rollup_rule_check($sco,$userid,'satisfied')){//with satisfied rollup for the activity
691 scorm_seq_set('objectiveprogressstatus',$sco->id,$userid);
692 scorm_seq_set('objectivesatisfiedstatus',$sco->id,$userid);
699 function scorm_seq_activity_progress_rollup ($sco, $userid){
701 if(scorm_seq_rollup_rule_check($sco,$userid,'incomplete')){
702 //incomplete rollup action
703 scorm_seq_set('attemptcompletionstatus',$sco->id,$userid,false,$seq->attempt);
704 scorm_seq_set('attemptprogressstatus',$sco->id,$userid,true,$seq->attempt);
707 if(scorm_seq_rollup_rule_check($sco,$userid,'completed')){
708 //incomplete rollup action
709 scorm_seq_set('attemptcompletionstatus',$sco->id,true,$userid);
710 scorm_seq_set('attemptprogressstatus',$sco->id,true,$userid);
715 function scorm_seq_rollup_rule_check ($sco,$userid,$action){
717 if($rolluprules = get_record('scorm_seq_rolluprule','scoid',$sco->id,'userid',$userid,'action',$action)){
719 $childrenbag = Array ();
720 $children = scorm_get_children ($sco);
722 foreach($rolluprules as $rolluprule){
724 foreach ($children as $child){
726 /*$tracked = get_records('scorm_scoes_track','scoid',$child->id,'userid',$userid);
727 if($tracked && $tracked->attemp != 0){*/
728 $child = scorm_get_sco ($child);
729 if (!isset($child->tracked) || ($child->tracked == 1)){
731 if(scorm_seq_check_child ($child,$action,$userid)){
733 $rollupruleconds = get_records('scorm_seq_rolluprulecond','rollupruleid',$rolluprule->id);
734 $evaluate = scorm_seq_evaluate_rollupcond($child,$rolluprule->conditioncombination,$rollupruleconds,$userid);
735 if ($evaluate=='unknown'){
736 array_push($childrenbag,'unknown');
738 else{
739 if($evaluate == true){
740 array_push($childrenbag,true);
742 else{
743 array_push($childrenbag,false);
750 $change = false;
752 switch ($rolluprule->childactivityset){
754 case 'all':
755 if((array_search(false,$childrenbag)===false)&&(array_search('unknown',$childrenbag)===false)){//I think I can use this condition instead equivalent to OR
756 $change = true;
758 break;
760 case 'any':
761 if(array_search(true,$childrenbag)!==false){//I think I can use this condition instead equivalent to OR
762 $change = true;
764 break;
766 case 'none':
767 if((array_search(true,$childrenbag)===false)&&(array_search('unknown',$childrenbag)===false)){//I think I can use this condition instead equivalent to OR
768 $change = true;
770 break;
772 case 'atleastcount':
773 foreach ($childrenbag as $itm){//I think I can use this condition instead equivalent to OR
774 $cont = 0;
775 if($itm === true){
776 $cont++;
778 if($cont >= $roullprule->minimumcount){
779 $change = true;
782 break;
784 case 'atleastcount':
785 foreach ($childrenbag as $itm){//I think I can use this condition instead equivalent to OR
786 $cont = 0;
787 if($itm === true){
788 $cont++;
790 if($cont >= $roullprule->minimumcount){
791 $change = true;
794 break;
796 case 'atleastpercent':
797 foreach ($childrenbag as $itm){//I think I can use this condition instead equivalent to OR
798 $cont = 0;
799 if($itm === true){
800 $cont++;
802 if(($cont/sizeof($childrenbag)) >= $roullprule->minimumcount){
803 $change = true;
806 break;
808 if ($change==true){
809 return true;
813 return false;
817 function scorm_seq_evaluate_rollupcond($sco,$conditioncombination,$rollupruleconds,$userid){
818 $bag = Array();
819 $con = "";
820 $val = false;
821 $unk = false;
822 foreach($rollupruleconds as $rolluprulecond){
824 $condit = scorm_evaluate_cond($rolluprulecond,$sco,$userid);
826 if($rule->operator=='not'){// If operator is not, negate the condition
827 if ($rule->cond != 'unknown'){
828 if ($condit){
829 $condit = false;
831 else{
832 $condit = true;
835 else{
836 $condit = 'unknown';
838 array_push($childrenbag,$condit);
842 if (empty($bag)){
843 return 'unknown';
845 else{
846 $i = 0;
847 foreach ($bag as $b){
849 if ($rolluprule->conditioncombination == 'all'){
851 $val = true;
852 if($b == 'unknown'){
853 $unk = true;
855 if($b === false){
856 return false;
860 else{
862 $val = false;
864 if($b == 'unknown'){
865 $unk = true;
867 if($b === true){
868 return true;
875 if ($unk){
876 return 'unknown';
878 return $val;
882 function scorm_evaluate_condition ($rolluprulecond,$sco,$userid){
884 $res = false;
886 switch ($rolluprulecond->cond){
888 case 'satisfied':
889 if($r=get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','objectivesatisfiedstatus')){
890 if($r->value == true){
891 if ($r=get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','objectiveprogressstatus')){
892 if($r->value == true){
893 $res= true;
898 break;
900 case 'objectiveStatusKnown':
901 if ($r=get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','objectiveprogressstatus')){
902 if($r->value == true){
903 $res= true;
906 break;
908 case 'objectiveMeasureKnown':
909 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','objectivemeasurestatus')){
910 if($r->value == true){
911 $res = true;
916 break;
918 case 'completed':
919 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','attemptcompletionstatus')){
920 if($r->value){
921 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','attemptprogressstatus')){
922 if($r->value){
923 $res = true;
930 break;
932 case 'attempted':
933 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityprogressstatus')){
934 if($r->value){
935 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityattemptcount')){
936 if($r->value > 0){
937 $res = true;
944 break;
947 case 'attemptLimitExceeded':
948 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityprogressstatus')){
949 if($r->value){
950 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','limitconditionattemptlimitcontrol')){
951 if($r->value){
952 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityattemptcount') && $r2 = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','limitconditionattemptlimit') ){
953 if($r->value >= $r2->value){
954 $res = true;
967 break;
969 case 'activityProgressKnown':
971 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityprogressstatus')){
972 if($r->value){
973 if ($r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','attemptprogressstatus')){
974 if($r->value){
975 $res = true;
984 break;
986 return $res;
990 function scorm_seq_check_child ($sco, $action, $userid){
991 $included = false;
992 $sco=scorm_get_sco($sco->id);
993 $r = get_record('scorm_scoes_track','scoid',$sco->id,'userid',$userid,'element','activityattemptcount');
994 if ($action == 'satisfied' || $action == 'notsatisfied'){
995 if (!$sco->rollupobjectivesatisfied){
996 $included = true;
997 if (($action == 'satisfied' && $sco->requiredforsatisfied == 'ifnotsuspended') || ($action == 'notsatisfied' && $sco->requiredfornotsatisfied == 'ifnotsuspended')){
999 if (!scorm_seq_is('activityprogressstatus',$sco->id,$userid) || ((($r->value)>0)&& !scorm_seq_is('suspended',$sco->id,$userid))){
1000 $included = false;
1004 else{
1005 if (($action == 'satisfied' && $sco->requiredforsatisfied == 'ifattempted') || ($action == 'notsatisfied' && $sco->requiredfornotsatisfied == 'ifattempted')){
1006 if (!scorm_seq_is('activityprogressstatus',$sco->id,$userid) || (($r->value) == 0)){
1007 $included = false;
1010 else{
1011 if (($action == 'satisfied' && $sco->requiredforsatisfied == 'ifnotskipped') || ($action == 'notsatisfied' && $sco->requiredfornotsatisfied == 'ifnotskipped')){
1012 $rulch = scorm_seq_rules_check($sco, 'skip');
1013 if ($rulch != null){
1014 $included = false;
1021 if ($action == 'completed' || $action == 'incomplete'){
1022 if (!$sco->rollupprogresscompletion){
1023 $included = true;
1025 if (($action == 'completed' && $sco->requiredforcompleted == 'ifnotsuspended') || ($action == 'incomplete' && $sco->requiredforincomplete == 'ifnotsuspended')){
1027 if (!scorm_seq_is('activityprogressstatus',$sco->id,$userid) || ( (($r->value)>0)&& !scorm_seq_is('suspended',$sco->id,$userid))){
1028 $included = false;
1032 else{
1034 if (($action == 'completed' && $sco->requiredforcompleted == 'ifattempted') || ($action == 'incomplete' && $sco->requiredforincomplete == 'ifattempted')){
1035 if (!scorm_seq_is('activityprogressstatus',$sco->id,$userid) || (($r->value)==0)){
1036 $included = false;
1040 else{
1041 if (($action == 'completed' && $sco->requiredforsatisfied == 'ifnotskipped') || ($action == 'incomplete' && $sco->requiredfornotsatisfied == 'ifnotskipped')){
1042 $rulch = scorm_seq_rules_check($sco, 'skip');
1043 if ($rulch != null){
1044 $included = false;
1054 return $included;
1058 function scorm_seq_sequencing ($scoid,$userid,$seq) {
1060 switch ($seq->sequencing) {
1062 case 'start':
1063 $seq = scorm_seq_start_sequencing($sco,$userid,$seq); //We'll see the parameters we have to send, this should update delivery and end
1064 $seq->sequencing = true;
1067 break;
1069 case 'resumeall':
1070 $seq = scorm_seq_resume_sequencing($sco,$userid,$seq); //We'll see the parameters we have to send, this should update delivery and end
1071 $seq->sequencing = true;
1075 break;
1077 case 'exit':
1078 $seq = scorm_seq_exit_sequencing($sco,$userid,$seq); //We'll see the parameters we have to send, this should update delivery and end
1079 $seq->sequencing = true;
1083 break;
1085 case 'retry':
1086 $seq = scorm_seq_retry_sequencing($sco,$userid,$seq); //We'll see the parameters we have to send, this should update delivery and end
1087 $seq->sequencing = true;
1090 break;
1092 case 'previous':
1093 $seq = scorm_seq_previous_sequencing($sco,$userid,$seq);// We'll see the parameters we have to send, this should update delivery and end
1094 $seq->sequencing = true;
1097 break;
1099 case 'choice':
1100 $seq = scorm_seq_choice_sequencing($sco,$userid,$seq);// We'll see the parameters we have to send, this should update delivery and end
1101 $seq->sequencing = true;
1104 break;
1108 if ($seq->exception != null){
1109 $seq->sequencing = false;
1110 return $seq;
1113 $seq->sequencing= true;
1114 return $seq;
1118 function scorm_seq_start_sequencing($scoid,$userid,$seq){
1119 if (!empty($seq->currentactivity)) {
1120 $seq->delivery = null;
1121 $seq->exception = 'SB.2.5-1';
1122 return $seq;
1124 $sco = get_record('scorm_scoes','scoid',$scoid,'userid',$userid);
1125 if (($sco->parent == '/') && scorm_is_leaf($sco)) {//if the activity is the root and is leaf
1126 $seq->delivery = $sco;
1128 else{
1129 $ancestors = scorm_get_ancestors($sco);
1130 $ancestorsroot = array_reverse($ancestors);
1131 $res = scorm_seq_flow($ancestorsroot[0],'forward',$seq,true,$userid);
1132 if($res){
1133 return $res;
1135 else{
1136 //return end and exception
1141 function scorm_seq_resume_all_sequencing($scoid,$userid,$seq){
1142 if (!empty($seq->currentactivity)){
1143 $seq->delivery = null;
1144 $seq->exception = 'SB.2.6-1';
1145 return $seq;
1147 $track = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','suspendedactivity');
1148 if (!$track) {
1149 $seq->delivery = null;
1150 $seq->exception = 'SB.2.6-2';
1151 return $seq;
1153 $seq->delivery = get_record('scorm_scoes','scoid',$scoid,'userid',$userid);//we assign the sco to the delivery
1157 function scorm_seq_continue_sequencing($scoid,$userid,$seq){
1158 if (empty($seq->currentactivity)) {
1159 $seq->delivery = null;
1160 $seq->exception = 'SB.2.7-1';
1161 return $seq;
1163 $currentact= $seq->currentactivity;
1164 if ($currentact->parent != '/') {//if the activity is the root and is leaf
1165 $parent = scorm_get_parent ($currentact);
1167 if (!isset($parent->flow) || ($parent->flow == false)) {
1168 $seq->delivery = null;
1169 $seq->exception = 'SB.2.7-2';
1170 return $seq;
1173 $res = scorm_seq_flow($currentact,'forward',$seq,false,$userid);
1174 if($res){
1175 return $res;
1177 else{
1178 //return end and exception
1184 function scorm_seq_previous_sequencing($scoid,$userid,$seq){
1185 if (empty($seq->currentactivity)) {
1186 $seq->delivery = null;
1187 $seq->exception = 'SB.2.8-1';
1188 return $seq;
1191 $currentact= $seq->currentactivity;
1192 if ($currentact->parent != '/') {//if the activity is the root and is leaf
1193 $parent = scorm_get_parent ($activity);
1194 if (!isset($parent->flow) || ($parent->flow == false)) {
1195 $seq->delivery = null;
1196 $seq->exception = 'SB.2.8-2';
1197 return $seq;
1200 $res = scorm_seq_flow($currentact,'backward',$seq,false,$userid);
1201 if($res){
1202 return $res;
1204 else{
1205 //return end and exception
1212 function scorm_seq_exit_sequencing($scoid,$userid,$seq){
1213 if (empty($seq->currentactivity)) {
1214 $seq->delivery = null;
1215 $seq->exception = 'SB.2.11-1';
1216 return $seq;
1219 if ($seq->active){
1220 $seq->endsession = false;
1221 $seq->exception = 'SB.2.11-2';
1222 return $seq;
1224 $currentact= $seq->currentactivity;
1225 if ($currentact->parent == '/'){
1226 $seq->endsession = true;
1227 return $seq;
1230 $seq->endsession = false;
1231 return $seq;
1235 function scorm_seq_retry_sequencing($scoid,$userid,$seq){
1236 if (empty($seq->currentactivity)) {
1237 $seq->delivery = null;
1238 $seq->exception = 'SB.2.10-1';
1239 return $seq;
1241 if ($seq->active || $seq->suspended){
1242 $seq->delivery = null;
1243 $seq->exception = 'SB.2.10-2';
1244 return $seq;
1247 if (!scorm_is_leaf($seq->currentactivity)){
1248 $res = scorm_seq_flow($seq->currentactivity,'forward',$seq,true,$userid);
1249 if($res != null){
1250 return $res;
1251 //return deliver
1253 else{
1254 $seq->delivery = null;
1255 $seq->exception = 'SB.2.10-3';
1256 return $seq;
1259 else{
1260 $seq->delivery = $seq->currentactivity;
1261 return $seq;
1266 function scorm_seq_flow ($candidate,$direction,$seq,$childrenflag,$userid){
1267 //$PREVDIRECTION NOT DEFINED YET
1269 $activity=$candidate;
1270 $deliverable=false;
1271 $previdirection = null;
1272 $seq = scorm_seq_flow_tree_traversal ($activity,$direction,$childrenflag,$prevdirection,$seq,$userid);
1273 if($seq->identifiedactivity == null){//if identifies
1274 $seq->identifiedactivity = $candidate;
1275 $seq->deliverable = false;
1276 return $seq;
1278 else{
1279 $activity = $seq->identifiedactivity;
1280 $seq = scorm_seq_flow_activity_traversal($activity,$userid,$direction,$childrenflag,$prevdirection,$seq,$userid);//
1281 return $seq;
1286 function scorm_seq_flow_activity_traversal ($activity, $userid, $direction, $childrenflag, $prevdirection, $seq,$userid){//returns the next activity on the tree, traversal direction, control returned to the LTS, (may) exception
1287 $activity = scorm_get_sco ($activity);
1288 $parent = scorm_get_parent ($activity);
1289 if (!isset($parent->flow) || ($parent->flow == false)) {
1290 $seq->deliverable = false;
1291 $seq->exception = 'SB.2.2-1';
1292 $seq->nextactivity = $activity;
1293 return $seq;
1296 $rulch = scorm_seq_rules_check($sco, 'skipped');
1297 if ($rulch != null){
1298 $seq = scorm_seq_flow_tree_traversal ($activity, $direction, false, $prevdirection, $seq,$userid);//endsession and exception
1299 if ($seq->identifiedactivity == null){
1300 $seq->deliverable = false;
1301 $seq->nextactivity = $activity;
1302 return $seq;
1304 else{
1306 if ($prevdirection = 'backward' && $seq->traversaldir == 'backward'){
1307 $seq = scorm_seq_flow_tree_traversal ($activity,$direction,false,null,$seq,$userid);
1308 $seq = scorm_seq_flow_activity($seq->identifiedactivity, $userid, $direction, $childrenflag, $prevdirection, $seq,$userid);
1310 else{
1311 $seq = scorm_seq_flow_tree_traversal ($activity,$direction,false,null,$seq,$userid);
1312 $seq = scorm_seq_flow_activity($seq->identifiedactivity, $userid, $direction, $childrenflag, $prevdirection, $seq,$userid);
1314 return $seq;
1318 $ch=scorm_check_activity ($activity,$userid);
1320 if ($ch){
1322 $seq->deliverable = false;
1323 $seq->exception = 'SB.2.2-2';
1324 $seq->nextactivity = $activity;
1325 return $seq;
1329 if (!scorm_is_leaf($activity)){
1331 $seq = scorm_seq_flow_tree_traversal ($activity,$direction,true,null,$seq,$userid);
1333 if ($seq->identifiedactivity == null){
1334 $seq->deliverable = false;
1335 $seq->nextactivity = $activity;
1336 return $seq;
1339 else{
1340 if($direction == 'backward' && $seq->traversaldir == 'forward'){
1341 $seq = scorm_seq_flow_activity($seq->identifiedactivity, $userid, 'forward', $childrenflag, 'backward', $seq,$userid);
1343 else{
1344 scorm_seq_flow_activity($seq->identifiedactivity, $userid, $direction, $childrenflag, null, $seq,$userid);
1346 return $seq;
1351 $seq->deliverable = true;
1352 $seq->nextactivity = $activity;
1353 return $seq;
1356 function scorm_seq_flow_tree_traversal ($activity,$direction,$childrenflag,$prevdirection,$seq,$userid){
1358 $revdirection = false;
1359 $parent = scorm_get_parent ($activity);
1360 $children = scorm_get_available_children ($parent);
1361 $siz = sizeof ($children);
1363 if (($prevdirection != null && $prevdirection == 'backward') && ($children[$siz-1]->id == $activity->id)){
1364 $direction = 'backward';
1365 $children[0] = $activity;
1366 $revdirection = true;
1369 if($direction = 'forward'){
1370 $ancestors = scorm_get_ancestors($activity);
1371 $ancestorsroot = array_reverse($ancestors);
1372 $preorder = scorm_get_preorder ($ancestorsroot);
1373 $siz= sizeof ($preorder);
1374 if (($activity->id == $preorder[$siz-1]->id) || (($activity->parent == '/') && !($childrenflag))){
1375 scorm_seq_terminate_descent($ancestorsroot,$userid);
1376 $seq->endsession = true;
1377 $seq->nextactivity = null;
1378 return $seq;
1380 if (scorm_is_leaf ($activity) || !$childrenflag){
1381 if ($children[$siz-1]->id == $activity->id){
1383 $seq = scorm_seq_flow_tree_traversal ($parent, $direction, false, null, $seq,$userid);
1384 // I think it's not necessary to do a return in here
1386 else{
1387 $parent = scorm_get_parent($activity);
1388 $children = scorm_get_available_children($parent);
1389 $seq->traversaldir = $direction;
1390 $sib = scorm_get_siblings($activity);
1391 $pos = array_search($sib, $activity);
1392 if ($pos !== false) {
1393 if ($pos != sizeof ($sib)){
1394 $seq->nextactivity = $sib [$pos+1];
1395 return $seq;
1397 else{
1398 $ch = scorm_get_children($sib[0]);
1399 $seq->nextactivity = $ch[0];
1400 return $seq;
1405 else{
1406 if (!empty ($children)){
1407 $seq->traversaldir = $direction;
1408 $seq->nextactivity = $children[0];
1409 return $seq;
1411 else{
1412 $seq->traversaldir = null;
1413 $seq->nextactivity = $children[0];
1414 $seq->exception = 'SB.2.1-2';
1415 return $seq;
1420 if($direction = 'backward'){
1422 if ($activity->parent == '/'){
1423 $seq->traversaldir = null;
1424 $seq->nextactivity = null;
1425 $seq->exception = 'SB.2.1-3';
1426 return $seq;
1428 if (scorm_is_leaf ($activity) || !$childrenflag){
1429 if (!$revdirection){
1430 if (isset($parent->forwardonly) && ($parent->forwardonly == true)) {
1431 $seq->traversaldir = null;
1432 $seq->nextactivity = null;
1433 $seq->exception = 'SB.2.1-4';
1434 return $seq;
1437 if ($children[0]->id == $activity->id){
1438 $seq = scorm_seq_flow_tree_traversal ($parent, 'backward', false, null, $seq);
1439 return $seq;
1441 else{
1442 $ancestors = scorm_get_ancestors($activity);
1443 $ancestorsroot = array_reverse ($ancestors);
1444 $preorder = scorm_get_preorder ($ancestorsroot);
1445 $pos = array_search($preorder, $children[$siz]);
1446 $preord = array_slice($preorder, 0, $pos-1);
1447 $revpreorder = array_reverse($preord);
1448 $position = array_search($revpreorder, $activity);
1449 $seq->nextactivity = $revpreorder[$pos+1];
1450 $seq->traversaldir = $direction;
1451 return $seq;
1454 else{
1455 if (!empty($children)){
1456 $activity = scorm_get_sco($activity->id);
1457 if (isset($parent->flow) && ($parent->flow == true)) {
1458 $children = scorm_get_children ($activity);
1459 $seq->traversaldir = 'forward';
1460 $seq->nextactivity = $children[0];
1461 return $seq;
1464 else{
1465 $children = scorm_get_children ($activity);
1466 $seq->traversaldir = 'backward';
1467 $seq->nextactivity = $children[sizeof($children)-1];
1468 return $seq;
1472 else{
1474 $seq->traversaldir = null;
1475 $seq->nextactivity = null;
1476 $seq->exception = 'SB.2.1-2';
1477 return $seq;
1485 function scorm_check_activity ($activity,$userid){
1486 $act = scorm_seq_rules_check($activity,'disabled');
1487 if ($act != null){
1488 return true;
1490 if(scorm_limit_cond_check ($activity,$userid)){
1491 return true;
1493 return false;
1498 function scorm_limit_cond_check ($activity,$userid){
1500 if (isset($activity->tracked) && ($activity->tracked == 0)){
1502 return false;
1505 if (scorm_seq_is('active',$activity->id,$userid) || scorm_seq_is('suspended',$activity->id,$userid)){
1506 return false;
1509 if (!isset($activity->limitcontrol) || ($activity->limitcontrol == 1)){
1510 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','activityattemptcount');
1511 if (scorm_seq_is('activityprogressstatus',$activity->id,$userid) && ($r->value >=$activity->limitattempt)){
1512 return true;
1516 if (!isset($activity->limitabsdurcontrol) || ($activity->limitabsdurcontrol == 1)){
1517 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','activityabsoluteduration');
1518 if (scorm_seq_is('activityprogressstatus',$activity->id,$userid) && ($r->value >=$activity->limitabsduration)){
1519 return true;
1523 if (!isset($activity->limitexpdurcontrol) || ($activity->limitexpdurcontrol == 1)){
1524 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','activityexperiencedduration');
1525 if (scorm_seq_is('activityprogressstatus',$activity->id,$userid) && ($r->value >=$activity->limitexpduration)){
1526 return true;
1530 if (!isset($activity->limitattabsdurcontrol) || ($activity->limitattabsdurcontrol == 1)){
1531 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','attemptabsoluteduration');
1532 if (scorm_seq_is('activityprogressstatus',$activity->id,$userid) && ($r->value >=$activity->limitattabsduration)){
1533 return true;
1537 if (!isset($activity->limitattexpdurcontrol) || ($activity->limitattexpdurcontrol == 1)){
1538 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','attemptexperiencedduration');
1539 if (scorm_seq_is('activityprogressstatus',$activity->id,$userid) && ($r->value >=$activity->limitattexpduration)){
1540 return true;
1544 if (!isset($activity->limitbegincontrol) || ($activity->limitbegincontrol == 1)){
1545 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','begintime');
1546 if (time()>=$activity->limitbegintime){
1547 return true;
1551 if (!isset($activity->limitbegincontrol) || ($activity->limitbegincontrol == 1)){
1552 if (time()<$activity->limitbegintime){
1553 return true;
1557 if (!isset($activity->limitendcontrol) || ($activity->limitendcontrol == 1)){
1559 if (time()>$activity->limitendtime){
1560 return true;
1563 return false;
1569 function scorm_seq_choice_sequencing($sco,$userid,$seq){
1571 $avchildren = Array ();
1572 $comancestor = null;
1573 $traverse = null;
1575 if ($sco == null){
1576 $seq->delivery = null;
1577 $seq->exception = 'SB.2.9-1';
1578 return $seq;
1581 $ancestors = scorm_get_ancestors($sco);
1582 $arrpath = array_reverse($ancestors);
1583 array_push ($arrpath,$sco);//path from the root to the target
1585 foreach ($arrpath as $activity){
1587 if ($activity->parent != '/') {
1588 $avchildren = scorm_get_available_children (scorm_get_parent($activity));
1589 $position = array_search($avchildren, $activity);
1590 if ($position !== false){
1591 $seq->delivery = null;
1592 $seq->exception = 'SB.2.9-2';
1593 return $seq;
1597 if (scorm_seq_rules_check($activity,'hidefromchoice' != null)){
1599 $seq->delivery = null;
1600 $seq->exception = 'SB.2.9-3';
1601 return $seq;
1607 if ($sco->parent != '/') {
1608 $parent = scorm_sco_get_parent ($sco);
1609 if ( isset($parent->choice) && ($parent->choice == false)){
1610 $seq->delivery = null;
1611 $seq->exception = 'SB.2.9-4';
1612 return $seq;
1616 if ($seq->currentactivity != null){
1617 $commonpos = scorm_find_common_ancestor($ancestors,$seq->currentactivity);
1618 $comancestor = $arrpath [$commonpos];
1620 else{
1621 $comancestor = $arrpath [0];
1624 if($seq->currentactivity === $sco) {
1625 break;
1628 $sib = scorm_get_siblings($seq->currentactivity);
1629 $pos = array_search($sib, $sco);
1631 if (pos !== false){
1633 $siblings = array_slice($sib, 0, $pos-1);
1635 if (empty($siblings)){
1637 $seq->delivery = null;
1638 $seq->exception = 'SB.2.9-5';
1639 return $seq;
1643 $children = scorm_get_children (scorm_get_parent ($sco));
1644 $pos1 = array_search($children, $sco);
1645 $pos2 = array_search($seq->currentactivity, $sco);
1646 if ($pos1>$pos2){
1647 $traverse = 'forward';
1649 else{
1650 $traverse = 'backward';
1653 foreach ($siblings as $sibling){
1654 $seq = scorm_seq_choice_activity_traversal($sibling,$userid,$seq,$traverse);
1655 if(!$seq->reachable){
1656 $seq->delivery = null;
1657 return $seq;
1660 break;
1664 if($seq->currentactivity == null || $seq->currentactivity == $comancestor){
1665 $commonpos = scorm_find_common_ancestor($ancestors,$seq->currentactivity);
1666 $comtarget = array_slice($ancestors, 1,$commonpos-1);//path from the common ancestor to the target activity
1667 $comtarget = array_reverse($comtarget);
1669 if (empty($comtarget)){
1670 $seq->delivery = null;
1671 $seq->exception = 'SB.2.9-5';
1672 return $seq;
1674 foreach ($comtarget as $act){
1675 $seq = scorm_seq_choice_activity_traversal($act,$userid,$seq,'forward');
1676 if(!$seq->reachable){
1677 $seq->delivery = null;
1678 return $seq;
1680 $act = scorm_get_sco ($acti->id);
1681 if(scorm_seq_is('active',$act->id,$userid) && ($act->id != $comancestor->id && $act->preventactivation)){//adlseq:can i write it like another property for the $seq object?
1682 $seq->delivery = null;
1683 $seq->exception = 'SB.2.9-6';
1684 return $seq;
1687 break;
1691 if ($comancestor->id == $sco->id){
1693 $ancestorscurrent = scorm_get_ancestors($seq->currentactivity);
1694 $possco = array_search ($ancestorscurrent, $sco);
1695 $curtarget = array_slice($ancestorscurrent,0,$possco);//path from the current activity to the target
1697 if (empty($curtarget)){
1698 $seq->delivery = null;
1699 $seq->exception = 'SB.2.9-5';
1700 return $seq;
1702 $i=0;
1703 foreach ($curtarget as $activ){
1704 $i++;
1705 if ($i != sizeof($curtarget)){
1706 if ( isset($activ->choiceexit) && ($activ->choiceexit == false)){
1707 $seq->delivery = null;
1708 $seq->exception = 'SB.2.9-7';
1709 return $seq;
1713 break;
1716 if (array_search ($ancestors, $comancestor)!== false){
1717 $ancestorscurrent = scorm_get_ancestors($seq->currentactivity);
1718 $commonpos = scorm_find_common_ancestor($ancestors,$sco);
1719 $curcommon = array_slice($ancestorscurrent,0,$commonpos-1);
1720 if(empty($curcommon)){
1721 $seq->delivery = null;
1722 $seq->exception = 'SB.2.9-5';
1723 return $seq;
1726 $constrained = null;
1727 foreach ($curcommon as $acti){
1728 $acti = scorm_get_sco($acti->id);
1729 if ( isset($acti->choiceexit) && ($acti->choiceexit == false)){
1730 $seq->delivery = null;
1731 $seq->exception = 'SB.2.9-7';
1732 return $seq;
1734 if ($constrained == null){
1735 if($acti->constrainchoice == true){
1736 $constrained = $acti;
1740 if ($constrained != null){
1741 $fwdir = scorm_get_preorder($constrained);
1743 if(array_search ($fwdir, $sco)!== false){
1744 $traverse = 'forward';
1746 else{
1747 $traverse = 'backward';
1749 $seq = scorm_seq_choice_flow ($constrained, $traverse, $seq);
1750 $actconsider = $seq->identifiedactivity;
1751 $avdescendents = Array();
1752 $avdescendents= scorm_get_available_descendents ($actconsider);
1753 if (array_search ($avdescendents, $sco) !== false && $sco->id != $actconsider->id && $constrained->id != $sco->id ){
1754 $seq->delivery = null;
1755 $seq->exception = 'SB.2.9-8';
1756 return $seq;
1759 //CONTINUE 11.5.5
1762 $commonpos = scorm_find_common_ancestor($ancestors,$seq->currentactivity);
1763 $comtarget = array_slice($ancestors, 1,$commonpos-1);//path from the common ancestor to the target activity
1764 $comtarget = array_reverse($comtarget);
1766 if (empty($comtarget)){
1767 $seq->delivery = null;
1768 $seq->exception = 'SB.2.9-5';
1769 return $seq;
1772 $fwdir = scorm_get_preorder($seq->currentactivity);
1774 if(array_search ($fwdir, $sco)!== false){
1776 foreach ($comtarget as $act){
1777 $seq = scorm_seq_choice_activity_traversal($act,$userid,$seq,'forward');
1778 if(!$seq->reachable){
1779 $seq->delivery = null;
1780 return $seq;
1782 $act = scorm_get_sco($act->id);
1783 if(scorm_seq_is('active',$act->id,$userid) && ($act->id != $comancestor->id && ($act->preventactivation == true))){
1784 $seq->delivery = null;
1785 $seq->exception = 'SB.2.9-6';
1786 return $seq;
1791 else{
1792 foreach ($comtarget as $act){
1793 $act = scorm_get_sco($act->id);
1794 if(scorm_seq_is('active',$act->id,$userid) && ($act->id != $comancestor->id && ($act->preventactivation==true))){
1795 $seq->delivery = null;
1796 $seq->exception = 'SB.2.9-6';
1797 return $seq;
1801 break;
1804 if(scorm_is_leaf ($sco)){
1805 $seq->delivery = $sco;
1806 $seq->exception = 'SB.2.9-6';
1807 return $seq;
1810 $seq = scorm_seq_flow ($sco,'forward',$seq,true,$userid);
1811 if ($seq->deliverable == false){
1812 scorm_terminate_descendent_attempts($comancestor,$userid,$seq);
1813 scorm_seq_end_attempt($comancestor,$userid,$seq->attempt);
1814 $seq->currentactivity = $sco;
1815 $seq->delivery = null;
1816 $seq->exception = 'SB.2.9-9';
1817 return $seq;
1820 else{
1821 return $seq;
1826 function scorm_seq_choice_flow ($constrained, $traverse, $seq){
1827 $seq = scorm_seq_choice_flow_tree ($constrained, $traverse, $seq);
1828 if ($seq->identifiedactivity == null){
1829 $seq->identifiedactivity = $constrained;
1830 return $seq;
1832 else{
1833 return $seq;
1837 function scorm_seq_choice_flow_tree ($constrained, $traverse, $seq){
1838 $islast = false;
1839 $parent = scorm_get_parent ($constrained);
1840 if ($traverse== 'forward'){
1841 $preord = scorm_get_preorder ($constrained);
1842 if (sizeof($preorder) == 0 || (sizeof($preorder) == 0 && $preorder[0]->id = $constrained->id)){
1843 $islast = true;//the function is the last activity available
1845 if ($constrained->parent == '/' || $islast){
1846 $seq->nextactivity = null;
1847 return $seq;
1849 $avchildren = scorm_get_available_children ($parent);//available children
1850 if ($avchildren [sizeof($avchildren)-1]->id == $constrained->id){
1851 $seq = scorm_seq_choice_flow_tree ($parent, 'forward', $seq);
1852 return $seq;
1854 else{
1855 $i=0;
1856 while(i < sizeof($avchildren)){
1857 if ($avchildren [i]->id == $constrained->id){
1858 $seq->nextactivity = $avchildren [i+1];
1859 return $seq;
1861 else{
1862 $i++;
1869 if ($traverse== 'backward'){
1870 if($constrained->parent == '/' ){
1871 $seq->nextactivity = null;
1872 return $seq;
1875 $avchildren = scorm_get_available_children ($parent);//available children
1876 if ($avchildren [0]->id == $constrained->id){
1877 $seq = scorm_seq_choice_flow_tree ($parent, 'backward', $seq);
1878 return $seq;
1880 else{
1881 $i=sizeof($avchildren)-1;
1882 while($i >=0){
1883 if ($avchildren [i]->id == $constrained->id){
1884 $seq->nextactivity = $avchildren [i-1];
1885 return $seq;
1887 else{
1888 $i--;
1894 function scorm_seq_choice_activity_traversal($activity,$userid,$seq,$direction){
1896 if($direction == 'forward'){
1898 $act = scorm_seq_rules_check($activity,'stopforwardtraversal');
1900 if($act != null){
1901 $seq->reachable = false;
1902 $seq->exception = 'SB.2.4-1';
1903 return $seq;
1905 $seq->reachable = false;
1906 return $seq;
1909 if($direction == 'backward'){
1910 $parentsco = scorm_get_parent($activity);
1911 if($parentsco!= null){
1912 if (isset($parentsco->forwardonly) && ($parentsco->forwardonly == true)){
1913 $seq->reachable = false;
1914 $seq->exception = 'SB.2.4-2';
1915 return $seq;
1917 else{
1918 $seq->reachable = false;
1919 $seq->exception = 'SB.2.4-3';
1920 return $seq;
1924 $seq->reachable = true;
1925 return $seq;
1929 //Delivery Request Process
1931 function scorm_sequencing_delivery($scoid,$userid,$seq){
1933 if(!scorm_is_leaf ($seq->delivery)){
1934 $seq->deliveryvalid = false;
1935 $seq->exception = 'DB.1.1-1';
1936 return $seq;
1938 $ancestors = scorm_get_ancestors($seq->delivery);
1939 $arrpath = array_reverse($ancestors);
1940 array_push ($arrpath,$seq->delivery);//path from the root to the target
1942 if (empty($arrpath)){
1943 $seq->deliveryvalid = false;
1944 $seq->exception = 'DB.1.1-2';
1945 return $seq;
1948 foreach ($arrpath as $activity){
1949 if(scorm_check_activity ($activity,$userid)){
1950 $seq->deliveryvalid = false;
1951 $seq->exception = 'DB.1.1-3';
1952 return $seq;
1956 $seq->deliveryvalid = true;
1957 return $seq;
1961 function scorm_content_delivery_environment ($seq,$userid){
1963 $act = $seq->currentactivity;
1964 if(scorm_seq_is('active',$act->id,$userid)){
1965 $seq->exception = 'DB.2-1';
1966 return $seq;
1968 $track = get_record('scorm_scoes_track','scoid',$act->id,'userid',$userid,'element','suspendedactivity');
1969 if ($track != null){
1970 $seq = scorm_clear_suspended_activity($seq->delivery, $seq);
1973 $seq = scorm_terminate_descendent_attempts ($seq->delivery,$userid,$seq);
1974 $ancestors = scorm_get_ancestors($seq->delivery);
1975 $arrpath = array_reverse($ancestors);
1976 array_push ($arrpath,$seq->delivery);
1977 foreach ($arrpath as $activity){
1978 if(!scorm_seq_is('active',$activity->id,$userid)){
1979 if(!isset($activity->tracked) || ($activity->tracked == 1)){
1980 if(!scorm_seq_is('suspended',$activity->id,$userid)){
1981 $r = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','activityattemptcount');
1982 $r->value = ($r->value)+1;
1983 update_record ('scorm_scoes_track',$r);
1984 if ($r->value == 1){
1985 scorm_seq_set('activityprogressstatus', $activity->id, $userid, 'true');
1987 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'objectiveprogressstatus', 'false');
1988 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'objectivesatisfiedstatus', 'false');
1989 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'objectivemeasurestatus', 'false');
1990 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'objectivenormalizedmeasure', 0.0);
1992 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'attemptprogressstatus', 'false');
1993 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'attemptcompletionstatus', 'false');
1994 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'attemptabsoluteduration', 0.0);
1995 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'attemptexperiencedduration', 0.0);
1996 scorm_insert_track($userid, $activity->scorm, $activity->id, 0, 'attemptcompletionamount', 0.0);
1999 scorm_seq_set('active', $activity->id, $userid, 'true');
2002 $seq->delivery = $seq->currentactivity;
2003 scorm_seq_set('suspendedactivity', $activity->id, $userid, 'false');
2005 //ONCE THE DELIVERY BEGINS (How should I check that?)
2007 if(isset($activity->tracked) || ($activity->tracked == 0)){
2008 //How should I track the info and what should I do to not record the information for the activity during delivery?
2009 $atabsdur = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','attemptabsoluteduration');
2010 $atexpdur = get_record('scorm_scoes_track','scoid',$activity->id,'userid',$userid,'element','attemptexperiencedduration');
2012 return $seq;
2016 function scorm_clear_suspended_activity($act,$seq){
2017 $currentact= $seq->currentactivity;
2018 $track = get_record('scorm_scoes_track','scoid',$currentact->id,'userid',$userid,'element','suspendedactivity');
2019 if ($track != null){
2020 $ancestors = scorm_get_ancestors($act);
2021 $commonpos = scorm_find_common_ancestor($ancestors,$currentact);
2022 if ($commonpos !== false) {
2023 if ($activitypath = array_slice($ancestors,0,$commonpos)) {
2024 if (!empty ($activitypath)){
2026 foreach ($activitypath as $activity) {
2027 if (scorm_is_leaf($activity)){
2028 scorm_seq_set('suspended',$activity->id,$userid,false);
2030 else{
2031 $children = scorm_get_children($activity);
2032 $bool= false;
2033 foreach ($children as $child){
2034 if(scorm_seq_is('suspended',$child->id,$userid)){
2035 $bool= true;
2038 if(!$bool){
2039 scorm_seq_set('suspended',$activity->id,$userid,false);
2046 scorm_seq_set('suspendedactivity',$act->id,$userid,false);
2051 function scorm_select_children_process($scoid,$userid){
2053 $sco = scorm_get_sco($scoid);
2054 if (!scorm_is_leaf($sco)){
2055 if(!scorm_seq_is('suspended',$scoid,$userid) && !scorm_seq_is('active',$scoid,$userid)){
2056 $r = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','selectiontiming');
2058 switch($r->value) {
2060 case 'oneachnewattempt':
2061 case 'never':
2062 break;
2064 case 'once':
2065 if(!scorm_seq_is('activityprogressstatus',$scoid,$userid)){
2066 if(scorm_seq_is('selectioncountsstatus',$scoid,$userid)){
2067 $childlist = '';
2068 $res = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','selectioncount');
2069 $i = ($res->value)-1;
2070 $children = scorm_get_children ($sco);
2072 while (i>=0){
2073 $pos = array_rand($children);
2074 array_push($childlist,$children [$pos]);
2075 array_splice($children,$pos,1);
2076 $i--;
2078 sort ($childlist);
2079 $clist = serialize ($childlist);
2080 scorm_seq_set('availablechildren', $scoid, $userid, false);
2081 scorm_seq_set('availablechildren', $scoid, $userid, $clist);
2086 break;
2094 function scorm_randomize_children_process($scoid,$userid){
2096 $sco = scorm_get_sco($scoid);
2097 if (!scorm_is_leaf($sco)){
2098 if(!scorm_seq_is('suspended',$scoid,$userid) && !scorm_seq_is('active',$scoid,$userid)){
2099 $r = get_record('scorm_scoes_track','scoid',$scoid,'userid',$userid,'element','randomizationtiming');
2101 switch($r->value) {
2104 case 'never':
2105 break;
2107 case 'oneachnewattempt':
2108 case 'once':
2109 if(!scorm_seq_is('activityprogressstatus',$scoid,$userid)){
2110 if(scorm_seq_is('randomizechildren',$scoid,$userid)){
2111 $childlist = array();
2112 $res = scorm_get_available_children($sco);
2113 $i = sizeof($res)-1;
2114 $children = $res->value;
2116 while (i>=0){
2117 $pos = array_rand($children);
2118 array_push($childlist,$children [$pos]);
2119 array_splice($children,$pos,1);
2120 $i--;
2123 $clist = serialize ($childlist);
2124 scorm_seq_set('availablechildren', $scoid, $userid, false);
2125 scorm_seq_set('availablechildren', $scoid, $userid, $clist);
2130 break;
2140 function scorm_terminate_descendent_attempts ($activity,$userid,$seq){
2141 $ancestors = scorm_get_ancestors($seq->currentactivity);
2142 $commonpos = scorm_find_common_ancestor($ancestors,$activity);
2143 if ($commonpos !== false) {
2144 if ($activitypath = array_slice($ancestors,1,$commonpos-2)) {
2145 if (!empty ($activitypath)){
2147 foreach ($activitypath as $sco) {
2148 scorm_seq_end_attempt($sco,$userid,$seq->attempt);
2156 function scorm_sequencing_exception($seq){
2157 if($seq->exception != null){
2158 switch($seq->exception){
2160 case 'NB.2.1-1':
2161 notify("Sequencing session has already begun");
2162 break;
2163 case 'NB.2.1-2':
2164 notify("Sequencing session has not begun");
2165 break;
2166 case 'NB.2.1-3':
2167 notify("Suspended activity is not defined");
2168 break;
2169 case 'NB.2.1-4':
2170 notify("Flow Sequencing Control Model Violation");
2171 break;
2172 case 'NB.2.1-5':
2173 notify("Flow or Forward only Sequencing Control Model Violation");
2174 break;
2175 case 'NB.2.1-6':
2176 notify("No activity is previous to the root");
2177 break;
2178 case 'NB.2.1-7':
2179 notify("Unsupported Navigation Request");
2180 break;
2181 case 'NB.2.1-8':
2182 notify("Choice Exit Sequencing Control Model Violation");
2183 break;
2184 case 'NB.2.1-9':
2185 notify("No activities to consider");
2186 break;
2187 case 'NB.2.1-10':
2188 notify("Choice Sequencing Control Model Violation");
2189 break;
2190 case 'NB.2.1-11':
2191 notify("Target Activity does not exist");
2192 break;
2193 case 'NB.2.1-12':
2194 notify("Current Activity already terminated");
2195 break;
2196 case 'NB.2.1-13':
2197 notify("Undefined Navigation Request");
2198 break;
2200 case 'TB.2.3-1':
2201 notify("Current Activity already terminated");
2202 break;
2203 case 'TB.2.3-2':
2204 notify("Current Activity already terminated");
2205 break;
2206 case 'TB.2.3-4':
2207 notify("Current Activity already terminated");
2208 break;
2209 case 'TB.2.3-5':
2210 notify("Nothing to suspend; No active activities");
2211 break;
2212 case 'TB.2.3-6':
2213 notify("Nothing to abandon; No active activities");
2214 break;
2216 case 'SB.2.1-1':
2217 notify("Last activity in the tree");
2218 break;
2219 case 'SB.2.1-2':
2220 notify("Cluster has no available children");
2221 break;
2222 case 'SB.2.1-3':
2223 notify("No activity is previous to the root");
2224 break;
2225 case 'SB.2.1-4':
2226 notify("Forward Only Sequencing Control Model Violation");
2227 break;
2229 case 'SB.2.2-1':
2230 notify("Flow Sequencing Control Model Violation");
2231 break;
2232 case 'SB.2.2-2':
2233 notify("Activity unavailable");
2234 break;
2236 case 'SB.2.3-1':
2237 notify("Forward Traversal Blocked");
2238 break;
2239 case 'SB.2.3-2':
2240 notify("Forward Only Sequencing Control Model Violation");
2241 break;
2242 case 'SB.2.3-3':
2243 notify("No activity is previous to the root");
2244 break;
2246 case 'SB.2.5-1':
2247 notify("Sequencing session has already begun");
2248 break;
2250 case 'SB.2.6-1':
2251 notify("Sequencing session has already begun");
2252 break;
2253 case 'SB.2.6-2':
2254 notify("No Suspended activity is defined");
2255 break;
2257 case 'SB.2.7-1':
2258 notify("Sequencing session has not begun");
2259 break;
2260 case 'SB.2.7-2':
2261 notify("Flow Sequencing Control Model Violation");
2262 break;
2264 case 'SB.2.8-1':
2265 notify("Sequencing session has not begun");
2266 break;
2267 case 'SB.2.8-2':
2268 notify("Flow Sequencing Control Model Violation");
2269 break;
2271 case 'SB.2.9-1':
2272 notify("No target for Choice");
2273 break;
2274 case 'SB.2.9-2':
2275 notify("Target Activity does not exist or is unavailable");
2276 break;
2277 case 'SB.2.9-3':
2278 notify("Target Activity hidden from choice");
2279 break;
2280 case 'SB.2.9-4':
2281 notify("Choice Sequencing Control Model Violation");
2282 break;
2283 case 'SB.2.9-5':
2284 notify("No activities to consider");
2285 break;
2286 case 'SB.2.9-6':
2287 notify("Unable to activate target; target is not a child of the Current Activity");
2288 break;
2289 case 'SB.2.9-7':
2290 notify("Choice Exit Sequencing Control Model Violation");
2291 break;
2292 case 'SB.2.9-8':
2293 notify("Unable to choose target activity - constrained choice");
2294 break;
2295 case 'SB.2.9-9':
2296 notify("Choice Request Prevented by Flow-only Activity");
2297 break;
2299 case 'SB.2.10-1':
2300 notify("Sequencing session has not begun");
2301 break;
2302 case 'SB.2.10-2':
2303 notify("Current Activity is active or suspended");
2304 break;
2305 case 'SB.2.10-3':
2306 notify("Flow Sequencing Control Model Violation");
2307 break;
2309 case 'SB.2.11-1':
2310 notify("Sequencing session has not begun");
2311 break;
2312 case 'SB.2.11-2':
2313 notify("Current Activity has not been terminated");
2314 break;
2316 case 'SB.2.12-2':
2317 notify("Undefined Sequencing Request");
2318 break;
2320 case 'DB.1.1-1':
2321 notify("Cannot deliver a non-leaf activity");
2322 break;
2323 case 'DB.1.1-2':
2324 notify("Nothing to deliver");
2325 break;
2326 case 'DB.1.1-3':
2327 notify("Activity unavailable");
2328 break;
2330 case 'DB.2-1':
2331 notify("Identified activity is already active");
2332 break;