Skip to content

Commit

Permalink
MDL-35123 SCORM: cleaner version of get_last_attempt and get_last_com…
Browse files Browse the repository at this point in the history
…pleted_attempt, fixes support for MS Sql driver
  • Loading branch information
danmarsden committed Sep 6, 2012
1 parent 8f3c8e7 commit 88c5506
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions mod/scorm/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,33 +644,50 @@ function scorm_count_launchable($scormid, $organization='') {
return $DB->count_records_select('scorm_scoes', "scorm = ? $sqlorganization AND ".$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), $params);
}

/**
* Returns the last attempt used - if no attempts yet, returns 1 for first attempt
*
* @param int $scormid the id of the scorm.
* @param int $userid the id of the user.
*
* @return int The attempt number to use.
*/
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;
}
$sql = "SELECT MAX(attempt)
FROM {scorm_scoes_track}
WHERE userid = ? AND scormid = ?";
$lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
if (empty($lastattempt)) {
return '1';
} else {
return false;
return $lastattempt;
}
}

/**
* Returns the last completed attempt used - if no completed attempts yet, returns 1 for first attempt
*
* @param int $scormid the id of the scorm.
* @param int $userid the id of the user.
*
* @return int The attempt number to use.
*/
function scorm_get_last_completed_attempt($scormid, $userid) {
global $DB;

/// Find the last attempt number for the given user id and scorm id
if ($lastattempt = $DB->get_record_select('scorm_scoes_track', "userid = ? AND scormid = ? AND (value='completed' OR value='passed')", array($userid, $scormid), 'max(attempt) as a')) {
if (empty($lastattempt->a)) {
return '1';
} else {
return $lastattempt->a;
}
/// Find the last completed attempt number for the given user id and scorm id
$sql = "SELECT MAX(attempt)
FROM {scorm_scoes_track}
WHERE userid = ? AND scormid = ?
AND (value='completed' OR value='passed')";
$lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
if (empty($lastattempt)) {
return '1';
} else {
return false;
return $lastattempt;
}
}

Expand Down

0 comments on commit 88c5506

Please sign in to comment.