MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / exercise / submissions.php
blobeb9430965dcdb100bb037b070740e678a14ae98d
1 <?php // $Id$
3 /*************************************************
4 ACTIONS handled are:
6 adminamendtitle
7 adminclearlate
8 adminconfirmdelete
9 admindelete
10 adminlateflag
11 adminlist
12 displayfinalgrades (teachers only)
13 listforassessmentstudent
14 listforassessmentteacher
15 saveweights
16 userconfirmdelete
17 userdelete
20 ************************************************/
22 require_once("../../config.php");
23 require_once("lib.php");
24 require_once("locallib.php");
25 require_once("version.php");
27 $id = required_param('id', PARAM_INT); // Course Module ID
28 $action = required_param('action', PARAM_ALPHA);
29 $aid = optional_param('aid', 0, PARAM_INT);
30 $sid = optional_param('sid', 0, PARAM_INT);
31 $title = optional_param('title', '', PARAM_CLEAN);
33 // get some essential stuff...
34 if (! $cm = get_coursemodule_from_id('exercise', $id)) {
35 error("Course Module ID was incorrect");
38 if (! $course = get_record("course", "id", $cm->course)) {
39 error("Course is misconfigured");
42 if (! $exercise = get_record("exercise", "id", $cm->instance)) {
43 error("Course module is incorrect");
46 require_login($course->id, false, $cm);
48 $strexercises = get_string("modulenameplural", "exercise");
49 $strexercise = get_string("modulename", "exercise");
50 $strsubmissions = get_string("submissions", "exercise");
52 // ... print the header and...
53 $navlinks = array();
54 $navlinks[] = array('name' => $strexercises, 'link' => "index.php?id=$course->id", 'type' => 'activity');
55 $navlinks[] = array('name' => format_string($exercise->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
56 $navlinks[] = array('name' => $strsubmissions, 'link' => '', 'type' => 'title');
58 $navigation = build_navigation($navlinks);
59 print_header_simple(format_string($exercise->name), "", $navigation,
60 "", "", true);
63 /******************* admin amend title ************************************/
64 if ($action == 'adminamendtitle' ) {
66 if (!isteacher($course->id)) {
67 error("Only teachers can look at this page");
69 if (empty($sid)) {
70 error("Admin Amend Title: submission id missing");
73 $submission = get_record("exercise_submissions", "id", $sid);
74 print_heading(get_string("amendtitle", "exercise"));
76 <form id="amendtitleform" action="submissions.php" method="post">
77 <input type="hidden" name="action" value="adminupdatetitle" />
78 <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
79 <input type="hidden" name="sid" value="<?php echo $sid ?>" />
80 <center>
81 <table celpadding="5" border="1">
82 <?php
84 // now get the comment
85 echo "<tr valign=\"top\">\n";
86 echo " <td align=\"right\"><p><b>". get_string("title", "exercise").":</b></p></td>\n";
87 echo " <td>\n";
88 echo " <input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
89 echo " </td></tr></table>\n";
90 echo "<input type=\"submit\" value=\"".get_string("amendtitle", "exercise")."\" />\n";
91 echo "</center></form>\n";
96 /******************* admin clear late (flag) ************************************/
97 elseif ($action == 'adminclearlate' ) {
99 if (!isteacher($course->id)) {
100 error("Only teachers can look at this page");
102 if (empty($sid)) {
103 error("Admin clear late flag: submission id missing");
106 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
107 error("Admin clear late flag: can not get submission record");
109 if (set_field("exercise_submissions", "late", 0, "id", $sid)) {
110 print_heading(get_string("clearlateflag", "exercise")." ".get_string("ok"));
113 add_to_log($course->id, "exercise", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");
115 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
119 /******************* admin confirm delete ************************************/
120 elseif ($action == 'adminconfirmdelete' ) {
122 if (!isteacher($course->id)) {
123 error("Only teachers can look at this page");
125 if (empty($sid)) {
126 error("Admin confirm delete: submission id missing");
128 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
129 error("Admin delete: can not get submission record");
132 if (isteacher($course->id, $submission->userid)) {
133 if (!isteacheredit($course->id)) {
134 error("Only teacher with editing permissions can delete teacher submissions.");
136 if ($assessments = exercise_get_assessments($submission)) {
137 echo "<p align=\"center\">".get_string("deletesubmissionwarning", "exercise", count($assessments)).
138 "</p>\n";
141 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("submission", "exercise")),
142 "submissions.php?action=admindelete&amp;id=$cm->id&amp;sid=$sid", "submissions.php?id=$cm->id&amp;action=adminlist");
146 /******************* admin delete ************************************/
147 elseif ($action == 'admindelete' ) {
149 if (!isteacher($course->id)) {
150 error("Only teachers can look at this page");
152 if (empty($sid)) {
153 error("Admin delete: submission id missing");
156 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
157 error("Admin delete: can not get submission record");
159 print_string("deleting", "exercise");
160 // first get any assessments...
161 if ($assessments = exercise_get_assessments($submission)) {
162 foreach($assessments as $assessment) {
163 // ...and all the associated records...
164 delete_records("exercise_grades", "assessmentid", $assessment->id);
165 echo ".";
167 // ...now delete the assessments...
168 delete_records("exercise_assessments", "submissionid", $submission->id);
170 // ...and the submission record...
171 delete_records("exercise_submissions", "id", $submission->id);
172 // ..and finally the submitted file
173 exercise_delete_submitted_files($exercise, $submission);
174 add_to_log($course->id, "exercise", "delete", "view.php?id=$cm->id", "submission $submission->id");
176 print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
180 /******************* admin (confirm) late flag ************************************/
181 elseif ($action == 'adminlateflag' ) {
183 if (!isteacher($course->id)) {
184 error("Only teachers can look at this page");
186 if (empty($sid)) {
187 error("Admin confirm late flag: submission id missing");
189 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
190 error("Admin confirm late flag: can not get submission record");
193 notice_yesno(get_string("clearlateflag","exercise")."?",
194 "submissions.php?action=adminclearlate&amp;id=$cm->id&amp;sid=$sid",
195 "submissions.php?id=$cm->id&amp;action=adminlist");
199 /******************* list all submissions ************************************/
200 elseif ($action == 'adminlist' ) {
202 if (!isteacher($course->id)) {
203 error("Only teachers can look at this page");
206 echo "<p><small>Exercise Version-> $module->version</small></p>";
207 exercise_list_submissions_for_admin($exercise);
208 print_continue("view.php?id=$cm->id");
213 /******************* admin update title ************************************/
214 elseif ($action == 'adminupdatetitle' ) {
216 if (!isteacher($course->id)) {
217 error("Only teachers can look at this page");
219 if (empty($sid)) {
220 error("Admin Update Title: submission id missing");
223 if (set_field("exercise_submissions", "title", $title, "id", $sid)) {
224 print_heading(get_string("amendtitle", "exercise")." ".get_string("ok"));
226 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
230 /*************** display final grades (by teacher) ***************************/
231 elseif ($action == 'displayfinalgrades') {
232 $groupid = get_current_group($course->id);
233 // Get all the students
234 if (!$users = get_course_students($course->id, "u.lastname, u.firstname")) {
235 print_heading(get_string("nostudentsyet"));
236 print_footer($course);
237 exit;
240 // show the final grades as stored in the tables...
241 print_heading_with_help(get_string("displayoffinalgrades", "exercise"), "finalgrades", "exercise");
242 echo "<center><table border=\"1\" width=\"90%\"><tr>\n";
243 echo "<td><b>".$course->student."</b></td>";
244 echo "<td><b>".get_string("submission", "exercise")."</b></td>";
245 echo "<td align=\"center\"><b>".get_string("gradeforassessment", "exercise")."</b></td>";
246 echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "exercise")."</b></td>";
247 echo "<td align=\"center\"><b>".get_string("overallgrade", "exercise")."</b></td></tr>\n";
248 // now the weights
249 echo "<tr><td><b>".get_string("maximumgrade")."</b></td>";
250 echo "<td><b>&nbsp;</b></td>\n";
251 echo "<td align=\"center\"><b>$exercise->gradinggrade</b></td>\n";
252 echo "<td align=\"center\"><b>$exercise->grade</b></td>\n";
253 echo "<td><b>&nbsp;</b></td></tr>\n";
254 foreach ($users as $user) {
255 // check group membership, if necessary
256 if ($groupid) {
257 // check user's group
258 if (!groups_is_member($groupid, $user->id)) {
259 continue; // skip this user
262 // first get user's own assessment reord, it should contain their grading grade
263 if ($ownassessments = exercise_get_user_assessments($exercise, $user)) {
264 foreach ($ownassessments as $ownassessment) {
265 break; // there should only be one
268 else {
269 $ownassessment->gradinggrade = 0;
271 if ($submissions = exercise_get_user_submissions($exercise, $user)) {
272 foreach ($submissions as $submission) {
273 if ($assessments = exercise_get_assessments($submission)) {
274 foreach ($assessments as $assessment) { // (normally there should only be one
275 $gradinggrade = number_format($ownassessment->gradinggrade * $exercise->gradinggrade /
276 100.0, 1);
277 $grade = number_format($assessment->grade * $exercise->grade / 100.0, 1);
278 $overallgrade = number_format(($assessment->grade * $exercise->grade / 100.0) +
279 ($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0), 1);
280 if ($submission->late) {
281 $grade = "<font color=\"red\">(".$grade.")</font>";
282 $overallgrade = "<font color=\"red\">(".$overallgrade.")</font>";
284 echo "<tr><td>".fullname($user)."</td>\n";
285 echo "<td>".exercise_print_submission_title($exercise, $submission)."</td>\n";
286 echo "<td align=\"center\">$gradinggrade</td>";
287 echo "<td align=\"center\">$grade</td>";
288 echo "<td align=\"center\">$overallgrade</td></tr>\n";
294 echo "</table><br clear=\"all\" />\n";
295 if ($exercise->showleaguetable) {
296 exercise_print_league_table($exercise);
297 echo "<br />\n";
299 echo get_string("maximumgrade").": $exercise->grade\n";
300 print_continue("view.php?id=$cm->id");
304 /******************* list for assessment student (submissions) ************************************/
305 elseif ($action == 'listforassessmentstudent' ) {
306 if (!$users = get_course_students($course->id)) {
307 print_heading(get_string("nostudentsyet"));
308 print_footer($course);
309 exit;
311 if (!isteacher($course->id)) {
312 error("Only teachers can look at this page");
314 exercise_list_unassessed_student_submissions($exercise, $USER);
315 print_continue("view.php?id=$cm->id");
320 /******************* list for assessment teacher (submissions) ************************************/
321 elseif ($action == 'listforassessmentteacher' ) {
322 if (!$users = get_course_students($course->id)) {
323 print_heading(get_string("nostudentsyet"));
324 print_footer($course);
325 exit;
327 exercise_list_unassessed_teacher_submissions($exercise, $USER);
328 print_continue("view.php?id=$cm->id");
333 /****************** save league table entries and anonimity setting (by teacher) **************/
334 elseif ($action == 'saveleaguetable') {
336 $form = (object)$_POST;
338 if (!isteacher($course->id)) {
339 error("Only teachers can look at this page");
342 // save the number of league table entries from the form...
343 if ($form->nentries == 'All') {
344 $nentries = 99;
345 } else {
346 $nentries = $form->nentries;
348 // ...and save it
349 set_field("exercise", "showleaguetable", $nentries, "id", "$exercise->id");
351 // ...and save the anonimity setting
352 set_field("exercise", "anonymous", $form->anonymous, "id", "$exercise->id");
354 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("entriessaved", "exercise"));
357 /*************** save weights (by teacher) ***************************/
358 elseif ($action == 'saveweights') {
360 $form = (object)$_POST;
362 if (!isteacher($course->id)) {
363 error("Only teachers can look at this page");
366 // save the weights from the form...
367 if (isset($form->teacherweight)) {
368 $teacherweight = $form->teacherweight;
369 // ...and save them
370 set_field("exercise", "teacherweight", $teacherweight, "id", "$exercise->id");
373 if (isset($form->gradingweight)) {
374 $gradingweight = $form->gradingweight;
375 // ...and save them
376 set_field("exercise", "gradingweight", $gradingweight, "id", "$exercise->id");
379 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("weightssaved", "exercise"));
383 /******************* user confirm delete ************************************/
384 elseif ($action == 'userconfirmdelete' ) {
386 if (empty($sid)) {
387 error("User Confirm Delete: submission id missing");
390 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("submission", "exercise")),
391 "submissions.php?action=userdelete&amp;id=$cm->id&amp;sid=$sid", "view.php?id=$cm->id");
395 /******************* user delete ************************************/
396 elseif ($action == 'userdelete' ) {
398 if (empty($sid)) {
399 error("User Delete: submission id missing");
402 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
403 error("User Delete: can not get submission record");
405 print_string("deleting", "exercise");
406 // first get any assessments...
407 if ($assessments = exercise_get_assessments($submission)) {
408 foreach($assessments as $assessment) {
409 // ...and all the associated records...
410 delete_records("exercise_grades", "assessmentid", $assessment->id);
411 echo ".";
413 // ...now delete the assessments...
414 delete_records("exercise_assessments", "submissionid", $submission->id);
416 // ...and the submission record...
417 delete_records("exercise_submissions", "id", $submission->id);
418 // ..and finally the submitted file
419 exercise_delete_submitted_files($exercise, $submission);
420 add_to_log($course->id, "exercise", "delete", "view.php?id=$cm->id", "submission $submission->id");
422 print_continue("view.php?id=$cm->id");
426 /*************** no man's land **************************************/
428 else {
430 error("Fatal Error: Unknown Action: ".$action."\n");
435 print_footer($course);