3

我想将外部网站与moodle系统连接起来。我已经设置了 webService 并创建了一个令牌来获取访问权限。

我已经按照http://www.rumours.co.nz/manuals/using_moodle_web_services.htm设置但相反我想通过 REST 实现连接,如https://github.com/moodlehq/sample-ws-客户/查找/主

我的方法是有一个moodle类来处理数据交换。首先,我只是想尝试通过 webService 创建一些新的硬编码用户,但它因 Moodle-Response 而失败:

“invalidrecord 在数据库表 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 在他们的文档中链接到它.. docs.moodle.org/dev/Creating_a_web_service_client

我的调用的入口点现在是硬编码的:

<?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”错误或有不同的方法/建议我如何远程创建我的用户?

提前致谢

4

1 回答 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:为开发人员提供的额外 Moodle 调试消息

于 2015-10-30T03:39:10.510 回答