Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/lib/form/ -> group.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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       * @param mixed $attributes (optional) Either a typical HTML attribute string
  63       *              or an associative array
  64       */
  65      public function __construct(
  66          $elementName = null,
  67          $elementLabel = null,
  68          $elements = null,
  69          $separator = null,
  70          $appendName = true,
  71          $attributes = null
  72      ) {
  73          parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName, $attributes);
  74      }
  75  
  76      /**
  77       * Old syntax of class constructor. Deprecated in PHP7.
  78       *
  79       * @deprecated since Moodle 3.1
  80       */
  81      public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
  82          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  83          self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
  84      }
  85  
  86      /** @var string template type, would cause problems with client side validation so will leave for now */
  87      //var $_elementTemplateType='fieldset';
  88  
  89      /**
  90       * set html for help button
  91       */
  92      function getHelpButton(){
  93          return $this->_helpbutton;
  94      }
  95  
  96      /**
  97       * Returns element template, nodisplay/static/fieldset
  98       *
  99       * @return string
 100       */
 101      function getElementTemplateType(){
 102          if ($this->_flagFrozen){
 103              if ($this->getGroupType() == 'submit'){
 104                  return 'nodisplay';
 105              } else {
 106                  return 'static';
 107              }
 108          } else {
 109              if ($this->getGroupType() == 'submit') {
 110                  return 'actionbuttons';
 111              }
 112              return 'fieldset';
 113          }
 114      }
 115  
 116      /**
 117       * Sets the grouped elements and hides label
 118       *
 119       * @param array $elements
 120       */
 121      function setElements($elements){
 122          parent::setElements($elements);
 123          foreach ($this->_elements as $element){
 124              if (method_exists($element, 'setHiddenLabel')){
 125                  $element->setHiddenLabel(true);
 126              }
 127          }
 128      }
 129  
 130      /**
 131       * Stores the form this element was added to
 132       * This object is later used by {@link MoodleQuickForm_group::createElement()}
 133       * @param null|MoodleQuickForm $mform
 134       */
 135      public function setMoodleForm($mform) {
 136          if ($mform && $mform instanceof MoodleQuickForm) {
 137              $this->_mform = $mform;
 138          }
 139      }
 140  
 141      /**
 142       * Called by HTML_QuickForm whenever form event is made on this element
 143       *
 144       * If this function is overridden and parent is not called the element must be responsible for
 145       * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
 146       *
 147       * @param     string $event Name of event
 148       * @param     mixed $arg event arguments
 149       * @param     mixed $caller calling object
 150       */
 151      public function onQuickFormEvent($event, $arg, &$caller) {
 152          $this->setMoodleForm($caller);
 153          return parent::onQuickFormEvent($event, $arg, $caller);
 154      }
 155  
 156      /**
 157       * Creates an element to add to the group
 158       * Expects the same arguments as MoodleQuickForm::createElement()
 159       */
 160      public function createFormElement() {
 161          if (!$this->_mform) {
 162              throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
 163          }
 164          return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
 165      }
 166  
 167      /**
 168       * Return attributes suitable for passing to {@see createFormElement}, comprised of all group attributes without ID in
 169       * order to ensure uniqueness of that value within the group
 170       *
 171       * @return array
 172       */
 173      public function getAttributesForFormElement(): array {
 174          return array_diff_key((array) $this->getAttributes(), array_flip(['id']));
 175      }
 176  
 177      public function export_for_template(renderer_base $output) {
 178          global $OUTPUT;
 179  
 180          $context = $this->export_for_template_base($output);
 181  
 182          $this->_renderedfromtemplate = true;
 183  
 184          include_once('HTML/QuickForm/Renderer/Default.php');
 185  
 186          $elements = [];
 187          $name = $this->getName();
 188          $i = 0;
 189          foreach ($this->_elements as $key => $element) {
 190              $elementname = '';
 191              if ($this->_appendName) {
 192                  $elementname = $element->getName();
 193                  if (isset($elementname)) {
 194                      $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
 195                  } else {
 196                      $element->setName($name);
 197                  }
 198              }
 199              $element->_generateId();
 200  
 201              $out = $OUTPUT->mform_element($element, false, false, '', true);
 202  
 203              if (empty($out)) {
 204                  $renderer = new HTML_QuickForm_Renderer_Default();
 205                  $renderer->setElementTemplate('{element}');
 206                  $element->accept($renderer);
 207                  $out = $renderer->toHtml();
 208              }
 209  
 210              // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
 211              $separator = '';
 212              if ($i > 0) {
 213                  if (is_array($this->_separator)) {
 214                      $separator = $this->_separator[($i - 1) % count($this->_separator)];
 215                  } else if ($this->_separator === null) {
 216                      $separator = '&nbsp;';
 217                  } else {
 218                      $separator = (string) $this->_separator;
 219                  }
 220              }
 221  
 222              $elements[] = [
 223                  'separator' => $separator,
 224                  'html' => $out
 225              ];
 226  
 227              // Restore the element's name.
 228              if ($this->_appendName) {
 229                  $element->setName($elementname);
 230              }
 231  
 232              $i++;
 233          }
 234  
 235          $context['groupname'] = $name;
 236          $context['elements'] = $elements;
 237          return $context;
 238      }
 239  
 240      /**
 241       * Accepts a renderer
 242       *
 243       * @param object     An HTML_QuickForm_Renderer object
 244       * @param bool       Whether a group is required
 245       * @param string     An error message associated with a group
 246       * @access public
 247       * @return void
 248       */
 249      public function accept(&$renderer, $required = false, $error = null) {
 250          $this->_createElementsIfNotExist();
 251          $renderer->startGroup($this, $required, $error);
 252          if (!$this->_renderedfromtemplate) {
 253              // Backwards compatible path - only do this if we didn't render the sub-elements already.
 254              $name = $this->getName();
 255              foreach (array_keys($this->_elements) as $key) {
 256                  $element =& $this->_elements[$key];
 257                  $elementname = '';
 258                  if ($this->_appendName) {
 259                      $elementname = $element->getName();
 260                      if (isset($elementname)) {
 261                          $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
 262                      } else {
 263                          $element->setName($name);
 264                      }
 265                  }
 266  
 267                  $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
 268  
 269                  $element->accept($renderer, $required);
 270  
 271                  // Restore the element's name.
 272                  if ($this->_appendName) {
 273                      $element->setName($elementname);
 274                  }
 275              }
 276          }
 277          $renderer->finishGroup($this);
 278      }
 279  
 280      /**
 281       * Calls the validateSubmitValue function for the containing elements and returns an error string as soon as it finds one.
 282       *
 283       * @param array $values Values of the containing elements.
 284       * @return string|null Validation error message or null.
 285       */
 286      public function validateSubmitValue($values) {
 287          foreach ($this->_elements as $element) {
 288              if (method_exists($element, 'validateSubmitValue')) {
 289                  $value = $values[$element->getName()] ?? null;
 290                  $result = $element->validateSubmitValue($value);
 291                  if (!empty($result) && is_string($result)) {
 292                      return $result;
 293                  }
 294              }
 295          }
 296      }
 297  }