Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/edit/tree/category_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_category_form extends moodleform {
00025     private $aggregation_options = array();
00026 
00027     function definition() {
00028         global $CFG, $COURSE, $DB;
00029         $mform =& $this->_form;
00030 
00031         $category = $this->_customdata['current'];
00032 
00033         $this->aggregation_options = array(GRADE_AGGREGATE_MEAN            =>get_string('aggregatemean', 'grades'),
00034                                            GRADE_AGGREGATE_WEIGHTED_MEAN   =>get_string('aggregateweightedmean', 'grades'),
00035                                            GRADE_AGGREGATE_WEIGHTED_MEAN2  =>get_string('aggregateweightedmean2', 'grades'),
00036                                            GRADE_AGGREGATE_EXTRACREDIT_MEAN=>get_string('aggregateextracreditmean', 'grades'),
00037                                            GRADE_AGGREGATE_MEDIAN          =>get_string('aggregatemedian', 'grades'),
00038                                            GRADE_AGGREGATE_MIN             =>get_string('aggregatemin', 'grades'),
00039                                            GRADE_AGGREGATE_MAX             =>get_string('aggregatemax', 'grades'),
00040                                            GRADE_AGGREGATE_MODE            =>get_string('aggregatemode', 'grades'),
00041                                            GRADE_AGGREGATE_SUM             =>get_string('aggregatesum', 'grades'));
00042 
00043         // visible elements
00044         $mform->addElement('header', 'headercategory', get_string('gradecategory', 'grades'));
00045         $mform->addElement('text', 'fullname', get_string('categoryname', 'grades'));
00046         $mform->addRule('fullname', null, 'required', null, 'client');
00047 
00048         $mform->addElement('select', 'aggregation', get_string('aggregation', 'grades'), $this->aggregation_options);
00049         $mform->addHelpButton('aggregation', 'aggregation', 'grades');
00050 
00051         if ((int)$CFG->grade_aggregation_flag & 2) {
00052             $mform->setAdvanced('aggregation');
00053         }
00054 
00055         $mform->addElement('checkbox', 'aggregateonlygraded', get_string('aggregateonlygraded', 'grades'));
00056         $mform->addHelpButton('aggregateonlygraded', 'aggregateonlygraded', 'grades');
00057         $mform->disabledIf('aggregateonlygraded', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
00058 
00059         if ((int)$CFG->grade_aggregateonlygraded_flag & 2) {
00060             $mform->setAdvanced('aggregateonlygraded');
00061         }
00062 
00063         if (empty($CFG->enableoutcomes)) {
00064             $mform->addElement('hidden', 'aggregateoutcomes');
00065             $mform->setType('aggregateoutcomes', PARAM_INT);
00066         } else {
00067             $mform->addElement('checkbox', 'aggregateoutcomes', get_string('aggregateoutcomes', 'grades'));
00068             $mform->addHelpButton('aggregateoutcomes', 'aggregateoutcomes', 'grades');
00069             if ((int)$CFG->grade_aggregateoutcomes_flag & 2) {
00070                 $mform->setAdvanced('aggregateoutcomes');
00071             }
00072         }
00073 
00074         $mform->addElement('advcheckbox', 'aggregatesubcats', get_string('aggregatesubcats', 'grades'));
00075         $mform->addHelpButton('aggregatesubcats', 'aggregatesubcats', 'grades');
00076 
00077         if ((int)$CFG->grade_aggregatesubcats_flag & 2) {
00078             $mform->setAdvanced('aggregatesubcats');
00079         }
00080 
00081         $options = array(0 => get_string('none'));
00082 
00083         for ($i=1; $i<=20; $i++) {
00084             $options[$i] = $i;
00085         }
00086 
00087         $mform->addElement('select', 'keephigh', get_string('keephigh', 'grades'), $options);
00088         $mform->addHelpButton('keephigh', 'keephigh', 'grades');
00089         if ((int)$CFG->grade_keephigh_flag & 2) {
00090             $mform->setAdvanced('keephigh');
00091         }
00092 
00093         $mform->addElement('select', 'droplow', get_string('droplow', 'grades'), $options);
00094         $mform->addHelpButton('droplow', 'droplow', 'grades');
00095         $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
00096         if ((int)$CFG->grade_droplow_flag & 2) {
00097             $mform->setAdvanced('droplow');
00098         }
00099 
00100         $mform->disabledIf('keephigh', 'droplow', 'noteq', 0);
00101         $mform->disabledIf('droplow', 'keephigh', 'noteq', 0);
00102 
00103         // Grade item settings
00104         // Displayed as Category total to avoid confusion between grade items requiring marking and category totals
00105         $mform->addElement('header', 'general', get_string('categorytotal', 'grades'));
00106 
00107         $mform->addElement('text', 'grade_item_itemname', get_string('categorytotalname', 'grades'));
00108         $mform->setAdvanced('grade_item_itemname');
00109 
00110         $mform->addElement('text', 'grade_item_iteminfo', get_string('iteminfo', 'grades'));
00111         $mform->addHelpButton('grade_item_iteminfo', 'iteminfo', 'grades');
00112 
00113         $mform->addElement('text', 'grade_item_idnumber', get_string('idnumbermod'));
00114         $mform->addHelpButton('grade_item_idnumber', 'idnumbermod');
00115 
00116         $options = array(GRADE_TYPE_NONE=>get_string('typenone', 'grades'),
00117                          GRADE_TYPE_VALUE=>get_string('typevalue', 'grades'),
00118                          GRADE_TYPE_SCALE=>get_string('typescale', 'grades'),
00119                          GRADE_TYPE_TEXT=>get_string('typetext', 'grades'));
00120 
00121         $mform->addElement('select', 'grade_item_gradetype', get_string('gradetype', 'grades'), $options);
00122         $mform->addHelpButton('grade_item_gradetype', 'gradetype', 'grades');
00123         $mform->setDefault('grade_item_gradetype', GRADE_TYPE_VALUE);
00124         $mform->disabledIf('grade_item_gradetype', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
00125 
00126         //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
00127         //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_TEXT);
00128         //$mform->disabledIf('calculation', 'gradetype', 'eq', GRADE_TYPE_NONE);
00129 
00130         $options = array(0=>get_string('usenoscale', 'grades'));
00131         if ($scales = grade_scale::fetch_all_local($COURSE->id)) {
00132             foreach ($scales as $scale) {
00133                 $options[$scale->id] = $scale->get_name();
00134             }
00135         }
00136         if ($scales = grade_scale::fetch_all_global()) {
00137             foreach ($scales as $scale) {
00138                 $options[$scale->id] = $scale->get_name();
00139             }
00140         }
00141         // ugly BC hack - it was possible to use custom scale from other courses :-(
00142         if (!empty($category->grade_item_scaleid) and !isset($options[$category->grade_item_scaleid])) {
00143             if ($scale = grade_scale::fetch(array('id'=>$category->grade_item_scaleid))) {
00144                 $options[$scale->id] = $scale->get_name().' '.get_string('incorrectcustomscale', 'grades');
00145             }
00146         }
00147         $mform->addElement('select', 'grade_item_scaleid', get_string('scale'), $options);
00148         $mform->addHelpButton('grade_item_scaleid', 'typescale', 'grades');
00149         $mform->disabledIf('grade_item_scaleid', 'grade_item_gradetype', 'noteq', GRADE_TYPE_SCALE);
00150         $mform->disabledIf('grade_item_scaleid', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
00151 
00152         $mform->addElement('text', 'grade_item_grademax', get_string('grademax', 'grades'));
00153         $mform->addHelpButton('grade_item_grademax', 'grademax', 'grades');
00154         $mform->disabledIf('grade_item_grademax', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
00155         $mform->disabledIf('grade_item_grademax', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
00156 
00157         $mform->addElement('text', 'grade_item_grademin', get_string('grademin', 'grades'));
00158         $mform->addHelpButton('grade_item_grademin', 'grademin', 'grades');
00159         $mform->disabledIf('grade_item_grademin', 'grade_item_gradetype', 'noteq', GRADE_TYPE_VALUE);
00160         $mform->disabledIf('grade_item_grademin', 'aggregation', 'eq', GRADE_AGGREGATE_SUM);
00161 
00162         $mform->addElement('text', 'grade_item_gradepass', get_string('gradepass', 'grades'));
00163         $mform->addHelpButton('grade_item_gradepass', 'gradepass', 'grades');
00164         $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
00165         $mform->disabledIf('grade_item_gradepass', 'grade_item_gradetype', 'eq', GRADE_TYPE_TEXT);
00166 
00168         $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
00169         $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
00170                          GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
00171                          GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
00172                          GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
00173                          GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
00174                          GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
00175                          GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
00176                          GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
00177                          GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
00178                          GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
00179                          );
00180 
00181         asort($options);
00182 
00183         foreach ($options as $key=>$option) {
00184             if ($key == $default_gradedisplaytype) {
00185                 $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
00186                 break;
00187             }
00188         }
00189         $mform->addElement('select', 'grade_item_display', get_string('gradedisplaytype', 'grades'), $options);
00190         $mform->addHelpButton('grade_item_display', 'gradedisplaytype', 'grades');
00191 
00192         $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
00193         $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
00194         $mform->addElement('select', 'grade_item_decimals', get_string('decimalpoints', 'grades'), $options);
00195         $mform->addHelpButton('grade_item_decimals', 'decimalpoints', 'grades');
00196         $mform->setDefault('grade_item_decimals', -1);
00197         $mform->disabledIf('grade_item_decimals', 'grade_item_display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
00198 
00199         if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
00200             $mform->disabledIf('grade_item_decimals', 'grade_item_display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
00201         }
00202 
00204         // advcheckbox is not compatible with disabledIf!
00205         $mform->addElement('checkbox', 'grade_item_hidden', get_string('hidden', 'grades'));
00206         $mform->addHelpButton('grade_item_hidden', 'hidden', 'grades');
00207         $mform->addElement('date_time_selector', 'grade_item_hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
00208         $mform->disabledIf('grade_item_hidden', 'grade_item_hiddenuntil[off]', 'notchecked');
00209 
00211         $mform->addElement('checkbox', 'grade_item_locked', get_string('locked', 'grades'));
00212         $mform->addHelpButton('grade_item_locked', 'locked', 'grades');
00213 
00214         $mform->addElement('date_time_selector', 'grade_item_locktime', get_string('locktime', 'grades'), array('optional'=>true));
00215         $mform->disabledIf('grade_item_locktime', 'grade_item_gradetype', 'eq', GRADE_TYPE_NONE);
00216 
00218         $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
00219 
00220         $options = array();
00221         $default = '';
00222         $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
00223 
00224         foreach ($categories as $cat) {
00225             $cat->apply_forced_settings();
00226             $options[$cat->id] = $cat->get_name();
00227             if ($cat->is_course_category()) {
00228                 $default = $cat->id;
00229             }
00230         }
00231 
00232         if (count($categories) > 1) {
00233             $mform->addElement('select', 'parentcategory', get_string('parentcategory', 'grades'), $options);
00234             $mform->addElement('static', 'currentparentaggregation', get_string('currentparentaggregation', 'grades'));
00235         }
00236 
00237         // hidden params
00238         $mform->addElement('hidden', 'id', 0);
00239         $mform->setType('id', PARAM_INT);
00240 
00241         $mform->addElement('hidden', 'courseid', 0);
00242         $mform->setType('courseid', PARAM_INT);
00243 
00245         $gpr = $this->_customdata['gpr'];
00246         $gpr->add_mform_elements($mform);
00247 
00249         if (isset($CFG->grade_item_advanced)) {
00250             $advanced = explode(',', $CFG->grade_item_advanced);
00251             foreach ($advanced as $el) {
00252                 $el = 'grade_item_'.$el;
00253                 if ($mform->elementExists($el)) {
00254                     $mform->setAdvanced($el);
00255                 }
00256             }
00257         }
00258 
00259 //-------------------------------------------------------------------------------
00260         // buttons
00261         $this->add_action_buttons();
00262 //-------------------------------------------------------------------------------
00263         $this->set_data($category);
00264     }
00265 
00266 
00268     function definition_after_data() {
00269         global $CFG, $COURSE;
00270 
00271         $mform =& $this->_form;
00272 
00273         $somecat = new grade_category();
00274 
00275         foreach ($somecat->forceable as $property) {
00276             if ((int)$CFG->{"grade_{$property}_flag"} & 1) {
00277                 if ($mform->elementExists($property)) {
00278                     if (empty($CFG->grade_hideforcedsettings)) {
00279                         $mform->hardFreeze($property);
00280                     } else {
00281                         if ($mform->elementExists($property)) {
00282                             $mform->removeElement($property);
00283                         }
00284                     }
00285                 }
00286             }
00287         }
00288 
00289         if ($CFG->grade_droplow > 0) {
00290             if ($mform->elementExists('keephigh')) {
00291                 $mform->removeElement('keephigh');
00292             }
00293         } else if ($CFG->grade_keephigh > 0) {
00294             if ($mform->elementExists('droplow')) {
00295                 $mform->removeElement('droplow');
00296             }
00297         }
00298 
00299         if ($id = $mform->getElementValue('id')) {
00300             $grade_category = grade_category::fetch(array('id'=>$id));
00301             $grade_item = $grade_category->load_grade_item();
00302 
00303             // remove agg coef if not used
00304             if ($grade_category->is_course_category()) {
00305                 if ($mform->elementExists('parentcategory')) {
00306                     $mform->removeElement('parentcategory');
00307                 }
00308                 if ($mform->elementExists('currentparentaggregation')) {
00309                     $mform->removeElement('currentparentaggregation');
00310                 }
00311 
00312             } else {
00313                 // if we wanted to change parent of existing category - we would have to verify there are no circular references in parents!!!
00314                 if ($mform->elementExists('parentcategory')) {
00315                     $mform->hardFreeze('parentcategory');
00316                 }
00317                 $parent_cat = $grade_category->get_parent_category();
00318                 $mform->setDefault('currentparentaggregation', $this->aggregation_options[$parent_cat->aggregation]);
00319 
00320             }
00321 
00322             if ($grade_item->is_calculated()) {
00323                 // following elements are ignored when calculation formula used
00324                 if ($mform->elementExists('aggregation')) {
00325                     $mform->removeElement('aggregation');
00326                 }
00327                 if ($mform->elementExists('keephigh')) {
00328                     $mform->removeElement('keephigh');
00329                 }
00330                 if ($mform->elementExists('droplow')) {
00331                     $mform->removeElement('droplow');
00332                 }
00333                 if ($mform->elementExists('aggregateonlygraded')) {
00334                     $mform->removeElement('aggregateonlygraded');
00335                 }
00336                 if ($mform->elementExists('aggregateoutcomes')) {
00337                     $mform->removeElement('aggregateoutcomes');
00338                 }
00339                 if ($mform->elementExists('aggregatesubcats')) {
00340                     $mform->removeElement('aggregatesubcats');
00341                 }
00342             }
00343 
00344             // If it is a course category, remove the "required" rule from the "fullname" element
00345             if ($grade_category->is_course_category()) {
00346                 unset($mform->_rules['fullname']);
00347                 $key = array_search('fullname', $mform->_required);
00348                 unset($mform->_required[$key]);
00349             }
00350 
00351             // If it is a course category and its fullname is ?, show an empty field
00352             if ($grade_category->is_course_category() && $mform->getElementValue('fullname') == '?') {
00353                 $mform->setDefault('fullname', '');
00354             }
00355             // remove unwanted aggregation options
00356             if ($mform->elementExists('aggregation')) {
00357                 $allaggoptions = array_keys($this->aggregation_options);
00358                 $agg_el =& $mform->getElement('aggregation');
00359                 $visible = explode(',', $CFG->grade_aggregations_visible);
00360                 if (!is_null($grade_category->aggregation)) {
00361                     // current type is always visible
00362                     $visible[] = $grade_category->aggregation;
00363                 }
00364                 foreach ($allaggoptions as $type) {
00365                     if (!in_array($type, $visible)) {
00366                         $agg_el->removeOption($type);
00367                     }
00368                 }
00369             }
00370 
00371         } else {
00372             // adding new category
00373             if ($mform->elementExists('currentparentaggregation')) {
00374                 $mform->removeElement('currentparentaggregation');
00375             }
00376             // remove unwanted aggregation options
00377             if ($mform->elementExists('aggregation')) {
00378                 $allaggoptions = array_keys($this->aggregation_options);
00379                 $agg_el =& $mform->getElement('aggregation');
00380                 $visible = explode(',', $CFG->grade_aggregations_visible);
00381                 foreach ($allaggoptions as $type) {
00382                     if (!in_array($type, $visible)) {
00383                         $agg_el->removeOption($type);
00384                     }
00385                 }
00386             }
00387         }
00388 
00389 
00390         // no parent header for course category
00391         if (!$mform->elementExists('parentcategory')) {
00392             $mform->removeElement('headerparent');
00393         }
00394 
00396         if ($id = $mform->getElementValue('id')) {
00397             $grade_category = grade_category::fetch(array('id'=>$id));
00398             $grade_item = $grade_category->load_grade_item();
00399 
00400             $mform->setDefault('grade_item_hidden', (int) $grade_item->hidden);
00401 
00402             if ($grade_item->is_outcome_item()) {
00403                 // we have to prevent incompatible modifications of outcomes if outcomes disabled
00404                 $mform->removeElement('grade_item_grademax');
00405                 $mform->removeElement('grade_item_grademin');
00406                 $mform->removeElement('grade_item_gradetype');
00407                 $mform->removeElement('grade_item_display');
00408                 $mform->removeElement('grade_item_decimals');
00409                 $mform->hardFreeze('grade_item_scaleid');
00410             }
00411 
00412             //remove the aggregation coef element if not needed
00413             if ($grade_item->is_course_item()) {
00414                 if ($mform->elementExists('grade_item_aggregationcoef')) {
00415                     $mform->removeElement('grade_item_aggregationcoef');
00416                 }
00417 
00418             } else {
00419                 if ($grade_item->is_category_item()) {
00420                     $category = $grade_item->get_item_category();
00421                     $parent_category = $category->get_parent_category();
00422                 } else {
00423                     $parent_category = $grade_item->get_parent_category();
00424                 }
00425 
00426                 $parent_category->apply_forced_settings();
00427 
00428                 if (!$parent_category->is_aggregationcoef_used()) {
00429                     if ($mform->elementExists('grade_item_aggregationcoef')) {
00430                         $mform->removeElement('grade_item_aggregationcoef');
00431                     }
00432                 } else {
00433 
00434                     $coefstring = $grade_item->get_coefstring();
00435 
00436                     if ($coefstring == 'aggregationcoefextrasum') {
00437                         // advcheckbox is not compatible with disabledIf!
00438                         $element =& $mform->createElement('checkbox', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
00439                     } else {
00440                         $element =& $mform->createElement('text', 'grade_item_aggregationcoef', get_string($coefstring, 'grades'));
00441                     }
00442                     $mform->insertElementBefore($element, 'parentcategory');
00443                     $mform->addHelpButton('grade_item_aggregationcoef', $coefstring, 'grades');
00444                 }
00445             }
00446         }
00447     }
00448 
00450     function validation($data, $files) {
00451         global $COURSE;
00452 
00453         $errors = parent::validation($data, $files);
00454 
00455         if (array_key_exists('grade_item_gradetype', $data) and $data['grade_item_gradetype'] == GRADE_TYPE_SCALE) {
00456             if (empty($data['grade_item_scaleid'])) {
00457                 $errors['grade_item_scaleid'] = get_string('missingscale', 'grades');
00458             }
00459         }
00460         if (array_key_exists('grade_item_grademin', $data) and array_key_exists('grade_item_grademax', $data)) {
00461             if (($data['grade_item_grademax'] != 0 OR $data['grade_item_grademin'] != 0) AND
00462                 ($data['grade_item_grademax'] == $data['grade_item_grademin'] OR
00463                  $data['grade_item_grademax'] < $data['grade_item_grademin'])) {
00464                  $errors['grade_item_grademin'] = get_string('incorrectminmax', 'grades');
00465                  $errors['grade_item_grademax'] = get_string('incorrectminmax', 'grades');
00466              }
00467         }
00468 
00469         return $errors;
00470     }
00471 }
00472 
00473 
 All Data Structures Namespaces Files Functions Variables Enumerations