Monday, 8 May 2017

Users from Microsoft SQL Server to Moodle

I have to sync users from an existing Microsoft SQL Server to Moodle via the API. I have done some batch uploading from a csv file like this:

insertMoodleGebruikersBatch(array: csvGebruiker[]) {
    this.functionName = "core_user_create_users";
    this.urlParameters = "";

    for (let x = 0; x < array.length; x++) {
      this.urlParameters +=
        "&users[" + x + "][username]=" + array[x][0] +
        "&users[" + x + "][password]=" + array[x][1] +
        "&users[" + x + "][firstname]=" + array[x][2] +
        "&users[" + x + "][lastname]=" + array[x][3] +
        "&users[" + x + "][email]=" + array[x][4];
    }
    this.serverurl = this.baseDomain + "/webservice/rest/server.php" + "?wstoken=" + this.token + "&wsfunction=" + this.functionName;
    this.fullurl = this.serverurl + this.urlParameters;
    this.urlParameters = "";
    return this.http.get("http://" + this.fullurl);
  }

This is very ugly and slow. I have to think there is another way I can do this, like giving JSON data as a payload to Moodle. But I can't find anything about this subject.

Any ideas how to improve my code?

Thanks in advance!



via Milan_w

No comments:

Post a Comment