001bfe3b0853276ba6f939ab9cc8c127acb44994
[moodle.git] / mod / workshop / submissions.php
1 <?php  // $Id: lib.php,v 1.1 22 Aug 2003
3 /*************************************************
4     ACTIONS handled are:
6     dminamendtitle
7     adminconfirmdelete
8     admindelete
9     adminlist
10     displayfinalgrades (teachers only)
11     editsubmission
12     listallsubmissions
13     listforassessmentstudent
14     listforassessmentteacher
15     showsubmission
16     updatesubmission
17     userconfirmdelete
18     userdelete
19     
21 ************************************************/
23     require("../../config.php");
24     require("lib.php");
25     require("locallib.php");
27     require_variable($id);    // Course Module ID
28     
29     $timenow = time();
31     // get some useful stuff...
32     if (! $cm = get_record("course_modules", "id", $id)) {
33         error("Course Module ID was incorrect");
34     }
35     if (! $course = get_record("course", "id", $cm->course)) {
36         error("Course is misconfigured");
37     }
38     if (! $workshop = get_record("workshop", "id", $cm->instance)) {
39         error("Course module is incorrect");
40     }
42     require_login($course->id);
44     $strworkshops = get_string("modulenameplural", "workshop");
45     $strworkshop  = get_string("modulename", "workshop");
46     $strsubmissions = get_string("submissions", "workshop");
48     // ... print the header and...
49     print_header_simple("$workshop->name", "",
50                  "<a href=\"index.php?id=$course->id\">$strworkshops</a> -> 
51                   <a href=\"view.php?id=$cm->id\">$workshop->name</a> -> $strsubmissions", 
52                   "", "", true);
54     //...get the action or set up an suitable default
55     optional_variable($action);
56     if (empty($action)) {
57         $action = "listallsubmissions";
58         }
61 /******************* admin amend title ************************************/
62     elseif ($action == 'adminamendtitle' ) {
64         if (!isteacher($course->id)) {
65             error("Only teachers can look at this page");
66             }
67         if (empty($_GET['sid'])) {
68             error("Admin Amend Title: submission id missing");
69             }
70         
71         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
72         print_heading(get_string("amendtitle", "workshop"));
73         ?>
74         <form name="amendtitleform" action="submissions.php" method="post">
75         <input type="hidden" name="action" value="adminupdatetitle" />
76         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
77         <input type="hidden" name="sid" value="<?php echo $_REQUEST['sid'] ?>" />
78         <center>
79         <table celpadding="5" border="1">
80         <?php
82         // now get the comment
83         echo "<tr valign=\"top\">\n";
84         echo "  <td align=\"right\"><p><b>". get_string("title", "workshop").":</b></p></td>\n";
85         echo "  <td>\n";
86         echo "      <input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
87         echo "  </td></tr></table>\n";
88         echo "<input type=\"submit\" value=\"".get_string("amendtitle", "workshop")."\" />\n";
89         echo "</center></form>\n";
91         }
92     
94     /******************* admin clear late (flag) ************************************/
95     elseif ($action == 'adminclearlate' ) {
97         if (!isteacher($course->id)) {
98             error("Only teachers can look at this page");
99         }
100         if (empty($_GET['sid'])) {
101             error("Admin clear late flag: submission id missing");
102         }
103     
104         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
105             error("Admin clear late flag: can not get submission record");
106         }
107         if (set_field("workshop_submissions", "late", 0, "id", $_GET['sid'])) {
108             print_heading(get_string("clearlateflag", "workshop")." ".get_string("ok"));
109         }
110         
111         add_to_log($course->id, "workshop", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");
112         
113         redirect("submissions.php?id=$cm->id&amp;action=adminlist");
114     }
115     
117     /******************* admin confirm delete ************************************/
118     elseif ($action == 'adminconfirmdelete' ) {
120         if (!isteacher($course->id)) {
121             error("Only teachers can look at this page");
122             }
123         if (empty($_GET['sid'])) {
124             error("Admin confirm delete: submission id missing");
125             }
126             
127         notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("submission", "workshop")), 
128              "submissions.php?action=admindelete&amp;id=$cm->id&amp;sid=$_GET[sid]", "submissions.php?id=$cm->id&amp;action=adminlist");
129         }
130     
132     /******************* admin delete ************************************/
133     elseif ($action == 'admindelete' ) {
135         if (!isteacher($course->id)) {
136             error("Only teachers can look at this page");
137             }
138         if (empty($_GET['sid'])) {
139             error("Admin delete: submission id missing");
140             }
141     
142         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
143             error("Admin delete: can not get submission record");
144             }
145         print_string("deleting", "workshop");
146         // first get any assessments...
147         if ($assessments = workshop_get_assessments($submission, 'ALL')) {
148             foreach($assessments as $assessment) {
149                 // ...and all the associated records...
150                 delete_records("workshop_comments", "assessmentid", $assessment->id);
151                 delete_records("workshop_grades", "assessmentid", $assessment->id);
152                 echo ".";
153                 }
154             // ...now delete the assessments...
155             delete_records("workshop_assessments", "submissionid", $submission->id);
156             }
157         // ...and the submission record...
158         delete_records("workshop_submissions", "id", $submission->id);
159         // ..and finally the submitted file
160         workshop_delete_submitted_files($workshop, $submission);
161         
162         print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
163         }
164     
166     /******************* admin (confirm) late flag ************************************/
167     elseif ($action == 'adminlateflag' ) {
169         if (!isteacher($course->id)) {
170             error("Only teachers can look at this page");
171             }
172         if (empty($_GET['sid'])) {
173             error("Admin confirm late flag: submission id missing");
174             }
175         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
176             error("Admin confirm late flag: can not get submission record");
177             }
179         notice_yesno(get_string("clearlateflag","workshop")."?", 
180              "submissions.php?action=adminclearlate&amp;id=$cm->id&amp;sid=$_GET[sid]", 
181              "submissions.php?id=$cm->id&amp;action=adminlist");
182         }
183     
185     /******************* list all submissions ************************************/
186     elseif ($action == 'adminlist' ) {
188         if (!isteacher($course->id)) {
189             error("Only teachers can look at this page");
190             }
191         if (empty($_GET['order'])) {
192             $order = "name";
193             }
194         else {
195             $order = $_GET['order'];
196             }
197             
198         workshop_list_submissions_for_admin($workshop, $order);
199         print_continue("view.php?id=$cm->id");
200         
201         }
202     
204     /******************* admin update title ************************************/
205     elseif ($action == 'adminupdatetitle' ) {
207         if (!isteacher($course->id)) {
208             error("Only teachers can look at this page");
209             }
210         if (empty($_POST['sid'])) {
211             error("Admin Update Title: submission id missing");
212             }
213     
214         if (set_field("workshop_submissions", "title", $_POST['title'], "id", $_POST['sid'])) {
215             print_heading(get_string("amendtitle", "workshop")." ".get_string("ok"));
216             }
217         print_continue("submissions.php?id=$cm->id&amp;action=adminlist");
218         }
219     
221     /******************* confirm remove attachments ************************************/
222     elseif ($action == 'confirmremoveattachments' ) {
224         if (empty($_GET['sid'])) {
225             error("Admin confirm delete: submission id missing");
226             }
227         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
228             error("Admin delete: can not get submission record");
229             }
231         notice_yesno(get_string("confirmremoveattachments","workshop"), 
232              "submissions.php?action=removeattachments&amp;id=$cm->id&amp;sid=$_GET[sid]", 
233              "view.php?id=$cm->id");
234         }
235     
237     /*************** display grades (by teacher) ***************************/
238     elseif ($action == 'displaygrades' or $action == 'displayfinalgrades' or $action == 'displaycurrentgrades') {
240         if (groupmode($course, $cm) == SEPARATEGROUPS) {
241             $groupid = get_current_group($course->id);
242         } else {
243             $groupid = 0;
244         }
245         
246         print_heading_with_help(get_string("displayofgrades", "workshop"), "finalgrades", "workshop");
248         if ($workshop->ntassessments) {
249             // display the teacher's submissions
250             echo "<center><table border=\"1\" width=\"90%\"><tr>\n";
251             echo "<td bgcolor=\"$THEME->cellheading2\"><b>$course->teacher ".get_string("submission", "workshop").
252                 "</b></td>";
253             echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("assessmentsby", "workshop",
254                     $course->teachers)."</b></td>";
255             echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("assessmentsby", "workshop",
256                    $course->students)."</b></td></tr>";
257             if ($submissions = workshop_get_teacher_submissions($workshop)) {
258                 foreach ($submissions as $submission) {
259                     echo "<tr><td>".workshop_print_submission_title($workshop, $submission)."</td>\n";
260                     echo "<td align=\"center\">".workshop_print_submission_assessments($workshop, $submission,
261                                 "teacher")."</td>";
262                     echo "<td align=\"center\">".workshop_print_submission_assessments($workshop, $submission,
263                                 "student")."</td></tr>";
264                 }
265             }
266             echo "</table><br clear=\"all\" />\n";
267             workshop_print_key($workshop);
268         }
270         // Get all the students
271         if (!$users = get_course_students($course->id, "u.lastname, u.firstname")) {
272             print_heading(get_string("nostudentsyet"));
273             print_footer($course);
274             exit;
275         }
276         
277         // show the grades as stored in the tables...     
278         echo "<center><table border=\"1\" width=\"90%\"><tr>
279             <td bgcolor=\"$THEME->cellheading2\" width=\"120\"><b>".$course->student."</b></td>";
280         if ($workshop->wtype) {
281             echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("assessmentsdone", "workshop").
282                 "</b></td>";
283             echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("gradeforassessments", 
284                 "workshop")."</b></td>";
285         }
286         echo "<td bgcolor=\"$THEME->cellheading2\"><b>".get_string("submission", "workshop")."</b></td>";
287         echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("assessmentsby", "workshop", 
288                 $course->teachers)."</b></td>";
289         if ($workshop->wtype) {
290             echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("assessmentsby", "workshop", 
291                 $course->students)."</b></td>";
292         }
293         echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("gradeforsubmission", 
294                 "workshop")."</b></td>";
295         echo "<td bgcolor=\"$THEME->cellheading2\" align=\"center\"><b>".get_string("overallgrade", "workshop").
296                 "</b></td></tr>\n";
298         foreach ($users as $user) {
299             // skip if student not in group
300             if ($groupid) {
301                 if (!ismember($groupid, $user->id)) {
302                     continue;
303                 }
304             }
305             if ($submissions = workshop_get_user_submissions($workshop, $user)) {
306                 $gradinggrade = workshop_gradinggrade($workshop, $user);
307                 foreach ($submissions as $submission) {
308                     $grade = workshop_submission_grade($workshop, $submission);
309                     echo "<tr><td><a href=\"{$CFG->wwwroot}/user/view.php?id=$user->id&amp;course=$course->id\">".
310                         fullname($user).'</a></td>';
311                     if ($workshop->wtype) {
312                         echo "<td align=\"center\">".workshop_print_user_assessments($workshop, $user)."</td>";
313                         echo "<td align=\"center\">$gradinggrade</td>";
314                     }
315                     echo "<td>".workshop_print_submission_title($workshop, $submission)."</td>\n";
316                     $strteachergrades = workshop_print_submission_assessments($workshop, $submission, "teacher");
317                     if ($strteachergrades == '&nbsp;') {
318                         $strteachergrades = '<a href="assessments.php?action=assesssubmission&id='.
319                             $cm->id.'&sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
320                     }
321                     echo "<td align=\"center\">$strteachergrades</td>";
322                     if ($workshop->wtype) {
323                         echo "<td align=\"center\">".workshop_print_submission_assessments($workshop, $submission, 
324                             "student")."</td>";
325                     }
326                     echo "<td align=\"center\">$grade</td>";
327                     echo "<td align=\"center\">".number_format($gradinggrade + $grade, 1)."</td></tr>\n";
328                 }
329             }
330         }
331         echo "</table><br clear=\"all\" />\n";
332         workshop_print_key($workshop);
333         if ($workshop->showleaguetable and time() > $workshop->assessmentend) {
334             workshop_print_league_table($workshop);
335             if ($workshop->anonymous) {
336                 echo "<p>".get_string("namesnotshowntostudents", "workshop", $course->students)."</p>\n";
337             }
338         }
339         print_continue("view.php?id=$cm->id");
340     }
343     /******************* edit submission ************************************/
344     elseif ($action == 'editsubmission' ) {
346         if (empty($_GET['sid'])) {
347             error("Edit submission: submission id missing");
348         }
349         $usehtmleditor = can_use_html_editor();
350         
351         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
352         print_heading(get_string("editsubmission", "workshop"));
353         if ($submission->userid <> $USER->id) {
354             error("Edit submission: Userids do not match");
355         }
356         if (($submission->timecreated < ($timenow - $CFG->maxeditingtime)) and ($workshop->assessmentstart < $timenow)) {
357             error(get_string('notallowed', 'workshop'));
358         }
359         ?>
360         <form name="editform" enctype="multipart/form-data" action="submissions.php" method="post">
361         <input type="hidden" name="action" value="updatesubmission" />
362         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
363         <input type="hidden" name="sid" value="<?php echo $_GET['sid'] ?>" />
364         <center>
365         <table celpadding="5" border="1">
366         <?php
367         echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
368         echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
369         echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
370         print_textarea($usehtmleditor, 25,70, 630, 400, "description", $submission->description);
371         use_html_editor("description");
372         echo "</td></tr>\n";
373         if ($workshop->nattachments) {
374             $filearea = workshop_file_area_name($workshop, $submission);
375             if ($basedir = workshop_file_area($workshop, $submission)) {
376                 if ($files = get_directory_list($basedir)) {
377                     echo "<tr><td><b>".get_string("attachments", "workshop").
378                         "</b><div align=\"right\"><input type=\"button\" value=\"".get_string("removeallattachments",
379                         "workshop")."\" onclick=\"document.editform.action.value='removeattachments';
380                         document.editform.submit();\"/></div></td></tr>\n";
381                     $n = 1;
382                     foreach ($files as $file) {
383                         $icon = mimeinfo("icon", $file);
384                         if ($CFG->slasharguments) {
385                             $ffurl = "file.php/$filearea/$file";
386                         } else {
387                             $ffurl = "file.php?file=/$filearea/$file";
388                         }
389                         echo "<tr><td>".get_string("attachment", "workshop")." $n: <img src=\"$CFG->pixpath/f/$icon\" 
390                             height=\"16\" width=\"16\" border=\"0\" alt=\"File\" />".
391                             "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a></td></tr>\n";
392                     }
393                 } else {
394                     echo "<tr><td><b>".get_string("noattachments", "workshop")."</b></td></tr>\n";
395                 }
396             }
397             echo "<tr><td>\n";
398             require_once($CFG->dirroot.'/lib/uploadlib.php');
399             for ($i=0; $i < $workshop->nattachments; $i++) {
400                 $iplus1 = $i + 1;
401                 $tag[$i] = get_string("newattachment", "workshop")." $iplus1:";
402             }
403             upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
404                 $workshop->maxbytes,false);
405             echo "</td></tr>\n";
406         }
408         echo "</table>\n";
409         echo "<input type=\"submit\" value=\"".get_string("savemysubmission", "workshop")."\" />\n";
410         echo "</center></form>\n";
411         }
412     
414     /******************* list all submissions ************************************/
415     elseif ($action == 'listallsubmissions' ) {
416         if (!$users = get_course_students($course->id)) {
417             print_heading(get_string("nostudentsyet"));
418             print_footer($course);
419             exit;
420             }
421         print_heading(get_string("listofallsubmissions", "workshop").":", "CENTER");
422         workshop_list_all_submissions($workshop, $USER);
423         print_continue("view.php?id=$cm->id");
424         
425         }
426     
428     /******************* list for assessment student (submissions) ************************************/
429     elseif ($action == 'listforassessmentstudent' ) {
430         if (!$users = get_course_students($course->id)) {
431             print_heading(get_string("nostudentsyet"));
432             print_footer($course);
433             exit;
434             }
435         workshop_list_unassessed_student_submissions($workshop, $USER);
436         print_continue("view.php?id=$cm->id");
437         
438         }
439     
441     /******************* list for assessment teacher (submissions) ************************************/
442     elseif ($action == 'listforassessmentteacher' ) {
443    
444         if (!isteacher($course->id)) {
445             error("Only teachers can look at this page");
446             }
448         workshop_list_unassessed_teacher_submissions($workshop, $USER);
449         print_continue("view.php?id=$cm->id");
450         
451         }
452     
454     /******************* remove (all) attachments ************************************/
455     elseif ($action == 'removeattachments' ) {
457         $form = data_submitted();
458         
459         if (empty($form->sid)) {
460             error("Update submission: submission id missing");
461             }
462         $submission = get_record("workshop_submissions", "id", $form->sid);
463         // amend title... just in case they were modified
464         // check existence of title
465         if (empty($form->title)) {
466             notify(get_string("notitlegiven", "workshop"));
467         } else {
468             set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
469             set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
470         } 
471         print_string("removeallattachments", "workshop");
472         workshop_delete_submitted_files($workshop, $submission);
473         add_to_log($course->id, "workshop", "removeattachments", "view.php?id=$cm->id", "submission $submission->id");
474         
475         print_continue("view.php?id=$cm->id");
476         }
477     
479     /******************* show submission ************************************/
480     elseif ($action == 'showsubmission' ) {
482         if (empty($_GET['sid'])) {
483             error("Edit submission: submission id missing");
484             }
485         
486         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
487         workshop_print_submission($workshop, $submission);
488         print_continue("view.php?id=$cm->id");
489         }
490     
492     /*************** update (league table options teacher) ***************************/
493     elseif ($action == 'updateleaguetable') {
494         
495         if (!isteacher($course->id)) {
496             error("Only teachers can look at this page");
497         }
499         $form = (object)$_POST;
500         
501         // save number of entries in showleaguetable option
502         if ($form->nentries == 'All') {
503             $form->nentries = 99;
504             }
505         set_field("workshop", "showleaguetable", $form->nentries, "id", "$workshop->id");
506         
507         // save the anonymous option
508         set_field("workshop", "anonymous", $form->anonymous, "id", "$workshop->id");
509         add_to_log($course->id, "workshop", "league table", "view.php?id=$cm->id", $form->nentries, $cm->id);
511         redirect("submissions.php?action=adminlist&amp;id=$cm->id");
512         }
515     /*************** update submission ***************************/
516     elseif ($action == 'updatesubmission') {
518         $form = data_submitted();
519         
520         if (empty($form->sid)) {
521             error("Update submission: submission id missing");
522             }
523         $submission = get_record("workshop_submissions", "id", $form->sid);
524         // check existence of title
525         if (empty($form->title)) {
526             $title = get_string("notitle", "workshop");
527         } 
528         set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
529         set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
530         set_field("workshop_submissions", "timecreated", $timenow, "id", $submission->id);
531         if ($workshop->nattachments) {
532             require_once($CFG->dirroot.'/lib/uploadlib.php');
533             $um = new upload_manager(null,false,false,$course,false,$workshop->maxbytes);
534             if ($um->preprocess_files()) {
535                 $dir = workshop_file_area_name($workshop, $submission);
536                 if ($um->save_files($dir)) {
537                     add_to_log($course->id, "workshop", "newattachment", "view.php?id=$cm->id", "$workshop->id");
538                     print_heading(get_string("uploadsuccess", "workshop"));
539                 }
540                 // upload manager will print errors.
541             } 
542             print_continue("view.php?id=$cm->id");
543         } else {
544             redirect("view.php?id=$cm->id");
545         }
546     }
547                 
548      /******************* user confirm delete ************************************/
549     elseif ($action == 'userconfirmdelete' ) {
551         if (empty($_GET['sid'])) {
552             error("User Confirm Delete: submission id missing");
553             }
554             
555         notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("submission", "workshop")), 
556              "submissions.php?action=userdelete&amp;id=$cm->id&amp;sid=$_GET[sid]", "view.php?id=$cm->id");
557         }
558     
560     /******************* user delete ************************************/
561     elseif ($action == 'userdelete' ) {
563         if (empty($_GET['sid'])) {
564             error("User Delete: submission id missing");
565             }
566     
567         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
568             error("User Delete: can not get submission record");
569             }
570         print_string("deleting", "workshop");
571         // first get any assessments...
572         if ($assessments = workshop_get_assessments($submission, 'ALL')) {
573             foreach($assessments as $assessment) {
574                 // ...and all the associated records...
575                 delete_records("workshop_comments", "assessmentid", $assessment->id);
576                 delete_records("workshop_grades", "assessmentid", $assessment->id);
577                 echo ".";
578                 }
579             // ...now delete the assessments...
580             delete_records("workshop_assessments", "submissionid", $submission->id);
581             }
582         // ...and the submission record...
583         delete_records("workshop_submissions", "id", $submission->id);
584         // ..and finally the submitted file
585         workshop_delete_submitted_files($workshop, $submission);
586         
587         print_continue("view.php?id=$cm->id");
588         }
589     
591     /*************** no man's land **************************************/
593     else {
595         error("Fatal Error: Unknown Action: ".$action."\n");
597         }
600     print_footer($course);
601  
602 ?>