Merge branch 'MDL-26523_grader' of git://github.com/andyjdavis/moodle
[moodle.git] / grade / edit / tree / grade_form.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8ad36f4c 16
a153c9f2
AD
17/**
18 * A moodleform to allow the editing of a user's grade for a particular activity
19 *
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
bfebaf64
MD
25if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
20544755 29require_once $CFG->libdir.'/formslib.php';
30
31class edit_grade_form extends moodleform {
23207a1a 32
20544755 33 function definition() {
5c75a0a3 34 global $CFG, $COURSE, $DB;
9c25957a 35
20544755 36 $mform =& $this->_form;
920a0fb2 37
23207a1a 38 $grade_item = $this->_customdata['grade_item'];
39 $gpr = $this->_customdata['gpr'];
40
d714732d 41 if ($grade_item->is_course_item()) {
42 $grade_category = null;
43 } else if ($grade_item->is_category_item()) {
44 $grade_category = $grade_item->get_item_category();
45 $grade_category = $grade_category->get_parent_category();
46 } else {
47 $grade_category = $grade_item->get_parent_category();
48 }
49
23207a1a 50 /// information fields
51 $mform->addElement('static', 'user', get_string('user'));
52 $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
920a0fb2 53
0f392ff4 54 $mform->addElement('checkbox', 'overridden', get_string('overridden', 'grades'));
81ff6f1c 55 $mform->addHelpButton('overridden', 'overridden', 'grades');
0f392ff4 56
23207a1a 57 /// actual grade - numeric or scale
58 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
20544755 59 // numeric grade
60 $mform->addElement('text', 'finalgrade', get_string('finalgrade', 'grades'));
f960faf8 61 $mform->addHelpButton('finalgrade', 'finalgrade', 'grades');
0f392ff4 62 $mform->disabledIf('finalgrade', 'overridden', 'notchecked');
79eabc2a 63
23207a1a 64 } else if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
20544755 65 // scale grade
23207a1a 66 $scaleopt = array();
0658afc9 67
68 if (empty($grade_item->outcomeid)) {
4d910cad 69 $scaleopt[-1] = get_string('nograde');
0658afc9 70 } else {
4d910cad 71 $scaleopt[-1] = get_string('nooutcome', 'grades');
0658afc9 72 }
920a0fb2 73
7e3c9767 74 $i = 1;
5c75a0a3 75 if ($scale = $DB->get_record('scale', array('id' => $grade_item->scaleid))) {
c71f3265 76 foreach (explode(",", $scale->scale) as $option) {
7e3c9767 77 $scaleopt[$i] = $option;
78 $i++;
20544755 79 }
80 }
920a0fb2 81
20544755 82 $mform->addElement('select', 'finalgrade', get_string('finalgrade', 'grades'), $scaleopt);
692e4f7b 83 $mform->addHelpButton('finalgrade', 'finalgrade', 'grades');
0f392ff4 84 $mform->disabledIf('finalgrade', 'overridden', 'notchecked');
20544755 85 }
920a0fb2 86
d714732d 87 if ($grade_category and $grade_category->aggregation == GRADE_AGGREGATE_SUM) {
88 $mform->addElement('advcheckbox', 'excluded', get_string('excluded', 'grades'), '<small>('.get_string('warningexcludedsum', 'grades').')</small>');
89 } else {
90 $mform->addElement('advcheckbox', 'excluded', get_string('excluded', 'grades'));
91 }
97fecb21 92 $mform->addHelpButton('excluded', 'excluded', 'grades');
920a0fb2 93
15a51159 94 /// hiding
95 /// advcheckbox is not compatible with disabledIf !!
96 $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
d7a04343 97 $mform->addHelpButton('hidden', 'hidden', 'grades');
d3c3da1b 98 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
f60c61b1 99 $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
15a51159 100
23207a1a 101 /// locking
9c25957a 102 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
731bc4cc 103 $mform->addHelpButton('locked', 'locked', 'grades');
20544755 104 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
105 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
20544755 106
79eabc2a 107 // Feedback format is automatically converted to html if user has enabled editor
64f93798 108 $feedbackoptions = array('maxfiles'=>0, 'maxbytes'=>0); //TODO: no files here for now, if ever gets implemented use component 'grade' and filearea 'feedback'
8bdc9cac 109 $mform->addElement('editor', 'feedback', get_string('feedback', 'grades'), null, $feedbackoptions);
58c04512 110 $mform->addHelpButton('feedback', 'feedback', 'grades');
23207a1a 111 $mform->setType('text', PARAM_RAW); // to be cleaned before display, no XSS risk
20544755 112
113 // hidden params
76317c73 114 $mform->addElement('hidden', 'oldgrade');
d18e0fe6 115 $mform->setType('oldgrade', PARAM_RAW);
0f392ff4 116 $mform->addElement('hidden', 'oldfeedback');
d18e0fe6 117 $mform->setType('oldfeedback', PARAM_RAW);
76317c73 118
20544755 119 $mform->addElement('hidden', 'id', 0);
23207a1a 120 $mform->setType('id', PARAM_INT);
121
122 $mform->addElement('hidden', 'itemid', 0);
123 $mform->setType('itemid', PARAM_INT);
20544755 124
23207a1a 125 $mform->addElement('hidden', 'userid', 0);
126 $mform->setType('userid', PARAM_INT);
127
128 $mform->addElement('hidden', 'courseid', $COURSE->id);
20544755 129 $mform->setType('courseid', PARAM_INT);
130
3af29899 131/// add return tracking info
3af29899 132 $gpr->add_mform_elements($mform);
133
20544755 134//-------------------------------------------------------------------------------
135 // buttons
136 $this->add_action_buttons();
137 }
23207a1a 138
139 function definition_after_data() {
5c75a0a3 140 global $CFG, $COURSE, $DB;
27b1735b 141
142 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
23207a1a 143
144 $mform =& $this->_form;
145 $grade_item = $this->_customdata['grade_item'];
146
27b1735b 147 // fill in user name if user still exists
148 $userid = $mform->getElementValue('userid');
5c75a0a3 149 if ($user = $DB->get_record('user', array('id' => $userid))) {
23207a1a 150 $username = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'">'.fullname($user).'</a>';
151 $user_el =& $mform->getElement('user');
152 $user_el->setValue($username);
153 }
154
27b1735b 155 // add activity name + link
23207a1a 156 if ($grade_item->itemtype == 'mod') {
157 $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
158 $itemname = '<a href="'.$CFG->wwwroot.'/mod/'.$grade_item->itemmodule.'/view.php?id='.$cm->id.'">'.$grade_item->get_name().'</a>';
159 } else {
160 $itemname = $grade_item->get_name();
161 }
162 $itemname_el =& $mform->getElement('itemname');
163 $itemname_el->setValue($itemname);
27b1735b 164
165 // access control - disable not allowed elements
166 if (!has_capability('moodle/grade:manage', $context)) {
167 $mform->hardFreeze('excluded');
168 }
169
170 if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
171 $mform->hardFreeze('hidden');
172 $mform->hardFreeze('hiddenuntil');
173 }
174
175 $old_grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid));
27b1735b 176
0f392ff4 177 if (!$grade_item->is_overridable_item()) {
178 $mform->removeElement('overridden');
179 }
180
f60c61b1 181 if ($grade_item->is_hidden()) {
182 $mform->hardFreeze('hidden');
183 }
184
fb0e3570 185 if ($old_grade_grade->is_locked()) {
186 if ($grade_item->is_locked()) {
187 $mform->hardFreeze('locked');
188 $mform->hardFreeze('locktime');
189 }
190
191 $mform->hardFreeze('overridden');
192 $mform->hardFreeze('finalgrade');
193 $mform->hardFreeze('feedback');
194
195 } else {
196 if (empty($old_grade_grade->id)) {
197 $old_grade_grade->locked = $grade_item->locked;
198 $old_grade_grade->locktime = $grade_item->locktime;
199 }
27b1735b 200
fb0e3570 201 if (($old_grade_grade->locked or $old_grade_grade->locktime)
202 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context))) {
203 $mform->hardFreeze('locked');
204 $mform->hardFreeze('locktime');
205
206 } else if ((!$old_grade_grade->locked and !$old_grade_grade->locktime)
207 and (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context))) {
208 $mform->hardFreeze('locked');
209 $mform->hardFreeze('locktime');
210 }
27b1735b 211 }
23207a1a 212 }
20544755 213}
214
6c3ef410 215