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\tagareaenabled
  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  
  29  /**
  30   * Class to display tag area enabled control
  31   *
  32   * @package   core_tag
  33   * @copyright 2016 Marina Glancy
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tagareaenabled extends \core\output\inplace_editable {
  37  
  38      /**
  39       * Constructor.
  40       *
  41       * @param \stdClass $tagarea
  42       */
  43      public function __construct($tagarea) {
  44          $editable = has_capability('moodle/tag:manage', context_system::instance());
  45          $value = $tagarea->enabled ? 1 : 0;
  46  
  47          parent::__construct('core_tag', 'tagareaenable', $tagarea->id, $editable, '', $value);
  48          $this->set_type_toggle();
  49      }
  50  
  51      /**
  52       * Export this data so it can be used as the context for a mustache template.
  53       *
  54       * @param \renderer_base $output
  55       * @return \stdClass
  56       */
  57      public function export_for_template(\renderer_base $output) {
  58          if ($this->value) {
  59              $this->edithint = get_string('disable');
  60              $this->displayvalue = $output->pix_icon('i/hide', get_string('disable'));
  61          } else {
  62              $this->edithint = get_string('enable');
  63              $this->displayvalue = $output->pix_icon('i/show', get_string('enable'));
  64          }
  65  
  66          return parent::export_for_template($output);
  67      }
  68  
  69      /**
  70       * Updates the value in database and returns itself, called from inplace_editable callback
  71       *
  72       * @param int $itemid
  73       * @param mixed $newvalue
  74       * @return \self
  75       */
  76      public static function update($itemid, $newvalue) {
  77          global $DB;
  78          require_capability('moodle/tag:manage', context_system::instance());
  79          $tagarea = $DB->get_record('tag_area', array('id' => $itemid), '*', MUST_EXIST);
  80          $newvalue = $newvalue ? 1 : 0;
  81          $data = array('enabled' => $newvalue);
  82          \core_tag_area::update($tagarea, $data);
  83          $tagarea->enabled = $newvalue;
  84          $tmpl = new self($tagarea);
  85          return $tmpl;
  86      }
  87  }