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]

   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 scales
  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  'edit_form.php';
  29  
  30  $courseid = optional_param('courseid', 0, PARAM_INT);
  31  $id       = optional_param('id', 0, PARAM_INT);
  32  
  33  $PAGE->set_url('/grade/edit/scale/edit.php', array('id' => $id, 'courseid' => $courseid));
  34  $PAGE->set_pagelayout('admin');
  35  navigation_node::override_active_url(new moodle_url('/grade/edit/scale/index.php',
  36      array('id' => $courseid)));
  37  
  38  $systemcontext = context_system::instance();
  39  
  40  // a bit complex access control :-O
  41  if ($id) {
  42      /// editing existing scale
  43      if (!$scale_rec = $DB->get_record('scale', array('id' => $id))) {
  44          throw new \moodle_exception('invalidscaleid');
  45      }
  46      if ($scale_rec->courseid) {
  47          $scale_rec->standard = 0;
  48          if (!$course = $DB->get_record('course', array('id' => $scale_rec->courseid))) {
  49              throw new \moodle_exception('invalidcourseid');
  50          }
  51          require_login($course);
  52          $context = context_course::instance($course->id);
  53          require_capability('moodle/course:managescales', $context);
  54          $courseid = $course->id;
  55      } else {
  56          if ($courseid) {
  57              if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  58                  throw new \moodle_exception('invalidcourseid');
  59              }
  60          }
  61          $scale_rec->standard = 1;
  62          $scale_rec->courseid = $courseid;
  63          require_login($courseid);
  64          require_capability('moodle/course:managescales', $systemcontext);
  65      }
  66  
  67  } else if ($courseid){
  68      /// adding new scale from course
  69      if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  70          throw new \moodle_exception('invalidcourseid');
  71      }
  72      $scale_rec = new stdClass();
  73      $scale_rec->standard = 0;
  74      $scale_rec->courseid = $courseid;
  75      require_login($course);
  76      $context = context_course::instance($course->id);
  77      require_capability('moodle/course:managescales', $context);
  78  
  79  } else {
  80      /// adding new scale from admin section
  81      $scale_rec = new stdClass();
  82      $scale_rec->standard = 1;
  83      $scale_rec->courseid = 0;
  84      require_login();
  85      require_capability('moodle/course:managescales', $systemcontext);
  86  }
  87  
  88  if (!$courseid) {
  89      require_once $CFG->libdir.'/adminlib.php';
  90      admin_externalpage_setup('scales');
  91      $PAGE->set_primary_active_tab('siteadminnode');
  92  }
  93  
  94  // default return url
  95  $gpr = new grade_plugin_return();
  96  $returnurl = $gpr->get_return_url('index.php?id='.$courseid);
  97  $editoroptions = array(
  98      'maxfiles'  => EDITOR_UNLIMITED_FILES,
  99      'maxbytes'  => $CFG->maxbytes,
 100      'trusttext' => false,
 101      'noclean'   => true,
 102      'context'   => $systemcontext
 103  );
 104  
 105  if (!empty($scale_rec->id)) {
 106      $editoroptions['subdirs'] = file_area_contains_subdirs($systemcontext, 'grade', 'scale', $scale_rec->id);
 107      $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale_rec->id);
 108  } else {
 109      $editoroptions['subdirs'] = false;
 110      $scale_rec = file_prepare_standard_editor($scale_rec, 'description', $editoroptions, $systemcontext, 'grade', 'scale', null);
 111  }
 112  $mform = new edit_scale_form(null, compact('gpr', 'editoroptions'));
 113  
 114  $mform->set_data($scale_rec);
 115  
 116  if ($mform->is_cancelled()) {
 117      redirect($returnurl);
 118  
 119  } else if ($data = $mform->get_data()) {
 120      $scale = new grade_scale(array('id'=>$id));
 121      $data->userid = $USER->id;
 122  
 123      if (empty($scale->id)) {
 124          $data->description = $data->description_editor['text'];
 125          $data->descriptionformat = $data->description_editor['format'];
 126          grade_scale::set_properties($scale, $data);
 127          if (!has_capability('moodle/grade:manage', $systemcontext)) {
 128              $data->standard = 0;
 129          }
 130          $scale->courseid = !empty($data->standard) ? 0 : $courseid;
 131          $scale->insert();
 132          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $scale->id);
 133          $DB->set_field($scale->table, 'description', $data->description, array('id'=>$scale->id));
 134      } else {
 135          $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $systemcontext, 'grade', 'scale', $id);
 136          grade_scale::set_properties($scale, $data);
 137          if (isset($data->standard)) {
 138              $scale->courseid = !empty($data->standard) ? 0 : $courseid;
 139          } else {
 140              unset($scale->courseid); // keep previous
 141          }
 142          $scale->update();
 143      }
 144      redirect($returnurl);
 145  }
 146  
 147  $heading = $id ? get_string('editscale', 'grades') : get_string('addscale', 'grades');
 148  $PAGE->navbar->add($heading);
 149  print_grade_page_head($COURSE->id, 'scale', null, $heading, false, false, false);
 150  
 151  $mform->display();
 152  
 153  echo $OUTPUT->footer();