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 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   * 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      /**
  44       * Constructor.
  45       *
  46       * @param core_availability_multiple_messages $renderable the availability messages
  47       */
  48      public function __construct(core_availability_multiple_messages $renderable) {
  49          $this->availabilitymessages = $renderable;
  50      }
  51  
  52      /**
  53       * Export this data so it can be used as the context for a mustache template.
  54       *
  55       * @param \renderer_base $output typically, the renderer that's calling this function
  56       * @return stdClass data context for a mustache template
  57       */
  58      public function export_for_template(\renderer_base $output): stdClass {
  59  
  60          $template = $this->get_item_template($this->availabilitymessages);
  61  
  62          $template->id = uniqid();
  63  
  64          return $template;
  65      }
  66  
  67      /**
  68       * Get the item base template.
  69       *
  70       * @return stdClass the template base
  71       */
  72      protected function get_item_base_template(): stdClass {
  73          return (object)[
  74              'id' => false,
  75              'items' => [],
  76              'hasitems' => false,
  77          ];
  78      }
  79  
  80      /**
  81       * Get the item template.
  82       *
  83       * @param core_availability_multiple_messages $availability the availability messages
  84       * @return stdClass the template
  85       */
  86      protected function get_item_template(core_availability_multiple_messages $availability): stdClass {
  87  
  88          $template = $this->get_item_base_template();
  89  
  90          $template->header = $this->get_item_header($availability);
  91  
  92          foreach ($availability->items as $item) {
  93              if (is_string($item)) {
  94                  $simple_item = $this->get_item_base_template();
  95                  $simple_item->header = $item;
  96                  $template->items[] = $simple_item;
  97              } else {
  98                  $template->items[] = $this->get_item_template($item);
  99              }
 100          }
 101  
 102          $template->hasitems = !empty($template->items);
 103  
 104          return $template;
 105      }
 106  
 107      /**
 108       * Get the item header.
 109       * Depending on availability configuration this will return a string from a combined string identifier.
 110       * For example: list_root_and_hidden, list_and, list_root_or_hidden, list_root_or, etc.
 111       *
 112       * @param core_availability_multiple_messages $availability the availability messages
 113       * @return string the item header
 114       */
 115      protected function get_item_header(core_availability_multiple_messages $availability): string {
 116          $stridentifier = 'list_' . ($availability->root ? 'root_' : '') .
 117              ($availability->andoperator ? 'and' : 'or') .
 118              ($availability->treehidden ? '_hidden' : '');
 119  
 120          return get_string($stridentifier, 'availability');
 121      }
 122  }