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 * Base class to make it easier to implement actions that are menuable_actions.
19 *
20 * @package   core_question
21 * @copyright 2019 Tim Hunt
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_question\bank;
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Base class to make it easier to implement actions that are menuable_actions.
31 *
32 * Use this class if your action is simple (defined by just a URL, label and icon).
33 * If your action is not simple enough to fit into the pattern that this
34 * class implements, then you will have to implement the menuable_action
35 * interface yourself.
36 *
37 * @copyright 2019 Tim Hunt
38 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40abstract class menu_action_column_base extends action_column_base implements menuable_action {
41
42    /**
43     * Get the information required to display this action either as a menu item or a separate action column.
44     *
45     * If this action cannot apply to this question (e.g. because the user does not have
46     * permission, then return [null, null, null].
47     *
48     * @param \stdClass $question the row from the $question table, augmented with extra information.
49     * @return array with three elements.
50     *      $url - the URL to perform the action.
51     *      $icon - the icon for this action. E.g. 't/delete'.
52     *      $label - text label to display in the UI (either in the menu, or as a tool-tip on the icon)
53     */
54    abstract protected function get_url_icon_and_label(\stdClass $question): array;
55
56    protected function display_content($question, $rowclasses) {
57        [$url, $icon, $label] = $this->get_url_icon_and_label($question);
58        if ($url) {
59            $this->print_icon($icon, $label, $url);
60        }
61    }
62
63    public function get_action_menu_link(\stdClass $question): ?\action_menu_link {
64        [$url, $icon, $label] = $this->get_url_icon_and_label($question);
65        if (!$url) {
66            return null;
67        }
68        return new \action_menu_link_secondary($url, new \pix_icon($icon, ''), $label);
69    }
70}
71