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