Permalink
Browse files

Teacher can now regrade (recalculate grades) of all quiz attempts

(in case the quiz changed somehow)
  • Loading branch information...
1 parent 5ecb135 commit e331eb06fdbb4adc5ebce3cbf4aacda7ce7a5ed5 moodler committed Oct 22, 2002
Showing with 69 additions and 6 deletions.
  1. +3 −0 lang/en/quiz.php
  2. +1 −1 mod/quiz/attempt.php
  3. +4 −4 mod/quiz/lib.php
  4. +56 −1 mod/quiz/report.php
  5. +5 −0 mod/quiz/view.php
View
3 lang/en/quiz.php
@@ -56,6 +56,7 @@
$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['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";
View
2 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!");
}
View
8 mod/quiz/lib.php
@@ -748,19 +748,19 @@ 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;
}
$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)) {
View
57 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 "<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;
@@ -121,6 +170,12 @@
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);
View
5 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),

0 comments on commit e331eb0

Please sign in to comment.