MDL-21432 backup - complete standard/course outcomes backup & restore
[moodle.git] / mod / workshop / assessment.php
CommitLineData
0968b1a3 1<?php
53fad4b9
DM
2
3// This file is part of Moodle - http://moodle.org/
4//
0968b1a3
DM
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.
53fad4b9 14//
0968b1a3
DM
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/>.
53fad4b9 17
0968b1a3 18/**
407b1e91 19 * Assess a submission or view the single assessment
0968b1a3 20 *
407b1e91
DM
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.
0968b1a3
DM
31 *
32 * @package mod-workshop
33 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36
37require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
0968b1a3
DM
38require_once(dirname(__FILE__).'/locallib.php');
39
407b1e91
DM
40$asid = required_param('asid', PARAM_INT); // assessment id
41$assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
becec954 42$submission = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 0), '*', MUST_EXIST);
407b1e91
DM
43$workshop = $DB->get_record('workshop', array('id' => $submission->workshopid), '*', MUST_EXIST);
44$course = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
45$cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
0968b1a3
DM
46
47require_login($course, false, $cm);
a39d7d87
DM
48if (isguestuser()) {
49 print_error('guestsarenotallowed');
50}
51$workshop = new workshop($workshop, $cm, $course);
6e309973 52
407b1e91
DM
53$PAGE->set_url($workshop->assess_url($assessment->id));
54$PAGE->set_title($workshop->name);
55$PAGE->set_heading($course->fullname);
56$PAGE->navbar->add(get_string('assessingsubmission', 'workshop'));
407b1e91
DM
57
58$canviewallassessments = has_capability('mod/workshop:viewallassessments', $workshop->context);
59$canviewallsubmissions = has_capability('mod/workshop:viewallsubmissions', $workshop->context);
c6b784f0
DM
60$cansetassessmentweight = has_capability('mod/workshop:allocate', $workshop->context);
61$canoverridegrades = has_capability('mod/workshop:overridegrades', $workshop->context);
407b1e91
DM
62$isreviewer = ($USER->id == $assessment->reviewerid);
63$isauthor = ($USER->id == $submission->authorid);
64
65if ($isreviewer or $isauthor or ($canviewallassessments and $canviewallsubmissions)) {
66 // such a user can continue
67} else {
68 print_error('nopermissions', '', $workshop->view_url());
69}
70
71// only the reviewer is allowed to modify the assessment
72if ($isreviewer and $workshop->assessing_allowed()) {
77f43e7d 73 $assessmenteditable = true;
407b1e91 74} else {
77f43e7d 75 $assessmenteditable = false;
0968b1a3 76}
0968b1a3 77
cff28ef0
DM
78// check that all required examples have been assessed by the user
79if ($assessmenteditable and $workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT
80 and !has_capability('mod/workshop:manageexamples', $workshop->context)) {
81 // the reviewer must have submitted their own submission
82 $reviewersubmission = $workshop->get_submission_by_author($assessment->reviewerid);
83 if (!$reviewersubmission) {
84 // no money, no love
85 $assessmenteditable = false;
86 echo $OUTPUT->header();
87 echo $OUTPUT->heading(get_string('exampleneedsubmission', 'workshop'), 2);
88 echo $OUTPUT->footer();
89 exit;
90 } else {
91 $examples = $workshop->get_examples_for_reviewer($assessment->reviewerid);
92 foreach ($examples as $exampleid => $example) {
93 if (is_null($example->grade)) {
94 $assessmenteditable = false;
95 echo $OUTPUT->header();
96 echo $OUTPUT->heading(get_string('exampleneedassessed', 'workshop'), 2);
97 echo $OUTPUT->footer();
98 exit;
99 }
100 }
101 }
102}
103
05e69e56
DM
104// load the grading strategy logic
105$strategy = $workshop->grading_strategy_instance();
106
c6b784f0
DM
107if (is_null($assessment->grade) and !$assessmenteditable) {
108 $mform = null;
109} else {
110 // load the assessment form and process the submitted data eventually
111 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable,
112 array('editableweight' => $cansetassessmentweight));
113 $mform->set_data(array('weight' => $assessment->weight)); // other values are set by subplugins
114 if ($mform->is_cancelled()) {
407b1e91 115 redirect($workshop->view_url());
c6b784f0
DM
116 } elseif ($assessmenteditable and ($data = $mform->get_data())) {
117 $rawgrade = $strategy->save_assessment($assessment, $data);
118 if (isset($data->weight) and $cansetassessmentweight) {
119 $DB->set_field('workshop_assessments', 'weight', $data->weight, array('id' => $assessment->id));
120 }
121 if (!is_null($rawgrade) and isset($data->saveandclose)) {
122 redirect($workshop->view_url());
123 } else {
124 // either it is not possible to calculate the $rawgrade
125 // or the reviewer has chosen "Save and continue"
126 redirect($PAGE->url);
127 }
05e69e56
DM
128 }
129}
130
c6b784f0
DM
131// load the form to override gradinggrade and/or set weight and process the submitted data eventually
132if ($canoverridegrades or $cansetassessmentweight) {
133 $options = array(
134 'editable' => true,
135 'editableweight' => $cansetassessmentweight,
136 'overridablegradinggrade' => $canoverridegrades);
137 $feedbackform = $workshop->get_feedbackreviewer_form($PAGE->url, $assessment, $options);
77f43e7d 138 if ($data = $feedbackform->get_data()) {
e554671d 139 $data = file_postupdate_standard_editor($data, 'feedbackreviewer', array(), $workshop->context);
7a789aa8 140 $record = new stdclass();
e554671d 141 $record->id = $assessment->id;
c6b784f0
DM
142 if ($cansetassessmentweight) {
143 $record->weight = $data->weight;
144 }
145 if ($canoverridegrades) {
146 $record->gradinggradeover = $workshop->raw_grade_value($data->gradinggradeover, $workshop->gradinggrade);
147 $record->gradinggradeoverby = $USER->id;
148 $record->feedbackreviewer = $data->feedbackreviewer;
149 $record->feedbackreviewerformat = $data->feedbackreviewerformat;
150 }
e554671d
DM
151 $DB->update_record('workshop_assessments', $record);
152 redirect($workshop->view_url());
77f43e7d
DM
153 }
154}
155
407b1e91 156// output starts here
39861053 157echo $OUTPUT->header();
77f43e7d 158echo $OUTPUT->heading(get_string('assessedsubmission', 'workshop'), 2);
a39d7d87 159
6adbcb80 160$wsoutput = $PAGE->get_renderer('mod_workshop'); // workshop renderer
407b1e91
DM
161$submission = $workshop->get_submission_by_id($submission->id); // reload so can be passed to the renderer
162echo $wsoutput->submission_full($submission, has_capability('mod/workshop:viewauthornames', $workshop->context));
a39d7d87 163
77f43e7d
DM
164if ($isreviewer) {
165 echo $OUTPUT->heading(get_string('assessmentbyyourself', 'workshop'), 2);
166} elseif (has_capability('mod/workshop:viewreviewernames', $workshop->context)) {
167 $assessment = $workshop->get_assessment_by_id($assessment->id); // extend the current record with user details
7a789aa8 168 $reviewer = new stdclass();
77f43e7d
DM
169 $reviewer->firstname = $assessment->reviewerfirstname;
170 $reviewer->lastname = $assessment->reviewerlastname;
171 echo $OUTPUT->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
172} else {
173 echo $OUTPUT->heading(get_string('assessmentbyunknown', 'workshop'), 2);
174}
77f43e7d 175
c6b784f0
DM
176if ($mform) {
177 $mform->display();
178} else {
179 echo $OUTPUT->heading(get_string('notassessed', 'workshop'));
180}
77f43e7d
DM
181if ($canoverridegrades) {
182 $feedbackform->display();
77f43e7d 183}
d84d06ce 184echo $OUTPUT->footer();