Skip to content

Commit

Permalink
MDL-63953 mod_scorm: Use correct value for first attempt.
Browse files Browse the repository at this point in the history
  • Loading branch information
danmarsden committed Nov 11, 2018
1 parent 789dcd6 commit 6dae57a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 10 additions & 1 deletion mod/scorm/lib.php
Expand Up @@ -1453,9 +1453,18 @@ function scorm_check_mode($scorm, &$newattempt, &$attempt, $userid, &$mode) {

if ($scorm->forcenewattempt == SCORM_FORCEATTEMPT_ALWAYS) {
// This SCORM is configured to force a new attempt on every re-entry.
$attempt++;
$newattempt = 'on';
$mode = 'normal';
if ($attempt == 1) {
// Check if the user has any existing data or if this is really the first attempt.
$exists = $DB->record_exists('scorm_scoes_track', array('userid' => $userid, 'scormid' => $scorm->id));
if (!$exists) {
// No records yet - Attempt should == 1.
return;
}
}
$attempt++;

return;
}
// Check if the scorm module is incomplete (used to validate user request to start a new attempt).
Expand Down
26 changes: 25 additions & 1 deletion mod/scorm/locallib.php
Expand Up @@ -780,7 +780,7 @@ function scorm_grade_user($scorm, $userid) {

switch ($scorm->whatgrade) {
case FIRSTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, 1);
return scorm_grade_user_attempt($scorm, $userid, scorm_get_first_attempt($scorm->id, $userid));
break;
case LASTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_completed_attempt($scorm->id, $userid));
Expand Down Expand Up @@ -850,6 +850,30 @@ function scorm_get_last_attempt($scormid, $userid) {
}
}

/**
* Returns the first 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 first attempt number.
*/
function scorm_get_first_attempt($scormid, $userid) {
global $DB;

// Find the first attempt number for the given user id and scorm id.
$sql = "SELECT MIN(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 $lastattempt;
}
}

/**
* Returns the last completed attempt used - if no completed attempts yet, returns 1 for first attempt
*
Expand Down

0 comments on commit 6dae57a

Please sign in to comment.