[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/edit/tree/ -> calculation_form.php (source)

   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  
  63  /// hidden params
  64          $mform->addElement('hidden', 'id', 0);
  65          $mform->setType('id', PARAM_INT);
  66  
  67          $mform->addElement('hidden', 'courseid', 0);
  68          $mform->setType('courseid', PARAM_INT);
  69  
  70          $mform->addElement('hidden', 'section', 0);
  71          $mform->setType('section', PARAM_ALPHA);
  72          $mform->setDefault('section', 'calculation');
  73  
  74  /// add return tracking info
  75          $gpr = $this->_customdata['gpr'];
  76          $gpr->add_mform_elements($mform);
  77  
  78          $this->add_action_buttons();
  79      }
  80  
  81      function definition_after_data() {
  82          global $CFG, $COURSE;
  83  
  84          $mform =& $this->_form;
  85      }
  86  
  87  /// perform extra validation before submission
  88      function validation($data, $files) {
  89          $errors = parent::validation($data, $files);
  90  
  91          $mform =& $this->_form;
  92  
  93          // check the calculation formula
  94          if ($data['calculation'] != '') {
  95              $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
  96              $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
  97              $result = $grade_item->validate_formula($calculation);
  98              if ($result !== true) {
  99                  $errors['calculation'] = $result;
 100              }
 101          }
 102  
 103          return $errors;
 104      }
 105  
 106  }
 107  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1