From e331eb06fdbb4adc5ebce3cbf4aacda7ce7a5ed5 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 22 Oct 2002 06:52:23 +0000 Subject: [PATCH] Teacher can now regrade (recalculate grades) of all quiz attempts (in case the quiz changed somehow) --- lang/en/quiz.php | 3 +++ mod/quiz/attempt.php | 2 +- mod/quiz/lib.php | 8 +++---- mod/quiz/report.php | 57 +++++++++++++++++++++++++++++++++++++++++++- mod/quiz/view.php | 5 ++++ 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/lang/en/quiz.php b/lang/en/quiz.php index 8b33d6567b..49783a96c9 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -56,6 +56,7 @@ $string['introduction'] = "Introduction"; $string['marks'] = "Marks"; $string['multichoice'] = "Multiple Choice"; $string['noanswers'] = "No answers were selected!"; +$string['noattempts'] = "No attempts have been made on this quiz"; $string['nomoreattempts'] = "No more attempts are allowed"; $string['noquestions'] = "No questions have been added yet"; $string['publish'] = "Publish"; @@ -69,6 +70,8 @@ $string['quizclosed'] = "This quiz closed on \$a"; $string['quizopen'] = "Open the quiz"; $string['quiznotavailable'] = "The quiz will not be available until: \$a"; $string['random'] = "Random set"; +$string['regrade'] = "Regrade all attempts"; +$string['regradecomplete'] = "All attempts have been regraded"; $string['rename'] = "Rename"; $string['report'] = "Reports"; $string['save'] = "Save"; diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 9fbdbffbb7..5b1f6eefd8 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -120,7 +120,7 @@ exit; } - if (! quiz_save_best_grade($quiz, $USER)) { + if (! quiz_save_best_grade($quiz, $USER->id)) { error("Sorry! Could not calculate your best grade!"); } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 03aa70df08..8118abfdc3 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -748,11 +748,11 @@ function quiz_get_grade_records($quiz) { AND qg.user = u.id"); } -function quiz_save_best_grade($quiz, $user) { +function quiz_save_best_grade($quiz, $userid) { /// Calculates the best grade out of all attempts at a quiz for a user, /// and then saves that grade in the quiz_grades table. - if (!$attempts = quiz_get_user_attempts($quiz->id, $user->id)) { + if (!$attempts = quiz_get_user_attempts($quiz->id, $userid)) { notify("Could not find any user attempts"); return false; } @@ -760,7 +760,7 @@ function quiz_save_best_grade($quiz, $user) { $bestgrade = quiz_calculate_best_grade($quiz, $attempts); $bestgrade = (($bestgrade / $quiz->sumgrades) * $quiz->grade); - if ($grade = get_record_sql("SELECT * FROM quiz_grades WHERE quiz='$quiz->id' AND user='$user->id'")) { + if ($grade = get_record_sql("SELECT * FROM quiz_grades WHERE quiz='$quiz->id' AND user='$userid'")) { $grade->grade = $bestgrade; $grade->timemodified = time(); if (!update_record("quiz_grades", $grade)) { @@ -769,7 +769,7 @@ function quiz_save_best_grade($quiz, $user) { } } else { $grade->quiz = $quiz->id; - $grade->user = $user->id; + $grade->user = $userid; $grade->grade = round($bestgrade, 2); $grade->timemodified = time(); if (!insert_record("quiz_grades", $grade)) { diff --git a/mod/quiz/report.php b/mod/quiz/report.php index 7169b1b151..cdb67ce3eb 100644 --- a/mod/quiz/report.php +++ b/mod/quiz/report.php @@ -9,6 +9,7 @@ optional_variable($q); // quiz ID optional_variable($attempt); // A particular attempt ID + optional_variable($regrade); // Regrade all attempts if ($id) { if (! $cm = get_record("course_modules", "id", $id)) { @@ -80,8 +81,14 @@ error("Could not re-grade this quiz attempt!"); } + if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { + $timetaken = format_time($timetaken); + } else { + $timetaken = "-"; + } + $table->align = array("RIGHT", "LEFT"); - $table->data[] = array("$strtimetaken:", format_time($attempt->timefinish - $attempt->timestart)); + $table->data[] = array("$strtimetaken:", $timetaken); $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish)); $table->data[] = array("$strscore:", "$result->sumgrades/$quiz->sumgrades ($result->percentage %)"); $table->data[] = array("$strgrade:", "$result->grade/$quiz->grade"); @@ -98,6 +105,48 @@ exit; } + if ($regrade) { + if (!$attempts = get_records("quiz_attempts", "quiz", $quiz->id)) { + print_header(get_string("noattempts", "quiz")); + print_continue("report.php?id=$cm->id"); + print_footer($course); + exit; + } + + $users = array(); + foreach ($attempts as $attempt) { + if (! $questions = quiz_get_attempt_responses($attempt)) { + error("Could not reconstruct quiz results for attempt $attempt->id!"); + } + + if (!$result = quiz_grade_attempt_results($quiz, $questions)) { + error("Could not re-grade this quiz attempt!"); + } + + echo "

$attempt->sumgrades --> $result->sumgrades

"; + $attempt->sumgrades = $result->sumgrades; + + if (! update_record("quiz_attempts", $attempt)) { + notify("Could not regrade attempt $attempt->id"); + continue; + } + + $users[$attempt->user] = $attempt->user; + } + + if ($users) { + foreach ($users as $userid) { + if (! quiz_save_best_grade($quiz, $userid)) { + notify("Could not save best grade for user $userid!"); + } + } + } + print_heading(get_string("regradecomplete", "quiz")); + print_continue("report.php?id=$cm->id"); + print_footer($course); + exit; + } + if (!$grades = quiz_get_grade_records($quiz)) { print_footer($course); exit; @@ -121,6 +170,12 @@ print_table($table); + echo "

"; + $options["regrade"] = "true"; + $options["id"] = $cm->id; + print_single_button("report.php", $options, get_string("regrade", "quiz")); + echo "

"; + // Finish the page print_footer($course); diff --git a/mod/quiz/view.php b/mod/quiz/view.php index 0efaf88d46..52b7782620 100644 --- a/mod/quiz/view.php +++ b/mod/quiz/view.php @@ -101,6 +101,11 @@ $table->align = array("CENTER", "CENTER", "LEFT", "RIGHT"); $table->width = array("", "", "", ""); foreach ($attempts as $attempt) { + if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { + $timetaken = format_time($timetaken); + } else { + $timetaken = "-"; + } $table->data[] = array( $attempt->attempt, format_time($attempt->timefinish - $attempt->timestart), userdate($attempt->timefinish), -- 2.39.5