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]

   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 page for grade outcomes.
  19   *
  20   * @package   core_grades
  21   * @copyright 2008 Nicolas Connault
  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  'edit_form.php';
  29  
  30  $courseid = optional_param('courseid', 0, PARAM_INT);
  31  $id       = optional_param('id', 0, PARAM_INT);
  32  
  33  $url = new moodle_url('/grade/edit/outcome/edit.php');
  34  if ($courseid !== 0) {
  35      $url->param('courseid', $courseid);
  36  }
  37  if ($id !== 0) {
  38      $url->param('id', $id);
  39  }
  40  $PAGE->set_url($url);
  41  $PAGE->set_pagelayout('admin');
  42  
  43  $systemcontext = context_system::instance();
  44  $heading = get_string('addoutcome', 'grades');
  45  
  46  // a bit complex access control :-O
  47  if ($id) {
  48      $heading = get_string('editoutcome', 'grades');
  49  
  50      /// editing existing outcome
  51      if (!$outcome_rec = $DB->get_record('grade_outcomes', array('id' => $id))) {
  52          throw new \moodle_exception('invalidoutcome');
  53      }
  54      if ($outcome_rec->courseid) {
  55          $outcome_rec->standard = 0;
  56          if (!$course = $DB->get_record('course', array('id' => $outcome_rec->courseid))) {
  57              throw new \moodle_exception('invalidcourseid');
  58          }
  59          require_login($course);
  60          $context = context_course::instance($course->id);
  61          require_capability('moodle/grade:manage', $context);
  62          $courseid = $course->id;
  63      } else {
  64          if ($courseid) {
  65              if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  66                  throw new \moodle_exception('invalidcourseid');
  67              }
  68          }
  69          $outcome_rec->standard = 1;
  70          $outcome_rec->courseid = $courseid;
  71          require_login();
  72          require_capability('moodle/grade:manage', $systemcontext);
  73          $PAGE->set_context($systemcontext);
  74      }
  75  
  76  } else if ($courseid){
  77      /// adding new outcome from course
  78      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  79      require_login($course);
  80      $context = context_course::instance($course->id);
  81      require_capability('moodle/grade:manage', $context);
  82  
  83      $outcome_rec = new stdClass();
  84      $outcome_rec->standard = 0;
  85      $outcome_rec->courseid = $courseid;
  86  } else {
  87      require_login();
  88      require_capability('moodle/grade:manage', $systemcontext);
  89      $PAGE->set_context($systemcontext);
  90  
  91      /// adding new outcome from admin section
  92      $outcome_rec = new stdClass();
  93      $outcome_rec->standard = 1;
  94      $outcome_rec->courseid = 0;
  95  }
  96  
  97  if (!$courseid) {
  98      require_once $CFG->libdir.'/adminlib.php';
  99      admin_externalpage_setup('outcomes');
 100  
 101      $PAGE->set_primary_active_tab('siteadminnode');
 102  } else {
 103      navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', ['id' => $courseid]));
 104      $PAGE->navbar->add(get_string('manageoutcomes', 'grades'),
 105          new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]));
 106  }
 107  
 108  // default return url
 109  $gpr = new grade_plugin_return();
 110  $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
 111  $editoroptions = array(
 112      'maxfiles'  => EDITOR_UNLIMITED_FILES,
 113      'maxbytes'  => $CFG->maxbytes,
 114      'trusttext' => false,
 115      'noclean'   => true,
 116      'context'   => $systemcontext
 117  );
 118  
 119  if (!empty($outcome_rec->id)) {
 120      $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'outcome', $outcome_rec->id);
 121      $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id);
 122  } else {
 123      $editoroptions['subdirs'] = false;
 124      $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', null);
 125  }
 126  
 127  $mform = new edit_outcome_form(null, compact('gpr', 'editoroptions'));
 128  
 129  $mform->set_data($outcome_rec);
 130  
 131  if ($mform->is_cancelled()) {
 132      redirect($returnurl);
 133  
 134  } else if ($data = $mform->get_data()) {
 135      $outcome = new grade_outcome(array('id'=>$id));
 136      $data->usermodified = $USER->id;
 137  
 138      if (empty($outcome->id)) {
 139          $data->description = $data->description_editor['text'];
 140          grade_outcome::set_properties($outcome, $data);
 141          if (!has_capability('moodle/grade:manage', $systemcontext)) {
 142              $data->standard = 0;
 143          }
 144          $outcome->courseid = !empty($data->standard) ? null : $courseid;
 145          if (empty($outcome->courseid)) {
 146              $outcome->courseid = null;
 147          }
 148          $outcome->insert();
 149  
 150          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome->id);
 151          $DB->set_field($outcome->table, 'description', $data->description, array('id'=>$outcome->id));
 152      } else {
 153          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $id);
 154          grade_outcome::set_properties($outcome, $data);
 155          if (isset($data->standard)) {
 156              $outcome->courseid = !empty($data->standard) ? null : $courseid;
 157          } else {
 158              unset($outcome->courseid); // keep previous
 159          }
 160          $outcome->update();
 161      }
 162  
 163      redirect($returnurl);
 164  }
 165  
 166  $PAGE->navbar->add($heading, $url);
 167  
 168  print_grade_page_head($courseid ?: SITEID, 'outcome', 'edit', $heading, false, false, false);
 169  
 170  if (!grade_scale::fetch_all_local($courseid) && !grade_scale::fetch_all_global()) {
 171      echo $OUTPUT->confirm(get_string('noscales', 'grades'), $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$courseid, $returnurl);
 172      echo $OUTPUT->footer();
 173      die();
 174  }
 175  
 176  $mform->display();
 177  echo $OUTPUT->footer();