Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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