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.
   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 form for grade outcomes
  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  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  27  }
  28  
  29  require_once $CFG->libdir.'/formslib.php';
  30  
  31  class edit_outcome_form extends moodleform {
  32      public function definition() {
  33          global $CFG, $COURSE;
  34          $mform =& $this->_form;
  35  
  36          // visible elements
  37          $mform->addElement('header', 'general', get_string('outcomes', 'grades'));
  38  
  39          $mform->addElement('text', 'fullname', get_string('outcomefullname', 'grades'), 'size="40"');
  40          $mform->addRule('fullname', get_string('required'), 'required');
  41          $mform->setType('fullname', PARAM_TEXT);
  42  
  43          $mform->addElement('text', 'shortname', get_string('outcomeshortname', 'grades'), 'size="20"');
  44          $mform->addRule('shortname', get_string('required'), 'required');
  45          $mform->setType('shortname', PARAM_NOTAGS);
  46  
  47          $mform->addElement('advcheckbox', 'standard', get_string('outcomestandard', 'grades'));
  48          $mform->addHelpButton('standard', 'outcomestandard', 'grades');
  49  
  50          $options = array();
  51  
  52          $mform->addElement('selectwithlink', 'scaleid', get_string('scale'), $options, null,
  53              array('link' => $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$COURSE->id, 'label' => get_string('scalescustomcreate')));
  54          $mform->addHelpButton('scaleid', 'typescale', 'grades');
  55          $mform->addRule('scaleid', get_string('required'), 'required');
  56  
  57          $mform->addElement('editor', 'description_editor', get_string('description'), null, $this->_customdata['editoroptions']);
  58  
  59  
  60          // hidden params
  61          $mform->addElement('hidden', 'id', 0);
  62          $mform->setType('id', PARAM_INT);
  63  
  64          $mform->addElement('hidden', 'courseid', 0);
  65          $mform->setType('courseid', PARAM_INT);
  66  
  67  /// add return tracking info
  68          $gpr = $this->_customdata['gpr'];
  69          $gpr->add_mform_elements($mform);
  70  
  71  //-------------------------------------------------------------------------------
  72          // buttons
  73          $this->add_action_buttons();
  74      }
  75  
  76  
  77  /// tweak the form - depending on existing data
  78      function definition_after_data() {
  79          global $CFG;
  80  
  81          $mform =& $this->_form;
  82  
  83          // first load proper scales
  84          if ($courseid = $mform->getElementValue('courseid')) {
  85              $options = array();
  86              if ($scales = grade_scale::fetch_all_local($courseid)) {
  87                  $options[-1] = '--'.get_string('scalescustom');
  88                  foreach($scales as $scale) {
  89                      $options[$scale->id] = $scale->get_name();
  90                  }
  91              }
  92              if ($scales = grade_scale::fetch_all_global()) {
  93                  $options[-2] = '--'.get_string('scalesstandard');
  94                  foreach($scales as $scale) {
  95                      $options[$scale->id] = $scale->get_name();
  96                  }
  97              }
  98              $scale_el =& $mform->getElement('scaleid');
  99              $scale_el->load($options);
 100  
 101          } else {
 102              $options = array();
 103              if ($scales = grade_scale::fetch_all_global()) {
 104                  foreach($scales as $scale) {
 105                      $options[$scale->id] = $scale->get_name();
 106                  }
 107              }
 108              $scale_el =& $mform->getElement('scaleid');
 109              $scale_el->load($options);
 110          }
 111  
 112          if ($id = $mform->getElementValue('id')) {
 113              $outcome = grade_outcome::fetch(array('id'=>$id));
 114              $itemcount   = $outcome->get_item_uses_count();
 115              $coursecount = $outcome->get_course_uses_count();
 116  
 117              if ($itemcount) {
 118                  $mform->hardFreeze('scaleid');
 119              }
 120  
 121              if (empty($courseid)) {
 122                  $mform->hardFreeze('standard');
 123  
 124              } else if (!has_capability('moodle/grade:manage', context_system::instance())) {
 125                  $mform->hardFreeze('standard');
 126  
 127              } else if ($coursecount and empty($outcome->courseid)) {
 128                  $mform->hardFreeze('standard');
 129              }
 130  
 131  
 132          } else {
 133              if (empty($courseid) or !has_capability('moodle/grade:manage', context_system::instance())) {
 134                  $mform->hardFreeze('standard');
 135              }
 136          }
 137      }
 138  
 139  /// perform extra validation before submission
 140      function validation($data, $files) {
 141          $errors = parent::validation($data, $files);
 142  
 143          if ($data['scaleid'] < 1) {
 144              $errors['scaleid'] = get_string('required');
 145          }
 146  
 147          if (!empty($data['standard']) and $scale = grade_scale::fetch(array('id'=>$data['scaleid']))) {
 148              if (!empty($scale->courseid)) {
 149                  //TODO: localize
 150                  $errors['scaleid'] = 'Can not use custom scale in global outcome!';
 151              }
 152          }
 153  
 154          return $errors;
 155      }
 156  
 157  
 158  }
 159  
 160