Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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/edit/tree/lib.php');
  28  require_once($CFG->dirroot.'/grade/report/lib.php');
  29  require_once ('category_form.php');
  30  
  31  $courseid = required_param('courseid', PARAM_INT);
  32  $id       = optional_param('id', 0, PARAM_INT); // grade_category->id
  33  
  34  $url = new moodle_url('/grade/edit/tree/category.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      throw new \moodle_exception('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  
  56  $heading = get_string('categoryedit', 'grades');
  57  
  58  if ($id) {
  59      if (!$grade_category = grade_category::fetch(array('id'=>$id, 'courseid'=>$course->id))) {
  60          throw new \moodle_exception('invalidcategory');
  61      }
  62      $grade_category->apply_forced_settings();
  63      $category = $grade_category->get_record_data();
  64      // set parent
  65      $category->parentcategory = $grade_category->parent;
  66      $grade_item = $grade_category->load_grade_item();
  67      // nomalize coef values if needed
  68      $parent_category = $grade_category->get_parent_category();
  69  
  70      foreach ($grade_item->get_record_data() as $key => $value) {
  71          $category->{"grade_item_$key"} = $value;
  72      }
  73  
  74      $decimalpoints = $grade_item->get_decimals();
  75  
  76      $category->grade_item_grademax   = format_float($category->grade_item_grademax, $decimalpoints);
  77      $category->grade_item_grademin   = format_float($category->grade_item_grademin, $decimalpoints);
  78      $category->grade_item_gradepass  = format_float($category->grade_item_gradepass, $decimalpoints);
  79      $category->grade_item_multfactor = format_float($category->grade_item_multfactor, 4);
  80      $category->grade_item_plusfactor = format_float($category->grade_item_plusfactor, 4);
  81      $category->grade_item_aggregationcoef2 = format_float($category->grade_item_aggregationcoef2 * 100.0, 4);
  82  
  83      if (!$parent_category) {
  84          // keep as is
  85      } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
  86          $category->grade_item_aggregationcoef = $category->grade_item_aggregationcoef == 0 ? 0 : 1;
  87      } else {
  88          $category->grade_item_aggregationcoef = format_float($category->grade_item_aggregationcoef, 4);
  89      }
  90      // Check to see if the gradebook is frozen. This allows grades to not be altered at all until a user verifies that they
  91      // wish to update the grades.
  92      $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
  93      // Stick with the original code if the grade book is frozen.
  94      if ($gradebookcalculationsfreeze && (int)$gradebookcalculationsfreeze <= 20150627) {
  95          if ($category->aggregation == GRADE_AGGREGATE_SUM) {
  96              // Input fields for grademin and grademax are disabled for the "Natural" category,
  97              // this means they will be ignored if user does not change aggregation method.
  98              // But if user does change aggregation method the default values should be used.
  99              $category->grademax = 100;
 100              $category->grade_item_grademax = 100;
 101              $category->grademin = 0;
 102              $category->grade_item_grademin = 0;
 103          }
 104      } else {
 105          if ($category->aggregation == GRADE_AGGREGATE_SUM && !$grade_item->is_calculated()) {
 106              // Input fields for grademin and grademax are disabled for the "Natural" category,
 107              // this means they will be ignored if user does not change aggregation method.
 108              // But if user does change aggregation method the default values should be used.
 109              // This does not apply to calculated category totals.
 110              $category->grademax = 100;
 111              $category->grade_item_grademax = 100;
 112              $category->grademin = 0;
 113              $category->grade_item_grademin = 0;
 114          }
 115      }
 116  
 117  } else {
 118      $heading = get_string('newcategory', 'grades');
 119      $grade_category = new grade_category(array('courseid'=>$courseid), false);
 120      $grade_category->apply_default_settings();
 121      $grade_category->apply_forced_settings();
 122  
 123      $category = $grade_category->get_record_data();
 124  
 125      $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false);
 126      foreach ($grade_item->get_record_data() as $key => $value) {
 127          $category->{"grade_item_$key"} = $value;
 128      }
 129  }
 130  
 131  $mform = new edit_category_form(null, array('current'=>$category, 'gpr'=>$gpr));
 132  
 133  if ($mform->is_cancelled()) {
 134      redirect($returnurl);
 135  
 136  } else if ($data = $mform->get_data()) {
 137      grade_edit_tree::update_gradecategory($grade_category, $data);
 138      redirect($returnurl);
 139  }
 140  
 141  $PAGE->navbar->add($heading);
 142  print_grade_page_head($courseid, 'settings', null, $heading, false, false, false);
 143  
 144  $mform->display();
 145  
 146  echo $OUTPUT->footer();
 147  die;