[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/grade/edit/tree/ -> item_form.php (source)

   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  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  27  }
  28  
  29  require_once $CFG->libdir.'/formslib.php';
  30  
  31  class 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          if ((bool) get_config('moodle', 'grade_report_showmin')) {
  94              $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
  95              $mform->addHelpButton('grademin', 'grademin', 'grades');
  96              $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
  97              $mform->setType('grademin', PARAM_RAW);
  98          }
  99  
 100          $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
 101          $mform->addHelpButton('gradepass', 'gradepass', 'grades');
 102          $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
 103          $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 104          $mform->setType('gradepass', PARAM_RAW);
 105  
 106          $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
 107          $mform->addHelpButton('multfactor', 'multfactor', 'grades');
 108          $mform->setAdvanced('multfactor');
 109          $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
 110          $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 111          $mform->setType('multfactor', PARAM_RAW);
 112  
 113          $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
 114          $mform->addHelpButton('plusfactor', 'plusfactor', 'grades');
 115          $mform->setAdvanced('plusfactor');
 116          $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
 117          $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 118          $mform->setType('plusfactor', PARAM_RAW);
 119  
 120          /// grade display prefs
 121          $default_gradedisplaytype = grade_get_setting($COURSE->id, 'displaytype', $CFG->grade_displaytype);
 122          $options = array(GRADE_DISPLAY_TYPE_DEFAULT            => get_string('default', 'grades'),
 123                           GRADE_DISPLAY_TYPE_REAL               => get_string('real', 'grades'),
 124                           GRADE_DISPLAY_TYPE_PERCENTAGE         => get_string('percentage', 'grades'),
 125                           GRADE_DISPLAY_TYPE_LETTER             => get_string('letter', 'grades'),
 126                           GRADE_DISPLAY_TYPE_REAL_PERCENTAGE    => get_string('realpercentage', 'grades'),
 127                           GRADE_DISPLAY_TYPE_REAL_LETTER        => get_string('realletter', 'grades'),
 128                           GRADE_DISPLAY_TYPE_LETTER_REAL        => get_string('letterreal', 'grades'),
 129                           GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE  => get_string('letterpercentage', 'grades'),
 130                           GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER  => get_string('percentageletter', 'grades'),
 131                           GRADE_DISPLAY_TYPE_PERCENTAGE_REAL    => get_string('percentagereal', 'grades')
 132                           );
 133  
 134          asort($options);
 135  
 136          foreach ($options as $key=>$option) {
 137              if ($key == $default_gradedisplaytype) {
 138                  $options[GRADE_DISPLAY_TYPE_DEFAULT] = get_string('defaultprev', 'grades', $option);
 139                  break;
 140              }
 141          }
 142          $mform->addElement('select', 'display', get_string('gradedisplaytype', 'grades'), $options);
 143          $mform->addHelpButton('display', 'gradedisplaytype', 'grades');
 144  
 145          $default_gradedecimals = grade_get_setting($COURSE->id, 'decimalpoints', $CFG->grade_decimalpoints);
 146          $options = array(-1=>get_string('defaultprev', 'grades', $default_gradedecimals), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
 147          $mform->addElement('select', 'decimals', get_string('decimalpoints', 'grades'), $options);
 148          $mform->addHelpButton('decimals', 'decimalpoints', 'grades');
 149          $mform->setDefault('decimals', -1);
 150          $mform->disabledIf('decimals', 'display', 'eq', GRADE_DISPLAY_TYPE_LETTER);
 151          if ($default_gradedisplaytype == GRADE_DISPLAY_TYPE_LETTER) {
 152              $mform->disabledIf('decimals', 'display', "eq", GRADE_DISPLAY_TYPE_DEFAULT);
 153          }
 154  
 155          /// hiding
 156          if ($item->cancontrolvisibility) {
 157              // advcheckbox is not compatible with disabledIf!
 158              $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
 159              $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
 160              $mform->disabledIf('hidden', 'hiddenuntil[off]', 'notchecked');
 161          } else {
 162              $mform->addElement('static', 'hidden', get_string('hidden', 'grades'),
 163                      get_string('componentcontrolsvisibility', 'grades'));
 164              // Unset hidden to avoid data override.
 165              unset($item->hidden);
 166          }
 167          $mform->addHelpButton('hidden', 'hidden', 'grades');
 168  
 169          /// locking
 170          $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
 171          $mform->addHelpButton('locked', 'locked', 'grades');
 172  
 173          $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
 174          $mform->disabledIf('locktime', 'gradetype', 'eq', GRADE_TYPE_NONE);
 175  
 176  /// parent category related settings
 177          $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
 178  
 179          $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
 180          $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
 181          $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_NONE);
 182          $mform->disabledIf('weightoverride', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 183  
 184          $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
 185          $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
 186          $mform->setType('aggregationcoef2', PARAM_RAW);
 187          $mform->disabledIf('aggregationcoef2', 'weightoverride');
 188          $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_NONE);
 189          $mform->disabledIf('aggregationcoef2', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 190  
 191          $options = array();
 192          $coefstring = '';
 193          $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
 194  
 195          foreach ($categories as $cat) {
 196              $cat->apply_forced_settings();
 197              $options[$cat->id] = $cat->get_name();
 198          }
 199  
 200          if (count($categories) > 1) {
 201              $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
 202          }
 203  
 204  /// hidden params
 205          $mform->addElement('hidden', 'id', 0);
 206          $mform->setType('id', PARAM_INT);
 207  
 208          $mform->addElement('hidden', 'courseid', $COURSE->id);
 209          $mform->setType('courseid', PARAM_INT);
 210  
 211          $mform->addElement('hidden', 'itemtype', 'manual'); // all new items are manual only
 212          $mform->setType('itemtype', PARAM_ALPHA);
 213  
 214  /// add return tracking info
 215          $gpr = $this->_customdata['gpr'];
 216          $gpr->add_mform_elements($mform);
 217  
 218  /// mark advanced according to site settings
 219          if (isset($CFG->grade_item_advanced)) {
 220              $advanced = explode(',', $CFG->grade_item_advanced);
 221              foreach ($advanced as $el) {
 222                  if ($mform->elementExists($el)) {
 223                      $mform->setAdvanced($el);
 224                  }
 225              }
 226          }
 227  //-------------------------------------------------------------------------------
 228          // buttons
 229          $this->add_action_buttons();
 230  //-------------------------------------------------------------------------------
 231          $this->set_data($item);
 232      }
 233  
 234  
 235  /// tweak the form - depending on existing data
 236      function definition_after_data() {
 237          global $CFG, $COURSE;
 238  
 239          $mform =& $this->_form;
 240  
 241          if ($id = $mform->getElementValue('id')) {
 242              $grade_item = grade_item::fetch(array('id'=>$id));
 243  
 244              if (!$grade_item->is_raw_used()) {
 245                  $mform->removeElement('plusfactor');
 246                  $mform->removeElement('multfactor');
 247              }
 248  
 249              if ($grade_item->is_outcome_item()) {
 250                  // we have to prevent incompatible modifications of outcomes if outcomes disabled
 251                  $mform->removeElement('grademax');
 252                  if ($mform->elementExists('grademin')) {
 253                      $mform->removeElement('grademin');
 254                  }
 255                  $mform->removeElement('gradetype');
 256                  $mform->removeElement('display');
 257                  $mform->removeElement('decimals');
 258                  $mform->hardFreeze('scaleid');
 259  
 260              } else {
 261                  if ($grade_item->is_external_item()) {
 262                      // following items are set up from modules and should not be overrided by user
 263                      if ($mform->elementExists('grademin')) {
 264                          // The site setting grade_report_showmin may have prevented grademin being added to the form.
 265                          $mform->hardFreeze('grademin');
 266                      }
 267                      $mform->hardFreeze('itemname,gradetype,grademax,scaleid');
 268                      if ($grade_item->itemnumber == 0) {
 269                          // the idnumber of grade itemnumber 0 is synced with course_modules
 270                          $mform->hardFreeze('idnumber');
 271                      }
 272                      //$mform->removeElement('calculation');
 273                  }
 274              }
 275  
 276              // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
 277              if ($mform->elementExists('parentcategory')) {
 278                  $mform->hardFreeze('parentcategory');
 279              }
 280  
 281              $parent_category = $grade_item->get_parent_category();
 282              $parent_category->apply_forced_settings();
 283  
 284              if (!$parent_category->is_aggregationcoef_used()) {
 285                  if ($mform->elementExists('aggregationcoef')) {
 286                      $mform->removeElement('aggregationcoef');
 287                  }
 288  
 289              } else {
 290                  $coefstring = $grade_item->get_coefstring();
 291  
 292                  if ($coefstring !== '') {
 293                      if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
 294                          // advcheckbox is not compatible with disabledIf!
 295                          $coefstring = 'aggregationcoefextrasum';
 296                          $element =& $mform->createElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
 297                      } else {
 298                          $element =& $mform->createElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
 299                      }
 300                      if ($mform->elementExists('parentcategory')) {
 301                          $mform->insertElementBefore($element, 'parentcategory');
 302                      } else {
 303                          $mform->insertElementBefore($element, 'id');
 304                      }
 305                      $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
 306                  }
 307                  $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_NONE);
 308                  $mform->disabledIf('aggregationcoef', 'gradetype', 'eq', GRADE_TYPE_TEXT);
 309                  $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $parent_category->id);
 310              }
 311  
 312              // Remove fields used by natural weighting if the parent category is not using natural weighting.
 313              // Or if the item is a scale and scales are not used in aggregation.
 314              if ($parent_category->aggregation != GRADE_AGGREGATE_SUM
 315                      || (empty($CFG->grade_includescalesinaggregation) && $grade_item->gradetype == GRADE_TYPE_SCALE)) {
 316                  if ($mform->elementExists('weightoverride')) {
 317                      $mform->removeElement('weightoverride');
 318                  }
 319                  if ($mform->elementExists('aggregationcoef2')) {
 320                      $mform->removeElement('aggregationcoef2');
 321                  }
 322              }
 323  
 324              if ($category = $grade_item->get_item_category()) {
 325                  if ($category->aggregation == GRADE_AGGREGATE_SUM) {
 326                      if ($mform->elementExists('gradetype')) {
 327                          $mform->hardFreeze('gradetype');
 328                      }
 329                      if ($mform->elementExists('grademin')) {
 330                          $mform->hardFreeze('grademin');
 331                      }
 332                      if ($mform->elementExists('grademax')) {
 333                          $mform->hardFreeze('grademax');
 334                      }
 335                      if ($mform->elementExists('scaleid')) {
 336                          $mform->removeElement('scaleid');
 337                      }
 338                  }
 339              }
 340  
 341          } else {
 342              // all new items are manual, children of course category
 343              $mform->removeElement('plusfactor');
 344              $mform->removeElement('multfactor');
 345          }
 346  
 347          // no parent header for course category
 348          if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
 349              $mform->removeElement('headerparent');
 350          }
 351      }
 352  
 353  /// perform extra validation before submission
 354      function validation($data, $files) {
 355          global $COURSE;
 356  
 357          $errors = parent::validation($data, $files);
 358  
 359          if (array_key_exists('idnumber', $data)) {
 360              if ($data['id']) {
 361                  $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
 362                  if ($grade_item->itemtype == 'mod') {
 363                      $cm = get_coursemodule_from_instance($grade_item->itemmodule, $grade_item->iteminstance, $grade_item->courseid);
 364                  } else {
 365                      $cm = null;
 366                  }
 367              } else {
 368                  $grade_item = null;
 369                  $cm = null;
 370              }
 371              if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, $cm)) {
 372                  $errors['idnumber'] = get_string('idnumbertaken');
 373              }
 374          }
 375  
 376          if (array_key_exists('gradetype', $data) and $data['gradetype'] == GRADE_TYPE_SCALE) {
 377              if (empty($data['scaleid'])) {
 378                  $errors['scaleid'] = get_string('missingscale', 'grades');
 379              }
 380          }
 381  
 382          if (array_key_exists('grademin', $data) and array_key_exists('grademax', $data)) {
 383              if ($data['grademax'] == $data['grademin'] or $data['grademax'] < $data['grademin']) {
 384                  $errors['grademin'] = get_string('incorrectminmax', 'grades');
 385                  $errors['grademax'] = get_string('incorrectminmax', 'grades');
 386              }
 387          }
 388  
 389          return $errors;
 390      }
 391  
 392  }
 393  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1