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   * A page to create or edit outcome grade items
  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_outcome;
  26  
  27  require_once '../../../config.php';
  28  require_once $CFG->dirroot.'/grade/lib.php';
  29  require_once $CFG->dirroot.'/grade/report/lib.php';
  30  require_once  'outcomeitem_form.php';
  31  
  32  $courseid = required_param('courseid', PARAM_INT);
  33  $id       = optional_param('id', 0, PARAM_INT);
  34  
  35  $url = new moodle_url('/grade/edit/tree/outcomeitem.php', array('courseid'=>$courseid));
  36  if ($id !== 0) {
  37      $url->param('id', $id);
  38  }
  39  $PAGE->set_url($url);
  40  $PAGE->set_pagelayout('admin');
  41  navigation_node::override_active_url(new moodle_url('/grade/edit/tree/index.php',
  42      array('id'=>$courseid)));
  43  
  44  if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  45      throw new \moodle_exception('invalidcourseid');
  46  }
  47  
  48  require_login($course);
  49  $context = context_course::instance($course->id);
  50  require_capability('moodle/grade:manage', $context);
  51  
  52  
  53  // default return url
  54  $gpr = new grade_plugin_return();
  55  $returnurl = $gpr->get_return_url('index.php?id='.$course->id);
  56  
  57  $mform = new edit_outcomeitem_form(null, array('gpr'=>$gpr));
  58  
  59  if ($mform->is_cancelled() || empty($CFG->enableoutcomes)) {
  60      redirect($returnurl);
  61  }
  62  
  63  $heading = get_string('outcomeitemsedit', 'grades');
  64  
  65  if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
  66      // redirect if outcomeid present
  67      if (empty($grade_item->outcomeid)) {
  68          $url = new moodle_url('/grade/edit/tree/item.php', ['id' => $id, 'courseid' => $courseid]);
  69          redirect($gpr->add_url_params($url));
  70      }
  71      $item = $grade_item->get_record_data();
  72  
  73      $parent_category = $grade_item->get_parent_category();
  74      $item->parentcategory = $parent_category->id;
  75  
  76      if ($item->itemtype == 'mod') {
  77          $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
  78          $item->cmid = $cm->id;
  79      } else {
  80          $item->cmid = 0;
  81      }
  82  
  83  } else {
  84      $heading = get_string('newoutcomeitem', 'grades');
  85      $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false);
  86      $item = $grade_item->get_record_data();
  87      $item->cmid = 0;
  88      $parent_category = grade_category::fetch_course_category($courseid);
  89      $item->parentcategory = $parent_category->id;
  90  }
  91  
  92  $decimalpoints = $grade_item->get_decimals();
  93  
  94  if ($item->hidden > 1) {
  95      $item->hiddenuntil = $item->hidden;
  96      $item->hidden = 0;
  97  } else {
  98      $item->hiddenuntil = 0;
  99  }
 100  
 101  $item->locked = !empty($item->locked);
 102  
 103  $item->gradepass       = format_float($item->gradepass, $decimalpoints);
 104  
 105  if (empty($parent_category)) {
 106      $item->aggregationcoef = 0;
 107  } else if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
 108      $item->aggregationcoef = $item->aggregationcoef > 0 ? 1 : 0;
 109      $item->aggregationcoef2 = format_float($item->aggregationcoef2 * 100.0);
 110  } else {
 111      $item->aggregationcoef = format_float($item->aggregationcoef, 4);
 112  }
 113  
 114  $mform->set_data($item);
 115  
 116  $simpleform = new add_outcome(null, ['itemid' => $grade_item->id, 'courseid' => $courseid, 'gpr' => $gpr]);
 117  // Data has been carried over from the dynamic form.
 118  if ($simpledata = $simpleform->get_submitted_data()) {
 119      $mform->set_data($simpledata);
 120  }
 121  
 122  if ($data = $mform->get_data()) {
 123  
 124      // This is a new item, and the category chosen is different than the default category.
 125      if (empty($grade_item->id) && isset($data->parentcategory) && $parent_category->id != $data->parentcategory) {
 126          $parent_category = grade_category::fetch(array('id' => $data->parentcategory));
 127      }
 128  
 129      // If unset, give the aggregation values a default based on parent aggregation method.
 130      $defaults = grade_category::get_default_aggregation_coefficient_values($parent_category->aggregation);
 131      if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
 132          $data->aggregationcoef = $defaults['aggregationcoef'];
 133      }
 134      if (!isset($data->weightoverride)) {
 135          $data->weightoverride = $defaults['weightoverride'];
 136      }
 137  
 138      if (property_exists($data, 'calculation')) {
 139          $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
 140      }
 141  
 142      $hidden      = empty($data->hidden) ? 0: $data->hidden;
 143      $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
 144      unset($data->hidden);
 145      unset($data->hiddenuntil);
 146  
 147      $locked   = empty($data->locked) ? 0: $data->locked;
 148      $locktime = empty($data->locktime) ? 0: $data->locktime;
 149      unset($data->locked);
 150      unset($data->locktime);
 151  
 152      $convert = array('gradepass', 'aggregationcoef', 'aggregationcoef2');
 153      foreach ($convert as $param) {
 154          if (property_exists($data, $param)) {
 155              $data->$param = unformat_float($data->$param);
 156          }
 157      }
 158      if (isset($data->aggregationcoef2) && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
 159          $data->aggregationcoef2 = $data->aggregationcoef2 / 100.0;
 160      } else {
 161          $data->aggregationcoef2 = $defaults['aggregationcoef2'];
 162      }
 163  
 164      $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
 165      grade_item::set_properties($grade_item, $data);
 166  
 167      // fix activity links
 168      if (empty($data->cmid)) {
 169          // manual item
 170          $grade_item->itemtype     = 'manual';
 171          $grade_item->itemmodule   = null;
 172          $grade_item->iteminstance = null;
 173          $grade_item->itemnumber   = 0;
 174  
 175      } else {
 176          $params = array($data->cmid);
 177          $module = $DB->get_record_sql("SELECT cm.*, m.name as modname
 178                                      FROM {modules} m, {course_modules} cm
 179                                     WHERE cm.id = ? AND cm.module = m.id ", $params);
 180          $grade_item->itemtype     = 'mod';
 181          $grade_item->itemmodule   = $module->modname;
 182          $grade_item->iteminstance = $module->instance;
 183  
 184          if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
 185                                             'iteminstance'=>$grade_item->iteminstance, 'courseid'=>$COURSE->id))) {
 186              if (!empty($grade_item->id) and in_array($grade_item, $items)) {
 187                  //no change needed
 188              } else {
 189                  $max = 999;
 190                  foreach($items as $item) {
 191                      if (empty($item->outcomeid)) {
 192                          continue;
 193                      }
 194                      if ($item->itemnumber > $max) {
 195                          $max = $item->itemnumber;
 196                      }
 197                  }
 198                  $grade_item->itemnumber = $max + 1;
 199              }
 200          } else {
 201              $grade_item->itemnumber = 1000;
 202          }
 203      }
 204  
 205      // fix scale used
 206      $outcome = grade_outcome::fetch(array('id'=>$data->outcomeid));
 207      $grade_item->gradetype = GRADE_TYPE_SCALE;
 208      $grade_item->scaleid = $outcome->scaleid; //TODO: we might recalculate existing outcome grades when changing scale
 209  
 210      if (empty($grade_item->id)) {
 211          $grade_item->insert();
 212          // move next to activity if adding linked outcome
 213          if ($grade_item->itemtype == 'mod') {
 214              if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
 215                           'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
 216                  $grade_item->set_parent($item->categoryid);
 217                  $grade_item->move_after_sortorder($item->sortorder);
 218              }
 219          } else {
 220              // set parent if needed
 221              if (isset($data->parentcategory)) {
 222                  $grade_item->set_parent($data->parentcategory, false);
 223              }
 224          }
 225  
 226      } else {
 227          $grade_item->update();
 228      }
 229  
 230      // update hiding flag
 231      if ($hiddenuntil) {
 232          $grade_item->set_hidden($hiddenuntil, false);
 233      } else {
 234          $grade_item->set_hidden($hidden, false);
 235      }
 236  
 237      $grade_item->set_locktime($locktime); // locktime first - it might be removed when unlocking
 238      $grade_item->set_locked($locked, false, true);
 239  
 240      redirect($returnurl);
 241  }
 242  
 243  $PAGE->navbar->add($heading);
 244  print_grade_page_head($courseid, 'settings', null, $heading, false, false, false);
 245  
 246  if (!grade_outcome::fetch_all_available($COURSE->id)) {
 247      echo $OUTPUT->confirm(get_string('nooutcomes', 'grades'), $CFG->wwwroot.'/grade/edit/outcome/course.php?id='.$courseid, $returnurl);
 248      echo $OUTPUT->footer();
 249      die();
 250  }
 251  
 252  $mform->display();
 253  
 254  echo $OUTPUT->footer();