Migrating `core_enrol_edit_user_enrolment` to `core_enrol_submit_user_enrolment_form`

Migrating `core_enrol_edit_user_enrolment` to `core_enrol_submit_user_enrolment_form`

by David Moreno Montero -
Number of replies: 2

Hello everyone,

I have a external server that uses webservices to create the enrollments and activate and deactivate them. We are using `core_enrol_edit_user_enrolment` but it is marked deprecated and since 311 (IIRC) it can not be even added to the allowed webservice functions. So I'm migrating to use `core_enrol_submit_user_enrolment_form`.

But the documentation is very lacking and I think there is some bug that prevents it to be used at all from the external webservice.

For my usage the old function had `ueid` and `status`, but the new one has only `userform` which is not very descriptive. Investigating it looks like i can just put my data there URL encoded, as the web UI does. And I get further, but I get `{'result': False, 'validationerror': True}` which sounds like it does not like my data.

Investigating further, and inspecting differences with the web UI request, it seems to want a session key. So that lets me think that this function is working ONLY on the web ui, but not webservices.

Anybody knows more? Anybody has `core_enrol_submit_user_enrolment_form` from an external service?

If not, anybody can point me on whats are needed and I prepare a patch so that it works securely for next releases?

A possible test would be this curl request, change keys are required:

```
curl https://mymoodle.example.com/moodle/webservice/rest/server.php -X POST \
-d"formdata=ue%3d2018%26status%3d0" \
-dwstoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
-dmoodlewsrestformat=json \
-dwsfunction=core_enrol_submit_user_enrolment_form
{"result":false,"validationerror":true}
```

Average of ratings: -
In reply to David Moreno Montero

Re: Migrating `core_enrol_edit_user_enrolment` to `core_enrol_submit_user_enrolment_form`

by István Holbok -

I ran into the same problem. The "core_enrol_submit_user_enrolment_form" webservice parameters are not documented.
The Webservice documentations contains the following data: 

Submit form data for enrolment form

Arguments

formdata (Required)  - The data from the event form

General structure

string   //The data from the event form

XML-RPC (PHP structure)

[formdata] => string

REST (POST parameters)

formdata= string

---------

The event form (it is a modal) collects the following data:

<input name="ue" type="hidden" value="740"> where ue is the id of the ##__user_enrolments table
<input name="ifilter" type="hidden" value="">
<input name="sesskey" type="hidden" value="hCLcwZGuII">
<input name="_qf__enrol_user_enrolment_form" type="hidden" value="1">

status (0 active, 1 suspended)
timestart
timeend

The ue (as unique ID) would be enough to find the record if it is part of the POST request. But, the webservice API returns:

[exception] => dml_missing_record_exception
[errorcode] => invalidrecord
[message] => The user_enrolments table do not contains record.
[debuginfo] => SELECT * FROM {user_enrolments} WHERE id IS NULL

The call was:
/webservice/rest/server.php?moodlewsrestformat=json&wstoken=...&
wsfunction=core_enrol_submit_user_enrolment_form&
formdata=%7B%22ue%22:%22740%22,%22timestart%22:%22%22,%22timeend%22:%221661990399%22%7D

Is there any idea? It would be help to solve this issue.