Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 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 = null;
  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          print_error('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              print_error('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                  print_error('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      $heading = get_string('addoutcome', 'grades');
  78      /// adding new outcome from course
  79      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  80      require_login($course);
  81      $context = context_course::instance($course->id);
  82      require_capability('moodle/grade:manage', $context);
  83      navigation_node::override_active_url(new moodle_url('/grade/edit/outcome/course.php', array('id'=>$courseid)));
  84  
  85      $outcome_rec = new stdClass();
  86      $outcome_rec->standard = 0;
  87      $outcome_rec->courseid = $courseid;
  88  } else {
  89      require_login();
  90      require_capability('moodle/grade:manage', $systemcontext);
  91      $PAGE->set_context($systemcontext);
  92  
  93      /// adding new outcome from admin section
  94      $outcome_rec = new stdClass();
  95      $outcome_rec->standard = 1;
  96      $outcome_rec->courseid = 0;
  97  }
  98  
  99  if (!$courseid) {
 100      require_once $CFG->libdir.'/adminlib.php';
 101      admin_externalpage_setup('outcomes');
 102  }
 103  
 104  // default return url
 105  $gpr = new grade_plugin_return();
 106  $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
 107  $editoroptions = array(
 108      'maxfiles'  => EDITOR_UNLIMITED_FILES,
 109      'maxbytes'  => $CFG->maxbytes,
 110      'trusttext' => false,
 111      'noclean'   => true,
 112      'context'   => $systemcontext
 113  );
 114  
 115  if (!empty($outcome_rec->id)) {
 116      $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'outcome', $outcome_rec->id);
 117      $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome_rec->id);
 118  } else {
 119      $editoroptions['subdirs'] = false;
 120      $outcome_rec = file_prepare_standard_editor($outcome_rec, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', null);
 121  }
 122  
 123  $mform = new edit_outcome_form(null, compact('gpr', 'editoroptions'));
 124  
 125  $mform->set_data($outcome_rec);
 126  
 127  if ($mform->is_cancelled()) {
 128      redirect($returnurl);
 129  
 130  } else if ($data = $mform->get_data()) {
 131      $outcome = new grade_outcome(array('id'=>$id));
 132      $data->usermodified = $USER->id;
 133  
 134      if (empty($outcome->id)) {
 135          $data->description = $data->description_editor['text'];
 136          grade_outcome::set_properties($outcome, $data);
 137          if (!has_capability('moodle/grade:manage', $systemcontext)) {
 138              $data->standard = 0;
 139          }
 140          $outcome->courseid = !empty($data->standard) ? null : $courseid;
 141          if (empty($outcome->courseid)) {
 142              $outcome->courseid = null;
 143          }
 144          $outcome->insert();
 145  
 146          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $outcome->id);
 147          $DB->set_field($outcome->table, 'description', $data->description, array('id'=>$outcome->id));
 148      } else {
 149          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'outcome', $id);
 150          grade_outcome::set_properties($outcome, $data);
 151          if (isset($data->standard)) {
 152              $outcome->courseid = !empty($data->standard) ? null : $courseid;
 153          } else {
 154              unset($outcome->courseid); // keep previous
 155          }
 156          $outcome->update();
 157      }
 158  
 159      redirect($returnurl);
 160  }
 161  
 162  if ($courseid) {
 163      print_grade_page_head($courseid, 'outcome', 'edit', $heading);
 164  } else {
 165      echo $OUTPUT->header();
 166  }
 167  
 168  if (!grade_scale::fetch_all_local($courseid) && !grade_scale::fetch_all_global()) {
 169      echo $OUTPUT->confirm(get_string('noscales', 'grades'), $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$courseid, $returnurl);
 170      echo $OUTPUT->footer();
 171      die();
 172  }
 173  
 174  $mform->display();
 175  echo $OUTPUT->footer();