3

外部のウェブサイトをmoodle-systemに接続したいです。すでに webService をセットアップし、アクセスするためのトークンを作成しました。

http://www.rumours.co.nz/manuals/using_moodle_web_services.htmのセットアップに従いましたが、対照的に、 https://github.com/moodlehq/sample-ws-のように REST 経由で接続を実現したかったのです。クライアント/検索/マスター

私のアプローチは、データ交換を処理するムードル クラスを持つことです。最初に、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-projectの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 に答える