Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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 class core_tag\output\tagindex
  19   *
  20   * @package   core_tag
  21   * @copyright 2015 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_tag\output;
  26  
  27  use renderable;
  28  use templatable;
  29  use renderer_base;
  30  use stdClass;
  31  use moodle_url;
  32  use core_tag_tag;
  33  
  34  /**
  35   * Class to display items tagged with a specific tag
  36   *
  37   * @package   core_tag
  38   * @copyright 2015 Marina Glancy
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class tagindex implements templatable {
  42  
  43      /** @var core_tag_tag|stdClass */
  44      protected $tag;
  45  
  46      /** @var stdClass */
  47      protected $tagarea;
  48  
  49      /** @var stdClass */
  50      protected $record;
  51  
  52      /**
  53       * Constructor
  54       *
  55       * @param core_tag_tag|stdClass $tag
  56       * @param string $component
  57       * @param string $itemtype
  58       * @param string $content
  59       * @param bool $exclusivemode
  60       * @param int $fromctx context id where the link was displayed, may be used by callbacks
  61       *            to display items in the same context first
  62       * @param int $ctx context id where we need to search for items
  63       * @param int $rec search items in sub contexts as well
  64       * @param int $page
  65       * @param bool $totalpages
  66       */
  67      public function __construct($tag, $component, $itemtype, $content,
  68              $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0, $totalpages = 1) {
  69          $this->record = new stdClass();
  70          $this->tag = $tag;
  71  
  72          $tagareas = \core_tag_area::get_areas();
  73          if (!isset($tagareas[$itemtype][$component])) {
  74              throw new \coding_exception('Tag area for component '.$component.' and itemtype '.$itemtype.' is not defined');
  75          }
  76          $this->tagarea = $tagareas[$itemtype][$component];
  77          $this->record->tagid = $tag->id;
  78          $this->record->ta = $this->tagarea->id;
  79          $this->record->itemtype = $itemtype;
  80          $this->record->component = $component;
  81  
  82          $a = (object)array(
  83              'tagarea' => \core_tag_area::display_name($component, $itemtype),
  84              'tag' => \core_tag_tag::make_display_name($tag)
  85          );
  86          if ($exclusivemode) {
  87              $this->record->title = get_string('itemstaggedwith', 'tag', $a);
  88          } else {
  89              $this->record->title = (string)$a->tagarea;
  90          }
  91          $this->record->content = $content;
  92  
  93          $this->record->nextpageurl = null;
  94          $this->record->prevpageurl = null;
  95          $this->record->exclusiveurl = null;
  96  
  97          $url = core_tag_tag::make_url($tag->tagcollid, $tag->rawname, $exclusivemode, $fromctx, $ctx, $rec);
  98          $urlparams = array('ta' => $this->tagarea->id);
  99          if ($totalpages > $page + 1) {
 100              $this->record->nextpageurl = new moodle_url($url, $urlparams + array('page' => $page + 1));
 101          }
 102          if ($page > 0) {
 103              $this->record->prevpageurl = new moodle_url($url, $urlparams + array('page' => $page - 1));
 104          }
 105          if (!$exclusivemode && ($totalpages > 1 || $page)) {
 106              $this->record->exclusiveurl = new moodle_url($url, $urlparams + array('excl' => 1));
 107          }
 108          $this->record->exclusivetext = get_string('exclusivemode', 'tag', $a);
 109          $this->record->hascontent = ($totalpages > 1 || $page || $content);
 110          $this->record->anchor = $component . '_' . $itemtype;
 111      }
 112  
 113      /**
 114       * Magic setter
 115       *
 116       * @param string $name
 117       * @param mixed $value
 118       */
 119      public function __set($name, $value) {
 120          $this->record->$name = $value;
 121      }
 122  
 123      /**
 124       * Magic getter
 125       *
 126       * @param string $name
 127       * @return mixed
 128       */
 129      public function __get($name) {
 130          return $this->record->$name;
 131      }
 132  
 133      /**
 134       * Magic isset
 135       *
 136       * @param string $name
 137       * @return bool
 138       */
 139      public function __isset($name) {
 140          return isset($this->record->$name);
 141      }
 142  
 143      /**
 144       * Export this data so it can be used as the context for a mustache template.
 145       *
 146       * @param renderer_base $output
 147       * @return stdClass
 148       */
 149      public function export_for_template(renderer_base $output) {
 150          if ($this->record->nextpageurl && $this->record->nextpageurl instanceof moodle_url) {
 151              $this->record->nextpageurl = $this->record->nextpageurl->out(false);
 152          }
 153          if ($this->record->prevpageurl && $this->record->prevpageurl instanceof moodle_url) {
 154              $this->record->prevpageurl = $this->record->prevpageurl->out(false);
 155          }
 156          if ($this->record->exclusiveurl && $this->record->exclusiveurl instanceof moodle_url) {
 157              $this->record->exclusiveurl = $this->record->exclusiveurl->out(false);
 158          }
 159          return $this->record;
 160      }
 161  }