There are multiple ways of sending enrollments via web service. The basic call is the most common, however, enrollment questions are also supported.
Creating an enrollment via web service
When enrolling a user via a web service call, we first want to verify if the user is enrolled in the course. After confirming they do not have an enrollment, we can send a request to create a new enrollment record.
In this example, the course node id (nid) we want to enroll the user in is 300 and the Drupal user ID (uid) is 200.
Code Block |
---|
language | php |
---|
title | 1. Verify a user is enrolled |
---|
linenumbers | true |
---|
|
<?php
// Login to the site and request the access token
$curl =
curl_init('http://your-domain.com/restws/session/token');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); // Your web service user credentials go here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($curl);
// The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%"
// Verify if the user is enrolled in the course
$curl = curl_init("http://your-domain.com/course_enrollment.json?uid=200&nid=300");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials go here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($curl);
$response = json_decode($json);
|
The verification request will return an array of information, including any records found to match the given criteria. The response array is structured to handle paginated requests, and includes a 'list' of the records on the current page ('self' in the returned array). Due to the fact that the verification request above included the uid and nid parameters, if an enrollment exists, there will be only one record in the response list, as a user cannot have multiple enrollment records per course in EthosCE. If no enollment record is present, the 'list' array will be empty. The examples below show responses where user 200 is enrolled in node 310, but not enrolled in node 300.
Code Block |
---|
language | php |
---|
title | Response With Enrollment |
---|
linenumbers | true |
---|
collapse | true |
---|
|
stdClass Object
(
[self] => http://your-domain.com/course_enrollment?uid=200&nid=310 // The current page the list value displaying information
[first] => http://your-domain.com/course_enrollment?uid=200&nid=310&page=0
[last] => http://your-domain.com/course_enrollment?uid=200&nid=310&page=0
[list] => Array
(
[0] => stdClass Object
(
[eid] => 185
[nid] => stdClass Object
(
[uri] => http://your-domain.com/node/310
[id] => 310
[resource] => node
[uuid] => 9aabcf09-58c3-4679-85b3-c5f7ddc93ac6
)
[uid] => stdClass Object
(
[uri] => http://your-domain.com/user/200
[id] => 200
[resource] => user
[uuid] => ba5bc8af-25d7-48de-bb54-87ae70ac33b5
)
[enrollmenttype] =>
[status] => 1
[created] => 1459800985
[timestamp] => 1459800985
[enroll_end] => 0
[code] =>
[user_type] =>
[data] =>
[feed_nid] =>
)
)
) |
Code Block |
---|
title | Response Without Enrollment |
---|
linenumbers | true |
---|
collapse | true |
---|
|
stdClass Object
(
[self] => http://your-domain.com/course_enrollment?uid=200&nid=300 // The current page the list value displaying information
[first] => http://your-domain.com/course_enrollment?uid=200&nid=300&page=0
[last] => http://your-domain.com/course_enrollment?uid=200&nid=300&page=0
[list] => []
) |
Continuing from section #1, as the call to http://your-domain.com/course_enrollment?uid=200&nid=300 has returned an empty list, we can can now send the creation request.
Code Block |
---|
language | php |
---|
title | 2. Creation Request |
---|
linenumbers | true |
---|
|
$response = json_decode($json);
$list = $response->list;
if(empty($list)){
$data = array(
'nid' => 300,
'uid' => 200,
'enrollmenttype' => 'webservice_call',
'type' => 'webservice_call',$list = $response->list; // See note below
if(empty($list)){
$data = array(
'nid' => 300,
'uid' => 200,
'enrollmenttype' => 'webservice_call', // This is used to identify the source of the enrollment. Only use letters, numbers, and underscores.
'type' => 'course_enrollment', // This is the key of the enrollment question set. Only use letters, numbers, and underscores.
'status' => 1,
);
// Send the enrollment creation request
$curl = curl_init("http://your-domain.com/course_enrollment");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials go here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
} |
A successful creation request will return an array of values containing the "URI" of the new enrollment record, as well as it's enrollment "id".
Code Block |
---|
|
{"uri":"http:\/\/your-domain.com\/course_enrollment\/458","id":"458","resource":"course_enrollment"} |
Info |
---|
For an explanation of the information returned from the verification request, see Web Service Responses. |
Creating/Updating an enrollment containing questions
Enrollment questions can be updated during an enrollment web service call. However, only questions which are set to Show this field on enrollment, are supported via the web service.
You may set the enrollment bundle by updating the type attribute in the payload, to the machine name of the enrollment question bundle used in the course. The default bundle is course_enrollment, however, custom enrollment question bundles are supported.
The data example below updates a custom set titled "Marketing Questions", which contains a text field, and has a machine name of marketing_questions.
Code Block |
---|
$data = array(
'nid' => 300,
'uid' => 200,
'enrollmenttype' => 'webservice_call', // This is used to identify the source of the enrollment. Only use letters, numbers, and underscores.
'type' => 'marketing_questions', // This is the key of the enrollment question set. Only use letters, numbers, and underscores.
'status' => 1,
'field_textbox'=> 'ETHOSCE123',
); |