/mod/scorm/datamodels/scorm_12lib.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 122 lines · 77 code · 9 blank · 36 comment · 26 complexity · 6de965c5f6b8ad04207406868f86d4b2 MD5 · raw file

  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/>.
  16. /**
  17. * This is really a little language parser for AICC_SCRIPT
  18. * evaluates the expression and returns a boolean answer
  19. * see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec (CAM).
  20. *
  21. * @param string $prerequisites the aicc_script prerequisites expression
  22. * @param array $usertracks the tracked user data of each SCO visited
  23. * @return boolean
  24. */
  25. function scorm_eval_prerequisites($prerequisites, $usertracks) {
  26. // this is really a little language parser - AICC_SCRIPT is the reference
  27. // see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec
  28. $element = '';
  29. $stack = array();
  30. $statuses = array(
  31. 'passed' => 'passed',
  32. 'completed' => 'completed',
  33. 'failed' => 'failed',
  34. 'incomplete' => 'incomplete',
  35. 'browsed' => 'browsed',
  36. 'not attempted' => 'notattempted',
  37. 'p' => 'passed',
  38. 'c' => 'completed',
  39. 'f' => 'failed',
  40. 'i' => 'incomplete',
  41. 'b' => 'browsed',
  42. 'n' => 'notattempted'
  43. );
  44. $i=0;
  45. // expand the amp entities
  46. $prerequisites = preg_replace('/&amp;/', '&', $prerequisites);
  47. // find all my parsable tokens
  48. $prerequisites = preg_replace('/(&|\||\(|\)|\~)/', '\t$1\t', $prerequisites);
  49. // expand operators
  50. $prerequisites = preg_replace('/&/', '&&', $prerequisites);
  51. $prerequisites = preg_replace('/\|/', '||', $prerequisites);
  52. // now - grab all the tokens
  53. $elements = explode('\t', trim($prerequisites));
  54. // process each token to build an expression to be evaluated
  55. $stack = array();
  56. foreach ($elements as $element) {
  57. $element = trim($element);
  58. if (empty($element)) {
  59. continue;
  60. }
  61. if (!preg_match('/^(&&|\|\||\(|\))$/', $element)) {
  62. // create each individual expression
  63. // search for ~ = <> X*{}
  64. // sets like 3*{S34, S36, S37, S39}
  65. if (preg_match('/^(\d+)\*\{(.+)\}$/', $element, $matches)) {
  66. $repeat = $matches[1];
  67. $set = explode(',', $matches[2]);
  68. $count = 0;
  69. foreach ($set as $setelement) {
  70. if (isset($usertracks[$setelement]) &&
  71. ($usertracks[$setelement]->status == 'completed' || $usertracks[$setelement]->status == 'passed')) {
  72. $count++;
  73. }
  74. }
  75. if ($count >= $repeat) {
  76. $element = 'true';
  77. } else {
  78. $element = 'false';
  79. }
  80. // ~ Not
  81. } else if ($element == '~') {
  82. $element = '!';
  83. // = | <>
  84. } else if (preg_match('/^(.+)(\=|\<\>)(.+)$/', $element, $matches)) {
  85. $element = trim($matches[1]);
  86. if (isset($usertracks[$element])) {
  87. $value = trim(preg_replace('/(\'|\")/', '', $matches[3]));
  88. if (isset($statuses[$value])) {
  89. $value = $statuses[$value];
  90. }
  91. if ($matches[2] == '<>') {
  92. $oper = '!=';
  93. } else {
  94. $oper = '==';
  95. }
  96. $element = '(\''.$usertracks[$element]->status.'\' '.$oper.' \''.$value.'\')';
  97. } else {
  98. $element = 'false';
  99. }
  100. // everything else must be an element defined like S45 ...
  101. } else {
  102. if (isset($usertracks[$element]) &&
  103. ($usertracks[$element]->status == 'completed' || $usertracks[$element]->status == 'passed')) {
  104. $element = 'true';
  105. } else {
  106. $element = 'false';
  107. }
  108. }
  109. }
  110. $stack []= ' '.$element.' ';
  111. }
  112. return eval('return '.implode($stack).';');
  113. }