Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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