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.
   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\tagareacollection
  19   *
  20   * @package   core_tag
  21   * @copyright 2016 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 context_system;
  28  use lang_string;
  29  use core_tag_area;
  30  
  31  /**
  32   * Class to display collection select for the tag area
  33   *
  34   * @package   core_tag
  35   * @copyright 2016 Marina Glancy
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class tagareacollection extends \core\output\inplace_editable {
  39  
  40      /**
  41       * Constructor.
  42       *
  43       * @param \stdClass $tagarea
  44       */
  45      public function __construct($tagarea) {
  46          if (!empty($tagarea->locked)) {
  47              // If the tag collection for the current tag area is locked, display the
  48              // name of the collection without possibility to edit it.
  49              $tagcoll = \core_tag_collection::get_by_id($tagarea->tagcollid);
  50              parent::__construct('core_tag', 'tagareacollection', $tagarea->id, false,
  51                  \core_tag_collection::display_name($tagcoll), $tagarea->tagcollid);
  52              return;
  53          }
  54  
  55          $tagcollections = \core_tag_collection::get_collections_menu(true);
  56          $editable = (count($tagcollections) > 1) &&
  57                  has_capability('moodle/tag:manage', context_system::instance());
  58          $areaname = core_tag_area::display_name($tagarea->component, $tagarea->itemtype);
  59          $edithint = new lang_string('edittagcollection', 'core_tag');
  60          $editlabel = new lang_string('changetagcoll', 'core_tag', $areaname);
  61          $value = $tagarea->tagcollid;
  62  
  63          parent::__construct('core_tag', 'tagareacollection', $tagarea->id, $editable,
  64                  null, $value, $edithint, $editlabel);
  65          $this->set_type_select($tagcollections);
  66      }
  67  
  68      /**
  69       * Updates the value in database and returns itself, called from inplace_editable callback
  70       *
  71       * @param int $itemid
  72       * @param mixed $newvalue
  73       * @return \self
  74       */
  75      public static function update($itemid, $newvalue) {
  76          global $DB;
  77          require_capability('moodle/tag:manage', \context_system::instance());
  78          $tagarea = $DB->get_record('tag_area', array('id' => $itemid), '*', MUST_EXIST);
  79          $newvalue = clean_param($newvalue, PARAM_INT);
  80          $tagcollections = \core_tag_collection::get_collections_menu(true);
  81          if (!array_key_exists($newvalue, $tagcollections)) {
  82              throw new \moodle_exception('invalidparameter', 'debug');
  83          }
  84          $data = array('tagcollid' => $newvalue);
  85          core_tag_area::update($tagarea, $data);
  86          $tagarea->tagcollid = $newvalue;
  87          $tmpl = new self($tagarea);
  88          return $tmpl;
  89      }
  90  }