Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/edit/tree/item_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 if (!defined('MOODLE_INTERNAL')) {
00019     die('Direct access to this script is forbidden.');    
00020 }
00021 
00022 require_once $CFG->libdir.'/formslib.php';
00023 
00024 class edit_item_form extends moodleform {
00025     private $displayoptions;
00026 
00027     function definition() {
00028         global $COURSE, $CFG, $DB;
00029 
00030         $mform =& $this->_form;
00031 
00032         $item = $this->_customdata['current'];
00033 
00035         $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
00036 
00037         $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
00038         $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
00039         $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
00040 
00041         $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
00042         $mform->addHelpButton('idnumber', 'idnumbermod');
00043 
00044         $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
00045                          GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
00046                          GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
00047                          GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
00048 
00049         $mform->addElement('select', 'gradetype', get_string('gradetype', 'grades'), $options);
00050         $mform->addHelpButton('gradetype', 'gradetype', 'grades');
00051         $mform->setDefault('gradetype', GRADE_TYPE_VALUE);
00052 
00053         //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
00054         //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
00055         //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
00056 
00057         $options = array(0=>get_string('usenoscale', 'grades'));
00058         if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
00059             foreach ($scales as $scale) {
00060                 $options[$scale->id] = $scale->get_name();
00061             }
00062         }
00063         if ($scales = grade_scale::fetch_all_global()) {
00064             foreach ($scales as $scale) {
00065                 $options[$scale->id] = $scale->get_name();
00066             }
00067         }
00068         // ugly BC hack - it was possible to use custom scale from other courses :-(
00069         if (!empty($item->scaleid) and !isset($options[$item->scaleid])) {
00070             if ($scale = grade_scale::fetch(array('id'=>$item->scaleid))) {
00071                 $options[$scale->id] = $scale->get_name().get_string('incorrectcustomscale', 'grades');
00072             }
00073         }
00074         $mform->addElement('select', 'scaleid', get_string('scale'), $options);
00075         $mform->addHelpButton('scaleid', 'typescale', 'grades');
00076         $mform->disabledIf('scaleid', 'gradetype', 'noteq', GRADE_TYPE_SCALE);
00077 
00078         $mform->addElement('text', 'grademax', get_string('grademax', 'grades'));
00079         $mform->addHelpButton('grademax', 'grademax', 'grades');
00080         $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
00081 
00082         $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
00083         $mform->addHelpButton('grademin', 'grademin', 'grades');
00084         $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
00085 
00086         $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
00087         $mform->addHelpButton('gradepass', 'gradepass', 'grades');
00088         $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
00089         $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
00090 
00091         $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
00092         $mform->addHelpButton('multfactor', 'multfactor', 'grades');
00093         $mform->setAdvanced('multfactor');
00094         $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
00095         $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
00096 
00097         $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
00098         $mform->addHelpButton('plusfactor', 'plusfactor', 'grades');
00099         $mform->setAdvanced('plusfactor');
00100         $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
00101         $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
00102 
00104         $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
00105         $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
00106                          GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
00107                          GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
00108                          GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
00109                          GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
00110                          GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
00111                          GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
00112                          GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
00113                          GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
00114                          GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
00115                          );
00116 
00117         asort($options);
00118 
00119         foreach ($options as $key=>$option) {
00120             if ($key == $default_gradedisplaytype) {
00121                 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
00122                 break;
00123             }
00124         }
00125         $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options);
00126         $mform->addHelpButton('display', 'gradedisplaytype', 'grades');
00127 
00128         $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
00129         $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
00130         $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options);
00131         $mform->addHelpButton('decimals', 'decimalpoints', 'grades');
00132         $mform->setDefault('decimals', -1);
00133         $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
00134         if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
00135             $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
00136         }
00137 
00139         // advcheckbox is not compatible with disabledIf!
00140         $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
00141         $mform->addHelpButton('hidden', 'hidden', 'grades');
00142         $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
00143         $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
00144 
00146         $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
00147         $mform->addHelpButton('locked', 'locked', 'grades');
00148 
00149         $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
00150         $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
00151 
00153         $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
00154 
00155         $options = array();
00156         $coefstring = '';
00157         $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
00158 
00159         foreach ($categories as $cat) {
00160             $cat->apply_forced_settings();
00161             $options[$cat->id] = $cat->get_name();
00162         }
00163 
00164         if (count($categories) > 1) {
00165             $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
00166         }
00167 
00169         $mform->addElement('hidden', 'id', 0);
00170         $mform->setType('id', PARAM_INT);
00171 
00172         $mform->addElement('hidden', 'courseid', $COURSE->id);
00173         $mform->setType('courseid', PARAM_INT);
00174 
00175         $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
00176         $mform->setType('itemtype', PARAM_ALPHA);
00177 
00179         $gpr = $this->_customdata['gpr'];
00180         $gpr->add_mform_elements($mform);
00181 
00183         if (isset($CFG->grade_item_advanced)) {
00184             $advanced = explode(',', $CFG->grade_item_advanced);
00185             foreach ($advanced as $el) {
00186                 if ($mform->elementExists($el)) {
00187                     $mform->setAdvanced($el);
00188                 }
00189             }
00190         }
00191 //-------------------------------------------------------------------------------
00192         // buttons
00193         $this->add_action_buttons();
00194 //-------------------------------------------------------------------------------
00195         $this->set_data($item);
00196     }
00197 
00198 
00200     function definition_after_data() {
00201         global $CFG, $COURSE;
00202 
00203         $mform =& $this->_form;
00204 
00205         if ($id = $mform->getElementValue('id')) {
00206             $grade_item = grade_item::fetch(array('id'=>$id));
00207 
00208             if (!$grade_item->is_raw_used()) {
00209                 $mform->removeElement('plusfactor');
00210                 $mform->removeElement('multfactor');
00211             }
00212 
00213             if ($grade_item->is_outcome_item()) {
00214                 // we have to prevent incompatible modifications of outcomes if outcomes disabled
00215                 $mform->removeElement('grademax');
00216                 $mform->removeElement('grademin');
00217                 $mform->removeElement('gradetype');
00218                 $mform->removeElement('display');
00219                 $mform->removeElement('decimals');
00220                 $mform->hardFreeze('scaleid');
00221 
00222             } else {
00223                 if ($grade_item->is_external_item()) {
00224                     // following items are set up from modules and should not be overrided by user
00225                     $mform->hardFreeze('itemname,gradetype,grademax,grademin,scaleid');
00226                     if ($grade_item->itemnumber == 0) {
00227                         // the idnumber of grade itemnumber 0 is synced with course_modules
00228                         $mform->hardFreeze('idnumber');
00229                     }
00230                     //$mform->removeElement('calculation');
00231                 }
00232             }
00233 
00234             // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
00235             if ($mform->elementExists('parentcategory')) {
00236                 $mform->hardFreeze('parentcategory');
00237             }
00238 
00239             $parent_category = $grade_item->get_parent_category();
00240             $parent_category->apply_forced_settings();
00241 
00242             if (!$parent_category->is_aggregationcoef_used()) {
00243                 if ($mform->elementExists('aggregationcoef')) {
00244                     $mform->removeElement('aggregationcoef');
00245                 }
00246 
00247             } else {
00248                 $coefstring = $grade_item->get_coefstring();
00249 
00250                 if ($coefstring !== '') {
00251                     if ($coefstring == 'aggregationcoefextrasum') {
00252                         // advcheckbox is not compatible with disabledIf!
00253                         $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
00254                     } else {
00255                         $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
00256                     }
00257                     if ($mform->elementExists('parentcategory')) {
00258                         $mform->insertElementBefore($element, 'parentcategory');
00259                     } else {
00260                         $mform->insertElementBefore($element, 'id');
00261                     }
00262                     $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
00263                 }
00264 
00265                 $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
00266             }
00267 
00268             if ($category = $grade_item->get_item_category()) {
00269                 if ($category->aggregation == GRADE_AGGREGATE_SUM) {
00270                     if ($mform->elementExists('gradetype')) {
00271                         $mform->hardFreeze('gradetype');
00272                     }
00273                     if ($mform->elementExists('grademin')) {
00274                         $mform->hardFreeze('grademin');
00275                     }
00276                     if ($mform->elementExists('grademax')) {
00277                         $mform->hardFreeze('grademax');
00278                     }
00279                     if ($mform->elementExists('scaleid')) {
00280                         $mform->removeElement('scaleid');
00281                     }
00282                 }
00283             }
00284 
00285         } else {
00286             // all new items are manual, children of course category
00287             $mform->removeElement('plusfactor');
00288             $mform->removeElement('multfactor');
00289         }
00290 
00291         // no parent header for course category
00292         if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
00293             $mform->removeElement('headerparent');
00294         }
00295     }
00296 
00297 
00299     function validation($data, $files) {
00300         global $COURSE;
00301 
00302         $errors = parent::validation($data, $files);
00303 
00304         if (array_key_exists('idnumber', $data)) {
00305             if ($data['id']) {
00306                 $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
00307                 if ($grade_item->itemtype == 'mod') {
00308                     $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
00309                 } else {
00310                     $cm = null;
00311                 }
00312             } else {
00313                 $grade_item = null;
00314                 $cm = null;
00315             }
00316             if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
00317                 $errors['idnumber'] = get_string('idnumbertaken');
00318             }
00319         }
00320 
00321         if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
00322             if (empty($data['scaleid'])) {
00323                 $errors['scaleid'] = get_string('missingscale', 'grades');
00324             }
00325         }
00326 
00327         if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
00328             if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
00329                 $errors['grademin'] = get_string('incorrectminmax', 'grades');
00330                 $errors['grademax'] = get_string('incorrectminmax', 'grades');
00331             }
00332         }
00333 
00334         return $errors;
00335     }
00336 
00337 }
00338 
 All Data Structures Namespaces Files Functions Variables Enumerations