[ Index ]

PHP Cross Reference of moodle-2.8

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 MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
  65          parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  66          $this->gradingattributes = $attributes;
  67      }
  68  
  69      /**
  70       * Helper function to retrieve gradingform_instance passed in element attributes
  71       *
  72       * @return gradingform_instance
  73       */
  74      public function get_gradinginstance() {
  75          if (is_array($this->gradingattributes) && array_key_exists('gradinginstance', $this->gradingattributes)) {
  76              return $this->gradingattributes['gradinginstance'];
  77          } else {
  78              return null;
  79          }
  80      }
  81  
  82      /**
  83       * Returns the input field in HTML
  84       *
  85       * @return string
  86       */
  87      public function toHtml(){
  88          global $PAGE;
  89          return $this->get_gradinginstance()->render_grading_element($PAGE, $this);
  90      }
  91  
  92      /**
  93       * get html for help button
  94       *
  95       * @return string html for help button
  96       */
  97      public function getHelpButton(){
  98          return $this->_helpbutton;
  99      }
 100  
 101      /**
 102       * The renderer of gradingform_instance will take care itself about different display
 103       * in normal and frozen states
 104       *
 105       * @return string
 106       */
 107      public function getElementTemplateType(){
 108          return 'default';
 109      }
 110  
 111      /**
 112       * Called by HTML_QuickForm whenever form event is made on this element.
 113       * Adds necessary rules to the element and checks that coorenct instance of gradingform_instance
 114       * is passed in attributes
 115       *
 116       * @param string $event Name of event
 117       * @param mixed $arg event arguments
 118       * @param object $caller calling object
 119       * @return bool
 120       * @throws moodle_exception
 121       */
 122      public function onQuickFormEvent($event, $arg, &$caller) {
 123          if ($event == 'createElement') {
 124              $attributes = $arg[2];
 125              if (!is_array($attributes) || !array_key_exists('gradinginstance', $attributes) || !($attributes['gradinginstance'] instanceof gradingform_instance)) {
 126                  throw new moodle_exception('exc_gradingformelement', 'grading');
 127              }
 128          }
 129  
 130          $name = $this->getName();
 131          if ($name && $caller->elementExists($name)) {
 132              $caller->addRule($name, $this->get_gradinginstance()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes);
 133          }
 134          return parent::onQuickFormEvent($event, $arg, $caller);
 135      }
 136  
 137      /**
 138       * Function registered as rule for this element and is called when this element is being validated.
 139       * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element
 140       *
 141       * @param mixed $elementValue value of element to be validated
 142       * @param array $attributes element attributes
 143       * @return MoodleQuickForm_grading
 144       */
 145      static function _validate($elementValue, $attributes = null) {
 146          return $attributes['gradinginstance']->validate_grading_element($elementValue);
 147      }
 148  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1