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