Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
4 / 5
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
run_workflow
80.00% covered (warning)
80.00%
4 / 5
50.00% covered (danger)
50.00%
1 / 2
4.13
0.00% covered (danger)
0.00%
0 / 1
 get_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
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\task;
18
19use tool_autoflow\local\runner;
20
21/**
22 * Runs one workflow. Queued by runner::queue() with the run-as user set, so
23 * Moodle switches to that user before execute().
24 *
25 * @package    tool_autoflow
26 * @copyright  2026 DevLMS <contact@devlms.com>
27 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29class run_workflow extends \core\task\adhoc_task {
30    /**
31     * Name shown in the task logs.
32     *
33     * @return string
34     */
35    public function get_name(): string {
36        return get_string('task:runworkflow', 'tool_autoflow');
37    }
38
39    /**
40     * Run it.
41     */
42    public function execute() {
43        $data = json_decode($this->get_custom_data_as_string(), true);
44        $run = runner::run((int) $data['workflowid'], $data['trigger'] ?? [], $data['triggertype'], (int) $data['depth']);
45        if ($run) {
46            mtrace("Workflow {$data['workflowid']}: {$run->status}" . ($run->error ? " ({$run->error})" : ''));
47        }
48    }
49}