Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
observer
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
0.00% covered (danger)
0.00%
0 / 1
 handle
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
6.04
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;
18
19use tool_autoflow\local\runner;
20use tool_autoflow\local\workflow_store;
21
22/**
23 * Starts workflows whose trigger is the event that just happened.
24 *
25 * This sees every event on the site, so it does one cached lookup and returns.
26 * The run happens later in an ad hoc task, never inside the user's request.
27 *
28 * @package    tool_autoflow
29 * @copyright  2026 DevLMS <contact@devlms.com>
30 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class observer {
33    /**
34     * Queue the workflows for this event.
35     *
36     * @param \core\event\base $event
37     */
38    public static function handle(\core\event\base $event): void {
39        global $CFG;
40        if (during_initial_install() || !empty($CFG->upgraderunning)) {
41            return;
42        }
43        $ids = workflow_store::workflows_for_event($event->eventname);
44        if (!$ids) {
45            return;
46        }
47        // An event fired by a running workflow starts the next level of the chain.
48        $current = runner::current_depth();
49        $depth = $current === null ? 0 : $current + 1;
50        $data = $event->get_data();
51        foreach ($ids as $id) {
52            runner::queue($id, $data, 'event', $depth);
53        }
54    }
55}