MDL-27550 workshop: assessments are now displayed via proper rendering subsystem
[moodle.git] / mod / workshop / submission.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 a single (usually the own) submission, submit own work.
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     = optional_param('id', 0, PARAM_INT);           // submission id
32 $edit   = optional_param('edit', false, PARAM_BOOL);    // open for editing?
33 $assess = optional_param('assess', false, PARAM_BOOL);  // instant assessment required
35 $cm     = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
36 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38 require_login($course, false, $cm);
39 if (isguestuser()) {
40     print_error('guestsarenotallowed');
41 }
43 $workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
44 $workshop = new workshop($workshop, $cm, $course);
46 $PAGE->set_url($workshop->submission_url(), array('cmid' => $cmid, 'id' => $id, 'edit' => $edit));
48 if ($id) { // submission is specified
49     $submission = $workshop->get_submission_by_id($id);
50     $workshop->log('view submission', $workshop->submission_url($submission->id), $submission->id);
52 } else { // no submission specified
53     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
54         $submission = new stdclass();
55         $submission->id = null;
56         $submission->authorid = $USER->id;
57         $submission->example = 0;
58         $submission->grade = null;
59         $submission->gradeover = null;
60         $submission->published = null;
61         $submission->feedbackauthor = null;
62         $submission->feedbackauthorformat = editors_get_preferred_format();
63     }
64 }
66 $ownsubmission  = $submission->authorid == $USER->id;
67 $canviewall     = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
68 $cansubmit      = has_capability('mod/workshop:submit', $workshop->context);
69 $canallocate    = has_capability('mod/workshop:allocate', $workshop->context);
70 $canpublish     = has_capability('mod/workshop:publishsubmissions', $workshop->context);
71 $canoverride    = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
72 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
73 $isreviewer     = !empty($userassessment);
74 $editable       = ($cansubmit and $ownsubmission);
75 $ispublished    = ($workshop->phase == workshop::PHASE_CLOSED
76                     and $submission->published == 1
77                     and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
79 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
80     $editable = false;
81 }
82 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
83     $editable = false;
84 }
86 if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
87         and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
88     // check that all required examples have been assessed by the user
89     $examples = $workshop->get_examples_for_reviewer($USER->id);
90     foreach ($examples as $exampleid => $example) {
91         if (is_null($example->grade)) {
92             $editable = false;
93             break;
94         }
95     }
96 }
97 $edit = ($editable and $edit);
99 $seenaspublished = false; // is the submission seen as a published submission?
101 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
102     // ok you can go
103 } elseif ($submission->id and $ispublished) {
104     // ok you can go
105     $seenaspublished = true;
106 } elseif (is_null($submission->id) and $cansubmit) {
107     // ok you can go
108 } else {
109     print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
112 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
113     require_sesskey();
114     $assessmentid = $workshop->add_allocation($submission, $USER->id);
115     redirect($workshop->assess_url($assessmentid));
118 if ($edit) {
119     require_once(dirname(__FILE__).'/submission_form.php');
121     $maxfiles       = $workshop->nattachments;
122     $maxbytes       = $workshop->maxbytes;
123     $contentopts    = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
124     $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
125     $submission     = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
126                                         'mod_workshop', 'submission_content', $submission->id);
127     $submission     = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
128                                         'mod_workshop', 'submission_attachment', $submission->id);
130     $mform          = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
131                                                     'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
133     if ($mform->is_cancelled()) {
134         redirect($workshop->view_url());
136     } elseif ($cansubmit and $formdata = $mform->get_data()) {
137         if ($formdata->example == 0) {
138             // this was used just for validation, it must be set to zero when dealing with normal submissions
139             unset($formdata->example);
140         } else {
141             throw new coding_exception('Invalid submission form data value: example');
142         }
143         $timenow = time();
144         if (is_null($submission->id)) {
145             $formdata->workshopid     = $workshop->id;
146             $formdata->example        = 0;
147             $formdata->authorid       = $USER->id;
148             $formdata->timecreated    = $timenow;
149             $formdata->feedbackauthorformat = editors_get_preferred_format();
150         }
151         $formdata->timemodified       = $timenow;
152         $formdata->title              = trim($formdata->title);
153         $formdata->content            = '';          // updated later
154         $formdata->contentformat      = FORMAT_HTML; // updated later
155         $formdata->contenttrust       = 0;           // updated later
156         $formdata->late               = 0x0;         // bit mask
157         if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
158             $formdata->late = $formdata->late | 0x1;
159         }
160         if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
161             $formdata->late = $formdata->late | 0x2;
162         }
163         if (is_null($submission->id)) {
164             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
165             $workshop->log('add submission', $workshop->submission_url($submission->id), $submission->id);
166         } else {
167             $workshop->log('update submission', $workshop->submission_url($submission->id), $submission->id);
168             if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
169                 throw new moodle_exception('err_submissionid', 'workshop');
170             }
171         }
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', $submission->id);
175         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
176                                                            'mod_workshop', 'submission_attachment', $submission->id);
177         if (empty($formdata->attachment)) {
178             // explicit cast to zero integer
179             $formdata->attachment = 0;
180         }
181         // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
182         $DB->update_record('workshop_submissions', $formdata);
183         redirect($workshop->submission_url($formdata->id));
184     }
187 // load the form to override grade and/or publish the submission and process the submitted data eventually
188 if (!$edit and ($canoverride or $canpublish)) {
189     $options = array(
190         'editable' => true,
191         'editablepublished' => $canpublish,
192         'overridablegrade' => $canoverride);
193     $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
194     if ($data = $feedbackform->get_data()) {
195         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
196         $record = new stdclass();
197         $record->id = $submission->id;
198         if ($canoverride) {
199             $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
200             $record->gradeoverby = $USER->id;
201             $record->feedbackauthor = $data->feedbackauthor;
202             $record->feedbackauthorformat = $data->feedbackauthorformat;
203         }
204         if ($canpublish) {
205             $record->published = !empty($data->published);
206         }
207         $DB->update_record('workshop_submissions', $record);
208         redirect($workshop->view_url());
209     }
212 $PAGE->set_title($workshop->name);
213 $PAGE->set_heading($course->fullname);
214 if ($edit) {
215     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
216     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
217 } elseif ($ownsubmission) {
218     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
219 } else {
220     $PAGE->navbar->add(get_string('submission', 'workshop'));
223 // Output starts here
224 $output = $PAGE->get_renderer('mod_workshop');
225 echo $output->header();
226 echo $output->heading(format_string($workshop->name), 2);
228 // show instructions for submitting as thay may contain some list of questions and we need to know them
229 // while reading the submitted answer
230 if (trim($workshop->instructauthors)) {
231     $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
232         'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
233     print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
234     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
235     print_collapsible_region_end();
238 // if in edit mode, display the form to edit the submission
240 if ($edit) {
241     $mform->display();
242     echo $output->footer();
243     die();
246 // else display the submission
248 if ($submission->id) {
249     if ($seenaspublished) {
250         $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
251     } else {
252         $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
253     }
254     echo $output->render($workshop->prepare_submission($submission, $showauthor));
255 } else {
256     echo $output->box(get_string('noyoursubmission', 'workshop'));
259 if ($editable) {
260     if ($submission->id) {
261         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
262         $btntxt = get_string('editsubmission', 'workshop');
263     } else {
264         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
265         $btntxt = get_string('createsubmission', 'workshop');
266     }
267     echo $output->single_button($btnurl, $btntxt, 'get');
270 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
271     $url = new moodle_url($PAGE->url, array('assess' => 1));
272     echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
275 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
276     if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
277         echo $output->render(new workshop_feedback_author($submission));
278     }
281 // and possibly display the submission's review(s)
283 if ($isreviewer) {
284     // user's own assessment
285     $strategy   = $workshop->grading_strategy_instance();
286     $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
287     $options    = array(
288         'showreviewer'  => true,
289         'showauthor'    => $showauthor,
290         'showform'      => !is_null($userassessment->grade),
291         'showweight'    => true,
292     );
293     $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
294     $assessment->title = get_string('assessmentbyyourself', 'workshop');
296     if ($workshop->assessing_allowed($USER->id)) {
297         if (is_null($userassessment->grade)) {
298             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
299         } else {
300             $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
301         }
302     }
303     if ($canoverride) {
304         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
305     }
307     echo $output->render($assessment);
310 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
311     // other assessments
312     $strategy       = $workshop->grading_strategy_instance();
313     $assessments    = $workshop->get_assessments_of_submission($submission->id);
314     $showreviewer   = has_capability('mod/workshop:viewreviewernames', $workshop->context);
315     foreach ($assessments as $assessment) {
316         if ($assessment->reviewerid == $USER->id) {
317             // own assessment has been displayed already
318             continue;
319         }
320         $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
321         $options    = array(
322             'showreviewer'  => $showreviewer,
323             'showauthor'    => $showauthor,
324             'showform'      => !is_null($assessment->grade),
325             'showweight'    => true,
326         );
327         $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
328         if ($canoverride) {
329             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
330         }
331         echo $output->render($assessment);
332     }
335 if (!$edit and $canoverride) {
336     // display a form to override the submission grade
337     $feedbackform->display();
340 echo $output->footer();