| 1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Module to handle AJAX interactions.
|
|
|
18 |
*
|
|
|
19 |
* @module core_user/repository
|
|
|
20 |
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
| 1441 |
ariadna |
24 |
import Config from 'core/config';
|
| 1 |
efrain |
25 |
import {call as fetchMany} from 'core/ajax';
|
| 1441 |
ariadna |
26 |
import Fetch from 'core/fetch';
|
| 1 |
efrain |
27 |
|
| 1441 |
ariadna |
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 |
|
| 1 |
efrain |
40 |
/**
|
| 1441 |
ariadna |
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));
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
/**
|
| 1 |
efrain |
74 |
* Get single user preference
|
|
|
75 |
*
|
|
|
76 |
* @param {String} name Name of the preference
|
|
|
77 |
* @param {Number} userid User ID (defaults to current user)
|
|
|
78 |
* @return {Promise}
|
|
|
79 |
*/
|
| 1441 |
ariadna |
80 |
export const getUserPreference = (name, userid = 0) => getUserPreferences(name, userid)
|
|
|
81 |
.then((response) => response[name]);
|
| 1 |
efrain |
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Get multiple user preferences
|
|
|
85 |
*
|
|
|
86 |
* @param {String|null} name Name of the preference (omit if you want to retrieve all)
|
|
|
87 |
* @param {Number} userid User ID (defaults to current user)
|
| 1441 |
ariadna |
88 |
* @return {Promise<object<string, string>>}
|
| 1 |
efrain |
89 |
*/
|
|
|
90 |
export const getUserPreferences = (name = null, userid = 0) => {
|
| 1441 |
ariadna |
91 |
checkUserId(userid);
|
|
|
92 |
const endpoint = ['current', 'preferences'];
|
|
|
93 |
|
|
|
94 |
if (name) {
|
|
|
95 |
endpoint.push(name);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return Fetch.performGet('core_user', endpoint.join('/')).then((response) => response.json());
|
| 1 |
efrain |
99 |
};
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Set single user preference
|
|
|
103 |
*
|
|
|
104 |
* @param {String} name Name of the preference
|
|
|
105 |
* @param {String|null} value Value of the preference (omit if you want to remove the current value)
|
|
|
106 |
* @param {Number} userid User ID (defaults to current user)
|
|
|
107 |
* @return {Promise}
|
|
|
108 |
*/
|
|
|
109 |
export const setUserPreference = (name, value = null, userid = 0) => {
|
| 1441 |
ariadna |
110 |
checkUserId(userid);
|
|
|
111 |
return Fetch.performPost(
|
|
|
112 |
'core_user',
|
|
|
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}]));
|
| 1 |
efrain |
121 |
};
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Set multiple user preferences
|
|
|
125 |
*
|
|
|
126 |
* @param {Object[]} preferences Array of preferences containing name/value/userid attributes
|
|
|
127 |
* @return {Promise}
|
|
|
128 |
*/
|
|
|
129 |
export const setUserPreferences = (preferences) => {
|
| 1441 |
ariadna |
130 |
preferences.forEach((preference) => checkUserId(preference.userid));
|
|
|
131 |
return Fetch.performPost(
|
|
|
132 |
'core_user',
|
|
|
133 |
'current/preferences',
|
|
|
134 |
{
|
|
|
135 |
body: {
|
|
|
136 |
preferences: Object.fromEntries (preferences.map((preference) => ([preference.name, preference.value]))),
|
|
|
137 |
},
|
|
|
138 |
},
|
|
|
139 |
)
|
|
|
140 |
// Return the result of the fetch call, and also add in the legacy saved property.
|
|
|
141 |
.then((response) => response.json())
|
|
|
142 |
.then((response) => addLegacySavedProperty(response, preferences));
|
| 1 |
efrain |
143 |
};
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Unenrol the user with the specified user enrolmentid ID.
|
|
|
147 |
*
|
|
|
148 |
* @param {Number} userEnrolmentId
|
|
|
149 |
* @return {Promise}
|
|
|
150 |
*/
|
|
|
151 |
export const unenrolUser = userEnrolmentId => {
|
|
|
152 |
return fetchMany([{
|
|
|
153 |
methodname: 'core_enrol_unenrol_user_enrolment',
|
|
|
154 |
args: {
|
|
|
155 |
ueid: userEnrolmentId,
|
|
|
156 |
},
|
|
|
157 |
}])[0];
|
|
|
158 |
};
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Submit the user enrolment form with the specified form data.
|
|
|
162 |
*
|
|
|
163 |
* @param {String} formdata
|
|
|
164 |
* @return {Promise}
|
|
|
165 |
*/
|
|
|
166 |
export const submitUserEnrolmentForm = formdata => {
|
|
|
167 |
return fetchMany([{
|
|
|
168 |
methodname: 'core_enrol_submit_user_enrolment_form',
|
|
|
169 |
args: {
|
|
|
170 |
formdata,
|
|
|
171 |
},
|
|
|
172 |
}])[0];
|
|
|
173 |
};
|
|
|
174 |
|
|
|
175 |
export const createNotesForUsers = notes => {
|
|
|
176 |
return fetchMany([{
|
|
|
177 |
methodname: 'core_notes_create_notes',
|
|
|
178 |
args: {
|
|
|
179 |
notes
|
|
|
180 |
}
|
|
|
181 |
}])[0];
|
|
|
182 |
};
|
|
|
183 |
|
|
|
184 |
export const sendMessagesToUsers = messages => {
|
|
|
185 |
return fetchMany([{
|
|
|
186 |
methodname: 'core_message_send_instant_messages',
|
|
|
187 |
args: {messages}
|
|
|
188 |
}])[0];
|
|
|
189 |
};
|