Moodle enrol_manual_enrol_users error

Moodle enrol_manual_enrol_users error

by Glenn Onyango -
Number of replies: 3

I have been getting the error below when i try to enroll users to moodle through enrol_manual_enrol_users

Array ( [exception] => invalid_parameter_exception [errorcode] => invalidparameter [message] => Invalid parameter value detected [debuginfo] => enrolments => Invalid parameter value detected: Missing required key in single structure: roleid )

This is my code

$MoodleRest = new MoodleRest('http://localhost/moodle/webservice/rest/server.php', $this->token);
$enrolment = array(
"userid" => $user_id,
"courseid" => $course_id,
"roleid" => 4
);
$enrolments = array(array($enrolment));
$params = array( 'enrolments' => $enrolments );
$result_query = $MoodleRest->request('enrol_manual_enrol_users', $params);
if (!empty($result_query['exception'])) {
print_r(array('Error querying enroll', $result_query));
die();
}


Average of ratings: -
In reply to Glenn Onyango

Re: Moodle enrol_manual_enrol_users error

by Neill Magill -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
I suspect you have too many arrays nested. Your output will look like:

[
    'enrolements' => [
        [
             [
                 [
                     'userid' => 1,
                      ....
                 ],
             ],
        ],
    ],
]

Instead of like:

[
    'enrolements' => [
        [
            'userid' => 1,
            ....
        ],
    ],
]
In reply to Neill Magill

Re: Moodle enrol_manual_enrol_users error

by Glenn Onyango -
Hey i am glad you replied but i already tried reducing the nested arrays and it was giving me another error

Array ( [0] => Error querying enroll [1] => Array ( [exception] => invalid_parameter_exception [errorcode] => invalidparameter [message] => Invalid parameter value detected [debuginfo] => Context does not exist ) )

The Less nested arrays are as follows :
$enrolment = array(
"userid" => $user_id,
"courseid" => $course_id,
"roleid" => 4
);
$enrolments = array($enrolment);
$params = array( 'enrolments' => $enrolments );
In reply to Glenn Onyango

Re: Moodle enrol_manual_enrol_users error

by Neill Magill -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
The error suggests that at least one value you are passing to the web service does not exist.

Have you verified that both the userid and courseid being passed exist in the Moodle instance you are querying?

You may also want to check that the account that you are logging into the webservice as has the correct permissions to view and enrol users onto the course (although I would expect a different error for that)