Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 19... Línea 19...
19
 * @module     core_user/repository
19
 * @module     core_user/repository
20
 * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
20
 * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
22
 */
Línea -... Línea 23...
-
 
23
 
23
 
24
import Config from 'core/config';
-
 
25
import {call as fetchMany} from 'core/ajax';
-
 
26
import Fetch from 'core/fetch';
-
 
27
 
-
 
28
const checkUserId = (userid) => {
-
 
29
    if (Number(userid) === 0) {
-
 
30
        return;
-
 
31
    }
-
 
32
    if (Number(userid) === Config.userId) {
-
 
33
        return;
-
 
34
    }
-
 
35
    throw new Error(
-
 
36
        `Invalid user ID: ${userid}. It is only possible to manage preferences for the current user.`,
-
 
37
    );
-
 
38
};
-
 
39
 
-
 
40
/**
-
 
41
 * Turn the response object into a Proxy object that will log a warning if the saved property is accessed.
-
 
42
 *
-
 
43
 * @param {Object} response
-
 
44
 * @param {Object} preferences The preferences that might be in the response
-
 
45
 * @return {Promise<Proxy>}
-
 
46
 */
-
 
47
const addLegacySavedProperty = (response, preferences) => {
-
 
48
    const debugLogger = {
-
 
49
        get(target, prop, receiver) {
-
 
50
            if (prop === 'then') {
-
 
51
                // To proxy a Promise we have to return null when the then key is requested.
-
 
52
                return null;
-
 
53
            }
-
 
54
            if (prop === 'saved') {
-
 
55
                window.console.warn(
-
 
56
                    'The saved property is deprecated. Please use the response object directly.',
-
 
57
                );
-
 
58
 
-
 
59
                return preferences
-
 
60
                    .filter((preference) => target.hasOwnProperty(preference.name))
-
 
61
                    .map((preference) => ({
-
 
62
                        name: preference.name,
-
 
63
                        userid: Config.userid,
-
 
64
                    }));
-
 
65
            }
-
 
66
            return Reflect.get(target, prop, receiver);
-
 
67
        },
-
 
68
    };
-
 
69
 
-
 
70
    return Promise.resolve(new Proxy(response, debugLogger));
Línea 24... Línea 71...
24
import {call as fetchMany} from 'core/ajax';
71
};
25
 
72
 
26
/**
73
/**
27
 * Get single user preference
74
 * Get single user preference
28
 *
75
 *
29
 * @param {String} name Name of the preference
76
 * @param {String} name Name of the preference
30
 * @param {Number} userid User ID (defaults to current user)
77
 * @param {Number} userid User ID (defaults to current user)
31
 * @return {Promise}
78
 * @return {Promise}
32
 */
-
 
33
export const getUserPreference = (name, userid = 0) => {
79
 */
34
    return getUserPreferences(name, userid)
-
 
Línea 35... Línea 80...
35
        .then(response => response.preferences[0].value);
80
export const getUserPreference = (name, userid = 0) => getUserPreferences(name, userid)
36
};
81
    .then((response) => response[name]);
37
 
82
 
38
/**
83
/**
39
 * Get multiple user preferences
84
 * Get multiple user preferences
40
 *
85
 *
41
 * @param {String|null} name Name of the preference (omit if you want to retrieve all)
86
 * @param {String|null} name Name of the preference (omit if you want to retrieve all)
42
 * @param {Number} userid User ID (defaults to current user)
87
 * @param {Number} userid User ID (defaults to current user)
43
 * @return {Promise}
88
 * @return {Promise<object<string, string>>}
44
 */
89
 */
-
 
90
export const getUserPreferences = (name = null, userid = 0) => {
-
 
91
    checkUserId(userid);
45
export const getUserPreferences = (name = null, userid = 0) => {
92
    const endpoint = ['current', 'preferences'];
46
    return fetchMany([{
93
 
-
 
94
    if (name) {
-
 
95
        endpoint.push(name);
47
        methodname: 'core_user_get_user_preferences',
96
    }
Línea 48... Línea 97...
48
        args: {name, userid}
97
 
49
    }])[0];
98
    return Fetch.performGet('core_user', endpoint.join('/')).then((response) => response.json());
50
};
99
};
Línea 56... Línea 105...
56
 * @param {String|null} value Value of the preference (omit if you want to remove the current value)
105
 * @param {String|null} value Value of the preference (omit if you want to remove the current value)
57
 * @param {Number} userid User ID (defaults to current user)
106
 * @param {Number} userid User ID (defaults to current user)
58
 * @return {Promise}
107
 * @return {Promise}
59
 */
108
 */
60
export const setUserPreference = (name, value = null, userid = 0) => {
109
export const setUserPreference = (name, value = null, userid = 0) => {
-
 
110
    checkUserId(userid);
-
 
111
    return Fetch.performPost(
-
 
112
        'core_user',
61
    return setUserPreferences([{name, value, userid}]);
113
        `current/preferences/${name}`,
-
 
114
        {
-
 
115
            body: {value},
-
 
116
        },
-
 
117
    )
-
 
118
    // Return the result of the fetch call, and also add in the legacy saved property.
-
 
119
    .then((response) => response.json())
-
 
120
    .then((response) => addLegacySavedProperty(response, [{name}]));
62
};
121
};
Línea 63... Línea 122...
63
 
122
 
64
/**
123
/**
65
 * Set multiple user preferences
124
 * Set multiple user preferences
66
 *
125
 *
67
 * @param {Object[]} preferences Array of preferences containing name/value/userid attributes
126
 * @param {Object[]} preferences Array of preferences containing name/value/userid attributes
68
 * @return {Promise}
127
 * @return {Promise}
69
 */
128
 */
-
 
129
export const setUserPreferences = (preferences) => {
70
export const setUserPreferences = (preferences) => {
130
    preferences.forEach((preference) => checkUserId(preference.userid));
71
    return fetchMany([{
131
    return Fetch.performPost(
72
        methodname: 'core_user_set_user_preferences',
132
        'core_user',
-
 
133
        'current/preferences',
-
 
134
        {
-
 
135
            body: {
-
 
136
                preferences: Object.fromEntries (preferences.map((preference) => ([preference.name, preference.value]))),
73
        args: {preferences}
137
            },
-
 
138
        },
-
 
139
    )
-
 
140
    // Return the result of the fetch call, and also add in the legacy saved property.
-
 
141
    .then((response) => response.json())
74
    }])[0];
142
    .then((response) => addLegacySavedProperty(response, preferences));
Línea 75... Línea 143...
75
};
143
};
76
 
144
 
77
/**
145
/**