5930ca768b36c8a147de45761d33eb22e9cb47a1
[moodle.git] / grade / edit / tree / outcomeitem_form.php
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/>.
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  */
25 if (!defined('MOODLE_INTERNAL')) {
26     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
27 }
29 require_once $CFG->libdir.'/formslib.php';
31 class edit_outcomeitem_form extends moodleform {
32     function definition() {
33         global $COURSE, $CFG;
35         $mform =& $this->_form;
37 /// visible elements
38         $mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades'));
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);
44         $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
45         $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
46         $mform->setType('iteminfo', PARAM_TEXT);
48         $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
49         $mform->addHelpButton('idnumber', 'idnumbermod');
50         $mform->setType('idnumber', PARAM_RAW);
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         $url = new moodle_url('/grade/edit/outcome/course.php', array('id' => $COURSE->id));
60         $label = get_string('outcomeassigntocourse', 'grades');
62         $mform->addElement('select', 'outcomeid', get_string('outcome', 'grades'), $options);
63         $mform->addElement('static', 'outcomeidlink', '', html_writer::link($url, $label));
64         $mform->addHelpButton('outcomeid', 'outcome', 'grades');
65         $mform->addRule('outcomeid', get_string('required'), 'required');
67         $options = array(0=>get_string('none'));
68         if ($coursemods = get_course_mods($COURSE->id)) {
69             foreach ($coursemods as $coursemod) {
70                 if ($mod = get_coursemodule_from_id($coursemod->modname, $coursemod->id)) {
71                     $options[$coursemod->id] = format_string($mod->name);
72                 }
73             }
74         }
75         $mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options);
76         $mform->addHelpButton('cmid', 'linkedactivity', 'grades');
77         $mform->setDefault('cmid', 0);
79         /// hiding
80         /// advcheckbox is not compatible with disabledIf !!
81         $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
82         $mform->addHelpButton('hidden', 'hidden', 'grades');
83         $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
84         $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
86         //locking
87         $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
88         $mform->addHelpButton('locked', 'locked', 'grades');
89         $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
91 /// parent category related settings
92         $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
94         $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
95         $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
97         $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
98         $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
99         $mform->setType('aggregationcoef2', PARAM_RAW);
100         $mform->disabledIf('aggregationcoef2', 'weightoverride');
102         $options = array();
103         $default = '';
104         $coefstring = '';
105         $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
106         foreach ($categories as $cat) {
107             $cat->apply_forced_settings();
108             $options[$cat->id] = $cat->get_name();
109             if ($cat->is_course_category()) {
110                 $default = $cat->id;
111             }
112             if ($cat->is_aggregationcoef_used()) {
113                 if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
114                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
116                 } else if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
117                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
119                 } else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
120                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextraweight') ? 'aggregationcoefextraweight' : 'aggregationcoef';
122                 } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
123                     $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
125                 } else {
126                     $coefstring = 'aggregationcoef';
127                 }
128             } else {
129                 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id);
130             }
131         }
133         if (count($categories) > 1) {
134             $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
135             $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
136         }
138         if ($coefstring !== '') {
139             if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
140                 // advcheckbox is not compatible with disabledIf!
141                 $coefstring = 'aggregationcoefextrasum';
142                 $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
143             } else {
144                 $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
145             }
146             $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
147         }
149 /// hidden params
150         $mform->addElement('hidden', 'id', 0);
151         $mform->setType('id', PARAM_INT);
153         $mform->addElement('hidden', 'courseid', $COURSE->id);
154         $mform->setType('courseid', PARAM_INT);
156 /// add return tracking info
157         $gpr = $this->_customdata['gpr'];
158         $gpr->add_mform_elements($mform);
160 /// mark advanced according to site settings
161         if (isset($CFG->grade_item_advanced)) {
162             $advanced = explode(',', $CFG->grade_item_advanced);
163             foreach ($advanced as $el) {
164                 if ($mform->elementExists($el)) {
165                     $mform->setAdvanced($el);
166                 }
167             }
168         }
169 //-------------------------------------------------------------------------------
170         // buttons
171         $this->add_action_buttons();
172     }
175 /// tweak the form - depending on existing data
176     function definition_after_data() {
177         global $CFG, $COURSE;
179         $mform =& $this->_form;
181         if ($id = $mform->getElementValue('id')) {
182             $grade_item = grade_item::fetch(array('id'=>$id));
184             //remove the aggregation coef element if not needed
185             if ($grade_item->is_course_item()) {
186                 if ($mform->elementExists('parentcategory')) {
187                     $mform->removeElement('parentcategory');
188                 }
189                 if ($mform->elementExists('aggregationcoef')) {
190                     $mform->removeElement('aggregationcoef');
191                 }
193             } else {
194                 // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
195                 if ($mform->elementExists('parentcategory')) {
196                     $mform->hardFreeze('parentcategory');
197                 }
199                 if ($grade_item->is_category_item()) {
200                     $category = $grade_item->get_item_category();
201                     $parent_category = $category->get_parent_category();
202                 } else {
203                     $parent_category = $grade_item->get_parent_category();
204                 }
206                 $parent_category->apply_forced_settings();
208                 if (!$parent_category->is_aggregationcoef_used() || !$parent_category->aggregateoutcomes) {
209                     if ($mform->elementExists('aggregationcoef')) {
210                         $mform->removeElement('aggregationcoef');
211                     }
212                 } else {
213                     //fix label if needed
214                     $agg_el =& $mform->getElement('aggregationcoef');
215                     $aggcoef = '';
216                     if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
217                         $aggcoef = 'aggregationcoefweight';
219                     } else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
220                         $aggcoef = 'aggregationcoefextrasum';
222                     } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
223                         $aggcoef = 'aggregationcoefextraweight';
225                     } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
226                         $aggcoef = 'aggregationcoefextrasum';
227                     }
229                     if ($aggcoef !== '') {
230                         $agg_el->setLabel(get_string($aggcoef, 'grades'));
231                         $mform->addHelpButton('aggregationcoef', $aggcoef, 'grades');
232                     }
233                 }
235                 // Remove the natural weighting fields for other aggregations,
236                 // or when the category does not aggregate outcomes.
237                 if ($parent_category->aggregation != GRADE_AGGREGATE_SUM ||
238                         !$parent_category->aggregateoutcomes) {
239                     if ($mform->elementExists('weightoverride')) {
240                         $mform->removeElement('weightoverride');
241                     }
242                     if ($mform->elementExists('aggregationcoef2')) {
243                         $mform->removeElement('aggregationcoef2');
244                     }
245                 }
246             }
248         }
250         // no parent header for course category
251         if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
252             $mform->removeElement('headerparent');
253         }
254     }
257 /// perform extra validation before submission
258     function validation($data, $files) {
259         global $COURSE;
261         $errors = parent::validation($data, $files);
263         if (array_key_exists('idnumber', $data)) {
264             if ($data['id']) {
265                 $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
266             } else {
267                 $grade_item = null;
268             }
269             if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
270                 $errors['idnumber'] = get_string('idnumbertaken');
271             }
272         }
274         return $errors;
275     }