1

Let me explain: My purpose is to create moodle users from a web app.

I am implementing a web app on Tomcat 8.0.15.0. That is, I use java servlets on the server side. Many JSP files and javascript, with much of it in jQuery, resides on the client side.

On the other hand, on another server, I have a test moodle installation. Via site-administration> plugins> web services> external services, I created a ws that enables the core_user_create_users function. Also created a token to access this ws, and put the admin user as authorized user.

And then, typed the following URL on Chrome:

https://mysyte.com/webservice/rest/server.php?wstoken=780f8b3a1164163d4dc00a757071194e&wsfunction=core_user_create_users&moodlewsrestformat=json&users[0][username]=testuser&usr[ [email] =john@smith.com&users [0] [password] = XXXXXX

And it worked. It returned a blank page, with the text

[{"id": 1, "username": "testuser"}]

Thus creating a user in moodle.

My question is: How can I do this from java?, or from javascript?, or from jQuery even better.

And if not, from PHP, I guess I would have no problem calling it from java, or javascript, or jQuery.

My Wrong Hint: In another part of the application I used, in javascript, the call $.getJSON() successfully. That's why I thought would also serve me in this case. But no success now, when the mozilla debugger reaches the call, it hangs.

Any feedback will be most welcome.

The call looks like


function create_moodle_user(username,firstname,lastname,email,password) {

    var url = "https://mysyte.com/webservice/rest/server.php?"
            + "wstoken=780f8b3a1164163d4dc00a757071194e" + "&" 
            + "wsfunction=core_user_create_users"        + "&" 
            + "moodlewsrestformat=json"                  + "&"
            + "users[0][username]=" + username           + "&"
            + "users[0][firstname]=" + firstname         + "&" 
            + "users[0][lastname]=" + lastname           + "&"
            + "users[0][email]=" + email                 + "&"
            + "users[0][password]=" + password;
    $.getJSON(url, function(data) {             // should return [{"id":4,"username":"testuser"}]                       

    // this point is never reached

        if (data.length < 64) {                  
        }
        else {
        }
    });                         
}
carles
  • 174
  • 1
  • 10

1 Answers1

1

Finally, it worked by changing the call and the way how parameters were passed.


function create_moodle_user(u,f,l,e,fn) {
  var domainname = 'https://my.moodle.site.com';

  var data = {
    wstoken: '780f8b3a1164163d4dc00a757071194e'
    wsfunction: 'core_user_create_users'
    moodlewsrestformat: 'json',
    users: [
        {
            username:u,
            password:'xxxxxxxx',
            email:e,
            firstname:f,
            lastname:l
        }
    ]
  };
  var response = $.ajax({
    type: 'GET',
    data: data,
    url: domainname + '/webservice/rest/server.php'
  });
  // pass the function parameter
  response.done(fn);
}

And this worked!

My problem now is to get user info, since I don't know how to get the response from core_user_get_users_by_field.

This is what I have:

function get_moodle_user(u,fn) {
  var domainname = 'https://my.moodle.site.com';

  var data = {
    wstoken: '780f8b3a1164163d4dc00a757071194e'
    wsfunction: 'core_user_get_users_by_field'
    moodlewsrestformat: 'json',
    field: 'username',
    username:u
  };

  var response = $.ajax({
    type: 'GET',
    data: data,
    url: domainname + '/webservice/rest/server.php'
  });
  
  console.log(response);        // this does not show the result data

  // pass the function parameter
  response.done(fn);
}

Any ideas, please?

carles
  • 174
  • 1
  • 10
  • one option is to do your own custom-moodle-api. Mainly, query db for any fields you need (based on id) and expose through own service (json ... regular link accessed via post). Could secure api access with a predefined token (or even a valid user/pass). Used this way on wordpress since it's more convenient rather then depend on their exposed field (more over many db-fields could be from various plugins so it's much easy to see what exactly it's needed) – Traian GEICU Jul 18 '21 at 06:44