1 |
efrain |
1 |
<?php
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* A moodleform to allow the creation and editing of outcome grade items
|
|
|
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 |
|
|
|
25 |
if (!defined('MOODLE_INTERNAL')) {
|
|
|
26 |
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
require_once $CFG->libdir.'/formslib.php';
|
|
|
30 |
|
|
|
31 |
class edit_outcomeitem_form extends moodleform {
|
|
|
32 |
function definition() {
|
|
|
33 |
global $COURSE, $CFG;
|
|
|
34 |
|
|
|
35 |
$mform =& $this->_form;
|
|
|
36 |
|
|
|
37 |
/// visible elements
|
|
|
38 |
$mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades'));
|
|
|
39 |
|
|
|
40 |
$mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
|
|
|
41 |
$mform->addRule('itemname', get_string('required'), 'required', null, 'client');
|
|
|
42 |
$mform->setType('itemname', PARAM_TEXT);
|
|
|
43 |
|
|
|
44 |
$mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
|
|
|
45 |
$mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
|
|
|
46 |
$mform->setType('iteminfo', PARAM_TEXT);
|
|
|
47 |
|
|
|
48 |
$mform->addElement('text', 'idnumber', get_string('idnumbermod'));
|
|
|
49 |
$mform->addHelpButton('idnumber', 'idnumbermod');
|
|
|
50 |
$mform->setType('idnumber', PARAM_RAW);
|
|
|
51 |
|
|
|
52 |
// allow setting of outcomes on module items too
|
|
|
53 |
$options = array();
|
|
|
54 |
if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
|
|
|
55 |
foreach ($outcomes as $outcome) {
|
|
|
56 |
$options[$outcome->id] = $outcome->get_name();
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
$mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null,
|
|
|
60 |
array('link' => $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$COURSE->id, 'label' => get_string('outcomeassigntocourse', 'grades')));
|
|
|
61 |
$mform->addHelpButton('outcomeid', 'outcome', 'grades');
|
|
|
62 |
$mform->addRule('outcomeid', get_string('required'), 'required');
|
|
|
63 |
|
|
|
64 |
$options = array(0=>get_string('none'));
|
|
|
65 |
if ($coursemods = get_course_mods($COURSE->id)) {
|
|
|
66 |
foreach ($coursemods as $coursemod) {
|
|
|
67 |
if ($mod = get_coursemodule_from_id($coursemod->modname, $coursemod->id)) {
|
|
|
68 |
$options[$coursemod->id] = format_string($mod->name);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
$mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options);
|
|
|
73 |
$mform->addHelpButton('cmid', 'linkedactivity', 'grades');
|
|
|
74 |
$mform->setDefault('cmid', 0);
|
|
|
75 |
|
|
|
76 |
/// hiding
|
|
|
77 |
/// advcheckbox is not compatible with disabledIf !!
|
|
|
78 |
$mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
|
|
|
79 |
$mform->addHelpButton('hidden', 'hidden', 'grades');
|
|
|
80 |
$mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
|
|
|
81 |
$mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked');
|
|
|
82 |
|
|
|
83 |
//locking
|
|
|
84 |
$mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
|
|
|
85 |
$mform->addHelpButton('locked', 'locked', 'grades');
|
|
|
86 |
$mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
|
|
|
87 |
|
|
|
88 |
/// parent category related settings
|
|
|
89 |
$mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
|
|
|
90 |
|
|
|
91 |
$mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
|
|
|
92 |
$mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
|
|
|
93 |
|
|
|
94 |
$mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
|
|
|
95 |
$mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
|
|
|
96 |
$mform->setType('aggregationcoef2', PARAM_RAW);
|
|
|
97 |
$mform->disabledIf('aggregationcoef2', 'weightoverride');
|
|
|
98 |
|
|
|
99 |
$options = array();
|
|
|
100 |
$default = '';
|
|
|
101 |
$coefstring = '';
|
|
|
102 |
$categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
|
|
|
103 |
foreach ($categories as $cat) {
|
|
|
104 |
$cat->apply_forced_settings();
|
|
|
105 |
$options[$cat->id] = $cat->get_name();
|
|
|
106 |
if ($cat->is_course_category()) {
|
|
|
107 |
$default = $cat->id;
|
|
|
108 |
}
|
|
|
109 |
if ($cat->is_aggregationcoef_used()) {
|
|
|
110 |
if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
|
|
|
111 |
$coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
|
|
|
112 |
|
|
|
113 |
} else if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
|
|
|
114 |
$coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
|
|
|
115 |
|
|
|
116 |
} else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
|
|
|
117 |
$coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextraweight') ? 'aggregationcoefextraweight' : 'aggregationcoef';
|
|
|
118 |
|
|
|
119 |
} else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
|
|
|
120 |
$coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
|
|
|
121 |
|
|
|
122 |
} else {
|
|
|
123 |
$coefstring = 'aggregationcoef';
|
|
|
124 |
}
|
|
|
125 |
} else {
|
|
|
126 |
$mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if (count($categories) > 1) {
|
|
|
131 |
$mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
|
|
|
132 |
$mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if ($coefstring !== '') {
|
|
|
136 |
if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
|
|
|
137 |
// advcheckbox is not compatible with disabledIf!
|
|
|
138 |
$coefstring = 'aggregationcoefextrasum';
|
|
|
139 |
$mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
|
|
|
140 |
} else {
|
|
|
141 |
$mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
|
|
|
142 |
}
|
|
|
143 |
$mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/// hidden params
|
|
|
147 |
$mform->addElement('hidden', 'id', 0);
|
|
|
148 |
$mform->setType('id', PARAM_INT);
|
|
|
149 |
|
|
|
150 |
$mform->addElement('hidden', 'courseid', $COURSE->id);
|
|
|
151 |
$mform->setType('courseid', PARAM_INT);
|
|
|
152 |
|
|
|
153 |
/// add return tracking info
|
|
|
154 |
$gpr = $this->_customdata['gpr'];
|
|
|
155 |
$gpr->add_mform_elements($mform);
|
|
|
156 |
|
|
|
157 |
//-------------------------------------------------------------------------------
|
|
|
158 |
// buttons
|
|
|
159 |
$this->add_action_buttons();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
/// tweak the form - depending on existing data
|
|
|
164 |
function definition_after_data() {
|
|
|
165 |
global $CFG, $COURSE;
|
|
|
166 |
|
|
|
167 |
$mform =& $this->_form;
|
|
|
168 |
|
|
|
169 |
if ($id = $mform->getElementValue('id')) {
|
|
|
170 |
$grade_item = grade_item::fetch(array('id'=>$id));
|
|
|
171 |
|
|
|
172 |
//remove the aggregation coef element if not needed
|
|
|
173 |
if ($grade_item->is_course_item()) {
|
|
|
174 |
if ($mform->elementExists('parentcategory')) {
|
|
|
175 |
$mform->removeElement('parentcategory');
|
|
|
176 |
}
|
|
|
177 |
if ($mform->elementExists('aggregationcoef')) {
|
|
|
178 |
$mform->removeElement('aggregationcoef');
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
} else {
|
|
|
182 |
// if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
|
|
|
183 |
if ($mform->elementExists('parentcategory')) {
|
|
|
184 |
$mform->hardFreeze('parentcategory');
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
if ($grade_item->is_category_item()) {
|
|
|
188 |
$category = $grade_item->get_item_category();
|
|
|
189 |
$parent_category = $category->get_parent_category();
|
|
|
190 |
} else {
|
|
|
191 |
$parent_category = $grade_item->get_parent_category();
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$parent_category->apply_forced_settings();
|
|
|
195 |
|
|
|
196 |
if (!$parent_category->is_aggregationcoef_used() || !$parent_category->aggregateoutcomes) {
|
|
|
197 |
if ($mform->elementExists('aggregationcoef')) {
|
|
|
198 |
$mform->removeElement('aggregationcoef');
|
|
|
199 |
}
|
|
|
200 |
} else {
|
|
|
201 |
//fix label if needed
|
|
|
202 |
$agg_el =& $mform->getElement('aggregationcoef');
|
|
|
203 |
$aggcoef = '';
|
|
|
204 |
if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
|
|
|
205 |
$aggcoef = 'aggregationcoefweight';
|
|
|
206 |
|
|
|
207 |
} else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
|
|
|
208 |
$aggcoef = 'aggregationcoefextrasum';
|
|
|
209 |
|
|
|
210 |
} else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
|
|
|
211 |
$aggcoef = 'aggregationcoefextraweight';
|
|
|
212 |
|
|
|
213 |
} else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
|
|
|
214 |
$aggcoef = 'aggregationcoefextrasum';
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
if ($aggcoef !== '') {
|
|
|
218 |
$agg_el->setLabel(get_string($aggcoef, 'grades'));
|
|
|
219 |
$mform->addHelpButton('aggregationcoef', $aggcoef, 'grades');
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// Remove the natural weighting fields for other aggregations,
|
|
|
224 |
// or when the category does not aggregate outcomes.
|
|
|
225 |
if ($parent_category->aggregation != GRADE_AGGREGATE_SUM ||
|
|
|
226 |
!$parent_category->aggregateoutcomes) {
|
|
|
227 |
if ($mform->elementExists('weightoverride')) {
|
|
|
228 |
$mform->removeElement('weightoverride');
|
|
|
229 |
}
|
|
|
230 |
if ($mform->elementExists('aggregationcoef2')) {
|
|
|
231 |
$mform->removeElement('aggregationcoef2');
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
// no parent header for course category
|
|
|
239 |
if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
|
|
|
240 |
$mform->removeElement('headerparent');
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
/// perform extra validation before submission
|
|
|
246 |
function validation($data, $files) {
|
|
|
247 |
global $COURSE;
|
|
|
248 |
|
|
|
249 |
$errors = parent::validation($data, $files);
|
|
|
250 |
|
|
|
251 |
if (array_key_exists('idnumber', $data)) {
|
|
|
252 |
if ($data['id']) {
|
|
|
253 |
$grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
|
|
|
254 |
} else {
|
|
|
255 |
$grade_item = null;
|
|
|
256 |
}
|
|
|
257 |
if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
|
|
|
258 |
$errors['idnumber'] = get_string('idnumbertaken');
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
return $errors;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
}
|
|
|
266 |
|