Easily connect Moodle to SilverStripe via the web services REST API.
- Plop the moodle/ folder into your SilverStripe directory or composer require otago/moodle
- create _config/moodle.yml
- run /dev/build?flush=all
And you've installed the module! The examples/MoodleExamplePage.php.example file contains three example web service requests that to help you get started.
You'll need to configure moodle.yml with a password/username or a token supplied by Moodle. (Under Site administration > Advanced features > Plugins > Web services > Manage tokens)
Example moodle.yml file:
---
Name: MoodleWebservice
---
OP\MoodleWebservice:
authentication:
username: my_moodle_username
password: my_password
service: webservice_name
statictoken:
locationTest: http://moodle.yourtertiaryaddress.com/
locationDev: http://moodle.yourtertiaryaddress.com/
locationLive: http://moodle.yourtertiaryaddress.com/
Your Moodle instance will need to be configured to accept web service requests.
/**
* returns a user's profile
* @return \Dataobject|null
*/
public function getUserProfile() {
// connect to moodle
$moodle = MoodleWebservice::connect();
if(!$moodle) {
return Debug::message('Failed to connect to Moodle Webservice');
}
// create a user list containing one generic user
$params = array('userlist' => array(
(object) array(
'userid'=>'2',
'courseid' => '1'
)
));
// call the function core_user_get_course_user_profiles
$userprofile = $moodle->call ('core_user_get_course_user_profiles', $params);
// return it
return $userprofile->Data();
}
$moodle = MoodleWebservice::connect();
if(!$moodle) {
return Debug::message('Failed to connect to Moodle Webservice');
}
// find programmes belonging to this user
$params = array('userid' => '2');
$courselist = $moodle->call ('core_enrol_get_users_courses', $params);
return $courselist->Data();
$moodle = MoodleWebservice::connect();
if(!$moodle) {
return Debug::message('Failed to connect to Moodle Webservice');
}
$params = array('values' => array('[email protected]'), 'field' => 'email');
$userdata = $moodle->call ('core_user_get_users_by_field', $params);
return $userdata->Data();
The most difficult integration task for this module will be configuring your Moodle instance to allow web service requests. There are many resources on the web explaining how to do this, but by far the most helpful is the Moodle check list under Site administration > Advanced features > Plugins > Web services > Overview.
- Create a user that will be used for the service
- Create the roll for this user, with access to the web services
- Assign your new roll to the user
- Create an external service Note: Moodle 2.5 users will have to manually set the shortname in the db
- Add the functions you want to call to the external service
- cURL PHP module
- Moodle 2.2+ for JSON/REST
- SilverStripe 3.x
- Create MoodleExamplePage on a test instance
- Save and load the page
- Update MoodleExamplePage.php to relevent user IDs
- View the raw output by using
Debug::show();
on the output from$moodle->call()
I recommend using Advanced REST Client for Google Chrome for debugging purposes.
debuging your moodleWebService:
- Make sure your $params match the API docs (Under Site administration > Advanced features > Plugins > Web services > API Documentation)
- Moodle's API Documentation can be notirious to read, make sure you tripple check the varibles you're using
Debug::show()
your call()die(print_r($url, true));
inside MoodleWebservice->call()- Make sure it's working using Advanced REST Client
- Invalid parameter value detected - match your params with the Moodle web service client documentation
- Access to the function XXXXXX() is not allowed. - add this function to the external service (see Project Setup)
- functions return nothing - enable debugging mode, you'll see debug messages.
- Fork on git
- Raise an issue
BSD