moodle/mod/scorm/locallib.php
2008-09-09 08:30:00 +00:00

780 lines
27 KiB
PHP
Executable File

<?php // $Id$
require_once("$CFG->dirroot/mod/scorm/lib.php");
/// Constants and settings for module scorm
define('UPDATE_NEVER', '0');
define('UPDATE_ONCHANGE', '1');
define('UPDATE_EVERYDAY', '2');
define('UPDATE_EVERYTIME', '3');
define('SCO_ALL', 0);
define('SCO_DATA', 1);
define('SCO_ONLY', 2);
define('GRADESCOES', '0');
define('GRADEHIGHEST', '1');
define('GRADEAVERAGE', '2');
define('GRADESUM', '3');
define('HIGHESTATTEMPT', '0');
define('AVERAGEATTEMPT', '1');
define('FIRSTATTEMPT', '2');
define('LASTATTEMPT', '3');
/// Local Library of functions for module scorm
/**
* Extracts scrom package, sets up all variables.
* Called whenever scorm changes
* @param object $scorm instance - fields are updated and changes saved into database
* @param bool $full force full update if true
* @return void
*/
function scorm_parse($scorm, $full) {
global $CFG, $DB;
if (!isset($scorm->cmid)) {
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
$scorm->cmid = $cm->id;
}
$context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
$newhash = $scorm->sha1hash;
if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
$fs = get_file_storage();
$packagefile = false;
if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
if ($packagefile = $fs->get_file($context->id, 'scorm_package', 0, '/', $scorm->reference)) {
$newhash = $packagefile->get_contenthash();
} else {
$newhash = null;
}
} else {
if (!$CFG->scorm_allowtypelocalsync) {
// sorry - localsync disabled
return;
}
if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
$fs->delete_area_files($context->id, 'scorm_package');
$file_record = array('contextid'=>$context->id, 'filearea'=>'scorm_package', 'itemid'=>0, 'filepath'=>'/');
if ($packagefile = $fs->create_file_from_url($file_record, $scorm->reference)) {
$newhash = sha1($scorm->reference);
} else {
$newhash = null;
}
}
}
if ($packagefile) {
if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
if (strpos($scorm->version, 'SCORM') !== false) {
if ($fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
// no need to update
return;
}
} else if (strpos($scorm->version, 'AICC') !== false) {
// TODO: add more sanity checks - something really exists in scorm_content area
return;
}
}
// now extract files
$fs->delete_area_files($context->id, 'scorm_content');
$packer = get_file_packer('application/zip');
$packagefile->extract_to_storage($packer, $context->id, 'scorm_content', 0, '/');
} else if (!$full) {
return;
}
if ($manifest = $fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
// SCORM
if (!scorm_parse_scorm($scorm, $manifest)) {
$scorm->version = 'ERROR';
}
} else {
require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
// AICC
if (!scorm_parse_aicc($scorm)) {
$scorm->version = 'ERROR';
}
}
} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $CFG->scorm_allowtypeexternal) {
if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
return;
}
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
// SCORM only, AICC can not be external
if (!scorm_parse_scorm($scorm, $scorm->reference)) {
$scorm->version = 'ERROR';
}
$newhash = sha1($scorm->reference);
} else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY and !empty($CFG->repositoryactivate) and $CFG->scorm_allowtypeimsrepository) {
if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
return;
}
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
if (!scorm_parse_scorm($scorm, $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml')) {
$scorm->version = 'ERROR';
}
$newhash = sha1($scorm->reference);
} else {
// sorry, disabled type
return;
}
$scorm->revision++;
$scorm->sha1hash = $newhash;
$DB->update_record('scorm', $scorm);
}
function scorm_array_search($item, $needle, $haystacks, $strict=false) {
if (!empty($haystacks)) {
foreach ($haystacks as $key => $element) {
if ($strict) {
if ($element->{$item} === $needle) {
return $key;
}
} else {
if ($element->{$item} == $needle) {
return $key;
}
}
}
}
return false;
}
function scorm_repeater($what, $times) {
if ($times <= 0) {
return null;
}
$return = '';
for ($i=0; $i<$times;$i++) {
$return .= $what;
}
return $return;
}
function scorm_external_link($link) {
// check if a link is external
$result = false;
$link = strtolower($link);
if (substr($link,0,7) == 'http://') {
$result = true;
} else if (substr($link,0,8) == 'https://') {
$result = true;
} else if (substr($link,0,4) == 'www.') {
$result = true;
}
return $result;
}
/**
* Returns an object containing all datas relative to the given sco ID
*
* @param integer $id The sco ID
* @return mixed (false if sco id does not exists)
*/
function scorm_get_sco($id,$what=SCO_ALL) {
global $DB;
if ($sco = $DB->get_record('scorm_scoes', array('id'=>$id))) {
$sco = ($what == SCO_DATA) ? new stdClass() : $sco;
if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id)))) {
foreach ($scodatas as $scodata) {
$sco->{$scodata->name} = $scodata->value;
}
} else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id))))) {
$sco->parameters = '';
}
return $sco;
} else {
return false;
}
}
/**
* Returns an object (array) containing all the scoes data related to the given sco ID
*
* @param integer $id The sco ID
* @param integer $organisation an organisation ID - defaults to false if not required
* @return mixed (false if there are no scoes or an array)
*/
function scorm_get_scoes($id,$organisation=false) {
global $DB;
$organizationsql = '';
$queryarray = array('scorm'=>$id);
if (!empty($organisation)) {
$queryarray['organization'] = $organisation;
}
if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'id ASC')) {
// drop keys so that it is a simple array as expected
$scoes = array_values($scoes);
foreach ($scoes as $sco) {
if ($scodatas = $DB->get_records('scorm_scoes_data',array('scoid'=>$sco->id))) {
foreach ($scodatas as $scodata) {
$sco->{$scodata->name} = stripslashes_safe($scodata->value);
}
}
}
return $scoes;
} else {
return false;
}
}
function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
global $DB;
$id = null;
if ($track = $DB->get_record('scorm_scoes_track',array('userid'=>$userid, 'scormid'=>$scormid, 'scoid'=>$scoid, 'attempt'=>$attempt, 'element'=>$element))) {
$track->value = $value;
$track->timemodified = time();
$id = $DB->update_record('scorm_scoes_track',$track);
} else {
$track->userid = $userid;
$track->scormid = $scormid;
$track->scoid = $scoid;
$track->attempt = $attempt;
$track->element = $element;
$track->value = $value;
$track->timemodified = time();
$id = $DB->insert_record('scorm_scoes_track',$track);
}
// MDL-9552, update the gradebook everything raw score is sent
// Scoring by learning objects also needs to be included in the gradebook update
if (strstr($element, '.score.raw') ||
(($element == 'cmi.core.lesson_status' || $element == 'cmi.completion_status') && ($track->value == 'completed' || $track->value == 'passed'))) {
$scorm = $DB->get_record('scorm', array('id' => $scormid));
$grademethod = $scorm->grademethod % 10;
if (strstr($element, '.score.raw') || $grademethod == GRADESCOES) {
include_once('lib.php');
scorm_update_grades($scorm, $userid);
}
}
return $id;
}
function scorm_get_tracks($scoid,$userid,$attempt='') {
/// Gets all tracks of specified sco and user
global $CFG, $DB;
if (empty($attempt)) {
if ($scormid = $DB->get_field('scorm_scoes','scorm', array('id'=>$scoid))) {
$attempt = scorm_get_last_attempt($scormid,$userid);
} else {
$attempt = 1;
}
}
if ($tracks = $DB->get_records('scorm_scoes_track', array('userid'=>$userid, 'scoid'=>$scoid, 'attempt'=>$attempt),'element ASC')) {
$usertrack->userid = $userid;
$usertrack->scoid = $scoid;
// Defined in order to unify scorm1.2 and scorm2004
$usertrack->score_raw = '';
$usertrack->status = '';
$usertrack->total_time = '00:00:00';
$usertrack->session_time = '00:00:00';
$usertrack->timemodified = 0;
foreach ($tracks as $track) {
$element = $track->element;
$usertrack->{$element} = $track->value;
switch ($element) {
case 'cmi.core.lesson_status':
case 'cmi.completion_status':
if ($track->value == 'not attempted') {
$track->value = 'notattempted';
}
$usertrack->status = $track->value;
break;
case 'cmi.core.score.raw':
case 'cmi.score.raw':
$usertrack->score_raw = $track->value;
break;
case 'cmi.core.session_time':
case 'cmi.session_time':
$usertrack->session_time = $track->value;
break;
case 'cmi.core.total_time':
case 'cmi.total_time':
$usertrack->total_time = $track->value;
break;
}
if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
$usertrack->timemodified = $track->timemodified;
}
}
if (is_array($usertrack)) {
ksort($usertrack);
}
return $usertrack;
} else {
return false;
}
}
function scorm_get_user_data($userid) {
global $DB;
/// Gets user info required to display the table of scorm results
/// for report.php
return $DB->get_record('user', array('id'=>$userid),'firstname, lastname, picture');
}
function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
global $DB;
$attemptscore = NULL;
$attemptscore->scoes = 0;
$attemptscore->values = 0;
$attemptscore->max = 0;
$attemptscore->sum = 0;
$attemptscore->lastmodify = 0;
if (!$scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
return NULL;
}
// this treatment is necessary as the whatgrade field was not in the DB
// and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
// and 1s are grademethod
$grademethod = $scorm->grademethod % 10;
foreach ($scoes as $sco) {
if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
$attemptscore->scoes++;
}
if (!empty($userdata->score_raw)) {
$attemptscore->values++;
$attemptscore->sum += $userdata->score_raw;
$attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
$attemptscore->lastmodify = $userdata->timemodified;
} else {
$attemptscore->lastmodify = 0;
}
}
}
}
switch ($grademethod) {
case GRADEHIGHEST:
$score = $attemptscore->max;
break;
case GRADEAVERAGE:
if ($attemptscore->values > 0) {
$score = $attemptscore->sum/$attemptscore->values;
} else {
$score = 0;
}
break;
case GRADESUM:
$score = $attemptscore->sum;
break;
case GRADESCOES:
$score = $attemptscore->scoes;
break;
default:
$score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default
}
if ($time) {
$result = new stdClass();
$result->score = $score;
$result->time = $attemptscore->lastmodify;
} else {
$result = $score;
}
return $result;
}
function scorm_grade_user($scorm, $userid, $time=false) {
// this treatment is necessary as the whatgrade field was not in the DB
// and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
// and 1s are grademethod
$whatgrade = intval($scorm->grademethod / 10);
// insure we dont grade user beyond $scorm->maxattempt settings
$lastattempt = scorm_get_last_attempt($scorm->id, $userid);
if($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt){
$lastattempt = $scorm->maxattempt;
}
switch ($whatgrade) {
case FIRSTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, 1, $time);
break;
case LASTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
break;
case HIGHESTATTEMPT:
$maxscore = 0;
$attempttime = 0;
for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
$attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
if ($time) {
if ($attemptscore->score > $maxscore) {
$maxscore = $attemptscore->score;
$attempttime = $attemptscore->time;
}
} else {
$maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
}
}
if ($time) {
$result = new stdClass();
$result->score = $maxscore;
$result->time = $attempttime;
return $result;
} else {
return $maxscore;
}
break;
case AVERAGEATTEMPT:
$lastattempt = scorm_get_last_attempt($scorm->id, $userid);
$sumscore = 0;
for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
$attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
if ($time) {
$sumscore += $attemptscore->score;
} else {
$sumscore += $attemptscore;
}
}
if ($lastattempt > 0) {
$score = $sumscore / $lastattempt;
} else {
$score = 0;
}
if ($time) {
$result = new stdClass();
$result->score = $score;
$result->time = $attemptscore->time;
return $result;
} else {
return $score;
}
break;
}
}
function scorm_count_launchable($scormid,$organization='') {
global $DB;
$sqlorganization = '';
$params = array($scormid);
if (!empty($organization)) {
$sqlorganization = " AND organization=?";
$params[] = $organization;
}
$params []= ''; // empty launch
return $DB->count_records_select('scorm_scoes',"scorm = ? $sqlorganization AND launch <> ?", $params);
}
function scorm_get_last_attempt($scormid, $userid) {
global $DB;
/// Find the last attempt number for the given user id and scorm id
if ($lastattempt = $DB->get_record('scorm_scoes_track', array('userid'=>$userid, 'scormid'=>$scormid), 'max(attempt) as a')) {
if (empty($lastattempt->a)) {
return '1';
} else {
return $lastattempt->a;
}
}
}
function scorm_course_format_display($user,$course) {
global $CFG, $DB;
$strupdate = get_string('update');
$strmodule = get_string('modulename','scorm');
$context = get_context_instance(CONTEXT_COURSE,$course->id);
echo '<div class="mod-scorm">';
if ($scorms = get_all_instances_in_course('scorm', $course)) {
// The module SCORM activity with the least id is the course
$scorm = current($scorms);
if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
print_error('invalidcoursemodule');
}
$colspan = '';
$headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
if (has_capability('moodle/course:manageactivities', $context)) {
if (isediting($course->id)) {
// Display update icon
$path = $CFG->wwwroot.'/course';
$headertext .= '<span class="commands">'.
'<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
'<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
}
$headertext .= '</td>';
// Display report link
$trackedusers = $DB->get_record('scorm_scoes_track', array('scormid'=>$scorm->id), 'count(distinct(userid)) as c');
if ($trackedusers->c > 0) {
$headertext .= '<td class="reportlink">'.
'<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
get_string('viewallreports','scorm',$trackedusers->c).'</a>';
} else {
$headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
}
$colspan = ' colspan="2"';
}
$headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
print_simple_box($headertext,'','100%');
scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
} else {
if (has_capability('moodle/course:update', $context)) {
// Create a new activity
redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
} else {
notify('Could not find a scorm course here');
}
}
echo '</div>';
}
function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
global $CFG, $DB;
if ($scorm->updatefreq == UPDATE_EVERYTIME) {
scorm_parse($scorm, false);
}
$organization = optional_param('organization', '', PARAM_INT);
print_simple_box_start('center',$boxwidth);
?>
<div class="structurehead"><?php print_string('contents','scorm') ?></div>
<?php
if (empty($organization)) {
$organization = $scorm->launch;
}
if ($orgs = $DB->get_records_menu('scorm_scoes', array('scorm'=>$scorm->id, 'organization'=>'', 'launch'=>''), 'id', 'id,title')) {
if (count($orgs) > 1) {
?>
<div class='scorm-center'>
<?php print_string('organizations','scorm') ?>
<form id='changeorg' method='post' action='<?php echo $action ?>'>
<?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
</form>
</div>
<?php
}
}
$orgidentifier = '';
if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
if (($sco->organization == '') && ($sco->launch == '')) {
$orgidentifier = $sco->identifier;
} else {
$orgidentifier = $sco->organization;
}
}
/*
$orgidentifier = '';
if ($org = $DB->get_record('scorm_scoes', array('id'=>$organization))) {
if (($org->organization == '') && ($org->launch == '')) {
$orgidentifier = $org->identifier;
} else {
$orgidentifier = $org->organization;
}
}*/
$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
$scorm->version = 'scorm_12';
}
require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
$result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
$incomplete = $result->incomplete;
echo $result->toc;
print_simple_box_end();
?>
<div class="scorm-center">
<form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php">
<?php
if ($scorm->hidebrowse == 0) {
print_string('mode','scorm');
echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
} else {
echo '<input type="hidden" name="mode" value="normal" />'."\n";
}
if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
?>
<br />
<input type="checkbox" id="a" name="newattempt" />
<label for="a"><?php print_string('newattempt','scorm') ?></label>
<?php
}
?>
<br />
<input type="hidden" name="scoid"/>
<input type="hidden" name="id" value="<?php echo $cm->id ?>"/>
<input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
<input type="submit" value="<?php print_string('enter','scorm') ?>" />
</form>
</div>
<?php
}
function scorm_simple_play($scorm,$user) {
global $DB;
$result = false;
$scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND launch <> ?', array($scorm->id, $DB->sql_empty()));
if ($scoes && (count($scoes) == 1)) {
if ($scorm->skipview >= 1) {
$sco = current($scoes);
if (scorm_get_tracks($sco->id,$user->id) === false) {
header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
$result = true;
} else if ($scorm->skipview == 2) {
header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
$result = true;
}
}
}
return $result;
}
/*
function scorm_simple_play($scorm,$user) {
global $DB;
$result = false;
if ($scoes = $DB->get_records_select('scorm_scoes','scorm=? AND launch<>""', array($scorm->id))) {
if (count($scoes) == 1) {
if ($scorm->skipview >= 1) {
$sco = current($scoes);
if (scorm_get_tracks($sco->id,$user->id) === false) {
header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
$result = true;
} else if ($scorm->skipview == 2) {
header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
$result = true;
}
}
}
}
return $result;
}
*/
function scorm_get_count_users($scormid, $groupingid=null) {
global $CFG, $DB;
if (!empty($CFG->enablegroupings) && !empty($groupingid)) {
$sql = "SELECT COUNT(DISTINCT st.userid)
FROM {scorm_scoes_track} st
INNER JOIN {groups_members} gm ON st.userid = gm.userid
INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
WHERE st.scormid = ? AND gg.groupingid = ?
";
$params = array($scormid, $groupingid);
} else {
$sql = "SELECT COUNT(DISTINCT st.userid)
FROM {scorm_scoes_track} st
WHERE st.scormid = ?
";
$params = array($scormid);
}
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
}
}
?>