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.

Differences Between: [Versions 401 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  namespace core\output;
  18  
  19  use renderable;
  20  
  21  /**
  22   * Class to render a sticky footer element.
  23   *
  24   * Sticky footer can be rendered at any moment if the page (even inside a form) but
  25   * it will be displayed at the bottom of the page.
  26   *
  27   * Important: note that pages can only display one sticky footer at once.
  28   *
  29   * Important: not all themes are compatible with sticky footer. If the current theme
  30   * is not compatible it will be rendered as a standard div element.
  31   *
  32   * @package    core
  33   * @category   output
  34   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class sticky_footer implements named_templatable, renderable {
  38  
  39      /**
  40       * @var string content of the sticky footer.
  41       */
  42      protected $stickycontent = '';
  43  
  44      /**
  45       * @var string extra CSS classes. By default, elements are justified to the end.
  46       */
  47      protected $stickyclasses = 'justify-content-end';
  48  
  49      /**
  50       * @var bool if the footer should auto enable or not.
  51       */
  52      protected $autoenable = true;
  53  
  54      /**
  55       * @var array extra HTML attributes (attribute => value).
  56       */
  57      protected $attributes = [];
  58  
  59      /**
  60       * Constructor.
  61       *
  62       * @param string $stickycontent the footer content
  63       * @param string|null $stickyclasses extra CSS classes
  64       * @param array $attributes extra html attributes (attribute => value)
  65       */
  66      public function __construct(string $stickycontent = '', ?string $stickyclasses = null, array $attributes = []) {
  67          $this->stickycontent = $stickycontent;
  68          if ($stickyclasses !== null) {
  69              $this->stickyclasses = $stickyclasses;
  70          }
  71          $this->attributes = $attributes;
  72      }
  73  
  74      /**
  75       * Set the footer contents.
  76       *
  77       * @param string $stickycontent the footer content
  78       */
  79      public function set_content(string $stickycontent) {
  80          $this->stickycontent = $stickycontent;
  81      }
  82  
  83      /**
  84       * Set the auto enable value.
  85       *
  86       * @param bool $autoenable the footer content
  87       */
  88      public function set_auto_enable(bool $autoenable) {
  89          $this->autoenable = $autoenable;
  90      }
  91  
  92      /**
  93       * Add extra classes to the sticky footer.
  94       *
  95       * @param string $stickyclasses the extra classes
  96       */
  97      public function add_classes(string $stickyclasses) {
  98          if (!empty($this->stickyclasses)) {
  99              $this->stickyclasses .= ' ';
 100          }
 101          $this->stickyclasses = $stickyclasses;
 102      }
 103  
 104      /**
 105       * Add extra attributes to the sticky footer element.
 106       *
 107       * @param string $atribute the attribute
 108       * @param string $value the value
 109       */
 110      public function add_attribute(string $atribute, string $value) {
 111          $this->attributes[$atribute] = $value;
 112      }
 113  
 114      /**
 115       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
 116       *
 117       * @param \renderer_base $output typically, the renderer that's calling this function
 118       * @return array data context for a mustache template
 119       */
 120      public function export_for_template(\renderer_base $output) {
 121          $extras = [];
 122          foreach ($this->attributes as $attribute => $value) {
 123              $extras[] = [
 124                  'attribute' => $attribute,
 125                  'value' => $value,
 126              ];
 127          }
 128          $data = [
 129              'stickycontent' => (string)$this->stickycontent,
 130              'stickyclasses' => $this->stickyclasses,
 131              'extras' => $extras,
 132          ];
 133          if (!$this->autoenable) {
 134              $data['disable'] = true;
 135          }
 136          return $data;
 137      }
 138  
 139      /**
 140       * Get the name of the template to use for this templatable.
 141       *
 142       * @param \renderer_base $renderer The renderer requesting the template name
 143       * @return string the template name
 144       */
 145      public function get_template_name(\renderer_base $renderer): string {
 146          return 'core/sticky_footer';
 147      }
 148  }