Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
run_workflow
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
3
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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 execute_returns
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\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\runner;
24use tool_autoflow\local\workflow_store;
25
26/**
27 * Queue a manual run of a workflow, which happens on the next cron run.
28 *
29 * @package    tool_autoflow
30 * @copyright  2026 DevLMS <contact@devlms.com>
31 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class run_workflow extends external_api {
34    /**
35     * Parameters.
36     *
37     * @return external_function_parameters
38     */
39    public static function execute_parameters(): external_function_parameters {
40        return new external_function_parameters([
41            'id' => new external_value(PARAM_INT, 'Workflow id'),
42        ]);
43    }
44
45    /**
46     * Queue the run.
47     *
48     * @param int $id
49     * @return array
50     */
51    public static function execute(int $id): array {
52        global $USER;
53        ['id' => $id] = self::validate_parameters(self::execute_parameters(), ['id' => $id]);
54        $context = \context_system::instance();
55        self::validate_context($context);
56        require_capability('tool/autoflow:manage', $context);
57        workflow_store::get($id);
58        return ['queued' => runner::queue($id, ['userid' => (int) $USER->id], 'manual')];
59    }
60
61    /**
62     * Return structure.
63     *
64     * @return external_single_structure
65     */
66    public static function execute_returns(): external_single_structure {
67        return new external_single_structure([
68            'queued' => new external_value(PARAM_BOOL, 'Whether the run was queued'),
69        ]);
70    }
71}