通过Web服务在Moodle中创建用户的PHP方法

3

我想要将外部网站与Moodle系统连接起来。我已经设置了Web服务并创建了一个令牌以便获取访问权限。

我按照http://www.rumours.co.nz/manuals/using_moodle_web_services.htm的步骤进行设置,但是与之相反的是,我希望通过REST实现连接,就像在https://github.com/moodlehq/sample-ws-clients/find/master中一样。

我的方法是创建一个Moodle类来处理数据交换。首先,我只是想尝试通过Web服务创建一些新的硬编码用户,但它失败了,Moodle响应如下:

"invalidrecord Can not find data record in database table external_functions."

这对我来说似乎是调用成功了,但Moodle有问题找不到“core_user_create_users”函数。我已经检查了本地Moodle数据库,在external_functions表中有一个“core_user_create_users”的条目,所以我有点困惑Moodle不知道该怎么做。

这是我的类:

require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');

class Moodle {

    private $token;          
    private $domainName;     // 'local.moodle.dev';
    private $serverUrl;

    public function __construct($token, $domainName) {
        $this->token = $token;
        $this->domainName = $domainName;
        $this->serverUrl = $this->domainName . '/webservice/rest/server.php' . '?wstoken=' . $this->token;

        echo "initialize Service: $this->serverUrl </br>";
    }


    public function createUser() {
        $functionName = 'core_user_create_users';

        /// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
        $user1 = new stdClass();
        $user1->username = 'testusername1';
        $user1->password = 'testpassword1';
        $user1->firstname = 'testfirstname1';
        $user1->lastname = 'testlastname1';
        $user1->email = 'testemail1@moodle.com';
        $user1->auth = 'manual';
        $user1->idnumber = 'testidnumber1';
        $user1->lang = 'en';
        $user1->theme = 'standard';
        $user1->timezone = '-12.5';
        $user1->mailformat = 0;
        $user1->description = 'Hello World!';
        $user1->city = 'testcity1';
        $user1->country = 'au';
        $preferencename1 = 'preference1';
        $preferencename2 = 'preference2';
        $user1->preferences = array(
        array('type' => $preferencename1, 'value' => 'preferencevalue1'),
        array('type' => $preferencename2, 'value' => 'preferencevalue2'));
        $user2 = new stdClass();
        $user2->username = 'testusername2';
        $user2->password = 'testpassword2';
        $user2->firstname = 'testfirstname2';
        $user2->lastname = 'testlastname2';
        $user2->email = 'testemail2@moodle.com';
        $user2->timezone = 'Pacific/Port_Moresby';
        $users = array($user1, $user2);
        $params = array('users' => $users);

        /// REST CALL
        $serverurl = $this->serverUrl .  '&wsfunction=' . $functionName;
        require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');
        $curl = new curl;
        //if rest format == 'xml', then we do not add the param for   backward compatibility with Moodle < 2.2
        $restformat = "json";
        $resp = $curl->post($serverurl . $restformat, $params);
        //print_r($resp);

        echo '</br>*************Server Response*************</br>';
        var_dump($resp);
   }

}

我正在使用与上面我发布的 Github 项目相同的 Curl 类 - Moodle 在他们的文档中链接到它。我的调用入口目前是硬编码的:
<?php

include_once (DOCUMENT_ROOT.'/tcm/api/moodle/moodle.php');
//entry point of code

if (isset($_POST)){
    //token and domain would be in $_POST
    $bla = new Moodle('0b5a1e98061c5f7fb70fc3b42af6bfc4', 'local.moodle.dev');
    $bla->createUser();
}

有人知道如何解决“invalidrecord Can not find data record in database table external_functions”错误,或者有不同的方法/建议可以远程创建我的用户吗?

提前致谢

1个回答

4
我终于用以下代码使它正常工作了:
class Moodle {

    private $token;          //'0b5a1e98061c5f7fb70fc3b42af6bfc4';
    private $domainName;     // 'http://local.moodle.dev';
    private $serverUrl;
    public $error;

    public function __construct($token, $domainName) {
        $this->token = $token;
        $this->domainName = $domainName;

        $this->serverUrl = $this->domainName . '/webservice/rest/server.php' . '?wstoken=' . $this->token;

        echo "initialize Service: $this->serverUrl </br>";
    }

    public function createUser() {
        $functionName = 'core_user_create_users';

        $user1 = new stdClass();
        $user1->username = 'testusername1';
        $user1->password = 'Uk3@0d5w';
        $user1->firstname = 'testfirstname1';
        $user1->lastname = 'testlastname1';
        $user1->email = 'testemail1@moodle.com';
        $user1->auth = 'manual';
        $user1->idnumber = '';
        $user1->lang = 'en';
        $user1->timezone = 'Australia/Sydney';
        $user1->mailformat = 0;
        $user1->description = '';
        $user1->city = '';
        $user1->country = 'AU';     //list of abrevations is in yourmoodle/lang/en/countries
        $preferencename1 = 'auth_forcepasswordchange';
        $user1->preferences = array(
            array('type' => $preferencename1, 'value' => 'true')
            );

        $users = array($user1);
        $params = array('users' => $users);

        /// REST CALL
        $restformat = "json";
        $serverurl = $this->serverUrl . '&wsfunction=' . $functionName. '&moodlewsrestformat=' . $restformat;
        require_once (DOCUMENT_ROOT . '/tcm/api/moodle/curl.php');
        $curl = new curl();


        $resp = $curl->post($serverurl, $params);


        echo '</br>************************** Server Response    createUser()**************************</br></br>';
        echo $serverurl . '</br></br>';

        var_dump($resp);
    }
}

信息:

对于所有的Moodle初学者,激活Moodle调试信息可以帮助一些。您将会在从服务器返回的响应中收到额外的错误信息。

Moodle -> 站点管理 -> 开发 -> 调试 -> 调试信息

选择: DEVELOPER:extra Moodle debug messages for developers


示例Web服务客户端仅包括以下内容:https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-REST/client.php - FMCorz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接