Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / exercise / view.php
blob5ee7fc7addc3f784e74e3b41fd9b2737bcf1a267
1 <?php // $Id$
3 /*************************************************
4 ACTIONS handled are:
6 displayfinalgrade (for students)
7 makeleaguetableavailable (for teachers)
8 notavailable (for students)
9 openexercise (for teachers)
10 setupassignment (for teachers)
11 showsubmissions (for students)
12 studentsview
13 submitassignment
14 teachersview
16 ************************************************/
18 require_once("../../config.php");
19 require_once("lib.php");
20 require_once("locallib.php");
22 $id = required_param('id', PARAM_INT); // Course Module ID
23 $action = optional_param('action', '', PARAM_ALPHA);
24 $changegroup = optional_param('group', -1, PARAM_INT);
26 // get some esential stuff...
27 if (! $cm = get_coursemodule_from_id('exercise', $id)) {
28 error("Course Module ID was incorrect");
31 if (! $course = get_record("course", "id", $cm->course)) {
32 error("Course is misconfigured");
35 if (! $exercise = get_record("exercise", "id", $cm->instance)) {
36 error("Course module is incorrect");
39 require_login($course->id, false, $cm);
41 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
43 // ...log activity...
44 add_to_log($course->id, "exercise", "view", "view.php?id=$cm->id", $exercise->id, $cm->id);
46 $strexercises = get_string("modulenameplural", "exercise");
47 $strexercise = get_string("modulename", "exercise");
49 // ...display header...
50 $navigation = build_navigation('', $cm);
52 print_header_simple(format_string($exercise->name), "", $navigation,
53 "", "", true, update_module_button($cm->id, $course->id, $strexercise), navmenu($course, $cm));
55 if (isteacher($course->id)) {
56 if (empty($action)) { // no action specified, either go straight to elements page else the admin page
57 // has the assignment any elements
58 if (count_records("exercise_elements", "exerciseid", $exercise->id)) {
59 $action = "teachersview";
61 else {
62 redirect("assessments.php?action=editelements&amp;id=$cm->id");
66 elseif (!isguest()) { // it's a student then
67 if (!$cm->visible) {
68 notice(get_string("activityiscurrentlyhidden"));
70 switch ($exercise->phase) {
71 case 0 :
72 case 1 : $action = 'notavailable'; break;
73 case 2 : $action = 'studentsview'; break;
74 case 3 : $action = 'displayfinalgrade';
77 else { // it's a guest, oh no!
78 $action = 'notavailable';
82 /****************** display final grade (for students) ************************************/
83 if ($action == 'displayfinalgrade' ) {
85 // show the final grades as stored in the tables...
86 print_heading(get_string("displayoffinalgrades", "exercise"));
87 if ($submissions = exercise_get_user_submissions($exercise, $USER)) { // any submissions from user?
88 echo "<center><table border=\"1\" width=\"90%\"><tr>";
89 echo "<td><b>".get_string("submissions", "exercise")."</b></td>";
90 echo "<td align=\"center\"><b>".get_string("gradeforassessment", "exercise")."</b></td>";
91 echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "exercise", $course->teacher)."</b></td>";
92 echo "<td align=\"center\"><b>".get_string("overallgrade", "exercise")."</b></td></tr>\n";
93 // now the weights
94 echo "<tr><td><b>".get_string("maximumgrade")."</b></td>";
95 echo "<td align=\"center\"><b>$exercise->gradinggrade</b></td>\n";
96 echo "<td align=\"center\"><b>$exercise->grade</b></td>\n";
97 echo "<td><b>&nbsp;</b></td></tr>\n";
98 // first get user's own assessment reord, it should contain their grading grade
99 if ($ownassessments = exercise_get_user_assessments($exercise, $USER)) {
100 foreach ($ownassessments as $ownassessment) {
101 break; // there should only be one
104 else {
105 $ownassessment->gradinggrade = 0;
107 foreach ($submissions as $submission) {
108 if ($assessments = exercise_get_assessments($submission)) {
109 foreach ($assessments as $assessment) { // (normally there should only be one
110 $gradinggrade = number_format($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0,
112 $grade = number_format($assessment->grade * $exercise->grade / 100.0, 1);
113 $overallgrade = number_format(($assessment->grade * $exercise->grade / 100.0) +
114 ($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0 ), 1);
115 if ($submission->late) {
116 $grade = "<font color=\"red\">(".$grade.")</font>";
117 $overallgrade = "<font color=\"red\">(".$overallgrade.")</font>";
119 echo "<tr><td>".exercise_print_submission_title($exercise, $submission)."</td>\n";
120 echo "<td align=\"center\">$gradinggrade</td>";
121 echo "<td align=\"center\">$grade</td>";
122 echo "<td align=\"center\">$overallgrade</td></tr>\n";
127 echo "</table></center><br clear=\"all\" />\n";
128 if ($exercise->showleaguetable) {
129 exercise_print_league_table($exercise);
131 echo "<br />".get_string("maximumgrade").": $exercise->grade<br />\n";
135 /****************** make final grades available (for teachers only)**************/
136 elseif ($action == 'makeleaguetableavailable') {
138 if (!isteacheredit($course->id)) {
139 error("Only teachers with editing permissions can do this.");
142 set_field("exercise", "phase", 3, "id", "$exercise->id");
143 add_to_log($course->id, "exercise", "display", "view.php?id=$cm->id", "$exercise->id", $cm->id);
144 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 3));
148 /*********************** assignment not available (for students)***********************/
149 elseif ($action == 'notavailable') {
150 print_heading(get_string("notavailable", "exercise"));
154 /****************** open exercise for student assessments and submissions (phase 2) (for teachers)**/
155 elseif ($action == 'openexercise') {
157 if (!isteacheredit($course->id)) {
158 error("Only teachers with editing permissions can do this.");
161 // move to phase 2, check that teacher has made enough submissions
162 if (exercise_count_teacher_submissions($exercise) == 0) {
163 redirect("view.php?id=$cm->id", get_string("noexercisedescriptionssubmitted", "exercise"));
165 elseif (($exercise->gradingstrategy == 3) and ($exercise->nelements ==1 )) {
166 // only one criterion specified
167 redirect("view.php?id=$cm->id", get_string("numberofcriterionelements", "exercise"));
168 } else {
169 set_field("exercise", "phase", 2, "id", "$exercise->id");
170 add_to_log($course->id, "exercise", "open", "view.php?id=$cm->id", "$exercise->id", $cm->id);
171 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 2));
176 /****************** set up assignment (move back to phase 1) (for teachers)***********************/
177 elseif ($action == 'setupassignment') {
179 if (!isteacher($course->id)) {
180 error("Only teachers with editing permissions can do this.");
183 set_field("exercise", "phase", 1, "id", "$exercise->id");
184 add_to_log($course->id, "exercise", "set up", "view.php?id=$cm->id", "$exercise->id", $cm->id);
185 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 1));
189 /****************** showsubmissions (for students, in phase 3)***********************/
190 elseif ($action == 'showsubmissions') {
191 exercise_print_assignment_info($exercise);
192 print_heading(get_string("submissionsnowclosed", "exercise"));
193 // show student's assessment (linked to the teacher's exercise/submission
194 print_heading(get_string("yourassessment", "exercise"));
195 exercise_list_teacher_submissions($exercise, $USER);
196 echo "<hr size=\"1\" noshade=\"noshade\" />";
197 if ($submissions = exercise_get_user_submissions($exercise, $USER)) {
198 print_heading(get_string("yoursubmission", "exercise"));
199 print_simple_box_start("center");
200 $table->head = array (get_string("submission", "exercise"), get_string("submitted", "exercise"),
201 get_string("assessed", "exercise"), get_string("grade"));
202 $table->width = "100%";
203 $table->align = array ("left", "left", "left", "center");
204 $table->size = array ("*", "*", "*", "*");
205 $table->cellpadding = 2;
206 $table->cellspacing = 0;
208 foreach ($submissions as $submission) {
209 if ($assessments = exercise_get_assessments($submission)) {
210 // should only be one but we'll loop anyway
211 foreach ($assessments as $assessment) {
212 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
213 userdate($submission->timecreated), userdate($assessment->timecreated),
214 "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".$assessment->grade * $exercise->grade / 100.0."</a>");
216 } else {
217 // submission not yet assessed (by teacher)
218 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
219 userdate($submission->timecreated), get_string("notassessedyet", "exercise"), 0);
222 print_table($table);
223 print_simple_box_end();
224 } else {
225 print_heading(get_string("nosubmissions", "exercise"));
227 // always allow student to resubmit
228 if (exercise_test_for_resubmission($exercise, $USER)) {
229 // if resubmission requested print upload form
230 echo "<hr size=\"1\" noshade=\"noshade\" />";
231 print_heading(get_string("pleasesubmityourwork", "exercise").":");
232 exercise_print_upload_form($exercise);
234 echo "<hr size=\"1\" noshade=\"noshade\" />";
238 /****************** student's view could be in 1 of 3 stages ***********************/
239 elseif ($action == 'studentsview') {
240 exercise_print_assignment_info($exercise);
241 // is a password needed?
242 if ($exercise->usepassword) {
243 $correctpass = false;
244 if (isset($_POST['userpassword'])) {
245 if ($exercise->password == md5(trim($_POST['userpassword']))) {
246 $USER->exerciseloggedin[$exercise->id] = true;
247 $correctpass = true;
249 } elseif (isset($USER->exerciseloggedin[$exercise->id])) {
250 $correctpass = true;
253 if (!$correctpass) {
254 print_simple_box_start("center");
255 echo "<form id=\"password\" method=\"post\" action=\"view.php\">\n";
256 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
257 echo "<table cellpadding=\"7px\">";
258 if (isset($_POST['userpassword'])) {
259 echo "<tr align=\"center\" style='color:#DF041E;'><td>".get_string("wrongpassword", "exercise").
260 "</td></tr>";
262 echo "<tr align=\"center\"><td>".get_string("passwordprotectedexercise", "exercise", format_string($exercise->name)).
263 "</td></tr>";
264 echo "<tr align=\"center\"><td>".get_string("enterpassword", "exercise").
265 " <input type=\"password\" name=\"userpassword\" /></td></tr>";
267 echo "<tr align=\"center\"><td>";
268 echo "<input type=\"button\" value=\"".get_string("cancel").
269 "\" onclick=\"parent.location='../../course/view.php?id=$course->id';\"> ";
270 echo "<input type=\"button\" value=\"".get_string("continue").
271 "\" onclick=\"document.password.submit();\" />";
272 echo "</td></tr></table>";
273 print_simple_box_end();
274 exit();
277 // in Stage 1 - the student must make an assessment (linked to the teacher's exercise/submission
278 if (!exercise_test_user_assessments($exercise, $USER)) {
279 print_heading(get_string("pleaseviewtheexercise", "exercise", $course->teacher));
280 exercise_list_teacher_submissions($exercise, $USER);
282 // in stage 2? - submit own first attempt
283 else {
284 // show assessment the teacher's examples, there may be feedback from teacher
285 if (exercise_count_user_submissions($exercise, $USER) == 0) {
286 print_heading(get_string("atthisstageyou", "exercise", $course->teacher));
287 exercise_list_teacher_submissions($exercise, $USER, true); // true = allow re-assessing
288 // print upload form
289 print_heading(get_string("pleasesubmityourwork", "exercise").":");
290 exercise_print_upload_form($exercise);
292 // in stage 3? - awaiting grading of assessment and assessment of work by teacher,
293 // may resubmit if allowed
294 else {
295 exercise_list_teacher_submissions($exercise, $USER);
296 echo "<hr size=\"1\" noshade=\"noshade\" />";
297 print_heading(get_string("yoursubmission", "exercise"));
298 exercise_list_user_submissions($exercise, $USER);
299 if (exercise_test_for_resubmission($exercise, $USER)) {
300 // if resubmission requested print upload form
301 echo "<hr size=\"1\" noshade=\"noshade\" />";
302 print_heading(get_string("pleasesubmityourwork", "exercise").":");
303 exercise_print_upload_form($exercise);
304 echo "<hr size=\"1\" noshade=\"noshade\" />";
311 /****************** submission of assignment by teacher only***********************/
312 elseif ($action == 'submitassignment') {
314 if (!has_capability('mod/exercise:assess', $context)) {
315 error("Only teachers with editing permissions can do this.");
318 exercise_print_assignment_info($exercise);
320 // list previous submissions from this user
321 exercise_list_user_submissions($exercise, $USER);
323 echo "<hr size=\"1\" noshade=\"noshade\" />";
325 // print upload form
326 print_heading(get_string("submitexercisedescription", "exercise").":");
327 exercise_print_upload_form($exercise);
331 /****************** teacher's view - display admin page (current phase options) ************/
332 elseif ($action == 'teachersview') {
334 if (!isteacher($course->id)) {
335 error("Only teachers can look at this page");
338 /// Check to see if groups are being used in this exercise
339 /// and if so, set $currentgroup to reflect the current group
340 $groupmode = groupmode($course, $cm); // Groups are being used?
341 $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
343 print_heading_with_help(get_string("managingassignment", "exercise"), "managing", "exercise");
345 exercise_print_assignment_info($exercise);
346 $tabs->names = array("1. ".get_string("phase1", "exercise"),
347 "2. ".get_string("phase2", "exercise", $course->student),
348 "3. ".get_string("phase3", "exercise", $course->student));
349 if (isteacheredit($course->id)) {
350 $tabs->urls = array("view.php?id=$cm->id&amp;action=setupassignment",
351 "view.php?id=$cm->id&amp;action=openexercise",
352 "view.php?id=$cm->id&amp;action=makeleaguetableavailable");
353 } else {
354 // don't allow non-editing teacher to change phase
355 $tabs->urls = array("view.php?id=$cm->id",
356 "view.php?id=$cm->id",
357 "view.php?id=$cm->id");
359 if ($exercise->phase) { // phase 1 or more
360 $tabs->highlight = $exercise->phase - 1;
361 } else {
362 $tabs->highlight = 0; // phase is zero
364 exercise_print_tabbed_heading($tabs);
366 echo "<center>\n";
367 switch ($exercise->phase) {
368 case 0:
369 case 1: // set up assignment
370 if (isteacheredit($course->id)) {
371 echo "<p><b><a href=\"assessments.php?id=$cm->id&amp;action=editelements\">".
372 get_string("amendassessmentelements", "exercise")."</a></b></p> \n";
373 helpbutton("elements", get_string("amendassessmentelements", "exercise"), "exercise");
374 echo "<p><b><a href=\"view.php?id=$cm->id&amp;action=submitassignment\">".
375 get_string("submitexercisedescription", "exercise")."</a></b></p> \n";
376 helpbutton("submissionofdescriptions", get_string("submitexercisedescription", "exercise"), "exercise");
378 break;
380 case 2: // submissions and assessments
381 // just show student submissions link, the (self) assessments are show above the assessment form for
382 // the submissions
383 echo "<p><b><a href=\"submissions.php?id=$cm->id&amp;action=listforassessmentstudent\">".
384 get_string("studentsubmissionsforassessment", "exercise",
385 exercise_count_unassessed_student_submissions($exercise))."</a></b></p> \n";
386 helpbutton("grading", get_string("studentsubmissionsforassessment", "exercise"),
387 "exercise");
388 break;
390 case 3: // show final grades
391 echo "<p><b><a href=\"submissions.php?id=$cm->id&amp;action=listforassessmentstudent\">".
392 get_string("studentsubmissionsforassessment", "exercise",
393 exercise_count_unassessed_student_submissions($exercise))."</a></b></p> \n";
394 helpbutton("grading", get_string("studentsubmissionsforassessment", "exercise"),
395 "exercise");
396 print_heading("<a href=\"submissions.php?id=$cm->id&amp;action=displayfinalgrades\">".
397 get_string("displayoffinalgrades", "exercise")."</a>");
399 print_heading("<a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
400 get_string("administration")."</a>");
401 echo "</center>\n";
405 /*************** no man's land **************************************/
406 else {
407 error("Fatal Error: Unknown Action: ".$action."\n");
410 print_footer($course);