Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Edit the grade options for an individual grade category
  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  require_once '../../../config.php';
  26  require_once $CFG->dirroot.'/grade/lib.php';
  27  require_once $CFG->dirroot.'/grade/report/lib.php';
  28  require_once  'category_form.php';
  29  
  30  $courseid = required_param('courseid', PARAM_INT);
  31  $id       = optional_param('id', 0, PARAM_INT); // grade_category->id
  32  
  33  $url = new moodle_url('/grade/edit/tree/category.php', array('courseid'=>$courseid));
  34  if ($id !== 0) {
  35      $url->param('id', $id);
  36  }
  37  $PAGE->set_url($url);
  38  $PAGE->set_pagelayout('admin');
  39  navigation_node::override_active_url(new moodle_url('/grade/edit/tree/index.php',
  40      array('id'=>$courseid)));
  41  
  42  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  43      print_error('invalidcourseid');
  44  }
  45  
  46  require_login($course);
  47  $context = context_course::instance($course->id);
  48  require_capability('moodle/grade:manage', $context);
  49  
  50  // default return url
  51  $gpr = new grade_plugin_return();
  52  $returnurl = $gpr->get_return_url('index.php?id='.$course->id);
  53  
  54  
  55  $heading = get_string('categoryedit', 'grades');
  56  
  57  if ($id) {
  58      if (!$grade_category = grade_category::fetch(array('id'=>$id, 'courseid'=>$course->id))) {
  59          print_error('invalidcategory');
  60      }
  61      $grade_category->apply_forced_settings();
  62      $category = $grade_category->get_record_data();
  63      // set parent
  64      $category->parentcategory = $grade_category->parent;
  65      $grade_item = $grade_category->load_grade_item();
  66      // nomalize coef values if needed
  67      $parent_category = $grade_category->get_parent_category();
  68  
  69      foreach ($grade_item->get_record_data() as $key => $value) {
  70          $category->{"grade_item_$key"} = $value;
  71      }
  72  
  73      $decimalpoints = $grade_item->get_decimals();
  74  
  75      $category->grade_item_grademax   = format_float($category->grade_item_grademax, $decimalpoints);
  76      $category->grade_item_grademin   = format_float($category->grade_item_grademin, $decimalpoints);
  77      $category->grade_item_gradepass  = format_float($category->grade_item_gradepass, $decimalpoints);
  78      $category->grade_item_multfactor = format_float($category->grade_item_multfactor, 4);
  79      $category->grade_item_plusfactor = format_float($category->grade_item_plusfactor, 4);
  80      $category->grade_item_aggregationcoef2 = format_float($category->grade_item_aggregationcoef2 * 100.0, 4);
  81  
  82      if (!$parent_category) {
  83          // keep as is
  84      } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  85          $category->grade_item_aggregationcoef = $category->grade_item_aggregationcoef == 0 ? 0 : 1;
  86      } else {
  87          $category->grade_item_aggregationcoef = format_float($category->grade_item_aggregationcoef, 4);
  88      }
  89      // Check to see if the gradebook is frozen. This allows grades to not be altered at all until a user verifies that they
  90      // wish to update the grades.
  91      $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
  92      // Stick with the original code if the grade book is frozen.
  93      if ($gradebookcalculationsfreeze && (int)$gradebookcalculationsfreeze <= 20150627) {
  94          if ($category->aggregation == GRADE_AGGREGATE_SUM) {
  95              // Input fields for grademin and grademax are disabled for the "Natural" category,
  96              // this means they will be ignored if user does not change aggregation method.
  97              // But if user does change aggregation method the default values should be used.
  98              $category->grademax = 100;
  99              $category->grade_item_grademax = 100;
 100              $category->grademin = 0;
 101              $category->grade_item_grademin = 0;
 102          }
 103      } else {
 104          if ($category->aggregation == GRADE_AGGREGATE_SUM && !$grade_item->is_calculated()) {
 105              // Input fields for grademin and grademax are disabled for the "Natural" category,
 106              // this means they will be ignored if user does not change aggregation method.
 107              // But if user does change aggregation method the default values should be used.
 108              // This does not apply to calculated category totals.
 109              $category->grademax = 100;
 110              $category->grade_item_grademax = 100;
 111              $category->grademin = 0;
 112              $category->grade_item_grademin = 0;
 113          }
 114      }
 115  
 116  } else {
 117      $heading = get_string('newcategory', 'grades');
 118      $grade_category = new grade_category(array('courseid'=>$courseid), false);
 119      $grade_category->apply_default_settings();
 120      $grade_category->apply_forced_settings();
 121  
 122      $category = $grade_category->get_record_data();
 123  
 124      $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false);
 125      foreach ($grade_item->get_record_data() as $key => $value) {
 126          $category->{"grade_item_$key"} = $value;
 127      }
 128  }
 129  
 130  $mform = new edit_category_form(null, array('current'=>$category, 'gpr'=>$gpr));
 131  
 132  if ($mform->is_cancelled()) {
 133      redirect($returnurl);
 134  
 135  } else if ($data = $mform->get_data(false)) {
 136      // If no fullname is entered for a course category, put ? in the DB
 137      if (!isset($data->fullname) || $data->fullname == '') {
 138          $data->fullname = '?';
 139      }
 140  
 141      if (!isset($data->aggregateonlygraded)) {
 142          $data->aggregateonlygraded = 0;
 143      }
 144      if (!isset($data->aggregateoutcomes)) {
 145          $data->aggregateoutcomes = 0;
 146      }
 147      grade_category::set_properties($grade_category, $data);
 148  
 149      /// CATEGORY
 150      if (empty($grade_category->id)) {
 151          $grade_category->insert();
 152  
 153      } else {
 154          $grade_category->update();
 155      }
 156  
 157      /// GRADE ITEM
 158      // grade item data saved with prefix "grade_item_"
 159      $itemdata = new stdClass();
 160      foreach ($data as $k => $v) {
 161          if (preg_match('/grade_item_(.*)/', $k, $matches)) {
 162              $itemdata->{$matches[1]} = $v;
 163          }
 164      }
 165  
 166      if (!isset($itemdata->aggregationcoef)) {
 167          $itemdata->aggregationcoef = 0;
 168      }
 169  
 170      if (!isset($itemdata->gradepass) || $itemdata->gradepass == '') {
 171          $itemdata->gradepass = 0;
 172      }
 173  
 174      if (!isset($itemdata->grademax) || $itemdata->grademax == '') {
 175          $itemdata->grademax = 0;
 176      }
 177  
 178      if (!isset($itemdata->grademin) || $itemdata->grademin == '') {
 179          $itemdata->grademin = 0;
 180      }
 181  
 182      $hidden      = empty($itemdata->hidden) ? 0: $itemdata->hidden;
 183      $hiddenuntil = empty($itemdata->hiddenuntil) ? 0: $itemdata->hiddenuntil;
 184      unset($itemdata->hidden);
 185      unset($itemdata->hiddenuntil);
 186  
 187      $locked   = empty($itemdata->locked) ? 0: $itemdata->locked;
 188      $locktime = empty($itemdata->locktime) ? 0: $itemdata->locktime;
 189      unset($itemdata->locked);
 190      unset($itemdata->locktime);
 191  
 192      $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
 193      foreach ($convert as $param) {
 194          if (property_exists($itemdata, $param)) {
 195              $itemdata->$param = unformat_float($itemdata->$param);
 196          }
 197      }
 198      if (isset($itemdata->aggregationcoef2)) {
 199          $itemdata->aggregationcoef2 = $itemdata->aggregationcoef2 / 100.0;
 200      }
 201  
 202      // When creating a new category, a number of grade item fields are filled out automatically, and are required.
 203      // If the user leaves these fields empty during creation of a category, we let the default values take effect
 204      // Otherwise, we let the user-entered grade item values take effect
 205      $grade_item = $grade_category->load_grade_item();
 206      $grade_item_copy = fullclone($grade_item);
 207      grade_item::set_properties($grade_item, $itemdata);
 208  
 209      if (empty($grade_item->id)) {
 210          $grade_item->id = $grade_item_copy->id;
 211      }
 212      if (empty($grade_item->grademax) && $grade_item->grademax != '0') {
 213          $grade_item->grademax = $grade_item_copy->grademax;
 214      }
 215      if (empty($grade_item->grademin) && $grade_item->grademin != '0') {
 216          $grade_item->grademin = $grade_item_copy->grademin;
 217      }
 218      if (empty($grade_item->gradepass) && $grade_item->gradepass != '0') {
 219          $grade_item->gradepass = $grade_item_copy->gradepass;
 220      }
 221      if (empty($grade_item->aggregationcoef) && $grade_item->aggregationcoef != '0') {
 222          $grade_item->aggregationcoef = $grade_item_copy->aggregationcoef;
 223      }
 224  
 225      // Handle null decimals value - must be done before update!
 226      if (!property_exists($itemdata, 'decimals') or $itemdata->decimals < 0) {
 227          $grade_item->decimals = null;
 228      }
 229  
 230      // Change weightoverride flag. Check if the value is set, because it is not when the checkbox is not ticked.
 231      $itemdata->weightoverride = isset($itemdata->weightoverride) ? $itemdata->weightoverride : 0;
 232      if ($grade_item->weightoverride != $itemdata->weightoverride && $grade_category->aggregation == GRADE_AGGREGATE_SUM) {
 233          // If we are using natural weight and the weight has been un-overriden, force parent category to recalculate weights.
 234          $grade_category->force_regrading();
 235      }
 236      $grade_item->weightoverride = $itemdata->weightoverride;
 237  
 238      $grade_item->outcomeid = null;
 239  
 240      // This means we want to rescale overridden grades as well.
 241      if (!empty($data->grade_item_rescalegrades) && $data->grade_item_rescalegrades == 'yes') {
 242          $grade_item->markasoverriddenwhengraded = false;
 243          $grade_item->rescale_grades_keep_percentage($grade_item_copy->grademin, $grade_item_copy->grademax, $grade_item->grademin,
 244                  $grade_item->grademax, 'gradebook');
 245      }
 246  
 247      // update hiding flag
 248      if ($hiddenuntil) {
 249          $grade_item->set_hidden($hiddenuntil, false);
 250      } else {
 251          $grade_item->set_hidden($hidden, false);
 252      }
 253  
 254      $grade_item->set_locktime($locktime); // locktime first - it might be removed when unlocking
 255      $grade_item->set_locked($locked, false, true);
 256  
 257      $grade_item->update(); // We don't need to insert it, it's already created when the category is created
 258  
 259      // set parent if needed
 260      if (isset($data->parentcategory)) {
 261          $grade_category->set_parent($data->parentcategory, 'gradebook');
 262      }
 263  
 264      redirect($returnurl);
 265  }
 266  
 267  $PAGE->navbar->add($heading);
 268  print_grade_page_head($courseid, 'settings', null, $heading, false, false, false);
 269  
 270  $mform->display();
 271  
 272  echo $OUTPUT->footer();
 273  die;