Teacher can now regrade (recalculate grades) of all quiz attempts
authormoodler <moodler>
Tue, 22 Oct 2002 06:52:23 +0000 (06:52 +0000)
committermoodler <moodler>
Tue, 22 Oct 2002 06:52:23 +0000 (06:52 +0000)
(in case the quiz changed somehow)

lang/en/quiz.php
mod/quiz/attempt.php
mod/quiz/lib.php
mod/quiz/report.php
mod/quiz/view.php

index 8b33d65..49783a9 100644 (file)
@@ -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";
index 9fbdbff..5b1f6ee 100644 (file)
             exit;
         }
 
-        if (! quiz_save_best_grade($quiz, $USER)) {
+        if (! quiz_save_best_grade($quiz, $USER->id)) {
             error("Sorry! Could not calculate your best grade!");
         }
 
index 03aa70d..8118abf 100644 (file)
@@ -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)) {
index 7169b1b..cdb67ce 100644 (file)
@@ -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)) {
             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");
         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 "<P ALIGN=center>$attempt->sumgrades --> $result->sumgrades</P>";
+            $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;
 
     print_table($table);
 
+    echo "<CENTER><P>";
+    $options["regrade"] = "true";
+    $options["id"] = $cm->id;
+    print_single_button("report.php", $options, get_string("regrade", "quiz"));
+    echo "</P></CENTER>";
+
 // Finish the page
     print_footer($course);
 
index 0efaf88..52b7782 100644 (file)
         $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),