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   * Contains the default activity name inplace editable.
  19   *
  20   * @package   core_courseformat
  21   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_courseformat\output\local\content\cm;
  26  
  27  use cm_info;
  28  use core\output\named_templatable;
  29  use core_courseformat\base as course_format;
  30  use core_courseformat\output\local\courseformat_named_templatable;
  31  use renderable;
  32  use section_info;
  33  use stdClass;
  34  
  35  /**
  36   * Base class to render a course module inplace editable header.
  37   *
  38   * @package   core_courseformat
  39   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class cmname implements named_templatable, renderable {
  43  
  44      use courseformat_named_templatable;
  45  
  46      /** @var course_format the course format */
  47      protected $format;
  48  
  49      /** @var section_info the section object */
  50      private $section;
  51  
  52      /** @var cm_info the course module instance */
  53      protected $mod;
  54  
  55      /** @var array optional display options */
  56      protected $displayoptions;
  57  
  58      /** @var string the activity title output class name */
  59      protected $titleclass;
  60  
  61      /** @var string the activity icon output class name */
  62      protected $iconclass;
  63  
  64      /**
  65       * Constructor.
  66       *
  67       * @param course_format $format the course format
  68       * @param section_info $section the section info
  69       * @param cm_info $mod the course module ionfo
  70       * @param null $unused This parameter has been deprecated since 4.1 and should not be used anymore.
  71       * @param array $displayoptions optional extra display options
  72       */
  73      public function __construct(
  74          course_format $format,
  75          section_info $section,
  76          cm_info $mod,
  77          ?bool $unused = null,
  78          array $displayoptions = []
  79      ) {
  80          if ($unused !== null) {
  81              debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
  82          }
  83  
  84          $this->format = $format;
  85          $this->section = $section;
  86          $this->mod = $mod;
  87          $this->displayoptions = $displayoptions;
  88  
  89          // Get the necessary classes.
  90          $this->titleclass = $format->get_output_classname('content\\cm\\title');
  91          $this->iconclass = $format->get_output_classname('content\\cm\\cmicon');
  92      }
  93  
  94      /**
  95       * Export this data so it can be used as the context for a mustache template.
  96       *
  97       * @param \renderer_base $output typically, the renderer that's calling this function
  98       * @return stdClass data context for a mustache template
  99       */
 100      public function export_for_template(\renderer_base $output): array {
 101          $mod = $this->mod;
 102          $displayoptions = $this->displayoptions;
 103  
 104          if (!$this->has_name()) {
 105              // Nothing to be displayed to the user.
 106              return [];
 107          }
 108  
 109          $data = [
 110              'url' => $mod->url,
 111              'modname' => $mod->modname,
 112              'textclasses' => $displayoptions['textclasses'] ?? '',
 113              'activityicon' => $this->get_icon_data($output),
 114              'activityname' => $this->get_title_data($output),
 115          ];
 116  
 117          return $data;
 118      }
 119  
 120      /**
 121       * Get the title data.
 122       *
 123       * @param \renderer_base $output typically, the renderer that's calling this function
 124       * @return array data context for a mustache template
 125       */
 126      protected function get_title_data(\renderer_base $output): array {
 127          $title = new $this->titleclass(
 128              $this->format,
 129              $this->section,
 130              $this->mod,
 131              $this->displayoptions
 132          );
 133          return (array) $title->export_for_template($output);
 134      }
 135  
 136      /**
 137       * Get the icon data.
 138       *
 139       * @param \renderer_base $output typically, the renderer that's calling this function
 140       * @return array data context for a mustache template
 141       */
 142      protected function get_icon_data(\renderer_base $output): array {
 143          $icon = new $this->iconclass(
 144              $this->format,
 145              $this->mod,
 146          );
 147          return (array) $icon->export_for_template($output);
 148      }
 149  
 150      /**
 151       * Return if the activity has a visible name.
 152       *
 153       * @return bool if the title is visible.
 154       */
 155      public function has_name(): bool {
 156          return $this->mod->is_visible_on_course_page() && $this->mod->url;
 157      }
 158  }