get_string('gradescoes', 'scorm'), GRADEHIGHEST => get_string('gradehighest', 'scorm'), GRADEAVERAGE => get_string('gradeaverage', 'scorm'), GRADESUM => get_string('gradesum', 'scorm')); define('HIGHESTATTEMPT', '0'); define('AVERAGEATTEMPT', '1'); define('FIRSTATTEMPT', '2'); define('LASTATTEMPT', '3'); $SCORM_WHAT_GRADE = array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'), AVERAGEATTEMPT => get_string('averageattempt', 'scorm'), FIRSTATTEMPT => get_string('firstattempt', 'scorm'), LASTATTEMPT => get_string('lastattempt', 'scorm')); $SCORM_POPUP_OPTIONS = array('resizable'=>1, 'scrollbars'=>1, 'directories'=>0, 'location'=>0, 'menubar'=>0, 'toolbar'=>0, 'status'=>0); $stdoptions = ''; foreach ($SCORM_POPUP_OPTIONS as $popupopt => $value) { $stdoptions .= $popupopt.'='.$value; if ($popupopt != 'status') { $stdoptions .= ','; } } if (!isset($CFG->scorm_maxattempts)) { set_config('scorm_maxattempts','6'); } if (!isset($CFG->scorm_frameheight)) { set_config('scorm_frameheight','500'); } if (!isset($CFG->scorm_framewidth)) { set_config('scorm_framewidth','100%'); } if (!isset($CFG->scorm_updatetime)) { set_config('scorm_updatetime','2'); } if (!isset($CFG->scorm_advancedsettings)) { set_config('scorm_advancedsettings','0'); } if (!isset($CFG->scorm_windowsettings)) { set_config('scorm_windowsettings','0'); } /// Local Library of functions for module scorm /** * This function will permanently delete the given * directory and all files and subdirectories. * * @param string $directory The directory to remove * @return boolean */ function scorm_delete_files($directory) { if (is_dir($directory)) { $files=scandir($directory); set_time_limit(0); foreach($files as $file) { if (($file != '.') && ($file != '..')) { if (!is_dir($directory.'/'.$file)) { unlink($directory.'/'.$file); } else { scorm_delete_files($directory.'/'.$file); } } } rmdir($directory); return true; } return false; } /** * Create a new temporary subdirectory with a random name in the given path * * @param string $strpath The scorm data directory * @return string/boolean */ function scorm_tempdir($strPath) { global $CFG; if (is_dir($strPath)) { do { // Create a random string of 8 chars $randstring = NULL; $lchar = ''; $len = 8; for ($i=0; $i<$len; $i++) { $char = chr(rand(48,122)); while (!ereg('[a-zA-Z0-9]', $char)){ if ($char == $lchar) continue; $char = chr(rand(48,90)); } $randstring .= $char; $lchar = $char; } $datadir='/'.$randstring; } while (file_exists($strPath.$datadir)); mkdir($strPath.$datadir, $CFG->directorypermissions); @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it return $strPath.$datadir; } else { return false; } } 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 '
'.get_string('name').': '.format_string($scorm->name).'';
if (has_capability('moodle/course:manageactivities', $context)) {
if (isediting($course->id)) {
// Display update icon
$path = $CFG->wwwroot.'/course';
$headertext .= ''.
''.
'![]() | ';
// Display report link
$trackedusers = $DB->get_record('scorm_scoes_track', array('scormid'=>$scorm->id), 'count(distinct(userid)) as c');
if ($trackedusers->c > 0) {
$headertext .= ''. 'frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'. get_string('viewallreports','scorm',$trackedusers->c).''; } else { $headertext .= ' | '.get_string('noreports','scorm'); } $colspan = ' colspan="2"'; } $headertext .= ' |
'.format_text(get_string('summary').': '.$scorm->summary).' |