Get a user created by core_user_create_users using core_user_get_users

Re: Get a user created by core_user_create_users using core_user_get_users

by Lawrence Lagerlof -
Number of replies: 0
Hi Vincent. MoodleRest dev here.

This class is just an easier way to query Moodle webservices. It runs synchronous, so if you call a core_user_create_users using this class you will get the same result of querying the webservice manually.

Well, without your code I can't check the problem, but we can review some important items to make your ultimate goal a reality.

- The core_user_create_users accepts a list of users, but if just one of them generates an error like a duplicated username or missing email, none is inserted and an error is returned by the webservice. If the function added all the users succefully a list of IDs is returned.

- core_user_get_users don't accept a list of usernames or emails, so you must query one by one.

Said that, I wrote an example for your specific scenario:

<?php // Load the MoodleRest class require_once('MoodleRest.php'); // Put your webservice url $webservervice_url = 'http://127.0.0.1/moodle/webservice/rest/server.php'; // Put a token that have permission to execute core_user_get_users and core_user_create_users $token = '07284be04dc3e8c48c0da131a8f8e40f'; // Instantiate a MoodleRest object $MoodleRest = new MoodleRest($webservervice_url, $token); // Query user $username = 'buddy'; $query_user['criteria'][0] = array('key' => 'username', 'value' => $username); $result_query = $MoodleRest->request('core_user_get_users', $query_user); // Test if an exception was returned when querying the user data if (!empty($result_query['exception'])) { print_r(array('Error querying user', $result_query)); die(); } // Check if the user was returned. If not, create him if (empty($result_query['users'][0]['id'])) { $people['users'][0] = array( 'username' => $username, 'password' => '123456', 'firstname' => 'Buddy', 'lastname' => "Revell ($username)", 'email' => $username . '@email.fake' ); $result_insert = $MoodleRest->request('core_user_create_users', $people, MoodleRest::METHOD_POST); // Test if an exception was returned when creating the user if (!empty($result_insert['exception'])) { print_r(array('Error creating user', $result_insert)); die(); } print_r(array('Created user', $result_insert)); } else { print_r(array('Returned user', $result_query)); }




Average of ratings: Useful (1)