d1ab70dbd4067077030761f49c1fff6586b73808
[moodle.git] / mod / workshop / exsubmission.php
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.
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/>.
18 /**
19  * View, create or edit single example submission
20  *
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
25  */
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');
43 }
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'));
55 }
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;
68 }
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');
80 }
82 if ($id and $delete and $confirm and $canmanage) {
83     require_sesskey();
84     $workshop->delete_submission($example);
85     redirect($workshop->view_url());
86 }
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);
94     }
95     redirect($workshop->exassess_url($refasid));
96 }
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);
105     }
106     redirect($workshop->exassess_url($asid));
109 if ($edit and $canmanage) {
110     require_once(dirname(__FILE__).'/submission_form.php');
112     $maxfiles       = $workshop->nattachments;
113     $maxbytes       = $workshop->maxbytes;
114     $contentopts    = array(
115                         'trusttext' => true,
116                         'subdirs'   => false,
117                         'maxfiles'  => $maxfiles,
118                         'maxbytes'  => $maxbytes,
119                         'context'   => $workshop->context
120                       );
122     $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
123     $example        = file_prepare_standard_editor($example, 'content', $contentopts, $workshop->context,
124                                         'mod_workshop', 'submission_content', $example->id);
125     $example        = file_prepare_standard_filemanager($example, 'attachment', $attachmentopts, $workshop->context,
126                                         'mod_workshop', 'submission_attachment', $example->id);
128     $mform          = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop,
129                                                     'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
131     if ($mform->is_cancelled()) {
132         redirect($workshop->view_url());
134     } elseif ($canmanage and $formdata = $mform->get_data()) {
135         if ($formdata->example == 1) {
136             // this was used just for validation, it must be set to one when dealing with example submissions
137             unset($formdata->example);
138         } else {
139             throw new coding_exception('Invalid submission form data value: example');
140         }
141         $timenow = time();
142         if (is_null($example->id)) {
143             $formdata->workshopid     = $workshop->id;
144             $formdata->example        = 1;
145             $formdata->authorid       = $USER->id;
146             $formdata->timecreated    = $timenow;
147             $formdata->feedbackauthorformat = editors_get_preferred_format();
148         }
149         $formdata->timemodified       = $timenow;
150         $formdata->title              = trim($formdata->title);
151         $formdata->content            = '';          // updated later
152         $formdata->contentformat      = FORMAT_HTML; // updated later
153         $formdata->contenttrust       = 0;           // updated later
154         if (is_null($example->id)) {
155             $example->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
156             $workshop->log('add example', $workshop->exsubmission_url($example->id), $example->id);
157         } else {
158             $workshop->log('update example', $workshop->exsubmission_url($example->id), $example->id);
159             if (empty($formdata->id) or empty($example->id) or ($formdata->id != $example->id)) {
160                 throw new moodle_exception('err_examplesubmissionid', 'workshop');
161             }
162         }
163         // save and relink embedded images and save attachments
164         $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
165                                                       'mod_workshop', 'submission_content', $example->id);
166         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
167                                                            'mod_workshop', 'submission_attachment', $example->id);
168         if (empty($formdata->attachment)) {
169             // explicit cast to zero integer
170             $formdata->attachment = 0;
171         }
172         // store the updated values or re-save the new example (re-saving needed because URLs are now rewritten)
173         $DB->update_record('workshop_submissions', $formdata);
174         redirect($workshop->exsubmission_url($formdata->id));
175     }
178 // Output starts here
179 echo $output->header();
180 echo $output->heading(format_string($workshop->name), 2);
182 // show instructions for submitting as they may contain some list of questions and we need to know them
183 // while reading the submitted answer
184 if (trim($workshop->instructauthors)) {
185     $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
186         'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
187     print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
188     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
189     print_collapsible_region_end();
192 // if in edit mode, display the form to edit the example
193 if ($edit and $canmanage) {
194     $mform->display();
195     echo $output->footer();
196     die();
199 // else display the example...
200 if ($example->id) {
201     if ($canmanage and $delete) {
202     echo $output->confirm(get_string('exampledeleteconfirm', 'workshop'),
203             new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
204     }
205     if ($canmanage and !$delete and !$DB->record_exists_select('workshop_assessments',
206             'grade IS NOT NULL AND weight=1 AND submissionid = ?', array($example->id))) {
207         echo $output->confirm(get_string('assessmentreferenceneeded', 'workshop'),
208                 new moodle_url($PAGE->url, array('assess' => 1)), $workshop->view_url());
209     }
210     echo $output->render($workshop->prepare_example_submission($example));
212 // ...with an option to edit or remove it
213 echo $output->container_start('buttonsbar');
214 if ($canmanage) {
215     if (empty($edit) and empty($delete)) {
216         $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
217         echo $output->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
219         $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
220         echo $output->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
221     }
223 // ...and optionally assess it
224 if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
225     $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
226     echo $output->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
228 echo $output->container_end(); // buttonsbar
229 // and possibly display the example's review(s) - todo
230 echo $output->footer();