From 6dae57a10277a2456a5b64244fd61087e74adb76 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Mon, 12 Nov 2018 09:19:37 +1300 Subject: [PATCH] MDL-63953 mod_scorm: Use correct value for first attempt. --- mod/scorm/lib.php | 11 ++++++++++- mod/scorm/locallib.php | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php index ee88c55e982..ad07348d740 100644 --- a/mod/scorm/lib.php +++ b/mod/scorm/lib.php @@ -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). diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index cf61bb03568..1793a954591 100644 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -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)); @@ -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 *