3 require_once $CFG->libdir
.'/formslib.php';
5 class edit_item_form
extends moodleform
{
6 function definition() {
9 $mform =& $this->_form
;
12 $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
14 $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
15 $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
16 $mform->setHelpButton('iteminfo', array(false, get_string('iteminfo', 'grades'), false, true, false, 'iteminfohelp'));
18 $mform->addElement('text', 'idnumber', get_string('idnumber'));
19 $mform->setHelpButton('idnumber', array(false, get_string('idnumber', 'grades'), false, true, false, 'idnumberhelp'));
21 $options = array(GRADE_TYPE_NONE
=>get_string('typenone', 'grades'),
22 GRADE_TYPE_VALUE
=>get_string('typevalue', 'grades'),
23 GRADE_TYPE_SCALE
=>get_string('typescale', 'grades'),
24 GRADE_TYPE_TEXT
=>get_string('typetext', 'grades'));
26 $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
27 $mform->setHelpButton('gradetype', array(false, get_string('gradetype', 'grades'), false, true, false, 'gradetypehelp'));
28 $mform->setDefault('gradetype', GRADE_TYPE_VALUE
);
30 //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
31 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
32 //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
34 $options = array(0=>get_string('usenoscale', 'grades'));
35 if ($scales = get_records('scale')) {
36 foreach ($scales as $scale) {
37 $options[$scale->id
] = format_string($scale->name
);
40 $mform->addElement('select', 'scaleid', get_string('scale'), $options);
41 $mform->setHelpButton('scaleid', array(false, get_string('scaleid', 'grades'), false, true, false, 'scaleidhelp'));
42 $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE
);
44 $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
45 $mform->setHelpButton('grademax', array(false, get_string('grademax', 'grades'), false, true, false, 'grademaxhelp'));
46 $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE
);
47 $mform->setDefault('grademax', 100.0);
49 $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
50 $mform->setHelpButton('grademin', array(false, get_string('grademin', 'grades'), false, true, false, 'grademinhelp'));
51 $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE
);
52 $mform->setDefault('grademin', 0.0);
54 $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
55 $mform->setHelpButton('gradepass', array(false, get_string('gradepass', 'grades'), false, true, false, 'gradepasshelp'));
56 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE
);
57 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT
);
58 $mform->setDefault('gradepass', 0.0);
60 $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
61 $mform->setHelpButton('multfactor', array(false, get_string('multfactor', 'grades'), false, true, false, 'multfactorhelp'));
62 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE
);
63 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT
);
64 $mform->setDefault('multfactor', 1.0);
66 $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
67 $mform->setHelpButton('plusfactor', array(false, get_string('plusfactor', 'grades'), false, true, false, 'plusfactorhelp'));
68 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE
);
69 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT
);
70 $mform->setDefault('plusfactor', 0.0);
72 $mform->addElement('text', 'aggregationcoef', get_string('aggregationcoef', 'grades'));
73 $mform->setHelpButton('aggregationcoef', array(false, get_string('aggregationcoef', 'grades'), false, true, false, 'aggregationcoefhelp'));
74 $mform->setDefault('aggregationcoef', 0.0);
76 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
77 $mform->setHelpButton('locked', array(false, get_string('locked', 'grades'), false, true, false, 'lockedhelp'));
79 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
80 $mform->setHelpButton('locktime', array(false, get_string('locktime', 'grades'), false, true, false, 'locktimehelp'));
81 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE
);
84 $mform->addElement('header', 'general', get_string('userpreferences', 'grades'));
85 $options = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> get_string('default', 'grades'),
86 GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL
=> get_string('real', 'grades'),
87 GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE
=> get_string('percentage', 'grades'),
88 GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER
=> get_string('letter', 'grades'));
89 $label = get_string('gradedisplaytype', 'grades') . ' (' . get_string('default', 'grades')
90 . ': ' . $options[$CFG->grade_report_gradedisplaytype
] . ')';
91 $mform->addElement('select', 'pref_gradedisplaytype', $label, $options);
92 $mform->setHelpButton('pref_gradedisplaytype', array(false, get_string('gradedisplaytype', 'grades'),
93 false, true, false, get_string("configgradedisplaytype", 'grades')));
94 $mform->setDefault('pref_gradedisplaytype', GRADE_REPORT_PREFERENCE_DEFAULT
);
96 $options = array(GRADE_REPORT_PREFERENCE_DEFAULT
=> get_string('default', 'grades'), 0, 1, 2, 3, 4, 5);
97 $label = get_string('decimalpoints', 'grades') . ' (' . get_string('default', 'grades')
98 . ': ' . $options[$CFG->grade_report_decimalpoints
] . ')';
99 $mform->addElement('select', 'pref_decimalpoints', $label, $options);
100 $mform->setHelpButton('pref_decimalpoints', array(false, get_string('decimalpoints', 'grades'),
101 false, true, false, get_string("configdecimalpoints", 'grades')));
102 $mform->setDefault('pref_decimalpoints', GRADE_REPORT_PREFERENCE_DEFAULT
);
105 $mform->addElement('hidden', 'id', 0);
106 $mform->setType('id', PARAM_INT
);
108 $mform->addElement('hidden', 'courseid', $COURSE->id
);
109 $mform->setType('courseid', PARAM_INT
);
111 $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
112 $mform->setType('itemtype', PARAM_ALPHA
);
114 /// add return tracking info
115 $gpr = $this->_customdata
['gpr'];
116 $gpr->add_mform_elements($mform);
118 //-------------------------------------------------------------------------------
120 $this->add_action_buttons();
124 /// tweak the form - depending on existing data
125 function definition_after_data() {
126 global $CFG, $COURSE;
128 $mform =& $this->_form
;
130 if ($id = $mform->getElementValue('id')) {
131 $grade_item = grade_item
::fetch(array('id'=>$id));
133 if ($grade_item->is_outcome_item()) {
134 // we have to prevent incompatible modifications of outcomes
135 $mform->removeElement('plusfactor');
136 $mform->removeElement('multfactor');
137 $mform->removeElement('grademax');
138 $mform->removeElement('grademin');
139 $mform->removeElement('gradetype');
140 $mform->hardFreeze('scaleid');
143 if ($grade_item->is_normal_item()) {
144 // following items are set up from modules and should not be overrided by user
145 $mform->hardFreeze('itemname,idnumber,gradetype,grademax,grademin,scaleid');
146 //$mform->removeElement('calculation');
148 } else if ($grade_item->is_manual_item()) {
149 // manual grade item does not use these - uses only final grades
150 $mform->removeElement('plusfactor');
151 $mform->removeElement('multfactor');
154 //remove the aggregation coef element if not needed
155 if ($grade_item->is_course_item()) {
156 $mform->removeElement('aggregationcoef');
158 } else if ($grade_item->is_category_item()) {
159 $category = $grade_item->get_item_category();
160 $parent_category = $category->get_parent_category();
161 if (!$parent_category->is_aggregationcoef_used()) {
162 $mform->removeElement('aggregationcoef');
166 $parent_category = $grade_item->get_parent_category();
167 if (!$parent_category->is_aggregationcoef_used()) {
168 $mform->removeElement('aggregationcoef');
173 // all new items are manual, children of course category
174 $mform->removeElement('plusfactor');
175 $mform->removeElement('multfactor');
177 $course_category = grade_category
::fetch_course_category($COURSE->id
);
178 if (!$course_category->is_aggregationcoef_used()) {
179 $mform->removeElement('aggregationcoef');
185 /// perform extra validation before submission
186 function validation($data){
189 if (array_key_exists('idnumber', $data)) {
191 $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
192 if ($grade_item->itemtype
== 'mod') {
193 $cm = get_coursemodule_from_instance($grade_item->itemmodule
, $grade_item->iteminstance
, $grade_item->courseid
);
201 if (!grade_verify_idnumber($data['idnumber'], $grade_item, $cm)) {
202 $errors['idnumber'] = get_string('idnumbertaken');
207 if (array_key_exists('calculation', $data) and $data['calculation'] != '') {
208 $grade_item = new grade_item(array('id'=>$data['id'], 'itemtype'=>$data['itemtype'], 'courseid'=>$data['courseid']));
209 $result = $grade_item->validate_formula($data['calculation']);
210 if ($result !== true) {
211 $errors['calculation'] = $result;
216 if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
217 if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
218 $errors['grademin'] = get_String('incorrectminmax', 'grades');
219 $errors['grademax'] = get_String('incorrectminmax', 'grades');
223 if (0 == count($errors)){