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 * The question tags column subclass.
19 *
20 * @package   core_question
21 * @copyright 2018 Simey Lameze <simey@moodle.com>
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_question\bank;
25defined('MOODLE_INTERNAL') || die();
26
27
28/**
29 * Action to add and remove tags to questions.
30 *
31 * @copyright 2018 Simey Lameze <simey@moodle.com>
32 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tags_action_column extends action_column_base implements menuable_action {
35    /**
36     * @var string store this lang string for performance.
37     */
38    protected $managetags;
39
40    public function init() {
41        parent::init();
42        $this->managetags = get_string('managetags', 'tag');
43    }
44
45    /**
46     * Return the name for this column.
47     *
48     * @return string
49     */
50    public function get_name() {
51        return 'tagsaction';
52    }
53
54    /**
55     * Display tags column content.
56     *
57     * @param object $question The question database record.
58     * @param string $rowclasses
59     */
60    protected function display_content($question, $rowclasses) {
61        global $OUTPUT;
62
63        if (\core_tag_tag::is_enabled('core_question', 'question') &&
64                question_has_capability_on($question, 'view')) {
65
66            [$url, $attributes] = $this->get_link_url_and_attributes($question);
67            echo \html_writer::link($url, $OUTPUT->pix_icon('t/tags',
68                    $this->managetags), $attributes);
69        }
70    }
71
72    /**
73     * Helper used by display_content and get_action_menu_link.
74     *
75     * @param object $question the row from the $question table, augmented with extra information.
76     * @return array with two elements, \moodle_url and
77     *     an array or data $attributes needed to make the JavaScript work.
78     */
79    protected function get_link_url_and_attributes($question) {
80        $url = new \moodle_url($this->qbank->edit_question_url($question->id));
81
82        $attributes = [
83                'data-action' => 'edittags',
84                'data-cantag' => question_has_capability_on($question, 'tag'),
85                'data-contextid' => $this->qbank->get_most_specific_context()->id,
86                'data-questionid' => $question->id
87        ];
88
89        return [$url, $attributes];
90    }
91
92    public function get_action_menu_link(\stdClass $question): ?\action_menu_link {
93        if (!\core_tag_tag::is_enabled('core_question', 'question') ||
94                !question_has_capability_on($question, 'view')) {
95            return null;
96        }
97
98        [$url, $attributes] = $this->get_link_url_and_attributes($question);
99        return new \action_menu_link_secondary($url, new \pix_icon('t/tags', ''),
100                $this->managetags, $attributes);
101    }
102}
103