|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 function scorm_eval_prerequisites($prerequisites, $usertracks) { 00027 00028 // this is really a little language parser - AICC_SCRIPT is the reference 00029 // see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec 00030 $element = ''; 00031 $stack = array(); 00032 $statuses = array( 00033 'passed' => 'passed', 00034 'completed' => 'completed', 00035 'failed' => 'failed', 00036 'incomplete' => 'incomplete', 00037 'browsed' => 'browsed', 00038 'not attempted' => 'notattempted', 00039 'p' => 'passed', 00040 'c' => 'completed', 00041 'f' => 'failed', 00042 'i' => 'incomplete', 00043 'b' => 'browsed', 00044 'n' => 'notattempted' 00045 ); 00046 $i=0; 00047 00048 // expand the amp entities 00049 $prerequisites = preg_replace('/&/', '&', $prerequisites); 00050 // find all my parsable tokens 00051 $prerequisites = preg_replace('/(&|\||\(|\)|\~)/', '\t$1\t', $prerequisites); 00052 // expand operators 00053 $prerequisites = preg_replace('/&/', '&&', $prerequisites); 00054 $prerequisites = preg_replace('/\|/', '||', $prerequisites); 00055 // now - grab all the tokens 00056 $elements = explode('\t', trim($prerequisites)); 00057 00058 // process each token to build an expression to be evaluated 00059 $stack = array(); 00060 foreach ($elements as $element) { 00061 $element = trim($element); 00062 if (empty($element)) { 00063 continue; 00064 } 00065 if (!preg_match('/^(&&|\|\||\(|\))$/', $element)) { 00066 // create each individual expression 00067 // search for ~ = <> X*{} 00068 00069 // sets like 3*{S34, S36, S37, S39} 00070 if (preg_match('/^(\d+)\*\{(.+)\}$/', $element, $matches)) { 00071 $repeat = $matches[1]; 00072 $set = explode(',', $matches[2]); 00073 $count = 0; 00074 foreach ($set as $setelement) { 00075 if (isset($usertracks[$setelement]) && 00076 ($usertracks[$setelement]->status == 'completed' || $usertracks[$setelement]->status == 'passed')) { 00077 $count++; 00078 } 00079 } 00080 if ($count >= $repeat) { 00081 $element = 'true'; 00082 } else { 00083 $element = 'false'; 00084 } 00085 00086 // ~ Not 00087 } else if ($element == '~') { 00088 $element = '!'; 00089 00090 // = | <> 00091 } else if (preg_match('/^(.+)(\=|<>)(.+)$/', $element, $matches)) { 00092 $element = trim($matches[1]); 00093 if (isset($usertracks[$element])) { 00094 $value = trim(preg_replace('/(\'|\")/', '', $matches[3])); 00095 if (isset($statuses[$value])) { 00096 $value = $statuses[$value]; 00097 } 00098 if ($matches[2] == '<>') { 00099 $oper = '!='; 00100 } else { 00101 $oper = '=='; 00102 } 00103 $element = '(\''.$usertracks[$element]->status.'\' '.$oper.' \''.$value.'\')'; 00104 } else { 00105 $element = 'false'; 00106 } 00107 00108 // everything else must be an element defined like S45 ... 00109 } else { 00110 if (isset($usertracks[$element]) && 00111 ($usertracks[$element]->status == 'completed' || $usertracks[$element]->status == 'passed')) { 00112 $element = 'true'; 00113 } else { 00114 $element = 'false'; 00115 } 00116 } 00117 00118 } 00119 $stack []= ' '.$element.' '; 00120 } 00121 return eval('return '.implode($stack).';'); 00122 }