Some bug fixes, added new score view (thanks Matt) and added a new validation procedure

This commit is contained in:
bobopinna 2004-07-19 14:53:43 +00:00
parent 485913f784
commit 1a12b1f1d1
16 changed files with 487 additions and 235 deletions

View file

@ -3,27 +3,46 @@
/// Library of functions and constants for module scorm
/// (replace scorm with the name of your module and delete this line)
$SCORM_WINDOW_OPTIONS = array("resizable", "scrollbars", "status", "height", "width");
define('VALUESCOES', '0');
define('VALUEHIGHEST', '1');
define('VALUEAVERAGE', '2');
define('VALUESUM', '3');
$SCORM_GRADE_METHOD = array (VALUESCOES => get_string("gradescoes", "scorm"),
VALUEHIGHEST => get_string("gradehighest", "scorm"),
VALUEAVERAGE => get_string("gradeaverage", "scorm"),
VALUESUM => get_string("gradesum", "scorm"));
$SCORM_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'status', 'height', 'width');
if (!isset($CFG->scorm_popup)) {
set_config("scorm_popup", "");
}
set_config('scorm_popup', '');
}
if (!isset($CFG->scorm_validate)) {
$scorm_validate = 'none';
if (extension_loaded('domxml')) {
$scorm_validate = 'domxml';
}
if (version_compare(phpversion(),'5.0.0','>=')) {
$scorm_validate = 'php5';
}
set_config('scorm_validate', $scorm_validate);
}
foreach ($SCORM_WINDOW_OPTIONS as $popupoption) {
$popupoption = "scorm_popup$popupoption";
if (!isset($CFG->$popupoption)) {
if ($popupoption == "scorm_popupheight") {
if ($popupoption == 'scorm_popupheight') {
set_config($popupoption, 450);
} else if ($popupoption == "scorm_popupwidth") {
} else if ($popupoption == 'scorm_popupwidth') {
set_config($popupoption, 620);
} else {
set_config($popupoption, "checked");
set_config($popupoption, 'checked');
}
}
}
if (!isset($CFG->scorm_framesize)) {
set_config("scorm_framesize", 140);
set_config('scorm_framesize', 140);
}
function scorm_add_instance($scorm) {
@ -37,23 +56,23 @@ function scorm_add_instance($scorm) {
# May have to add extra stuff in here #
global $SCORM_WINDOW_OPTIONS;
$scorm->popup = "";
$scorm->popup = '';
$optionlist = array();
foreach ($SCORM_WINDOW_OPTIONS as $option) {
if (isset($scorm->$option)) {
$optionlist[] = $option."=".$scorm->$option;
$optionlist[] = $option.'='.$scorm->$option;
}
}
$scorm->popup = implode(',', $optionlist);
if ($scorm->popup != "") {
if ($scorm->popup != '') {
$scorm->popup .= ',location=0,menubar=0,toolbar=0';
$scorm->auto = '0';
}
return insert_record("scorm", $scorm);
return insert_record('scorm', $scorm);
}
@ -68,21 +87,21 @@ function scorm_update_instance($scorm) {
# May have to add extra stuff in here #
global $SCORM_WINDOW_OPTIONS;
$scorm->popup = "";
$scorm->popup = '';
$optionlist = array();
foreach ($SCORM_WINDOW_OPTIONS as $option) {
if (isset($scorm->$option)) {
$optionlist[] = $option."=".$scorm->$option;
$optionlist[] = $option.'='.$scorm->$option;
}
}
$scorm->popup = implode(',', $optionlist);
if ($scorm->popup != "") {
if ($scorm->popup != '') {
$scorm->popup .= ',location=0,menubar=0,toolbar=0';
$scorm->auto = '0';
}
return update_record("scorm", $scorm);
return update_record('scorm', $scorm);
}
@ -93,23 +112,23 @@ function scorm_delete_instance($id) {
require('../config.php');
if (! $scorm = get_record("scorm", "id", "$id")) {
if (! $scorm = get_record('scorm', 'id', $id)) {
return false;
}
$result = true;
# Delete any dependent files #
scorm_delete_files($CFG->dataroot."/".$scorm->course."/moddata/scorm".$scorm->datadir);
scorm_delete_files($CFG->dataroot.'/'.$scorm->course.'/moddata/scorm'.$scorm->datadir);
# Delete any dependent records here #
if (! delete_records("scorm_sco_users", "scormid", "$scorm->id")) {
if (! delete_records('scorm_sco_users', 'scormid', $scorm->id)) {
$result = false;
}
if (! delete_records("scorm_scoes", "scorm", "$scorm->id")) {
if (! delete_records('scorm_scoes', 'scorm', $scorm->id)) {
$result = false;
}
if (! delete_records("scorm", "id", "$scorm->id")) {
if (! delete_records('scorm', 'id', $scorm->id)) {
$result = false;
}
@ -163,44 +182,84 @@ function scorm_grades($scormid) {
global $CFG;
if (!$return->maxgrade = count_records_select("scorm_scoes","scorm='$scormid' AND launch<>''")) {
if (!$scorm = get_record("scorm", "id", $scormid)) {
return NULL;
}
$return->grades = NULL;
if ($sco_users=get_records_select("scorm_sco_users", "scormid='$scormid' GROUP BY userid")) {
foreach ($sco_users as $sco_user) {
$user_data=get_records_select("scorm_sco_users","scormid='$scormid' AND userid='$sco_user->userid'");
$scores->completed=0;
$scores->browsed=0;
$scores->incomplete=0;
$scores->failed=0;
$scores->notattempted=0;
$result="";
$data = current($user_data);
foreach ($user_data as $data) {
if ($data->cmi_core_lesson_status=="passed")
$scores->completed++;
else
$scores->{scorm_remove_spaces($data->cmi_core_lesson_status)}++;
}
if ($scores->completed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/completed.gif\" alt=\"".get_string("completed","scorm")."\" title=\"".get_string("completed","scorm")."\"> $scores->completed ";
if ($scores->incomplete)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/incomplete.gif\" alt=\"".get_string("incomplete","scorm")."\" title=\"".get_string("incomplete","scorm")."\"> $scores->incomplete ";
if ($scores->failed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/failed.gif\" alt=\"".get_string("failed","scorm")."\" title=\"".get_string("failed","scorm")."\"> $scores->failed ";
if ($scores->browsed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/browsed.gif\" alt=\"".get_string("browsed","scorm")."\" title=\"".get_string("browsed","scorm")."\"> $scores->browsed ";
if ($scores->notattempted)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/notattempted.gif\" alt=\"".get_string("notattempted","scorm")."\" title=\"".get_string("notattempted","scorm")."\"> $scores->notattempted ";
if ($scorm->grademethod == VALUESCOES) {
if (!$return->maxgrade = count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
return NULL;
}
$return->grades = NULL;
if ($sco_users=get_records_select('scorm_sco_users', "scormid='$scormid' GROUP BY userid")) {
foreach ($sco_users as $sco_user) {
$user_data=get_records_select('scorm_sco_users',"scormid='$scormid' AND userid='$sco_user->userid'");
$scores->completed=0;
$scores->browsed=0;
$scores->incomplete=0;
$scores->failed=0;
$scores->notattempted=0;
$result='';
$data = current($user_data);
foreach ($user_data as $data) {
if ($data->cmi_core_lesson_status=='passed')
$scores->completed++;
else
$scores->{scorm_remove_spaces($data->cmi_core_lesson_status)}++;
}
if ($scores->completed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/completed.gif\" alt=\"".get_string('completed','scorm')."\" title=\"".get_string('completed','scorm')."\"> $scores->completed ";
if ($scores->incomplete)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/incomplete.gif\" alt=\"".get_string('incomplete','scorm')."\" title=\"".get_string('incomplete','scorm')."\"> $scores->incomplete ";
if ($scores->failed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/failed.gif\" alt=\"".get_string('failed','scorm')."\" title=\"".get_string('failed','scorm')."\"> $scores->failed ";
if ($scores->browsed)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/browsed.gif\" alt=\"".get_string('browsed','scorm')."\" title=\"".get_string('browsed','scorm')."\"> $scores->browsed ";
if ($scores->notattempted)
$result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/notattempted.gif\" alt=\"".get_string('notattempted','scorm')."\" title=\"".get_string('notattempted','scorm')."\"> $scores->notattempted ";
$return->grades[$sco_user->userid]=$result;
}
$return->grades[$sco_user->userid]=$result;
}
}
} else {
$grades = get_records_select("scorm_sco_users", "scormid=$scormid AND cmi_core_score_raw>0","","id,userid,cmi_core_score_raw");
//$grades = get_records_menu("scorm_sco_users", "scormid",$scormid,"","userid,cmi_core_score_raw");
$valutations = array();
foreach ($grades as $grade) {
if (!isset($valutations[$grade->userid])) {
if ($scorm->grademethod == VALUEAVERAGE) {
$values = array();
$values[$grade->userid]->grade = 0;
$values[$grade->userid]->values = 0;
}
$valutations[$grade->userid] = 0;
}
switch ($scorm->grademethod) {
case VALUEHIGHEST:
if ($grade->cmi_core_score_raw > $valutations[$grade->userid]) {
$valutations[$grade->userid] = $grade->cmi_core_score_raw;
}
break;
case VALUEAVERAGE:
$values[$grade->userid]->grade += $grade->cmi_core_score_raw;
$values[$grade->userid]->values++;
break;
case VALUESUM:
$valutations[$grade->userid] += $grade->cmi_core_score_raw;
break;
}
}
if ($scorm->grademethod == VALUEAVERAGE) {
foreach($values as $userid => $value) {
$valutations[$userid] = $value->grade/$value->values;
}
}
//print_r($grades);
$return->grades = $valutations;
$return->maxgrade = $scorm->maxgrade;
}
return $return;
}
@ -210,12 +269,13 @@ function scorm_grades($scormid) {
/// starts with scorm_
function scorm_randstring($len = "8")
function scorm_randstring($len = '8')
{
$rstring = NULL;
$lchar = '';
for($i=0; $i<$len; $i++) {
$char = chr(rand(48,122));
while (!ereg("[a-zA-Z0-9]", $char)){
while (!ereg('[a-zA-Z0-9]', $char)){
if($char == $lchar) continue;
$char = chr(rand(48,90));
}
@ -226,16 +286,16 @@ function scorm_randstring($len = "8")
}
function scorm_datadir($strPath, $existingdir="", $prefix = "SCORM")
function scorm_datadir($strPath, $existingdir='', $prefix = 'SCORM')
{
global $CFG;
if (($existingdir!="") && (is_dir($strPath.$existingdir)))
if (($existingdir!='') && (is_dir($strPath.$existingdir)))
return $strPath.$existingdir;
if (is_dir($strPath)) {
do {
$datadir="/".$prefix.scorm_randstring();
$datadir='/'.$prefix.scorm_randstring();
} while (file_exists($strPath.$datadir));
mkdir($strPath.$datadir, $CFG->directorypermissions);
@chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
@ -245,15 +305,64 @@ function scorm_datadir($strPath, $existingdir="", $prefix = "SCORM")
}
}
if ($CFG->scorm_validate == 'domxml') {
require_once('validatordomxml.php');
}
function scorm_validate($manifest)
{
global $CFG;
global $item_idref_array;
global $idres_array;
global $def_org_array;
global $id_org_array;
if (is_file ($manifest)) {
if (file_exists($manifest))
{
return "regular";
}
if (file_exists($manifest)) {
if ($CFG->scorm_validate == 'domxml') {
$manifest_string = file_get_contents($manifest);
/* Elimino i caratteri speciali di spaziatura e ritorno a capo dal file xml */
$spec = array('\n', '\r', '\t', '\0', '\x0B');
$content = str_replace($spec, '', $manifest_string);
if ($xmldoc = domxml_open_mem($content)) {
$root = $xmldoc->document_element();
if (!testRoot($root)) {
return 'syntax';
}
if (testNode($root)) {
// Nel corpo di questo if si controllano le corrispondenze fra gli attributi
// Nello Standard SCORM ad ogni attributo idRef di <item> deve corrispondere
// un attributo ID di <resource>
// Gli array degli attributi sono stati dichiarati globali in validator.php
// pertanto possono essere utilizzati direttamente all'interno di main.php
foreach($item_idref_array as $elem_it) {
if (array_search($elem_it, $idres_array) === false) {
return 'mismatch';
}
}
foreach($def_org_array as $elem_def) {
if (array_search($elem_it, $id_org_array) === false) {
return 'mismatch';
}
}
} else {
return 'badmanifest';
}
}
return 'regular';
} else {
return 'found';
}
}
} else {
return "nomanifest";
return 'nomanifest';
}
}
@ -264,53 +373,66 @@ function scorm_delete_files($directory)
$handle=opendir($directory);
while (($file = readdir($handle)) != '')
{
if ($file != "." && $file != "..")
{
if (!is_dir($directory."/".$file))
unlink($directory."/".$file);
else
scorm_delete_files($directory."/".$file);
}
if ($file != '.' && $file != '..')
{
if (!is_dir($directory.'/'.$file)) {
//chmod($directory.'/'.$file,0777);
unlink($directory.'/'.$file);
} else {
scorm_delete_files($directory.'/'.$file);
}
}
}
rmdir($directory);
}
}
function scorm_startElement($parser, $name, $attrs) {
global $manifest,$i,$resources,$parent,$level;
if ($name == "ITEM") {
global $scoes,$i,$resources,$parent,$level,$organization,$manifest,$defaultorg;
if ($name == 'ITEM') {
$i++;
$manifest[$i]["identifier"] = $attrs["IDENTIFIER"];
if (empty($attrs["IDENTIFIERREF"]))
$attrs["IDENTIFIERREF"] = "";
$manifest[$i]["identifierref"] = $attrs["IDENTIFIERREF"];
if (empty($attrs["ISVISIBLE"]))
$attrs["ISVISIBLE"] = "";
$manifest[$i]["isvisible"] = $attrs["ISVISIBLE"];
$manifest[$i]["parent"] = $parent[$level];
$scoes[$i]['manifest'] = $manifest;
$scoes[$i]['organization'] = $organization;
$scoes[$i]['identifier'] = $attrs['IDENTIFIER'];
if (empty($attrs['IDENTIFIERREF']))
$attrs['IDENTIFIERREF'] = '';
$scoes[$i]['identifierref'] = $attrs['IDENTIFIERREF'];
if (empty($attrs['ISVISIBLE']))
$attrs['ISVISIBLE'] = '';
$scoes[$i]['isvisible'] = $attrs['ISVISIBLE'];
$scoes[$i]['parent'] = $parent[$level];
$level++;
$parent[$level] = $attrs["IDENTIFIER"];
$parent[$level] = $attrs['IDENTIFIER'];
}
if ($name == "RESOURCE") {
$resources[$attrs["IDENTIFIER"]]["href"]=$attrs["HREF"];
$resources[$attrs["IDENTIFIER"]]["type"]=$attrs["ADLCP:SCORMTYPE"];
if ($name == 'RESOURCE') {
if (!isset($attrs['HREF'])) {
$attrs['HREF'] = '';
}
$resources[$attrs['IDENTIFIER']]['href']=$attrs['HREF'];
$resources[$attrs['IDENTIFIER']]['type']=$attrs['ADLCP:SCORMTYPE'];
}
if ($name == 'ORGANIZATION') {
$organization = $attrs['IDENTIFIER'];
}
if ($name == 'MANIFEST') {
$manifest = $attrs['IDENTIFIER'];
}
if ($name == 'ORGANIZATIONS') {
$defaultorg = $attrs['DEFAULT'];
}
}
function scorm_endElement($parser, $name) {
global $manifest,$i,$level,$datacontent,$navigation;
if ($name == "ITEM") {
global $scoes,$i,$level,$datacontent,$navigation;
if ($name == 'ITEM') {
$level--;
}
if ($name == "TITLE" && $level>0) {
$manifest[$i]["title"] = $datacontent;
}
if ($name == "ADLCP:HIDERTSUI") {
$manifest[$i][$datacontent] = 1;
}
if ($name == "ORGANIZATION") {
$level = 0;
}
if ($name == 'TITLE' && $level>0)
$scoes[$i]['title'] = $datacontent;
if ($name == 'ADLCP:HIDERTSUI')
$scoes[$i][$datacontent] = 1;
if ($name == 'ADLCP:DATAFROMLMS')
$scoes[$i]['datafromlms'] = $datacontent;
}
function scorm_characterData($parser, $data) {
@ -319,26 +441,28 @@ function scorm_characterData($parser, $data) {
}
function scorm_parse($basedir,$file,$scorm_id) {
global $manifest,$i,$resources,$parent,$level;
$datacontent = "";
$manifest[][] = "";
$resources[] = "";
global $scoes,$i,$resources,$parent,$level,$defaultorg;
$datacontent = '';
$scoes[][] = '';
$resources[] = '';
$organization = '';
$defaultorg = '';
$i = 0;
$level = 0;
$parent[$level] = "/";
$parent[$level] = '/';
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "scorm_startElement", "scorm_endElement");
xml_set_character_data_handler($xml_parser, "scorm_characterData");
if (!($fp = fopen($basedir.$file, "r"))) {
die("could not open XML input");
xml_set_element_handler($xml_parser, 'scorm_startElement', 'scorm_endElement');
xml_set_character_data_handler($xml_parser, 'scorm_characterData');
if (!($fp = fopen($basedir.$file, 'r'))) {
die('could not open XML input');
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
die(sprintf('XML error: %s at line %d',
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
@ -347,45 +471,62 @@ function scorm_parse($basedir,$file,$scorm_id) {
$launch = 0;
$sco->scorm = $scorm_id;
delete_records("scorm_scoes","scorm",$scorm_id);
delete_records("scorm_sco_users","scormid",$scorm_id);
delete_records('scorm_scoes','scorm',$scorm_id);
delete_records('scorm_sco_users','scormid',$scorm_id);
for ($j=1; $j<=$i; $j++) {
$sco->identifier = $manifest[$j]["identifier"];
$sco->parent = $manifest[$j]["parent"];
$sco->title = $manifest[$j]["title"];
if (!isset($manifest[$j]["datafromlms"])) {
$manifest[$j]["datafromlms"] = "";
}
$sco->datafromlms = $manifest[$j]["datafromlms"];
if (isset($scoes[1])) {
for ($j=1; $j<=$i; $j++) {
$sco->identifier = $scoes[$j]['identifier'];
$sco->parent = $scoes[$j]['parent'];
$sco->title = $scoes[$j]['title'];
$sco->organization = $scoes[$j]['organization'];
if (!isset($scoes[$j]['datafromlms'])) {
$scoes[$j]['datafromlms'] = '';
}
$sco->datafromlms = $scoes[$j]['datafromlms'];
if (!isset($resources[($manifest[$j]["identifierref"])]["href"])) {
$resources[($manifest[$j]["identifierref"])]["href"] = "";
}
$sco->launch = $resources[($manifest[$j]["identifierref"])]["href"];
if (!isset($resources[($scoes[$j]['identifierref'])]['href'])) {
$resources[($scoes[$j]['identifierref'])]['href'] = '';
}
$sco->launch = $resources[($scoes[$j]['identifierref'])]['href'];
if (!isset($resources[($manifest[$j]["identifierref"])]["type"])) {
$resources[($manifest[$j]["identifierref"])]["type"] = "";
}
$sco->type = $resources[($manifest[$j]["identifierref"])]["type"];
if (!isset($resources[($scoes[$j]['identifierref'])]['type'])) {
$resources[($scoes[$j]['identifierref'])]['type'] = '';
}
$sco->type = $resources[($scoes[$j]['identifierref'])]['type'];
if (!isset($manifest[$j]["previous"])) {
$manifest[$j]["previous"] = 0;
}
$sco->previous = $manifest[$j]["previous"];
if (!isset($scoes[$j]['previous'])) {
$scoes[$j]['previous'] = 0;
}
$sco->previous = $scoes[$j]['previous'];
if (!isset($manifest[$j]["continue"])) {
$manifest[$j]["continue"] = 0;
}
$sco->next = $manifest[$j]["continue"];
if (!isset($scoes[$j]['continue'])) {
$scoes[$j]['continue'] = 0;
}
$sco->next = $scoes[$j]['continue'];
if (scorm_remove_spaces($manifest[$j]["isvisible"]) != "false") {
$id = insert_record("scorm_scoes",$sco);
}
if ($launch==0 && $sco->launch) {
$launch = $id;
}
if (scorm_remove_spaces($scoes[$j]['isvisible']) != 'false') {
$id = insert_record('scorm_scoes',$sco);
}
if (($launch==0) && (isset($sco->launch)) && ($defaultorg==$sco->organization)) {
$launch = $id;
}
}
} else {
foreach ($resources as $label => $resource) {
if ((isset($resource['type'])) && ($resource['type'] == 'sco')) {
$sco->identifier = $label;
$sco->title = $label;
$sco->parent = '/';
$sco->launch = $resource['href'];
$sco->type = $resource['type'];
$id = insert_record('scorm_scoes',$sco);
if ($launch == 0) {
$launch = $id;
}
}
}
}
return $launch;
}
@ -406,7 +547,7 @@ function scorm_get_scoes_records($sco_user) {
function scorm_remove_spaces($sourcestr) {
// Remove blank space from a string
$newstr="";
$newstr='';
for( $i=0; $i<strlen($sourcestr); $i++) {
if ($sourcestr[$i]!=' ')
$newstr .=$sourcestr[$i];
@ -418,7 +559,7 @@ function scorm_string_round($stringa) {
// Crop a string to $len character and set an anchor title to the full string
$len=11;
if ( strlen($stringa)>$len ) {
return "<A name=\"\" title=\"$stringa\">".substr($stringa,0,$len-4)."...".substr($stringa,strlen($stringa)-1,1)."</A>";
return "<A name=\"\" title=\"$stringa\">".substr($stringa,0,$len-4).'...'.substr($stringa,strlen($stringa)-1,1).'</A>';
} else
return $stringa;
}
@ -427,19 +568,19 @@ function scorm_external_link($link) {
// check if a link is external
$result = false;
$link = strtolower($link);
if (substr($link,0,7) == "http://")
if (substr($link,0,7) == 'http://')
$result = true;
else if (substr($link,0,8) == "https://")
else if (substr($link,0,8) == 'https://')
$result = true;
else if (substr($link,0,4) == "www.")
else if (substr($link,0,4) == 'www.')
$result = true;
/*else if (substr($link,0,7) == "rstp://")
/*else if (substr($link,0,7) == 'rstp://')
$result = true;
else if (substr($link,0,6) == "rtp://")
else if (substr($link,0,6) == 'rtp://')
$result = true;
else if (substr($link,0,6) == "ftp://")
else if (substr($link,0,6) == 'ftp://')
$result = true;
else if (substr($link,0,9) == "gopher://")
else if (substr($link,0,9) == 'gopher://')
$result = true; */
return $result;
}