LMSouq
moodle-core Open

Moodle enrol_manual_enrol_users doesn't work

GU
Guilherme
1 month ago
3 views
Problem Description
I try to enrol users in a course using `Moodle Rest API` method `enrol_manual_enrol_users()`, but it's don't create anything in the `mdl_role_assignments` table. <!-- language: lang-php --> /// Connection $token = 'ed874e6d9f51239e180f49f4d926d50f'; $domainname = 'http://localhost/moodle'; $functionname = 'enrol_manual_enrol_users'; $restformat = 'json'; //////// enrol_manual_enrol_users //////// /// Paramètres $enrolment = new stdClass(); $enrolment->roleid = 5; //estudante(student) -> 5; moderador(teacher) -> 4; professor(editingteacher) -> 3; $enrolment->userid = 2; $enrolment->courseid = 5; $enrolments = array( $enrolment); $params = array('enrolments' => $enrolments); print_r($params); header('Content-Type: text/plain'); $serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname; require_once('./curl.php'); $curl = new curl; //if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2 $restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:''; $resp = $curl->post($serverurl . $restformat, $params); print_r($resp); I need a way to enroll users in moodle courses by webservice... **UPDATE** The following response is returned: "debuginfo":"Missing required key in single structure: enrolments" The parameters are <!-- language: lang-php --> 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'), 'userid' => new external_value(PARAM_INT, 'The user that is going to be enrolled'), 'courseid' => new external_value(PARAM_INT, 'The course to enrol the user role in'), 'timestart' => new external_value(PARAM_INT, 'Timestamp when the enrolment start', VALUE_OPTIONAL), 'timeend' => new external_value(PARAM_INT, 'Timestamp when the enrolment end', VALUE_OPTIONAL), 'suspend' => new external_value(PARAM_INT, 'set to 1 to suspend the enrolment', VALUE_OPTIONAL) The userid and the courseid are valids id's

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base