MDL-27550 workshop: rendering links to the submission and the assessment page
[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));
48 if ($edit) {
49     $PAGE->url->param('edit', $edit);
50 }
52 if ($id) { // submission is specified
53     $submission = $workshop->get_submission_by_id($id);
54     $workshop->log('view submission', $workshop->submission_url($submission->id), $submission->id);
56 } else { // no submission specified
57     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
58         $submission = new stdclass();
59         $submission->id = null;
60         $submission->authorid = $USER->id;
61         $submission->example = 0;
62         $submission->grade = null;
63         $submission->gradeover = null;
64         $submission->published = null;
65         $submission->feedbackauthor = null;
66         $submission->feedbackauthorformat = editors_get_preferred_format();
67     }
68 }
70 $ownsubmission  = $submission->authorid == $USER->id;
71 $canviewall     = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
72 $cansubmit      = has_capability('mod/workshop:submit', $workshop->context);
73 $canallocate    = has_capability('mod/workshop:allocate', $workshop->context);
74 $canpublish     = has_capability('mod/workshop:publishsubmissions', $workshop->context);
75 $canoverride    = (($workshop->phase == workshop::PHASE_EVALUATION) and has_capability('mod/workshop:overridegrades', $workshop->context));
76 $userassessment = $workshop->get_assessment_of_submission_by_user($submission->id, $USER->id);
77 $isreviewer     = !empty($userassessment);
78 $editable       = ($cansubmit and $ownsubmission);
79 $ispublished    = ($workshop->phase == workshop::PHASE_CLOSED
80                     and $submission->published == 1
81                     and has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context));
83 if (empty($submission->id) and !$workshop->creating_submission_allowed($USER->id)) {
84     $editable = false;
85 }
86 if ($submission->id and !$workshop->modifying_submission_allowed($USER->id)) {
87     $editable = false;
88 }
90 if ($editable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION
91         and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
92     // check that all required examples have been assessed by the user
93     $examples = $workshop->get_examples_for_reviewer($USER->id);
94     foreach ($examples as $exampleid => $example) {
95         if (is_null($example->grade)) {
96             $editable = false;
97             break;
98         }
99     }
101 $edit = ($editable and $edit);
103 $seenaspublished = false; // is the submission seen as a published submission?
105 if ($submission->id and ($ownsubmission or $canviewall or $isreviewer)) {
106     // ok you can go
107 } elseif ($submission->id and $ispublished) {
108     // ok you can go
109     $seenaspublished = true;
110 } elseif (is_null($submission->id) and $cansubmit) {
111     // ok you can go
112 } else {
113     print_error('nopermissions', 'error', $workshop->view_url(), 'view or create submission');
116 if ($assess and $submission->id and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
117     require_sesskey();
118     $assessmentid = $workshop->add_allocation($submission, $USER->id);
119     redirect($workshop->assess_url($assessmentid));
122 if ($edit) {
123     require_once(dirname(__FILE__).'/submission_form.php');
125     $maxfiles       = $workshop->nattachments;
126     $maxbytes       = $workshop->maxbytes;
127     $contentopts    = array('trusttext' => true, 'subdirs' => false, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
128     $attachmentopts = array('subdirs' => true, 'maxfiles' => $maxfiles, 'maxbytes' => $maxbytes);
129     $submission     = file_prepare_standard_editor($submission, 'content', $contentopts, $workshop->context,
130                                         'mod_workshop', 'submission_content', $submission->id);
131     $submission     = file_prepare_standard_filemanager($submission, 'attachment', $attachmentopts, $workshop->context,
132                                         'mod_workshop', 'submission_attachment', $submission->id);
134     $mform          = new workshop_submission_form($PAGE->url, array('current' => $submission, 'workshop' => $workshop,
135                                                     'contentopts' => $contentopts, 'attachmentopts' => $attachmentopts));
137     if ($mform->is_cancelled()) {
138         redirect($workshop->view_url());
140     } elseif ($cansubmit and $formdata = $mform->get_data()) {
141         if ($formdata->example == 0) {
142             // this was used just for validation, it must be set to zero when dealing with normal submissions
143             unset($formdata->example);
144         } else {
145             throw new coding_exception('Invalid submission form data value: example');
146         }
147         $timenow = time();
148         if (is_null($submission->id)) {
149             $formdata->workshopid     = $workshop->id;
150             $formdata->example        = 0;
151             $formdata->authorid       = $USER->id;
152             $formdata->timecreated    = $timenow;
153             $formdata->feedbackauthorformat = editors_get_preferred_format();
154         }
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         $formdata->late               = 0x0;         // bit mask
161         if (!empty($workshop->submissionend) and ($workshop->submissionend < time())) {
162             $formdata->late = $formdata->late | 0x1;
163         }
164         if ($workshop->phase == workshop::PHASE_ASSESSMENT) {
165             $formdata->late = $formdata->late | 0x2;
166         }
167         if (is_null($submission->id)) {
168             $submission->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
169             $workshop->log('add submission', $workshop->submission_url($submission->id), $submission->id);
170         } else {
171             $workshop->log('update submission', $workshop->submission_url($submission->id), $submission->id);
172             if (empty($formdata->id) or empty($submission->id) or ($formdata->id != $submission->id)) {
173                 throw new moodle_exception('err_submissionid', 'workshop');
174             }
175         }
176         // save and relink embedded images and save attachments
177         $formdata = file_postupdate_standard_editor($formdata, 'content', $contentopts, $workshop->context,
178                                                       'mod_workshop', 'submission_content', $submission->id);
179         $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $attachmentopts, $workshop->context,
180                                                            'mod_workshop', 'submission_attachment', $submission->id);
181         if (empty($formdata->attachment)) {
182             // explicit cast to zero integer
183             $formdata->attachment = 0;
184         }
185         // store the updated values or re-save the new submission (re-saving needed because URLs are now rewritten)
186         $DB->update_record('workshop_submissions', $formdata);
187         redirect($workshop->submission_url($formdata->id));
188     }
191 // load the form to override grade and/or publish the submission and process the submitted data eventually
192 if (!$edit and ($canoverride or $canpublish)) {
193     $options = array(
194         'editable' => true,
195         'editablepublished' => $canpublish,
196         'overridablegrade' => $canoverride);
197     $feedbackform = $workshop->get_feedbackauthor_form($PAGE->url, $submission, $options);
198     if ($data = $feedbackform->get_data()) {
199         $data = file_postupdate_standard_editor($data, 'feedbackauthor', array(), $workshop->context);
200         $record = new stdclass();
201         $record->id = $submission->id;
202         if ($canoverride) {
203             $record->gradeover = $workshop->raw_grade_value($data->gradeover, $workshop->grade);
204             $record->gradeoverby = $USER->id;
205             $record->feedbackauthor = $data->feedbackauthor;
206             $record->feedbackauthorformat = $data->feedbackauthorformat;
207         }
208         if ($canpublish) {
209             $record->published = !empty($data->published);
210         }
211         $DB->update_record('workshop_submissions', $record);
212         redirect($workshop->view_url());
213     }
216 $PAGE->set_title($workshop->name);
217 $PAGE->set_heading($course->fullname);
218 if ($edit) {
219     $PAGE->navbar->add(get_string('mysubmission', 'workshop'), $workshop->submission_url(), navigation_node::TYPE_CUSTOM);
220     $PAGE->navbar->add(get_string('editingsubmission', 'workshop'));
221 } elseif ($ownsubmission) {
222     $PAGE->navbar->add(get_string('mysubmission', 'workshop'));
223 } else {
224     $PAGE->navbar->add(get_string('submission', 'workshop'));
227 // Output starts here
228 $output = $PAGE->get_renderer('mod_workshop');
229 echo $output->header();
230 echo $output->heading(format_string($workshop->name), 2);
232 // show instructions for submitting as thay may contain some list of questions and we need to know them
233 // while reading the submitted answer
234 if (trim($workshop->instructauthors)) {
235     $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
236         'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
237     print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
238     echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
239     print_collapsible_region_end();
242 // if in edit mode, display the form to edit the submission
244 if ($edit) {
245     $mform->display();
246     echo $output->footer();
247     die();
250 // else display the submission
252 if ($submission->id) {
253     if ($seenaspublished) {
254         $showauthor = has_capability('mod/workshop:viewauthorpublished', $workshop->context);
255     } else {
256         $showauthor = has_capability('mod/workshop:viewauthornames', $workshop->context);
257     }
258     echo $output->render($workshop->prepare_submission($submission, $showauthor));
259 } else {
260     echo $output->box(get_string('noyoursubmission', 'workshop'));
263 if ($editable) {
264     if ($submission->id) {
265         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on', 'id' => $submission->id));
266         $btntxt = get_string('editsubmission', 'workshop');
267     } else {
268         $btnurl = new moodle_url($PAGE->url, array('edit' => 'on'));
269         $btntxt = get_string('createsubmission', 'workshop');
270     }
271     echo $output->single_button($btnurl, $btntxt, 'get');
274 if ($submission->id and !$edit and !$isreviewer and $canallocate and $workshop->assessing_allowed($USER->id)) {
275     $url = new moodle_url($PAGE->url, array('assess' => 1));
276     echo $output->single_button($url, get_string('assess', 'workshop'), 'post');
279 if (($workshop->phase == workshop::PHASE_CLOSED) and ($ownsubmission or $canviewall)) {
280     if (!empty($submission->gradeoverby) and strlen(trim($submission->feedbackauthor)) > 0) {
281         echo $output->render(new workshop_feedback_author($submission));
282     }
285 // and possibly display the submission's review(s)
287 if ($isreviewer) {
288     // user's own assessment
289     $strategy   = $workshop->grading_strategy_instance();
290     $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $userassessment, false);
291     $options    = array(
292         'showreviewer'  => true,
293         'showauthor'    => $showauthor,
294         'showform'      => !is_null($userassessment->grade),
295         'showweight'    => true,
296     );
297     $assessment = $workshop->prepare_assessment($userassessment, $mform, $options);
298     $assessment->title = get_string('assessmentbyyourself', 'workshop');
300     if ($workshop->assessing_allowed($USER->id)) {
301         if (is_null($userassessment->grade)) {
302             $assessment->add_action($workshop->assess_url($assessment->id), get_string('assess', 'workshop'));
303         } else {
304             $assessment->add_action($workshop->assess_url($assessment->id), get_string('reassess', 'workshop'));
305         }
306     }
307     if ($canoverride) {
308         $assessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
309     }
311     echo $output->render($assessment);
313     if ($workshop->phase == workshop::PHASE_CLOSED) {
314         if (strlen(trim($userassessment->feedbackreviewer)) > 0) {
315             echo $output->render(new workshop_feedback_reviewer($userassessment));
316         }
317     }
320 if (has_capability('mod/workshop:viewallassessments', $workshop->context) or ($ownsubmission and $workshop->assessments_available())) {
321     // other assessments
322     $strategy       = $workshop->grading_strategy_instance();
323     $assessments    = $workshop->get_assessments_of_submission($submission->id);
324     $showreviewer   = has_capability('mod/workshop:viewreviewernames', $workshop->context);
325     foreach ($assessments as $assessment) {
326         if ($assessment->reviewerid == $USER->id) {
327             // own assessment has been displayed already
328             continue;
329         }
330         if (is_null($assessment->grade) and !has_capability('mod/workshop:viewallassessments', $workshop->context)) {
331             // students do not see peer-assessment that are not graded yet
332             continue;
333         }
334         $mform      = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, false);
335         $options    = array(
336             'showreviewer'  => $showreviewer,
337             'showauthor'    => $showauthor,
338             'showform'      => !is_null($assessment->grade),
339             'showweight'    => true,
340         );
341         $displayassessment = $workshop->prepare_assessment($assessment, $mform, $options);
342         if ($canoverride) {
343             $displayassessment->add_action($workshop->assess_url($assessment->id), get_string('assessmentsettings', 'workshop'));
344         }
345         echo $output->render($displayassessment);
347         if ($workshop->phase == workshop::PHASE_CLOSED and has_capability('mod/workshop:viewallassessments', $workshop->context)) {
348             if (strlen(trim($assessment->feedbackreviewer)) > 0) {
349                 echo $output->render(new workshop_feedback_reviewer($assessment));
350             }
351         }
352     }
355 if (!$edit and $canoverride) {
356     // display a form to override the submission grade
357     $feedbackform->display();
360 echo $output->footer();