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  namespace mod_feedback\output;
  18  
  19  use context_module;
  20  use renderable;
  21  use renderer_base;
  22  use templatable;
  23  
  24  /**
  25   * Class base_action_bar
  26   *
  27   * Base class to be inherited by any other feedback action bar
  28   *
  29   * @package     mod_feedback
  30   * @copyright   2021 onwards Peter Dias
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  abstract class base_action_bar implements renderable, templatable {
  34      /** @var int $cmid The module id */
  35      protected $cmid;
  36      /** @var object $context The context we are in */
  37      protected $context;
  38      /** @var object $course The course we are in */
  39      protected $course;
  40      /** @var array $urlparams The default params to be used when creating urls */
  41      protected $urlparams;
  42      /** @var object $feedback The activity record that is being viewed */
  43      protected $feedback;
  44  
  45      /**
  46       * base_action_bar constructor.
  47       *
  48       * @param int $cmid
  49       */
  50      public function __construct(int $cmid) {
  51          global $PAGE;
  52          $this->cmid = $cmid;
  53          $this->context = context_module::instance($cmid);
  54          [$course, $cm] = get_course_and_cm_from_cmid($cmid);
  55          $this->course = $course;
  56          $this->urlparams = [
  57              'id' => $cmid
  58          ];
  59          $this->feedback = $PAGE->activityrecord;
  60      }
  61  
  62      /**
  63       * Recursively iterates through to array of renderables and exports
  64       *
  65       * @param array $items Collection of renderables
  66       * @param renderer_base $output
  67       * @return array $items Data to be used in the mustache template
  68       */
  69      private function export_items_for_template(array $items, renderer_base $output): array {
  70          $items = array_map(function($item) use ($output) {
  71              if (is_array($item)) {
  72                  return $this->export_items_for_template($item, $output);
  73              }
  74  
  75              if (is_object($item) && method_exists($item, 'export_for_template')) {
  76                  return $item->export_for_template($output);
  77              }
  78  
  79              return $item;
  80          }, $items);
  81  
  82          return $items;
  83      }
  84  
  85      /**
  86       * Export the data so it can be used as the context for a mustache template.
  87       *
  88       * @param renderer_base $output
  89       * @return array
  90       */
  91      public function export_for_template(renderer_base $output): array {
  92          $items = $this->export_items_for_template($this->get_items(), $output);
  93          return $items;
  94      }
  95  
  96      /**
  97       * Function to generate a list of renderables to be displayed
  98       * @return array
  99       */
 100      abstract protected function get_items(): array;
 101  }