Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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\taglist
  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 templatable;
  28  use renderer_base;
  29  use stdClass;
  30  use core_tag_tag;
  31  use context;
  32  
  33  /**
  34   * Class to preapare a list of tags for display, usually the list of tags some entry is tagged with.
  35   *
  36   * @package   core_tag
  37   * @copyright 2015 Marina Glancy
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class taglist implements templatable {
  41  
  42      /** @var array */
  43      protected $tags;
  44  
  45      /** @var string */
  46      protected $label;
  47  
  48      /** @var bool $accesshidelabel if true, the label should have class="accesshide" added. */
  49      protected $accesshidelabel;
  50  
  51      /** @var string */
  52      protected $classes;
  53  
  54      /** @var int */
  55      protected $limit;
  56  
  57      /**
  58       * Constructor
  59       *
  60       * @param array $tags list of instances of \core_tag_tag or \stdClass
  61       * @param string $label label to display in front, by default 'Tags' (get_string('tags')), set to null
  62       *               to use default, set to '' (empty string) to omit the label completely
  63       * @param string $classes additional classes for the enclosing div element
  64       * @param int $limit limit the number of tags to display, if size of $tags is more than this limit the "more" link
  65       *               will be appended to the end, JS will toggle the rest of the tags. 0 means no limit.
  66       * @param context $pagecontext specify if needed to overwrite the current page context for the view tag link
  67       * @param bool $accesshidelabel if true, the label should have class="accesshide" added.
  68       */
  69      public function __construct($tags, $label = null, $classes = '',
  70              $limit = 10, $pagecontext = null, $accesshidelabel = false) {
  71          global $PAGE;
  72          $canmanagetags = has_capability('moodle/tag:manage', \context_system::instance());
  73  
  74          $this->label = ($label === null) ? get_string('tags') : $label;
  75          $this->accesshidelabel = $accesshidelabel;
  76          $this->classes = $classes;
  77          $fromctx = $pagecontext ? $pagecontext->id :
  78                  (($PAGE->context->contextlevel == CONTEXT_SYSTEM) ? 0 : $PAGE->context->id);
  79  
  80          $this->tags = array();
  81          foreach ($tags as $idx => $tag) {
  82              $this->tags[$idx] = new stdClass();
  83  
  84              $this->tags[$idx]->name = core_tag_tag::make_display_name($tag, false);
  85  
  86              if ($canmanagetags && !empty($tag->flag)) {
  87                  $this->tags[$idx]->flag = 1;
  88              }
  89  
  90              $viewurl = core_tag_tag::make_url($tag->tagcollid, $tag->rawname, 0, $fromctx);
  91              $this->tags[$idx]->viewurl = $viewurl->out(false);
  92  
  93              if (isset($tag->isstandard)) {
  94                  $this->tags[$idx]->isstandard = $tag->isstandard ? 1 : 0;
  95              }
  96  
  97              if ($limit && count($this->tags) > $limit) {
  98                  $this->tags[$idx]->overlimit = 1;
  99              }
 100          }
 101          $this->limit = $limit;
 102      }
 103  
 104      /**
 105       * Export this data so it can be used as the context for a mustache template.
 106       *
 107       * @param renderer_base $output
 108       * @return stdClass
 109       */
 110      public function export_for_template(renderer_base $output) {
 111          $cnt = count($this->tags);
 112          return (object)array(
 113              'tags' => array_values($this->tags),
 114              'label' => $this->label,
 115              'accesshidelabel' => $this->accesshidelabel,
 116              'tagscount' => $cnt,
 117              'overflow' => ($this->limit && $cnt > $this->limit) ? 1 : 0,
 118              'classes' => $this->classes,
 119          );
 120      }
 121  }