MDL-46775: Fix call to unavailable function when loading an invalid JS file
[moodle.git] / mod / workshop / exsubmission.php
blob327ea2e848243ba0b8009de72d41bf7bca268240
1 <?php
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.
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/>.
18 /**
19 * View, create or edit single example submission
21 * @package mod_workshop
22 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27 require_once(dirname(__FILE__).'/locallib.php');
29 $cmid = required_param('cmid', PARAM_INT); // course module id
30 $id = required_param('id', PARAM_INT); // example submission id, 0 for the new one
31 $edit = optional_param('edit', false, PARAM_BOOL); // open for editing?
32 $delete = optional_param('delete', false, PARAM_BOOL); // example removal requested
33 $confirm = optional_param('confirm', false, PARAM_BOOL); // example removal request confirmed
34 $assess = optional_param('assess', false, PARAM_BOOL); // assessment required
36 $cm = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
39 require_login($course, false, $cm);
40 if (isguestuser()) {
41 print_error('guestsarenotallowed');
44 $workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
45 $workshop = new workshop($workshop, $cm, $course);
47 $PAGE->set_url($workshop->exsubmission_url($id), array('edit' => $edit));
48 $PAGE->set_title($workshop->name);
49 $PAGE->set_heading($course->fullname);
50 if ($edit) {
51 $PAGE->navbar->add(get_string('exampleediting', 'workshop'));
52 } else {
53 $PAGE->navbar->add(get_string('example', 'workshop'));
55 $output = $PAGE->get_renderer('mod_workshop');
57 if ($id) { // example is specified
58 $example = $workshop->get_example_by_id($id);
59 } else { // no example specified - create new one
60 require_capability('mod/workshop:manageexamples', $workshop->context);
61 $example = new stdclass();
62 $example->id = null;
63 $example->authorid = $USER->id;
64 $example->example = 1;
67 $canmanage = has_capability('mod/workshop:manageexamples', $workshop->context);
68 $canassess = has_capability('mod/workshop:peerassess', $workshop->context);
69 $refasid = $DB->get_field('workshop_assessments', 'id', array('submissionid' => $example->id, 'weight' => 1));
71 if ($example->id and ($canmanage or ($workshop->assessing_examples_allowed() and $canassess))) {
72 // ok you can go
73 } elseif (is_null($example->id) and $canmanage) {
74 // ok you can go
75 } else {
76 print_error('nopermissions', 'error', $workshop->view_url(), 'view or manage example submission');
79 if ($id and $delete and $confirm and $canmanage) {
80 require_sesskey();
81 $workshop->delete_submission($example);
82 redirect($workshop->view_url());
85 if ($id and $assess and $canmanage) {
86 // reference assessment of an example is the assessment with the weight = 1. There should be just one
87 // such assessment
88 require_sesskey();
89 if (!$refasid) {
90 $refasid = $workshop->add_allocation($example, $USER->id, 1);
92 redirect($workshop->exassess_url($refasid));
95 if ($id and $assess and $canassess) {
96 // training assessment of an example is the assessment with the weight = 0
97 require_sesskey();
98 $asid = $DB->get_field('workshop_assessments', 'id',
99 array('submissionid' => $example->id, 'weight' => 0, 'reviewerid' => $USER->id));
100 if (!$asid) {
101 $asid = $workshop->add_allocation($example, $USER->id, 0);
103 if ($asid == workshop::ALLOCATION_EXISTS) {
104 // the training assessment of the example was not found but the allocation already
105 // exists. this probably means that the user is the author of the reference assessment.
106 echo $output->header();
107 echo $output->box(get_string('assessmentreferenceconflict', 'workshop'));
108 echo $output->continue_button($workshop->view_url());
109 echo $output->footer();
110 die();
112 redirect($workshop->exassess_url($asid));
115 if ($edit and $canmanage) {
116 require_once(dirname(__FILE__).'/submission_form.php');
118 $maxfiles = $workshop->nattachments;
119 $maxbytes = $workshop->maxbytes;
120 $contentopts = array(
121 'trusttext' => true,
122 'subdirs' => false,
123 'maxfiles' => $maxfiles,
124 'maxbytes' => $maxbytes,
125 'context' => $workshop->context
128 $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
129 $example = file_prepare_standard_editor($example, 'content', $contentopts, $workshop->context,
130 'mod_workshop', 'submission_content', $example->id);
131 $example = file_prepare_standard_filemanager($example, 'attachment', $attachmentopts, $workshop->context,
132 'mod_workshop', 'submission_attachment', $example->id);
134 $mform = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop,
135 'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
137 if ($mform->is_cancelled()) {
138 redirect($workshop->view_url());
140 } elseif ($canmanage and $formdata = $mform->get_data()) {
141 if ($formdata->example == 1) {
142 // this was used just for validation, it must be set to one when dealing with example submissions
143 unset($formdata->example);
144 } else {
145 throw new coding_exception('Invalid submission form data value: example');
147 $timenow = time();
148 if (is_null($example->id)) {
149 $formdata->workshopid = $workshop->id;
150 $formdata->example = 1;
151 $formdata->authorid = $USER->id;
152 $formdata->timecreated = $timenow;
153 $formdata->feedbackauthorformat = editors_get_preferred_format();
155 $formdata->timemodified = $timenow;
156 $formdata->title = trim($formdata->title);
157 $formdata->content = ''; // updated later
158 $formdata->contentformat = FORMAT_HTML; // updated later
159 $formdata->contenttrust = 0; // updated later
160 if (is_null($example->id)) {
161 $example->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
162 } else {
163 if (empty($formdata->id) or empty($example->id) or ($formdata->id != $example->id)) {
164 throw new moodle_exception('err_examplesubmissionid', 'workshop');
167 // save and relink embedded images and save attachments
168 $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
169 'mod_workshop', 'submission_content', $example->id);
170 $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
171 'mod_workshop', 'submission_attachment', $example->id);
172 if (empty($formdata->attachment)) {
173 // explicit cast to zero integer
174 $formdata->attachment = 0;
176 // store the updated values or re-save the new example (re-saving needed because URLs are now rewritten)
177 $DB->update_record('workshop_submissions', $formdata);
178 redirect($workshop->exsubmission_url($formdata->id));
182 // Output starts here
183 echo $output->header();
184 echo $output->heading(format_string($workshop->name), 2);
186 // show instructions for submitting as they may contain some list of questions and we need to know them
187 // while reading the submitted answer
188 if (trim($workshop->instructauthors)) {
189 $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
190 'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
191 print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
192 echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
193 print_collapsible_region_end();
196 // if in edit mode, display the form to edit the example
197 if ($edit and $canmanage) {
198 $mform->display();
199 echo $output->footer();
200 die();
203 // else display the example...
204 if ($example->id) {
205 if ($canmanage and $delete) {
206 echo $output->confirm(get_string('exampledeleteconfirm', 'workshop'),
207 new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
209 if ($canmanage and !$delete and !$DB->record_exists_select('workshop_assessments',
210 'grade IS NOT NULL AND weight=1 AND submissionid = ?', array($example->id))) {
211 echo $output->confirm(get_string('assessmentreferenceneeded', 'workshop'),
212 new moodle_url($PAGE->url, array('assess' => 1)), $workshop->view_url());
214 echo $output->render($workshop->prepare_example_submission($example));
216 // ...with an option to edit or remove it
217 echo $output->container_start('buttonsbar');
218 if ($canmanage) {
219 if (empty($edit) and empty($delete)) {
220 $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
221 echo $output->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
223 $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
224 echo $output->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
227 // ...and optionally assess it
228 if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
229 $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
230 echo $output->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
232 echo $output->container_end(); // buttonsbar
233 // and possibly display the example's review(s) - todo
234 echo $output->footer();