MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / exercise / view.php
blob3cf1b12375eb85edcd8124cffbc1968967c76503
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 $navlinks = array();
51 $navlinks[] = array('name' => $strexercises, 'link' => "index.php?id=$course->id", 'type' => 'activity');
52 $navlinks[] = array('name' => format_string($exercise->name), 'link' => '', 'type' => 'activityinstance');
53 $navigation = build_navigation($navlinks);
55 print_header_simple(format_string($exercise->name), "", $navigation,
56 "", "", true, update_module_button($cm->id, $course->id, $strexercise), navmenu($course, $cm));
58 if (isteacher($course->id)) {
59 if (empty($action)) { // no action specified, either go straight to elements page else the admin page
60 // has the assignment any elements
61 if (count_records("exercise_elements", "exerciseid", $exercise->id)) {
62 $action = "teachersview";
64 else {
65 redirect("assessments.php?action=editelements&amp;id=$cm->id");
69 elseif (!isguest()) { // it's a student then
70 if (!$cm->visible) {
71 notice(get_string("activityiscurrentlyhidden"));
73 switch ($exercise->phase) {
74 case 0 :
75 case 1 : $action = 'notavailable'; break;
76 case 2 : $action = 'studentsview'; break;
77 case 3 : $action = 'displayfinalgrade';
80 else { // it's a guest, oh no!
81 $action = 'notavailable';
85 /****************** display final grade (for students) ************************************/
86 if ($action == 'displayfinalgrade' ) {
88 // show the final grades as stored in the tables...
89 print_heading(get_string("displayoffinalgrades", "exercise"));
90 if ($submissions = exercise_get_user_submissions($exercise, $USER)) { // any submissions from user?
91 echo "<center><table border=\"1\" width=\"90%\"><tr>";
92 echo "<td><b>".get_string("submissions", "exercise")."</b></td>";
93 echo "<td align=\"center\"><b>".get_string("gradeforassessment", "exercise")."</b></td>";
94 echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "exercise", $course->teacher)."</b></td>";
95 echo "<td align=\"center\"><b>".get_string("overallgrade", "exercise")."</b></td></tr>\n";
96 // now the weights
97 echo "<tr><td><b>".get_string("maximumgrade")."</b></td>";
98 echo "<td align=\"center\"><b>$exercise->gradinggrade</b></td>\n";
99 echo "<td align=\"center\"><b>$exercise->grade</b></td>\n";
100 echo "<td><b>&nbsp;</b></td></tr>\n";
101 // first get user's own assessment reord, it should contain their grading grade
102 if ($ownassessments = exercise_get_user_assessments($exercise, $USER)) {
103 foreach ($ownassessments as $ownassessment) {
104 break; // there should only be one
107 else {
108 $ownassessment->gradinggrade = 0;
110 foreach ($submissions as $submission) {
111 if ($assessments = exercise_get_assessments($submission)) {
112 foreach ($assessments as $assessment) { // (normally there should only be one
113 $gradinggrade = number_format($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0,
115 $grade = number_format($assessment->grade * $exercise->grade / 100.0, 1);
116 $overallgrade = number_format(($assessment->grade * $exercise->grade / 100.0) +
117 ($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0 ), 1);
118 if ($submission->late) {
119 $grade = "<font color=\"red\">(".$grade.")</font>";
120 $overallgrade = "<font color=\"red\">(".$overallgrade.")</font>";
122 echo "<tr><td>".exercise_print_submission_title($exercise, $submission)."</td>\n";
123 echo "<td align=\"center\">$gradinggrade</td>";
124 echo "<td align=\"center\">$grade</td>";
125 echo "<td align=\"center\">$overallgrade</td></tr>\n";
130 echo "</table></center><br clear=\"all\" />\n";
131 if ($exercise->showleaguetable) {
132 exercise_print_league_table($exercise);
134 echo "<br />".get_string("maximumgrade").": $exercise->grade<br />\n";
138 /****************** make final grades available (for teachers only)**************/
139 elseif ($action == 'makeleaguetableavailable') {
141 if (!isteacheredit($course->id)) {
142 error("Only teachers with editing permissions can do this.");
145 set_field("exercise", "phase", 3, "id", "$exercise->id");
146 add_to_log($course->id, "exercise", "display", "view.php?id=$cm->id", "$exercise->id", $cm->id);
147 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 3));
151 /*********************** assignment not available (for students)***********************/
152 elseif ($action == 'notavailable') {
153 print_heading(get_string("notavailable", "exercise"));
157 /****************** open exercise for student assessments and submissions (phase 2) (for teachers)**/
158 elseif ($action == 'openexercise') {
160 if (!isteacheredit($course->id)) {
161 error("Only teachers with editing permissions can do this.");
164 // move to phase 2, check that teacher has made enough submissions
165 if (exercise_count_teacher_submissions($exercise) == 0) {
166 redirect("view.php?id=$cm->id", get_string("noexercisedescriptionssubmitted", "exercise"));
168 elseif (($exercise->gradingstrategy == 3) and ($exercise->nelements ==1 )) {
169 // only one criterion specified
170 redirect("view.php?id=$cm->id", get_string("numberofcriterionelements", "exercise"));
171 } else {
172 set_field("exercise", "phase", 2, "id", "$exercise->id");
173 add_to_log($course->id, "exercise", "open", "view.php?id=$cm->id", "$exercise->id", $cm->id);
174 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 2));
179 /****************** set up assignment (move back to phase 1) (for teachers)***********************/
180 elseif ($action == 'setupassignment') {
182 if (!isteacher($course->id)) {
183 error("Only teachers with editing permissions can do this.");
186 set_field("exercise", "phase", 1, "id", "$exercise->id");
187 add_to_log($course->id, "exercise", "set up", "view.php?id=$cm->id", "$exercise->id", $cm->id);
188 redirect("view.php?id=$cm->id", get_string("movingtophase", "exercise", 1));
192 /****************** showsubmissions (for students, in phase 3)***********************/
193 elseif ($action == 'showsubmissions') {
194 exercise_print_assignment_info($exercise);
195 print_heading(get_string("submissionsnowclosed", "exercise"));
196 // show student's assessment (linked to the teacher's exercise/submission
197 print_heading(get_string("yourassessment", "exercise"));
198 exercise_list_teacher_submissions($exercise, $USER);
199 echo "<hr size=\"1\" noshade=\"noshade\" />";
200 if ($submissions = exercise_get_user_submissions($exercise, $USER)) {
201 print_heading(get_string("yoursubmission", "exercise"));
202 print_simple_box_start("center");
203 $table->head = array (get_string("submission", "exercise"), get_string("submitted", "exercise"),
204 get_string("assessed", "exercise"), get_string("grade"));
205 $table->width = "100%";
206 $table->align = array ("left", "left", "left", "center");
207 $table->size = array ("*", "*", "*", "*");
208 $table->cellpadding = 2;
209 $table->cellspacing = 0;
211 foreach ($submissions as $submission) {
212 if ($assessments = exercise_get_assessments($submission)) {
213 // should only be one but we'll loop anyway
214 foreach ($assessments as $assessment) {
215 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
216 userdate($submission->timecreated), userdate($assessment->timecreated),
217 "<a href=\"assessments.php?action=viewassessment&amp;id=$cm->id&amp;aid=$assessment->id\">".$assessment->grade * $exercise->grade / 100.0."</a>");
219 } else {
220 // submission not yet assessed (by teacher)
221 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
222 userdate($submission->timecreated), get_string("notassessedyet", "exercise"), 0);
225 print_table($table);
226 print_simple_box_end();
227 } else {
228 print_heading(get_string("nosubmissions", "exercise"));
230 // always allow student to resubmit
231 if (exercise_test_for_resubmission($exercise, $USER)) {
232 // if resubmission requested print upload form
233 echo "<hr size=\"1\" noshade=\"noshade\" />";
234 print_heading(get_string("pleasesubmityourwork", "exercise").":");
235 exercise_print_upload_form($exercise);
237 echo "<hr size=\"1\" noshade=\"noshade\" />";
241 /****************** student's view could be in 1 of 3 stages ***********************/
242 elseif ($action == 'studentsview') {
243 exercise_print_assignment_info($exercise);
244 // is a password needed?
245 if ($exercise->usepassword) {
246 $correctpass = false;
247 if (isset($_POST['userpassword'])) {
248 if ($exercise->password == md5(trim($_POST['userpassword']))) {
249 $USER->exerciseloggedin[$exercise->id] = true;
250 $correctpass = true;
252 } elseif (isset($USER->exerciseloggedin[$exercise->id])) {
253 $correctpass = true;
256 if (!$correctpass) {
257 print_simple_box_start("center");
258 echo "<form id=\"password\" method=\"post\" action=\"view.php\">\n";
259 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
260 echo "<table cellpadding=\"7px\">";
261 if (isset($_POST['userpassword'])) {
262 echo "<tr align=\"center\" style='color:#DF041E;'><td>".get_string("wrongpassword", "exercise").
263 "</td></tr>";
265 echo "<tr align=\"center\"><td>".get_string("passwordprotectedexercise", "exercise", format_string($exercise->name)).
266 "</td></tr>";
267 echo "<tr align=\"center\"><td>".get_string("enterpassword", "exercise").
268 " <input type=\"password\" name=\"userpassword\" /></td></tr>";
270 echo "<tr align=\"center\"><td>";
271 echo "<input type=\"button\" value=\"".get_string("cancel").
272 "\" onclick=\"parent.location='../../course/view.php?id=$course->id';\"> ";
273 echo "<input type=\"button\" value=\"".get_string("continue").
274 "\" onclick=\"document.password.submit();\" />";
275 echo "</td></tr></table>";
276 print_simple_box_end();
277 exit();
280 // in Stage 1 - the student must make an assessment (linked to the teacher's exercise/submission
281 if (!exercise_test_user_assessments($exercise, $USER)) {
282 print_heading(get_string("pleaseviewtheexercise", "exercise", $course->teacher));
283 exercise_list_teacher_submissions($exercise, $USER);
285 // in stage 2? - submit own first attempt
286 else {
287 // show assessment the teacher's examples, there may be feedback from teacher
288 if (exercise_count_user_submissions($exercise, $USER) == 0) {
289 print_heading(get_string("atthisstageyou", "exercise", $course->teacher));
290 exercise_list_teacher_submissions($exercise, $USER, true); // true = allow re-assessing
291 // print upload form
292 print_heading(get_string("pleasesubmityourwork", "exercise").":");
293 exercise_print_upload_form($exercise);
295 // in stage 3? - awaiting grading of assessment and assessment of work by teacher,
296 // may resubmit if allowed
297 else {
298 exercise_list_teacher_submissions($exercise, $USER);
299 echo "<hr size=\"1\" noshade=\"noshade\" />";
300 print_heading(get_string("yoursubmission", "exercise"));
301 exercise_list_user_submissions($exercise, $USER);
302 if (exercise_test_for_resubmission($exercise, $USER)) {
303 // if resubmission requested print upload form
304 echo "<hr size=\"1\" noshade=\"noshade\" />";
305 print_heading(get_string("pleasesubmityourwork", "exercise").":");
306 exercise_print_upload_form($exercise);
307 echo "<hr size=\"1\" noshade=\"noshade\" />";
314 /****************** submission of assignment by teacher only***********************/
315 elseif ($action == 'submitassignment') {
317 if (!has_capability('mod/exercise:assess', $context)) {
318 error("Only teachers with editing permissions can do this.");
321 exercise_print_assignment_info($exercise);
323 // list previous submissions from this user
324 exercise_list_user_submissions($exercise, $USER);
326 echo "<hr size=\"1\" noshade=\"noshade\" />";
328 // print upload form
329 print_heading(get_string("submitexercisedescription", "exercise").":");
330 exercise_print_upload_form($exercise);
334 /****************** teacher's view - display admin page (current phase options) ************/
335 elseif ($action == 'teachersview') {
337 if (!isteacher($course->id)) {
338 error("Only teachers can look at this page");
341 /// Check to see if groups are being used in this exercise
342 /// and if so, set $currentgroup to reflect the current group
343 $groupmode = groupmode($course, $cm); // Groups are being used?
344 $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
346 print_heading_with_help(get_string("managingassignment", "exercise"), "managing", "exercise");
348 exercise_print_assignment_info($exercise);
349 $tabs->names = array("1. ".get_string("phase1", "exercise"),
350 "2. ".get_string("phase2", "exercise", $course->student),
351 "3. ".get_string("phase3", "exercise", $course->student));
352 if (isteacheredit($course->id)) {
353 $tabs->urls = array("view.php?id=$cm->id&amp;action=setupassignment",
354 "view.php?id=$cm->id&amp;action=openexercise",
355 "view.php?id=$cm->id&amp;action=makeleaguetableavailable");
356 } else {
357 // don't allow non-editing teacher to change phase
358 $tabs->urls = array("view.php?id=$cm->id",
359 "view.php?id=$cm->id",
360 "view.php?id=$cm->id");
362 if ($exercise->phase) { // phase 1 or more
363 $tabs->highlight = $exercise->phase - 1;
364 } else {
365 $tabs->highlight = 0; // phase is zero
367 exercise_print_tabbed_heading($tabs);
369 echo "<center>\n";
370 switch ($exercise->phase) {
371 case 0:
372 case 1: // set up assignment
373 if (isteacheredit($course->id)) {
374 echo "<p><b><a href=\"assessments.php?id=$cm->id&amp;action=editelements\">".
375 get_string("amendassessmentelements", "exercise")."</a></b></p> \n";
376 helpbutton("elements", get_string("amendassessmentelements", "exercise"), "exercise");
377 echo "<p><b><a href=\"view.php?id=$cm->id&amp;action=submitassignment\">".
378 get_string("submitexercisedescription", "exercise")."</a></b></p> \n";
379 helpbutton("submissionofdescriptions", get_string("submitexercisedescription", "exercise"), "exercise");
381 break;
383 case 2: // submissions and assessments
384 // just show student submissions link, the (self) assessments are show above the assessment form for
385 // the submissions
386 echo "<p><b><a href=\"submissions.php?id=$cm->id&amp;action=listforassessmentstudent\">".
387 get_string("studentsubmissionsforassessment", "exercise",
388 exercise_count_unassessed_student_submissions($exercise))."</a></b></p> \n";
389 helpbutton("grading", get_string("studentsubmissionsforassessment", "exercise"),
390 "exercise");
391 break;
393 case 3: // show final grades
394 echo "<p><b><a href=\"submissions.php?id=$cm->id&amp;action=listforassessmentstudent\">".
395 get_string("studentsubmissionsforassessment", "exercise",
396 exercise_count_unassessed_student_submissions($exercise))."</a></b></p> \n";
397 helpbutton("grading", get_string("studentsubmissionsforassessment", "exercise"),
398 "exercise");
399 print_heading("<a href=\"submissions.php?id=$cm->id&amp;action=displayfinalgrades\">".
400 get_string("displayoffinalgrades", "exercise")."</a>");
402 print_heading("<a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
403 get_string("administration")."</a>");
404 echo "</center>\n";
408 /*************** no man's land **************************************/
409 else {
410 error("Fatal Error: Unknown Action: ".$action."\n");
413 print_footer($course);