Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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   * Performs actions on grade items and categories like hiding and locking
  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  
  28  $courseid = required_param('id', PARAM_INT);
  29  $action   = required_param('action', PARAM_ALPHA);
  30  $eid      = required_param('eid', PARAM_ALPHANUM);
  31  
  32  $PAGE->set_url('/grade/edit/tree/action.php', array('id'=>$courseid, 'action'=>$action, 'eid'=>$eid));
  33  
  34  /// Make sure they can even access this course
  35  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  36      throw new \moodle_exception('invalidcourseid');
  37  }
  38  require_login($course);
  39  $context = context_course::instance($course->id);
  40  
  41  // default return url
  42  $gpr = new grade_plugin_return();
  43  $returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/edit/tree/index.php?id='.$course->id);
  44  
  45  // get the grading tree object
  46  $gtree = new grade_tree($courseid, false, false);
  47  
  48  // what are we working with?
  49  if (!$element = $gtree->locate_element($eid)) {
  50      throw new \moodle_exception('invalidelementid', '', $returnurl);
  51  }
  52  $object = $element['object'];
  53  $type   = $element['type'];
  54  
  55  
  56  switch ($action) {
  57      case 'hide':
  58          if ($eid and confirm_sesskey()) {
  59              if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
  60                  throw new \moodle_exception('nopermissiontohide', '', $returnurl);
  61              }
  62              if ($type == 'grade' and empty($object->id)) {
  63                  $object->insert();
  64              }
  65              if (!$object->can_control_visibility()) {
  66                  throw new \moodle_exception('componentcontrolsvisibility', 'grades', $returnurl);
  67              }
  68              $object->set_hidden(1, true);
  69          }
  70          break;
  71  
  72      case 'show':
  73          if ($eid and confirm_sesskey()) {
  74              if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:hide', $context)) {
  75                  throw new \moodle_exception('nopermissiontoshow', '', $returnurl);
  76              }
  77              if ($type == 'grade' and empty($object->id)) {
  78                  $object->insert();
  79              }
  80              if (!$object->can_control_visibility()) {
  81                  throw new \moodle_exception('componentcontrolsvisibility', 'grades', $returnurl);
  82              }
  83              $object->set_hidden(0, true);
  84          }
  85          break;
  86  
  87      case 'lock':
  88          if ($eid and confirm_sesskey()) {
  89              if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:lock', $context)) {
  90                  throw new \moodle_exception('nopermissiontolock', '', $returnurl);
  91              }
  92              if ($type == 'grade' and empty($object->id)) {
  93                  $object->insert();
  94              }
  95              $object->set_locked(1, false, true);
  96          }
  97          break;
  98  
  99      case 'unlock':
 100          if ($eid and confirm_sesskey()) {
 101              if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:unlock', $context)) {
 102                  throw new \moodle_exception('nopermissiontounlock', '', $returnurl);
 103              }
 104              if ($type == 'grade' and empty($object->id)) {
 105                  $object->insert();
 106              }
 107              $object->set_locked(0, false, true);
 108          }
 109          break;
 110  
 111      case 'resetweights':
 112          if ($eid && confirm_sesskey()) {
 113  
 114              // This is specific to category items with natural weight as an aggregation method, and can
 115              // only be done by someone who can manage the grades.
 116              if ($type != 'category' || $object->aggregation != GRADE_AGGREGATE_SUM ||
 117                      !has_capability('moodle/grade:manage', $context)) {
 118                  throw new \moodle_exception('nopermissiontoresetweights', 'grades', $returnurl);
 119              }
 120  
 121              // Remove the weightoverride flag from the children.
 122              $children = $object->get_children();
 123              foreach ($children as $item) {
 124                  if ($item['type'] == 'category') {
 125                      $gradeitem = $item['object']->load_grade_item();
 126                  } else {
 127                      $gradeitem = $item['object'];
 128                  }
 129  
 130                  if ($gradeitem->weightoverride == false) {
 131                      continue;
 132                  }
 133  
 134                  $gradeitem->weightoverride = false;
 135                  $gradeitem->update();
 136              }
 137  
 138              // Force regrading.
 139              $object->force_regrading();
 140          }
 141  }
 142  
 143  redirect($returnurl);
 144  //redirect($returnurl, 'debug delay', 5);
 145  
 146