Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| save_workflow | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| execute_parameters | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| execute_returns | |
100.00% |
11 / 11 |
|
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 | |
| 17 | namespace tool_autoflow\external; |
| 18 | |
| 19 | use core_external\external_api; |
| 20 | use core_external\external_function_parameters; |
| 21 | use core_external\external_multiple_structure; |
| 22 | use core_external\external_single_structure; |
| 23 | use core_external\external_value; |
| 24 | use tool_autoflow\local\workflow_store; |
| 25 | |
| 26 | /** |
| 27 | * Save a workflow from the editor. A graph with problems is kept as a disabled draft |
| 28 | * and the problems come back for the editor to show. |
| 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 | */ |
| 34 | class save_workflow extends external_api { |
| 35 | /** |
| 36 | * Parameters. |
| 37 | * |
| 38 | * @return external_function_parameters |
| 39 | */ |
| 40 | public static function execute_parameters(): external_function_parameters { |
| 41 | return new external_function_parameters([ |
| 42 | 'id' => new external_value(PARAM_INT, 'Workflow id, 0 to create one'), |
| 43 | 'name' => new external_value(PARAM_TEXT, 'Name'), |
| 44 | 'graph' => new external_value(PARAM_RAW, 'Graph JSON from the editor'), |
| 45 | 'enabled' => new external_value(PARAM_BOOL, 'Whether to enable it'), |
| 46 | 'runasuserid' => new external_value(PARAM_INT, 'User the workflow runs as, 0 for the person saving', VALUE_DEFAULT, 0), |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Save. |
| 52 | * |
| 53 | * @param int $id |
| 54 | * @param string $name |
| 55 | * @param string $graph |
| 56 | * @param bool $enabled |
| 57 | * @param int $runasuserid User the workflow runs as, 0 for the person saving. |
| 58 | * @return array |
| 59 | */ |
| 60 | public static function execute(int $id, string $name, string $graph, bool $enabled, int $runasuserid = 0): array { |
| 61 | $params = self::validate_parameters( |
| 62 | self::execute_parameters(), |
| 63 | ['id' => $id, 'name' => $name, 'graph' => $graph, 'enabled' => $enabled, 'runasuserid' => $runasuserid] |
| 64 | ); |
| 65 | $context = \context_system::instance(); |
| 66 | self::validate_context($context); |
| 67 | require_capability('tool/autoflow:manage', $context); |
| 68 | |
| 69 | $result = workflow_store::save( |
| 70 | $params['id'], |
| 71 | $params['name'], |
| 72 | $params['graph'], |
| 73 | $params['enabled'], |
| 74 | $params['runasuserid'] |
| 75 | ); |
| 76 | $result['runasname'] = fullname(\core_user::get_user($result['runasuserid'])); |
| 77 | $result['errors'] = array_map(fn($e) => [ |
| 78 | 'code' => $e['code'], |
| 79 | 'message' => get_string('error:' . $e['code'], 'tool_autoflow'), |
| 80 | 'nodeids' => array_map('strval', $e['nodeids'] ?? []), |
| 81 | ], $result['errors']); |
| 82 | return $result; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return structure. |
| 87 | * |
| 88 | * @return external_single_structure |
| 89 | */ |
| 90 | public static function execute_returns(): external_single_structure { |
| 91 | return new external_single_structure([ |
| 92 | 'id' => new external_value(PARAM_INT, 'Workflow id'), |
| 93 | 'enabled' => new external_value(PARAM_BOOL, 'Whether it is enabled now'), |
| 94 | 'runasuserid' => new external_value(PARAM_INT, 'User the workflow runs as'), |
| 95 | 'runasname' => new external_value(PARAM_TEXT, 'Full name of that user'), |
| 96 | 'errors' => new external_multiple_structure(new external_single_structure([ |
| 97 | 'code' => new external_value(PARAM_ALPHA, 'Problem code'), |
| 98 | 'message' => new external_value(PARAM_TEXT, 'Problem in words'), |
| 99 | 'nodeids' => new external_multiple_structure(new external_value(PARAM_RAW, 'Node id')), |
| 100 | ])), |
| 101 | ]); |
| 102 | } |
| 103 | } |