diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 7243170deb9..0e1ea245328 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -544,25 +544,21 @@ function quiz_save_best_grade($quiz, $userid = null, $attempts = array()) { /** * Calculate the overall grade for a quiz given a number of attempts by a particular user. * - * @return float The overall grade - * @param object $quiz The quiz for which the best grade is to be calculated - * @param array $attempts An array of all the attempts of the user at the quiz + * @param object $quiz the quiz settings object. + * @param array $attempts an array of all the user's attempts at this quiz in order. + * @return float the overall grade */ function quiz_calculate_best_grade($quiz, $attempts) { switch ($quiz->grademethod) { case QUIZ_ATTEMPTFIRST: - foreach ($attempts as $attempt) { - return $attempt->sumgrades; - } - return $final; + $firstattempt = reset($attempts); + return $firstattempt->sumgrades; case QUIZ_ATTEMPTLAST: - foreach ($attempts as $attempt) { - $final = $attempt->sumgrades; - } - return $final; + $lastattempt = end($attempts); + return $lastattempt->sumgrades; case QUIZ_GRADEAVERAGE: $sum = 0; @@ -578,8 +574,8 @@ function quiz_calculate_best_grade($quiz, $attempts) { } return $sum / $count; - default: case QUIZ_GRADEHIGHEST: + default: $max = null; foreach ($attempts as $attempt) { if ($attempt->sumgrades > $max) {