From 69f07dc3aa277ebc7da8455886f8813f5fc3df7e Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Thu, 9 Jan 2014 21:51:11 +1300 Subject: [PATCH] MDL-43222 SCORM: correct check for list of attempts and make correct check for current attempt in scorm_get_toc --- mod/scorm/locallib.php | 46 ++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 51d79ee37ce..9c2b5d2153e 100644 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -702,7 +702,7 @@ function scorm_grade_user($scorm, $userid) { break; case AVERAGEATTEMPT: - $attemptcount = scorm_get_attempt_count($userid, $scorm, true); + $attemptcount = scorm_get_attempt_count($userid, $scorm, true, true); if (empty($attemptcount)) { return 0; } else { @@ -1237,27 +1237,43 @@ function scorm_get_attempt_status($user, $scorm, $cm='') { * * @param object $user Current context user * @param object $scorm a moodle scrom object - mdl_scorm - * @param bool $attempts return the list of attempts + * @param bool $returnobjects if true returns a object with attempts, if false returns count of attempts. + * @param bool $ignoremissingcompletion - ignores attempts that haven't reported a grade/completion. * @return int - no. of attempts so far */ -function scorm_get_attempt_count($userid, $scorm, $attempts_only=false) { +function scorm_get_attempt_count($userid, $scorm, $returnobjects = false, $ignoremissingcompletion = false) { global $DB; - $attemptcount = 0; - $element = 'cmi.core.score.raw'; - if ($scorm->grademethod == GRADESCOES) { - $element = 'cmi.core.lesson_status'; - } + + // Historically attempts that don't report these elements haven't been included in the average attempts grading method + // we may want to change this in future, but to avoid unexpected grade decreases we're leaving this in. MDL-43222 if (scorm_version_check($scorm->version, SCORM_13)) { $element = 'cmi.score.raw'; + } else if ($scorm->grademethod == GRADESCOES) { + $element = 'cmi.core.lesson_status'; + } else { + $element = 'cmi.core.score.raw'; } - $attempts = $DB->get_records_select('scorm_scoes_track', "element=? AND userid=? AND scormid=?", array($element, $userid, $scorm->id), 'attempt', 'DISTINCT attempt AS attemptnumber'); - if ($attempts_only) { + + if ($returnobjects) { + $params = array('userid' => $userid, 'scormid' => $scorm->id); + if ($ignoremissingcompletion) { // Exclude attempts that don't have the completion element requested. + $params['element'] = $element; + } + $attempts = $DB->get_records('scorm_scoes_track', $params, 'attempt', 'DISTINCT attempt AS attemptnumber'); return $attempts; + } else { + $params = array($userid, $scorm->id); + $sql = "SELECT COUNT(DISTINCT attempt) + FROM {scorm_scoes_track} + WHERE 'userid' = ? AND 'scormid' = ?"; + if ($ignoremissingcompletion) { // Exclude attempts that don't have the completion element requested. + $sql .= ' AND element = ?'; + $params[] = $element; + } + + $attemptscount = $DB->count_records_sql($sql, $params); + return $attemptscount; } - if (!empty($attempts)) { - $attemptcount = count($attempts); - } - return $attemptcount; } /** @@ -1746,7 +1762,7 @@ function scorm_get_toc($user, $scorm, $cmid, $toclink=TOCJSLINK, $currentorg='', global $CFG, $DB, $OUTPUT; if (empty($attempt)) { - $attempt = scorm_get_attempt_count($user->id, $scorm); + $attempt = scorm_get_last_attempt($scorm->id, $user->id); } $result = new stdClass();