More changes to the user interface. See http://moodle.org/mod/forum/view.php?id=740.
[moodle.git] / mod / workshop / submissions.php
1 <?php  // $Id: lib.php,v 1.1 22 Aug 2003
3 /*************************************************
4     ACTIONS handled are:
6     adminamendtitle
7     confirmdelete
8     delete
9     adminlist
10     editsubmission
11     listallsubmissions
12     listforassessmentstudent
13     listforassessmentteacher
14     showsubmission
15     updatesubmission
16     
18 ************************************************/
20     require("../../config.php");
21     require("lib.php");
22     require("locallib.php");
24     require_variable($id);    // Course Module ID
25     
26     $timenow = time();
28     // get some useful stuff...
29     if (! $cm = get_record("course_modules", "id", $id)) {
30         error("Course Module ID was incorrect");
31     }
32     if (! $course = get_record("course", "id", $cm->course)) {
33         error("Course is misconfigured");
34     }
35     if (! $workshop = get_record("workshop", "id", $cm->instance)) {
36         error("Course module is incorrect");
37     }
39     require_login($course->id);
41     $strworkshops = get_string("modulenameplural", "workshop");
42     $strworkshop  = get_string("modulename", "workshop");
43     $strsubmissions = get_string("submissions", "workshop");
45     // ... print the header and...
46     print_header_simple("$workshop->name", "",
47                  "<a href=\"index.php?id=$course->id\">$strworkshops</a> -> 
48                   <a href=\"view.php?id=$cm->id\">$workshop->name</a> -> $strsubmissions", 
49                   "", "", true);
51     //...get the action or set up an suitable default
52     optional_variable($action);
53     if (empty($action)) {
54         $action = "listallsubmissions";
55     }
58 /******************* admin amend title ************************************/
59     elseif ($action == 'adminamendtitle' ) {
61         if (!isteacher($course->id)) {
62             error("Only teachers can look at this page");
63         }
64         if (empty($_GET['sid'])) {
65             error("Admin Amend Title: submission id missing");
66         }
67         
68         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
69         print_heading(get_string("amendtitle", "workshop"));
70         ?>
71         <form name="amendtitleform" action="submissions.php" method="post">
72         <input type="hidden" name="action" value="adminupdatetitle" />
73         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
74         <input type="hidden" name="sid" value="<?php echo $_REQUEST['sid'] ?>" />
75         <center>
76         <table celpadding="5" border="1">
77         <?php
79         // now get the comment
80         echo "<tr valign=\"top\">\n";
81         echo "  <td align=\"right\"><p><b>". get_string("title", "workshop").":</b></p></td>\n";
82         echo "  <td>\n";
83         echo "      <input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
84         echo "  </td></tr></table>\n";
85         echo "<input type=\"submit\" value=\"".get_string("amendtitle", "workshop")."\" />\n";
86         echo "</center></form>\n";
87         
88         print_heading("<a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id#sid=$submission->id\">".get_string("cancel")."</a>");
89     }
90     
92     /******************* admin clear late (flag) ************************************/
93     elseif ($action == 'adminclearlate' ) {
95         if (!isteacher($course->id)) {
96             error("Only teachers can look at this page");
97         }
98         if (empty($_GET['sid'])) {
99             error("Admin clear late flag: submission id missing");
100         }
101     
102         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
103             error("Admin clear late flag: can not get submission record");
104         }
105         if (set_field("workshop_submissions", "late", 0, "id", $_GET['sid'])) {
106             print_heading(get_string("clearlateflag", "workshop")." ".get_string("ok"));
107         }
108         
109         add_to_log($course->id, "workshop", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");
110         
111         redirect("view.php?id=$cm->id");
112     }
113     
115     /******************* confirm delete ************************************/
116     elseif ($action == 'confirmdelete' ) {
118         if (empty($_GET['sid'])) {
119             error("Confirm delete: submission id missing");
120             }
121         $sid = $_GET['sid'];
122         notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("submission", "workshop")), 
123              "submissions.php?action=delete&amp;id=$cm->id&amp;sid=$sid", "view.php?id=$cm->id#sid=$sid");
124         }
125     
127     /******************* delete ************************************/
128     elseif ($action == 'delete' ) {
130         if (empty($_GET['sid'])) {
131             error("Delete: submission id missing");
132         }
133     
134         if (!$submission = get_record("workshop_submissions", "id", $_GET['sid'])) {
135             error("Admin delete: can not get submission record");
136         }
137         
138         // students are only allowed to delete their own submission and only up to the deadline
139         if (!(isteacher($course->id) or 
140                (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
141                    and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
142             error("You are not authorized to delete this submission");
143         }
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);
162         redirect("view.php?id=$cm->id");
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              "view.php?id=$cm->id");
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("view.php?id=$cm->id");
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     /******************* edit submission ************************************/
238     elseif ($action == 'editsubmission' ) {
240         if (empty($_GET['sid'])) {
241             error("Edit submission: submission id missing");
242         }
243         $usehtmleditor = can_use_html_editor();
244         
245         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
246         print_heading(get_string("editsubmission", "workshop"));
247         if ($submission->userid <> $USER->id) {
248             error("Edit submission: Userids do not match");
249         }
250         if (($submission->timecreated < ($timenow - $CFG->maxeditingtime)) and ($workshop->assessmentstart < $timenow)) {
251             error(get_string('notallowed', 'workshop'));
252         }
253         ?>
254         <form name="editform" enctype="multipart/form-data" action="submissions.php" method="post">
255         <input type="hidden" name="action" value="updatesubmission" />
256         <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
257         <input type="hidden" name="sid" value="<?php echo $_GET['sid'] ?>" />
258         <center>
259         <table celpadding="5" border="1">
260         <?php
261         echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
262         echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
263         echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
264         print_textarea($usehtmleditor, 25,70, 630, 400, "description", $submission->description);
265         use_html_editor("description");
266         echo "</td></tr>\n";
267         if ($workshop->nattachments) {
268             $filearea = workshop_file_area_name($workshop, $submission);
269             if ($basedir = workshop_file_area($workshop, $submission)) {
270                 if ($files = get_directory_list($basedir)) {
271                     echo "<tr><td><b>".get_string("attachments", "workshop").
272                         "</b><div align=\"right\"><input type=\"button\" value=\"".get_string("removeallattachments",
273                         "workshop")."\" onclick=\"document.editform.action.value='removeattachments';
274                         document.editform.submit();\"/></div></td></tr>\n";
275                     $n = 1;
276                     foreach ($files as $file) {
277                         $icon = mimeinfo("icon", $file);
278                         if ($CFG->slasharguments) {
279                             $ffurl = "file.php/$filearea/$file";
280                         } else {
281                             $ffurl = "file.php?file=/$filearea/$file";
282                         }
283                         echo "<tr><td>".get_string("attachment", "workshop")." $n: <img src=\"$CFG->pixpath/f/$icon\" 
284                             height=\"16\" width=\"16\" border=\"0\" alt=\"File\" />".
285                             "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a></td></tr>\n";
286                     }
287                 } else {
288                     echo "<tr><td><b>".get_string("noattachments", "workshop")."</b></td></tr>\n";
289                 }
290             }
291             echo "<tr><td>\n";
292             require_once($CFG->dirroot.'/lib/uploadlib.php');
293             for ($i=0; $i < $workshop->nattachments; $i++) {
294                 $iplus1 = $i + 1;
295                 $tag[$i] = get_string("newattachment", "workshop")." $iplus1:";
296             }
297             upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
298                 $workshop->maxbytes,false);
299             echo "</td></tr>\n";
300         }
302         echo "</table>\n";
303         echo "<input type=\"submit\" value=\"".get_string("savemysubmission", "workshop")."\" />\n";
304         echo "</center></form>\n";
305     }
306     
308     /******************* list all submissions ************************************/
309     elseif ($action == 'listallsubmissions' ) {
310         if (!$users = get_course_students($course->id)) {
311             print_heading(get_string("nostudentsyet"));
312             print_footer($course);
313             exit;
314         }
315         print_heading(get_string("listofallsubmissions", "workshop").":", "CENTER");
316         workshop_list_all_submissions($workshop, $USER);
317         print_continue("view.php?id=$cm->id");
318         
319     }
320     
322     /******************* list for assessment student (submissions) ************************************/
323     elseif ($action == 'listforassessmentstudent' ) {
324         if (!$users = get_course_students($course->id)) {
325             print_heading(get_string("nostudentsyet"));
326             print_footer($course);
327             exit;
328         }
329         workshop_list_unassessed_student_submissions($workshop, $USER);
330         print_continue("view.php?id=$cm->id");
331         
332     }
333     
335     /******************* list for assessment teacher (submissions) ************************************/
336     elseif ($action == 'listforassessmentteacher' ) {
337    
338         if (!isteacher($course->id)) {
339             error("Only teachers can look at this page");
340         }
342         workshop_list_unassessed_teacher_submissions($workshop, $USER);
343         print_continue("view.php?id=$cm->id");
344         
345     }
346     
348     /******************* remove (all) attachments ************************************/
349     elseif ($action == 'removeattachments' ) {
350     
351         $form = data_submitted();
352         
353         if (empty($form->sid)) {
354             error("Update submission: submission id missing");
355         }
356         
357         $submission = get_record("workshop_submissions", "id", $form->sid);
358         
359         // students are only allowed to remove their own attachments and only up to the deadline
360         if (!(isteacher($course->id) or 
361                (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
362                    and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
363             error("You are not authorized to delete these attachments");
364         }
365         
366         // amend title... just in case they were modified
367         // check existence of title
368         if (empty($form->title)) {
369             notify(get_string("notitlegiven", "workshop"));
370         } else {
371             set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
372             set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
373         } 
374         print_string("removeallattachments", "workshop");
375         workshop_delete_submitted_files($workshop, $submission);
376         add_to_log($course->id, "workshop", "removeattachments", "view.php?id=$cm->id", "submission $submission->id");
377         
378         print_continue("view.php?id=$cm->id#sid=$submission->id");
379     }
380     
382     /******************* show submission ************************************/
383     elseif ($action == 'showsubmission' ) {
385         if (empty($_GET['sid'])) {
386             error("Show submission: submission id missing");
387         }
388         
389         $submission = get_record("workshop_submissions", "id", $_GET['sid']);
390         $title = '"'.$submission->title.'" ';
391         if (isteacher($course->id)) {
392             $title .= get_string('by', 'workshop').' '.workshop_fullname($submission->userid, $course->id);
393         }
394         print_heading($title);
395         echo '<center>'.get_string('submitted', 'workshop').': '.userdate($submission->timecreated).'</center><br />';
396         workshop_print_submission($workshop, $submission);
397         print_continue($_SERVER['HTTP_REFERER'].'#sid='.$submission->id);
398     }
399     
401     /*************** update (league table options teacher) ***************************/
402     elseif ($action == 'updateleaguetable') {
403         
404         if (!isteacher($course->id)) {
405             error("Only teachers can look at this page");
406         }
408         $form = (object)$_POST;
409         
410         // save number of entries in showleaguetable option
411         if ($form->nentries == 'All') {
412             $form->nentries = 99;
413         }
414         set_field("workshop", "showleaguetable", $form->nentries, "id", "$workshop->id");
415         
416         // save the anonymous option
417         set_field("workshop", "anonymous", $form->anonymous, "id", "$workshop->id");
418         add_to_log($course->id, "workshop", "league table", "view.php?id=$cm->id", $form->nentries, $cm->id);
420         redirect("view.php?id=$cm->id");
421     }
424     /*************** update submission ***************************/
425     elseif ($action == 'updatesubmission') {
427         $form = data_submitted();
428         
429         if (empty($form->sid)) {
430             error("Update submission: submission id missing");
431         }
432         $submission = get_record("workshop_submissions", "id", $form->sid);
433         
434         // students are only allowed to update their own submission and only up to the deadline
435         if (!(isteacher($course->id) or 
436                (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
437                    and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
438             error("You are not authorized to update your submission");
439         }
440         
441         // check existence of title
442         if (empty($form->title)) {
443             $title = get_string("notitle", "workshop");
444         } 
445         set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
446         set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
447         set_field("workshop_submissions", "timecreated", $timenow, "id", $submission->id);
448         if ($workshop->nattachments) {
449             require_once($CFG->dirroot.'/lib/uploadlib.php');
450             $um = new upload_manager(null,false,false,$course,false,$workshop->maxbytes);
451             if ($um->preprocess_files()) {
452                 $dir = workshop_file_area_name($workshop, $submission);
453                 if ($um->save_files($dir)) {
454                     add_to_log($course->id, "workshop", "newattachment", "view.php?id=$cm->id", "$workshop->id");
455                     print_heading(get_string("uploadsuccess", "workshop"));
456                 }
457                 // upload manager will print errors.
458             } 
459             print_continue("view.php?id=$cm->id");
460         } else {
461             redirect("view.php?id=$cm->id#sid=$submission->id");
462         }
463     }
466     /*************** no man's land **************************************/
468     else {
470         error("Fatal Error: Unknown Action: ".$action."\n");
472     }
475     print_footer($course);
476  
477 ?>