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 JM |
54 | 'text' => new external_value(PARAM_RAW, 'the text of the message'), |
55 | 'textformat' => new external_format_value('text', VALUE_DEFAULT), | |
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 | ||
9aa012b5 MN |
423 | /** |
424 | * Get messagearea conversations parameters. | |
425 | * | |
426 | * @return external_function_parameters | |
427 | */ | |
428 | public static function data_for_messagearea_conversations_parameters() { | |
429 | return new external_function_parameters( | |
430 | array( | |
431 | 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), | |
432 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
433 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
434 | ) | |
435 | ); | |
436 | } | |
437 | ||
438 | /** | |
439 | * Get messagearea conversations. | |
440 | * | |
441 | * @param int $userid The id of the user who we are viewing conversations for | |
442 | * @param int $limitfrom | |
443 | * @param int $limitnum | |
444 | * @return external_function_parameters | |
445 | */ | |
446 | public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) { | |
447 | global $CFG, $PAGE; | |
448 | ||
449 | // Check if messaging is enabled. | |
450 | if (!$CFG->messaging) { | |
451 | throw new moodle_exception('disabled', 'message'); | |
452 | } | |
453 | ||
454 | $params = array( | |
455 | 'userid' => $userid, | |
456 | 'limitfrom' => $limitfrom, | |
457 | 'limitnum' => $limitnum | |
458 | ); | |
459 | self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params); | |
460 | ||
461 | self::validate_context(context_user::instance($userid)); | |
462 | ||
463 | $contacts = \core_message\api::get_conversations($userid, 0, $limitfrom, $limitnum); | |
464 | ||
465 | $renderer = $PAGE->get_renderer('core_message'); | |
466 | return $contacts->export_for_template($renderer); | |
467 | } | |
468 | ||
469 | /** | |
470 | * Get messagearea conversations returns. | |
471 | * | |
472 | * @return external_function_parameters | |
473 | */ | |
474 | public static function data_for_messagearea_conversations_returns() { | |
475 | return new external_function_parameters( | |
476 | array( | |
477 | 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), | |
478 | 'conversationsselected' => new external_value(PARAM_BOOL, 'Determines if conversations were selected, | |
479 | otherwise contacts were'), | |
480 | 'contacts' => new external_multiple_structure( | |
481 | new external_single_structure( | |
482 | array( | |
483 | 'userid' => new external_value(PARAM_INT, 'The user\'s id'), | |
484 | 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'), | |
485 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'), | |
486 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'), | |
487 | 'lastmessage' => new external_value(PARAM_NOTAGS, 'The user\'s last message', VALUE_OPTIONAL), | |
488 | 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status', VALUE_OPTIONAL) | |
489 | ) | |
490 | ) | |
491 | ) | |
492 | ) | |
493 | ); | |
494 | } | |
495 | ||
496 | /** | |
497 | * Get messagearea contacts parameters. | |
498 | * | |
499 | * @return external_function_parameters | |
500 | */ | |
501 | public static function data_for_messagearea_contacts_parameters() { | |
502 | return self::data_for_messagearea_conversations_parameters(); | |
503 | } | |
504 | ||
505 | /** | |
506 | * Get messagearea contacts parameters. | |
507 | * | |
508 | * @param int $userid The id of the user who we are viewing conversations for | |
509 | * @param int $limitfrom | |
510 | * @param int $limitnum | |
511 | * @return external_function_parameters | |
512 | */ | |
513 | public static function data_for_messagearea_contacts($userid, $limitfrom = 0, $limitnum = 0) { | |
514 | global $CFG, $PAGE; | |
515 | ||
516 | // Check if messaging is enabled. | |
517 | if (!$CFG->messaging) { | |
518 | throw new moodle_exception('disabled', 'message'); | |
519 | } | |
520 | ||
521 | $params = array( | |
522 | 'userid' => $userid, | |
523 | 'limitfrom' => $limitfrom, | |
524 | 'limitnum' => $limitnum | |
525 | ); | |
526 | self::validate_parameters(self::data_for_messagearea_contacts_parameters(), $params); | |
527 | ||
528 | self::validate_context(context_user::instance($userid)); | |
529 | ||
530 | $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum); | |
531 | ||
532 | $renderer = $PAGE->get_renderer('core_message'); | |
533 | return $contacts->export_for_template($renderer); | |
534 | } | |
535 | ||
536 | /** | |
537 | * Get messagearea contacts returns. | |
538 | * | |
539 | * @return external_function_parameters | |
540 | */ | |
541 | public static function data_for_messagearea_contacts_returns() { | |
542 | return self::data_for_messagearea_conversations_returns(); | |
543 | } | |
544 | ||
3cd13828 MN |
545 | /** |
546 | * Get messagearea messages parameters. | |
547 | * | |
548 | * @return external_function_parameters | |
549 | */ | |
550 | public static function data_for_messagearea_messages_parameters() { | |
551 | return new external_function_parameters( | |
552 | array( | |
553 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
554 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
555 | 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), | |
556 | 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) | |
557 | ) | |
558 | ); | |
559 | } | |
560 | ||
561 | /** | |
562 | * Get messagearea messages. | |
563 | * | |
564 | * @param int $currentuserid The current user's id | |
565 | * @param int $otheruserid The other user's id | |
566 | * @param int $limitfrom | |
567 | * @param int $limitnum | |
568 | * @return external_description | |
569 | */ | |
570 | public static function data_for_messagearea_messages($currentuserid, $otheruserid, $limitfrom = 0, $limitnum = 0) { | |
571 | global $CFG, $PAGE; | |
572 | ||
573 | // Check if messaging is enabled. | |
574 | if (!$CFG->messaging) { | |
575 | throw new moodle_exception('disabled', 'message'); | |
576 | } | |
577 | ||
578 | $params = array( | |
579 | 'currentuserid' => $currentuserid, | |
580 | 'otheruserid' => $otheruserid, | |
581 | 'limitfrom' => $limitfrom, | |
582 | 'limitnum' => $limitnum | |
583 | ); | |
584 | self::validate_parameters(self::data_for_messagearea_messages_parameters(), $params); | |
585 | ||
586 | self::validate_context(context_user::instance($currentuserid)); | |
587 | ||
588 | $messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom, $limitnum); | |
589 | ||
590 | $renderer = $PAGE->get_renderer('core_message'); | |
591 | return $messages->export_for_template($renderer); | |
592 | } | |
593 | ||
594 | /** | |
595 | * Get messagearea messages returns. | |
596 | * | |
597 | * @return external_description | |
598 | */ | |
599 | public static function data_for_messagearea_messages_returns() { | |
600 | return new external_function_parameters( | |
601 | array( | |
602 | 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing the messages on behalf of?'), | |
603 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
604 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
605 | 'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'), | |
606 | 'messages' => new external_multiple_structure( | |
607 | new external_single_structure( | |
608 | array( | |
3090f52f | 609 | 'id' => new external_value(PARAM_INT, 'The id of the message'), |
3cd13828 | 610 | 'text' => new external_value(PARAM_RAW, 'The text of the message'), |
3090f52f | 611 | 'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'), |
3cd13828 MN |
612 | 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), |
613 | 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), | |
614 | 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), | |
3090f52f | 615 | 'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'), |
3cd13828 MN |
616 | ) |
617 | ) | |
618 | ) | |
619 | ) | |
620 | ); | |
621 | } | |
622 | ||
c060cd49 MN |
623 | /** |
624 | * Get the most recent message in a conversation parameters. | |
625 | * | |
626 | * @return external_function_parameters | |
627 | */ | |
628 | public static function data_for_messagearea_get_most_recent_message_parameters() { | |
629 | return new external_function_parameters( | |
630 | array( | |
631 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
632 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
633 | ) | |
634 | ); | |
635 | } | |
636 | ||
637 | /** | |
638 | * Get the most recent message in a conversation. | |
639 | * | |
640 | * @param int $currentuserid The current user's id | |
641 | * @param int $otheruserid The other user's id | |
642 | * @return external_single_structure | |
643 | */ | |
644 | public static function data_for_messagearea_get_most_recent_message($currentuserid, $otheruserid) { | |
645 | global $CFG, $PAGE; | |
646 | ||
647 | // Check if messaging is enabled. | |
648 | if (!$CFG->messaging) { | |
649 | throw new moodle_exception('disabled', 'message'); | |
650 | } | |
651 | ||
652 | $params = array( | |
653 | 'currentuserid' => $currentuserid, | |
654 | 'otheruserid' => $otheruserid | |
655 | ); | |
656 | self::validate_parameters(self::data_for_messagearea_get_most_recent_message_parameters(), $params); | |
657 | ||
658 | self::validate_context(context_user::instance($currentuserid)); | |
659 | ||
660 | $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid); | |
661 | ||
662 | $renderer = $PAGE->get_renderer('core_message'); | |
663 | return $message->export_for_template($renderer); | |
664 | } | |
665 | ||
666 | /** | |
667 | * Get messagearea get most recent message returns. | |
668 | * | |
669 | * @return external_single_structure | |
670 | */ | |
671 | public static function data_for_messagearea_get_most_recent_message_returns() { | |
672 | return new external_single_structure( | |
673 | array( | |
3090f52f | 674 | 'id' => new external_value(PARAM_INT, 'The id of the message'), |
c060cd49 | 675 | 'text' => new external_value(PARAM_RAW, 'The text of the message'), |
3090f52f | 676 | 'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'), |
c060cd49 MN |
677 | 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), |
678 | 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), | |
679 | 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), | |
3090f52f | 680 | 'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'), |
c060cd49 MN |
681 | ) |
682 | ); | |
683 | } | |
684 | ||
c6e97f54 MN |
685 | /** |
686 | * The get profile parameters. | |
687 | * | |
688 | * @return external_function_parameters | |
689 | */ | |
690 | public static function data_for_messagearea_get_profile_parameters() { | |
691 | return new external_function_parameters( | |
692 | array( | |
693 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
694 | 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we want to view'), | |
695 | ) | |
696 | ); | |
697 | } | |
698 | ||
699 | /** | |
700 | * Get the profile information for a contact. | |
701 | * | |
702 | * @param int $currentuserid The current user's id | |
703 | * @param int $otheruserid The id of the user whose profile we are viewing | |
704 | * @return external_single_structure | |
705 | */ | |
706 | public static function data_for_messagearea_get_profile($currentuserid, $otheruserid) { | |
707 | global $CFG, $PAGE; | |
708 | ||
709 | // Check if messaging is enabled. | |
710 | if (!$CFG->messaging) { | |
711 | throw new moodle_exception('disabled', 'message'); | |
712 | } | |
713 | ||
714 | $params = array( | |
715 | 'currentuserid' => $currentuserid, | |
716 | 'otheruserid' => $otheruserid | |
717 | ); | |
718 | self::validate_parameters(self::data_for_messagearea_get_profile_parameters(), $params); | |
719 | ||
720 | self::validate_context(context_user::instance($otheruserid)); | |
721 | ||
722 | $profile = \core_message\api::get_profile($currentuserid, $otheruserid); | |
723 | ||
724 | $renderer = $PAGE->get_renderer('core_message'); | |
725 | return $profile->export_for_template($renderer); | |
726 | } | |
727 | ||
728 | /** | |
729 | * Get profile returns. | |
730 | * | |
731 | * @return external_single_structure | |
732 | */ | |
733 | public static function data_for_messagearea_get_profile_returns() { | |
734 | return new external_single_structure( | |
735 | array( | |
736 | 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing the profile on behalf of?'), | |
737 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
738 | 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we are viewing'), | |
739 | 'email' => new external_value(core_user::get_property_type('email'), 'An email address'), | |
740 | 'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user'), | |
741 | 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user'), | |
742 | 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'), | |
743 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'), | |
744 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'), | |
745 | 'isblocked' => new external_value(PARAM_BOOL, 'Is the user blocked?'), | |
746 | 'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?') | |
747 | ) | |
748 | ); | |
749 | } | |
750 | ||
d6731600 FM |
751 | /** |
752 | * Get contacts parameters description. | |
753 | * | |
754 | * @return external_function_parameters | |
5bcfd504 | 755 | * @since Moodle 2.5 |
d6731600 FM |
756 | */ |
757 | public static function get_contacts_parameters() { | |
758 | return new external_function_parameters(array()); | |
759 | } | |
760 | ||
761 | /** | |
762 | * Get contacts. | |
763 | * | |
764 | * @param array $userids array of user IDs. | |
765 | * @return external_description | |
5bcfd504 | 766 | * @since Moodle 2.5 |
d6731600 FM |
767 | */ |
768 | public static function get_contacts() { | |
d85bedf7 | 769 | global $CFG, $PAGE; |
436bbf89 DM |
770 | |
771 | // Check if messaging is enabled. | |
772 | if (!$CFG->messaging) { | |
773 | throw new moodle_exception('disabled', 'message'); | |
774 | } | |
775 | ||
d6731600 FM |
776 | require_once($CFG->dirroot . '/user/lib.php'); |
777 | ||
778 | list($online, $offline, $strangers) = message_get_contacts(); | |
779 | $allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers); | |
780 | foreach ($allcontacts as $mode => $contacts) { | |
781 | foreach ($contacts as $key => $contact) { | |
782 | $newcontact = array( | |
783 | 'id' => $contact->id, | |
784 | 'fullname' => fullname($contact), | |
785 | 'unread' => $contact->messagecount | |
786 | ); | |
787 | ||
d85bedf7 JL |
788 | $userpicture = new user_picture($contact); |
789 | $userpicture->size = 1; // Size f1. | |
790 | $newcontact['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
791 | $userpicture->size = 0; // Size f2. | |
792 | $newcontact['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
793 | |
794 | $allcontacts[$mode][$key] = $newcontact; | |
795 | } | |
796 | } | |
797 | return $allcontacts; | |
798 | } | |
799 | ||
800 | /** | |
801 | * Get contacts return description. | |
802 | * | |
803 | * @return external_description | |
5bcfd504 | 804 | * @since Moodle 2.5 |
d6731600 FM |
805 | */ |
806 | public static function get_contacts_returns() { | |
807 | return new external_single_structure( | |
808 | array( | |
809 | 'online' => new external_multiple_structure( | |
810 | new external_single_structure( | |
811 | array( | |
812 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
813 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
814 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
815 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
816 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
817 | ) | |
818 | ), | |
819 | 'List of online contacts' | |
820 | ), | |
821 | 'offline' => new external_multiple_structure( | |
822 | new external_single_structure( | |
823 | array( | |
824 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
825 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
826 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
827 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
828 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
829 | ) | |
830 | ), | |
831 | 'List of offline contacts' | |
832 | ), | |
833 | 'strangers' => new external_multiple_structure( | |
834 | new external_single_structure( | |
835 | array( | |
836 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
837 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
838 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
839 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
840 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
841 | ) | |
842 | ), | |
843 | 'List of users that are not in the user\'s contact list but have sent a message' | |
844 | ) | |
845 | ) | |
846 | ); | |
847 | } | |
848 | ||
849 | /** | |
850 | * Search contacts parameters description. | |
851 | * | |
852 | * @return external_function_parameters | |
5bcfd504 | 853 | * @since Moodle 2.5 |
d6731600 FM |
854 | */ |
855 | public static function search_contacts_parameters() { | |
856 | return new external_function_parameters( | |
857 | array( | |
858 | 'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'), | |
859 | 'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses', | |
860 | VALUE_DEFAULT, false) | |
861 | ) | |
862 | ); | |
863 | } | |
864 | ||
865 | /** | |
866 | * Search contacts. | |
867 | * | |
868 | * @param string $searchtext query string. | |
869 | * @param bool $onlymycourses limit the search to the user's courses only. | |
870 | * @return external_description | |
5bcfd504 | 871 | * @since Moodle 2.5 |
d6731600 FM |
872 | */ |
873 | public static function search_contacts($searchtext, $onlymycourses = false) { | |
d85bedf7 | 874 | global $CFG, $USER, $PAGE; |
11d83ab3 | 875 | require_once($CFG->dirroot . '/user/lib.php'); |
436bbf89 DM |
876 | |
877 | // Check if messaging is enabled. | |
878 | if (!$CFG->messaging) { | |
879 | throw new moodle_exception('disabled', 'message'); | |
880 | } | |
881 | ||
d6731600 FM |
882 | require_once($CFG->libdir . '/enrollib.php'); |
883 | ||
884 | $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses); | |
885 | $params = self::validate_parameters(self::search_contacts_parameters(), $params); | |
886 | ||
887 | // Extra validation, we do not allow empty queries. | |
888 | if ($params['searchtext'] === '') { | |
889 | throw new moodle_exception('querystringcannotbeempty'); | |
890 | } | |
891 | ||
892 | $courseids = array(); | |
893 | if ($params['onlymycourses']) { | |
894 | $mycourses = enrol_get_my_courses(array('id')); | |
895 | foreach ($mycourses as $mycourse) { | |
896 | $courseids[] = $mycourse->id; | |
897 | } | |
898 | } else { | |
899 | $courseids[] = SITEID; | |
900 | } | |
901 | ||
902 | // Retrieving the users matching the query. | |
903 | $users = message_search_users($courseids, $params['searchtext']); | |
904 | $results = array(); | |
905 | foreach ($users as $user) { | |
906 | $results[$user->id] = $user; | |
907 | } | |
908 | ||
909 | // Reorganising information. | |
910 | foreach ($results as &$user) { | |
911 | $newuser = array( | |
912 | 'id' => $user->id, | |
913 | 'fullname' => fullname($user) | |
914 | ); | |
915 | ||
916 | // Avoid undefined property notice as phone not specified. | |
917 | $user->phone1 = null; | |
918 | $user->phone2 = null; | |
919 | ||
d85bedf7 JL |
920 | $userpicture = new user_picture($user); |
921 | $userpicture->size = 1; // Size f1. | |
922 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
923 | $userpicture->size = 0; // Size f2. | |
924 | $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
925 | |
926 | $user = $newuser; | |
927 | } | |
928 | ||
929 | return $results; | |
930 | } | |
931 | ||
932 | /** | |
933 | * Search contacts return description. | |
934 | * | |
935 | * @return external_description | |
5bcfd504 | 936 | * @since Moodle 2.5 |
d6731600 FM |
937 | */ |
938 | public static function search_contacts_returns() { | |
939 | return new external_multiple_structure( | |
940 | new external_single_structure( | |
941 | array( | |
942 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
943 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
944 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
945 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL) | |
946 | ) | |
947 | ), | |
948 | 'List of contacts' | |
949 | ); | |
950 | } | |
aff9da17 JL |
951 | |
952 | /** | |
953 | * Get messages parameters description. | |
954 | * | |
955 | * @return external_function_parameters | |
193edf7f | 956 | * @since 2.8 |
aff9da17 JL |
957 | */ |
958 | public static function get_messages_parameters() { | |
959 | return new external_function_parameters( | |
960 | array( | |
6ff4464b | 961 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), |
127ef540 SH |
962 | 'useridfrom' => new external_value( |
963 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
964 | VALUE_DEFAULT, 0), | |
965 | 'type' => new external_value( | |
966 | PARAM_ALPHA, 'type of message to return, expected values are: notifications, conversations and both', | |
967 | VALUE_DEFAULT, 'both'), | |
6ff4464b | 968 | 'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true), |
127ef540 SH |
969 | 'newestfirst' => new external_value( |
970 | PARAM_BOOL, 'true for ordering by newest first, false for oldest first', | |
971 | VALUE_DEFAULT, true), | |
aff9da17 | 972 | 'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0), |
127ef540 SH |
973 | 'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0) |
974 | ) | |
aff9da17 JL |
975 | ); |
976 | } | |
977 | ||
978 | /** | |
979 | * Get messages function implementation. | |
127ef540 SH |
980 | * |
981 | * @since 2.8 | |
982 | * @throws invalid_parameter_exception | |
983 | * @throws moodle_exception | |
6ff4464b JL |
984 | * @param int $useridto the user id who received the message |
985 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
193edf7f | 986 | * @param string $type type of message to return, expected values: notifications, conversations and both |
aff9da17 | 987 | * @param bool $read true for retreiving read messages, false for unread |
6ff4464b | 988 | * @param bool $newestfirst true for ordering by newest first, false for oldest first |
aff9da17 JL |
989 | * @param int $limitfrom limit from |
990 | * @param int $limitnum limit num | |
991 | * @return external_description | |
aff9da17 | 992 | */ |
193edf7f | 993 | public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, |
aff9da17 | 994 | $newestfirst = true, $limitfrom = 0, $limitnum = 0) { |
127ef540 | 995 | global $CFG, $USER; |
aff9da17 JL |
996 | |
997 | $warnings = array(); | |
998 | ||
999 | $params = array( | |
1000 | 'useridto' => $useridto, | |
6ff4464b | 1001 | 'useridfrom' => $useridfrom, |
aff9da17 JL |
1002 | 'type' => $type, |
1003 | 'read' => $read, | |
aff9da17 JL |
1004 | 'newestfirst' => $newestfirst, |
1005 | 'limitfrom' => $limitfrom, | |
1006 | 'limitnum' => $limitnum | |
1007 | ); | |
1008 | ||
1009 | $params = self::validate_parameters(self::get_messages_parameters(), $params); | |
1010 | ||
1011 | $context = context_system::instance(); | |
1012 | self::validate_context($context); | |
1013 | ||
1014 | $useridto = $params['useridto']; | |
6ff4464b | 1015 | $useridfrom = $params['useridfrom']; |
aff9da17 JL |
1016 | $type = $params['type']; |
1017 | $read = $params['read']; | |
aff9da17 JL |
1018 | $newestfirst = $params['newestfirst']; |
1019 | $limitfrom = $params['limitfrom']; | |
1020 | $limitnum = $params['limitnum']; | |
1021 | ||
1022 | $allowedvalues = array('notifications', 'conversations', 'both'); | |
1023 | if (!in_array($type, $allowedvalues)) { | |
1024 | throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . | |
1025 | 'allowed values are: ' . implode(',', $allowedvalues)); | |
1026 | } | |
1027 | ||
1028 | // Check if private messaging between users is allowed. | |
1029 | if (empty($CFG->messaging)) { | |
1030 | // If we are retreiving only conversations, and messaging is disabled, throw an exception. | |
aff9da17 JL |
1031 | if ($type == "conversations") { |
1032 | throw new moodle_exception('disabled', 'message'); | |
1033 | } | |
1034 | if ($type == "both") { | |
1035 | $warning = array(); | |
1036 | $warning['item'] = 'message'; | |
1037 | $warning['itemid'] = $USER->id; | |
1038 | $warning['warningcode'] = '1'; | |
1039 | $warning['message'] = 'Private messages (conversations) are not enabled in this site. | |
1040 | Only notifications will be returned'; | |
1041 | $warnings[] = $warning; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | if (!empty($useridto)) { | |
6ff4464b JL |
1046 | if (core_user::is_real_user($useridto)) { |
1047 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
1048 | } else { | |
1049 | throw new moodle_exception('invaliduser'); | |
1050 | } | |
aff9da17 JL |
1051 | } |
1052 | ||
1053 | if (!empty($useridfrom)) { | |
1054 | // We use get_user here because the from user can be the noreply or support user. | |
1055 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
1056 | } | |
1057 | ||
1058 | // Check if the current user is the sender/receiver or just a privileged user. | |
1059 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
1060 | !has_capability('moodle/site:readallmessages', $context)) { | |
1061 | throw new moodle_exception('accessdenied', 'admin'); | |
1062 | } | |
1063 | ||
127ef540 | 1064 | // Which type of messages to retrieve. |
193edf7f | 1065 | $notifications = -1; |
aff9da17 | 1066 | if ($type != 'both') { |
193edf7f | 1067 | $notifications = ($type == 'notifications') ? 1 : 0; |
aff9da17 JL |
1068 | } |
1069 | ||
aff9da17 | 1070 | $orderdirection = $newestfirst ? 'DESC' : 'ASC'; |
193edf7f | 1071 | $sort = "mr.timecreated $orderdirection"; |
aff9da17 | 1072 | |
193edf7f | 1073 | if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) { |
aff9da17 JL |
1074 | $canviewfullname = has_capability('moodle/site:viewfullnames', $context); |
1075 | ||
1076 | // In some cases, we don't need to get the to/from user objects from the sql query. | |
1077 | $userfromfullname = ''; | |
1078 | $usertofullname = ''; | |
1079 | ||
1080 | // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there. | |
1081 | if (!empty($useridto)) { | |
1082 | $usertofullname = fullname($userto, $canviewfullname); | |
1083 | // The user from may or may not be filled. | |
1084 | if (!empty($useridfrom)) { | |
1085 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1086 | } | |
1087 | } else { | |
1088 | // If the useridto field is empty, the useridfrom must be filled. | |
1089 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1090 | } | |
aff9da17 JL |
1091 | foreach ($messages as $mid => $message) { |
1092 | ||
ea21d637 JL |
1093 | // Do not return deleted messages. |
1094 | if (($useridto == $USER->id and $message->timeusertodeleted) or | |
1095 | ($useridfrom == $USER->id and $message->timeuserfromdeleted)) { | |
1096 | ||
1097 | unset($messages[$mid]); | |
1098 | continue; | |
1099 | } | |
1100 | ||
aff9da17 JL |
1101 | // We need to get the user from the query. |
1102 | if (empty($userfromfullname)) { | |
6ff4464b JL |
1103 | // Check for non-reply and support users. |
1104 | if (core_user::is_real_user($message->useridfrom)) { | |
127ef540 | 1105 | $user = new stdClass(); |
6ff4464b JL |
1106 | $user = username_load_fields_from_object($user, $message, 'userfrom'); |
1107 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1108 | } else { | |
1109 | $user = core_user::get_user($message->useridfrom); | |
1110 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1111 | } | |
aff9da17 JL |
1112 | } else { |
1113 | $message->userfromfullname = $userfromfullname; | |
1114 | } | |
1115 | ||
1116 | // We need to get the user from the query. | |
1117 | if (empty($usertofullname)) { | |
127ef540 | 1118 | $user = new stdClass(); |
aff9da17 JL |
1119 | $user = username_load_fields_from_object($user, $message, 'userto'); |
1120 | $message->usertofullname = fullname($user, $canviewfullname); | |
1121 | } else { | |
1122 | $message->usertofullname = $usertofullname; | |
1123 | } | |
1124 | ||
193edf7f | 1125 | // This field is only available in the message_read table. |
aff9da17 JL |
1126 | if (!isset($message->timeread)) { |
1127 | $message->timeread = 0; | |
1128 | } | |
1129 | ||
aff9da17 | 1130 | $message->text = message_format_message_text($message); |
aff9da17 JL |
1131 | $messages[$mid] = (array) $message; |
1132 | } | |
1133 | } | |
1134 | ||
1135 | $results = array( | |
1136 | 'messages' => $messages, | |
1137 | 'warnings' => $warnings | |
1138 | ); | |
1139 | ||
1140 | return $results; | |
1141 | } | |
1142 | ||
1143 | /** | |
1144 | * Get messages return description. | |
1145 | * | |
6ff4464b | 1146 | * @return external_single_structure |
193edf7f | 1147 | * @since 2.8 |
aff9da17 JL |
1148 | */ |
1149 | public static function get_messages_returns() { | |
1150 | return new external_single_structure( | |
1151 | array( | |
1152 | 'messages' => new external_multiple_structure( | |
1153 | new external_single_structure( | |
1154 | array( | |
193edf7f | 1155 | 'id' => new external_value(PARAM_INT, 'Message id'), |
aff9da17 JL |
1156 | 'useridfrom' => new external_value(PARAM_INT, 'User from id'), |
1157 | 'useridto' => new external_value(PARAM_INT, 'User to id'), | |
1158 | 'subject' => new external_value(PARAM_TEXT, 'The message subject'), | |
1159 | 'text' => new external_value(PARAM_RAW, 'The message text formated'), | |
1160 | 'fullmessage' => new external_value(PARAM_RAW, 'The message'), | |
193edf7f | 1161 | 'fullmessageformat' => new external_format_value('fullmessage'), |
aff9da17 JL |
1162 | 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'), |
1163 | 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'), | |
1164 | 'notification' => new external_value(PARAM_INT, 'Is a notification?'), | |
1165 | 'contexturl' => new external_value(PARAM_RAW, 'Context URL'), | |
1166 | 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'), | |
1167 | 'timecreated' => new external_value(PARAM_INT, 'Time created'), | |
1168 | 'timeread' => new external_value(PARAM_INT, 'Time read'), | |
1169 | 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'), | |
1170 | 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name') | |
1171 | ), 'message' | |
1172 | ) | |
1173 | ), | |
1174 | 'warnings' => new external_warnings() | |
1175 | ) | |
1176 | ); | |
1177 | } | |
1178 | ||
60ab2e1b JL |
1179 | /** |
1180 | * Get blocked users parameters description. | |
1181 | * | |
1182 | * @return external_function_parameters | |
1183 | * @since 2.9 | |
1184 | */ | |
1185 | public static function get_blocked_users_parameters() { | |
1186 | return new external_function_parameters( | |
1187 | array( | |
1188 | 'userid' => new external_value(PARAM_INT, | |
1189 | 'the user whose blocked users we want to retrieve', | |
1190 | VALUE_REQUIRED), | |
1191 | ) | |
1192 | ); | |
1193 | } | |
1194 | ||
1195 | /** | |
1196 | * Retrieve a list of users blocked | |
1197 | * | |
1198 | * @param int $userid the user whose blocked users we want to retrieve | |
1199 | * @return external_description | |
1200 | * @since 2.9 | |
1201 | */ | |
1202 | public static function get_blocked_users($userid) { | |
d85bedf7 | 1203 | global $CFG, $USER, $PAGE; |
60ab2e1b JL |
1204 | |
1205 | // Warnings array, it can be empty at the end but is mandatory. | |
1206 | $warnings = array(); | |
1207 | ||
1208 | // Validate params. | |
1209 | $params = array( | |
1210 | 'userid' => $userid | |
1211 | ); | |
1212 | $params = self::validate_parameters(self::get_blocked_users_parameters(), $params); | |
1213 | $userid = $params['userid']; | |
1214 | ||
1215 | // Validate context. | |
1216 | $context = context_system::instance(); | |
1217 | self::validate_context($context); | |
1218 | ||
1219 | // Check if private messaging between users is allowed. | |
1220 | if (empty($CFG->messaging)) { | |
1221 | throw new moodle_exception('disabled', 'message'); | |
1222 | } | |
1223 | ||
4485f7c5 JL |
1224 | $user = core_user::get_user($userid, '*', MUST_EXIST); |
1225 | core_user::require_active_user($user); | |
60ab2e1b JL |
1226 | |
1227 | // Check if we have permissions for retrieve the information. | |
1228 | if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { | |
1229 | throw new moodle_exception('accessdenied', 'admin'); | |
1230 | } | |
1231 | ||
1232 | // Now, we can get safely all the blocked users. | |
1233 | $users = message_get_blocked_users($user); | |
1234 | ||
1235 | $blockedusers = array(); | |
1236 | foreach ($users as $user) { | |
1237 | $newuser = array( | |
1238 | 'id' => $user->id, | |
1239 | 'fullname' => fullname($user), | |
1240 | ); | |
0b074e88 | 1241 | |
d85bedf7 JL |
1242 | $userpicture = new user_picture($user); |
1243 | $userpicture->size = 1; // Size f1. | |
1244 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
60ab2e1b JL |
1245 | |
1246 | $blockedusers[] = $newuser; | |
1247 | } | |
1248 | ||
1249 | $results = array( | |
1250 | 'users' => $blockedusers, | |
1251 | 'warnings' => $warnings | |
1252 | ); | |
1253 | return $results; | |
1254 | } | |
1255 | ||
1256 | /** | |
1257 | * Get blocked users return description. | |
1258 | * | |
1259 | * @return external_single_structure | |
1260 | * @since 2.9 | |
1261 | */ | |
1262 | public static function get_blocked_users_returns() { | |
1263 | return new external_single_structure( | |
1264 | array( | |
1265 | 'users' => new external_multiple_structure( | |
1266 | new external_single_structure( | |
1267 | array( | |
1268 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1269 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1270 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL) | |
1271 | ) | |
1272 | ), | |
1273 | 'List of blocked users' | |
1274 | ), | |
1275 | 'warnings' => new external_warnings() | |
1276 | ) | |
1277 | ); | |
1278 | } | |
1279 | ||
31c474da JL |
1280 | /** |
1281 | * Returns description of method parameters | |
1282 | * | |
1283 | * @return external_function_parameters | |
1284 | * @since 2.9 | |
1285 | */ | |
1286 | public static function mark_message_read_parameters() { | |
1287 | return new external_function_parameters( | |
1288 | array( | |
1289 | 'messageid' => new external_value(PARAM_INT, 'id of the message (in the message table)'), | |
1290 | 'timeread' => new external_value(PARAM_INT, 'timestamp for when the message should be marked read') | |
1291 | ) | |
1292 | ); | |
1293 | } | |
1294 | ||
1295 | /** | |
1296 | * Mark a single message as read, trigger message_viewed event | |
1297 | * | |
1298 | * @param int $messageid id of the message (in the message table) | |
1299 | * @param int $timeread timestamp for when the message should be marked read | |
1300 | * @return external_description | |
1301 | * @throws invalid_parameter_exception | |
1302 | * @throws moodle_exception | |
1303 | * @since 2.9 | |
1304 | */ | |
1305 | public static function mark_message_read($messageid, $timeread) { | |
1306 | global $CFG, $DB, $USER; | |
31c474da JL |
1307 | |
1308 | // Check if private messaging between users is allowed. | |
1309 | if (empty($CFG->messaging)) { | |
1310 | throw new moodle_exception('disabled', 'message'); | |
1311 | } | |
1312 | ||
1313 | // Warnings array, it can be empty at the end but is mandatory. | |
1314 | $warnings = array(); | |
1315 | ||
1316 | // Validate params. | |
1317 | $params = array( | |
1318 | 'messageid' => $messageid, | |
1319 | 'timeread' => $timeread | |
1320 | ); | |
1321 | $params = self::validate_parameters(self::mark_message_read_parameters(), $params); | |
1322 | ||
1323 | // Validate context. | |
1324 | $context = context_system::instance(); | |
1325 | self::validate_context($context); | |
1326 | ||
1327 | $message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST); | |
1328 | ||
1329 | if ($message->useridto != $USER->id) { | |
1330 | throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read'); | |
1331 | } | |
1332 | ||
1333 | $messageid = message_mark_message_read($message, $params['timeread']); | |
1334 | ||
1335 | $results = array( | |
1336 | 'messageid' => $messageid, | |
1337 | 'warnings' => $warnings | |
1338 | ); | |
1339 | return $results; | |
1340 | } | |
1341 | ||
1342 | /** | |
1343 | * Returns description of method result value | |
1344 | * | |
1345 | * @return external_description | |
1346 | * @since 2.9 | |
1347 | */ | |
1348 | public static function mark_message_read_returns() { | |
1349 | return new external_single_structure( | |
1350 | array( | |
1351 | 'messageid' => new external_value(PARAM_INT, 'the id of the message in the message_read table'), | |
1352 | 'warnings' => new external_warnings() | |
1353 | ) | |
1354 | ); | |
1355 | } | |
1356 | ||
dec0cd99 MN |
1357 | /** |
1358 | * Returns description of method parameters. | |
1359 | * | |
1360 | * @return external_function_parameters | |
1361 | * @since 3.2 | |
1362 | */ | |
1363 | public static function delete_conversation_parameters() { | |
1364 | return new external_function_parameters( | |
1365 | array( | |
1366 | 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'), | |
1367 | 'otheruserid' => new external_value(PARAM_INT, 'The user id of the other user in the conversation'), | |
1368 | ) | |
1369 | ); | |
1370 | } | |
1371 | ||
1372 | /** | |
1373 | * Deletes a conversation. | |
1374 | * | |
1375 | * @param int $userid The user id of who we want to delete the conversation for | |
1376 | * @param int $otheruserid The user id of the other user in the conversation | |
1377 | * @return array | |
1378 | * @throws moodle_exception | |
1379 | * @since 3.2 | |
1380 | */ | |
1381 | public static function delete_conversation($userid, $otheruserid) { | |
1382 | global $CFG; | |
1383 | ||
1384 | // Check if private messaging between users is allowed. | |
1385 | if (empty($CFG->messaging)) { | |
1386 | throw new moodle_exception('disabled', 'message'); | |
1387 | } | |
1388 | ||
1389 | // Warnings array, it can be empty at the end but is mandatory. | |
1390 | $warnings = array(); | |
1391 | ||
1392 | // Validate params. | |
1393 | $params = array( | |
1394 | 'userid' => $userid, | |
1395 | 'otheruserid' => $otheruserid, | |
1396 | ); | |
1397 | $params = self::validate_parameters(self::delete_conversation_parameters(), $params); | |
1398 | ||
1399 | // Validate context. | |
1400 | $context = context_system::instance(); | |
1401 | self::validate_context($context); | |
1402 | ||
1403 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
1404 | core_user::require_active_user($user); | |
1405 | ||
1406 | if (\core_message\api::can_delete_conversation($user->id)) { | |
1407 | $status = \core_message\api::delete_conversation($user->id, $otheruserid); | |
1408 | } else { | |
1409 | throw new moodle_exception('You do not have permission to delete messages'); | |
1410 | } | |
1411 | ||
1412 | $results = array( | |
1413 | 'status' => $status, | |
1414 | 'warnings' => $warnings | |
1415 | ); | |
1416 | ||
1417 | return $results; | |
1418 | } | |
1419 | ||
1420 | /** | |
1421 | * Returns description of method result value. | |
1422 | * | |
1423 | * @return external_description | |
1424 | * @since 3.2 | |
1425 | */ | |
1426 | public static function delete_conversation_returns() { | |
1427 | return new external_single_structure( | |
1428 | array( | |
1429 | 'status' => new external_value(PARAM_BOOL, 'True if the conversation was deleted, false otherwise'), | |
1430 | 'warnings' => new external_warnings() | |
1431 | ) | |
1432 | ); | |
1433 | } | |
1434 | ||
419b1128 JL |
1435 | /** |
1436 | * Returns description of method parameters | |
1437 | * | |
1438 | * @return external_function_parameters | |
1439 | * @since 3.1 | |
1440 | */ | |
1441 | public static function delete_message_parameters() { | |
1442 | return new external_function_parameters( | |
1443 | array( | |
1444 | 'messageid' => new external_value(PARAM_INT, 'The message id'), | |
1445 | 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the message for'), | |
1446 | 'read' => new external_value(PARAM_BOOL, 'If is a message read', VALUE_DEFAULT, true) | |
1447 | ) | |
1448 | ); | |
1449 | } | |
1450 | ||
1451 | /** | |
1452 | * Deletes a message | |
1453 | * | |
1454 | * @param int $messageid the message id | |
1455 | * @param int $userid the user id of who we want to delete the message for | |
1456 | * @param bool $read if is a message read (default to true) | |
1457 | * @return external_description | |
1458 | * @throws moodle_exception | |
1459 | * @since 3.1 | |
1460 | */ | |
1461 | public static function delete_message($messageid, $userid, $read = true) { | |
1462 | global $CFG, $DB; | |
419b1128 JL |
1463 | |
1464 | // Check if private messaging between users is allowed. | |
1465 | if (empty($CFG->messaging)) { | |
1466 | throw new moodle_exception('disabled', 'message'); | |
1467 | } | |
1468 | ||
1469 | // Warnings array, it can be empty at the end but is mandatory. | |
1470 | $warnings = array(); | |
1471 | ||
1472 | // Validate params. | |
1473 | $params = array( | |
1474 | 'messageid' => $messageid, | |
1475 | 'userid' => $userid, | |
1476 | 'read' => $read | |
1477 | ); | |
1478 | $params = self::validate_parameters(self::delete_message_parameters(), $params); | |
1479 | ||
1480 | // Validate context. | |
1481 | $context = context_system::instance(); | |
1482 | self::validate_context($context); | |
1483 | ||
1484 | $messagestable = $params['read'] ? 'message_read' : 'message'; | |
1485 | $message = $DB->get_record($messagestable, array('id' => $params['messageid']), '*', MUST_EXIST); | |
1486 | ||
1487 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
1488 | core_user::require_active_user($user); | |
1489 | ||
1490 | $status = false; | |
1491 | if (message_can_delete_message($message, $user->id)) { | |
1492 | $status = message_delete_message($message, $user->id);; | |
1493 | } else { | |
1494 | throw new moodle_exception('You do not have permission to delete this message'); | |
1495 | } | |
1496 | ||
1497 | $results = array( | |
1498 | 'status' => $status, | |
1499 | 'warnings' => $warnings | |
1500 | ); | |
1501 | return $results; | |
1502 | } | |
1503 | ||
1504 | /** | |
1505 | * Returns description of method result value | |
1506 | * | |
1507 | * @return external_description | |
1508 | * @since 3.1 | |
1509 | */ | |
1510 | public static function delete_message_returns() { | |
1511 | return new external_single_structure( | |
1512 | array( | |
1513 | 'status' => new external_value(PARAM_BOOL, 'True if the message was deleted, false otherwise'), | |
1514 | 'warnings' => new external_warnings() | |
1515 | ) | |
1516 | ); | |
1517 | } | |
1518 | ||
a623b6b8 | 1519 | } |