Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * A moodleform to allow the editing of a calculated grade item
  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_calculation_form extends moodleform {
  32      public $available;
  33      public $noidnumbers;
  34  
  35      function definition() {
  36          global $COURSE;
  37  
  38          $mform =& $this->_form;
  39  
  40          $itemid = $this->_customdata['itemid'];
  41  
  42          $this->available = grade_item::fetch_all(array('courseid'=>$COURSE->id));
  43          $this->noidnumbers = array();
  44  
  45          // All items that have no idnumbers are added to a separate section of the form (hidden by default),
  46          // enabling the user to assign idnumbers to these grade_items.
  47          foreach ($this->available as $item) {
  48              if (empty($item->idnumber)) {
  49                  $this->noidnumbers[$item->id] = $item;
  50                  unset($this->available[$item->id]);
  51              }
  52              if ($item->id == $itemid) { // Do not include the current grade_item in the available section
  53                  unset($this->available[$item->id]);
  54              }
  55          }
  56  
  57  /// visible elements
  58          $mform->addElement('header', 'general', get_string('gradeitem', 'grades'));
  59          $mform->addElement('static', 'itemname', get_string('itemname', 'grades'));
  60          $mform->addElement('textarea', 'calculation', get_string('calculation', 'grades'), 'cols="60" rows="5"');
  61          $mform->addHelpButton('calculation', 'calculation', 'grades');
  62          $mform->setForceLtr('calculation');
  63  
  64  /// hidden params
  65          $mform->addElement('hidden', 'id', 0);
  66          $mform->setType('id', PARAM_INT);
  67  
  68          $mform->addElement('hidden', 'courseid', 0);
  69          $mform->setType('courseid', PARAM_INT);
  70  
  71          $mform->addElement('hidden', 'section', 0);
  72          $mform->setType('section', PARAM_ALPHA);
  73          $mform->setDefault('section', 'calculation');
  74  
  75  /// add return tracking info
  76          $gpr = $this->_customdata['gpr'];
  77          $gpr->add_mform_elements($mform);
  78  
  79          $this->add_action_buttons();
  80      }
  81  
  82      function definition_after_data() {
  83          global $CFG, $COURSE;
  84  
  85          $mform =& $this->_form;
  86      }
  87  
  88  /// perform extra validation before submission
  89      function validation($data, $files) {
  90          $errors = parent::validation($data, $files);
  91  
  92          $mform =& $this->_form;
  93  
  94          // check the calculation formula
  95          if ($data['calculation'] != '') {
  96              $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  97              $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
  98              $result = $grade_item->validate_formula($calculation);
  99              if ($result !== true) {
 100                  $errors['calculation'] = $result;
 101              }
 102          }
 103  
 104          return $errors;
 105      }
 106  
 107  }
 108