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  namespace qbank_tagquestion;
  18  
  19  use core_question\local\bank\action_column_base;
  20  use core_question\local\bank\menuable_action;
  21  
  22  /**
  23   * Action to add and remove tags to questions.
  24   *
  25   * @package   qbank_tagquestion
  26   * @copyright 2018 Simey Lameze <simey@moodle.com>
  27   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class tags_action_column extends action_column_base implements menuable_action {
  31  
  32      /**
  33       * @var string store this lang string for performance.
  34       */
  35      protected $managetags;
  36  
  37      /**
  38       * @var bool tags enabled or not from config.
  39       */
  40      protected $tagsenabled = true;
  41  
  42      public function init(): void {
  43          parent::init();
  44          $this->check_tags_status();
  45          if ($this->tagsenabled) {
  46              global $PAGE;
  47              $PAGE->requires->js_call_amd('qbank_tagquestion/edit_tags', 'init', ['#questionscontainer']);
  48          }
  49          $this->managetags = get_string('managetags', 'tag');
  50      }
  51  
  52      protected function check_tags_status(): void {
  53          global $CFG;
  54          if (!$CFG->usetags) {
  55              $this->tagsenabled = false;
  56          }
  57      }
  58  
  59      public function get_name(): string {
  60          return 'tagsaction';
  61      }
  62  
  63      protected function display_content($question, $rowclasses): void {
  64          global $OUTPUT;
  65  
  66          if (\core_tag_tag::is_enabled('core_question', 'question') &&
  67                  question_has_capability_on($question, 'view') && $this->tagsenabled) {
  68  
  69              [$url, $attributes] = $this->get_link_url_and_attributes($question);
  70              echo \html_writer::link($url, $OUTPUT->pix_icon('t/tags',
  71                      $this->managetags), $attributes);
  72          }
  73      }
  74  
  75      protected function get_link_url_and_attributes($question): array {
  76          $url = new \moodle_url($this->qbank->returnurl);
  77  
  78          $attributes = [
  79                  'data-action' => 'edittags',
  80                  'data-cantag' => question_has_capability_on($question, 'tag'),
  81                  'data-contextid' => $this->qbank->get_most_specific_context()->id,
  82                  'data-questionid' => $question->id
  83          ];
  84  
  85          return [$url, $attributes];
  86      }
  87  
  88      public function get_action_menu_link(\stdClass $question): ?\action_menu_link {
  89          if (!\core_tag_tag::is_enabled('core_question', 'question') ||
  90                  !question_has_capability_on($question, 'view') || !$this->tagsenabled) {
  91              return null;
  92          }
  93  
  94          [$url, $attributes] = $this->get_link_url_and_attributes($question);
  95          return new \action_menu_link_secondary($url, new \pix_icon('t/tags', ''),
  96                  $this->managetags, $attributes);
  97      }
  98  }