3 // This file is part of Moodle - http://moodle.org/
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.
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.
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 if (!defined('MOODLE_INTERNAL')) {
19 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
22 require_once $CFG->libdir
.'/formslib.php';
24 class edit_outcomeitem_form
extends moodleform
{
25 function definition() {
28 $mform =& $this->_form
;
31 $mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades'));
33 $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
34 $mform->addRule('itemname', get_string('required'), 'required', null, 'client');
36 $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
37 $mform->setHelpButton('iteminfo', array('iteminfo', get_string('iteminfo', 'grades'), 'grade'), true);
39 $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
40 $mform->setHelpButton('idnumber', array('idnumber', get_string('idnumber', 'grades'), 'grade'), true);
42 // allow setting of outcomes on module items too
44 if ($outcomes = grade_outcome
::fetch_all_available($COURSE->id
)) {
45 foreach ($outcomes as $outcome) {
46 $options[$outcome->id
] = $outcome->get_name();
49 $mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null,
50 array('link' => $CFG->wwwroot
.'/grade/edit/outcome/course.php?id='.$COURSE->id
, 'label' => get_string('outcomeassigntocourse', 'grades')));
51 $mform->setHelpButton('outcomeid', array('outcomeid', get_string('outcomeid', 'grades'), 'grade'), true);
52 $mform->addRule('outcomeid', get_string('required'), 'required');
54 $options = array(0=>get_string('none'));
55 if ($coursemods = get_course_mods($COURSE->id
)) {
56 foreach ($coursemods as $coursemod) {
57 if ($mod = get_coursemodule_from_id($coursemod->modname
, $coursemod->id
)) {
58 $options[$coursemod->id
] = format_string($mod->name
);
62 $mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options);
63 $mform->setHelpButton('cmid', array('linkedactivity', get_string('linkedactivity', 'grades'), 'grade'), true);
64 $mform->setDefault('cmid', 0);
66 /*$mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
67 $mform->setHelpButton('gradepass', array(false, get_string('gradepass', 'grades'),
68 false, true, false, get_string('gradepasshelp', 'grades')));*/
71 /// advcheckbox is not compatible with disabledIf !!
72 $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
73 $mform->setHelpButton('hidden', array('hidden', get_string('hidden', 'grades'), 'grade'));
74 $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
75 $mform->setHelpButton('hiddenuntil', array('hiddenuntil', get_string('hiddenuntil', 'grades'), 'grade'));
76 $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
79 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
80 $mform->setHelpButton('locked', array('locked', get_string('locked', 'grades'), 'grade'));
81 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
82 $mform->setHelpButton('locktime', array('lockedafter', get_string('locktime', 'grades'), 'grade'));
84 /// parent category related settings
85 $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
90 $categories = grade_category
::fetch_all(array('courseid'=>$COURSE->id
));
91 foreach ($categories as $cat) {
92 $cat->apply_forced_settings();
93 $options[$cat->id
] = $cat->get_name();
94 if ($cat->is_course_category()) {
97 if ($cat->is_aggregationcoef_used()) {
98 if ($cat->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN
) {
99 $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ?
'aggregationcoefweight' : 'aggregationcoef';
101 } else if ($cat->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN2
) {
102 $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ?
'aggregationcoefextrasum' : 'aggregationcoef';
104 } else if ($cat->aggregation
== GRADE_AGGREGATE_EXTRACREDIT_MEAN
) {
105 $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextra') ?
'aggregationcoefextra' : 'aggregationcoef';
107 } else if ($cat->aggregation
== GRADE_AGGREGATE_SUM
) {
108 $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ?
'aggregationcoefextrasum' : 'aggregationcoef';
111 $coefstring = 'aggregationcoef';
114 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id
);
118 if (count($categories) > 1) {
119 $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
120 $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
123 if ($coefstring !== '') {
124 if ($coefstring == 'aggregationcoefextrasum') {
125 // advcheckbox is not compatible with disabledIf!
126 $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
128 $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
130 $mform->setHelpButton('aggregationcoef', array($coefstring, get_string($coefstring, 'grades'), 'grade'), true);
134 $mform->addElement('hidden', 'id', 0);
135 $mform->setType('id', PARAM_INT
);
137 $mform->addElement('hidden', 'courseid', $COURSE->id
);
138 $mform->setType('courseid', PARAM_INT
);
140 /// add return tracking info
141 $gpr = $this->_customdata
['gpr'];
142 $gpr->add_mform_elements($mform);
144 /// mark advanced according to site settings
145 if (isset($CFG->grade_item_advanced
)) {
146 $advanced = explode(',', $CFG->grade_item_advanced
);
147 foreach ($advanced as $el) {
148 if ($mform->elementExists($el)) {
149 $mform->setAdvanced($el);
153 //-------------------------------------------------------------------------------
155 $this->add_action_buttons();
159 /// tweak the form - depending on existing data
160 function definition_after_data() {
161 global $CFG, $COURSE;
163 $mform =& $this->_form
;
165 if ($id = $mform->getElementValue('id')) {
166 $grade_item = grade_item
::fetch(array('id'=>$id));
168 //remove the aggregation coef element if not needed
169 if ($grade_item->is_course_item()) {
170 if ($mform->elementExists('parentcategory')) {
171 $mform->removeElement('parentcategory');
173 if ($mform->elementExists('aggregationcoef')) {
174 $mform->removeElement('aggregationcoef');
178 // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
179 if ($mform->elementExists('parentcategory')) {
180 $mform->hardFreeze('parentcategory');
183 if ($grade_item->is_category_item()) {
184 $category = $grade_item->get_item_category();
185 $parent_category = $category->get_parent_category();
187 $parent_category = $grade_item->get_parent_category();
190 $parent_category->apply_forced_settings();
192 if (!$parent_category->is_aggregationcoef_used() or $parent_category->aggregation
== GRADE_AGGREGATE_SUM
) {
193 if ($mform->elementExists('aggregationcoef')) {
194 $mform->removeElement('aggregationcoef');
197 //fix label if needed
198 $agg_el =& $mform->getElement('aggregationcoef');
200 if ($parent_category->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN
) {
201 $aggcoef = 'aggregationcoefweight';
203 } else if ($parent_category->aggregation
== GRADE_AGGREGATE_WEIGHTED_MEAN2
) {
204 $aggcoef = 'aggregationcoefextrasum';
206 } else if ($parent_category->aggregation
== GRADE_AGGREGATE_EXTRACREDIT_MEAN
) {
207 $aggcoef = 'aggregationcoefextra';
209 } else if ($parent_category->aggregation
== GRADE_AGGREGATE_SUM
) {
210 $aggcoef = 'aggregationcoefextrasum';
213 if ($aggcoef !== '') {
214 $agg_el->setLabel(get_string($aggcoef, 'grades'));
215 $mform->setHelpButton('aggregationcoef', array($aggcoef, get_string($aggcoef, 'grades'), 'grade'));
222 // no parent header for course category
223 if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
224 $mform->removeElement('headerparent');
229 /// perform extra validation before submission
230 function validation($data, $files) {
233 $errors = parent
::validation($data, $files);
235 if (array_key_exists('idnumber', $data)) {
237 $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
241 if (!grade_verify_idnumber($data['idnumber'], $COURSE->id
, $grade_item, null)) {
242 $errors['idnumber'] = get_string('idnumbertaken');