Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/lib/form/ -> group.php (source)

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Form element group
  20   *
  21   * Contains HTML class for group form element
  22   *
  23   * @package   core_form
  24   * @copyright 2007 Jamie Pratt <me@jamiep.org>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once("HTML/QuickForm/group.php");
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * HTML class for a form element group
  33   *
  34   * Overloaded {@link HTML_QuickForm_group} with default behavior modified for Moodle.
  35   *
  36   * @package   core_form
  37   * @category  form
  38   * @copyright 2007 Jamie Pratt <me@jamiep.org>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable {
  42      use templatable_form_element {
  43          export_for_template as export_for_template_base;
  44      }
  45  
  46      /** @var string html for help button, if empty then no help */
  47      var $_helpbutton='';
  48  
  49      /** @var MoodleQuickForm */
  50      protected $_mform = null;
  51  
  52      protected $_renderedfromtemplate = false;
  53  
  54      /**
  55       * constructor
  56       *
  57       * @param string $elementName (optional) name of the group
  58       * @param string $elementLabel (optional) group label
  59       * @param array $elements (optional) array of HTML_QuickForm_element elements to group
  60       * @param string $separator (optional) string to seperate elements.
  61       * @param string $appendName (optional) string to appened to grouped elements.
  62       */
  63      public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
  64          parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
  65      }
  66  
  67      /**
  68       * Old syntax of class constructor. Deprecated in PHP7.
  69       *
  70       * @deprecated since Moodle 3.1
  71       */
  72      public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
  73          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  74          self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
  75      }
  76  
  77      /** @var string template type, would cause problems with client side validation so will leave for now */
  78      //var $_elementTemplateType='fieldset';
  79  
  80      /**
  81       * set html for help button
  82       */
  83      function getHelpButton(){
  84          return $this->_helpbutton;
  85      }
  86  
  87      /**
  88       * Returns element template, nodisplay/static/fieldset
  89       *
  90       * @return string
  91       */
  92      function getElementTemplateType(){
  93          if ($this->_flagFrozen){
  94              if ($this->getGroupType() == 'submit'){
  95                  return 'nodisplay';
  96              } else {
  97                  return 'static';
  98              }
  99          } else {
 100              if ($this->getGroupType() == 'submit') {
 101                  return 'actionbuttons';
 102              }
 103              return 'fieldset';
 104          }
 105      }
 106  
 107      /**
 108       * Sets the grouped elements and hides label
 109       *
 110       * @param array $elements
 111       */
 112      function setElements($elements){
 113          parent::setElements($elements);
 114          foreach ($this->_elements as $element){
 115              if (method_exists($element, 'setHiddenLabel')){
 116                  $element->setHiddenLabel(true);
 117              }
 118          }
 119      }
 120  
 121      /**
 122       * Stores the form this element was added to
 123       * This object is later used by {@link MoodleQuickForm_group::createElement()}
 124       * @param null|MoodleQuickForm $mform
 125       */
 126      public function setMoodleForm($mform) {
 127          if ($mform && $mform instanceof MoodleQuickForm) {
 128              $this->_mform = $mform;
 129          }
 130      }
 131  
 132      /**
 133       * Called by HTML_QuickForm whenever form event is made on this element
 134       *
 135       * If this function is overridden and parent is not called the element must be responsible for
 136       * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
 137       *
 138       * @param     string $event Name of event
 139       * @param     mixed $arg event arguments
 140       * @param     mixed $caller calling object
 141       */
 142      public function onQuickFormEvent($event, $arg, &$caller) {
 143          $this->setMoodleForm($caller);
 144          return parent::onQuickFormEvent($event, $arg, $caller);
 145      }
 146  
 147      /**
 148       * Creates an element to add to the group
 149       * Expects the same arguments as MoodleQuickForm::createElement()
 150       */
 151      public function createFormElement() {
 152          if (!$this->_mform) {
 153              throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
 154          }
 155          return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
 156      }
 157  
 158      public function export_for_template(renderer_base $output) {
 159          global $OUTPUT;
 160  
 161          $context = $this->export_for_template_base($output);
 162  
 163          $this->_renderedfromtemplate = true;
 164  
 165          include_once('HTML/QuickForm/Renderer/Default.php');
 166  
 167          $elements = [];
 168          $name = $this->getName();
 169          $i = 0;
 170          foreach ($this->_elements as $key => $element) {
 171              $elementname = '';
 172              if ($this->_appendName) {
 173                  $elementname = $element->getName();
 174                  if (isset($elementname)) {
 175                      $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
 176                  } else {
 177                      $element->setName($name);
 178                  }
 179              }
 180              $element->_generateId();
 181  
 182              $out = $OUTPUT->mform_element($element, false, false, '', true);
 183  
 184              if (empty($out)) {
 185                  $renderer = new HTML_QuickForm_Renderer_Default();
 186                  $renderer->setElementTemplate('{element}');
 187                  $element->accept($renderer);
 188                  $out = $renderer->toHtml();
 189              }
 190  
 191              // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
 192              $separator = '';
 193              if ($i > 0) {
 194                  if (is_array($this->_separator)) {
 195                      $separator = $this->_separator[($i - 1) % count($this->_separator)];
 196                  } else if ($this->_separator === null) {
 197                      $separator = '&nbsp;';
 198                  } else {
 199                      $separator = (string) $this->_separator;
 200                  }
 201              }
 202  
 203              $elements[] = [
 204                  'separator' => $separator,
 205                  'html' => $out
 206              ];
 207  
 208              // Restore the element's name.
 209              if ($this->_appendName) {
 210                  $element->setName($elementname);
 211              }
 212  
 213              $i++;
 214          }
 215  
 216          $context['groupname'] = $name;
 217          $context['elements'] = $elements;
 218          return $context;
 219      }
 220  
 221      /**
 222       * Accepts a renderer
 223       *
 224       * @param object     An HTML_QuickForm_Renderer object
 225       * @param bool       Whether a group is required
 226       * @param string     An error message associated with a group
 227       * @access public
 228       * @return void
 229       */
 230      public function accept(&$renderer, $required = false, $error = null) {
 231          $this->_createElementsIfNotExist();
 232          $renderer->startGroup($this, $required, $error);
 233          if (!$this->_renderedfromtemplate) {
 234              // Backwards compatible path - only do this if we didn't render the sub-elements already.
 235              $name = $this->getName();
 236              foreach (array_keys($this->_elements) as $key) {
 237                  $element =& $this->_elements[$key];
 238                  $elementname = '';
 239                  if ($this->_appendName) {
 240                      $elementname = $element->getName();
 241                      if (isset($elementname)) {
 242                          $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
 243                      } else {
 244                          $element->setName($name);
 245                      }
 246                  }
 247  
 248                  $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
 249  
 250                  $element->accept($renderer, $required);
 251  
 252                  // Restore the element's name.
 253                  if ($this->_appendName) {
 254                      $element->setName($elementname);
 255                  }
 256              }
 257          }
 258          $renderer->finishGroup($this);
 259      }
 260  
 261      /**
 262       * Calls the validateSubmitValue function for the containing elements and returns an error string as soon as it finds one.
 263       *
 264       * @param array $values Values of the containing elements.
 265       * @return string|null Validation error message or null.
 266       */
 267      public function validateSubmitValue($values) {
 268          foreach ($this->_elements as $element) {
 269              if (method_exists($element, 'validateSubmitValue')) {
 270                  $value = $values[$element->getName()] ?? null;
 271                  $result = $element->validateSubmitValue($value);
 272                  if (!empty($result) && is_string($result)) {
 273                      return $result;
 274                  }
 275              }
 276          }
 277      }
 278  }