From 527af45775c43c91b4eb06008bada35cfb049d6a Mon Sep 17 00:00:00 2001 From: piers Date: Mon, 8 Sep 2008 19:45:50 +0000 Subject: [PATCH] MDL-11734 - cmi.comments_from_learner not read - write. revamp the handling of array style elements. --- mod/scorm/datamodels/scorm_12.js.php | 33 +++--------- mod/scorm/datamodels/scorm_13.js.php | 30 +++-------- mod/scorm/locallib.php | 75 +++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 52 deletions(-) diff --git a/mod/scorm/datamodels/scorm_12.js.php b/mod/scorm/datamodels/scorm_12.js.php index 513f513875a..c71e6f0b805 100644 --- a/mod/scorm/datamodels/scorm_12.js.php +++ b/mod/scorm/datamodels/scorm_12.js.php @@ -1,4 +1,6 @@ dirroot.'/mod/scorm/locallib.php'); + if (isset($userdata->status)) { if ($userdata->status == '') { $userdata->entry = 'ab-initio'; @@ -137,31 +139,8 @@ function SCORMapi1_2() { } $value){ - if (substr($element,0,14) == 'cmi.objectives') { - $element = preg_replace('/\.(\d+)\./', "_\$1.", $element); - preg_match('/\_(\d+)\./', $element, $matches); - if (count($matches) > 0 && $current_objective != $matches[1]) { - $current_objective = $matches[1]; - $count++; - $end = strpos($element,$matches[1])+strlen($matches[1]); - $subelement = substr($element,0,$end); - echo ' '.$subelement." = new Object();\n"; - echo ' '.$subelement.".score = new Object();\n"; - echo ' '.$subelement.".score._children = score_children;\n"; - echo ' '.$subelement.".score.raw = '';\n"; - echo ' '.$subelement.".score.min = '';\n"; - echo ' '.$subelement.".score.max = '';\n"; - } - echo ' '.$element.' = \''.$value."';\n"; - } - } - if ($count > 0) { - echo ' cmi.objectives._count = '.$count.";\n"; - } + // reconstitute objectives + scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score')); ?> if (cmi.core.lesson_status == '') { @@ -246,8 +225,8 @@ function SCORMapi1_2() { elementmodel = String(element).replace(expression,'.n.'); if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") { if (eval('datamodel["'+elementmodel+'"].mod') != 'w') { - element = String(element).replace(expression, "_$1."); - elementIndexes = element.split('.'); + element = String(element).replace(expression, "_$1."); + elementIndexes = element.split('.'); subelement = 'cmi'; i = 1; while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) { diff --git a/mod/scorm/datamodels/scorm_13.js.php b/mod/scorm/datamodels/scorm_13.js.php index 9d4dabb1682..bc3aae1b2cf 100644 --- a/mod/scorm/datamodels/scorm_13.js.php +++ b/mod/scorm/datamodels/scorm_13.js.php @@ -1,4 +1,6 @@ dirroot.'/mod/scorm/locallib.php'); + if (isset($userdata->status)) { if (!isset($userdata->{'cmi.exit'}) || (($userdata->{'cmi.exit'} == 'time-out') || ($userdata->{'cmi.exit'} == 'normal'))) { $userdata->entry = 'ab-initio'; @@ -204,28 +206,10 @@ function SCORMapi1_3() { } $value){ - if (substr($element,0,14) == 'cmi.objectives') { - $element = preg_replace('/\.(\d+)\./', "_\$1.", $element); - preg_match('/\_(\d+)\./', $element, $matches); - if (count($matches) > 0 && $current_objective != $matches[1]) { - $current_objective = $matches[1]; - $count++; - $end = strpos($element,$matches[1])+strlen($matches[1]); - $subelement = substr($element,0,$end); - echo ' '.$subelement." = new Object();\n"; - echo ' '.$subelement.".score = new Object();\n"; - echo ' '.$subelement.".score._children = score_children;\n"; - } - echo ' '.$element.' = \''.$value."';\n"; - } - } - if ($count > 0) { - echo ' cmi.objectives._count = '.$count.";\n"; - } + // reconstitute objectives, comments_from_learner and comments_from_lms + scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score')); + scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.comments_from_learner', array()); + scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.comments_from_lms', array()); ?> if (cmi.completion_status == '') { @@ -334,7 +318,6 @@ function SCORMapi1_3() { if (element !="") { var expression = new RegExp(CMIIndex,'g'); var elementmodel = String(element).replace(expression,'.n.'); - if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") { if (eval('datamodel["'+elementmodel+'"].mod') != 'w') { @@ -344,7 +327,6 @@ function SCORMapi1_3() { var elementIndexes = element.split('.'); var subelement = element.substr(0,3); var i = 1; - while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) { subelement += '.'+elementIndexes[i++]; } diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index fc950f48cae..727c846916f 100755 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -1028,4 +1028,77 @@ function scorm_get_count_users($scormid, $groupingid=null) { return ($DB->count_records_sql($sql, $params)); } -?> +/** +* Build up the JavaScript representation of an array element +* +* @param string $sversion SCORM API version +* @param array $userdata User track data +* @param string $element_name Name of array element to get values for +* @param array $children list of sub elements of this array element that also need instantiating +* @return None +*/ +function scorm_reconstitute_array_element($sversion, $userdata, $element_name, $children) { + // reconstitute comments_from_learner and comments_from_lms + $current = ''; + $count = 0; + + // filter out the ones we want + $element_list = array(); + foreach($userdata as $element => $value){ + if (substr($element,0,strlen($element_name)) == $element_name) { + $element_list[$element] = $value; + } + } + + // sort elements in .n array order + uksort($element_list, "scorm_element_cmp"); + + // generate JavaScript + foreach($element_list as $element => $value){ + if ($sversion == 'scorm_13') { + $element = preg_replace('/\.(\d+)\./', ".N\$1.", $element); + preg_match('/\.(N\d+)\./', $element, $matches); + } else { + $element = preg_replace('/\.(\d+)\./', "_\$1.", $element); + preg_match('/\_(\d+)\./', $element, $matches); + } + if (count($matches) > 0 && $current != $matches[1]) { + $current = $matches[1]; + $count++; + $end = strpos($element,$matches[1])+strlen($matches[1]); + $subelement = substr($element,0,$end); + echo ' '.$subelement." = new Object();\n"; + // now add the children + foreach ($children as $child) { + echo ' '.$subelement.".".$child." = new Object();\n"; + echo ' '.$subelement.".".$child."._children = ".$child."_children;\n"; + } + } + echo ' '.$element.' = \''.$value."';\n"; + } + if ($count > 0) { + echo ' '.$element_name.'._count = '.$count.";\n"; + } +} + +/** +* Build up the JavaScript representation of an array element +* +* @param string $a left array element +* @param string $b right array element +* @return comparator - 0,1,-1 +*/ +function scorm_element_cmp($a, $b) { + preg_match('/(\d+)\./', $a, $matches); + $left = intval($matches[1]); + preg_match('/(\d+)\./', $b, $matches); + $right = intval($matches[1]); + if ($left < $right) { + return -1; // smaller + } elseif ($left > $right) { + return 1; // bigger + } else { + return 0; // equal to + } +} +?> \ No newline at end of file