gpt4 book ai didi

php - 使用 Moodle 创建用户并通过 SQL 将他们注册到类(class)中

转载 作者:行者123 更新时间:2023-11-29 01:06:02 27 4
gpt4 key购买 nike

我需要创建一个外部应用程序来创建、修改和注册 Moodle 用户。

我一直在阅读 Moodle 文档,但更多的是针对前端管理员而不是开发人员。我该怎么做才能创建用户?哪些表包含有关它们的强制性信息?以及如何将现有用户注册到 Moodle?

最佳答案

您应该使用 Web 服务而不是使用 SQL - https://docs.moodle.org/dev/Creating_a_web_service_client

  1. 启用网络服务/admin/search.php?query=enablewebservices
  2. 启用休息协议(protocol)/admin/settings.php?section=webserviceprotocols
  3. 添加服务/admin/settings.php?section=externalservices
    • 添加简称 = myservice
    • 启用=真
  4. 点击服务的功能。
  5. 添加 core_user_create_usersenrol_manual_enrol_users
    • 你需要查看参数的api文档
    • /admin/webservice/documentation.php
  6. 创建角色 -/admin/roles/manage.php
  7. 选择用户级别 + 系统上下文
  8. 添加功能 - webservice/rest:use
  9. 创建一个testuser并添加到上面创建的角色
  10. 为用户/admin/settings.php?section=webservicetokens 创建 token

设置完成后,使用如下内容:

// 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);

关于php - 使用 Moodle 创建用户并通过 SQL 将他们注册到类(class)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35881584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com