Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 403] [Versions 39 and 311]

   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 to allow the creation and editing of outcome grade items
  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_outcomeitem_form extends moodleform {
  32      function definition() {
  33          global $COURSE, $CFG;
  34  
  35          $mform =& $this->_form;
  36  
  37  /// visible elements
  38          $mform->addElement('header', 'general', get_string('gradeoutcomeitem', 'grades'));
  39  
  40          $mform->addElement('text', 'itemname', get_string('itemname', 'grades'));
  41          $mform->addRule('itemname', get_string('required'), 'required', null, 'client');
  42          $mform->setType('itemname', PARAM_TEXT);
  43  
  44          $mform->addElement('text', 'iteminfo', get_string('iteminfo', 'grades'));
  45          $mform->addHelpButton('iteminfo', 'iteminfo', 'grades');
  46          $mform->setType('iteminfo', PARAM_TEXT);
  47  
  48          $mform->addElement('text', 'idnumber', get_string('idnumbermod'));
  49          $mform->addHelpButton('idnumber', 'idnumbermod');
  50          $mform->setType('idnumber', PARAM_RAW);
  51  
  52          // allow setting of outcomes on module items too
  53          $options = array();
  54          if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
  55              foreach ($outcomes as $outcome) {
  56                  $options[$outcome->id] = $outcome->get_name();
  57              }
  58          }
  59          $mform->addElement('selectwithlink', 'outcomeid', get_string('outcome', 'grades'), $options, null,
  60              array('link' => $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$COURSE->id, 'label' => get_string('outcomeassigntocourse', 'grades')));
  61          $mform->addHelpButton('outcomeid', 'outcome', 'grades');
  62          $mform->addRule('outcomeid', get_string('required'), 'required');
  63  
  64          $options = array(0=>get_string('none'));
  65          if ($coursemods = get_course_mods($COURSE->id)) {
  66              foreach ($coursemods as $coursemod) {
  67                  if ($mod = get_coursemodule_from_id($coursemod->modname, $coursemod->id)) {
  68                      $options[$coursemod->id] = format_string($mod->name);
  69                  }
  70              }
  71          }
  72          $mform->addElement('select', 'cmid', get_string('linkedactivity', 'grades'), $options);
  73          $mform->addHelpButton('cmid', 'linkedactivity', 'grades');
  74          $mform->setDefault('cmid', 0);
  75  
  76          /// hiding
  77          /// advcheckbox is not compatible with disabledIf !!
  78          $mform->addElement('checkbox', 'hidden', get_string('hidden', 'grades'));
  79          $mform->addHelpButton('hidden', 'hidden', 'grades');
  80          $mform->addElement('date_time_selector', 'hiddenuntil', get_string('hiddenuntil', 'grades'), array('optional'=>true));
  81          $mform->disabledIf('hidden', 'hiddenuntil[enabled]', 'checked');
  82  
  83          //locking
  84          $mform->addElement('advcheckbox', 'locked', get_string('locked', 'grades'));
  85          $mform->addHelpButton('locked', 'locked', 'grades');
  86          $mform->addElement('date_time_selector', 'locktime', get_string('locktime', 'grades'), array('optional'=>true));
  87  
  88  /// parent category related settings
  89          $mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));
  90  
  91          $mform->addElement('advcheckbox', 'weightoverride', get_string('adjustedweight', 'grades'));
  92          $mform->addHelpButton('weightoverride', 'weightoverride', 'grades');
  93  
  94          $mform->addElement('text', 'aggregationcoef2', get_string('weight', 'grades'));
  95          $mform->addHelpButton('aggregationcoef2', 'weight', 'grades');
  96          $mform->setType('aggregationcoef2', PARAM_RAW);
  97          $mform->disabledIf('aggregationcoef2', 'weightoverride');
  98  
  99          $options = array();
 100          $default = '';
 101          $coefstring = '';
 102          $categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));
 103          foreach ($categories as $cat) {
 104              $cat->apply_forced_settings();
 105              $options[$cat->id] = $cat->get_name();
 106              if ($cat->is_course_category()) {
 107                  $default = $cat->id;
 108              }
 109              if ($cat->is_aggregationcoef_used()) {
 110                  if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
 111                      $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
 112  
 113                  } else if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
 114                      $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
 115  
 116                  } else if ($cat->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
 117                      $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextraweight') ? 'aggregationcoefextraweight' : 'aggregationcoef';
 118  
 119                  } else if ($cat->aggregation == GRADE_AGGREGATE_SUM) {
 120                      $coefstring = ($coefstring=='' or $coefstring=='aggregationcoefextrasum') ? 'aggregationcoefextrasum' : 'aggregationcoef';
 121  
 122                  } else {
 123                      $coefstring = 'aggregationcoef';
 124                  }
 125              } else {
 126                  $mform->disabledIf('aggregationcoef', 'parentcategory', 'eq', $cat->id);
 127              }
 128          }
 129  
 130          if (count($categories) > 1) {
 131              $mform->addElement('select', 'parentcategory', get_string('gradecategory', 'grades'), $options);
 132              $mform->disabledIf('parentcategory', 'cmid', 'noteq', 0);
 133          }
 134  
 135          if ($coefstring !== '') {
 136              if ($coefstring == 'aggregationcoefextrasum' || $coefstring == 'aggregationcoefextraweightsum') {
 137                  // advcheckbox is not compatible with disabledIf!
 138                  $coefstring = 'aggregationcoefextrasum';
 139                  $mform->addElement('checkbox', 'aggregationcoef', get_string($coefstring, 'grades'));
 140              } else {
 141                  $mform->addElement('text', 'aggregationcoef', get_string($coefstring, 'grades'));
 142              }
 143              $mform->addHelpButton('aggregationcoef', $coefstring, 'grades');
 144          }
 145  
 146  /// hidden params
 147          $mform->addElement('hidden', 'id', 0);
 148          $mform->setType('id', PARAM_INT);
 149  
 150          $mform->addElement('hidden', 'courseid', $COURSE->id);
 151          $mform->setType('courseid', PARAM_INT);
 152  
 153  /// add return tracking info
 154          $gpr = $this->_customdata['gpr'];
 155          $gpr->add_mform_elements($mform);
 156  
 157  /// mark advanced according to site settings
 158          if (isset($CFG->grade_item_advanced)) {
 159              $advanced = explode(',', $CFG->grade_item_advanced);
 160              foreach ($advanced as $el) {
 161                  if ($mform->elementExists($el)) {
 162                      $mform->setAdvanced($el);
 163                  }
 164              }
 165          }
 166  //-------------------------------------------------------------------------------
 167          // buttons
 168          $this->add_action_buttons();
 169      }
 170  
 171  
 172  /// tweak the form - depending on existing data
 173      function definition_after_data() {
 174          global $CFG, $COURSE;
 175  
 176          $mform =& $this->_form;
 177  
 178          if ($id = $mform->getElementValue('id')) {
 179              $grade_item = grade_item::fetch(array('id'=>$id));
 180  
 181              //remove the aggregation coef element if not needed
 182              if ($grade_item->is_course_item()) {
 183                  if ($mform->elementExists('parentcategory')) {
 184                      $mform->removeElement('parentcategory');
 185                  }
 186                  if ($mform->elementExists('aggregationcoef')) {
 187                      $mform->removeElement('aggregationcoef');
 188                  }
 189  
 190              } else {
 191                  // if we wanted to change parent of existing item - we would have to verify there are no circular references in parents!!!
 192                  if ($mform->elementExists('parentcategory')) {
 193                      $mform->hardFreeze('parentcategory');
 194                  }
 195  
 196                  if ($grade_item->is_category_item()) {
 197                      $category = $grade_item->get_item_category();
 198                      $parent_category = $category->get_parent_category();
 199                  } else {
 200                      $parent_category = $grade_item->get_parent_category();
 201                  }
 202  
 203                  $parent_category->apply_forced_settings();
 204  
 205                  if (!$parent_category->is_aggregationcoef_used() || !$parent_category->aggregateoutcomes) {
 206                      if ($mform->elementExists('aggregationcoef')) {
 207                          $mform->removeElement('aggregationcoef');
 208                      }
 209                  } else {
 210                      //fix label if needed
 211                      $agg_el =& $mform->getElement('aggregationcoef');
 212                      $aggcoef = '';
 213                      if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
 214                          $aggcoef = 'aggregationcoefweight';
 215  
 216                      } else if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
 217                          $aggcoef = 'aggregationcoefextrasum';
 218  
 219                      } else if ($parent_category->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN) {
 220                          $aggcoef = 'aggregationcoefextraweight';
 221  
 222                      } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
 223                          $aggcoef = 'aggregationcoefextrasum';
 224                      }
 225  
 226                      if ($aggcoef !== '') {
 227                          $agg_el->setLabel(get_string($aggcoef, 'grades'));
 228                          $mform->addHelpButton('aggregationcoef', $aggcoef, 'grades');
 229                      }
 230                  }
 231  
 232                  // Remove the natural weighting fields for other aggregations,
 233                  // or when the category does not aggregate outcomes.
 234                  if ($parent_category->aggregation != GRADE_AGGREGATE_SUM ||
 235                          !$parent_category->aggregateoutcomes) {
 236                      if ($mform->elementExists('weightoverride')) {
 237                          $mform->removeElement('weightoverride');
 238                      }
 239                      if ($mform->elementExists('aggregationcoef2')) {
 240                          $mform->removeElement('aggregationcoef2');
 241                      }
 242                  }
 243              }
 244  
 245          }
 246  
 247          // no parent header for course category
 248          if (!$mform->elementExists('aggregationcoef') and !$mform->elementExists('parentcategory')) {
 249              $mform->removeElement('headerparent');
 250          }
 251      }
 252  
 253  
 254  /// perform extra validation before submission
 255      function validation($data, $files) {
 256          global $COURSE;
 257  
 258          $errors = parent::validation($data, $files);
 259  
 260          if (array_key_exists('idnumber', $data)) {
 261              if ($data['id']) {
 262                  $grade_item = new grade_item(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
 263              } else {
 264                  $grade_item = null;
 265              }
 266              if (!grade_verify_idnumber($data['idnumber'], $COURSE->id, $grade_item, null)) {
 267                  $errors['idnumber'] = get_string('idnumbertaken');
 268              }
 269          }
 270  
 271          return $errors;
 272      }
 273  
 274  }
 275