[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> grading.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  /**
  19   * Advance grading form element
  20   *
  21   * Element-container for advanced grading custom input
  22   *
  23   * @package   core_form
  24   * @copyright 2011 Marina Glancy
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  require_once("HTML/QuickForm/element.php");
  30  require_once($CFG->dirroot.'/grade/grading/form/lib.php');
  31  
  32  if (class_exists('HTML_QuickForm')) {
  33      HTML_QuickForm::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
  34  }
  35  
  36  /**
  37   * Advance grading form element
  38   *
  39   * HTML class for a grading element. This is a wrapper for advanced grading plugins.
  40   * When adding the 'grading' element to the form, developer must pass an object of
  41   * class gradingform_instance as $attributes['gradinginstance']. Otherwise an exception will be
  42   * thrown.
  43   * This object is responsible for implementing functions to render element html and validate it
  44   *
  45   * @package   core_form
  46   * @category  form
  47   * @copyright 2011 Marina Glancy
  48   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   */
  50  class MoodleQuickForm_grading extends HTML_QuickForm_input{
  51      /** @var string html for help button, if empty then no help */
  52      var $_helpbutton='';
  53  
  54      /** @var array Stores attributes passed to the element */
  55      private $gradingattributes;
  56  
  57      /**
  58       * Class constructor
  59       *
  60       * @param string $elementName Input field name attribute
  61       * @param mixed $elementLabel Label(s) for the input field
  62       * @param mixed $attributes Either a typical HTML attribute string or an associative array
  63       */
  64      public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
  65          parent::__construct($elementName, $elementLabel, $attributes);
  66          $this->gradingattributes = $attributes;
  67      }
  68  
  69      /**
  70       * Old syntax of class constructor. Deprecated in PHP7.
  71       *
  72       * @deprecated since Moodle 3.1
  73       */
  74      public function MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
  75          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  76          self::__construct($elementName, $elementLabel, $attributes);
  77      }
  78  
  79      /**
  80       * Helper function to retrieve gradingform_instance passed in element attributes
  81       *
  82       * @return gradingform_instance
  83       */
  84      public function get_gradinginstance() {
  85          if (is_array($this->gradingattributes) && array_key_exists('gradinginstance', $this->gradingattributes)) {
  86              return $this->gradingattributes['gradinginstance'];
  87          } else {
  88              return null;
  89          }
  90      }
  91  
  92      /**
  93       * Returns the input field in HTML
  94       *
  95       * @return string
  96       */
  97      public function toHtml(){
  98          global $PAGE;
  99          return $this->get_gradinginstance()->render_grading_element($PAGE, $this);
 100      }
 101  
 102      /**
 103       * get html for help button
 104       *
 105       * @return string html for help button
 106       */
 107      public function getHelpButton(){
 108          return $this->_helpbutton;
 109      }
 110  
 111      /**
 112       * The renderer of gradingform_instance will take care itself about different display
 113       * in normal and frozen states
 114       *
 115       * @return string
 116       */
 117      public function getElementTemplateType(){
 118          return 'default';
 119      }
 120  
 121      /**
 122       * Called by HTML_QuickForm whenever form event is made on this element.
 123       * Adds necessary rules to the element and checks that coorenct instance of gradingform_instance
 124       * is passed in attributes
 125       *
 126       * @param string $event Name of event
 127       * @param mixed $arg event arguments
 128       * @param object $caller calling object
 129       * @return bool
 130       * @throws moodle_exception
 131       */
 132      public function onQuickFormEvent($event, $arg, &$caller) {
 133          if ($event == 'createElement') {
 134              $attributes = $arg[2];
 135              if (!is_array($attributes) || !array_key_exists('gradinginstance', $attributes) || !($attributes['gradinginstance'] instanceof gradingform_instance)) {
 136                  throw new moodle_exception('exc_gradingformelement', 'grading');
 137              }
 138          }
 139  
 140          $name = $this->getName();
 141          if ($name && $caller->elementExists($name)) {
 142              $caller->addRule($name, $this->get_gradinginstance()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes);
 143          }
 144          return parent::onQuickFormEvent($event, $arg, $caller);
 145      }
 146  
 147      /**
 148       * Function registered as rule for this element and is called when this element is being validated.
 149       * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element
 150       *
 151       * @param mixed $elementvalue value of element to be validated
 152       * @param array $attributes element attributes
 153       * @return MoodleQuickForm_grading
 154       */
 155      public static function _validate($elementvalue, $attributes = null) {
 156          if (!$attributes['gradinginstance']->is_empty_form($elementvalue)) {
 157              return $attributes['gradinginstance']->validate_grading_element($elementvalue);
 158          }
 159          return true;
 160      }
 161  }


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