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.

Differences Between: [Versions 400 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   * Renderable for the availability info.
  19   *
  20   * @package   core_availability
  21   * @copyright 2021 Bas Brands <bas@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_availability\output;
  26  use core_availability_multiple_messages;
  27  use renderable;
  28  use templatable;
  29  use stdClass;
  30  
  31  /**
  32   * Base class to render availability info.
  33   *
  34   * @package   core_availability
  35   * @copyright 2021 Bas Brands <bas@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class availability_info implements renderable, templatable {
  39  
  40      /** @var core_availability_multiple_messages availabilitymessages the course format class */
  41      protected $availabilitymessages;
  42  
  43      /** @var int counts number of conditions */
  44      protected $count = 0;
  45  
  46      /** @var int Maximum number of lines of availability info */
  47      protected const MAXVISIBLE = 4;
  48  
  49      /**
  50       * Constructor.
  51       *
  52       * @param core_availability_multiple_messages $renderable the availability messages
  53       */
  54      public function __construct(core_availability_multiple_messages $renderable) {
  55          $this->availabilitymessages = $renderable;
  56          $this->count = 0;
  57      }
  58  
  59      /**
  60       * Export this data so it can be used as the context for a mustache template.
  61       *
  62       * @param \renderer_base $output typically, the renderer that's calling this function
  63       * @return stdClass data context for a mustache template
  64       */
  65      public function export_for_template(\renderer_base $output): stdClass {
  66  
  67          $template = $this->get_item_template($this->availabilitymessages);
  68  
  69          $template->id = uniqid();
  70  
  71          if ($this->count >= self::MAXVISIBLE) {
  72              $template->showmorelink = true;
  73          }
  74  
  75          return $template;
  76      }
  77  
  78      /**
  79       * Get the item base template.
  80       *
  81       * @return stdClass the template base
  82       */
  83      protected function get_item_base_template(): stdClass {
  84          return (object)[
  85              'hidden' => $this->count > self::MAXVISIBLE,
  86              'abbreviate' => $this->count === self::MAXVISIBLE,
  87              'id' => false,
  88              'items' => [],
  89              'hasitems' => false,
  90              'showmorelink' => false,
  91          ];
  92      }
  93  
  94      /**
  95       * Get the item template.
  96       *
  97       * @param core_availability_multiple_messages $availability the availability messages
  98       * @return stdClass the template
  99       */
 100      protected function get_item_template(core_availability_multiple_messages $availability): stdClass {
 101  
 102          $template = $this->get_item_base_template();
 103  
 104          $template->header = get_string(
 105              'list_' . ($availability->root ? 'root_' : '') .
 106                  ($availability->andoperator ? 'and' : 'or') .
 107                  ($availability->treehidden ? '_hidden' : ''),
 108              'availability'
 109          );
 110  
 111          foreach ($availability->items as $item) {
 112              $this->count++;
 113              if (is_string($item)) {
 114                  $simple_item = $this->get_item_base_template();
 115                  $simple_item->header = $item;
 116                  $template->items[] = $simple_item;
 117              } else {
 118                  $template->items[] = $this->get_item_template($item);
 119              }
 120          }
 121  
 122          $template->hasitems = !empty($template->items);
 123  
 124          return $template;
 125      }
 126  }