From b1ca5d31dc33cea11fe1301499dca4d0148cf254 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Fri, 23 Jul 2010 09:23:52 +0000 Subject: [PATCH] SCORM MDL-23492 tidy up grading method display/calculation --- mod/scorm/datamodels/scorm_12lib.php | 2 +- mod/scorm/lang/en/scorm.php | 2 +- mod/scorm/locallib.php | 76 ++++++++++++++++++---------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/mod/scorm/datamodels/scorm_12lib.php b/mod/scorm/datamodels/scorm_12lib.php index 526114af7d9..5603c05884b 100644 --- a/mod/scorm/datamodels/scorm_12lib.php +++ b/mod/scorm/datamodels/scorm_12lib.php @@ -146,7 +146,7 @@ function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='n // If not specified retrieve the last attempt number // if (empty($attempt)) { - $attempt = scorm_get_attempt_count($user, $scorm); + $attempt = scorm_get_attempt_count($user->id, $scorm); } $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attempt; $conditions['scorm'] = $scorm->id; diff --git a/mod/scorm/lang/en/scorm.php b/mod/scorm/lang/en/scorm.php index b85a8a4db8b..f50032788ed 100644 --- a/mod/scorm/lang/en/scorm.php +++ b/mod/scorm/lang/en/scorm.php @@ -107,7 +107,7 @@ $string['gradeaverage'] = 'Average grade'; $string['gradeforattempt'] = 'Grade for attempt'; $string['gradehighest'] = 'Highest grade'; $string['grademethod'] = 'Grading method'; -$string['grademethod_help'] = 'The grading method defines how a final grade for the activity is determined. +$string['grademethod_help'] = 'The grading method defines how the grade for a single attempt of the activity is determined. There are 4 grading methods: diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 929e0a2c1bc..6ff17f9ae76 100755 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -592,6 +592,13 @@ function scorm_grade_user($scorm, $userid) { break; case AVERAGEATTEMPT: + $attemptcount = scorm_get_attempt_count($userid, $scorm, true); + print_object($attemptcount); + if (empty($attemptcount)) { + return 0; + } else { + $attemptcount = count($attemptcount); + } $lastattempt = scorm_get_last_attempt($scorm->id, $userid); $sumscore = 0; for ($attempt = 1; $attempt <= $lastattempt; $attempt++) { @@ -599,13 +606,7 @@ function scorm_grade_user($scorm, $userid) { $sumscore += $attemptscore; } - if ($lastattempt > 0) { - $score = $sumscore / $lastattempt; - } else { - $score = 0; - } - - return $score; + return round($sumscore / $attemptcount); break; } } @@ -765,7 +766,7 @@ function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') { } // is this the first attempt ? - $attemptcount = scorm_get_attempt_count($user, $scorm); + $attemptcount = scorm_get_attempt_count($user->id, $scorm); // do not give the player launch FORM if the SCORM object is locked after the final attempt if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) { @@ -1006,7 +1007,7 @@ function scorm_element_cmp($a, $b) { function scorm_get_attempt_status($user, $scorm) { global $DB; - $attempts = scorm_get_attempt_count($user, $scorm, true); + $attempts = scorm_get_attempt_count($user->id, $scorm, true); if(empty($attempts)) { $attemptcount = 0; } else { @@ -1023,33 +1024,52 @@ function scorm_get_attempt_status($user, $scorm) { $gradereported = 0; $gradesum = 0; - switch ($scorm->grademethod) { - case GRADEHIGHEST: - $grademethod = get_string('gradehighest', 'scorm'); - break; - case GRADEAVERAGE: - $grademethod = get_string('gradeaverage', 'scorm'); - break; - case GRADESUM: - $grademethod = get_string('gradesum', 'scorm'); - break; - case GRADESCOES: - $grademethod = get_string('gradescoes', 'scorm'); - break; - } + if ($scorm->maxattempt == 1) { + switch ($scorm->grademethod) { + case GRADEHIGHEST: + $grademethod = get_string('gradehighest', 'scorm'); + break; + case GRADEAVERAGE: + $grademethod = get_string('gradeaverage', 'scorm'); + break; + case GRADESUM: + $grademethod = get_string('gradesum', 'scorm'); + break; + case GRADESCOES: + $grademethod = get_string('gradescoes', 'scorm'); + break; + } + } else { + switch ($scorm->whatgrade) { + case HIGHESTATTEMPT: + $grademethod = get_string('highestattempt', 'scorm'); + break; + case AVERAGEATTEMPT: + $grademethod = get_string('averageattempt', 'scorm'); + break; + case FIRSTATTEMPT: + $grademethod = get_string('firstattempt', 'scorm'); + break; + case LASTATTEMPT: + $grademethod = get_string('lastattempt', 'scorm'); + break; + } + } if(!empty($attempts)) { + $i = 1; foreach($attempts as $attempt) { $gradereported = scorm_grade_user_attempt($scorm, $user->id, $attempt->attemptnumber); - $result .= get_string('gradeforattempt', 'scorm').' ' . $attempt->attemptnumber . ': ' . $gradereported .'%
'; + $result .= get_string('gradeforattempt', 'scorm').' ' . $i . ': ' . $gradereported .'%
'; + $i++; } } - + $calculatedgrade = scorm_grade_user($scorm, $user->id); $result .= get_string('grademethod', 'scorm'). ': ' . $grademethod; if(empty($attempts)) { $result .= '
' . get_string('gradereported','scorm') . ': ' . get_string('none') . '
'; } else { - $result .= '
' . get_string('gradereported','scorm') . ': ' . $gradereported . ($scorm->grademethod == GRADESCOES ? '' : '%') .'
'; + $result .= '
' . get_string('gradereported','scorm') . ': ' . $calculatedgrade . ($scorm->grademethod == GRADESCOES ? '' : '%') .'
'; } $result .= '

'; if ($attemptcount >= $scorm->maxattempt and $scorm->maxattempt > 0) { @@ -1066,14 +1086,14 @@ function scorm_get_attempt_status($user, $scorm) { * @param bool $attempts return the list of attempts * @return int - no. of attempts so far */ -function scorm_get_attempt_count($user, $scorm, $attempts_only=false) { +function scorm_get_attempt_count($userid, $scorm, $attempts_only=false) { global $DB; $attemptcount = 0; $element = 'cmi.core.score.raw'; if ($scorm->version == 'scorm1_3') { $element = 'cmi.score.raw'; } - $attempts = $DB->get_records_select('scorm_scoes_track',"element=? AND userid=? AND scormid=?", array($element, $user->id, $scorm->id),'attempt','DISTINCT attempt AS attemptnumber'); + $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) { return $attempts; }