MDL-9716
[moodle-linuxchix.git] / mod / exercise / submissions.php
blob8698a8812cb35cedf0a0acc8c4c7ad3aff94a30b
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 $crumbs[] = array('name' => $strexercises, 'link' => "index.php?id=$course->id", 'type' => 'activity');
54 $crumbs[] = array('name' => format_string($exercise->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
55 $crumbs[] = array('name' => $strsubmissions, 'link' => '', 'type' => 'title');
57 $navigation = build_navigation($crumbs);
58 print_header_simple(format_string($exercise->name), "", $navigation,
59 "", "", true);
62 /******************* admin amend title ************************************/
63 if ($action == 'adminamendtitle' ) {
65 if (!isteacher($course->id)) {
66 error("Only teachers can look at this page");
68 if (empty($sid)) {
69 error("Admin Amend Title: submission id missing");
72 $submission = get_record("exercise_submissions", "id", $sid);
73 print_heading(get_string("amendtitle", "exercise"));
75 <form id="amendtitleform" action="submissions.php" method="post">
76 <input type="hidden" name="action" value="adminupdatetitle" />
77 <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
78 <input type="hidden" name="sid" value="<?php echo $sid ?>" />
79 <center>
80 <table celpadding="5" border="1">
81 <?php
83 // now get the comment
84 echo "<tr valign=\"top\">\n";
85 echo " <td align=\"right\"><p><b>". get_string("title", "exercise").":</b></p></td>\n";
86 echo " <td>\n";
87 echo " <input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
88 echo " </td></tr></table>\n";
89 echo "<input type=\"submit\" value=\"".get_string("amendtitle", "exercise")."\" />\n";
90 echo "</center></form>\n";
95 /******************* admin clear late (flag) ************************************/
96 elseif ($action == 'adminclearlate' ) {
98 if (!isteacher($course->id)) {
99 error("Only teachers can look at this page");
101 if (empty($sid)) {
102 error("Admin clear late flag: submission id missing");
105 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
106 error("Admin clear late flag: can not get submission record");
108 if (set_field("exercise_submissions", "late", 0, "id", $sid)) {
109 print_heading(get_string("clearlateflag", "exercise")." ".get_string("ok"));
112 add_to_log($course->id, "exercise", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");
114 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
118 /******************* admin confirm delete ************************************/
119 elseif ($action == 'adminconfirmdelete' ) {
121 if (!isteacher($course->id)) {
122 error("Only teachers can look at this page");
124 if (empty($sid)) {
125 error("Admin confirm delete: submission id missing");
127 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
128 error("Admin delete: can not get submission record");
131 if (isteacher($course->id, $submission->userid)) {
132 if (!isteacheredit($course->id)) {
133 error("Only teacher with editing permissions can delete teacher submissions.");
135 if ($assessments = exercise_get_assessments($submission)) {
136 echo "<p align=\"center\">".get_string("deletesubmissionwarning", "exercise", count($assessments)).
137 "</p>\n";
140 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("submission", "exercise")),
141 "submissions.php?action=admindelete&amp;id=$cm->id&amp;sid=$sid", "submissions.php?id=$cm->id&amp;action=adminlist");
145 /******************* admin delete ************************************/
146 elseif ($action == 'admindelete' ) {
148 if (!isteacher($course->id)) {
149 error("Only teachers can look at this page");
151 if (empty($sid)) {
152 error("Admin delete: submission id missing");
155 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
156 error("Admin delete: can not get submission record");
158 print_string("deleting", "exercise");
159 // first get any assessments...
160 if ($assessments = exercise_get_assessments($submission)) {
161 foreach($assessments as $assessment) {
162 // ...and all the associated records...
163 delete_records("exercise_grades", "assessmentid", $assessment->id);
164 echo ".";
166 // ...now delete the assessments...
167 delete_records("exercise_assessments", "submissionid", $submission->id);
169 // ...and the submission record...
170 delete_records("exercise_submissions", "id", $submission->id);
171 // ..and finally the submitted file
172 exercise_delete_submitted_files($exercise, $submission);
173 add_to_log($course->id, "exercise", "delete", "view.php?id=$cm->id", "submission $submission->id");
175 print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
179 /******************* admin (confirm) late flag ************************************/
180 elseif ($action == 'adminlateflag' ) {
182 if (!isteacher($course->id)) {
183 error("Only teachers can look at this page");
185 if (empty($sid)) {
186 error("Admin confirm late flag: submission id missing");
188 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
189 error("Admin confirm late flag: can not get submission record");
192 notice_yesno(get_string("clearlateflag","exercise")."?",
193 "submissions.php?action=adminclearlate&amp;id=$cm->id&amp;sid=$sid",
194 "submissions.php?id=$cm->id&amp;action=adminlist");
198 /******************* list all submissions ************************************/
199 elseif ($action == 'adminlist' ) {
201 if (!isteacher($course->id)) {
202 error("Only teachers can look at this page");
205 echo "<p><small>Exercise Version-> $module->version</small></p>";
206 exercise_list_submissions_for_admin($exercise);
207 print_continue("view.php?id=$cm->id");
212 /******************* admin update title ************************************/
213 elseif ($action == 'adminupdatetitle' ) {
215 if (!isteacher($course->id)) {
216 error("Only teachers can look at this page");
218 if (empty($sid)) {
219 error("Admin Update Title: submission id missing");
222 if (set_field("exercise_submissions", "title", $title, "id", $sid)) {
223 print_heading(get_string("amendtitle", "exercise")." ".get_string("ok"));
225 redirect("submissions.php?id=$cm->id&amp;action=adminlist");
229 /*************** display final grades (by teacher) ***************************/
230 elseif ($action == 'displayfinalgrades') {
231 $groupid = get_current_group($course->id);
232 // Get all the students
233 if (!$users = get_course_students($course->id, "u.lastname, u.firstname")) {
234 print_heading(get_string("nostudentsyet"));
235 print_footer($course);
236 exit;
239 // show the final grades as stored in the tables...
240 print_heading_with_help(get_string("displayoffinalgrades", "exercise"), "finalgrades", "exercise");
241 echo "<center><table border=\"1\" width=\"90%\"><tr>\n";
242 echo "<td><b>".$course->student."</b></td>";
243 echo "<td><b>".get_string("submission", "exercise")."</b></td>";
244 echo "<td align=\"center\"><b>".get_string("gradeforassessment", "exercise")."</b></td>";
245 echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "exercise")."</b></td>";
246 echo "<td align=\"center\"><b>".get_string("overallgrade", "exercise")."</b></td></tr>\n";
247 // now the weights
248 echo "<tr><td><b>".get_string("maximumgrade")."</b></td>";
249 echo "<td><b>&nbsp;</b></td>\n";
250 echo "<td align=\"center\"><b>$exercise->gradinggrade</b></td>\n";
251 echo "<td align=\"center\"><b>$exercise->grade</b></td>\n";
252 echo "<td><b>&nbsp;</b></td></tr>\n";
253 foreach ($users as $user) {
254 // check group membership, if necessary
255 if ($groupid) {
256 // check user's group
257 if (!ismember($groupid, $user->id)) {
258 continue; // skip this user
261 // first get user's own assessment reord, it should contain their grading grade
262 if ($ownassessments = exercise_get_user_assessments($exercise, $user)) {
263 foreach ($ownassessments as $ownassessment) {
264 break; // there should only be one
267 else {
268 $ownassessment->gradinggrade = 0;
270 if ($submissions = exercise_get_user_submissions($exercise, $user)) {
271 foreach ($submissions as $submission) {
272 if ($assessments = exercise_get_assessments($submission)) {
273 foreach ($assessments as $assessment) { // (normally there should only be one
274 $gradinggrade = number_format($ownassessment->gradinggrade * $exercise->gradinggrade /
275 100.0, 1);
276 $grade = number_format($assessment->grade * $exercise->grade / 100.0, 1);
277 $overallgrade = number_format(($assessment->grade * $exercise->grade / 100.0) +
278 ($ownassessment->gradinggrade * $exercise->gradinggrade / 100.0), 1);
279 if ($submission->late) {
280 $grade = "<font color=\"red\">(".$grade.")</font>";
281 $overallgrade = "<font color=\"red\">(".$overallgrade.")</font>";
283 echo "<tr><td>".fullname($user)."</td>\n";
284 echo "<td>".exercise_print_submission_title($exercise, $submission)."</td>\n";
285 echo "<td align=\"center\">$gradinggrade</td>";
286 echo "<td align=\"center\">$grade</td>";
287 echo "<td align=\"center\">$overallgrade</td></tr>\n";
293 echo "</table><br clear=\"all\" />\n";
294 if ($exercise->showleaguetable) {
295 exercise_print_league_table($exercise);
296 echo "<br />\n";
298 echo get_string("maximumgrade").": $exercise->grade\n";
299 print_continue("view.php?id=$cm->id");
303 /******************* list for assessment student (submissions) ************************************/
304 elseif ($action == 'listforassessmentstudent' ) {
305 if (!$users = get_course_students($course->id)) {
306 print_heading(get_string("nostudentsyet"));
307 print_footer($course);
308 exit;
310 if (!isteacher($course->id)) {
311 error("Only teachers can look at this page");
313 exercise_list_unassessed_student_submissions($exercise, $USER);
314 print_continue("view.php?id=$cm->id");
319 /******************* list for assessment teacher (submissions) ************************************/
320 elseif ($action == 'listforassessmentteacher' ) {
321 if (!$users = get_course_students($course->id)) {
322 print_heading(get_string("nostudentsyet"));
323 print_footer($course);
324 exit;
326 exercise_list_unassessed_teacher_submissions($exercise, $USER);
327 print_continue("view.php?id=$cm->id");
332 /****************** save league table entries and anonimity setting (by teacher) **************/
333 elseif ($action == 'saveleaguetable') {
335 $form = (object)$_POST;
337 if (!isteacher($course->id)) {
338 error("Only teachers can look at this page");
341 // save the number of league table entries from the form...
342 if ($form->nentries == 'All') {
343 $nentries = 99;
344 } else {
345 $nentries = $form->nentries;
347 // ...and save it
348 set_field("exercise", "showleaguetable", $nentries, "id", "$exercise->id");
350 // ...and save the anonimity setting
351 set_field("exercise", "anonymous", $form->anonymous, "id", "$exercise->id");
353 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("entriessaved", "exercise"));
356 /*************** save weights (by teacher) ***************************/
357 elseif ($action == 'saveweights') {
359 $form = (object)$_POST;
361 if (!isteacher($course->id)) {
362 error("Only teachers can look at this page");
365 // save the weights from the form...
366 if (isset($form->teacherweight)) {
367 $teacherweight = $form->teacherweight;
368 // ...and save them
369 set_field("exercise", "teacherweight", $teacherweight, "id", "$exercise->id");
372 if (isset($form->gradingweight)) {
373 $gradingweight = $form->gradingweight;
374 // ...and save them
375 set_field("exercise", "gradingweight", $gradingweight, "id", "$exercise->id");
378 redirect("submissions.php?id=$cm->id&amp;action=adminlist", get_string("weightssaved", "exercise"));
382 /******************* user confirm delete ************************************/
383 elseif ($action == 'userconfirmdelete' ) {
385 if (empty($sid)) {
386 error("User Confirm Delete: submission id missing");
389 notice_yesno(get_string("confirmdeletionofthisitem","exercise", get_string("submission", "exercise")),
390 "submissions.php?action=userdelete&amp;id=$cm->id&amp;sid=$sid", "view.php?id=$cm->id");
394 /******************* user delete ************************************/
395 elseif ($action == 'userdelete' ) {
397 if (empty($sid)) {
398 error("User Delete: submission id missing");
401 if (!$submission = get_record("exercise_submissions", "id", $sid)) {
402 error("User Delete: can not get submission record");
404 print_string("deleting", "exercise");
405 // first get any assessments...
406 if ($assessments = exercise_get_assessments($submission)) {
407 foreach($assessments as $assessment) {
408 // ...and all the associated records...
409 delete_records("exercise_grades", "assessmentid", $assessment->id);
410 echo ".";
412 // ...now delete the assessments...
413 delete_records("exercise_assessments", "submissionid", $submission->id);
415 // ...and the submission record...
416 delete_records("exercise_submissions", "id", $submission->id);
417 // ..and finally the submitted file
418 exercise_delete_submitted_files($exercise, $submission);
419 add_to_log($course->id, "exercise", "delete", "view.php?id=$cm->id", "submission $submission->id");
421 print_continue("view.php?id=$cm->id");
425 /*************** no man's land **************************************/
427 else {
429 error("Fatal Error: Unknown Action: ".$action."\n");
434 print_footer($course);