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