MDL-16221
[moodle-linuxchix.git] / mod / workshop / submissions.php
blobdc1f06d45e5140b4654f350bd85e57ff6615156d
1 <?php // $Id$
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
18 ************************************************/
20 require("../../config.php");
21 require("lib.php");
22 require("locallib.php");
24 $id = required_param('id', PARAM_INT); // Course Module ID
25 $action = optional_param('action', '', PARAM_ALPHA);
26 $sid = optional_param('sid', 0, PARAM_INT); //submission id
27 $order = optional_param('order', 'name', PARAM_ALPHA);
28 $title = optional_param('title', '', PARAM_CLEAN);
29 $nentries = optional_param('nentries', '', PARAM_ALPHANUM);
30 $anonymous = optional_param('anonymous', '', PARAM_CLEAN);
31 $description = optional_param('description', '', PARAM_CLEAN);
33 $timenow = time();
35 // get some useful stuff...
36 if (! $cm = get_coursemodule_from_id('workshop', $id)) {
37 error("Course Module ID was incorrect");
39 if (! $course = get_record("course", "id", $cm->course)) {
40 error("Course is misconfigured");
42 if (! $workshop = get_record("workshop", "id", $cm->instance)) {
43 error("Course module is incorrect");
46 require_login($course->id, false, $cm);
47 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
49 $strworkshops = get_string("modulenameplural", "workshop");
50 $strworkshop = get_string("modulename", "workshop");
51 $strsubmissions = get_string("submissions", "workshop");
53 // ... print the header and...
54 $navigation = build_navigation($strsubmissions, $cm);
55 print_header_simple(format_string($workshop->name), "", $navigation,
56 "", "", true);
58 //...get the action or set up an suitable default
59 if (empty($action)) {
60 $action = "listallsubmissions";
64 /******************* admin amend title ************************************/
65 elseif ($action == 'adminamendtitle' ) {
67 require_capability('mod/workshop:manage', $context);
68 if (empty($sid)) {
69 error("Admin Amend Title: submission id missing");
72 $submission = get_record("workshop_submissions", "id", $sid);
73 print_heading(get_string("amendtitle", "workshop"));
75 <form id="amendtitleform" action="submissions.php" method="post">
76 <fieldset class="invisiblefieldset">
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 <div class="boxaligncenter">
81 <table cellpadding="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", "workshop").":</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", "workshop")."\" />\n";
91 echo "</div></fieldset></form>\n";
93 print_heading("<a $CFG->frametarget href=\"view.php?id=$cm->id#sid=$submission->id\">".get_string("cancel")."</a>");
97 /******************* admin clear late (flag) ************************************/
98 elseif ($action == 'adminclearlate' ) {
100 require_capability('mod/workshop:manage', $context);
101 if (empty($sid)) {
102 error("Admin clear late flag: submission id missing");
105 if (!$submission = get_record("workshop_submissions", "id", $sid)) {
106 error("Admin clear late flag: can not get submission record");
108 if (set_field("workshop_submissions", "late", 0, "id", $sid)) {
109 print_heading(get_string("clearlateflag", "workshop")." ".get_string("ok"));
112 add_to_log($course->id, "workshop", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");
114 redirect("view.php?id=$cm->id");
118 /******************* confirm delete ************************************/
119 elseif ($action == 'confirmdelete' ) {
121 if (empty($sid)) {
122 error("Confirm delete: submission id missing");
124 notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("submission", "workshop")),
125 "submissions.php?action=delete&amp;id=$cm->id&amp;sid=$sid", "view.php?id=$cm->id#sid=$sid");
129 /******************* delete ************************************/
130 elseif ($action == 'delete' ) {
132 if (empty($sid)) {
133 error("Delete: submission id missing");
136 if (!$submission = get_record("workshop_submissions", "id", $sid)) {
137 error("Admin delete: can not get submission record");
140 // students are only allowed to delete their own submission and only up to the deadline
141 if (!(workshop_is_teacher($workshop) or
142 (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
143 and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
144 error("You are not authorized to delete this submission");
147 print_string("deleting", "workshop");
148 // first get any assessments...
149 if ($assessments = workshop_get_assessments($submission, 'ALL')) {
150 foreach($assessments as $assessment) {
151 // ...and all the associated records...
152 delete_records("workshop_comments", "assessmentid", $assessment->id);
153 delete_records("workshop_grades", "assessmentid", $assessment->id);
154 echo ".";
156 // ...now delete the assessments...
157 delete_records("workshop_assessments", "submissionid", $submission->id);
159 // ...and the submission record...
160 delete_records("workshop_submissions", "id", $submission->id);
161 // ..and finally the submitted file
162 workshop_delete_submitted_files($workshop, $submission);
164 redirect("view.php?id=$cm->id");
168 /******************* admin (confirm) late flag ************************************/
169 elseif ($action == 'adminlateflag' ) {
171 require_capability('mod/workshop:manage', $context);
172 if (empty($sid)) {
173 error("Admin confirm late flag: submission id missing");
175 if (!$submission = get_record("workshop_submissions", "id", $sid)) {
176 error("Admin confirm late flag: can not get submission record");
179 notice_yesno(get_string("clearlateflag","workshop")."?",
180 "submissions.php?action=adminclearlate&amp;id=$cm->id&amp;sid=$sid",
181 "view.php?id=$cm->id");
185 /******************* list all submissions ************************************/
186 elseif ($action == 'adminlist' ) {
188 require_capability('mod/workshop:manage', $context);
190 workshop_list_submissions_for_admin($workshop, $order);
191 print_continue("view.php?id=$cm->id");
196 /******************* admin update title ************************************/
197 elseif ($action == 'adminupdatetitle' ) {
199 require_capability('mod/workshop:manage', $context);
200 if (empty($sid)) {
201 error("Admin Update Title: submission id missing");
204 if (set_field("workshop_submissions", "title", $title, "id", $sid)) {
205 print_heading(get_string("amendtitle", "workshop")." ".get_string("ok"));
207 print_continue("view.php?id=$cm->id");
211 /******************* confirm remove attachments ************************************/
212 elseif ($action == 'confirmremoveattachments' ) {
214 if (empty($sid)) {
215 error("Admin confirm delete: submission id missing");
217 if (!$submission = get_record("workshop_submissions", "id", $sid)) {
218 error("Admin delete: can not get submission record");
221 notice_yesno(get_string("confirmremoveattachments","workshop"),
222 "submissions.php?action=removeattachments&amp;id=$cm->id&amp;sid=$sid",
223 "view.php?id=$cm->id");
227 /******************* edit submission ************************************/
228 elseif ($action == 'editsubmission' ) {
230 if (empty($sid)) {
231 error("Edit submission: submission id missing");
233 $usehtmleditor = can_use_html_editor();
235 $submission = get_record("workshop_submissions", "id", $sid);
236 print_heading(get_string("editsubmission", "workshop"));
237 if ($submission->userid <> $USER->id) {
238 error("Edit submission: Userids do not match");
240 if (($submission->timecreated < ($timenow - $CFG->maxeditingtime)) and ($workshop->assessmentstart < $timenow)) {
241 print_error('notallowed', 'workshop');
244 <form id="editform" enctype="multipart/form-data" action="submissions.php" method="post">
245 <fieldset class="invisiblefieldset">
246 <input type="hidden" name="action" value="updatesubmission" />
247 <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
248 <input type="hidden" name="sid" value="<?php echo $sid ?>" />
249 <div class="boxaligncenter">
250 <table cellpadding="5" border="1">
251 <?php
252 echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
253 echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
254 echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
255 print_textarea($usehtmleditor, 25,70, 630, 400, "description", $submission->description);
256 use_html_editor("description");
257 echo "</td></tr>\n";
258 if ($workshop->nattachments) {
259 $filearea = workshop_file_area_name($workshop, $submission);
260 if ($basedir = workshop_file_area($workshop, $submission)) {
261 if ($files = get_directory_list($basedir)) {
262 echo "<tr><td><b>".get_string("attachments", "workshop").
263 "</b><div style=\"text-align:right;\"><input type=\"button\" value=\"".get_string("removeallattachments",
264 "workshop")."\" onclick=\"getElementById('editform').action.value='removeattachments';
265 getElementById('editform').submit();\"/></div></td></tr>\n";
266 $n = 1;
267 require_once($CFG->libdir .'/filelib.php');
268 foreach ($files as $file) {
269 $icon = mimeinfo("icon", $file);
270 $ffurl = get_file_url("$filearea/$file");
271 // removed target=\"uploadedfile\"
272 // as it does not validate MDL_7861
273 echo "<tr><td>".get_string("attachment", "workshop")." $n: <img src=\"$CFG->pixpath/f/$icon\"
274 class=\"icon\" alt=\"".get_string('file')."\" />".
275 "&nbsp;<a href=\"$ffurl\">$file</a></td></tr>\n";
277 } else {
278 echo "<tr><td><b>".get_string("noattachments", "workshop")."</b></td></tr>\n";
281 echo "<tr><td>\n";
282 require_once($CFG->dirroot.'/lib/uploadlib.php');
283 for ($i=0; $i < $workshop->nattachments; $i++) {
284 $iplus1 = $i + 1;
285 $tag[$i] = get_string("newattachment", "workshop")." $iplus1:";
287 upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
288 $workshop->maxbytes,false);
289 echo "</td></tr>\n";
292 echo "</table>\n";
293 echo "<input type=\"submit\" value=\"".get_string("savemysubmission", "workshop")."\" />\n";
294 echo "</div></fieldset></form>\n";
298 /******************* list all submissions ************************************/
299 elseif ($action == 'listallsubmissions' ) {
300 if (!$users = workshop_get_students($workshop)) {
301 print_heading(get_string("nostudentsyet"));
302 print_footer($course);
303 exit;
305 print_heading(get_string("listofallsubmissions", "workshop").":", "CENTER");
306 workshop_list_all_submissions($workshop, $USER);
307 print_continue("view.php?id=$cm->id");
312 /******************* list for assessment student (submissions) ************************************/
313 elseif ($action == 'listforassessmentstudent' ) {
314 if (!$users = workshop_get_students($workshop)) {
315 print_heading(get_string("nostudentsyet"));
316 print_footer($course);
317 exit;
319 workshop_list_unassessed_student_submissions($workshop, $USER);
320 print_continue("view.php?id=$cm->id");
325 /******************* list for assessment teacher (submissions) ************************************/
326 elseif ($action == 'listforassessmentteacher' ) {
328 require_capability('mod/workshop:manage', $context);
330 workshop_list_unassessed_teacher_submissions($workshop, $USER);
331 print_continue("view.php?id=$cm->id");
336 /******************* remove (all) attachments ************************************/
337 elseif ($action == 'removeattachments' ) {
339 $form = data_submitted();
341 if (empty($form->sid)) {
342 error("Update submission: submission id missing");
345 $submission = get_record("workshop_submissions", "id", $form->sid);
347 // students are only allowed to remove their own attachments and only up to the deadline
348 if (!(workshop_is_teacher($workshop) or
349 (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
350 and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
351 error("You are not authorized to delete these attachments");
354 // amend title... just in case they were modified
355 // check existence of title
356 if (empty($form->title)) {
357 notify(get_string("notitlegiven", "workshop"));
358 } else {
359 set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
360 set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
362 print_string("removeallattachments", "workshop");
363 workshop_delete_submitted_files($workshop, $submission);
364 add_to_log($course->id, "workshop", "removeattachments", "view.php?id=$cm->id", "submission $submission->id");
366 print_continue("view.php?id=$cm->id#sid=$submission->id");
370 /******************* show submission ************************************/
371 elseif ($action == 'showsubmission' ) {
373 if (empty($sid)) {
374 error("Show submission: submission id missing");
377 $submission = get_record("workshop_submissions", "id", $sid);
378 $title = '"'.$submission->title.'" ';
379 if (workshop_is_teacher($workshop)) {
380 $title .= get_string('by', 'workshop').' '.workshop_fullname($submission->userid, $course->id);
382 print_heading($title);
383 echo '<div style="text-align:center">'.get_string('submitted', 'workshop').': '.userdate($submission->timecreated).'</div><br />';
384 workshop_print_submission($workshop, $submission);
385 print_continue(htmlentities($_SERVER['HTTP_REFERER'].'#sid='.$submission->id));
389 /*************** update (league table options teacher) ***************************/
390 elseif ($action == 'updateleaguetable') {
392 require_capability('mod/workshop:manage', $context);
394 // save number of entries in showleaguetable option
395 if ($nentries == 'All') {
396 $nentries = 99;
398 set_field("workshop", "showleaguetable", $nentries, "id", "$workshop->id");
400 // save the anonymous option
401 set_field("workshop", "anonymous", $anonymous, "id", "$workshop->id");
402 add_to_log($course->id, "workshop", "league table", "view.php?id=$cm->id", $nentries, $cm->id);
404 redirect("view.php?id=$cm->id");
408 /*************** update submission ***************************/
409 elseif ($action == 'updatesubmission') {
411 if (empty($sid)) {
412 error("Update submission: submission id missing");
414 $submission = get_record("workshop_submissions", "id", $sid);
416 // students are only allowed to update their own submission and only up to the deadline
417 if (!(workshop_is_teacher($workshop) or
418 (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
419 and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
420 error("You are not authorized to update your submission");
423 // check existence of title
424 if (empty($title)) {
425 $title = get_string("notitle", "workshop");
427 set_field("workshop_submissions", "title", $title, "id", $submission->id);
428 set_field("workshop_submissions", "description", trim($description), "id", $submission->id);
429 set_field("workshop_submissions", "timecreated", $timenow, "id", $submission->id);
430 if ($workshop->nattachments) {
431 require_once($CFG->dirroot.'/lib/uploadlib.php');
432 $um = new upload_manager(null,false,false,$course,false,$workshop->maxbytes);
433 if ($um->preprocess_files()) {
434 $dir = workshop_file_area_name($workshop, $submission);
435 if ($um->save_files($dir)) {
436 add_to_log($course->id, "workshop", "newattachment", "view.php?id=$cm->id", "$workshop->id");
437 print_heading(get_string("uploadsuccess", "workshop"));
439 // upload manager will print errors.
441 print_continue("view.php?id=$cm->id");
442 } else {
443 echo '</div>'; // close <div id='page'>
444 redirect("view.php?id=$cm->id#sid=$submission->id");
449 /*************** no man's land **************************************/
451 else {
453 error("Fatal Error: Unknown Action: ".$action."\n");
458 print_footer($course);