Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 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  
  26  use core_grades\form\add_item;
  27  
  28  require_once '../../../config.php';
  29  require_once $CFG->dirroot.'/grade/lib.php';
  30  require_once $CFG->dirroot.'/grade/report/lib.php';
  31  require_once  'item_form.php';
  32  
  33  $courseid = required_param('courseid', PARAM_INT);
  34  $id       = optional_param('id', 0, PARAM_INT);
  35  
  36  $url = new moodle_url('/grade/edit/tree/item.php', array('courseid'=>$courseid));
  37  if ($id !== 0) {
  38      $url->param('id', $id);
  39  }
  40  $PAGE->set_url($url);
  41  $PAGE->set_pagelayout('admin');
  42  navigation_node::override_active_url(new moodle_url('/grade/edit/tree/index.php',
  43      array('id'=>$courseid)));
  44  
  45  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  46      throw new \moodle_exception('invalidcourseid');
  47  }
  48  
  49  require_login($course);
  50  $context = context_course::instance($course->id);
  51  require_capability('moodle/grade:manage', $context);
  52  
  53  // default return url
  54  $gpr = new grade_plugin_return();
  55  $returnurl = $gpr->get_return_url('index.php?id='.$course->id);
  56  
  57  $heading = get_string('itemsedit', 'grades');
  58  
  59  if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
  60      // redirect if outcomeid present
  61      if (!empty($grade_item->outcomeid) && !empty($CFG->enableoutcomes)) {
  62          $url = new moodle_url('/grade/edit/tree/outcomeitem.php', ['id' => $id, 'courseid' => $courseid]);
  63          redirect($gpr->add_url_params($url));
  64      }
  65      if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
  66          $grade_category = $grade_item->get_item_category();
  67          $url = new moodle_url('/grade/edit/tree/category.php', ['id' => $grade_category->id, 'courseid' => $courseid]);
  68          redirect($gpr->add_url_params($url));
  69      }
  70  
  71      $item = $grade_item->get_record_data();
  72      $parent_category = $grade_item->get_parent_category();
  73      $item->parentcategory = $parent_category->id;
  74  
  75  } else {
  76      $heading = get_string('newitem', 'grades');
  77      $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false);
  78      $item = $grade_item->get_record_data();
  79      $parent_category = grade_category::fetch_course_category($courseid);
  80      $item->parentcategory = $parent_category->id;
  81  }
  82  $decimalpoints = $grade_item->get_decimals();
  83  
  84  if ($item->hidden > 1) {
  85      $item->hiddenuntil = $item->hidden;
  86      $item->hidden = 0;
  87  } else {
  88      $item->hiddenuntil = 0;
  89  }
  90  
  91  $item->locked = !empty($item->locked);
  92  
  93  $item->grademax        = format_float($item->grademax, $decimalpoints);
  94  $item->grademin        = format_float($item->grademin, $decimalpoints);
  95  $item->gradepass       = format_float($item->gradepass, $decimalpoints);
  96  $item->multfactor      = format_float($item->multfactor, 4);
  97  $item->plusfactor      = format_float($item->plusfactor, 4);
  98  
  99  if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
 100      $item->aggregationcoef = $item->aggregationcoef == 0 ? 0 : 1;
 101  } else {
 102      $item->aggregationcoef = format_float($item->aggregationcoef, 4);
 103  }
 104  if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
 105      $item->aggregationcoef2 = format_float($item->aggregationcoef2 * 100.0);
 106  }
 107  $item->cancontrolvisibility = $grade_item->can_control_visibility();
 108  
 109  $mform = new edit_item_form(null, array('current'=>$item, 'gpr'=>$gpr));
 110  
 111  $simpleform = new add_item(null, ['itemid' => $grade_item->id, 'courseid' => $courseid, 'gpr' => $gpr]);
 112  // Data has been carried over from the dynamic form.
 113  if ($simpledata = $simpleform->get_submitted_data()) {
 114      $mform->set_data($simpledata);
 115  }
 116  
 117  if ($mform->is_cancelled()) {
 118      redirect($returnurl);
 119  
 120  } else if ($data = $mform->get_data()) {
 121  
 122      // This is a new item, and the category chosen is different than the default category.
 123      if (empty($grade_item->id) && isset($data->parentcategory) && $parent_category->id != $data->parentcategory) {
 124          $parent_category = grade_category::fetch(array('id' => $data->parentcategory));
 125      }
 126  
 127      // If unset, give the aggregation values a default based on parent aggregation method.
 128      $defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation);
 129      if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
 130          $data->aggregationcoef = $defaults['aggregationcoef'];
 131      }
 132      if (!isset($data->weightoverride)) {
 133          $data->weightoverride = $defaults['weightoverride'];
 134      }
 135  
 136      if (!isset($data->gradepass) || $data->gradepass == '') {
 137          $data->gradepass = 0;
 138      }
 139  
 140      if (!isset($data->grademin) || $data->grademin == '') {
 141          $data->grademin = 0;
 142      }
 143  
 144      $hide = empty($data->hiddenuntil) ? 0 : $data->hiddenuntil;
 145      if (!$hide) {
 146          $hide = empty($data->hidden) ? 0 : $data->hidden;
 147      }
 148  
 149      unset($data->hidden);
 150      unset($data->hiddenuntil);
 151  
 152      $locked   = empty($data->locked) ? 0: $data->locked;
 153      $locktime = empty($data->locktime) ? 0: $data->locktime;
 154      unset($data->locked);
 155      unset($data->locktime);
 156  
 157      $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
 158      foreach ($convert as $param) {
 159          if (property_exists($data, $param)) {
 160              $data->$param = unformat_float($data->$param);
 161          }
 162      }
 163      if (isset($data->aggregationcoef2) && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
 164          $data->aggregationcoef2 = $data->aggregationcoef2 / 100.0;
 165      } else {
 166          $data->aggregationcoef2 = $defaults['aggregationcoef2'];
 167      }
 168  
 169      $gradeitem = new grade_item(array('id' => $id, 'courseid' => $courseid));
 170      $oldmin = $gradeitem->grademin;
 171      $oldmax = $gradeitem->grademax;
 172      grade_item::set_properties($gradeitem, $data);
 173      $gradeitem->outcomeid = null;
 174  
 175      // Handle null decimals value
 176      if (!property_exists($data, 'decimals') or $data->decimals < 0) {
 177          $gradeitem->decimals = null;
 178      }
 179  
 180      if (empty($gradeitem->id)) {
 181          $gradeitem->itemtype = 'manual'; // All new items to be manual only.
 182          $gradeitem->insert();
 183  
 184          // set parent if needed
 185          if (isset($data->parentcategory)) {
 186              $gradeitem->set_parent($data->parentcategory, false);
 187          }
 188  
 189      } else {
 190          $gradeitem->update();
 191  
 192          if (!empty($data->rescalegrades) && $data->rescalegrades == 'yes') {
 193              $newmin = $gradeitem->grademin;
 194              $newmax = $gradeitem->grademax;
 195              $gradeitem->rescale_grades_keep_percentage($oldmin, $oldmax, $newmin, $newmax, 'gradebook');
 196          }
 197      }
 198  
 199      if ($item->cancontrolvisibility) {
 200          // Update hiding flag.
 201          $gradeitem->set_hidden($hide, true);
 202      }
 203  
 204      $gradeitem->set_locktime($locktime); // Locktime first - it might be removed when unlocking.
 205      $gradeitem->set_locked($locked, false, true);
 206  
 207      redirect($returnurl);
 208  }
 209  
 210  $PAGE->navbar->add($heading);
 211  print_grade_page_head($courseid, 'settings', null, $heading, false, false, false);
 212  
 213  $mform->display();
 214  
 215  echo $OUTPUT->footer();