7ff372bb048c08064f75025eccd134ce14f77083
[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             $category = $DB->get_record('question_categories', ['id' => $question->category], 'contextid');
60             $url = $this->qbank->edit_question_url($question->id);
62             $this->print_tag_icon($question->id, $url, $cantag, $category->contextid);
63         }
64     }
66     /**
67      * Build and print the tags icon.
68      *
69      * @param int $id The question ID.
70      * @param string $url Editing question url.
71      * @param bool $cantag Whether the user can tag questions or not.
72      * @param int $contextid Question category context ID.
73      */
74     protected function print_tag_icon($id, $url, $cantag, $contextid) {
75         global $OUTPUT;
77         $params = [
78             'data-action' => 'edittags',
79             'data-cantag' => $cantag,
80             'data-contextid' => $contextid,
81             'data-questionid' => $id
82         ];
84         echo \html_writer::link($url, $OUTPUT->pix_icon('t/tags', get_string('managetags', 'tag')), $params);
85     }
86 }