Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
get_editor
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 execute_parameters
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
4
 execute_returns
100.00% covered (success)
100.00%
13 / 13
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\external;
18
19use core_external\external_api;
20use core_external\external_function_parameters;
21use core_external\external_single_structure;
22use core_external\external_value;
23use tool_autoflow\local\node_catalogue;
24use tool_autoflow\local\workflow_store;
25
26/**
27 * Everything the editor needs to open a workflow: the workflow, the node palette,
28 * the event and function lists and the interface strings.
29 *
30 * @package    tool_autoflow
31 * @copyright  2026 DevLMS <contact@devlms.com>
32 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class get_editor extends external_api {
35    /** Strings the editor shows, by key. */
36    private const STRINGS = ['save', 'saved', 'runnow', 'runqueued', 'enabled', 'workflowname', 'palette', 'nodesettings',
37        'selectnode', 'deletenode', 'problems', 'saving', 'savefailed', 'branch:true', 'branch:false', 'choose', 'workflowruns',
38        'cat:trigger', 'cat:logic', 'cat:action', 'cat:moodle', 'error:notrigger', 'error:manytriggers', 'error:duplicateid',
39        'error:danglingedge', 'error:cycle', 'error:unreachable', 'invalidjson', 'runsas', 'runasuserid'];
40
41    /**
42     * Parameters.
43     *
44     * @return external_function_parameters
45     */
46    public static function execute_parameters(): external_function_parameters {
47        return new external_function_parameters([
48            'workflowid' => new external_value(PARAM_INT, 'Workflow id, 0 for a new workflow'),
49        ]);
50    }
51
52    /**
53     * Load the editor data.
54     *
55     * @param int $workflowid
56     * @return array
57     */
58    public static function execute(int $workflowid): array {
59        ['workflowid' => $workflowid] = self::validate_parameters(self::execute_parameters(), ['workflowid' => $workflowid]);
60        $context = \context_system::instance();
61        self::validate_context($context);
62        require_capability('tool/autoflow:manage', $context);
63
64        global $USER;
65        $workflow = $workflowid ? workflow_store::get($workflowid)
66            : (object) ['id' => 0, 'name' => '', 'enabled' => 0, 'graph' => json_encode(['nodes' => [], 'edges' => []]),
67                'runasuserid' => $USER->id];
68        $runas = \core_user::get_user((int) $workflow->runasuserid) ?: $USER;
69        $strings = [];
70        foreach (self::STRINGS as $key) {
71            $strings[$key] = get_string($key, 'tool_autoflow');
72        }
73        return [
74            'id' => (int) $workflow->id,
75            'name' => $workflow->name,
76            'enabled' => (bool) $workflow->enabled,
77            'graph' => $workflow->graph,
78            'nodetypes' => json_encode(node_catalogue::describe()),
79            'events' => json_encode(node_catalogue::events()),
80            'functions' => json_encode(node_catalogue::functions()),
81            'strings' => json_encode($strings),
82            'runasuserid' => (int) $runas->id,
83            'runasname' => fullname($runas),
84            'canchooserunas' => has_capability('moodle/user:loginas', $context),
85        ];
86    }
87
88    /**
89     * Return structure. Graph, palette, events and strings travel as JSON text.
90     *
91     * @return external_single_structure
92     */
93    public static function execute_returns(): external_single_structure {
94        return new external_single_structure([
95            'id' => new external_value(PARAM_INT, 'Workflow id, 0 when new'),
96            'name' => new external_value(PARAM_TEXT, 'Name'),
97            'enabled' => new external_value(PARAM_BOOL, 'Enabled'),
98            'graph' => new external_value(PARAM_RAW, 'Graph JSON'),
99            'nodetypes' => new external_value(PARAM_RAW, 'Palette JSON'),
100            'events' => new external_value(PARAM_RAW, 'Event class names JSON'),
101            'functions' => new external_value(PARAM_RAW, 'Web service function names JSON'),
102            'strings' => new external_value(PARAM_RAW, 'Interface strings JSON'),
103            'runasuserid' => new external_value(PARAM_INT, 'User the workflow runs as'),
104            'runasname' => new external_value(PARAM_TEXT, 'Full name of that user'),
105            'canchooserunas' => new external_value(PARAM_BOOL, 'Whether the current user may pick another run-as user'),
106        ]);
107    }
108}