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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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  /**
  18   * The chooser_item renderable.
  19   *
  20   * @package    core
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\output;
  26  
  27  use coding_exception;
  28  use context;
  29  use pix_icon;
  30  use renderer_base;
  31  use renderable;
  32  use stdClass;
  33  use templatable;
  34  
  35  /**
  36   * The chooser_item renderable class.
  37   *
  38   * @package    core
  39   * @copyright  2016 Frédéric Massart - FMCorz.net
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class chooser_item implements renderable, templatable {
  43  
  44      /** @var string An identifier for the item. */
  45      public $id;
  46      /** @var string The label of this item. */
  47      public $label;
  48      /** @var string The value this item represents. */
  49      public $value;
  50      /** @var pix_icon The icon for this item. */
  51      public $icon;
  52      /** @var string The item description. */
  53      public $description;
  54      /** @var context The relevant context. */
  55      public $context;
  56  
  57      /**
  58       * Constructor.
  59       */
  60      public function __construct($id, $label, $value, pix_icon $icon, $description = null, context $context = null) {
  61          $this->id = $id;
  62          $this->label = $label;
  63          $this->value = $value;
  64          $this->icon = $icon;
  65          $this->description = $description;
  66  
  67          if (!empty($description) && empty($context)) {
  68              throw new coding_exception('The context must be passed when there is a description.');
  69          }
  70          $this->context = $context;
  71      }
  72  
  73      /**
  74       * Export for template.
  75       *
  76       * @param renderer_base  The renderer.
  77       * @return stdClass
  78       */
  79      public function export_for_template(renderer_base $output) {
  80          $data = new stdClass();
  81          $data->id = $this->id;
  82          $data->label = $this->label;
  83          $data->value = $this->value;
  84          $data->icon = $this->icon->export_for_template($output);
  85  
  86          $options = new stdClass();
  87          $options->trusted = false;
  88          $options->noclean = false;
  89          $options->smiley = false;
  90          $options->filter = false;
  91          $options->para = true;
  92          $options->newlines = false;
  93          $options->overflowdiv = false;
  94  
  95          $data->description = '';
  96          if (!empty($this->description)) {
  97              list($data->description) = \core_external\util::format_text((string) $this->description, FORMAT_MARKDOWN,
  98                  $this->context->id, null, null, null, $options);
  99          }
 100  
 101          return $data;
 102      }
 103  
 104  }