Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Tags block.
  19   *
  20   * @package   block_tags
  21   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  class block_tags extends block_base {
  26      public function init() {
  27          $this->title = get_string('pluginname', 'block_tags');
  28      }
  29  
  30      public function instance_allow_multiple() {
  31          return true;
  32      }
  33  
  34      public function applicable_formats() {
  35          return array('all' => true);
  36      }
  37  
  38      public function instance_allow_config() {
  39          return true;
  40      }
  41  
  42      public function specialization() {
  43  
  44          // Load userdefined title and make sure it's never empty.
  45          if (empty($this->config->title)) {
  46              $this->title = get_string('pluginname', 'block_tags');
  47          } else {
  48              $this->title = format_string($this->config->title, true, ['context' => $this->context]);
  49          }
  50      }
  51  
  52      public function get_content() {
  53  
  54          global $CFG, $COURSE, $USER, $SCRIPT, $OUTPUT;
  55  
  56          if (empty($CFG->usetags)) {
  57              $this->content = new stdClass();
  58              $this->content->text = '';
  59              if ($this->page->user_is_editing()) {
  60                  $this->content->text = get_string('disabledtags', 'block_tags');
  61              }
  62              return $this->content;
  63          }
  64  
  65          if (!isset($this->config)) {
  66              $this->config = new stdClass();
  67          }
  68  
  69          if (empty($this->config->numberoftags)) {
  70              $this->config->numberoftags = 80;
  71          }
  72  
  73          if (empty($this->config->showstandard)) {
  74              $this->config->showstandard = core_tag_tag::BOTH_STANDARD_AND_NOT;
  75          }
  76  
  77          if (empty($this->config->ctx)) {
  78              $this->config->ctx = 0;
  79          }
  80  
  81          if (empty($this->config->rec)) {
  82              $this->config->rec = 1;
  83          }
  84  
  85          if (empty($this->config->tagcoll)) {
  86              $this->config->tagcoll = 0;
  87          }
  88  
  89          if ($this->content !== NULL) {
  90              return $this->content;
  91          }
  92  
  93          if (empty($this->instance)) {
  94              $this->content = '';
  95              return $this->content;
  96          }
  97  
  98          $this->content = new stdClass;
  99          $this->content->text = '';
 100          $this->content->footer = '';
 101  
 102          // Get a list of tags.
 103  
 104          $tagcloud = core_tag_collection::get_tag_cloud($this->config->tagcoll,
 105                  $this->config->showstandard == core_tag_tag::STANDARD_ONLY,
 106                  $this->config->numberoftags,
 107                  'name', '', $this->page->context->id, $this->config->ctx, $this->config->rec);
 108          $this->content->text = $OUTPUT->render_from_template('core_tag/tagcloud', $tagcloud->export_for_template($OUTPUT));
 109  
 110          return $this->content;
 111      }
 112  
 113      /**
 114       * Return the plugin config settings for external functions.
 115       *
 116       * @return stdClass the configs for both the block instance and plugin
 117       * @since Moodle 3.8
 118       */
 119      public function get_config_for_external() {
 120          // Return all settings for all users since it is safe (no private keys, etc..).
 121          $configs = !empty($this->config) ? $this->config : new stdClass();
 122  
 123          return (object) [
 124              'instance' => $configs,
 125              'plugin' => new stdClass(),
 126          ];
 127      }
 128  
 129      /**
 130       * This block shouldn't be added to a page if the tags advanced feature is disabled.
 131       *
 132       * @param moodle_page $page
 133       * @return bool
 134       */
 135      public function can_block_be_added(moodle_page $page): bool {
 136          global $CFG;
 137  
 138          return $CFG->usetags;
 139      }
 140  }