Commit | Line | Data |
---|---|---|
33e4dea6 DM |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | ||
19 | /** | |
20 | * Submit an assignment or edit the already submitted work | |
21 | * | |
22 | * @package mod-workshop | |
23 | * @copyright 2009 David Mudrak <david.mudrak@gmail.com> | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
27 | require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); | |
28 | require_once(dirname(__FILE__).'/lib.php'); | |
29 | require_once(dirname(__FILE__).'/submission_form.php'); | |
30 | ||
31 | $cmid = required_param('cmid', PARAM_INT); // course module id | |
32 | $id = optional_param('id', 0, PARAM_INT); // submission id | |
33 | ||
34 | if (!$cm = get_coursemodule_from_id('workshop', $cmid)) { | |
35 | print_error('invalidcoursemodule'); | |
36 | } | |
37 | ||
38 | if (!$course = $DB->get_record('course', array('id' => $cm->course))) { | |
39 | print_error('coursemisconf'); | |
40 | } | |
41 | ||
42 | require_login($course, false, $cm); | |
43 | ||
44 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
45 | ||
46 | if (isguestuser()) { | |
47 | print_error('guestnoedit', 'workshop', "$CFG->wwwroot/mod/workshop/view.php?id=$cmid"); | |
48 | } | |
49 | ||
50 | if (!$workshop = $DB->get_record('workshop', array('id' => $cm->instance))) { | |
51 | print_error('invalidid', 'workshop'); | |
52 | } | |
53 | ||
54 | if ($id) { // if submission is specified | |
55 | if (!$submission = $DB->get_record('workshop_submissions', array('id' => $id, 'workshopid' => $workshop->id))) { | |
56 | print_error('invalidsubmissionid', 'workshop'); | |
57 | } | |
58 | // todo check access rights | |
59 | ||
60 | } else { // new submission | |
61 | //require_capability('mod/workshop:submit', $context); | |
62 | $submission = new object(); | |
63 | $submission->id = null; | |
64 | } | |
65 | ||
66 | $maxfiles = $workshop->nattachments; | |
67 | $maxbytes = $workshop->maxbytes; | |
68 | ||
69 | $dataoptions = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes); | |
70 | $attachmentoptions = array('subdirs' => false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes); | |
71 | ||
72 | $submission = file_prepare_standard_editor($submission, 'data', $dataoptions, $context, 'workshop_submission', $submission->id); | |
73 | $submission = file_prepare_standard_filemanager($submission, 'attachment', $attachmentoptions, $context, | |
74 | 'workshop_attachment', $submission->id); | |
75 | ||
76 | $submission->cmid = $cm->id; | |
77 | ||
78 | // create form and set initial data | |
79 | $mform = new workshop_submission_form(null, array('current' => $submission, 'cm' => $cm, 'workshop'=>$workshop, | |
80 | 'dataoptions' => $dataoptions, 'attachmentoptions'=>$attachmentoptions)); | |
81 | ||
82 | if ($mform->is_cancelled()){ | |
83 | if ($id){ | |
84 | redirect("view.php?id=$cm->id"); | |
85 | } else { | |
86 | redirect("view.php?id=$cm->id"); | |
87 | } | |
88 | ||
89 | } else if ($submission = $mform->get_data()) { | |
90 | ||
91 | $timenow = time(); | |
92 | ||
93 | if (empty($submission->id)) { | |
94 | $submission->workshopid = $workshop->id; | |
95 | $submission->example = 0; // todo add examples support | |
96 | $submission->userid = $USER->id; | |
97 | $submission->timecreated = $timenow; | |
98 | } | |
99 | ||
100 | $submission->timemodified = $timenow; | |
101 | $submission->title = trim($submission->title); | |
102 | $submission->data = ''; // updated later | |
103 | $submission->dataformat = FORMAT_HTML; // updated later | |
104 | $submission->datatrust = 0; // updated later | |
105 | ||
106 | if (empty($submission->id)) { | |
107 | //new submission | |
108 | $submission->id = $DB->insert_record('workshop_submissions', $submission); | |
109 | // todo add to log | |
110 | ||
111 | } else { | |
112 | //existing submission | |
113 | $DB->update_record('workshop_submissions', $submission); | |
114 | // todo add to log | |
115 | } | |
116 | ||
117 | // save and relink embedded images and save attachments | |
118 | $submission = file_postupdate_standard_editor($submission, 'data', $dataoptions, $context, 'workshop_submission', $submission->id); | |
119 | $submission = file_postupdate_standard_filemanager($submission, 'attachment', $attachmentoptions, $context, 'workshop_attachment', $submission->id); | |
120 | ||
121 | // store the updated value values | |
122 | $DB->update_record('workshop_submissions', $submission); | |
123 | ||
124 | redirect("view.php?id=$cm->id"); | |
125 | } | |
126 | ||
127 | $stredit = empty($submission->id) ? get_string('addsubmission', 'workshop') : get_string('edit'); | |
128 | ||
129 | $navigation = build_navigation($stredit, $cm); | |
130 | print_header_simple(format_string($workshop->name), "", $navigation, "", "", true, "", navmenu($course, $cm)); | |
131 | ||
132 | print_heading(format_string($workshop->name)); | |
133 | ||
134 | $mform->display(); | |
135 | ||
136 | print_footer($course); |