3

I am trying to suspend a user in Moodle by using Moodle's Web service API functions in PHP. I can change user fields like firstname, but I am not capable to suspend the user.

It always returns "null".

Here is my code:

<?php
$serverurl = "http://localhost/web/moodle/webservice/rest/server.php?wstoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&wsfunction=core_user_update_users&moodlewsrestformat=json";
$params = "users[0][id]=4&users[0][preferences][0][type]=suspended&users[0][preferences][0][value]=true";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serverurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);

print_r($response);

curl_close($ch);
?>
Aldo Paradiso
  • 911
  • 2
  • 15
  • 30
Pestana
  • 132
  • 1
  • 10

2 Answers2

1

Suspending a user was added to the Web Services in MDL-31465 which made it into version 3.2. If you're on an older version and don't want to upgrade, you can use the core_user_update_users function and set auth to nologin.

The API returning null does not indicate an error - if something goes wrong, you'll get a more verbose error.

Tobias Mühl
  • 1,788
  • 1
  • 18
  • 30
1

with moodle 3.7 should work, but not using"preferences" as in above example but using following template:

wsfunction=core_user_update_users&users[0][id]=1234&users[0][suspended]=1

Thomas
  • 11
  • 1