Commit | Line | Data |
---|---|---|
a623b6b8 | 1 | <?php |
a623b6b8 JM |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
4615817d | 17 | |
a623b6b8 JM |
18 | /** |
19 | * External message API | |
20 | * | |
6fbd60ef | 21 | * @package core_message |
4615817d JM |
22 | * @category external |
23 | * @copyright 2011 Jerome Mouneyrac | |
a623b6b8 JM |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
25 | */ | |
4615817d | 26 | |
a623b6b8 | 27 | require_once("$CFG->libdir/externallib.php"); |
705afe6f | 28 | require_once($CFG->dirroot . "/message/lib.php"); |
a623b6b8 | 29 | |
5d1017e1 | 30 | /** |
4615817d | 31 | * Message external functions |
6fbd60ef AD |
32 | * |
33 | * @package core_message | |
4615817d JM |
34 | * @category external |
35 | * @copyright 2011 Jerome Mouneyrac | |
75e4f98c | 36 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
4615817d | 37 | * @since Moodle 2.2 |
5d1017e1 JM |
38 | */ |
39 | class core_message_external extends external_api { | |
a623b6b8 JM |
40 | |
41 | /** | |
42 | * Returns description of method parameters | |
4615817d | 43 | * |
a623b6b8 | 44 | * @return external_function_parameters |
4615817d | 45 | * @since Moodle 2.2 |
a623b6b8 | 46 | */ |
5d1017e1 | 47 | public static function send_instant_messages_parameters() { |
a623b6b8 JM |
48 | return new external_function_parameters( |
49 | array( | |
50 | 'messages' => new external_multiple_structure( | |
51 | new external_single_structure( | |
52 | array( | |
53 | 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'), | |
93ce0e82 | 54 | 'text' => new external_value(PARAM_RAW, 'the text of the message'), |
14968ca9 | 55 | 'textformat' => new external_format_value('text', VALUE_DEFAULT, FORMAT_MOODLE), |
a623b6b8 JM |
56 | 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the message. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL), |
57 | ) | |
58 | ) | |
59 | ) | |
60 | ) | |
61 | ); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Send private messages from the current USER to other users | |
66 | * | |
4615817d JM |
67 | * @param array $messages An array of message to send. |
68 | * @return array | |
69 | * @since Moodle 2.2 | |
a623b6b8 | 70 | */ |
5d1017e1 | 71 | public static function send_instant_messages($messages = array()) { |
a623b6b8 | 72 | global $CFG, $USER, $DB; |
a623b6b8 | 73 | |
436bbf89 | 74 | // Check if messaging is enabled. |
a623b6b8 JM |
75 | if (!$CFG->messaging) { |
76 | throw new moodle_exception('disabled', 'message'); | |
77 | } | |
78 | ||
79 | // Ensure the current user is allowed to run this function | |
bf0f06b1 | 80 | $context = context_system::instance(); |
a623b6b8 JM |
81 | self::validate_context($context); |
82 | require_capability('moodle/site:sendmessage', $context); | |
83 | ||
5d1017e1 | 84 | $params = self::validate_parameters(self::send_instant_messages_parameters(), array('messages' => $messages)); |
a623b6b8 JM |
85 | |
86 | //retrieve all tousers of the messages | |
4de00da7 | 87 | $receivers = array(); |
a623b6b8 | 88 | foreach($params['messages'] as $message) { |
4de00da7 | 89 | $receivers[] = $message['touserid']; |
a623b6b8 | 90 | } |
4de00da7 | 91 | list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_'); |
a623b6b8 | 92 | $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams); |
4de00da7 DC |
93 | $blocklist = array(); |
94 | $contactlist = array(); | |
a623b6b8 | 95 | $sqlparams['contactid'] = $USER->id; |
4de00da7 DC |
96 | $rs = $DB->get_recordset_sql("SELECT * |
97 | FROM {message_contacts} | |
98 | WHERE userid $sqluserids | |
99 | AND contactid = :contactid", $sqlparams); | |
100 | foreach ($rs as $record) { | |
101 | if ($record->blocked) { | |
102 | // $record->userid is blocking current user | |
103 | $blocklist[$record->userid] = true; | |
104 | } else { | |
105 | // $record->userid have current user as contact | |
106 | $contactlist[$record->userid] = true; | |
107 | } | |
108 | } | |
109 | $rs->close(); | |
a623b6b8 JM |
110 | |
111 | $canreadallmessages = has_capability('moodle/site:readallmessages', $context); | |
112 | ||
113 | $resultmessages = array(); | |
114 | foreach ($params['messages'] as $message) { | |
a623b6b8 JM |
115 | $resultmsg = array(); //the infos about the success of the operation |
116 | ||
117 | //we are going to do some checking | |
118 | //code should match /messages/index.php checks | |
119 | $success = true; | |
120 | ||
121 | //check the user exists | |
122 | if (empty($tousers[$message['touserid']])) { | |
123 | $success = false; | |
124 | $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']); | |
125 | } | |
126 | ||
127 | //check that the touser is not blocking the current user | |
4de00da7 | 128 | if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) { |
a623b6b8 JM |
129 | $success = false; |
130 | $errormessage = get_string('userisblockingyou', 'message'); | |
131 | } | |
132 | ||
78736e5d | 133 | // Check if the user is a contact |
4615817d | 134 | //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid |
4de00da7 DC |
135 | $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']); |
136 | // message_blocknoncontacts option is on and current user is not in contact list | |
137 | if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) { | |
78736e5d SH |
138 | // The user isn't a contact and they have selected to block non contacts so this message won't be sent. |
139 | $success = false; | |
126a4bc4 CS |
140 | $errormessage = get_string('userisblockingyounoncontact', 'message', |
141 | fullname(core_user::get_user($message['touserid']))); | |
a623b6b8 JM |
142 | } |
143 | ||
144 | //now we can send the message (at least try) | |
145 | if ($success) { | |
4615817d | 146 | //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object |
93ce0e82 JM |
147 | $success = message_post_message($USER, $tousers[$message['touserid']], |
148 | $message['text'], external_validate_format($message['textformat'])); | |
a623b6b8 JM |
149 | } |
150 | ||
151 | //build the resultmsg | |
152 | if (isset($message['clientmsgid'])) { | |
78736e5d | 153 | $resultmsg['clientmsgid'] = $message['clientmsgid']; |
a623b6b8 JM |
154 | } |
155 | if ($success) { | |
156 | $resultmsg['msgid'] = $success; | |
157 | } else { | |
93ce0e82 JM |
158 | // WARNINGS: for backward compatibility we return this errormessage. |
159 | // We should have thrown exceptions as these errors prevent results to be returned. | |
160 | // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side . | |
a623b6b8 JM |
161 | $resultmsg['msgid'] = -1; |
162 | $resultmsg['errormessage'] = $errormessage; | |
163 | } | |
164 | ||
165 | $resultmessages[] = $resultmsg; | |
166 | } | |
167 | ||
168 | return $resultmessages; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Returns description of method result value | |
4615817d | 173 | * |
a623b6b8 | 174 | * @return external_description |
4615817d | 175 | * @since Moodle 2.2 |
a623b6b8 | 176 | */ |
5d1017e1 | 177 | public static function send_instant_messages_returns() { |
a623b6b8 JM |
178 | return new external_multiple_structure( |
179 | new external_single_structure( | |
180 | array( | |
78736e5d | 181 | 'msgid' => new external_value(PARAM_INT, 'test this to know if it succeeds: id of the created message if it succeeded, -1 when failed'), |
4de00da7 | 182 | 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL), |
78736e5d | 183 | 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL) |
a623b6b8 JM |
184 | ) |
185 | ) | |
186 | ); | |
187 | } | |
188 | ||
d6731600 FM |
189 | /** |
190 | * Create contacts parameters description. | |
191 | * | |
192 | * @return external_function_parameters | |
5bcfd504 | 193 | * @since Moodle 2.5 |
d6731600 FM |
194 | */ |
195 | public static function create_contacts_parameters() { | |
196 | return new external_function_parameters( | |
197 | array( | |
198 | 'userids' => new external_multiple_structure( | |
199 | new external_value(PARAM_INT, 'User ID'), | |
200 | 'List of user IDs' | |
34c2f347 MN |
201 | ), |
202 | 'userid' => new external_value(PARAM_INT, 'The id of the user we are creating the contacts for, 0 for the | |
203 | current user', VALUE_DEFAULT, 0) | |
d6731600 FM |
204 | ) |
205 | ); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Create contacts. | |
210 | * | |
211 | * @param array $userids array of user IDs. | |
34c2f347 | 212 | * @param int $userid The id of the user we are creating the contacts for |
d6731600 | 213 | * @return external_description |
5bcfd504 | 214 | * @since Moodle 2.5 |
d6731600 | 215 | */ |
34c2f347 | 216 | public static function create_contacts($userids, $userid = 0) { |
436bbf89 DM |
217 | global $CFG; |
218 | ||
219 | // Check if messaging is enabled. | |
220 | if (!$CFG->messaging) { | |
221 | throw new moodle_exception('disabled', 'message'); | |
222 | } | |
223 | ||
34c2f347 | 224 | $params = array('userids' => $userids, 'userid' => $userid); |
d6731600 FM |
225 | $params = self::validate_parameters(self::create_contacts_parameters(), $params); |
226 | ||
227 | $warnings = array(); | |
228 | foreach ($params['userids'] as $id) { | |
34c2f347 | 229 | if (!message_add_contact($id, 0, $userid)) { |
d6731600 FM |
230 | $warnings[] = array( |
231 | 'item' => 'user', | |
232 | 'itemid' => $id, | |
233 | 'warningcode' => 'contactnotcreated', | |
234 | 'message' => 'The contact could not be created' | |
235 | ); | |
236 | } | |
237 | } | |
238 | return $warnings; | |
239 | } | |
240 | ||
241 | /** | |
242 | * Create contacts return description. | |
243 | * | |
244 | * @return external_description | |
5bcfd504 | 245 | * @since Moodle 2.5 |
d6731600 FM |
246 | */ |
247 | public static function create_contacts_returns() { | |
248 | return new external_warnings(); | |
249 | } | |
250 | ||
251 | /** | |
252 | * Delete contacts parameters description. | |
253 | * | |
254 | * @return external_function_parameters | |
5bcfd504 | 255 | * @since Moodle 2.5 |
d6731600 FM |
256 | */ |
257 | public static function delete_contacts_parameters() { | |
258 | return new external_function_parameters( | |
259 | array( | |
260 | 'userids' => new external_multiple_structure( | |
261 | new external_value(PARAM_INT, 'User ID'), | |
262 | 'List of user IDs' | |
34c2f347 MN |
263 | ), |
264 | 'userid' => new external_value(PARAM_INT, 'The id of the user we are deleting the contacts for, 0 for the | |
265 | current user', VALUE_DEFAULT, 0) | |
d6731600 FM |
266 | ) |
267 | ); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Delete contacts. | |
272 | * | |
273 | * @param array $userids array of user IDs. | |
34c2f347 | 274 | * @param int $userid The id of the user we are deleting the contacts for |
d6731600 | 275 | * @return null |
5bcfd504 | 276 | * @since Moodle 2.5 |
d6731600 | 277 | */ |
34c2f347 | 278 | public static function delete_contacts($userids, $userid = 0) { |
436bbf89 DM |
279 | global $CFG; |
280 | ||
281 | // Check if messaging is enabled. | |
282 | if (!$CFG->messaging) { | |
283 | throw new moodle_exception('disabled', 'message'); | |
284 | } | |
285 | ||
34c2f347 | 286 | $params = array('userids' => $userids, 'userid' => $userid); |
d6731600 FM |
287 | $params = self::validate_parameters(self::delete_contacts_parameters(), $params); |
288 | ||
289 | foreach ($params['userids'] as $id) { | |
34c2f347 | 290 | message_remove_contact($id, $userid); |
d6731600 FM |
291 | } |
292 | ||
293 | return null; | |
294 | } | |
295 | ||
296 | /** | |
297 | * Delete contacts return description. | |
298 | * | |
299 | * @return external_description | |
5bcfd504 | 300 | * @since Moodle 2.5 |
d6731600 FM |
301 | */ |
302 | public static function delete_contacts_returns() { | |
303 | return null; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Block contacts parameters description. | |
308 | * | |
309 | * @return external_function_parameters | |
5bcfd504 | 310 | * @since Moodle 2.5 |
d6731600 FM |
311 | */ |
312 | public static function block_contacts_parameters() { | |
313 | return new external_function_parameters( | |
314 | array( | |
315 | 'userids' => new external_multiple_structure( | |
316 | new external_value(PARAM_INT, 'User ID'), | |
317 | 'List of user IDs' | |
34c2f347 MN |
318 | ), |
319 | 'userid' => new external_value(PARAM_INT, 'The id of the user we are blocking the contacts for, 0 for the | |
320 | current user', VALUE_DEFAULT, 0) | |
d6731600 FM |
321 | ) |
322 | ); | |
323 | } | |
324 | ||
325 | /** | |
326 | * Block contacts. | |
327 | * | |
328 | * @param array $userids array of user IDs. | |
34c2f347 | 329 | * @param int $userid The id of the user we are blocking the contacts for |
d6731600 | 330 | * @return external_description |
5bcfd504 | 331 | * @since Moodle 2.5 |
d6731600 | 332 | */ |
34c2f347 | 333 | public static function block_contacts($userids, $userid = 0) { |
436bbf89 DM |
334 | global $CFG; |
335 | ||
336 | // Check if messaging is enabled. | |
337 | if (!$CFG->messaging) { | |
338 | throw new moodle_exception('disabled', 'message'); | |
339 | } | |
340 | ||
34c2f347 | 341 | $params = array('userids' => $userids, 'userid' => $userid); |
d6731600 FM |
342 | $params = self::validate_parameters(self::block_contacts_parameters(), $params); |
343 | ||
344 | $warnings = array(); | |
345 | foreach ($params['userids'] as $id) { | |
34c2f347 | 346 | if (!message_block_contact($id, $userid)) { |
d6731600 FM |
347 | $warnings[] = array( |
348 | 'item' => 'user', | |
349 | 'itemid' => $id, | |
350 | 'warningcode' => 'contactnotblocked', | |
351 | 'message' => 'The contact could not be blocked' | |
352 | ); | |
353 | } | |
354 | } | |
355 | return $warnings; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Block contacts return description. | |
360 | * | |
361 | * @return external_description | |
5bcfd504 | 362 | * @since Moodle 2.5 |
d6731600 FM |
363 | */ |
364 | public static function block_contacts_returns() { | |
365 | return new external_warnings(); | |
366 | } | |
367 | ||
368 | /** | |
369 | * Unblock contacts parameters description. | |
370 | * | |
371 | * @return external_function_parameters | |
5bcfd504 | 372 | * @since Moodle 2.5 |
d6731600 FM |
373 | */ |
374 | public static function unblock_contacts_parameters() { | |
375 | return new external_function_parameters( | |
376 | array( | |
377 | 'userids' => new external_multiple_structure( | |
378 | new external_value(PARAM_INT, 'User ID'), | |
379 | 'List of user IDs' | |
34c2f347 MN |
380 | ), |
381 | 'userid' => new external_value(PARAM_INT, 'The id of the user we are unblocking the contacts for, 0 for the | |
382 | current user', VALUE_DEFAULT, 0) | |
d6731600 FM |
383 | ) |
384 | ); | |
385 | } | |
386 | ||
387 | /** | |
388 | * Unblock contacts. | |
389 | * | |
390 | * @param array $userids array of user IDs. | |
34c2f347 | 391 | * @param int $userid The id of the user we are unblocking the contacts for |
d6731600 | 392 | * @return null |
5bcfd504 | 393 | * @since Moodle 2.5 |
d6731600 | 394 | */ |
34c2f347 | 395 | public static function unblock_contacts($userids, $userid = 0) { |
436bbf89 DM |
396 | global $CFG; |
397 | ||
398 | // Check if messaging is enabled. | |
399 | if (!$CFG->messaging) { | |
400 | throw new moodle_exception('disabled', 'message'); | |
401 | } | |
402 | ||
34c2f347 | 403 | $params = array('userids' => $userids, 'userid' => $userid); |
d6731600 FM |
404 | $params = self::validate_parameters(self::unblock_contacts_parameters(), $params); |
405 | ||
406 | foreach ($params['userids'] as $id) { | |
34c2f347 | 407 | message_unblock_contact($id, $userid); |
d6731600 FM |
408 | } |
409 | ||
410 | return null; | |
411 | } | |
412 | ||
413 | /** | |
414 | * Unblock contacts return description. | |
415 | * | |
416 | * @return external_description | |
5bcfd504 | 417 | * @since Moodle 2.5 |
d6731600 FM |
418 | */ |
419 | public static function unblock_contacts_returns() { | |
420 | return null; | |
421 | } | |
422 | ||
a3e3a3a1 MN |
423 | /** |
424 | * Return the structure of a message area contact. | |
425 | * | |
426 | * @return external_single_structure | |
427 | * @since Moodle 3.2 | |
428 | */ | |
429 | private static function get_messagearea_contact_structure() { | |
430 | return new external_single_structure( | |
431 | array( | |
432 | 'userid' => new external_value(PARAM_INT, 'The user\'s id'), | |
433 | 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'), | |
434 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'), | |
435 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'), | |
5bf0ff27 | 436 | 'ismessaging' => new external_value(PARAM_BOOL, 'If we are messaging the user'), |
89a70ba1 | 437 | 'sentfromcurrentuser' => new external_value(PARAM_BOOL, 'Was the last message sent from the current user?'), |
a3e3a3a1 MN |
438 | 'lastmessage' => new external_value(PARAM_NOTAGS, 'The user\'s last message'), |
439 | 'messageid' => new external_value(PARAM_INT, 'The unique search message id', VALUE_DEFAULT, null), | |
440 | 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), | |
441 | 'isread' => new external_value(PARAM_BOOL, 'If the user has read the message'), | |
dd0c1403 | 442 | 'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'), |
a3e3a3a1 MN |
443 | 'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation', |
444 | VALUE_DEFAULT, null), | |
445 | ) | |
446 | ); | |
447 | } | |
448 | ||
94e1db61 MN |
449 | /** |
450 | * Return the structure of a message area message. | |
451 | * | |
452 | * @return external_single_structure | |
453 | * @since Moodle 3.2 | |
454 | */ | |
455 | private static function get_messagearea_message_structure() { | |
456 | return new external_single_structure( | |
457 | array( | |
458 | 'id' => new external_value(PARAM_INT, 'The id of the message'), | |
89a70ba1 MN |
459 | 'useridfrom' => new external_value(PARAM_INT, 'The id of the user who sent the message'), |
460 | 'useridto' => new external_value(PARAM_INT, 'The id of the user who received the message'), | |
94e1db61 MN |
461 | 'text' => new external_value(PARAM_RAW, 'The text of the message'), |
462 | 'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'), | |
463 | 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), | |
464 | 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), | |
465 | 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), | |
466 | 'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'), | |
467 | ) | |
468 | ); | |
469 | } | |
470 | ||
cd03b8d7 MN |
471 | /** |
472 | * Get messagearea search people parameters. | |
473 | * | |
474 | * @return external_function_parameters | |
475 | * @since 3.2 | |
476 | */ | |
477 | public static function data_for_messagearea_search_people_in_course_parameters() { | |
478 | return new external_function_parameters( | |
479 | array( | |
480 | 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'), | |
481 | 'courseid' => new external_value(PARAM_INT, 'The id of the course'), | |
482 | 'search' => new external_value(PARAM_RAW, 'The string being searched'), | |
483 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
484 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
485 | ) | |
486 | ); | |
487 | } | |
488 | ||
489 | /** | |
490 | * Get messagearea search people results. | |
491 | * | |
492 | * @param int $userid The id of the user who is performing the search | |
493 | * @param int $courseid The id of the course | |
494 | * @param string $search The string being searched | |
495 | * @param int $limitfrom | |
496 | * @param int $limitnum | |
497 | * @return stdClass | |
498 | * @throws moodle_exception | |
499 | * @since 3.2 | |
500 | */ | |
501 | public static function data_for_messagearea_search_people_in_course($userid, $courseid, $search, $limitfrom = 0, $limitnum = 0) { | |
502 | global $CFG, $PAGE; | |
503 | ||
504 | // Check if messaging is enabled. | |
505 | if (!$CFG->messaging) { | |
506 | throw new moodle_exception('disabled', 'message'); | |
507 | } | |
508 | ||
509 | $params = array( | |
510 | 'userid' => $userid, | |
511 | 'courseid' => $courseid, | |
512 | 'search' => $search, | |
513 | 'limitfrom' => $limitfrom, | |
514 | 'limitnum' => $limitnum | |
515 | ); | |
516 | self::validate_parameters(self::data_for_messagearea_search_people_in_course_parameters(), $params); | |
517 | ||
518 | self::validate_context(context_user::instance($userid)); | |
519 | ||
520 | $search = \core_message\api::search_people_in_course($userid, $courseid, $search, $limitfrom, $limitnum); | |
521 | ||
522 | $renderer = $PAGE->get_renderer('core_message'); | |
523 | return $search->export_for_template($renderer); | |
524 | } | |
525 | ||
526 | /** | |
527 | * Get messagearea search people returns. | |
528 | * | |
529 | * @return external_single_structure | |
530 | * @since 3.2 | |
531 | */ | |
532 | public static function data_for_messagearea_search_people_in_course_returns() { | |
533 | return new external_single_structure( | |
534 | array( | |
535 | 'hascontacts' => new external_value(PARAM_BOOL, 'Are there contacts?'), | |
536 | 'contacts' => new external_multiple_structure( | |
a3e3a3a1 | 537 | self::get_messagearea_contact_structure() |
cd03b8d7 MN |
538 | ), |
539 | ) | |
540 | ); | |
541 | } | |
542 | ||
543 | /** | |
544 | * Get messagearea search people parameters. | |
545 | * | |
546 | * @return external_function_parameters | |
547 | * @since 3.2 | |
548 | */ | |
549 | public static function data_for_messagearea_search_people_parameters() { | |
550 | return new external_function_parameters( | |
551 | array( | |
552 | 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'), | |
553 | 'search' => new external_value(PARAM_RAW, 'The string being searched'), | |
554 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
555 | ) | |
556 | ); | |
557 | } | |
558 | ||
559 | /** | |
560 | * Get messagearea search people results. | |
561 | * | |
562 | * @param int $userid The id of the user who is performing the search | |
563 | * @param string $search The string being searched | |
564 | * @param int $limitnum | |
565 | * @return stdClass | |
566 | * @throws moodle_exception | |
567 | * @since 3.2 | |
568 | */ | |
569 | public static function data_for_messagearea_search_people($userid, $search, $limitnum = 0) { | |
570 | global $CFG, $PAGE; | |
571 | ||
572 | // Check if messaging is enabled. | |
573 | if (!$CFG->messaging) { | |
574 | throw new moodle_exception('disabled', 'message'); | |
575 | } | |
576 | ||
577 | $params = array( | |
578 | 'userid' => $userid, | |
579 | 'search' => $search, | |
580 | 'limitnum' => $limitnum | |
581 | ); | |
582 | self::validate_parameters(self::data_for_messagearea_search_people_parameters(), $params); | |
583 | ||
584 | self::validate_context(context_user::instance($userid)); | |
585 | ||
586 | $search = \core_message\api::search_people($userid, $search, $limitnum); | |
587 | ||
588 | $renderer = $PAGE->get_renderer('core_message'); | |
589 | return $search->export_for_template($renderer); | |
590 | } | |
591 | ||
592 | /** | |
593 | * Get messagearea search people returns. | |
594 | * | |
595 | * @return external_single_structure | |
596 | * @since 3.2 | |
597 | */ | |
598 | public static function data_for_messagearea_search_people_returns() { | |
599 | return new external_single_structure( | |
600 | array( | |
601 | 'hascontacts' => new external_value(PARAM_BOOL, 'Are there contacts?'), | |
602 | 'contacts' => new external_multiple_structure( | |
a3e3a3a1 | 603 | self::get_messagearea_contact_structure() |
cd03b8d7 MN |
604 | ), |
605 | 'hascourses' => new external_value(PARAM_BOOL, 'Are there courses?'), | |
606 | 'courses' => new external_multiple_structure( | |
607 | new external_single_structure( | |
608 | array( | |
609 | 'id' => new external_value(PARAM_INT, 'The course id'), | |
610 | 'shortname' => new external_value(PARAM_NOTAGS, 'The course shortname'), | |
611 | 'fullname' => new external_value(PARAM_NOTAGS, 'The course fullname'), | |
612 | ) | |
613 | ) | |
614 | ), | |
615 | 'hasnoncontacts' => new external_value(PARAM_BOOL, 'Are there non-contacts?'), | |
616 | 'noncontacts' => new external_multiple_structure( | |
a3e3a3a1 | 617 | self::get_messagearea_contact_structure() |
cd03b8d7 MN |
618 | ) |
619 | ) | |
620 | ); | |
621 | } | |
622 | ||
623 | /** | |
624 | * Get messagearea search messages parameters. | |
625 | * | |
626 | * @return external_function_parameters | |
627 | * @since 3.2 | |
628 | */ | |
629 | public static function data_for_messagearea_search_messages_parameters() { | |
630 | return new external_function_parameters( | |
631 | array( | |
632 | 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'), | |
633 | 'search' => new external_value(PARAM_RAW, 'The string being searched'), | |
634 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
635 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
636 | ) | |
637 | ); | |
638 | } | |
639 | ||
640 | /** | |
641 | * Get messagearea search messages results. | |
642 | * | |
643 | * @param int $userid The id of the user who is performing the search | |
644 | * @param string $search The string being searched | |
645 | * @param int $limitfrom | |
646 | * @param int $limitnum | |
647 | * @return stdClass | |
648 | * @throws moodle_exception | |
649 | * @since 3.2 | |
650 | */ | |
651 | public static function data_for_messagearea_search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) { | |
652 | global $CFG, $PAGE; | |
653 | ||
654 | // Check if messaging is enabled. | |
655 | if (!$CFG->messaging) { | |
656 | throw new moodle_exception('disabled', 'message'); | |
657 | } | |
658 | ||
659 | $params = array( | |
660 | 'userid' => $userid, | |
661 | 'search' => $search, | |
662 | 'limitfrom' => $limitfrom, | |
663 | 'limitnum' => $limitnum | |
664 | ||
665 | ); | |
666 | self::validate_parameters(self::data_for_messagearea_search_messages_parameters(), $params); | |
667 | ||
668 | self::validate_context(context_user::instance($userid)); | |
669 | ||
670 | $search = \core_message\api::search_messages($userid, $search, $limitfrom, $limitnum); | |
671 | ||
672 | $renderer = $PAGE->get_renderer('core_message'); | |
673 | return $search->export_for_template($renderer); | |
674 | } | |
675 | ||
676 | /** | |
677 | * Get messagearea search messages returns. | |
678 | * | |
679 | * @return external_single_structure | |
680 | * @since 3.2 | |
681 | */ | |
682 | public static function data_for_messagearea_search_messages_returns() { | |
683 | return new external_single_structure( | |
684 | array( | |
685 | 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), | |
686 | 'contacts' => new external_multiple_structure( | |
a3e3a3a1 | 687 | self::get_messagearea_contact_structure() |
cd03b8d7 MN |
688 | ) |
689 | ) | |
690 | ); | |
691 | } | |
692 | ||
9aa012b5 | 693 | /** |
49aaadc3 | 694 | * The messagearea conversations parameters. |
9aa012b5 MN |
695 | * |
696 | * @return external_function_parameters | |
49aaadc3 | 697 | * @since 3.2 |
9aa012b5 MN |
698 | */ |
699 | public static function data_for_messagearea_conversations_parameters() { | |
700 | return new external_function_parameters( | |
701 | array( | |
702 | 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), | |
703 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
704 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
705 | ) | |
706 | ); | |
707 | } | |
708 | ||
709 | /** | |
710 | * Get messagearea conversations. | |
711 | * | |
712 | * @param int $userid The id of the user who we are viewing conversations for | |
713 | * @param int $limitfrom | |
714 | * @param int $limitnum | |
49aaadc3 MN |
715 | * @return stdClass |
716 | * @throws moodle_exception | |
717 | * @since 3.2 | |
9aa012b5 MN |
718 | */ |
719 | public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) { | |
720 | global $CFG, $PAGE; | |
721 | ||
722 | // Check if messaging is enabled. | |
723 | if (!$CFG->messaging) { | |
724 | throw new moodle_exception('disabled', 'message'); | |
725 | } | |
726 | ||
727 | $params = array( | |
728 | 'userid' => $userid, | |
729 | 'limitfrom' => $limitfrom, | |
730 | 'limitnum' => $limitnum | |
731 | ); | |
732 | self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params); | |
733 | ||
734 | self::validate_context(context_user::instance($userid)); | |
735 | ||
736 | $contacts = \core_message\api::get_conversations($userid, 0, $limitfrom, $limitnum); | |
737 | ||
738 | $renderer = $PAGE->get_renderer('core_message'); | |
739 | return $contacts->export_for_template($renderer); | |
740 | } | |
741 | ||
742 | /** | |
49aaadc3 | 743 | * The messagearea conversations return structure. |
9aa012b5 | 744 | * |
49aaadc3 MN |
745 | * @return external_single_structure |
746 | * @since 3.2 | |
9aa012b5 MN |
747 | */ |
748 | public static function data_for_messagearea_conversations_returns() { | |
49aaadc3 | 749 | return new external_single_structure( |
9aa012b5 MN |
750 | array( |
751 | 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), | |
4d1b76ee | 752 | 'isconversation' => new external_value(PARAM_BOOL, 'Are we storing conversations or contacts?'), |
9aa012b5 | 753 | 'contacts' => new external_multiple_structure( |
a3e3a3a1 | 754 | self::get_messagearea_contact_structure() |
9aa012b5 MN |
755 | ) |
756 | ) | |
757 | ); | |
758 | } | |
759 | ||
760 | /** | |
49aaadc3 | 761 | * The messagearea contacts return parameters. |
9aa012b5 MN |
762 | * |
763 | * @return external_function_parameters | |
49aaadc3 | 764 | * @since 3.2 |
9aa012b5 MN |
765 | */ |
766 | public static function data_for_messagearea_contacts_parameters() { | |
767 | return self::data_for_messagearea_conversations_parameters(); | |
768 | } | |
769 | ||
770 | /** | |
771 | * Get messagearea contacts parameters. | |
772 | * | |
773 | * @param int $userid The id of the user who we are viewing conversations for | |
774 | * @param int $limitfrom | |
775 | * @param int $limitnum | |
49aaadc3 MN |
776 | * @return stdClass |
777 | * @throws moodle_exception | |
778 | * @since 3.2 | |
9aa012b5 MN |
779 | */ |
780 | public static function data_for_messagearea_contacts($userid, $limitfrom = 0, $limitnum = 0) { | |
781 | global $CFG, $PAGE; | |
782 | ||
783 | // Check if messaging is enabled. | |
784 | if (!$CFG->messaging) { | |
785 | throw new moodle_exception('disabled', 'message'); | |
786 | } | |
787 | ||
788 | $params = array( | |
789 | 'userid' => $userid, | |
790 | 'limitfrom' => $limitfrom, | |
791 | 'limitnum' => $limitnum | |
792 | ); | |
793 | self::validate_parameters(self::data_for_messagearea_contacts_parameters(), $params); | |
794 | ||
795 | self::validate_context(context_user::instance($userid)); | |
796 | ||
797 | $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum); | |
798 | ||
799 | $renderer = $PAGE->get_renderer('core_message'); | |
800 | return $contacts->export_for_template($renderer); | |
801 | } | |
802 | ||
803 | /** | |
49aaadc3 | 804 | * The messagearea contacts return structure. |
9aa012b5 | 805 | * |
49aaadc3 MN |
806 | * @return external_single_structure |
807 | * @since 3.2 | |
9aa012b5 MN |
808 | */ |
809 | public static function data_for_messagearea_contacts_returns() { | |
810 | return self::data_for_messagearea_conversations_returns(); | |
811 | } | |
812 | ||
3cd13828 | 813 | /** |
49aaadc3 | 814 | * The messagearea messages parameters. |
3cd13828 MN |
815 | * |
816 | * @return external_function_parameters | |
49aaadc3 | 817 | * @since 3.2 |
3cd13828 MN |
818 | */ |
819 | public static function data_for_messagearea_messages_parameters() { | |
820 | return new external_function_parameters( | |
821 | array( | |
822 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
823 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
824 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
8ec78c48 MN |
825 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0), |
826 | 'newest' => new external_value(PARAM_BOOL, 'Newest first?', VALUE_DEFAULT, false), | |
3cd13828 MN |
827 | ) |
828 | ); | |
829 | } | |
830 | ||
831 | /** | |
832 | * Get messagearea messages. | |
833 | * | |
834 | * @param int $currentuserid The current user's id | |
835 | * @param int $otheruserid The other user's id | |
836 | * @param int $limitfrom | |
837 | * @param int $limitnum | |
8ec78c48 | 838 | * @param boolean $newest |
49aaadc3 MN |
839 | * @return stdClass |
840 | * @throws moodle_exception | |
841 | * @since 3.2 | |
3cd13828 | 842 | */ |
8ec78c48 | 843 | public static function data_for_messagearea_messages($currentuserid, $otheruserid, $limitfrom = 0, $limitnum = 0, $newest = false) { |
3cd13828 MN |
844 | global $CFG, $PAGE; |
845 | ||
846 | // Check if messaging is enabled. | |
847 | if (!$CFG->messaging) { | |
848 | throw new moodle_exception('disabled', 'message'); | |
849 | } | |
850 | ||
851 | $params = array( | |
852 | 'currentuserid' => $currentuserid, | |
853 | 'otheruserid' => $otheruserid, | |
854 | 'limitfrom' => $limitfrom, | |
8ec78c48 MN |
855 | 'limitnum' => $limitnum, |
856 | 'newest' => $newest | |
3cd13828 MN |
857 | ); |
858 | self::validate_parameters(self::data_for_messagearea_messages_parameters(), $params); | |
859 | ||
860 | self::validate_context(context_user::instance($currentuserid)); | |
861 | ||
8ec78c48 MN |
862 | if ($newest) { |
863 | $sort = 'timecreated DESC'; | |
864 | } else { | |
865 | $sort = 'timecreated ASC'; | |
866 | } | |
867 | $messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom, $limitnum, $sort); | |
3cd13828 MN |
868 | |
869 | $renderer = $PAGE->get_renderer('core_message'); | |
870 | return $messages->export_for_template($renderer); | |
871 | } | |
872 | ||
873 | /** | |
49aaadc3 | 874 | * The messagearea messages return structure. |
3cd13828 | 875 | * |
49aaadc3 MN |
876 | * @return external_single_structure |
877 | * @since 3.2 | |
3cd13828 MN |
878 | */ |
879 | public static function data_for_messagearea_messages_returns() { | |
49aaadc3 | 880 | return new external_single_structure( |
3cd13828 MN |
881 | array( |
882 | 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing the messages on behalf of?'), | |
883 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
884 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
885 | 'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'), | |
bf58081d | 886 | 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), |
3cd13828 | 887 | 'messages' => new external_multiple_structure( |
94e1db61 | 888 | self::get_messagearea_message_structure() |
3cd13828 MN |
889 | ) |
890 | ) | |
891 | ); | |
892 | } | |
893 | ||
c060cd49 | 894 | /** |
49aaadc3 | 895 | * The get most recent message return parameters. |
c060cd49 MN |
896 | * |
897 | * @return external_function_parameters | |
49aaadc3 | 898 | * @since 3.2 |
c060cd49 MN |
899 | */ |
900 | public static function data_for_messagearea_get_most_recent_message_parameters() { | |
901 | return new external_function_parameters( | |
902 | array( | |
903 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
904 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
905 | ) | |
906 | ); | |
907 | } | |
908 | ||
909 | /** | |
910 | * Get the most recent message in a conversation. | |
911 | * | |
912 | * @param int $currentuserid The current user's id | |
913 | * @param int $otheruserid The other user's id | |
49aaadc3 MN |
914 | * @return stdClass |
915 | * @throws moodle_exception | |
916 | * @since 3.2 | |
c060cd49 MN |
917 | */ |
918 | public static function data_for_messagearea_get_most_recent_message($currentuserid, $otheruserid) { | |
919 | global $CFG, $PAGE; | |
920 | ||
921 | // Check if messaging is enabled. | |
922 | if (!$CFG->messaging) { | |
923 | throw new moodle_exception('disabled', 'message'); | |
924 | } | |
925 | ||
926 | $params = array( | |
927 | 'currentuserid' => $currentuserid, | |
928 | 'otheruserid' => $otheruserid | |
929 | ); | |
930 | self::validate_parameters(self::data_for_messagearea_get_most_recent_message_parameters(), $params); | |
931 | ||
932 | self::validate_context(context_user::instance($currentuserid)); | |
933 | ||
934 | $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid); | |
935 | ||
936 | $renderer = $PAGE->get_renderer('core_message'); | |
937 | return $message->export_for_template($renderer); | |
938 | } | |
939 | ||
940 | /** | |
49aaadc3 | 941 | * The get most recent message return structure. |
c060cd49 MN |
942 | * |
943 | * @return external_single_structure | |
49aaadc3 | 944 | * @since 3.2 |
c060cd49 MN |
945 | */ |
946 | public static function data_for_messagearea_get_most_recent_message_returns() { | |
94e1db61 | 947 | return self::get_messagearea_message_structure(); |
c060cd49 MN |
948 | } |
949 | ||
c6e97f54 MN |
950 | /** |
951 | * The get profile parameters. | |
952 | * | |
953 | * @return external_function_parameters | |
49aaadc3 | 954 | * @since 3.2 |
c6e97f54 MN |
955 | */ |
956 | public static function data_for_messagearea_get_profile_parameters() { | |
957 | return new external_function_parameters( | |
958 | array( | |
959 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
960 | 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we want to view'), | |
961 | ) | |
962 | ); | |
963 | } | |
964 | ||
965 | /** | |
966 | * Get the profile information for a contact. | |
967 | * | |
968 | * @param int $currentuserid The current user's id | |
969 | * @param int $otheruserid The id of the user whose profile we are viewing | |
49aaadc3 MN |
970 | * @return stdClass |
971 | * @throws moodle_exception | |
972 | * @since 3.2 | |
c6e97f54 MN |
973 | */ |
974 | public static function data_for_messagearea_get_profile($currentuserid, $otheruserid) { | |
975 | global $CFG, $PAGE; | |
976 | ||
977 | // Check if messaging is enabled. | |
978 | if (!$CFG->messaging) { | |
979 | throw new moodle_exception('disabled', 'message'); | |
980 | } | |
981 | ||
982 | $params = array( | |
983 | 'currentuserid' => $currentuserid, | |
984 | 'otheruserid' => $otheruserid | |
985 | ); | |
986 | self::validate_parameters(self::data_for_messagearea_get_profile_parameters(), $params); | |
987 | ||
988 | self::validate_context(context_user::instance($otheruserid)); | |
989 | ||
990 | $profile = \core_message\api::get_profile($currentuserid, $otheruserid); | |
991 | ||
992 | $renderer = $PAGE->get_renderer('core_message'); | |
993 | return $profile->export_for_template($renderer); | |
994 | } | |
995 | ||
996 | /** | |
49aaadc3 | 997 | * The get profile return structure. |
c6e97f54 MN |
998 | * |
999 | * @return external_single_structure | |
49aaadc3 | 1000 | * @since 3.2 |
c6e97f54 MN |
1001 | */ |
1002 | public static function data_for_messagearea_get_profile_returns() { | |
1003 | return new external_single_structure( | |
1004 | array( | |
1005 | 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing the profile on behalf of?'), | |
1006 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
1007 | 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we are viewing'), | |
1008 | 'email' => new external_value(core_user::get_property_type('email'), 'An email address'), | |
1009 | 'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user'), | |
1010 | 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user'), | |
1011 | 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'), | |
1012 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'), | |
1013 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'), | |
bf58081d | 1014 | 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'), |
c6e97f54 MN |
1015 | 'isblocked' => new external_value(PARAM_BOOL, 'Is the user blocked?'), |
1016 | 'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?') | |
1017 | ) | |
1018 | ); | |
1019 | } | |
1020 | ||
d6731600 FM |
1021 | /** |
1022 | * Get contacts parameters description. | |
1023 | * | |
1024 | * @return external_function_parameters | |
5bcfd504 | 1025 | * @since Moodle 2.5 |
d6731600 FM |
1026 | */ |
1027 | public static function get_contacts_parameters() { | |
1028 | return new external_function_parameters(array()); | |
1029 | } | |
1030 | ||
1031 | /** | |
1032 | * Get contacts. | |
1033 | * | |
1034 | * @param array $userids array of user IDs. | |
1035 | * @return external_description | |
5bcfd504 | 1036 | * @since Moodle 2.5 |
d6731600 FM |
1037 | */ |
1038 | public static function get_contacts() { | |
d85bedf7 | 1039 | global $CFG, $PAGE; |
436bbf89 DM |
1040 | |
1041 | // Check if messaging is enabled. | |
1042 | if (!$CFG->messaging) { | |
1043 | throw new moodle_exception('disabled', 'message'); | |
1044 | } | |
1045 | ||
d6731600 FM |
1046 | require_once($CFG->dirroot . '/user/lib.php'); |
1047 | ||
1048 | list($online, $offline, $strangers) = message_get_contacts(); | |
1049 | $allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers); | |
1050 | foreach ($allcontacts as $mode => $contacts) { | |
1051 | foreach ($contacts as $key => $contact) { | |
1052 | $newcontact = array( | |
1053 | 'id' => $contact->id, | |
1054 | 'fullname' => fullname($contact), | |
1055 | 'unread' => $contact->messagecount | |
1056 | ); | |
1057 | ||
d85bedf7 JL |
1058 | $userpicture = new user_picture($contact); |
1059 | $userpicture->size = 1; // Size f1. | |
1060 | $newcontact['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
1061 | $userpicture->size = 0; // Size f2. | |
1062 | $newcontact['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
1063 | |
1064 | $allcontacts[$mode][$key] = $newcontact; | |
1065 | } | |
1066 | } | |
1067 | return $allcontacts; | |
1068 | } | |
1069 | ||
1070 | /** | |
1071 | * Get contacts return description. | |
1072 | * | |
1073 | * @return external_description | |
5bcfd504 | 1074 | * @since Moodle 2.5 |
d6731600 FM |
1075 | */ |
1076 | public static function get_contacts_returns() { | |
1077 | return new external_single_structure( | |
1078 | array( | |
1079 | 'online' => new external_multiple_structure( | |
1080 | new external_single_structure( | |
1081 | array( | |
1082 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1083 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1084 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
1085 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
1086 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
1087 | ) | |
1088 | ), | |
1089 | 'List of online contacts' | |
1090 | ), | |
1091 | 'offline' => new external_multiple_structure( | |
1092 | new external_single_structure( | |
1093 | array( | |
1094 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1095 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1096 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
1097 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
1098 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
1099 | ) | |
1100 | ), | |
1101 | 'List of offline contacts' | |
1102 | ), | |
1103 | 'strangers' => new external_multiple_structure( | |
1104 | new external_single_structure( | |
1105 | array( | |
1106 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1107 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1108 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
1109 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
1110 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
1111 | ) | |
1112 | ), | |
1113 | 'List of users that are not in the user\'s contact list but have sent a message' | |
1114 | ) | |
1115 | ) | |
1116 | ); | |
1117 | } | |
1118 | ||
1119 | /** | |
1120 | * Search contacts parameters description. | |
1121 | * | |
1122 | * @return external_function_parameters | |
5bcfd504 | 1123 | * @since Moodle 2.5 |
d6731600 FM |
1124 | */ |
1125 | public static function search_contacts_parameters() { | |
1126 | return new external_function_parameters( | |
1127 | array( | |
1128 | 'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'), | |
1129 | 'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses', | |
1130 | VALUE_DEFAULT, false) | |
1131 | ) | |
1132 | ); | |
1133 | } | |
1134 | ||
1135 | /** | |
1136 | * Search contacts. | |
1137 | * | |
1138 | * @param string $searchtext query string. | |
1139 | * @param bool $onlymycourses limit the search to the user's courses only. | |
1140 | * @return external_description | |
5bcfd504 | 1141 | * @since Moodle 2.5 |
d6731600 FM |
1142 | */ |
1143 | public static function search_contacts($searchtext, $onlymycourses = false) { | |
d85bedf7 | 1144 | global $CFG, $USER, $PAGE; |
11d83ab3 | 1145 | require_once($CFG->dirroot . '/user/lib.php'); |
436bbf89 DM |
1146 | |
1147 | // Check if messaging is enabled. | |
1148 | if (!$CFG->messaging) { | |
1149 | throw new moodle_exception('disabled', 'message'); | |
1150 | } | |
1151 | ||
d6731600 FM |
1152 | require_once($CFG->libdir . '/enrollib.php'); |
1153 | ||
1154 | $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses); | |
1155 | $params = self::validate_parameters(self::search_contacts_parameters(), $params); | |
1156 | ||
1157 | // Extra validation, we do not allow empty queries. | |
1158 | if ($params['searchtext'] === '') { | |
1159 | throw new moodle_exception('querystringcannotbeempty'); | |
1160 | } | |
1161 | ||
1162 | $courseids = array(); | |
1163 | if ($params['onlymycourses']) { | |
1164 | $mycourses = enrol_get_my_courses(array('id')); | |
1165 | foreach ($mycourses as $mycourse) { | |
1166 | $courseids[] = $mycourse->id; | |
1167 | } | |
1168 | } else { | |
1169 | $courseids[] = SITEID; | |
1170 | } | |
1171 | ||
1172 | // Retrieving the users matching the query. | |
1173 | $users = message_search_users($courseids, $params['searchtext']); | |
1174 | $results = array(); | |
1175 | foreach ($users as $user) { | |
1176 | $results[$user->id] = $user; | |
1177 | } | |
1178 | ||
1179 | // Reorganising information. | |
1180 | foreach ($results as &$user) { | |
1181 | $newuser = array( | |
1182 | 'id' => $user->id, | |
1183 | 'fullname' => fullname($user) | |
1184 | ); | |
1185 | ||
1186 | // Avoid undefined property notice as phone not specified. | |
1187 | $user->phone1 = null; | |
1188 | $user->phone2 = null; | |
1189 | ||
d85bedf7 JL |
1190 | $userpicture = new user_picture($user); |
1191 | $userpicture->size = 1; // Size f1. | |
1192 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
1193 | $userpicture->size = 0; // Size f2. | |
1194 | $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
1195 | |
1196 | $user = $newuser; | |
1197 | } | |
1198 | ||
1199 | return $results; | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * Search contacts return description. | |
1204 | * | |
1205 | * @return external_description | |
5bcfd504 | 1206 | * @since Moodle 2.5 |
d6731600 FM |
1207 | */ |
1208 | public static function search_contacts_returns() { | |
1209 | return new external_multiple_structure( | |
1210 | new external_single_structure( | |
1211 | array( | |
1212 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1213 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1214 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
1215 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL) | |
1216 | ) | |
1217 | ), | |
1218 | 'List of contacts' | |
1219 | ); | |
1220 | } | |
aff9da17 JL |
1221 | |
1222 | /** | |
1223 | * Get messages parameters description. | |
1224 | * | |
1225 | * @return external_function_parameters | |
193edf7f | 1226 | * @since 2.8 |
aff9da17 JL |
1227 | */ |
1228 | public static function get_messages_parameters() { | |
1229 | return new external_function_parameters( | |
1230 | array( | |
6ff4464b | 1231 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), |
127ef540 SH |
1232 | 'useridfrom' => new external_value( |
1233 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
1234 | VALUE_DEFAULT, 0), | |
1235 | 'type' => new external_value( | |
1236 | PARAM_ALPHA, 'type of message to return, expected values are: notifications, conversations and both', | |
1237 | VALUE_DEFAULT, 'both'), | |
6ff4464b | 1238 | 'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true), |
127ef540 SH |
1239 | 'newestfirst' => new external_value( |
1240 | PARAM_BOOL, 'true for ordering by newest first, false for oldest first', | |
1241 | VALUE_DEFAULT, true), | |
aff9da17 | 1242 | 'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0), |
127ef540 SH |
1243 | 'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0) |
1244 | ) | |
aff9da17 JL |
1245 | ); |
1246 | } | |
1247 | ||
1248 | /** | |
1249 | * Get messages function implementation. | |
127ef540 SH |
1250 | * |
1251 | * @since 2.8 | |
1252 | * @throws invalid_parameter_exception | |
1253 | * @throws moodle_exception | |
6ff4464b JL |
1254 | * @param int $useridto the user id who received the message |
1255 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
193edf7f | 1256 | * @param string $type type of message to return, expected values: notifications, conversations and both |
aff9da17 | 1257 | * @param bool $read true for retreiving read messages, false for unread |
6ff4464b | 1258 | * @param bool $newestfirst true for ordering by newest first, false for oldest first |
aff9da17 JL |
1259 | * @param int $limitfrom limit from |
1260 | * @param int $limitnum limit num | |
1261 | * @return external_description | |
aff9da17 | 1262 | */ |
193edf7f | 1263 | public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, |
aff9da17 | 1264 | $newestfirst = true, $limitfrom = 0, $limitnum = 0) { |
127ef540 | 1265 | global $CFG, $USER; |
aff9da17 JL |
1266 | |
1267 | $warnings = array(); | |
1268 | ||
1269 | $params = array( | |
1270 | 'useridto' => $useridto, | |
6ff4464b | 1271 | 'useridfrom' => $useridfrom, |
aff9da17 JL |
1272 | 'type' => $type, |
1273 | 'read' => $read, | |
aff9da17 JL |
1274 | 'newestfirst' => $newestfirst, |
1275 | 'limitfrom' => $limitfrom, | |
1276 | 'limitnum' => $limitnum | |
1277 | ); | |
1278 | ||
1279 | $params = self::validate_parameters(self::get_messages_parameters(), $params); | |
1280 | ||
1281 | $context = context_system::instance(); | |
1282 | self::validate_context($context); | |
1283 | ||
1284 | $useridto = $params['useridto']; | |
6ff4464b | 1285 | $useridfrom = $params['useridfrom']; |
aff9da17 JL |
1286 | $type = $params['type']; |
1287 | $read = $params['read']; | |
aff9da17 JL |
1288 | $newestfirst = $params['newestfirst']; |
1289 | $limitfrom = $params['limitfrom']; | |
1290 | $limitnum = $params['limitnum']; | |
1291 | ||
1292 | $allowedvalues = array('notifications', 'conversations', 'both'); | |
1293 | if (!in_array($type, $allowedvalues)) { | |
1294 | throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . | |
1295 | 'allowed values are: ' . implode(',', $allowedvalues)); | |
1296 | } | |
1297 | ||
1298 | // Check if private messaging between users is allowed. | |
1299 | if (empty($CFG->messaging)) { | |
1300 | // If we are retreiving only conversations, and messaging is disabled, throw an exception. | |
aff9da17 JL |
1301 | if ($type == "conversations") { |
1302 | throw new moodle_exception('disabled', 'message'); | |
1303 | } | |
1304 | if ($type == "both") { | |
1305 | $warning = array(); | |
1306 | $warning['item'] = 'message'; | |
1307 | $warning['itemid'] = $USER->id; | |
1308 | $warning['warningcode'] = '1'; | |
1309 | $warning['message'] = 'Private messages (conversations) are not enabled in this site. | |
1310 | Only notifications will be returned'; | |
1311 | $warnings[] = $warning; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | if (!empty($useridto)) { | |
6ff4464b JL |
1316 | if (core_user::is_real_user($useridto)) { |
1317 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1318 | } else { | |
1319 | throw new moodle_exception('invaliduser'); | |
1320 | } | |
aff9da17 JL |
1321 | } |
1322 | ||
1323 | if (!empty($useridfrom)) { | |
1324 | // We use get_user here because the from user can be the noreply or support user. | |
1325 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
1326 | } | |
1327 | ||
1328 | // Check if the current user is the sender/receiver or just a privileged user. | |
1329 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
1330 | !has_capability('moodle/site:readallmessages', $context)) { | |
1331 | throw new moodle_exception('accessdenied', 'admin'); | |
1332 | } | |
1333 | ||
127ef540 | 1334 | // Which type of messages to retrieve. |
193edf7f | 1335 | $notifications = -1; |
aff9da17 | 1336 | if ($type != 'both') { |
193edf7f | 1337 | $notifications = ($type == 'notifications') ? 1 : 0; |
aff9da17 JL |
1338 | } |
1339 | ||
aff9da17 | 1340 | $orderdirection = $newestfirst ? 'DESC' : 'ASC'; |
193edf7f | 1341 | $sort = "mr.timecreated $orderdirection"; |
aff9da17 | 1342 | |
193edf7f | 1343 | if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) { |
aff9da17 JL |
1344 | $canviewfullname = has_capability('moodle/site:viewfullnames', $context); |
1345 | ||
1346 | // In some cases, we don't need to get the to/from user objects from the sql query. | |
1347 | $userfromfullname = ''; | |
1348 | $usertofullname = ''; | |
1349 | ||
1350 | // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there. | |
1351 | if (!empty($useridto)) { | |
1352 | $usertofullname = fullname($userto, $canviewfullname); | |
1353 | // The user from may or may not be filled. | |
1354 | if (!empty($useridfrom)) { | |
1355 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1356 | } | |
1357 | } else { | |
1358 | // If the useridto field is empty, the useridfrom must be filled. | |
1359 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1360 | } | |
aff9da17 JL |
1361 | foreach ($messages as $mid => $message) { |
1362 | ||
ea21d637 JL |
1363 | // Do not return deleted messages. |
1364 | if (($useridto == $USER->id and $message->timeusertodeleted) or | |
1365 | ($useridfrom == $USER->id and $message->timeuserfromdeleted)) { | |
1366 | ||
1367 | unset($messages[$mid]); | |
1368 | continue; | |
1369 | } | |
1370 | ||
aff9da17 JL |
1371 | // We need to get the user from the query. |
1372 | if (empty($userfromfullname)) { | |
6ff4464b JL |
1373 | // Check for non-reply and support users. |
1374 | if (core_user::is_real_user($message->useridfrom)) { | |
127ef540 | 1375 | $user = new stdClass(); |
6ff4464b JL |
1376 | $user = username_load_fields_from_object($user, $message, 'userfrom'); |
1377 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1378 | } else { | |
1379 | $user = core_user::get_user($message->useridfrom); | |
1380 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1381 | } | |
aff9da17 JL |
1382 | } else { |
1383 | $message->userfromfullname = $userfromfullname; | |
1384 | } | |
1385 | ||
1386 | // We need to get the user from the query. | |
1387 | if (empty($usertofullname)) { | |
127ef540 | 1388 | $user = new stdClass(); |
aff9da17 JL |
1389 | $user = username_load_fields_from_object($user, $message, 'userto'); |
1390 | $message->usertofullname = fullname($user, $canviewfullname); | |
1391 | } else { | |
1392 | $message->usertofullname = $usertofullname; | |
1393 | } | |
1394 | ||
193edf7f | 1395 | // This field is only available in the message_read table. |
aff9da17 JL |
1396 | if (!isset($message->timeread)) { |
1397 | $message->timeread = 0; | |
1398 | } | |
1399 | ||
aff9da17 | 1400 | $message->text = message_format_message_text($message); |
aff9da17 JL |
1401 | $messages[$mid] = (array) $message; |
1402 | } | |
1403 | } | |
1404 | ||
1405 | $results = array( | |
1406 | 'messages' => $messages, | |
1407 | 'warnings' => $warnings | |
1408 | ); | |
1409 | ||
1410 | return $results; | |
1411 | } | |
1412 | ||
1413 | /** | |
1414 | * Get messages return description. | |
1415 | * | |
6ff4464b | 1416 | * @return external_single_structure |
193edf7f | 1417 | * @since 2.8 |
aff9da17 JL |
1418 | */ |
1419 | public static function get_messages_returns() { | |
1420 | return new external_single_structure( | |
1421 | array( | |
1422 | 'messages' => new external_multiple_structure( | |
1423 | new external_single_structure( | |
1424 | array( | |
193edf7f | 1425 | 'id' => new external_value(PARAM_INT, 'Message id'), |
aff9da17 JL |
1426 | 'useridfrom' => new external_value(PARAM_INT, 'User from id'), |
1427 | 'useridto' => new external_value(PARAM_INT, 'User to id'), | |
1428 | 'subject' => new external_value(PARAM_TEXT, 'The message subject'), | |
1429 | 'text' => new external_value(PARAM_RAW, 'The message text formated'), | |
1430 | 'fullmessage' => new external_value(PARAM_RAW, 'The message'), | |
193edf7f | 1431 | 'fullmessageformat' => new external_format_value('fullmessage'), |
aff9da17 JL |
1432 | 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'), |
1433 | 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'), | |
1434 | 'notification' => new external_value(PARAM_INT, 'Is a notification?'), | |
1435 | 'contexturl' => new external_value(PARAM_RAW, 'Context URL'), | |
1436 | 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'), | |
1437 | 'timecreated' => new external_value(PARAM_INT, 'Time created'), | |
1438 | 'timeread' => new external_value(PARAM_INT, 'Time read'), | |
1439 | 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'), | |
1440 | 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name') | |
1441 | ), 'message' | |
1442 | ) | |
1443 | ), | |
1444 | 'warnings' => new external_warnings() | |
1445 | ) | |
1446 | ); | |
3274d5ca RW |
1447 | } |
1448 | ||
1449 | /** | |
ada7695d | 1450 | * Get popup notifications parameters description. |
3274d5ca RW |
1451 | * |
1452 | * @return external_function_parameters | |
1453 | * @since 3.2 | |
1454 | */ | |
ada7695d | 1455 | public static function get_popup_notifications_parameters() { |
3274d5ca RW |
1456 | return new external_function_parameters( |
1457 | array( | |
1458 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), | |
3274d5ca RW |
1459 | 'status' => new external_value( |
1460 | PARAM_ALPHA, 'filter the results to just "read" or "unread" notifications', | |
1461 | VALUE_DEFAULT, ''), | |
1462 | 'embeduserto' => new external_value( | |
1463 | PARAM_BOOL, 'true for returning user details for the recipient in each notification', | |
1464 | VALUE_DEFAULT, false), | |
1465 | 'embeduserfrom' => new external_value( | |
1466 | PARAM_BOOL, 'true for returning user details for the sender in each notification', | |
1467 | VALUE_DEFAULT, false), | |
1468 | 'newestfirst' => new external_value( | |
1469 | PARAM_BOOL, 'true for ordering by newest first, false for oldest first', | |
1470 | VALUE_DEFAULT, true), | |
1471 | 'markasread' => new external_value( | |
1472 | PARAM_BOOL, 'mark notifications as read when they are returned by this function', | |
1473 | VALUE_DEFAULT, false), | |
1474 | 'limit' => new external_value(PARAM_INT, 'the number of results to return', VALUE_DEFAULT, 0), | |
1475 | 'offset' => new external_value(PARAM_INT, 'offset the result set by a given amount', VALUE_DEFAULT, 0) | |
1476 | ) | |
1477 | ); | |
1478 | } | |
1479 | ||
1480 | /** | |
1481 | * Get notifications function. | |
1482 | * | |
1483 | * @since 3.2 | |
1484 | * @throws invalid_parameter_exception | |
1485 | * @throws moodle_exception | |
c5dd16a1 | 1486 | * @param int $useridto the user id who received the message |
c5dd16a1 | 1487 | * @param string $status filter the results to only read or unread notifications |
c5dd16a1 RW |
1488 | * @param bool $embeduserto true to embed the recipient user details in the record for each notification |
1489 | * @param bool $embeduserfrom true to embed the send user details in the record for each notification | |
1490 | * @param bool $newestfirst true for ordering by newest first, false for oldest first | |
1491 | * @param bool $markasread mark notifications as read when they are returned by this function | |
1492 | * @param int $limit the number of results to return | |
1493 | * @param int $offset offset the result set by a given amount | |
3274d5ca RW |
1494 | * @return external_description |
1495 | */ | |
0b19d048 RW |
1496 | public static function get_popup_notifications($useridto, $status, $embeduserto, |
1497 | $embeduserfrom, $newestfirst, $markasread, $limit, $offset) { | |
24a76780 | 1498 | global $CFG, $USER, $PAGE; |
3274d5ca RW |
1499 | |
1500 | $params = self::validate_parameters( | |
ada7695d | 1501 | self::get_popup_notifications_parameters(), |
3274d5ca RW |
1502 | array( |
1503 | 'useridto' => $useridto, | |
3274d5ca RW |
1504 | 'status' => $status, |
1505 | 'embeduserto' => $embeduserto, | |
1506 | 'embeduserfrom' => $embeduserfrom, | |
1507 | 'newestfirst' => $newestfirst, | |
1508 | 'markasread' => $markasread, | |
1509 | 'limit' => $limit, | |
1510 | 'offset' => $offset, | |
1511 | ) | |
1512 | ); | |
1513 | ||
1514 | $context = context_system::instance(); | |
1515 | self::validate_context($context); | |
1516 | ||
1517 | $useridto = $params['useridto']; | |
3274d5ca RW |
1518 | $status = $params['status']; |
1519 | $embeduserto = $params['embeduserto']; | |
1520 | $embeduserfrom = $params['embeduserfrom']; | |
1521 | $newestfirst = $params['newestfirst']; | |
1522 | $markasread = $params['markasread']; | |
1523 | $limit = $params['limit']; | |
1524 | $offset = $params['offset']; | |
c5dd16a1 | 1525 | $issuperuser = has_capability('moodle/site:readallmessages', $context); |
24a76780 | 1526 | $renderer = $PAGE->get_renderer('core_message'); |
3274d5ca RW |
1527 | |
1528 | if (!empty($useridto)) { | |
1529 | if (core_user::is_real_user($useridto)) { | |
1530 | if ($embeduserto) { | |
1531 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1532 | } | |
1533 | } else { | |
1534 | throw new moodle_exception('invaliduser'); | |
1535 | } | |
1536 | } | |
1537 | ||
3274d5ca | 1538 | // Check if the current user is the sender/receiver or just a privileged user. |
ada7695d | 1539 | if ($useridto != $USER->id and !$issuperuser) { |
3274d5ca RW |
1540 | throw new moodle_exception('accessdenied', 'admin'); |
1541 | } | |
1542 | ||
1543 | $sort = $newestfirst ? 'DESC' : 'ASC'; | |
ada7695d | 1544 | $notifications = message_get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset); |
24a76780 | 1545 | $notificationcontexts = []; |
3274d5ca RW |
1546 | |
1547 | if ($notifications) { | |
ada7695d | 1548 | // In some cases, we don't need to get the to user objects from the sql query. |
3274d5ca RW |
1549 | $usertofullname = ''; |
1550 | ||
1551 | // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there. | |
1552 | if (!empty($useridto) && $embeduserto) { | |
1553 | $usertofullname = fullname($userto); | |
3274d5ca RW |
1554 | } |
1555 | ||
1556 | foreach ($notifications as $notification) { | |
1557 | ||
24a76780 | 1558 | $notificationoutput = new \core_message\output\popup_notification($notification, $embeduserto, |
0b19d048 | 1559 | $embeduserfrom, $usertofullname); |
3274d5ca | 1560 | |
24a76780 RW |
1561 | $notificationcontext = $notificationoutput->export_for_template($renderer); |
1562 | $notificationcontexts[] = $notificationcontext; | |
c5dd16a1 | 1563 | |
24a76780 RW |
1564 | if ($markasread && !$notificationcontext->read) { |
1565 | message_mark_message_read($notification, time()); | |
3274d5ca RW |
1566 | } |
1567 | } | |
1568 | } | |
1569 | ||
1570 | return array( | |
24a76780 | 1571 | 'notifications' => $notificationcontexts, |
ada7695d | 1572 | 'unreadcount' => message_count_unread_popup_notifications($useridto), |
3274d5ca RW |
1573 | ); |
1574 | } | |
1575 | ||
1576 | /** | |
1577 | * Get notifications return description. | |
1578 | * | |
1579 | * @return external_single_structure | |
1580 | * @since 3.2 | |
1581 | */ | |
ada7695d | 1582 | public static function get_popup_notifications_returns() { |
3274d5ca RW |
1583 | return new external_single_structure( |
1584 | array( | |
1585 | 'notifications' => new external_multiple_structure( | |
1586 | new external_single_structure( | |
1587 | array( | |
1588 | 'id' => new external_value(PARAM_INT, 'Notification id (this is not guaranteed to be unique within this result set)'), | |
1589 | 'useridfrom' => new external_value(PARAM_INT, 'User from id'), | |
1590 | 'useridto' => new external_value(PARAM_INT, 'User to id'), | |
1591 | 'subject' => new external_value(PARAM_TEXT, 'The notification subject'), | |
0b19d048 | 1592 | 'shortenedsubject' => new external_value(PARAM_TEXT, 'The notification subject shortened with ellipsis'), |
3274d5ca RW |
1593 | 'text' => new external_value(PARAM_RAW, 'The message text formated'), |
1594 | 'fullmessage' => new external_value(PARAM_RAW, 'The message'), | |
1595 | 'fullmessageformat' => new external_format_value('fullmessage'), | |
1596 | 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'), | |
1597 | 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'), | |
1598 | 'contexturl' => new external_value(PARAM_RAW, 'Context URL'), | |
1599 | 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'), | |
1600 | 'timecreated' => new external_value(PARAM_INT, 'Time created'), | |
1601 | 'timecreatedpretty' => new external_value(PARAM_TEXT, 'Time created in a pretty format'), | |
1602 | 'timeread' => new external_value(PARAM_INT, 'Time read'), | |
1603 | 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name', VALUE_OPTIONAL), | |
1604 | 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name', VALUE_OPTIONAL), | |
1605 | 'userfromprofileurl' => new external_value(PARAM_URL, 'User from profile url', VALUE_OPTIONAL), | |
1606 | 'read' => new external_value(PARAM_BOOL, 'notification read status'), | |
1607 | 'deleted' => new external_value(PARAM_BOOL, 'notification deletion status'), | |
1608 | 'iconurl' => new external_value(PARAM_URL, 'URL for notification icon'), | |
1609 | 'component' => new external_value(PARAM_TEXT, 'The component that generated the notification', VALUE_OPTIONAL), | |
1610 | 'eventtype' => new external_value(PARAM_TEXT, 'The type of notification', VALUE_OPTIONAL), | |
1611 | ), 'message' | |
1612 | ) | |
1613 | ), | |
1614 | 'unreadcount' => new external_value(PARAM_INT, 'the user whose blocked users we want to retrieve'), | |
1615 | ) | |
1616 | ); | |
1617 | } | |
1618 | ||
1619 | /** | |
1620 | * Mark all notifications as read parameters description. | |
1621 | * | |
1622 | * @return external_function_parameters | |
1623 | * @since 3.2 | |
1624 | */ | |
1625 | public static function mark_all_notifications_as_read_parameters() { | |
1626 | return new external_function_parameters( | |
1627 | array( | |
1628 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), | |
1629 | 'useridfrom' => new external_value( | |
1630 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
1631 | VALUE_DEFAULT, 0), | |
1632 | ) | |
1633 | ); | |
1634 | } | |
1635 | ||
1636 | /** | |
1637 | * Mark all notifications as read function. | |
1638 | * | |
1639 | * @since 3.2 | |
1640 | * @throws invalid_parameter_exception | |
1641 | * @throws moodle_exception | |
1642 | * @param int $useridto the user id who received the message | |
1643 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
1644 | * @return external_description | |
1645 | */ | |
1646 | public static function mark_all_notifications_as_read($useridto, $useridfrom) { | |
1647 | global $CFG, $USER; | |
1648 | ||
1649 | $params = self::validate_parameters( | |
1650 | self::mark_all_notifications_as_read_parameters(), | |
1651 | array( | |
1652 | 'useridto' => $useridto, | |
1653 | 'useridfrom' => $useridfrom, | |
1654 | ) | |
1655 | ); | |
1656 | ||
1657 | $context = context_system::instance(); | |
1658 | self::validate_context($context); | |
1659 | ||
1660 | $useridto = $params['useridto']; | |
1661 | $useridfrom = $params['useridfrom']; | |
1662 | ||
1663 | if (!empty($useridto)) { | |
1664 | if (core_user::is_real_user($useridto)) { | |
1665 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1666 | } else { | |
1667 | throw new moodle_exception('invaliduser'); | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | if (!empty($useridfrom)) { | |
1672 | // We use get_user here because the from user can be the noreply or support user. | |
1673 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
1674 | } | |
1675 | ||
1676 | // Check if the current user is the sender/receiver or just a privileged user. | |
1677 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
1678 | // deleteanymessage seems more reasonable here than readallmessages. | |
1679 | !has_capability('moodle/site:deleteanymessage', $context)) { | |
1680 | throw new moodle_exception('accessdenied', 'admin'); | |
1681 | } | |
1682 | ||
8c55bd6c | 1683 | message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION); |
3274d5ca RW |
1684 | |
1685 | return true; | |
1686 | } | |
1687 | ||
1688 | /** | |
1689 | * Mark all notifications as read return description. | |
1690 | * | |
1691 | * @return external_single_structure | |
1692 | * @since 3.2 | |
1693 | */ | |
1694 | public static function mark_all_notifications_as_read_returns() { | |
1695 | return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise'); | |
1696 | } | |
1697 | ||
1698 | /** | |
1699 | * Get unread notification count parameters description. | |
1700 | * | |
1701 | * @return external_function_parameters | |
1702 | * @since 3.2 | |
1703 | */ | |
ada7695d | 1704 | public static function get_unread_popup_notification_count_parameters() { |
3274d5ca RW |
1705 | return new external_function_parameters( |
1706 | array( | |
1707 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), | |
3274d5ca RW |
1708 | ) |
1709 | ); | |
1710 | } | |
1711 | ||
1712 | /** | |
1713 | * Get unread notification count function. | |
1714 | * | |
1715 | * @since 3.2 | |
1716 | * @throws invalid_parameter_exception | |
1717 | * @throws moodle_exception | |
1718 | * @param int $useridto the user id who received the message | |
3274d5ca RW |
1719 | * @return external_description |
1720 | */ | |
ada7695d | 1721 | public static function get_unread_popup_notification_count($useridto) { |
3274d5ca RW |
1722 | global $CFG, $USER; |
1723 | ||
1724 | $params = self::validate_parameters( | |
ada7695d RW |
1725 | self::get_unread_popup_notification_count_parameters(), |
1726 | array('useridto' => $useridto) | |
3274d5ca RW |
1727 | ); |
1728 | ||
1729 | $context = context_system::instance(); | |
1730 | self::validate_context($context); | |
1731 | ||
1732 | $useridto = $params['useridto']; | |
3274d5ca RW |
1733 | |
1734 | if (!empty($useridto)) { | |
1735 | if (core_user::is_real_user($useridto)) { | |
1736 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1737 | } else { | |
1738 | throw new moodle_exception('invaliduser'); | |
1739 | } | |
1740 | } | |
1741 | ||
3274d5ca | 1742 | // Check if the current user is the sender/receiver or just a privileged user. |
ada7695d | 1743 | if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { |
3274d5ca RW |
1744 | throw new moodle_exception('accessdenied', 'admin'); |
1745 | } | |
1746 | ||
ada7695d | 1747 | return message_count_unread_popup_notifications($useridto); |
3274d5ca RW |
1748 | } |
1749 | ||
1750 | /** | |
ada7695d | 1751 | * Get unread popup notification count return description. |
3274d5ca RW |
1752 | * |
1753 | * @return external_single_structure | |
1754 | * @since 3.2 | |
1755 | */ | |
ada7695d | 1756 | public static function get_unread_popup_notification_count_returns() { |
8c55bd6c RW |
1757 | return new external_value(PARAM_INT, 'The count of unread popup notifications'); |
1758 | } | |
1759 | ||
1760 | /** | |
1761 | * Get unread conversations count parameters description. | |
1762 | * | |
1763 | * @return external_function_parameters | |
1764 | * @since 3.2 | |
1765 | */ | |
1766 | public static function get_unread_conversations_count_parameters() { | |
1767 | return new external_function_parameters( | |
1768 | array( | |
1769 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), | |
1770 | ) | |
1771 | ); | |
1772 | } | |
1773 | ||
1774 | /** | |
1775 | * Get unread messages count function. | |
1776 | * | |
1777 | * @since 3.2 | |
1778 | * @throws invalid_parameter_exception | |
1779 | * @throws moodle_exception | |
1780 | * @param int $useridto the user id who received the message | |
1781 | * @return external_description | |
1782 | */ | |
1783 | public static function get_unread_conversations_count($useridto) { | |
1784 | global $USER; | |
1785 | ||
1786 | $params = self::validate_parameters( | |
1787 | self::get_unread_conversations_count_parameters(), | |
1788 | array('useridto' => $useridto) | |
1789 | ); | |
1790 | ||
1791 | $context = context_system::instance(); | |
1792 | self::validate_context($context); | |
1793 | ||
1794 | $useridto = $params['useridto']; | |
1795 | ||
1796 | if (!empty($useridto)) { | |
1797 | if (core_user::is_real_user($useridto)) { | |
1798 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1799 | } else { | |
1800 | throw new moodle_exception('invaliduser'); | |
1801 | } | |
1802 | } else { | |
1803 | $useridto = $USER->id; | |
1804 | } | |
1805 | ||
1806 | // Check if the current user is the receiver or just a privileged user. | |
1807 | if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { | |
1808 | throw new moodle_exception('accessdenied', 'admin'); | |
1809 | } | |
1810 | ||
1811 | return message_count_unread_conversations($userto); | |
1812 | } | |
1813 | ||
1814 | /** | |
1815 | * Get unread conversations count return description. | |
1816 | * | |
1817 | * @return external_single_structure | |
1818 | * @since 3.2 | |
1819 | */ | |
1820 | public static function get_unread_conversations_count_returns() { | |
1821 | return new external_value(PARAM_INT, 'The count of unread messages for the user'); | |
aff9da17 JL |
1822 | } |
1823 | ||
60ab2e1b JL |
1824 | /** |
1825 | * Get blocked users parameters description. | |
1826 | * | |
1827 | * @return external_function_parameters | |
1828 | * @since 2.9 | |
1829 | */ | |
1830 | public static function get_blocked_users_parameters() { | |
1831 | return new external_function_parameters( | |
1832 | array( | |
1833 | 'userid' => new external_value(PARAM_INT, | |
1834 | 'the user whose blocked users we want to retrieve', | |
1835 | VALUE_REQUIRED), | |
1836 | ) | |
1837 | ); | |
1838 | } | |
1839 | ||
1840 | /** | |
1841 | * Retrieve a list of users blocked | |
1842 | * | |
1843 | * @param int $userid the user whose blocked users we want to retrieve | |
1844 | * @return external_description | |
1845 | * @since 2.9 | |
1846 | */ | |
1847 | public static function get_blocked_users($userid) { | |
d85bedf7 | 1848 | global $CFG, $USER, $PAGE; |
60ab2e1b JL |
1849 | |
1850 | // Warnings array, it can be empty at the end but is mandatory. | |
1851 | $warnings = array(); | |
1852 | ||
1853 | // Validate params. | |
1854 | $params = array( | |
1855 | 'userid' => $userid | |
1856 | ); | |
1857 | $params = self::validate_parameters(self::get_blocked_users_parameters(), $params); | |
1858 | $userid = $params['userid']; | |
1859 | ||
1860 | // Validate context. | |
1861 | $context = context_system::instance(); | |
1862 | self::validate_context($context); | |
1863 | ||
1864 | // Check if private messaging between users is allowed. | |
1865 | if (empty($CFG->messaging)) { | |
1866 | throw new moodle_exception('disabled', 'message'); | |
1867 | } | |
1868 | ||
4485f7c5 JL |
1869 | $user = core_user::get_user($userid, '*', MUST_EXIST); |
1870 | core_user::require_active_user($user); | |
60ab2e1b JL |
1871 | |
1872 | // Check if we have permissions for retrieve the information. | |
1873 | if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { | |
1874 | throw new moodle_exception('accessdenied', 'admin'); | |
1875 | } | |
1876 | ||
1877 | // Now, we can get safely all the blocked users. | |
1878 | $users = message_get_blocked_users($user); | |
1879 | ||
1880 | $blockedusers = array(); | |
1881 | foreach ($users as $user) { | |
1882 | $newuser = array( | |
1883 | 'id' => $user->id, | |
1884 | 'fullname' => fullname($user), | |
1885 | ); | |
0b074e88 | 1886 | |
d85bedf7 JL |
1887 | $userpicture = new user_picture($user); |
1888 | $userpicture->size = 1; // Size f1. | |
1889 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
60ab2e1b JL |
1890 | |
1891 | $blockedusers[] = $newuser; | |
1892 | } | |
1893 | ||
1894 | $results = array( | |
1895 | 'users' => $blockedusers, | |
1896 | 'warnings' => $warnings | |
1897 | ); | |
1898 | return $results; | |
1899 | } | |
1900 | ||
1901 | /** | |
1902 | * Get blocked users return description. | |
1903 | * | |
1904 | * @return external_single_structure | |
1905 | * @since 2.9 | |
1906 | */ | |
1907 | public static function get_blocked_users_returns() { | |
1908 | return new external_single_structure( | |
1909 | array( | |
1910 | 'users' => new external_multiple_structure( | |
1911 | new external_single_structure( | |
1912 | array( | |
1913 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1914 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1915 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL) | |
1916 | ) | |
1917 | ), | |
1918 | 'List of blocked users' | |
1919 | ), | |
1920 | 'warnings' => new external_warnings() | |
1921 | ) | |
1922 | ); | |
1923 | } | |
1924 | ||
31c474da JL |
1925 | /** |
1926 | * Returns description of method parameters | |
1927 | * | |
1928 | * @return external_function_parameters | |
1929 | * @since 2.9 | |
1930 | */ | |
1931 | public static function mark_message_read_parameters() { | |
1932 | return new external_function_parameters( | |
1933 | array( | |
1934 | 'messageid' => new external_value(PARAM_INT, 'id of the message (in the message table)'), | |
0b19d048 | 1935 | 'timeread' => new external_value(PARAM_INT, 'timestamp for when the message should be marked read', VALUE_DEFAULT, 0) |
31c474da JL |
1936 | ) |
1937 | ); | |
1938 | } | |
1939 | ||
1940 | /** | |
1941 | * Mark a single message as read, trigger message_viewed event | |
1942 | * | |
1943 | * @param int $messageid id of the message (in the message table) | |
1944 | * @param int $timeread timestamp for when the message should be marked read | |
1945 | * @return external_description | |
1946 | * @throws invalid_parameter_exception | |
1947 | * @throws moodle_exception | |
1948 | * @since 2.9 | |
1949 | */ | |
1950 | public static function mark_message_read($messageid, $timeread) { | |
1951 | global $CFG, $DB, $USER; | |
31c474da JL |
1952 | |
1953 | // Check if private messaging between users is allowed. | |
1954 | if (empty($CFG->messaging)) { | |
1955 | throw new moodle_exception('disabled', 'message'); | |
1956 | } | |
1957 | ||
1958 | // Warnings array, it can be empty at the end but is mandatory. | |
1959 | $warnings = array(); | |
1960 | ||
1961 | // Validate params. | |
1962 | $params = array( | |
1963 | 'messageid' => $messageid, | |
1964 | 'timeread' => $timeread | |
1965 | ); | |
1966 | $params = self::validate_parameters(self::mark_message_read_parameters(), $params); | |
1967 | ||
0b19d048 RW |
1968 | if (empty($params['timeread'])) { |
1969 | $timeread = time(); | |
1970 | } else { | |
1971 | $timeread = $params['timeread']; | |
1972 | } | |
1973 | ||
31c474da JL |
1974 | // Validate context. |
1975 | $context = context_system::instance(); | |
1976 | self::validate_context($context); | |
1977 | ||
1978 | $message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST); | |
1979 | ||
1980 | if ($message->useridto != $USER->id) { | |
1981 | throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read'); | |
1982 | } | |
1983 | ||
0b19d048 | 1984 | $messageid = message_mark_message_read($message, $timeread); |
31c474da JL |
1985 | |
1986 | $results = array( | |
1987 | 'messageid' => $messageid, | |
1988 | 'warnings' => $warnings | |
1989 | ); | |
1990 | return $results; | |
1991 | } | |
1992 | ||
1993 | /** | |
1994 | * Returns description of method result value | |
1995 | * | |
1996 | * @return external_description | |
1997 | * @since 2.9 | |
1998 | */ | |
1999 | public static function mark_message_read_returns() { | |
2000 | return new external_single_structure( | |
2001 | array( | |
2002 | 'messageid' => new external_value(PARAM_INT, 'the id of the message in the message_read table'), | |
2003 | 'warnings' => new external_warnings() | |
2004 | ) | |
2005 | ); | |
2006 | } | |
2007 | ||
8c55bd6c RW |
2008 | /** |
2009 | * Mark all messages as read parameters description. | |
2010 | * | |
2011 | * @return external_function_parameters | |
2012 | * @since 3.2 | |
2013 | */ | |
2014 | public static function mark_all_messages_as_read_parameters() { | |
2015 | return new external_function_parameters( | |
2016 | array( | |
2017 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), | |
2018 | 'useridfrom' => new external_value( | |
2019 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
2020 | VALUE_DEFAULT, 0), | |
2021 | ) | |
2022 | ); | |
2023 | } | |
2024 | ||
2025 | /** | |
2026 | * Mark all notifications as read function. | |
2027 | * | |
2028 | * @since 3.2 | |
2029 | * @throws invalid_parameter_exception | |
2030 | * @throws moodle_exception | |
2031 | * @param int $useridto the user id who received the message | |
2032 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
2033 | * @return external_description | |
2034 | */ | |
2035 | public static function mark_all_messages_as_read($useridto, $useridfrom) { | |
2036 | global $CFG, $USER; | |
2037 | ||
2038 | $params = self::validate_parameters( | |
2039 | self::mark_all_messages_as_read_parameters(), | |
2040 | array( | |
2041 | 'useridto' => $useridto, | |
2042 | 'useridfrom' => $useridfrom, | |
2043 | ) | |
2044 | ); | |
2045 | ||
2046 | $context = context_system::instance(); | |
2047 | self::validate_context($context); | |
2048 | ||
2049 | $useridto = $params['useridto']; | |
2050 | $useridfrom = $params['useridfrom']; | |
2051 | ||
2052 | if (!empty($useridto)) { | |
2053 | if (core_user::is_real_user($useridto)) { | |
2054 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
2055 | } else { | |
2056 | throw new moodle_exception('invaliduser'); | |
2057 | } | |
2058 | } | |
2059 | ||
2060 | if (!empty($useridfrom)) { | |
2061 | // We use get_user here because the from user can be the noreply or support user. | |
2062 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
2063 | } | |
2064 | ||
2065 | // Check if the current user is the sender/receiver or just a privileged user. | |
2066 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
2067 | // deleteanymessage seems more reasonable here than readallmessages. | |
2068 | !has_capability('moodle/site:deleteanymessage', $context)) { | |
2069 | throw new moodle_exception('accessdenied', 'admin'); | |
2070 | } | |
2071 | ||
2072 | message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE); | |
2073 | ||
2074 | return true; | |
2075 | } | |
2076 | ||
2077 | /** | |
2078 | * Mark all notifications as read return description. | |
2079 | * | |
2080 | * @return external_single_structure | |
2081 | * @since 3.2 | |
2082 | */ | |
2083 | public static function mark_all_messages_as_read_returns() { | |
2084 | return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise'); | |
2085 | } | |
2086 | ||
dec0cd99 MN |
2087 | /** |
2088 | * Returns description of method parameters. | |
2089 | * | |
2090 | * @return external_function_parameters | |
2091 | * @since 3.2 | |
2092 | */ | |
2093 | public static function delete_conversation_parameters() { | |
2094 | return new external_function_parameters( | |
2095 | array( | |
2096 | 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'), | |
2097 | 'otheruserid' => new external_value(PARAM_INT, 'The user id of the other user in the conversation'), | |
2098 | ) | |
2099 | ); | |
2100 | } | |
2101 | ||
2102 | /** | |
2103 | * Deletes a conversation. | |
2104 | * | |
2105 | * @param int $userid The user id of who we want to delete the conversation for | |
2106 | * @param int $otheruserid The user id of the other user in the conversation | |
2107 | * @return array | |
2108 | * @throws moodle_exception | |
2109 | * @since 3.2 | |
2110 | */ | |
2111 | public static function delete_conversation($userid, $otheruserid) { | |
2112 | global $CFG; | |
2113 | ||
2114 | // Check if private messaging between users is allowed. | |
2115 | if (empty($CFG->messaging)) { | |
2116 | throw new moodle_exception('disabled', 'message'); | |
2117 | } | |
2118 | ||
2119 | // Warnings array, it can be empty at the end but is mandatory. | |
2120 | $warnings = array(); | |
2121 | ||
2122 | // Validate params. | |
2123 | $params = array( | |
2124 | 'userid' => $userid, | |
2125 | 'otheruserid' => $otheruserid, | |
2126 | ); | |
2127 | $params = self::validate_parameters(self::delete_conversation_parameters(), $params); | |
2128 | ||
2129 | // Validate context. | |
2130 | $context = context_system::instance(); | |
2131 | self::validate_context($context); | |
2132 | ||
2133 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
2134 | core_user::require_active_user($user); | |
2135 | ||
2136 | if (\core_message\api::can_delete_conversation($user->id)) { | |
2137 | $status = \core_message\api::delete_conversation($user->id, $otheruserid); | |
2138 | } else { | |
2139 | throw new moodle_exception('You do not have permission to delete messages'); | |
2140 | } | |
2141 | ||
2142 | $results = array( | |
2143 | 'status' => $status, | |
2144 | 'warnings' => $warnings | |
2145 | ); | |
2146 | ||
2147 | return $results; | |
2148 | } | |
2149 | ||
2150 | /** | |
2151 | * Returns description of method result value. | |
2152 | * | |
2153 | * @return external_description | |
2154 | * @since 3.2 | |
2155 | */ | |
2156 | public static function delete_conversation_returns() { | |
2157 | return new external_single_structure( | |
2158 | array( | |
2159 | 'status' => new external_value(PARAM_BOOL, 'True if the conversation was deleted, false otherwise'), | |
2160 | 'warnings' => new external_warnings() | |
2161 | ) | |
2162 | ); | |
2163 | } | |
2164 | ||
419b1128 JL |
2165 | /** |
2166 | * Returns description of method parameters | |
2167 | * | |
2168 | * @return external_function_parameters | |
2169 | * @since 3.1 | |
2170 | */ | |
2171 | public static function delete_message_parameters() { | |
2172 | return new external_function_parameters( | |
2173 | array( | |
2174 | 'messageid' => new external_value(PARAM_INT, 'The message id'), | |
2175 | 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the message for'), | |
2176 | 'read' => new external_value(PARAM_BOOL, 'If is a message read', VALUE_DEFAULT, true) | |
2177 | ) | |
2178 | ); | |
2179 | } | |
2180 | ||
2181 | /** | |
2182 | * Deletes a message | |
2183 | * | |
2184 | * @param int $messageid the message id | |
2185 | * @param int $userid the user id of who we want to delete the message for | |
2186 | * @param bool $read if is a message read (default to true) | |
2187 | * @return external_description | |
2188 | * @throws moodle_exception | |
2189 | * @since 3.1 | |
2190 | */ | |
2191 | public static function delete_message($messageid, $userid, $read = true) { | |
2192 | global $CFG, $DB; | |
419b1128 JL |
2193 | |
2194 | // Check if private messaging between users is allowed. | |
2195 | if (empty($CFG->messaging)) { | |
2196 | throw new moodle_exception('disabled', 'message'); | |
2197 | } | |
2198 | ||
2199 | // Warnings array, it can be empty at the end but is mandatory. | |
2200 | $warnings = array(); | |
2201 | ||
2202 | // Validate params. | |
2203 | $params = array( | |
2204 | 'messageid' => $messageid, | |
2205 | 'userid' => $userid, | |
2206 | 'read' => $read | |
2207 | ); | |
2208 | $params = self::validate_parameters(self::delete_message_parameters(), $params); | |
2209 | ||
2210 | // Validate context. | |
2211 | $context = context_system::instance(); | |
2212 | self::validate_context($context); | |
2213 | ||
2214 | $messagestable = $params['read'] ? 'message_read' : 'message'; | |
2215 | $message = $DB->get_record($messagestable, array('id' => $params['messageid']), '*', MUST_EXIST); | |
2216 | ||
2217 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
2218 | core_user::require_active_user($user); | |
2219 | ||
2220 | $status = false; | |
2221 | if (message_can_delete_message($message, $user->id)) { | |
2222 | $status = message_delete_message($message, $user->id);; | |
2223 | } else { | |
2224 | throw new moodle_exception('You do not have permission to delete this message'); | |
2225 | } | |
2226 | ||
2227 | $results = array( | |
2228 | 'status' => $status, | |
2229 | 'warnings' => $warnings | |
2230 | ); | |
2231 | return $results; | |
2232 | } | |
2233 | ||
2234 | /** | |
2235 | * Returns description of method result value | |
2236 | * | |
2237 | * @return external_description | |
2238 | * @since 3.1 | |
2239 | */ | |
2240 | public static function delete_message_returns() { | |
2241 | return new external_single_structure( | |
2242 | array( | |
2243 | 'status' => new external_value(PARAM_BOOL, 'True if the message was deleted, false otherwise'), | |
2244 | 'warnings' => new external_warnings() | |
2245 | ) | |
2246 | ); | |
2247 | } | |
2248 | ||
a0eabdd3 RW |
2249 | /** |
2250 | * Returns description of method parameters | |
2251 | * | |
2252 | * @return external_function_parameters | |
2253 | * @since 3.2 | |
2254 | */ | |
2255 | public static function message_processor_config_form_parameters() { | |
2256 | return new external_function_parameters( | |
2257 | array( | |
2258 | 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED), | |
2259 | 'name' => new external_value(PARAM_TEXT, 'The name of the message processor'), | |
2260 | 'formvalues' => new external_multiple_structure( | |
2261 | new external_single_structure( | |
2262 | array( | |
2263 | 'name' => new external_value(PARAM_TEXT, 'name of the form element', VALUE_REQUIRED), | |
2264 | 'value' => new external_value(PARAM_RAW, 'value of the form element', VALUE_REQUIRED), | |
2265 | ) | |
2266 | ), | |
2267 | 'Config form values', | |
2268 | VALUE_REQUIRED | |
2269 | ), | |
2270 | ) | |
2271 | ); | |
2272 | } | |
2273 | ||
2274 | /** | |
2275 | * Processes a message processor config form. | |
2276 | * | |
2277 | * @param int $userid the user id | |
2278 | * @param string $name the name of the processor | |
2279 | * @param array $formvalues the form values | |
2280 | * @return external_description | |
2281 | * @throws moodle_exception | |
2282 | * @since 3.2 | |
2283 | */ | |
2284 | public static function message_processor_config_form($userid, $name, $formvalues) { | |
8c125526 RW |
2285 | global $USER; |
2286 | ||
a0eabdd3 RW |
2287 | $params = self::validate_parameters( |
2288 | self::message_processor_config_form_parameters(), | |
2289 | array( | |
2290 | 'userid' => $userid, | |
2291 | 'name' => $name, | |
2292 | 'formvalues' => $formvalues, | |
2293 | ) | |
2294 | ); | |
2295 | ||
2296 | if (empty($params['userid'])) { | |
2297 | $params['userid'] = $USER->id; | |
2298 | } | |
2299 | ||
2300 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
2301 | core_user::require_active_user($user); | |
2302 | ||
2303 | $processor = get_message_processor($name); | |
2304 | $preferences = []; | |
2305 | $form = new stdClass(); | |
2306 | ||
2307 | foreach ($formvalues as $formvalue) { | |
2308 | $form->$formvalue['name'] = $formvalue['value']; | |
2309 | } | |
2310 | ||
2311 | $processor->process_form($form, $preferences); | |
2312 | ||
2313 | if (!empty($preferences)) { | |
2314 | set_user_preferences($preferences, $userid); | |
2315 | } | |
2316 | } | |
2317 | ||
2318 | /** | |
2319 | * Returns description of method result value | |
2320 | * | |
2321 | * @return external_description | |
2322 | * @since 3.2 | |
2323 | */ | |
2324 | public static function message_processor_config_form_returns() { | |
2325 | return null; | |
2326 | } | |
8c125526 RW |
2327 | |
2328 | /** | |
2329 | * Returns description of method parameters | |
2330 | * | |
2331 | * @return external_function_parameters | |
2332 | * @since 3.2 | |
2333 | */ | |
2334 | public static function get_message_processor_parameters() { | |
2335 | return new external_function_parameters( | |
2336 | array( | |
2337 | 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user'), | |
2338 | 'name' => new external_value(PARAM_TEXT, 'The name of the message processor', VALUE_REQUIRED), | |
2339 | ) | |
2340 | ); | |
2341 | } | |
2342 | ||
2343 | /** | |
2344 | * Get a message processor. | |
2345 | * | |
2346 | * @param string $name the name of the processor | |
2347 | * @return external_description | |
2348 | * @throws moodle_exception | |
2349 | * @since 3.2 | |
2350 | */ | |
2351 | public static function get_message_processor($userid = 0, $name) { | |
2352 | global $USER, $PAGE; | |
2353 | ||
2354 | $params = self::validate_parameters( | |
2355 | self::get_message_processor_parameters(), | |
2356 | array( | |
2357 | 'userid' => $userid, | |
2358 | 'name' => $name, | |
2359 | ) | |
2360 | ); | |
2361 | ||
2362 | if (empty($params['userid'])) { | |
2363 | $params['userid'] = $USER->id; | |
2364 | } | |
2365 | ||
2366 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
2367 | core_user::require_active_user($user); | |
2368 | self::validate_context(context_user::instance($params['userid'])); | |
2369 | ||
2370 | $processor = get_message_processor($name); | |
2371 | ||
2372 | $processoroutput = new \core_message\output\processor($processor, $user); | |
2373 | $renderer = $PAGE->get_renderer('core_message'); | |
2374 | ||
2375 | return $processoroutput->export_for_template($renderer); | |
2376 | } | |
2377 | ||
2378 | /** | |
2379 | * Returns description of method result value | |
2380 | * | |
2381 | * @return external_description | |
2382 | * @since 3.2 | |
2383 | */ | |
2384 | public static function get_message_processor_returns() { | |
2385 | return new external_function_parameters( | |
2386 | array( | |
2387 | 'systemconfigured' => new external_value(PARAM_BOOL, 'Site configuration status'), | |
2388 | 'userconfigured' => new external_value(PARAM_BOOL, 'The user configuration status'), | |
2389 | ) | |
2390 | ); | |
2391 | } | |
a623b6b8 | 2392 | } |