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( | |
609 | 'text' => new external_value(PARAM_RAW, 'The text of the message'), | |
610 | 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), | |
611 | 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), | |
612 | 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), | |
613 | ) | |
614 | ) | |
615 | ) | |
616 | ) | |
617 | ); | |
618 | } | |
619 | ||
c060cd49 MN |
620 | /** |
621 | * Get the most recent message in a conversation parameters. | |
622 | * | |
623 | * @return external_function_parameters | |
624 | */ | |
625 | public static function data_for_messagearea_get_most_recent_message_parameters() { | |
626 | return new external_function_parameters( | |
627 | array( | |
628 | 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), | |
629 | 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), | |
630 | ) | |
631 | ); | |
632 | } | |
633 | ||
634 | /** | |
635 | * Get the most recent message in a conversation. | |
636 | * | |
637 | * @param int $currentuserid The current user's id | |
638 | * @param int $otheruserid The other user's id | |
639 | * @return external_single_structure | |
640 | */ | |
641 | public static function data_for_messagearea_get_most_recent_message($currentuserid, $otheruserid) { | |
642 | global $CFG, $PAGE; | |
643 | ||
644 | // Check if messaging is enabled. | |
645 | if (!$CFG->messaging) { | |
646 | throw new moodle_exception('disabled', 'message'); | |
647 | } | |
648 | ||
649 | $params = array( | |
650 | 'currentuserid' => $currentuserid, | |
651 | 'otheruserid' => $otheruserid | |
652 | ); | |
653 | self::validate_parameters(self::data_for_messagearea_get_most_recent_message_parameters(), $params); | |
654 | ||
655 | self::validate_context(context_user::instance($currentuserid)); | |
656 | ||
657 | $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid); | |
658 | ||
659 | $renderer = $PAGE->get_renderer('core_message'); | |
660 | return $message->export_for_template($renderer); | |
661 | } | |
662 | ||
663 | /** | |
664 | * Get messagearea get most recent message returns. | |
665 | * | |
666 | * @return external_single_structure | |
667 | */ | |
668 | public static function data_for_messagearea_get_most_recent_message_returns() { | |
669 | return new external_single_structure( | |
670 | array( | |
671 | 'text' => new external_value(PARAM_RAW, 'The text of the message'), | |
672 | 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), | |
673 | 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), | |
674 | 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), | |
675 | ) | |
676 | ); | |
677 | } | |
678 | ||
d6731600 FM |
679 | /** |
680 | * Get contacts parameters description. | |
681 | * | |
682 | * @return external_function_parameters | |
5bcfd504 | 683 | * @since Moodle 2.5 |
d6731600 FM |
684 | */ |
685 | public static function get_contacts_parameters() { | |
686 | return new external_function_parameters(array()); | |
687 | } | |
688 | ||
689 | /** | |
690 | * Get contacts. | |
691 | * | |
692 | * @param array $userids array of user IDs. | |
693 | * @return external_description | |
5bcfd504 | 694 | * @since Moodle 2.5 |
d6731600 FM |
695 | */ |
696 | public static function get_contacts() { | |
d85bedf7 | 697 | global $CFG, $PAGE; |
436bbf89 DM |
698 | |
699 | // Check if messaging is enabled. | |
700 | if (!$CFG->messaging) { | |
701 | throw new moodle_exception('disabled', 'message'); | |
702 | } | |
703 | ||
d6731600 FM |
704 | require_once($CFG->dirroot . '/user/lib.php'); |
705 | ||
706 | list($online, $offline, $strangers) = message_get_contacts(); | |
707 | $allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers); | |
708 | foreach ($allcontacts as $mode => $contacts) { | |
709 | foreach ($contacts as $key => $contact) { | |
710 | $newcontact = array( | |
711 | 'id' => $contact->id, | |
712 | 'fullname' => fullname($contact), | |
713 | 'unread' => $contact->messagecount | |
714 | ); | |
715 | ||
d85bedf7 JL |
716 | $userpicture = new user_picture($contact); |
717 | $userpicture->size = 1; // Size f1. | |
718 | $newcontact['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
719 | $userpicture->size = 0; // Size f2. | |
720 | $newcontact['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
721 | |
722 | $allcontacts[$mode][$key] = $newcontact; | |
723 | } | |
724 | } | |
725 | return $allcontacts; | |
726 | } | |
727 | ||
728 | /** | |
729 | * Get contacts return description. | |
730 | * | |
731 | * @return external_description | |
5bcfd504 | 732 | * @since Moodle 2.5 |
d6731600 FM |
733 | */ |
734 | public static function get_contacts_returns() { | |
735 | return new external_single_structure( | |
736 | array( | |
737 | 'online' => new external_multiple_structure( | |
738 | new external_single_structure( | |
739 | array( | |
740 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
741 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
742 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
743 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
744 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
745 | ) | |
746 | ), | |
747 | 'List of online contacts' | |
748 | ), | |
749 | 'offline' => new external_multiple_structure( | |
750 | new external_single_structure( | |
751 | array( | |
752 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
753 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
754 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
755 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
756 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
757 | ) | |
758 | ), | |
759 | 'List of offline contacts' | |
760 | ), | |
761 | 'strangers' => new external_multiple_structure( | |
762 | new external_single_structure( | |
763 | array( | |
764 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
765 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
766 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
767 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
768 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
769 | ) | |
770 | ), | |
771 | 'List of users that are not in the user\'s contact list but have sent a message' | |
772 | ) | |
773 | ) | |
774 | ); | |
775 | } | |
776 | ||
777 | /** | |
778 | * Search contacts parameters description. | |
779 | * | |
780 | * @return external_function_parameters | |
5bcfd504 | 781 | * @since Moodle 2.5 |
d6731600 FM |
782 | */ |
783 | public static function search_contacts_parameters() { | |
784 | return new external_function_parameters( | |
785 | array( | |
786 | 'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'), | |
787 | 'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses', | |
788 | VALUE_DEFAULT, false) | |
789 | ) | |
790 | ); | |
791 | } | |
792 | ||
793 | /** | |
794 | * Search contacts. | |
795 | * | |
796 | * @param string $searchtext query string. | |
797 | * @param bool $onlymycourses limit the search to the user's courses only. | |
798 | * @return external_description | |
5bcfd504 | 799 | * @since Moodle 2.5 |
d6731600 FM |
800 | */ |
801 | public static function search_contacts($searchtext, $onlymycourses = false) { | |
d85bedf7 | 802 | global $CFG, $USER, $PAGE; |
11d83ab3 | 803 | require_once($CFG->dirroot . '/user/lib.php'); |
436bbf89 DM |
804 | |
805 | // Check if messaging is enabled. | |
806 | if (!$CFG->messaging) { | |
807 | throw new moodle_exception('disabled', 'message'); | |
808 | } | |
809 | ||
d6731600 FM |
810 | require_once($CFG->libdir . '/enrollib.php'); |
811 | ||
812 | $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses); | |
813 | $params = self::validate_parameters(self::search_contacts_parameters(), $params); | |
814 | ||
815 | // Extra validation, we do not allow empty queries. | |
816 | if ($params['searchtext'] === '') { | |
817 | throw new moodle_exception('querystringcannotbeempty'); | |
818 | } | |
819 | ||
820 | $courseids = array(); | |
821 | if ($params['onlymycourses']) { | |
822 | $mycourses = enrol_get_my_courses(array('id')); | |
823 | foreach ($mycourses as $mycourse) { | |
824 | $courseids[] = $mycourse->id; | |
825 | } | |
826 | } else { | |
827 | $courseids[] = SITEID; | |
828 | } | |
829 | ||
830 | // Retrieving the users matching the query. | |
831 | $users = message_search_users($courseids, $params['searchtext']); | |
832 | $results = array(); | |
833 | foreach ($users as $user) { | |
834 | $results[$user->id] = $user; | |
835 | } | |
836 | ||
837 | // Reorganising information. | |
838 | foreach ($results as &$user) { | |
839 | $newuser = array( | |
840 | 'id' => $user->id, | |
841 | 'fullname' => fullname($user) | |
842 | ); | |
843 | ||
844 | // Avoid undefined property notice as phone not specified. | |
845 | $user->phone1 = null; | |
846 | $user->phone2 = null; | |
847 | ||
d85bedf7 JL |
848 | $userpicture = new user_picture($user); |
849 | $userpicture->size = 1; // Size f1. | |
850 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
851 | $userpicture->size = 0; // Size f2. | |
852 | $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false); | |
d6731600 FM |
853 | |
854 | $user = $newuser; | |
855 | } | |
856 | ||
857 | return $results; | |
858 | } | |
859 | ||
860 | /** | |
861 | * Search contacts return description. | |
862 | * | |
863 | * @return external_description | |
5bcfd504 | 864 | * @since Moodle 2.5 |
d6731600 FM |
865 | */ |
866 | public static function search_contacts_returns() { | |
867 | return new external_multiple_structure( | |
868 | new external_single_structure( | |
869 | array( | |
870 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
871 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
872 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
873 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL) | |
874 | ) | |
875 | ), | |
876 | 'List of contacts' | |
877 | ); | |
878 | } | |
aff9da17 JL |
879 | |
880 | /** | |
881 | * Get messages parameters description. | |
882 | * | |
883 | * @return external_function_parameters | |
193edf7f | 884 | * @since 2.8 |
aff9da17 JL |
885 | */ |
886 | public static function get_messages_parameters() { | |
887 | return new external_function_parameters( | |
888 | array( | |
6ff4464b | 889 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), |
127ef540 SH |
890 | 'useridfrom' => new external_value( |
891 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
892 | VALUE_DEFAULT, 0), | |
893 | 'type' => new external_value( | |
894 | PARAM_ALPHA, 'type of message to return, expected values are: notifications, conversations and both', | |
895 | VALUE_DEFAULT, 'both'), | |
6ff4464b | 896 | 'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true), |
127ef540 SH |
897 | 'newestfirst' => new external_value( |
898 | PARAM_BOOL, 'true for ordering by newest first, false for oldest first', | |
899 | VALUE_DEFAULT, true), | |
aff9da17 | 900 | 'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0), |
127ef540 SH |
901 | 'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0) |
902 | ) | |
aff9da17 JL |
903 | ); |
904 | } | |
905 | ||
906 | /** | |
907 | * Get messages function implementation. | |
127ef540 SH |
908 | * |
909 | * @since 2.8 | |
910 | * @throws invalid_parameter_exception | |
911 | * @throws moodle_exception | |
6ff4464b JL |
912 | * @param int $useridto the user id who received the message |
913 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
193edf7f | 914 | * @param string $type type of message to return, expected values: notifications, conversations and both |
aff9da17 | 915 | * @param bool $read true for retreiving read messages, false for unread |
6ff4464b | 916 | * @param bool $newestfirst true for ordering by newest first, false for oldest first |
aff9da17 JL |
917 | * @param int $limitfrom limit from |
918 | * @param int $limitnum limit num | |
919 | * @return external_description | |
aff9da17 | 920 | */ |
193edf7f | 921 | public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, |
aff9da17 | 922 | $newestfirst = true, $limitfrom = 0, $limitnum = 0) { |
127ef540 | 923 | global $CFG, $USER; |
aff9da17 JL |
924 | |
925 | $warnings = array(); | |
926 | ||
927 | $params = array( | |
928 | 'useridto' => $useridto, | |
6ff4464b | 929 | 'useridfrom' => $useridfrom, |
aff9da17 JL |
930 | 'type' => $type, |
931 | 'read' => $read, | |
aff9da17 JL |
932 | 'newestfirst' => $newestfirst, |
933 | 'limitfrom' => $limitfrom, | |
934 | 'limitnum' => $limitnum | |
935 | ); | |
936 | ||
937 | $params = self::validate_parameters(self::get_messages_parameters(), $params); | |
938 | ||
939 | $context = context_system::instance(); | |
940 | self::validate_context($context); | |
941 | ||
942 | $useridto = $params['useridto']; | |
6ff4464b | 943 | $useridfrom = $params['useridfrom']; |
aff9da17 JL |
944 | $type = $params['type']; |
945 | $read = $params['read']; | |
aff9da17 JL |
946 | $newestfirst = $params['newestfirst']; |
947 | $limitfrom = $params['limitfrom']; | |
948 | $limitnum = $params['limitnum']; | |
949 | ||
950 | $allowedvalues = array('notifications', 'conversations', 'both'); | |
951 | if (!in_array($type, $allowedvalues)) { | |
952 | throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . | |
953 | 'allowed values are: ' . implode(',', $allowedvalues)); | |
954 | } | |
955 | ||
956 | // Check if private messaging between users is allowed. | |
957 | if (empty($CFG->messaging)) { | |
958 | // If we are retreiving only conversations, and messaging is disabled, throw an exception. | |
aff9da17 JL |
959 | if ($type == "conversations") { |
960 | throw new moodle_exception('disabled', 'message'); | |
961 | } | |
962 | if ($type == "both") { | |
963 | $warning = array(); | |
964 | $warning['item'] = 'message'; | |
965 | $warning['itemid'] = $USER->id; | |
966 | $warning['warningcode'] = '1'; | |
967 | $warning['message'] = 'Private messages (conversations) are not enabled in this site. | |
968 | Only notifications will be returned'; | |
969 | $warnings[] = $warning; | |
970 | } | |
971 | } | |
972 | ||
973 | if (!empty($useridto)) { | |
6ff4464b JL |
974 | if (core_user::is_real_user($useridto)) { |
975 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
976 | } else { | |
977 | throw new moodle_exception('invaliduser'); | |
978 | } | |
aff9da17 JL |
979 | } |
980 | ||
981 | if (!empty($useridfrom)) { | |
982 | // We use get_user here because the from user can be the noreply or support user. | |
983 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
984 | } | |
985 | ||
986 | // Check if the current user is the sender/receiver or just a privileged user. | |
987 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
988 | !has_capability('moodle/site:readallmessages', $context)) { | |
989 | throw new moodle_exception('accessdenied', 'admin'); | |
990 | } | |
991 | ||
127ef540 | 992 | // Which type of messages to retrieve. |
193edf7f | 993 | $notifications = -1; |
aff9da17 | 994 | if ($type != 'both') { |
193edf7f | 995 | $notifications = ($type == 'notifications') ? 1 : 0; |
aff9da17 JL |
996 | } |
997 | ||
aff9da17 | 998 | $orderdirection = $newestfirst ? 'DESC' : 'ASC'; |
193edf7f | 999 | $sort = "mr.timecreated $orderdirection"; |
aff9da17 | 1000 | |
193edf7f | 1001 | if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) { |
aff9da17 JL |
1002 | $canviewfullname = has_capability('moodle/site:viewfullnames', $context); |
1003 | ||
1004 | // In some cases, we don't need to get the to/from user objects from the sql query. | |
1005 | $userfromfullname = ''; | |
1006 | $usertofullname = ''; | |
1007 | ||
1008 | // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there. | |
1009 | if (!empty($useridto)) { | |
1010 | $usertofullname = fullname($userto, $canviewfullname); | |
1011 | // The user from may or may not be filled. | |
1012 | if (!empty($useridfrom)) { | |
1013 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1014 | } | |
1015 | } else { | |
1016 | // If the useridto field is empty, the useridfrom must be filled. | |
1017 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
1018 | } | |
aff9da17 JL |
1019 | foreach ($messages as $mid => $message) { |
1020 | ||
ea21d637 JL |
1021 | // Do not return deleted messages. |
1022 | if (($useridto == $USER->id and $message->timeusertodeleted) or | |
1023 | ($useridfrom == $USER->id and $message->timeuserfromdeleted)) { | |
1024 | ||
1025 | unset($messages[$mid]); | |
1026 | continue; | |
1027 | } | |
1028 | ||
aff9da17 JL |
1029 | // We need to get the user from the query. |
1030 | if (empty($userfromfullname)) { | |
6ff4464b JL |
1031 | // Check for non-reply and support users. |
1032 | if (core_user::is_real_user($message->useridfrom)) { | |
127ef540 | 1033 | $user = new stdClass(); |
6ff4464b JL |
1034 | $user = username_load_fields_from_object($user, $message, 'userfrom'); |
1035 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1036 | } else { | |
1037 | $user = core_user::get_user($message->useridfrom); | |
1038 | $message->userfromfullname = fullname($user, $canviewfullname); | |
1039 | } | |
aff9da17 JL |
1040 | } else { |
1041 | $message->userfromfullname = $userfromfullname; | |
1042 | } | |
1043 | ||
1044 | // We need to get the user from the query. | |
1045 | if (empty($usertofullname)) { | |
127ef540 | 1046 | $user = new stdClass(); |
aff9da17 JL |
1047 | $user = username_load_fields_from_object($user, $message, 'userto'); |
1048 | $message->usertofullname = fullname($user, $canviewfullname); | |
1049 | } else { | |
1050 | $message->usertofullname = $usertofullname; | |
1051 | } | |
1052 | ||
193edf7f | 1053 | // This field is only available in the message_read table. |
aff9da17 JL |
1054 | if (!isset($message->timeread)) { |
1055 | $message->timeread = 0; | |
1056 | } | |
1057 | ||
aff9da17 | 1058 | $message->text = message_format_message_text($message); |
aff9da17 JL |
1059 | $messages[$mid] = (array) $message; |
1060 | } | |
1061 | } | |
1062 | ||
1063 | $results = array( | |
1064 | 'messages' => $messages, | |
1065 | 'warnings' => $warnings | |
1066 | ); | |
1067 | ||
1068 | return $results; | |
1069 | } | |
1070 | ||
1071 | /** | |
1072 | * Get messages return description. | |
1073 | * | |
6ff4464b | 1074 | * @return external_single_structure |
193edf7f | 1075 | * @since 2.8 |
aff9da17 JL |
1076 | */ |
1077 | public static function get_messages_returns() { | |
1078 | return new external_single_structure( | |
1079 | array( | |
1080 | 'messages' => new external_multiple_structure( | |
1081 | new external_single_structure( | |
1082 | array( | |
193edf7f | 1083 | 'id' => new external_value(PARAM_INT, 'Message id'), |
aff9da17 JL |
1084 | 'useridfrom' => new external_value(PARAM_INT, 'User from id'), |
1085 | 'useridto' => new external_value(PARAM_INT, 'User to id'), | |
1086 | 'subject' => new external_value(PARAM_TEXT, 'The message subject'), | |
1087 | 'text' => new external_value(PARAM_RAW, 'The message text formated'), | |
1088 | 'fullmessage' => new external_value(PARAM_RAW, 'The message'), | |
193edf7f | 1089 | 'fullmessageformat' => new external_format_value('fullmessage'), |
aff9da17 JL |
1090 | 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'), |
1091 | 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'), | |
1092 | 'notification' => new external_value(PARAM_INT, 'Is a notification?'), | |
1093 | 'contexturl' => new external_value(PARAM_RAW, 'Context URL'), | |
1094 | 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'), | |
1095 | 'timecreated' => new external_value(PARAM_INT, 'Time created'), | |
1096 | 'timeread' => new external_value(PARAM_INT, 'Time read'), | |
1097 | 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'), | |
1098 | 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name') | |
1099 | ), 'message' | |
1100 | ) | |
1101 | ), | |
1102 | 'warnings' => new external_warnings() | |
1103 | ) | |
1104 | ); | |
1105 | } | |
1106 | ||
60ab2e1b JL |
1107 | /** |
1108 | * Get blocked users parameters description. | |
1109 | * | |
1110 | * @return external_function_parameters | |
1111 | * @since 2.9 | |
1112 | */ | |
1113 | public static function get_blocked_users_parameters() { | |
1114 | return new external_function_parameters( | |
1115 | array( | |
1116 | 'userid' => new external_value(PARAM_INT, | |
1117 | 'the user whose blocked users we want to retrieve', | |
1118 | VALUE_REQUIRED), | |
1119 | ) | |
1120 | ); | |
1121 | } | |
1122 | ||
1123 | /** | |
1124 | * Retrieve a list of users blocked | |
1125 | * | |
1126 | * @param int $userid the user whose blocked users we want to retrieve | |
1127 | * @return external_description | |
1128 | * @since 2.9 | |
1129 | */ | |
1130 | public static function get_blocked_users($userid) { | |
d85bedf7 | 1131 | global $CFG, $USER, $PAGE; |
60ab2e1b JL |
1132 | |
1133 | // Warnings array, it can be empty at the end but is mandatory. | |
1134 | $warnings = array(); | |
1135 | ||
1136 | // Validate params. | |
1137 | $params = array( | |
1138 | 'userid' => $userid | |
1139 | ); | |
1140 | $params = self::validate_parameters(self::get_blocked_users_parameters(), $params); | |
1141 | $userid = $params['userid']; | |
1142 | ||
1143 | // Validate context. | |
1144 | $context = context_system::instance(); | |
1145 | self::validate_context($context); | |
1146 | ||
1147 | // Check if private messaging between users is allowed. | |
1148 | if (empty($CFG->messaging)) { | |
1149 | throw new moodle_exception('disabled', 'message'); | |
1150 | } | |
1151 | ||
4485f7c5 JL |
1152 | $user = core_user::get_user($userid, '*', MUST_EXIST); |
1153 | core_user::require_active_user($user); | |
60ab2e1b JL |
1154 | |
1155 | // Check if we have permissions for retrieve the information. | |
1156 | if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) { | |
1157 | throw new moodle_exception('accessdenied', 'admin'); | |
1158 | } | |
1159 | ||
1160 | // Now, we can get safely all the blocked users. | |
1161 | $users = message_get_blocked_users($user); | |
1162 | ||
1163 | $blockedusers = array(); | |
1164 | foreach ($users as $user) { | |
1165 | $newuser = array( | |
1166 | 'id' => $user->id, | |
1167 | 'fullname' => fullname($user), | |
1168 | ); | |
0b074e88 | 1169 | |
d85bedf7 JL |
1170 | $userpicture = new user_picture($user); |
1171 | $userpicture->size = 1; // Size f1. | |
1172 | $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false); | |
60ab2e1b JL |
1173 | |
1174 | $blockedusers[] = $newuser; | |
1175 | } | |
1176 | ||
1177 | $results = array( | |
1178 | 'users' => $blockedusers, | |
1179 | 'warnings' => $warnings | |
1180 | ); | |
1181 | return $results; | |
1182 | } | |
1183 | ||
1184 | /** | |
1185 | * Get blocked users return description. | |
1186 | * | |
1187 | * @return external_single_structure | |
1188 | * @since 2.9 | |
1189 | */ | |
1190 | public static function get_blocked_users_returns() { | |
1191 | return new external_single_structure( | |
1192 | array( | |
1193 | 'users' => new external_multiple_structure( | |
1194 | new external_single_structure( | |
1195 | array( | |
1196 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
1197 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
1198 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL) | |
1199 | ) | |
1200 | ), | |
1201 | 'List of blocked users' | |
1202 | ), | |
1203 | 'warnings' => new external_warnings() | |
1204 | ) | |
1205 | ); | |
1206 | } | |
1207 | ||
31c474da JL |
1208 | /** |
1209 | * Returns description of method parameters | |
1210 | * | |
1211 | * @return external_function_parameters | |
1212 | * @since 2.9 | |
1213 | */ | |
1214 | public static function mark_message_read_parameters() { | |
1215 | return new external_function_parameters( | |
1216 | array( | |
1217 | 'messageid' => new external_value(PARAM_INT, 'id of the message (in the message table)'), | |
1218 | 'timeread' => new external_value(PARAM_INT, 'timestamp for when the message should be marked read') | |
1219 | ) | |
1220 | ); | |
1221 | } | |
1222 | ||
1223 | /** | |
1224 | * Mark a single message as read, trigger message_viewed event | |
1225 | * | |
1226 | * @param int $messageid id of the message (in the message table) | |
1227 | * @param int $timeread timestamp for when the message should be marked read | |
1228 | * @return external_description | |
1229 | * @throws invalid_parameter_exception | |
1230 | * @throws moodle_exception | |
1231 | * @since 2.9 | |
1232 | */ | |
1233 | public static function mark_message_read($messageid, $timeread) { | |
1234 | global $CFG, $DB, $USER; | |
31c474da JL |
1235 | |
1236 | // Check if private messaging between users is allowed. | |
1237 | if (empty($CFG->messaging)) { | |
1238 | throw new moodle_exception('disabled', 'message'); | |
1239 | } | |
1240 | ||
1241 | // Warnings array, it can be empty at the end but is mandatory. | |
1242 | $warnings = array(); | |
1243 | ||
1244 | // Validate params. | |
1245 | $params = array( | |
1246 | 'messageid' => $messageid, | |
1247 | 'timeread' => $timeread | |
1248 | ); | |
1249 | $params = self::validate_parameters(self::mark_message_read_parameters(), $params); | |
1250 | ||
1251 | // Validate context. | |
1252 | $context = context_system::instance(); | |
1253 | self::validate_context($context); | |
1254 | ||
1255 | $message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST); | |
1256 | ||
1257 | if ($message->useridto != $USER->id) { | |
1258 | throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read'); | |
1259 | } | |
1260 | ||
1261 | $messageid = message_mark_message_read($message, $params['timeread']); | |
1262 | ||
1263 | $results = array( | |
1264 | 'messageid' => $messageid, | |
1265 | 'warnings' => $warnings | |
1266 | ); | |
1267 | return $results; | |
1268 | } | |
1269 | ||
1270 | /** | |
1271 | * Returns description of method result value | |
1272 | * | |
1273 | * @return external_description | |
1274 | * @since 2.9 | |
1275 | */ | |
1276 | public static function mark_message_read_returns() { | |
1277 | return new external_single_structure( | |
1278 | array( | |
1279 | 'messageid' => new external_value(PARAM_INT, 'the id of the message in the message_read table'), | |
1280 | 'warnings' => new external_warnings() | |
1281 | ) | |
1282 | ); | |
1283 | } | |
1284 | ||
419b1128 JL |
1285 | /** |
1286 | * Returns description of method parameters | |
1287 | * | |
1288 | * @return external_function_parameters | |
1289 | * @since 3.1 | |
1290 | */ | |
1291 | public static function delete_message_parameters() { | |
1292 | return new external_function_parameters( | |
1293 | array( | |
1294 | 'messageid' => new external_value(PARAM_INT, 'The message id'), | |
1295 | 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the message for'), | |
1296 | 'read' => new external_value(PARAM_BOOL, 'If is a message read', VALUE_DEFAULT, true) | |
1297 | ) | |
1298 | ); | |
1299 | } | |
1300 | ||
1301 | /** | |
1302 | * Deletes a message | |
1303 | * | |
1304 | * @param int $messageid the message id | |
1305 | * @param int $userid the user id of who we want to delete the message for | |
1306 | * @param bool $read if is a message read (default to true) | |
1307 | * @return external_description | |
1308 | * @throws moodle_exception | |
1309 | * @since 3.1 | |
1310 | */ | |
1311 | public static function delete_message($messageid, $userid, $read = true) { | |
1312 | global $CFG, $DB; | |
419b1128 JL |
1313 | |
1314 | // Check if private messaging between users is allowed. | |
1315 | if (empty($CFG->messaging)) { | |
1316 | throw new moodle_exception('disabled', 'message'); | |
1317 | } | |
1318 | ||
1319 | // Warnings array, it can be empty at the end but is mandatory. | |
1320 | $warnings = array(); | |
1321 | ||
1322 | // Validate params. | |
1323 | $params = array( | |
1324 | 'messageid' => $messageid, | |
1325 | 'userid' => $userid, | |
1326 | 'read' => $read | |
1327 | ); | |
1328 | $params = self::validate_parameters(self::delete_message_parameters(), $params); | |
1329 | ||
1330 | // Validate context. | |
1331 | $context = context_system::instance(); | |
1332 | self::validate_context($context); | |
1333 | ||
1334 | $messagestable = $params['read'] ? 'message_read' : 'message'; | |
1335 | $message = $DB->get_record($messagestable, array('id' => $params['messageid']), '*', MUST_EXIST); | |
1336 | ||
1337 | $user = core_user::get_user($params['userid'], '*', MUST_EXIST); | |
1338 | core_user::require_active_user($user); | |
1339 | ||
1340 | $status = false; | |
1341 | if (message_can_delete_message($message, $user->id)) { | |
1342 | $status = message_delete_message($message, $user->id);; | |
1343 | } else { | |
1344 | throw new moodle_exception('You do not have permission to delete this message'); | |
1345 | } | |
1346 | ||
1347 | $results = array( | |
1348 | 'status' => $status, | |
1349 | 'warnings' => $warnings | |
1350 | ); | |
1351 | return $results; | |
1352 | } | |
1353 | ||
1354 | /** | |
1355 | * Returns description of method result value | |
1356 | * | |
1357 | * @return external_description | |
1358 | * @since 3.1 | |
1359 | */ | |
1360 | public static function delete_message_returns() { | |
1361 | return new external_single_structure( | |
1362 | array( | |
1363 | 'status' => new external_value(PARAM_BOOL, 'True if the message was deleted, false otherwise'), | |
1364 | 'warnings' => new external_warnings() | |
1365 | ) | |
1366 | ); | |
1367 | } | |
1368 | ||
a623b6b8 | 1369 | } |