Mysql
 sql >> Database >  >> RDS >> Mysql

Utilizzando Moodle crea utenti e iscrivili ai corsi tramite SQL

Dovresti utilizzare i servizi Web anziché SQL - https://docs.moodle.org/dev/Creating_a_web_service_client

  1. Abilita servizi web /admin/search.php?query=enablewebservices
  2. Abilita il protocollo di riposo /admin/settings.php?section=webserviceprotocols
  3. Aggiungi un servizio /admin/settings.php?section=externalservices
    • aggiungi nome breve =mioservizio
    • abilita =vero
  4. Clicca sulle funzioni per il servizio.
  5. Aggiungi core_user_create_users e enrol_manual_enrol_users
    • Dovrai guardare la documentazione dell'API per i parametri
    • /admin/webservice/documentation.php
  6. Crea un ruolo - /admin/roles/manage.php
  7. Scegli un livello utente + contesto di sistema
  8. Aggiungi funzionalità - webservice/rest:use
  9. Crea un testuser e aggiungilo al ruolo creato sopra
  10. Crea un token per l'utente /admin/settings.php?section=webservicetokens

Una volta che hai impostato, usa qualcosa come questo:

// 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' => '[email protected]',
// '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);