Using MOODLE create users and enroll them in courses via SQL

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I need to create an extern application that creates, modifies and enroll users of Moodle.

I've been reading moodle documentation but its more for front administrators that for developers. How can I do to create a user? which tables hold obligatory information about them? and how do I enroll existing users to moodle?

回答1:

You should use web services rather than using SQL - https://docs.moodle.org/dev/Creating_a_web_service_client

  1. Enable web services /admin/search.php?query=enablewebservices
  2. Enable rest protocol /admin/settings.php?section=webserviceprotocols
  3. Add a service /admin/settings.php?section=externalservices
    • add short name = myservice
    • enable = true
  4. Click on functions for the service.
  5. Add core_user_create_users and enrol_manual_enrol_users
    • You'll need to look at the api documentation for the parameters
    • /admin/webservice/documentation.php
  6. Create a role - /admin/roles/manage.php
  7. Choose a user level + system context
  8. Add capability - webservice/rest:use
  9. Create a testuser and add to the role created above
  10. Create a token for the user /admin/settings.php?section=webservicetokens

Once you have that set up, use something like this:

// First get the token. $tokenurl = 'http://www.yourmoodlesite.com/login/token.php?username=testuser&password=xx&service=myservice';  $tokenresponse = file_get_contents($tokenurl);  $tokenobject = json_decode($tokenresponse);  if (!empty($tokenobject->error)) {     echo $tokenobject->error;     die(); }  // Then call the create user and enrol functions // Remember to add the question mark after "server.php" because http_build_query() won't add it on its own and you'll end up with a 404 error $baseurl = 'http://www.yourmoodlesite.com/webservice/rest/server.php?';  // Then add these parameters to the url.  $users = array(); // See the api documentation /admin/webservice/documentation.php // for core_user_create_users for building the $users array // e.g. // $users = array(array( // 'username' => 'lecapitaine',   //Username policy is defined in Moodle security config // 'password' =>  'EngageNCC-1701', //Plain text password consisting of any characters // 'firstname' =>  'William', //The first name(s) of the user // 'lastname' => 'Shatner',  //The family name of the user // 'email' => 'jimmy.k@enterprise.com', // 'lang' => 'en', // ));  $params = array(     'wstoken' => $tokenobject->token,     'wsfunction' => 'core_user_create_users',     'moodlewsrestformat' => 'json',     'users' => $users, );  $url = $baseurl . http_build_query($params);  $response = file_get_contents($url);  $newusers = json_decode($response);  // Newusers will be an array of objects containing the new user ids.  $enrolments = array(); // See the api documentation /admin/webservice/documentation.php // for enrol_manual_enrol_users for building the $enrolments array  // Then enrol the users. $params = array(     'wstoken' => $tokenobject->token,     'wsfunction' => 'enrol_manual_enrol_users',     'moodlewsrestformat' => 'json',     'enrolments' => $enrolments, );  $url = $baseurl . http_build_query($params);  $response = file_get_contents($url);  $enrolled = json_decode($response); 


回答2:

As answered here creating moodle users and register them on courses programatically by me =3. This works completely extern to moodle.

$servername = 'localhost'; $username = 'username'; $password = 'password'; $dbname = 'moodle';  $u_moodle = 'theusernameyouwant'; $hp_moodle = password_hash('thepasswordyouwant', PASSWORD_DEFAULT); ///IMPORTANT! $name = 'first name'; $lname = 'last name'; $email = 'e@m.ail'; ///This have to be verified by you as we're inserting it directly $course = '123'; //Id that you put in moodle admin, not the real id      $conn = new mysqli($servername, $username, $password, $dbname);  $sql = "INSERT INTO 'mdl_user' (auth, confirmed, mnethostid, username, password, firstname, lastname, email)     VALUES ('manual', 1, 1, '$u_moodle', '$hp_moodle', '$name', '$lname', '$email')"; // auth = 'manual', confirmed = 1, mnethosti
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!