Permalink
Browse files

MDL-43222 SCORM: correct check for list of attempts and make correct …

…check for current attempt in scorm_get_toc
  • Loading branch information...
1 parent 704ecfe commit e71ac727e3ee88cdaaeb5b0ea78395bdb8910390 @danmarsden danmarsden committed Jan 9, 2014
Showing with 31 additions and 15 deletions.
  1. +31 −15 mod/scorm/locallib.php
@@ -716,7 +716,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 {
@@ -1217,27 +1217,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;
}
/**
@@ -1730,7 +1746,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();

0 comments on commit e71ac72

Please sign in to comment.