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