579ddad5 |
1 | <?PHP // $Id$ |
2 | |
3 | // This page prints a particular instance of quiz |
4 | |
5 | require("../../config.php"); |
6 | require("lib.php"); |
7 | |
8 | optional_variable($id); // Course Module ID, or |
9 | optional_variable($q); // quiz ID |
10 | |
e1c91df0 |
11 | optional_variable($attempt); // A particular attempt ID |
e331eb06 |
12 | optional_variable($regrade); // Regrade all attempts |
e1c91df0 |
13 | |
579ddad5 |
14 | if ($id) { |
15 | if (! $cm = get_record("course_modules", "id", $id)) { |
16 | error("Course Module ID was incorrect"); |
17 | } |
18 | |
19 | if (! $course = get_record("course", "id", $cm->course)) { |
20 | error("Course is misconfigured"); |
21 | } |
22 | |
23 | if (! $quiz = get_record("quiz", "id", $cm->instance)) { |
24 | error("Course module is incorrect"); |
25 | } |
26 | |
27 | } else { |
28 | if (! $quiz = get_record("quiz", "id", $q)) { |
29 | error("Course module is incorrect"); |
30 | } |
31 | if (! $course = get_record("course", "id", $quiz->course)) { |
32 | error("Course is misconfigured"); |
33 | } |
34 | if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) { |
35 | error("Course Module ID was incorrect"); |
36 | } |
37 | } |
38 | |
39 | require_login($course->id); |
40 | |
41 | if (!isteacher($course->id)) { |
42 | error("Only teachers can see this page"); |
43 | } |
44 | |
45 | add_to_log($course->id, "quiz", "report", "report.php?id=$cm->id", "$quiz->id"); |
46 | |
47 | // Print the page header |
48 | |
49 | if ($course->category) { |
50 | $navigation = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->"; |
51 | } |
52 | |
53 | $strquizzes = get_string("modulenameplural", "quiz"); |
54 | $strquiz = get_string("modulename", "quiz"); |
55 | $strreport = get_string("report", "quiz"); |
56 | $strname = get_string("name"); |
57 | $strattempts = get_string("attempts", "quiz"); |
6d86b5dc |
58 | $strscore = get_string("score", "quiz"); |
8d94f5a0 |
59 | $strgrade = get_string("grade"); |
f31edeeb |
60 | $strbestgrade = get_string("bestgrade", "quiz"); |
6d86b5dc |
61 | $strtimetaken = get_string("timetaken", "quiz"); |
62 | $strtimecompleted = get_string("timecompleted", "quiz"); |
579ddad5 |
63 | |
64 | print_header("$course->shortname: $quiz->name", "$course->fullname", |
65 | "$navigation <A HREF=index.php?id=$course->id>$strquizzes</A> |
66 | -> <A HREF=\"view.php?id=$cm->id\">$quiz->name</A> -> $strreport", |
67 | "", "", true); |
68 | |
69 | print_heading($quiz->name); |
70 | |
6d86b5dc |
71 | if ($attempt) { // Show a particular attempt |
72 | |
73 | if (! $attempt = get_record("quiz_attempts", "id", $attempt)) { |
74 | error("No such attempt ID exists"); |
75 | } |
76 | |
77 | if (! $questions = quiz_get_attempt_responses($attempt)) { |
78 | error("Could not reconstruct quiz results for attempt $attempt->id!"); |
79 | } |
80 | |
81 | if (!$result = quiz_grade_attempt_results($quiz, $questions)) { |
82 | error("Could not re-grade this quiz attempt!"); |
83 | } |
84 | |
e331eb06 |
85 | if ($timetaken = ($attempt->timefinish - $attempt->timestart)) { |
86 | $timetaken = format_time($timetaken); |
87 | } else { |
88 | $timetaken = "-"; |
89 | } |
90 | |
6d86b5dc |
91 | $table->align = array("RIGHT", "LEFT"); |
e331eb06 |
92 | $table->data[] = array("$strtimetaken:", $timetaken); |
6d86b5dc |
93 | $table->data[] = array("$strtimecompleted:", userdate($attempt->timefinish)); |
94 | $table->data[] = array("$strscore:", "$result->sumgrades/$quiz->sumgrades ($result->percentage %)"); |
95 | $table->data[] = array("$strgrade:", "$result->grade/$quiz->grade"); |
96 | print_table($table); |
97 | |
98 | print_continue("report.php?q=$quiz->id"); |
99 | |
100 | $quiz->feedback = true; |
101 | $quiz->correctanswers = true; |
102 | quiz_print_quiz_questions($quiz, $result); |
103 | |
104 | print_continue("report.php?q=$quiz->id"); |
105 | print_footer($course); |
106 | exit; |
107 | } |
108 | |
e331eb06 |
109 | if ($regrade) { |
110 | if (!$attempts = get_records("quiz_attempts", "quiz", $quiz->id)) { |
111 | print_header(get_string("noattempts", "quiz")); |
112 | print_continue("report.php?id=$cm->id"); |
113 | print_footer($course); |
114 | exit; |
115 | } |
116 | |
117 | $users = array(); |
ffc5a2c0 |
118 | $count->attempt = 0; |
119 | $count->changed = 0; |
e331eb06 |
120 | foreach ($attempts as $attempt) { |
ffc5a2c0 |
121 | |
cd63d77e |
122 | if (!$attempt->timefinish) { // Skip incomplete attempts |
123 | continue; |
124 | } |
ffc5a2c0 |
125 | |
126 | $count->attempt++; |
127 | |
e331eb06 |
128 | if (! $questions = quiz_get_attempt_responses($attempt)) { |
129 | error("Could not reconstruct quiz results for attempt $attempt->id!"); |
130 | } |
131 | |
132 | if (!$result = quiz_grade_attempt_results($quiz, $questions)) { |
133 | error("Could not re-grade this quiz attempt!"); |
134 | } |
135 | |
ffc5a2c0 |
136 | if ($attempt->sumgrades != $result->sumgrades) { |
137 | $attempt->sumgrades = $result->sumgrades; |
138 | $count->changed++; |
e331eb06 |
139 | |
ffc5a2c0 |
140 | if (! update_record("quiz_attempts", $attempt)) { |
141 | notify("Could not regrade attempt $attempt->id"); |
142 | } |
e331eb06 |
143 | } |
144 | |
145 | $users[$attempt->user] = $attempt->user; |
146 | } |
147 | |
148 | if ($users) { |
149 | foreach ($users as $userid) { |
150 | if (! quiz_save_best_grade($quiz, $userid)) { |
151 | notify("Could not save best grade for user $userid!"); |
152 | } |
153 | } |
154 | } |
155 | print_heading(get_string("regradecomplete", "quiz")); |
ffc5a2c0 |
156 | print_heading(get_string("regradecount", "quiz", $count)); |
e331eb06 |
157 | print_continue("report.php?id=$cm->id"); |
158 | print_footer($course); |
159 | exit; |
160 | } |
161 | |
579ddad5 |
162 | if (!$grades = quiz_get_grade_records($quiz)) { |
163 | print_footer($course); |
164 | exit; |
165 | } |
166 | |
81b635c3 |
167 | $table->head = array(" ", $strname, $strattempts, "$strbestgrade /$quiz->grade"); |
f31edeeb |
168 | $table->align = array("CENTER", "LEFT", "LEFT", "CENTER"); |
579ddad5 |
169 | $table->width = array(10, "*", "*", 20); |
170 | |
171 | foreach ($grades as $grade) { |
172 | $picture = print_user_picture($grade->user, $course->id, $grade->picture, false, true); |
173 | |
174 | if ($attempts = quiz_get_user_attempts($quiz->id, $grade->user)) { |
8d94f5a0 |
175 | $userattempts = quiz_get_user_attempts_string($quiz, $attempts, $grade->grade); |
579ddad5 |
176 | } |
177 | |
178 | $table->data[] = array ($picture, |
179 | "<A HREF=\"$CFG->wwwroot/user/view.php?id=$grade->user&course=$course->id\">$grade->firstname $grade->lastname</A>", |
e1c91df0 |
180 | "$userattempts", round($grade->grade,0)); |
579ddad5 |
181 | } |
182 | |
183 | print_table($table); |
184 | |
e331eb06 |
185 | echo "<CENTER><P>"; |
186 | $options["regrade"] = "true"; |
187 | $options["id"] = $cm->id; |
188 | print_single_button("report.php", $options, get_string("regrade", "quiz")); |
189 | echo "</P></CENTER>"; |
190 | |
579ddad5 |
191 | // Finish the page |
192 | print_footer($course); |
193 | |
194 | ?> |