79a7cbca5b7435d422bf4b26a196f4c9b1cf8bf3
[moodle.git] / question / classes / bank / tags_action_column.php
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/>.
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  */
24 namespace core_question\bank;
26 defined('MOODLE_INTERNAL') || die();
28 /**
29  * Action to add and remove tags to questions.
30  *
31  * @package    core_question
32  * @copyright  2018 Simey Lameze <simey@moodle.com>
33  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 class tags_action_column extends action_column_base {
37     /**
38      * Return the name for this column.
39      *
40      * @return string
41      */
42     public function get_name() {
43         return 'tagsaction';
44     }
46     /**
47      * Display tags column content.
48      *
49      * @param object $question The question database record.
50      * @param string $rowclasses
51      */
52     protected function display_content($question, $rowclasses) {
53         global $DB;
55         if (\core_tag_tag::is_enabled('core_question', 'question') &&
56                 question_has_capability_on($question, 'view')) {
58             $cantag = question_has_capability_on($question, 'tag');
59             $qbank = $this->qbank;
60             $url = $qbank->edit_question_url($question->id);
61             $editingcontext = $qbank->get_most_specific_context();
63             $this->print_tag_icon($question->id, $url, $cantag, $editingcontext->id);
64         }
65     }
67     /**
68      * Build and print the tags icon.
69      *
70      * @param int $id The question ID.
71      * @param string $url Editing question url.
72      * @param bool $cantag Whether the user can tag questions or not.
73      * @param int $contextid Question category context ID.
74      */
75     protected function print_tag_icon($id, $url, $cantag, $contextid) {
76         global $OUTPUT;
78         $params = [
79             'data-action' => 'edittags',
80             'data-cantag' => $cantag,
81             'data-contextid' => $contextid,
82             'data-questionid' => $id
83         ];
85         echo \html_writer::link($url, $OUTPUT->pix_icon('t/tags', get_string('managetags', 'tag')), $params);
86     }
87 }