Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / grade / edit / item_form.php
blob7e3d9e9c320a6cd6274203e6ddda679f5a044955
1 <?php //$Id$
3 require_once $CFG->libdir.'/formslib.php';
5 class edit_item_form extends moodleform {
6 function definition() {
7 global $COURSE, $CFG;
9 $mform =& $this->_form;
11 /// visible elements
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->addElement('text', 'idnumber', get_string('idnumber'));
18 $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
19 GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
20 GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
21 GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
23 $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
24 $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
26 $mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
27 $mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
28 $mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
30 $options = array(0=>get_string('usenoscale', 'grades'));
31 if ($scales = get_records('scale')) {
32 foreach ($scales as $scale) {
33 $options[$scale->id] = format_string($scale->name);
36 $mform->addElement('select', 'scaleid', get_string('scale'), $options);
37 $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
39 $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
40 $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
41 $mform->setDefault('grademax', 100.0);
43 $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
44 $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
45 $mform->setDefault('grademin', 0.0);
47 $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
48 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
49 $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
50 $mform->setDefault('gradepass', 0.0);
52 $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
53 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
54 $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
55 $mform->setDefault('multfactor', 1.0);
57 $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
58 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
59 $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
60 $mform->setDefault('plusfactor', 0.0);
62 $mform->addElement('text', 'aggregationcoef', get_string('aggregationcoef', 'grades'));
63 $mform->setDefault('aggregationcoef', 0.0);
65 $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
67 $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
68 $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
70 // user preferences
71 $mform->addElement('header', 'general', get_string('userpreferences', 'grades'));
72 $options = array(GRADE_REPORT_PREFERENCE_DEFAULT => get_string('default', 'grades'),
73 GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL => get_string('real', 'grades'),
74 GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE => get_string('percentage', 'grades'),
75 GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER => get_string('letter', 'grades'));
76 $label = get_string('gradedisplaytype', 'grades') . ' (' . get_string('default', 'grades')
77 . ': ' . $options[$CFG->grade_report_gradedisplaytype] . ')';
78 $mform->addElement('select', 'pref_gradedisplaytype', $label, $options);
79 $mform->setHelpButton('pref_gradedisplaytype', array(false, get_string('gradedisplaytype', 'grades'),
80 false, true, false, get_string("configgradedisplaytype", 'grades')));
81 $mform->setDefault('pref_gradedisplaytype', GRADE_REPORT_PREFERENCE_DEFAULT);
83 $options = array(GRADE_REPORT_PREFERENCE_DEFAULT => get_string('default', 'grades'), 0, 1, 2, 3, 4, 5);
84 $label = get_string('decimalpoints', 'grades') . ' (' . get_string('default', 'grades')
85 . ': ' . $options[$CFG->grade_report_decimalpoints] . ')';
86 $mform->addElement('select', 'pref_decimalpoints', $label, $options);
87 $mform->setHelpButton('pref_decimalpoints', array(false, get_string('decimalpoints', 'grades'),
88 false, true, false, get_string("configdecimalpoints", 'grades')));
89 $mform->setDefault('pref_decimalpoints', GRADE_REPORT_PREFERENCE_DEFAULT);
91 /// hidden params
92 $mform->addElement('hidden', 'id', 0);
93 $mform->setType('id', PARAM_INT);
95 $mform->addElement('hidden', 'courseid', $COURSE->id);
96 $mform->setType('courseid', PARAM_INT);
98 $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
99 $mform->setType('itemtype', PARAM_ALPHA);
101 /// add return tracking info
102 $gpr = $this->_customdata['gpr'];
103 $gpr->add_mform_elements($mform);
105 //-------------------------------------------------------------------------------
106 // buttons
107 $this->add_action_buttons();
111 /// tweak the form - depending on existing data
112 function definition_after_data() {
113 global $CFG, $COURSE;
115 $mform =& $this->_form;
117 if ($id = $mform->getElementValue('id')) {
118 $grade_item = grade_item::fetch(array('id'=>$id));
120 if ($grade_item->is_normal_item()) {
121 // following items are set up from modules and should not be overrided by user
122 $mform->hardFreeze('itemname,idnumber,gradetype,grademax,grademin,scaleid');
123 $mform->removeElement('calculation');
125 } else if ($grade_item->is_manual_item()) {
126 // manual grade item does not use these - uses only final grades
127 $mform->removeElement('plusfactor');
128 $mform->removeElement('multfactor');
132 //remove the aggregation coef element if not needed
133 if ($grade_item->is_course_item()) {
134 $mform->removeElement('aggregationcoef');
136 } else if ($grade_item->is_category_item()) {
137 $category = $grade_item->get_item_category();
138 $parent_category = $category->get_parent_category();
139 if (!$parent_category->is_aggregationcoef_used()) {
140 $mform->removeElement('aggregationcoef');
143 } else {
144 $parent_category = $grade_item->get_parent_category();
145 if (!$parent_category->is_aggregationcoef_used()) {
146 $mform->removeElement('aggregationcoef');
150 } else {
151 // all new items are manual, children of course category
152 $mform->removeElement('plusfactor');
153 $mform->removeElement('multfactor');
155 $course_category = grade_category::fetch_course_category($COURSE->id);
156 if (!$course_category->is_aggregationcoef_used()) {
157 $mform->removeElement('aggregationcoef');
163 /// perform extra validation before submission
164 function validation($data){
165 $errors= array();
167 if (array_key_exists('calculation', $data) and $data['calculation'] != '') {
168 $grade_item = new grade_item(array('id'=>$data['id'], 'itemtype'=>$data['itemtype'], 'courseid'=>$data['courseid']));
169 $result = $grade_item->validate_formula($data['calculation']);
170 if ($result !== true) {
171 $errors['calculation'] = $result;
175 if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
176 if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
177 $errors['grademin'] = get_String('incorrectminmax', 'grades');
178 $errors['grademax'] = get_String('incorrectminmax', 'grades');
182 if (0 == count($errors)){
183 return true;
184 } else {
185 return $errors;