0

How to enroll a user to a particular course through web service API, I have token, user credential and course details.How to create the request ?

I am try to implement enrollment to a course like this

var domainname = 'http://localhost/moodle';
var token = '9942f7fffc884a46e549bbe8cdc3374d';
var functionname = 'enrol_manual_enrol_users';
var serverurl = domainname + '/webservice/rest/server.php';
var enrollUser = [{
    roleid: '1',
    userid: '2',
    courseid: '5'
}];
var data = {
    wstoken: token,
    wsfunction: functionname,
    moodlewsrestformat: 'json',
    users: userstocreate
}
var response = $.ajax({
    type: 'POST',
    data: data,
    url: serverurl
});
console.log(response);

But it gives error..Pls Help

Varun Sreedharan
  • 517
  • 8
  • 28
  • 1
    Have a look at this answer : [Using MOODLE create users and enroll them in courses via SQL](http://stackoverflow.com/a/35912777/1603711) – Russell England Aug 09 '16 at 11:06
  • Yes , checked the answer , but it follows PHP script , i want to enroll a user to a course via a third party app , i want to make a call using javascript.. how its possible – Varun Sreedharan Aug 09 '16 at 11:23
  • 2
    if you want to be able to call this by javascript, your first need to have your javascript file put on the same server than the moodle and handle Cross-Origin Request (aka CORS) properly. Can't you just use the PHP on this answer and call it from your javascript like a proxy page ? – Walfrat Aug 09 '16 at 11:36
  • The best approach is the @Walfrat comment. Use PHP on your server that connects to the moodle server, and use javascript for contact with your server. By this way you don't have CORS error. – Marcos Pérez Gude Aug 09 '16 at 11:44
  • any option for extending service module inside moodle , can we add functions in to it. Its a better method ? – Varun Sreedharan Aug 10 '16 at 06:05
  • HI all , I am created new service (PHP side) and handle the api calls inside the service module.Its working fine now . Thank u all – Varun Sreedharan Aug 12 '16 at 06:43

0 Answers0