Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * submit type form element
  20   *
  21   * Contains HTML class for a submit type element
  22   *
  23   * @package   core_form
  24   * @copyright 2006 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/submit.php");
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * submit type form element
  33   *
  34   * HTML class for a submit type element
  35   *
  36   * @package   core_form
  37   * @category  form
  38   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatable {
  42      use templatable_form_element {
  43          export_for_template as export_for_template_base;
  44      }
  45  
  46      /**
  47       * @var bool $primary Is this button a primary button?
  48       */
  49      protected $primary;
  50  
  51      /**
  52       * Any class apart from 'btn' would be overridden with this content.
  53       *
  54       * By default, submit buttons will utilize the btn-primary OR btn-secondary classes. However there are cases where we
  55       * require a submit button with different stylings (e.g. btn-link). In these cases, $customclassoverride will override
  56       * the defaults mentioned previously and utilize the provided class(es).
  57       *
  58       * @var string $customclassoverride Custom class override for the input element
  59       */
  60      protected $customclassoverride;
  61  
  62      /**
  63       * constructor
  64       *
  65       * @param string $elementName (optional) name of the field
  66       * @param string $value (optional) field label
  67       * @param string $attributes (optional) Either a typical HTML attribute string or an associative array
  68       * @param bool|null $primary Is this button a primary button?
  69       * @param array $options Options to further customise the submit button. Currently accepted options are:
  70       *                  customclassoverride String The CSS class to use for the button instead of the standard
  71       *                                             btn-primary and btn-secondary classes.
  72       */
  73      public function __construct($elementName=null, $value=null, $attributes=null, $primary = null, $options = []) {
  74          parent::__construct($elementName, $value, $attributes);
  75  
  76          // Fallback to legacy behaviour if no value specified.
  77          if (is_null($primary)) {
  78              $this->primary = $this->getName() != 'cancel';
  79          } else {
  80              $this->primary = $primary;
  81          }
  82  
  83          $this->customclassoverride = $options['customclassoverride'] ?? false;
  84      }
  85  
  86      /**
  87       * Old syntax of class constructor. Deprecated in PHP7.
  88       *
  89       * @deprecated since Moodle 3.1
  90       */
  91      public function MoodleQuickForm_submit($elementName=null, $value=null, $attributes=null, $primary = null) {
  92          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  93          self::__construct($elementName, $value, $attributes, $primary);
  94      }
  95  
  96      /**
  97       * Called by HTML_QuickForm whenever form event is made on this element
  98       *
  99       * @param string $event Name of event
 100       * @param mixed $arg event arguments
 101       * @param object $caller calling object
 102       */
 103      function onQuickFormEvent($event, $arg, &$caller)
 104      {
 105          switch ($event) {
 106              case 'createElement':
 107                  parent::onQuickFormEvent($event, $arg, $caller);
 108                  if ($caller->isNoSubmitButton($arg[0])){
 109                      //need this to bypass client validation
 110                      //for buttons that submit but do not process the
 111                      //whole form.
 112                      $onClick = $this->getAttribute('onclick');
 113                      $skip = 'skipClientValidation = true;';
 114                      $onClick = ($onClick !== null)?$skip.' '.$onClick:$skip;
 115                      $this->updateAttributes(array('data-skip-validation' => 1, 'data-no-submit' => 1, 'onclick' => $onClick));
 116                  }
 117                  return true;
 118                  break;
 119          }
 120          return parent::onQuickFormEvent($event, $arg, $caller);
 121  
 122      }
 123  
 124      /**
 125       * Slightly different container template when frozen. Don't want to display a submit
 126       * button if the form is frozen.
 127       *
 128       * @return string
 129       */
 130      function getElementTemplateType(){
 131          if ($this->_flagFrozen){
 132              return 'nodisplay';
 133          } else {
 134              return 'actionbuttons';
 135          }
 136      }
 137  
 138      /**
 139       * Freeze the element so that only its value is returned
 140       */
 141      function freeze(){
 142          $this->_flagFrozen = true;
 143      }
 144  
 145      public function export_for_template(renderer_base $output) {
 146          $context = $this->export_for_template_base($output);
 147          if (!$this->primary) {
 148              $context['secondary'] = true;
 149          }
 150  
 151          if ($this->customclassoverride) {
 152              $context['customclassoverride'] = $this->customclassoverride;
 153          }
 154          return $context;
 155      }
 156  }