Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_user\hook;
18
 
19
use action_link;
20
use core\hook\described_hook;
21
use core\hook\deprecated_callback_replacement;
22
 
23
/**
24
 * Class extend_bulk_user_actions
25
 *
26
 * @package    core_user
27
 * @copyright  2024 Marina Glancy
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class extend_bulk_user_actions implements deprecated_callback_replacement, described_hook {
31
    /**
32
     * Describes the hook purpose.
33
     *
34
     * @return string
35
     */
36
    public static function get_hook_description(): string {
37
        return 'Extend bulk user actions menu';
38
    }
39
 
40
    /**
41
     * List of tags that describe this hook.
42
     *
43
     * @return string[]
44
     */
45
    public static function get_hook_tags(): array {
46
        return ['user'];
47
    }
48
 
49
    /**
50
     * Returns list of lib.php plugin callbacks that were deprecated by the hook.
51
     *
52
     * @return array
53
     */
54
    public static function get_deprecated_plugin_callbacks(): array {
55
        return ['bulk_user_actions'];
56
    }
57
 
58
    /** @var array Stores all added user actions */
59
    private $actions = [];
60
 
61
    /**
62
     * To be called by callback to add an action to the bulk user actions menu
63
     *
64
     * Callbacks with higher priority will be called first and actions they added will be displayed first.
65
     * Callbacks with lower priority can override actions added by callbacks with higher priority.
66
     *
67
     * To prevent accidental overrides plugins should prefix the action identifier with the plugin name.
68
     *
69
     * @param string $identifier Unique key for the action, recommended to prefix with plugin name
70
     * @param action_link|null $action an object containing the action URL and text,
71
     *        other properties are ignored. Can be set to null to remove an action added by somebody else.
72
     * @param ?string $category Label for the option group in the action select dropdown
73
     */
74
    public function add_action(string $identifier, ?action_link $action, ?string $category = null): void {
75
        $category = $category ?? get_string('actions', 'moodle');
76
 
77
        // If an action with the same identifier already exists in another option group, remove it.
78
        $oldcategory = $this->find_action_category($identifier);
79
        if ($oldcategory !== null && ($oldcategory !== $category || $action === null)) {
80
            unset($this->actions[$oldcategory][$identifier]);
81
            if (empty($this->actions[$oldcategory])) {
82
                unset($this->actions[$oldcategory]);
83
            }
84
        }
85
 
86
        // Add the new action.
87
        if ($action !== null) {
88
            $this->actions += [$category => []];
89
            $this->actions[$category][$identifier] = $action;
90
        }
91
    }
92
 
93
    /**
94
     * Returns all actions groupped by category
95
     *
96
     * @return action_link[][]
97
     */
98
    public function get_actions(): array {
99
        return $this->actions;
100
    }
101
 
102
    /**
103
     * Allows to locate an action by the identifier
104
     *
105
     * This method returns the option group label. The action itself can be found as:
106
     *    $category = $this->find_action_category($identifier);
107
     *    $action = $this->get_actions()[$category][$identifier];
108
     *
109
     * @param string $identifier
110
     * @return string|null
111
     */
112
    public function find_action_category(string $identifier): ?string {
113
        foreach ($this->actions as $category => $actions) {
114
            if (array_key_exists($identifier, $actions)) {
115
                return $category;
116
            }
117
        }
118
        return null;
119
    }
120
}