MDL-61133 core_question: add tags column class to question list
authorSimey Lameze <simey@moodle.com>
Wed, 31 Jan 2018 05:38:23 +0000 (13:38 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 5 Feb 2018 02:57:04 +0000 (10:57 +0800)
question/classes/bank/tags_action_column.php [new file with mode: 0644]
question/classes/bank/view.php

diff --git a/question/classes/bank/tags_action_column.php b/question/classes/bank/tags_action_column.php
new file mode 100644 (file)
index 0000000..5f49808
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The question tags column subclass.
+ *
+ * @package   core_question
+ * @copyright 2018 Simey Lameze <simey@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core_question\bank;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Action to add and remove tags to questions.
+ *
+ * @package    core_question
+ * @copyright  2018 Simey Lameze <simey@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tags_action_column extends action_column_base {
+
+    /**
+     * Return the name for this column.
+     *
+     * @return string
+     */
+    public function get_name() {
+        return 'tagsaction';
+    }
+
+    /**
+     * Display tags column content.
+     *
+     * @param object $question The question database record.
+     * @param string $rowclasses
+     */
+    protected function display_content($question, $rowclasses) {
+        global $DB;
+
+        if (\core_tag_tag::is_enabled('core_question', 'question') &&
+                question_has_capability_on($question, 'view')) {
+
+            $canedit = question_has_capability_on($question, 'edit');
+            $category = $DB->get_record('question_categories', ['id' => $question->category], 'contextid');
+            $url = $this->qbank->edit_question_url($question->id);
+
+            $this->print_tag_icon($question->id, $url, $canedit, $category->contextid);
+        }
+    }
+
+    /**
+     * Build and print the tags icon.
+     *
+     * @param int $id The question ID.
+     * @param string $url Editing question url.
+     * @param bool $canedit Whether the user can edit questions or not.
+     * @param int $contextid Question category context ID.
+     */
+    protected function print_tag_icon($id, $url, $canedit, $contextid) {
+        global $OUTPUT;
+
+        $params = [
+            'data-action' => 'edittags',
+            'data-canedit' => $canedit,
+            'data-contextid' => $contextid,
+            'data-questionid' => $id
+        ];
+
+        echo \html_writer::link($url, $OUTPUT->pix_icon('t/tags', get_string('managetags', 'tag')), $params);
+    }
+}
index 8c4142b..edeef00 100644 (file)
@@ -124,10 +124,9 @@ class view {
 
         if (empty($CFG->questionbankcolumns)) {
             $questionbankcolumns = array('checkbox_column', 'question_type_column',
-                                     'question_name_column', 'edit_action_column', 'copy_action_column',
-                                     'preview_action_column', 'delete_action_column',
-                                     'creator_name_column',
-                                     'modifier_name_column');
+                                     'question_name_column', 'tags_action_column', 'edit_action_column',
+                                     'copy_action_column', 'preview_action_column', 'delete_action_column',
+                                     'creator_name_column', 'modifier_name_column');
         } else {
              $questionbankcolumns = explode(',', $CFG->questionbankcolumns);
         }