Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
node_catalogue
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 registry
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 describe
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
 events
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 functions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
17namespace tool_autoflow\local;
18
19use tool_autoflow\local\engine\nodes\condition_node;
20use tool_autoflow\local\engine\nodes\log_node;
21use tool_autoflow\local\engine\nodes\set_variable_node;
22use tool_autoflow\local\engine\nodes\trigger_node;
23use tool_autoflow\local\engine\registry;
24use tool_autoflow\local\nodes\add_to_group_node;
25use tool_autoflow\local\nodes\call_function_node;
26use tool_autoflow\local\nodes\enrol_user_node;
27use tool_autoflow\local\nodes\http_request_node;
28use tool_autoflow\local\nodes\send_notification_node;
29
30/**
31 * Every node type this site can use: the engine's own and the Moodle ones.
32 *
33 * @package    tool_autoflow
34 * @copyright  2026 DevLMS <contact@devlms.com>
35 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37final class node_catalogue {
38    /**
39     * The registry used to run workflows.
40     *
41     * @return registry
42     */
43    public static function registry(): registry {
44        return new registry(
45            new trigger_node('trigger.manual'),
46            new trigger_node('trigger.schedule'),
47            new trigger_node('trigger.event'),
48            new condition_node(),
49            new set_variable_node(),
50            new log_node(),
51            new http_request_node(),
52            new send_notification_node(),
53            new enrol_user_node(),
54            new add_to_group_node(),
55            new call_function_node(),
56        );
57    }
58
59    /**
60     * Palette entries for the editor: type, label, category, branches and parameters with labels.
61     *
62     * @return array[]
63     */
64    public static function describe(): array {
65        $strings = get_string_manager();
66        $types = [];
67        foreach (self::registry()->types() as $type) {
68            $node = self::registry()->get($type);
69            $key = 'node:' . $type;
70            $types[] = [
71                'type' => $type,
72                'label' => $strings->string_exists($key, 'tool_autoflow') ? get_string($key, 'tool_autoflow') : $type,
73                'description' => $strings->string_exists("$key:help", 'tool_autoflow')
74                    ? get_string("$key:help", 'tool_autoflow') : '',
75                'category' => strtok($type, '.'),
76                'branches' => $node->branches(),
77                'params' => array_map(function ($param) use ($strings) {
78                    $labels = [];
79                    foreach ($param->options as $option) {
80                        $labels[$option] = get_string("option:$option", 'tool_autoflow');
81                    }
82                    return $param->to_array() + [
83                        'label' => $strings->string_exists("param:{$param->name}", 'tool_autoflow')
84                            ? get_string("param:{$param->name}", 'tool_autoflow') : $param->name,
85                        'optionlabels' => $labels,
86                    ];
87                }, $node->params()),
88            ];
89        }
90        return $types;
91    }
92
93    /**
94     * Every concrete event class on the site, for the event trigger picker.
95     *
96     * @return string[] Class names with a leading backslash, sorted.
97     */
98    public static function events(): array {
99        global $CFG;
100        // Loading a deprecated event class emits debugging; the core event list report silences it the same way.
101        [$debug, $display, $developer] = [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper];
102        [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper] = [0, false, false];
103        $events = [];
104        foreach (array_keys(\core_component::get_component_classes_in_namespace(null, 'event')) as $class) {
105            if (is_a($class, \core\event\base::class, true) && !(new \ReflectionClass($class))->isAbstract()) {
106                $events[] = '\\' . $class;
107            }
108        }
109        [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper] = [$debug, $display, $developer];
110        sort($events);
111        return $events;
112    }
113
114    /**
115     * Every web service function installed on the site, for the call function node.
116     *
117     * @return string[] Sorted names.
118     */
119    public static function functions(): array {
120        global $DB;
121        // Sorted here, not in SQL: database collations order underscores differently.
122        $names = $DB->get_fieldset_select('external_functions', 'name', '1 = 1');
123        sort($names, SORT_STRING);
124        return $names;
125    }
126}