「ユーザプロファイルフィールド」のAPI更新について

「ユーザプロファイルフィールド」のAPI更新について

- nob noby の投稿
返信数: 5

いつもお世話になっております。

下記、ご存知の方教えていただきたいです。


ご質問:ユーザのプロファイルの「他のフィールド」に表示されます「ユーザプロファイルフィールド」の更新には、API(core_user_update_users)のどのパラメータを使えばいいのでしょうか?


ユーザプロファイルフィールドをいくつか作成し、それをユーザ情報更新API(core_user_update_users)で更新しようと(フラグを立てようと)しましたが、

恐らく指定するパラメータが間違っており、うまく値が反映されません。


APIのドキュメントを参考にし   


users[0][customfields][0][type]  = other_field

users[0][customfields][0][value] = 作成したユーザプロファイルフィールド


をリクエスト項目に設定しましたがうまくいきません。

何でリクエストすればよいか、教えていただけますでしょうか。


よろしくお願いいたします。

nob noby への返信

Re: 「ユーザプロファイルフィールド」のAPI更新について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

お使いのMoodleのバージョンが分かりませんが、Moodle 3.9dev (Build: 20200312) でしたら、/usr/externallib.php の81行目あたりが参考になるかと思います。

    public static function create_users_parameters() {
        global $CFG;
        $userfields = [
            'createpassword' => new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.',
                VALUE_OPTIONAL),
            // General.
            'username' => new external_value(core_user::get_property_type('username'),
                'Username policy is defined in Moodle security config.'),
            'auth' => new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, etc',
                VALUE_DEFAULT, 'manual', core_user::get_property_null('auth')),
            'password' => new external_value(core_user::get_property_type('password'),
                'Plain text password consisting of any characters', VALUE_OPTIONAL),
            'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'),
            'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user'),
            'email' => new external_value(core_user::get_property_type('email'), 'A valid and unique email address'),
            'maildisplay' => new external_value(core_user::get_property_type('maildisplay'), 'Email display', VALUE_OPTIONAL),
            'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL),
            'country' => new external_value(core_user::get_property_type('country'),
                'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
            'timezone' => new external_value(core_user::get_property_type('timezone'),
                'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
            'description' => new external_value(core_user::get_property_type('description'), 'User profile description, no HTML',
                VALUE_OPTIONAL),
            // Additional names.
            'firstnamephonetic' => new external_value(core_user::get_property_type('firstnamephonetic'),
                'The first name(s) phonetically of the user', VALUE_OPTIONAL),
            'lastnamephonetic' => new external_value(core_user::get_property_type('lastnamephonetic'),
                'The family name phonetically of the user', VALUE_OPTIONAL),
            'middlename' => new external_value(core_user::get_property_type('middlename'), 'The middle name of the user',
                VALUE_OPTIONAL),
            'alternatename' => new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user',
                VALUE_OPTIONAL),
            // Interests.
            'interests' => new external_value(PARAM_TEXT, 'User interests (separated by commas)', VALUE_OPTIONAL),
            // Optional.
            'url' => new external_value(core_user::get_property_type('url'), 'User web page', VALUE_OPTIONAL),
            'icq' => new external_value(core_user::get_property_type('icq'), 'ICQ number', VALUE_OPTIONAL),
            'skype' => new external_value(core_user::get_property_type('skype'), 'Skype ID', VALUE_OPTIONAL),
            'aim' => new external_value(core_user::get_property_type('aim'), 'AIM ID', VALUE_OPTIONAL),
            'yahoo' => new external_value(core_user::get_property_type('yahoo'), 'Yahoo ID', VALUE_OPTIONAL),
            'msn' => new external_value(core_user::get_property_type('msn'), 'MSN ID', VALUE_OPTIONAL),
            'idnumber' => new external_value(core_user::get_property_type('idnumber'),
                'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, ''),
            'institution' => new external_value(core_user::get_property_type('institution'), 'institution', VALUE_OPTIONAL),
            'department' => new external_value(core_user::get_property_type('department'), 'department', VALUE_OPTIONAL),
            'phone1' => new external_value(core_user::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL),
            'phone2' => new external_value(core_user::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL),
            'address' => new external_value(core_user::get_property_type('address'), 'Postal address', VALUE_OPTIONAL),
            // Other user preferences stored in the user table.
            'lang' => new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server',
                VALUE_DEFAULT, core_user::get_property_default('lang'), core_user::get_property_null('lang')),
            'calendartype' => new external_value(core_user::get_property_type('calendartype'),
                'Calendar type such as "gregorian", must exist on server', VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL),
            'theme' => new external_value(core_user::get_property_type('theme'),
                'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
            'mailformat' => new external_value(core_user::get_property_type('mailformat'),
                'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
            // Custom user profile fields.
            'customfields' => new external_multiple_structure(
                new external_single_structure(
                    [
                        'type'  => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
                        'value' => new external_value(PARAM_RAW, 'The value of the custom field')
                    ]
                ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
            // User preferences.
            'preferences' => new external_multiple_structure(
            new external_single_structure(
                [
                    'type'  => new external_value(PARAM_RAW, 'The name of the preference'),
                    'value' => new external_value(PARAM_RAW, 'The value of the preference')
                ]
            ), 'User preferences', VALUE_OPTIONAL),
        ];
        return new external_function_parameters(
            [
                'users' => new external_multiple_structure(
                    new external_single_structure($userfields)
                )
            ]
        );
    }
Mitsuhiro Yoshida への返信

Re: 「ユーザプロファイルフィールド」のAPI更新について

- nob noby の投稿
ご回答いただき、ありがとうございます。

// Custom user profile fields.
'customfields' => new external_multiple_structure(
new external_single_structure(
[
'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
'value' => new external_value(PARAM_RAW, 'The value of the custom field')
]
),
'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),

を見ますと、customfieldsに設定するのはあってそうですね。

\moodle\customfield\classes\handler.php
\moodle\lang\en\customfield.php
なども見ますと、「他のフィールド」については、「otherfields」が項目名のようですね。

users[0][customfields][0][type] = otherfields
users[0][customfields][0][value] = 作成したユーザプロファイルフィールド

で一度試してみようと思います。
nob noby への返信

Re: 「ユーザプロファイルフィールド」のAPI更新について

- nob noby の投稿
お世話になっております。

> users[0][customfields][0][type] = otherfields
> users[0][customfields][0][value] = 作成したユーザプロファイルフィールド

こちらを設定し、APIを実施してみたのですが、反映されないようです。

項目内容に間違いありました、ご指摘いただけますと大変ありがたいです。

お手数おかけしますが、よろしくお願いいたします。
nob noby への返信

Re: 「ユーザプロファイルフィールド」のAPI更新について

- Mitsuhiro Yoshida の投稿
画像 Developers 画像 Particularly helpful Moodlers 画像 Translators

以下のような設定のように思えます。
users[0][customfields][0][type] = 作成したユーザプロファイルフィールド
users[0][customfields][0][value] = 作成したユーザプロファイルフィールドの値

以下のbiboy atienzaさんによる投稿が参考になりませんでしょうか?
[Moodle in English: Re: How to fill in Custom Field value using core_user_create_users?]
https://moodle.org/mod/forum/discuss.php?d=366530#p1478340

-----------------------------------------------------------------

Additional info:
If you have more than 1 custom fields, be sure to adjust the array index like below: 
&users[0][customfields][0][type]=customer
&users[0][customfields][0][value]=Customer1

&users[0][customfields][1][type]=customeremail
&users[0][customfields][1][value]=customer@email.com

&users[0][customfields][2][type]=customerphone
&users[0][customfields][2][value]=+6325217788

So the link would be :

https://our-sandbox.mrooms.net/webservice/rest/server.php?wstoken=OUT_TOKEN_HERE&wsfunction=core_user_create_users&moodlewsrestformat=xml&users[0][username]=biboyatienza@gmail.com&users[0][password]=00.00.0000&users[0][firstname]=biboy&users[0][lastname]=atienza&users[0][email]=biboyatienza@gmail.com&users[0][city]=Manila&users[0][country]=Philippines&users[0][customfields][0][type]=customer&users[0][customfields][0][value]=Customer1&users[0][customfields][1][type]=customeremail&users[0][customfields][1][value]=customer@email.com&users[0][customfields][2][type]=customerphone&users[0][customfields][2][value]=+6325217788

 

Hope this will help someone else.

br,

biboyatienza

Mitsuhiro Yoshida への返信

Re: 「ユーザプロファイルフィールド」のAPI更新について

- nob noby の投稿
ご返信いただき、ありがとうございます。

users[0][customfields][0][type] = 作成したユーザプロファイルフィールド
users[0][customfields][0][value] = 1(もしくは0)

このリクエストで、問題無く設定されました。
「他のフィールド」は関係ありませんでした。

また、GETでのリクエスト方法もありがとうございます。
URL直書きで確認出来ました。

前回の質問に続き、誠にありがとうございました!

また何かありましたら、ご助言いただけますと幸いです。