Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
enrol_user_node
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 params
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 branches
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
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\local\nodes;
18
19use tool_autoflow\local\engine\node_failed;
20use tool_autoflow\local\engine\node_result;
21use tool_autoflow\local\engine\node_type;
22use tool_autoflow\local\engine\param;
23
24/**
25 * Enrols a user in a course with manual enrolments, as a student unless another role is named.
26 *
27 * It calls enrol_manual_enrol_users, so the course must have a manual enrolment
28 * method and the user the workflow runs as needs enrol/manual:enrol there, plus
29 * the right to assign the role.
30 *
31 * @package    tool_autoflow
32 * @copyright  2026 DevLMS <contact@devlms.com>
33 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35final class enrol_user_node implements node_type {
36    /**
37     * Type name.
38     *
39     * @return string
40     */
41    public function type(): string {
42        return 'moodle.enrol_user';
43    }
44
45    /**
46     * Parameters.
47     *
48     * @return param[]
49     */
50    public function params(): array {
51        return [new param('userid', 'user', true), new param('courseid', 'number', true), new param('role', 'text', false)];
52    }
53
54    /**
55     * Branches.
56     *
57     * @return string[]
58     */
59    public function branches(): array {
60        return [];
61    }
62
63    /**
64     * Enrol the user.
65     *
66     * @param array $params userid, courseid, role (short name, default student)
67     * @param array $context
68     * @return node_result userid, courseid and role.
69     */
70    public function execute(array $params, array $context): node_result {
71        global $DB;
72        $userid = node_values::id($params['userid'] ?? null, 'user id');
73        $courseid = node_values::id($params['courseid'] ?? null, 'course id');
74        $role = trim((string) ($params['role'] ?? '')) ?: 'student';
75        $roleid = $DB->get_field('role', 'id', ['shortname' => $role]);
76        if (!$roleid) {
77            throw new node_failed("No role with the short name \"$role\".");
78        }
79        call_function_node::call('enrol_manual_enrol_users', [
80            'enrolments' => [['roleid' => (int) $roleid, 'userid' => $userid, 'courseid' => $courseid]],
81        ]);
82        return new node_result(['userid' => $userid, 'courseid' => $courseid, 'role' => $role]);
83    }
84}