Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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 array extra HTML attributes (attribute => value).
  51       */
  52      protected $attributes = [];
  53  
  54      /**
  55       * Constructor.
  56       *
  57       * @param string $stickycontent the footer content
  58       * @param string|null $stickyclasses extra CSS classes
  59       * @param array $attributes extra html attributes (attribute => value)
  60       */
  61      public function __construct(string $stickycontent = '', ?string $stickyclasses = null, array $attributes = []) {
  62          $this->stickycontent = $stickycontent;
  63          if ($stickyclasses !== null) {
  64              $this->stickyclasses = $stickyclasses;
  65          }
  66          $this->attributes = $attributes;
  67      }
  68  
  69      /**
  70       * Set the footer contents.
  71       *
  72       * @param string $stickycontent the footer content
  73       */
  74      public function set_content(string $stickycontent) {
  75          $this->stickycontent = $stickycontent;
  76      }
  77  
  78      /**
  79       * Add extra classes to the sticky footer.
  80       *
  81       * @param string $stickyclasses the extra classes
  82       */
  83      public function add_classes(string $stickyclasses) {
  84          if (!empty($this->stickyclasses)) {
  85              $this->stickyclasses .= ' ';
  86          }
  87          $this->stickyclasses = $stickyclasses;
  88      }
  89  
  90      /**
  91       * Add extra attributes to the sticky footer element.
  92       *
  93       * @param string $atribute the attribute
  94       * @param string $value the value
  95       */
  96      public function add_attribute(string $atribute, string $value) {
  97          $this->attributes[$atribute] = $value;
  98      }
  99  
 100      /**
 101       * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
 102       *
 103       * @param renderer_base $output typically, the renderer that's calling this function
 104       * @return array data context for a mustache template
 105       */
 106      public function export_for_template(\renderer_base $output) {
 107          $extras = [];
 108          foreach ($this->attributes as $attribute => $value) {
 109              $extras[] = [
 110                  'attribute' => $attribute,
 111                  'value' => $value,
 112              ];
 113          }
 114          return [
 115              'stickycontent' => (string)$this->stickycontent,
 116              'stickyclasses' => $this->stickyclasses,
 117              'extras' => $extras,
 118          ];
 119      }
 120  
 121      /**
 122       * Get the name of the template to use for this templatable.
 123       *
 124       * @param \renderer_base $renderer The renderer requesting the template name
 125       * @return string the template name
 126       */
 127      public function get_template_name(\renderer_base $renderer): string {
 128          return 'core/sticky_footer';
 129      }
 130  }