File: submissions.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (472 lines) | stat: -rw-r--r-- 19,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php  // $Id: submissions.php,v 1.46.2.1 2006/08/10 15:30:54 skodak Exp $

/*************************************************
    ACTIONS handled are:

    adminamendtitle
    confirmdelete
    delete
    adminlist
    editsubmission
    listallsubmissions
    listforassessmentstudent
    listforassessmentteacher
    showsubmission
    updatesubmission


************************************************/

    require("../../config.php");
    require("lib.php");
    require("locallib.php");

    $id          = required_param('id', PARAM_INT);    // Course Module ID
    $action      = optional_param('action', '', PARAM_ALPHA);
    $sid         = optional_param('sid', 0, PARAM_INT); //submission id
    $order       = optional_param('order', 'name', PARAM_ALPHA);
    $title       = optional_param('title', '', PARAM_CLEAN);
    $nentries    = optional_param('nentries', '', PARAM_ALPHANUM);
    $anonymous   = optional_param('anonymous', '', PARAM_CLEAN);
    $description = optional_param('description', '', PARAM_CLEAN);

    $timenow = time();

    // get some useful stuff...
    if (! $cm = get_coursemodule_from_id('workshop', $id)) {
        error("Course Module ID was incorrect");
    }
    if (! $course = get_record("course", "id", $cm->course)) {
        error("Course is misconfigured");
    }
    if (! $workshop = get_record("workshop", "id", $cm->instance)) {
        error("Course module is incorrect");
    }

    require_login($course->id, false, $cm);

    $strworkshops = get_string("modulenameplural", "workshop");
    $strworkshop  = get_string("modulename", "workshop");
    $strsubmissions = get_string("submissions", "workshop");

    // ... print the header and...
    print_header_simple(format_string($workshop->name), "",
                 "<a href=\"index.php?id=$course->id\">$strworkshops</a> ->
                  <a href=\"view.php?id=$cm->id\">".format_string($workshop->name,true)."</a> -> $strsubmissions",
                  "", "", true);

    //...get the action or set up an suitable default
    if (empty($action)) {
        $action = "listallsubmissions";
    }


/******************* admin amend title ************************************/
    elseif ($action == 'adminamendtitle' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }
        if (empty($sid)) {
            error("Admin Amend Title: submission id missing");
        }

        $submission = get_record("workshop_submissions", "id", $sid);
        print_heading(get_string("amendtitle", "workshop"));
        ?>
        <form name="amendtitleform" action="submissions.php" method="post">
        <input type="hidden" name="action" value="adminupdatetitle" />
        <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
        <input type="hidden" name="sid" value="<?php echo $sid ?>" />
        <center>
        <table cellpadding="5" border="1">
        <?php

        // now get the comment
        echo "<tr valign=\"top\">\n";
        echo "  <td align=\"right\"><p><b>". get_string("title", "workshop").":</b></p></td>\n";
        echo "  <td>\n";
        echo "      <input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
        echo "  </td></tr></table>\n";
        echo "<input type=\"submit\" value=\"".get_string("amendtitle", "workshop")."\" />\n";
        echo "</center></form>\n";

        print_heading("<a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id#sid=$submission->id\">".get_string("cancel")."</a>");
    }


    /******************* admin clear late (flag) ************************************/
    elseif ($action == 'adminclearlate' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }
        if (empty($sid)) {
            error("Admin clear late flag: submission id missing");
        }

        if (!$submission = get_record("workshop_submissions", "id", $sid)) {
            error("Admin clear late flag: can not get submission record");
        }
        if (set_field("workshop_submissions", "late", 0, "id", $sid)) {
            print_heading(get_string("clearlateflag", "workshop")." ".get_string("ok"));
        }

        add_to_log($course->id, "workshop", "late flag cleared", "view.php?id=$cm->id", "submission $submission->id");

        redirect("view.php?id=$cm->id");
    }


    /******************* confirm delete ************************************/
    elseif ($action == 'confirmdelete' ) {

        if (empty($sid)) {
            error("Confirm delete: submission id missing");
            }
        notice_yesno(get_string("confirmdeletionofthisitem","workshop", get_string("submission", "workshop")),
             "submissions.php?action=delete&amp;id=$cm->id&amp;sid=$sid", "view.php?id=$cm->id#sid=$sid");
        }


    /******************* delete ************************************/
    elseif ($action == 'delete' ) {

        if (empty($sid)) {
            error("Delete: submission id missing");
        }

        if (!$submission = get_record("workshop_submissions", "id", $sid)) {
            error("Admin delete: can not get submission record");
        }

        // students are only allowed to delete their own submission and only up to the deadline
        if (!(isteacher($course->id) or
               (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
                   and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
            error("You are not authorized to delete this submission");
        }

        print_string("deleting", "workshop");
        // first get any assessments...
        if ($assessments = workshop_get_assessments($submission, 'ALL')) {
            foreach($assessments as $assessment) {
                // ...and all the associated records...
                delete_records("workshop_comments", "assessmentid", $assessment->id);
                delete_records("workshop_grades", "assessmentid", $assessment->id);
                echo ".";
            }
            // ...now delete the assessments...
            delete_records("workshop_assessments", "submissionid", $submission->id);
        }
        // ...and the submission record...
        delete_records("workshop_submissions", "id", $submission->id);
        // ..and finally the submitted file
        workshop_delete_submitted_files($workshop, $submission);

        redirect("view.php?id=$cm->id");
    }


    /******************* admin (confirm) late flag ************************************/
    elseif ($action == 'adminlateflag' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }
        if (empty($sid)) {
            error("Admin confirm late flag: submission id missing");
        }
        if (!$submission = get_record("workshop_submissions", "id", $sid)) {
            error("Admin confirm late flag: can not get submission record");
        }

        notice_yesno(get_string("clearlateflag","workshop")."?",
             "submissions.php?action=adminclearlate&amp;id=$cm->id&amp;sid=$sid",
             "view.php?id=$cm->id");
    }


    /******************* list all submissions ************************************/
    elseif ($action == 'adminlist' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }

        workshop_list_submissions_for_admin($workshop, $order);
        print_continue("view.php?id=$cm->id");

    }


    /******************* admin update title ************************************/
    elseif ($action == 'adminupdatetitle' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }
        if (empty($sid)) {
            error("Admin Update Title: submission id missing");
        }

        if (set_field("workshop_submissions", "title", $title, "id", $sid)) {
            print_heading(get_string("amendtitle", "workshop")." ".get_string("ok"));
        }
        print_continue("view.php?id=$cm->id");
    }


    /******************* confirm remove attachments ************************************/
    elseif ($action == 'confirmremoveattachments' ) {

        if (empty($sid)) {
            error("Admin confirm delete: submission id missing");
        }
        if (!$submission = get_record("workshop_submissions", "id", $sid)) {
            error("Admin delete: can not get submission record");
        }

        notice_yesno(get_string("confirmremoveattachments","workshop"),
             "submissions.php?action=removeattachments&amp;id=$cm->id&amp;sid=$sid",
             "view.php?id=$cm->id");
    }


    /******************* edit submission ************************************/
    elseif ($action == 'editsubmission' ) {

        if (empty($sid)) {
            error("Edit submission: submission id missing");
        }
        $usehtmleditor = can_use_html_editor();

        $submission = get_record("workshop_submissions", "id", $sid);
        print_heading(get_string("editsubmission", "workshop"));
        if ($submission->userid <> $USER->id) {
            error("Edit submission: Userids do not match");
        }
        if (($submission->timecreated < ($timenow - $CFG->maxeditingtime)) and ($workshop->assessmentstart < $timenow)) {
            error(get_string('notallowed', 'workshop'));
        }
        ?>
        <form name="editform" enctype="multipart/form-data" action="submissions.php" method="post">
        <input type="hidden" name="action" value="updatesubmission" />
        <input type="hidden" name="id" value="<?php echo $cm->id ?>" />
        <input type="hidden" name="sid" value="<?php echo $sid ?>" />
        <center>
        <table cellpadding="5" border="1">
        <?php
        echo "<tr valign=\"top\"><td><b>". get_string("title", "workshop").":</b>\n";
        echo "<input type=\"text\" name=\"title\" size=\"60\" maxlength=\"100\" value=\"$submission->title\" />\n";
        echo "</td></tr><tr><td><b>".get_string("submission", "workshop").":</b><br />\n";
        print_textarea($usehtmleditor, 25,70, 630, 400, "description", $submission->description);
        use_html_editor("description");
        echo "</td></tr>\n";
        if ($workshop->nattachments) {
            $filearea = workshop_file_area_name($workshop, $submission);
            if ($basedir = workshop_file_area($workshop, $submission)) {
                if ($files = get_directory_list($basedir)) {
                    echo "<tr><td><b>".get_string("attachments", "workshop").
                        "</b><div align=\"right\"><input type=\"button\" value=\"".get_string("removeallattachments",
                        "workshop")."\" onclick=\"document.editform.action.value='removeattachments';
                        document.editform.submit();\"/></div></td></tr>\n";
                    $n = 1;
                    foreach ($files as $file) {
                        $icon = mimeinfo("icon", $file);
                        if ($CFG->slasharguments) {
                            $ffurl = "file.php/$filearea/$file";
                        } else {
                            $ffurl = "file.php?file=/$filearea/$file";
                        }
                        echo "<tr><td>".get_string("attachment", "workshop")." $n: <img src=\"$CFG->pixpath/f/$icon\"
                            height=\"16\" width=\"16\" border=\"0\" alt=\"File\" />".
                            "&nbsp;<a target=\"uploadedfile\" href=\"$CFG->wwwroot/$ffurl\">$file</a></td></tr>\n";
                    }
                } else {
                    echo "<tr><td><b>".get_string("noattachments", "workshop")."</b></td></tr>\n";
                }
            }
            echo "<tr><td>\n";
            require_once($CFG->dirroot.'/lib/uploadlib.php');
            for ($i=0; $i < $workshop->nattachments; $i++) {
                $iplus1 = $i + 1;
                $tag[$i] = get_string("newattachment", "workshop")." $iplus1:";
            }
            upload_print_form_fragment($workshop->nattachments,null,$tag,false,null,$course->maxbytes,
                $workshop->maxbytes,false);
            echo "</td></tr>\n";
        }

        echo "</table>\n";
        echo "<input type=\"submit\" value=\"".get_string("savemysubmission", "workshop")."\" />\n";
        echo "</center></form>\n";
    }


    /******************* list all submissions ************************************/
    elseif ($action == 'listallsubmissions' ) {
        if (!$users = get_course_students($course->id)) {
            print_heading(get_string("nostudentsyet"));
            print_footer($course);
            exit;
        }
        print_heading(get_string("listofallsubmissions", "workshop").":", "CENTER");
        workshop_list_all_submissions($workshop, $USER);
        print_continue("view.php?id=$cm->id");

    }


    /******************* list for assessment student (submissions) ************************************/
    elseif ($action == 'listforassessmentstudent' ) {
        if (!$users = get_course_students($course->id)) {
            print_heading(get_string("nostudentsyet"));
            print_footer($course);
            exit;
        }
        workshop_list_unassessed_student_submissions($workshop, $USER);
        print_continue("view.php?id=$cm->id");

    }


    /******************* list for assessment teacher (submissions) ************************************/
    elseif ($action == 'listforassessmentteacher' ) {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }

        workshop_list_unassessed_teacher_submissions($workshop, $USER);
        print_continue("view.php?id=$cm->id");

    }


    /******************* remove (all) attachments ************************************/
    elseif ($action == 'removeattachments' ) {

        $form = data_submitted();

        if (empty($form->sid)) {
            error("Update submission: submission id missing");
        }

        $submission = get_record("workshop_submissions", "id", $form->sid);

        // students are only allowed to remove their own attachments and only up to the deadline
        if (!(isteacher($course->id) or
               (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
                   and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
            error("You are not authorized to delete these attachments");
        }

        // amend title... just in case they were modified
        // check existence of title
        if (empty($form->title)) {
            notify(get_string("notitlegiven", "workshop"));
        } else {
            set_field("workshop_submissions", "title", $form->title, "id", $submission->id);
            set_field("workshop_submissions", "description", trim($form->description), "id", $submission->id);
        }
        print_string("removeallattachments", "workshop");
        workshop_delete_submitted_files($workshop, $submission);
        add_to_log($course->id, "workshop", "removeattachments", "view.php?id=$cm->id", "submission $submission->id");

        print_continue("view.php?id=$cm->id#sid=$submission->id");
    }


    /******************* show submission ************************************/
    elseif ($action == 'showsubmission' ) {

        if (empty($sid)) {
            error("Show submission: submission id missing");
        }

        $submission = get_record("workshop_submissions", "id", $sid);
        $title = '"'.$submission->title.'" ';
        if (isteacher($course->id)) {
            $title .= get_string('by', 'workshop').' '.workshop_fullname($submission->userid, $course->id);
        }
        print_heading($title);
        echo '<center>'.get_string('submitted', 'workshop').': '.userdate($submission->timecreated).'</center><br />';
        workshop_print_submission($workshop, $submission);
        print_continue($_SERVER['HTTP_REFERER'].'#sid='.$submission->id);
    }


    /*************** update (league table options teacher) ***************************/
    elseif ($action == 'updateleaguetable') {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }

        // save number of entries in showleaguetable option
        if ($nentries == 'All') {
            $nentries = 99;
        }
        set_field("workshop", "showleaguetable", $nentries, "id", "$workshop->id");

        // save the anonymous option
        set_field("workshop", "anonymous", $anonymous, "id", "$workshop->id");
        add_to_log($course->id, "workshop", "league table", "view.php?id=$cm->id", $nentries, $cm->id);

        redirect("view.php?id=$cm->id");
    }


    /*************** update submission ***************************/
    elseif ($action == 'updatesubmission') {

        if (empty($sid)) {
            error("Update submission: submission id missing");
        }
        $submission = get_record("workshop_submissions", "id", $sid);

        // students are only allowed to update their own submission and only up to the deadline
        if (!(isteacher($course->id) or
               (($USER->id = $submission->userid) and ($timenow < $workshop->submissionend)
                   and (($timenow < $workshop->assessmentstart) or ($timenow < $submission->timecreated + $CFG->maxeditingtime))))) {
            error("You are not authorized to update your submission");
        }

        // check existence of title
        if (empty($title)) {
            $title = get_string("notitle", "workshop");
        }
        set_field("workshop_submissions", "title", $title, "id", $submission->id);
        set_field("workshop_submissions", "description", trim($description), "id", $submission->id);
        set_field("workshop_submissions", "timecreated", $timenow, "id", $submission->id);
        if ($workshop->nattachments) {
            require_once($CFG->dirroot.'/lib/uploadlib.php');
            $um = new upload_manager(null,false,false,$course,false,$workshop->maxbytes);
            if ($um->preprocess_files()) {
                $dir = workshop_file_area_name($workshop, $submission);
                if ($um->save_files($dir)) {
                    add_to_log($course->id, "workshop", "newattachment", "view.php?id=$cm->id", "$workshop->id");
                    print_heading(get_string("uploadsuccess", "workshop"));
                }
                // upload manager will print errors.
            }
            print_continue("view.php?id=$cm->id");
        } else {
            redirect("view.php?id=$cm->id#sid=$submission->id");
        }
    }


    /*************** no man's land **************************************/

    else {

        error("Fatal Error: Unknown Action: ".$action."\n");

    }


    print_footer($course);

?>