Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / workshop / upload.php
blob5d040de7fe97232488c39dafd450c773c86b043a
1 <?php // $Id$
3 require("../../config.php");
4 require("lib.php");
5 require("locallib.php");
7 $id = required_param('id', PARAM_INT); // CM ID
10 if (! $cm = get_record("course_modules", "id", $id)) {
11 error("Course Module ID was incorrect");
13 if (! $course = get_record("course", "id", $cm->course)) {
14 error("Course is misconfigured");
16 if (! $workshop = get_record("workshop", "id", $cm->instance)) {
17 error("Course module is incorrect");
20 require_login($course->id, false, $cm);
22 $strworkshops = get_string('modulenameplural', 'workshop');
23 $strworkshop = get_string('modulename', 'workshop');
24 $strsubmission = get_string('submission', 'workshop');
26 $navigation = build_navigation($strsubmission, $cm);
27 print_header_simple(format_string($workshop->name)." : $strsubmission", "", $navigation,
28 "", "", true);
29 $timenow = time();
31 $form = data_submitted("nomatch"); // POST may come from two forms
33 // don't be picky about not having a title
34 if (!$title = $form->title) {
35 $title = get_string("notitle", "workshop");
38 // check that this is not a "rapid" second submission, caused by using the back button
39 // only check if a student, teachers may want to submit a set of workshop examples rapidly
40 if (workshop_is_student($workshop)) {
41 if ($submissions = workshop_get_user_submissions($workshop, $USER)) {
42 // returns all submissions, newest on first
43 foreach ($submissions as $submission) {
44 if ($submission->timecreated > $timenow - $CFG->maxeditingtime) {
45 // ignore this new submission
46 redirect("view.php?id=$cm->id");
47 print_footer($course);
48 exit();
54 // get the current set of submissions
55 $submissions = workshop_get_user_submissions($workshop, $USER);
56 // add new submission record
57 $newsubmission->workshopid = $workshop->id;
58 $newsubmission->userid = $USER->id;
59 $newsubmission->title = clean_param($title, PARAM_CLEAN);
60 $newsubmission->description = trim(clean_param($form->description, PARAM_CLEAN));
61 $newsubmission->timecreated = $timenow;
62 if ($timenow > $workshop->submissionend) {
63 $newsubmission->late = 1;
65 if (!$newsubmission->id = insert_record("workshop_submissions", $newsubmission)) {
66 error("Workshop submission: Failure to create new submission record!");
68 // see if this is a resubmission by looking at the previous submissions...
69 if ($submissions and ($workshop->submissionstart > time())) { // ...but not teacher submissions
70 // find the last submission
71 foreach ($submissions as $submission) {
72 $lastsubmission = $submission;
73 break;
75 // find all the possible assessments of this submission
76 // ...and if they have been assessed give the assessor a new assessment
77 // based on their old assessment, if the assessment has not be made
78 // just delete it!
79 if ($assessments = workshop_get_assessments($submission, 'ALL')) {
80 foreach ($assessments as $assessment) {
81 if ($assessment->timecreated < $timenow) {
82 // a Cold or Warm assessment...
83 if ($assessment->userid <> $USER->id) {
84 // only copy other students assessment not the self assessment (if present)
85 // copy it with feedback..
86 $newassessment = workshop_copy_assessment($assessment, $newsubmission, true);
87 // set the resubmission flag so student can be emailed/told about
88 // this assessment
89 set_field("workshop_assessments", "resubmission", 1, "id", $newassessment->id);
91 } else {
92 // a hot assessment, was not used, just dump it
93 delete_records("workshop_assessments", "id", $assessment->id);
97 add_to_log($course->id, "workshop", "resubmit", "view.php?id=$cm->id", "$workshop->id","$cm->id");
99 // do something about the attachments, if there are any
100 if ($workshop->nattachments) {
101 require_once($CFG->dirroot.'/lib/uploadlib.php');
102 $um = new upload_manager(null,false,false,$course,false,$workshop->maxbytes);
103 if ($um->preprocess_files()) {
104 $dir = workshop_file_area_name($workshop, $newsubmission);
105 if ($um->save_files($dir)) {
106 print_heading(get_string("uploadsuccess", "workshop"));
108 // um will take care of printing errors.
111 if (!$workshop->nattachments) {
112 print_heading(get_string("submitted", "workshop")." ".get_string("ok"));
114 add_to_log($course->id, "workshop", "submit", "view.php?id=$cm->id", "$workshop->id", "$cm->id");
115 print_continue("view.php?id=$cm->id");
116 print_footer($course);