MDL-11734 - cmi.comments_from_learner not read - write. revamp the handling of array style elements.

This commit is contained in:
piers 2008-09-08 19:45:50 +00:00
parent a682f0c2c3
commit 527af45775
3 changed files with 86 additions and 52 deletions

View File

@ -1,4 +1,6 @@
<?php
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
if (isset($userdata->status)) {
if ($userdata->status == '') {
$userdata->entry = 'ab-initio';
@ -137,31 +139,8 @@ function SCORMapi1_2() {
}
<?php
$current_objective = '';
$count = 0;
$objectives = '';
foreach($userdata as $element => $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")) {

View File

@ -1,4 +1,6 @@
<?php
require_once($CFG->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() {
}
<?php
$current_objective = '';
$count = 0;
$objectives = '';
foreach($userdata as $element => $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++];
}

View File

@ -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
}
}
?>