PageRenderTime 37ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/grade/edit/tree/item_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 360 lines | 251 code | 60 blank | 49 comment | 53 complexity | b56a11737e9210e3e7499b606c427f29 MD5 | raw file
  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 allowing the editing of the grade options for an individual grade item
 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
 25if (!defined('MOODLE_INTERNAL')) {
 26    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 27}
 28
 29require_once $CFG->libdir.'/formslib.php';
 30
 31class edit_item_form extends moodleform {
 32    private $displayoptions;
 33
 34    function definition() {
 35        global $COURSE, $CFG, $DB;
 36
 37        $mform =& $this->_form;
 38
 39        $item = $this->_customdata['current'];
 40
 41/// visible elements
 42        $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
 43
 44        $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
 45        $mform->setType('itemname', PARAM_TEXT);
 46        $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
 47        $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
 48        $mform->setType('iteminfo', PARAM_TEXT);
 49
 50        $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
 51        $mform->addHelpButton('idnumber', 'idnumbermod');
 52        $mform->setType('idnumber', PARAM_RAW);
 53
 54        // Manual grade items cannot have grade type GRADE_TYPE_NONE.
 55        $options = array(GRADE_TYPE_VALUE => get_string('typevalue', 'grades'),
 56                         GRADE_TYPE_SCALE => get_string('typescale', 'grades'),
 57                         GRADE_TYPE_TEXT => get_string('typetext', 'grades'));
 58
 59        $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
 60        $mform->addHelpButton('gradetype', 'gradetype', 'grades');
 61        $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
 62
 63        //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
 64        //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 65        //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
 66
 67        $options = array(0=>get_string('usenoscale', 'grades'));
 68        if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
 69            foreach ($scales as $scale) {
 70                $options[$scale->id] = $scale->get_name();
 71            }
 72        }
 73        if ($scales = grade_scale::fetch_all_global()) {
 74            foreach ($scales as $scale) {
 75                $options[$scale->id] = $scale->get_name();
 76            }
 77        }
 78        // ugly BC hack - it was possible to use custom scale from other courses :-(
 79        if (!empty($item->scaleid) and !isset($options[$item->scaleid])) {
 80            if ($scale = grade_scale::fetch(array('id'=>$item->scaleid))) {
 81                $options[$scale->id] = $scale->get_name().get_string('incorrectcustomscale', 'grades');
 82            }
 83        }
 84        $mform->addElement('select', 'scaleid', get_string('scale'), $options);
 85        $mform->addHelpButton('scaleid', 'typescale', 'grades');
 86        $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
 87
 88        $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
 89        $mform->addHelpButton('grademax', 'grademax', 'grades');
 90        $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
 91        $mform->setType('grademax', PARAM_RAW);
 92
 93        $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
 94        $mform->addHelpButton('grademin', 'grademin', 'grades');
 95        $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
 96        $mform->setType('grademin', PARAM_RAW);
 97
 98        $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
 99        $mform->addHelpButton('gradepass', 'gradepass', 'grades');
100        $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
101        $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
102        $mform->setType('gradepass', PARAM_RAW);
103
104        $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
105        $mform->addHelpButton('multfactor', 'multfactor', 'grades');
106        $mform->setAdvanced('multfactor');
107        $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
108        $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
109        $mform->setType('multfactor', PARAM_RAW);
110
111        $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
112        $mform->addHelpButton('plusfactor', 'plusfactor', 'grades');
113        $mform->setAdvanced('plusfactor');
114        $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
115        $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
116        $mform->setType('plusfactor', PARAM_RAW);
117
118        /// grade display prefs
119        $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
120        $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
121                         GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
122                         GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
123                         GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
124                         GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
125                         GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
126                         GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
127                         GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
128                         GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
129                         GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
130                         );
131
132        asort($options);
133
134        foreach ($options as $key=>$option) {
135            if ($key == $default_gradedisplaytype) {
136                $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
137                break;
138            }
139        }
140        $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options);
141        $mform->addHelpButton('display', 'gradedisplaytype', 'grades');
142
143        $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
144        $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
145        $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options);
146        $mform->addHelpButton('decimals', 'decimalpoints', 'grades');
147        $mform->setDefault('decimals', -1);
148        $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
149        if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
150            $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
151        }
152
153        /// hiding
154        if ($item->cancontrolvisibility) {
155            // advcheckbox is not compatible with disabledIf!
156            $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
157            $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
158            $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
159        } else {
160            $mform->addElement('static', 'hidden', get_string('hidden', 'grades'),
161                    get_string('componentcontrolsvisibility', 'grades'));
162            // Unset hidden to avoid data override.
163            unset($item->hidden);
164        }
165        $mform->addHelpButton('hidden', 'hidden', 'grades');
166
167        /// locking
168        $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
169        $mform->addHelpButton('locked', 'locked', 'grades');
170
171        $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
172        $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
173
174/// parent category related settings
175        $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
176
177        $options = array();
178        $coefstring = '';
179        $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
180
181        foreach ($categories as $cat) {
182            $cat->apply_forced_settings();
183            $options[$cat->id] = $cat->get_name();
184        }
185
186        if (count($categories) > 1) {
187            $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
188        }
189
190/// hidden params
191        $mform->addElement('hidden', 'id', 0);
192        $mform->setType('id', PARAM_INT);
193
194        $mform->addElement('hidden', 'courseid', $COURSE->id);
195        $mform->setType('courseid', PARAM_INT);
196
197        $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
198        $mform->setType('itemtype', PARAM_ALPHA);
199
200/// add return tracking info
201        $gpr = $this->_customdata['gpr'];
202        $gpr->add_mform_elements($mform);
203
204/// mark advanced according to site settings
205        if (isset($CFG->grade_item_advanced)) {
206            $advanced = explode(',', $CFG->grade_item_advanced);
207            foreach ($advanced as $el) {
208                if ($mform->elementExists($el)) {
209                    $mform->setAdvanced($el);
210                }
211            }
212        }
213//-------------------------------------------------------------------------------
214        // buttons
215        $this->add_action_buttons();
216//-------------------------------------------------------------------------------
217        $this->set_data($item);
218    }
219
220
221/// tweak the form - depending on existing data
222    function definition_after_data() {
223        global $CFG, $COURSE;
224
225        $mform =& $this->_form;
226
227        if ($id = $mform->getElementValue('id')) {
228            $grade_item = grade_item::fetch(array('id'=>$id));
229
230            if (!$grade_item->is_raw_used()) {
231                $mform->removeElement('plusfactor');
232                $mform->removeElement('multfactor');
233            }
234
235            if ($grade_item->is_outcome_item()) {
236                // we have to prevent incompatible modifications of outcomes if outcomes disabled
237                $mform->removeElement('grademax');
238                $mform->removeElement('grademin');
239                $mform->removeElement('gradetype');
240                $mform->removeElement('display');
241                $mform->removeElement('decimals');
242                $mform->hardFreeze('scaleid');
243
244            } else {
245                if ($grade_item->is_external_item()) {
246                    // following items are set up from modules and should not be overrided by user
247                    $mform->hardFreeze('itemname,gradetype,grademax,grademin,scaleid');
248                    if ($grade_item->itemnumber == 0) {
249                        // the idnumber of grade itemnumber 0 is synced with course_modules
250                        $mform->hardFreeze('idnumber');
251                    }
252                    //$mform->removeElement('calculation');
253                }
254            }
255
256            // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
257            if ($mform->elementExists('parentcategory')) {
258                $mform->hardFreeze('parentcategory');
259            }
260
261            $parent_category = $grade_item->get_parent_category();
262            $parent_category->apply_forced_settings();
263
264            if (!$parent_category->is_aggregationcoef_used()) {
265                if ($mform->elementExists('aggregationcoef')) {
266                    $mform->removeElement('aggregationcoef');
267                }
268
269            } else {
270                $coefstring = $grade_item->get_coefstring();
271
272                if ($coefstring !== '') {
273                    if ($coefstring == 'aggregationcoefextrasum') {
274                        // advcheckbox is not compatible with disabledIf!
275                        $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
276                    } else {
277                        $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
278                    }
279                    if ($mform->elementExists('parentcategory')) {
280                        $mform->insertElementBefore($element, 'parentcategory');
281                    } else {
282                        $mform->insertElementBefore($element, 'id');
283                    }
284                    $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
285                }
286
287                $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
288            }
289
290            if ($category = $grade_item->get_item_category()) {
291                if ($category->aggregation == GRADE_AGGREGATE_SUM) {
292                    if ($mform->elementExists('gradetype')) {
293                        $mform->hardFreeze('gradetype');
294                    }
295                    if ($mform->elementExists('grademin')) {
296                        $mform->hardFreeze('grademin');
297                    }
298                    if ($mform->elementExists('grademax')) {
299                        $mform->hardFreeze('grademax');
300                    }
301                    if ($mform->elementExists('scaleid')) {
302                        $mform->removeElement('scaleid');
303                    }
304                }
305            }
306
307        } else {
308            // all new items are manual, children of course category
309            $mform->removeElement('plusfactor');
310            $mform->removeElement('multfactor');
311        }
312
313        // no parent header for course category
314        if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
315            $mform->removeElement('headerparent');
316        }
317    }
318
319
320/// perform extra validation before submission
321    function validation($data, $files) {
322        global $COURSE;
323
324        $errors = parent::validation($data, $files);
325
326        if (array_key_exists('idnumber', $data)) {
327            if ($data['id']) {
328                $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
329                if ($grade_item->itemtype == 'mod') {
330                    $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
331                } else {
332                    $cm = null;
333                }
334            } else {
335                $grade_item = null;
336                $cm = null;
337            }
338            if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
339                $errors['idnumber'] = get_string('idnumbertaken');
340            }
341        }
342
343        if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
344            if (empty($data['scaleid'])) {
345                $errors['scaleid'] = get_string('missingscale', 'grades');
346            }
347        }
348
349        if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
350            if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
351                $errors['grademin'] = get_string('incorrectminmax', 'grades');
352                $errors['grademax'] = get_string('incorrectminmax', 'grades');
353            }
354        }
355
356        return $errors;
357    }
358
359}
360