diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 0e96212c95d..83f19c24257 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -581,25 +581,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; - } - break; + $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; @@ -608,10 +604,10 @@ function quiz_calculate_best_grade($quiz, $attempts) { $sum += $attempt->sumgrades; $count++; } - return (float)$sum/$count; + return (float) $sum/$count; - default: case QUIZ_GRADEHIGHEST: + default: $max = 0; foreach ($attempts as $attempt) { if ($attempt->sumgrades > $max) { diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index bf5ab291674..5ba30b8015b 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -596,7 +596,7 @@ class quiz_overview_report extends quiz_default_report { } else { $params = array(); } - $attemptsql .= "{quiz_attempts}.quiz =? AND preview = 0"; + $attemptsql .= "{quiz_attempts}.quiz = ? AND preview = 0"; $params[] = $quiz->id; } else { list($asql, $params) = $DB->get_in_or_equal($attemptids); @@ -613,10 +613,10 @@ class quiz_overview_report extends quiz_default_report { //not just those that have changed. $sql = "SELECT qa2.* FROM {quiz_attempts} qa2 WHERE " . "qa2.userid IN (SELECT DISTINCT userid FROM {quiz_attempts} WHERE $attemptsql) " . - "AND qa2.timefinish > 0 AND qa2.quiz = ?"; + "AND qa2.timefinish > 0 AND qa2.quiz = ? ORDER BY qa2.userid, qa2.attempt"; $params[] = $quiz->id; } else { - $sql = "SELECT * FROM {quiz_attempts} WHERE $attemptsql AND timefinish > 0"; + $sql = "SELECT * FROM {quiz_attempts} WHERE $attemptsql AND timefinish > 0 ORDER BY userid, attempt"; } if ($attempts = $DB->get_records_sql($sql, $params)) { $attemptsbyuser = quiz_report_index_by_keys($attempts, array('userid', 'id'));