aee2e4179d2904fbd836471ec7b9e600e46a7fe5
[moodle.git] / mod / workshop / exassessment.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 an 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 $asid       = required_param('asid', PARAM_INT);  // assessment id
31 $assessment = $DB->get_record('workshop_assessments', array('id' => $asid), '*', MUST_EXIST);
32 $example    = $DB->get_record('workshop_submissions', array('id' => $assessment->submissionid, 'example' => 1), '*', MUST_EXIST);
33 $workshop   = $DB->get_record('workshop', array('id' => $example->workshopid), '*', MUST_EXIST);
34 $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
35 $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
37 require_login($course, false, $cm);
38 if (isguestuser()) {
39     print_error('guestsarenotallowed');
40 }
41 $workshop = new workshop($workshop, $cm, $course);
43 $PAGE->set_url($workshop->exassess_url($assessment->id));
44 $PAGE->set_title($workshop->name);
45 $PAGE->set_heading($course->fullname);
46 $PAGE->navbar->add(get_string('assessingexample', 'workshop'));
47 $currenttab = 'assessment';
49 $canmanage  = has_capability('mod/workshop:manageexamples', $workshop->context);
50 $isreviewer = ($USER->id == $assessment->reviewerid);
52 if ($isreviewer or $canmanage) {
53     // such a user can continue
54 } else {
55     print_error('nopermissions', 'error', $workshop->view_url(), 'assess example submission');
56 }
58 // only the reviewer is allowed to modify the assessment
59 if ($canmanage or ($isreviewer and $workshop->assessing_examples_allowed())) {
60     $assessmenteditable = true;
61 } else {
62     $assessmenteditable = false;
63 }
65 // load the grading strategy logic
66 $strategy = $workshop->grading_strategy_instance();
68 // load the assessment form and process the submitted data eventually
69 $mform = $strategy->get_assessment_form($PAGE->url, 'assessment', $assessment, $assessmenteditable);
70 if ($mform->is_cancelled()) {
71     redirect($workshop->view_url());
72 } elseif ($assessmenteditable and ($data = $mform->get_data())) {
73     if ($canmanage) {
74         if (is_null($assessment->grade)) {
75             $workshop->log('add reference assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
76         } else {
77             $workshop->log('update reference assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
78         }
79     } else {
80         if (is_null($assessment->grade)) {
81             $workshop->log('add example assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
82         } else {
83             $workshop->log('update example assessment', $workshop->exassess_url($assessment->id), $assessment->submissionid);
84         }
85     }
86     $rawgrade = $strategy->save_assessment($assessment, $data);
87     if ($canmanage) {
88         // remember the last one who edited the reference assessment
89         $DB->set_field('workshop_assessments', 'reviewerid', $USER->id, array('id' => $assessment->id));
90     }
91     if (!is_null($rawgrade) and isset($data->saveandclose)) {
92         if ($canmanage) {
93             redirect($workshop->view_url());
94         } else {
95             redirect($workshop->excompare_url($example->id, $assessment->id));
96         }
97     } else {
98         // either it is not possible to calculate the $rawgrade
99         // or the reviewer has chosen "Save and continue"
100         redirect($PAGE->url);
101     }
104 // output starts here
105 $output = $PAGE->get_renderer('mod_workshop');      // workshop renderer
106 echo $output->header();
107 echo $output->heading(get_string('assessedexample', 'workshop'), 2);
109 $example = $workshop->get_example_by_id($example->id);     // reload so can be passed to the renderer
110 echo $output->render($workshop->prepare_example_submission(($example)));
112 // show instructions for assessing as thay may contain important information
113 // for evaluating the assessment
114 if (trim($workshop->instructreviewers)) {
115     $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
116         'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
117     print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
118     echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
119     print_collapsible_region_end();
122 if ($canmanage) {
123     echo $output->heading(get_string('assessmentreference', 'workshop'), 2);
124 } elseif ($isreviewer) {
125     echo $output->heading(get_string('assessmentbyyourself', 'workshop'), 2);
126 } else {
127     $assessment = $workshop->get_assessment_by_id($assessment->id); // extend the current record with user details
128     $reviewer   = new stdclass();
129     $reviewer->firstname = $assessment->reviewerfirstname;
130     $reviewer->lastname = $assessment->reviewerlastname;
131     echo $output->heading(get_string('assessmentbyknown', 'workshop', fullname($reviewer)), 2);
134 $mform->display();
135 echo $output->footer();