MDL-27550 workshop: assessments are now displayed via proper rendering subsystem
[moodle.git] / mod / workshop / assessment.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  * Assess a submission or view the single assessment
20  *
21  * Assessment id parameter must be passed. The script displays the submission and
22  * the assessment form. If the current user is the reviewer and the assessing is
23  * allowed, new assessment can be saved.
24  * If the assessing is not allowed (for example, the assessment period is over
25  * or the current user is eg a teacher), the assessment form is opened
26  * in a non-editable mode.
27  * The capability 'mod/workshop:peerassess' is intentionally not checked here.
28  * The user is considered as a reviewer if the corresponding assessment record
29  * has been prepared for him/her (during the allocation). So even a user without the
30  * peerassess capability (like a 'teacher', for example) can become a reviewer.
31  *
32  * @package    mod
33  * @subpackage workshop
34  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
38 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
39 require_once(dirname(__FILE__).'/locallib.php');
41 $asid       = required_param('asid', PARAM_INT);  // assessment id
42 $assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
43 $submission = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 0), '*', MUST_EXIST);
44 $workshop   = $DB->get_record('workshop', array('id' => $submission->workshopid), '*', MUST_EXIST);
45 $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
46 $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
48 require_login($course, false, $cm);
49 if (isguestuser()) {
50     print_error('guestsarenotallowed');
51 }
52 $workshop = new workshop($workshop, $cm, $course);
54 $PAGE->set_url($workshop->assess_url($assessment->id));
55 $PAGE->set_title($workshop->name);
56 $PAGE->set_heading($course->fullname);
57 $PAGE->navbar->add(get_string('assessingsubmission', 'workshop'));
59 $canviewallassessments  = has_capability('mod/workshop:viewallassessments', $workshop->context);
60 $canviewallsubmissions  = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
61 $cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
62 $canoverridegrades      = has_capability('mod/workshop:overridegrades', $workshop->context);
63 $isreviewer             = ($USER->id == $assessment->reviewerid);
64 $isauthor               = ($USER->id == $submission->authorid);
66 if ($isreviewer or $isauthor or ($canviewallassessments and $canviewallsubmissions)) {
67     // such a user can continue
68 } else {
69     print_error('nopermissions', 'error', $workshop->view_url(), 'view this assessment');
70 }
72 if ($isauthor and !$isreviewer and !$canviewallassessments and $workshop->phase != workshop::PHASE_CLOSED) {
73     // authors can see assessments of their work at the end of workshop only
74     print_error('nopermissions', 'error', $workshop->view_url(), 'view assessment of own work before workshop is closed');
75 }
77 // only the reviewer is allowed to modify the assessment
78 if ($isreviewer and $workshop->assessing_allowed($USER->id)) {
79     $assessmenteditable = true;
80 } else {
81     $assessmenteditable = false;
82 }
84 // check that all required examples have been assessed by the user
85 if ($assessmenteditable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT
86         and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
87     // the reviewer must have submitted their own submission
88     $reviewersubmission = $workshop->get_submission_by_author($assessment->reviewerid);
89     if (!$reviewersubmission) {
90         // no money, no love
91         $assessmenteditable = false;
92         echo $output->header();
93         echo $output->heading(get_string('exampleneedsubmission', 'workshop'), 2);
94         echo $output->footer();
95         exit;
96     } else {
97         $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
98         foreach ($examples as $exampleid => $example) {
99             if (is_null($example->grade)) {
100                 $assessmenteditable = false;
101                 echo $output->header();
102                 echo $output->heading(get_string('exampleneedassessed', 'workshop'), 2);
103                 echo $output->footer();
104                 exit;
105             }
106         }
107     }
110 // load the grading strategy logic
111 $strategy = $workshop->grading_strategy_instance();
113 if (is_null($assessment->grade) and !$assessmenteditable) {
114     $mform = null;
115 } else {
116     // load the assessment form and process the submitted data eventually
117     $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
118                                         array('editableweight' => $cansetassessmentweight));
119     $mform->set_data(array('weight' => $assessment->weight)); // other values are set by subplugins
120     if ($mform->is_cancelled()) {
121         redirect($workshop->view_url());
122     } elseif ($assessmenteditable and ($data = $mform->get_data())) {
123         if (is_null($assessment->grade)) {
124             $workshop->log('add assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
125         } else {
126             $workshop->log('update assessment', $workshop->assess_url($assessment->id), $assessment->submissionid);
127         }
128         $rawgrade = $strategy->save_assessment($assessment, $data);
129         if (isset($data->weight) and $cansetassessmentweight) {
130             $DB->set_field('workshop_assessments', 'weight', $data->weight, array('id' => $assessment->id));
131         }
132         if (!is_null($rawgrade) and isset($data->saveandclose)) {
133             redirect($workshop->view_url());
134         } else {
135             // either it is not possible to calculate the $rawgrade
136             // or the reviewer has chosen "Save and continue"
137             redirect($PAGE->url);
138         }
139     }
142 // load the form to override gradinggrade and/or set weight and process the submitted data eventually
143 if ($canoverridegrades or $cansetassessmentweight) {
144     $options = array(
145         'editable' => true,
146         'editableweight' => $cansetassessmentweight,
147         'overridablegradinggrade' => $canoverridegrades);
148     $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
149     if ($data = $feedbackform->get_data()) {
150         $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
151         $record = new stdclass();
152         $record->id = $assessment->id;
153         if ($cansetassessmentweight) {
154             $record->weight = $data->weight;
155         }
156         if ($canoverridegrades) {
157             $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
158             $record->gradinggradeoverby = $USER->id;
159             $record->feedbackreviewer = $data->feedbackreviewer;
160             $record->feedbackreviewerformat = $data->feedbackreviewerformat;
161         }
162         $DB->update_record('workshop_assessments', $record);
163         redirect($workshop->view_url());
164     }
167 // output starts here
168 $output = $PAGE->get_renderer('mod_workshop');      // workshop renderer
169 echo $output->header();
170 echo $output->heading(get_string('assessedsubmission', 'workshop'), 2);
172 $submission = $workshop->get_submission_by_id($submission->id);     // reload so can be passed to the renderer
173 echo $output->render($workshop->prepare_submission($submission, has_capability('mod/workshop:viewauthornames', $workshop->context)));
175 // show instructions for assessing as they may contain important information
176 // for evaluating the assessment
177 if (trim($workshop->instructreviewers)) {
178     $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
179         'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
180     print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
181     echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
182     print_collapsible_region_end();
185 // extend the current assessment record with user details
186 $assessment = $workshop->get_assessment_by_id($assessment->id);
188 if ($isreviewer) {
189     $options    = array(
190         'showreviewer'  => true,
191         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
192         'showform'      => $assessmenteditable or !is_null($assessment->grade),
193         'showweight'    => true,
194     );
195     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
196     $assessment->title = get_string('assessmentbyyourself', 'workshop');
197     echo $output->render($assessment);
199 } else {
200     $options    = array(
201         'showreviewer'  => has_capability('mod/workshop:viewreviewernames', $workshop->context),
202         'showauthor'    => has_capability('mod/workshop:viewauthornames', $workshop->context),
203         'showform'      => $assessmenteditable or !is_null($assessment->grade),
204         'showweight'    => true,
205     );
206     $assessment = $workshop->prepare_assessment($assessment, $mform, $options);
207     echo $output->render($assessment);
210 if (!$assessmenteditable and $canoverridegrades) {
211     $feedbackform->display();
214 echo $output->footer();