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 JM |
27 | require_once("$CFG->libdir/externallib.php"); |
28 | ||
5d1017e1 | 29 | /** |
4615817d | 30 | * Message external functions |
6fbd60ef AD |
31 | * |
32 | * @package core_message | |
4615817d JM |
33 | * @category external |
34 | * @copyright 2011 Jerome Mouneyrac | |
75e4f98c | 35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
4615817d | 36 | * @since Moodle 2.2 |
5d1017e1 JM |
37 | */ |
38 | class core_message_external extends external_api { | |
a623b6b8 JM |
39 | |
40 | /** | |
41 | * Returns description of method parameters | |
4615817d | 42 | * |
a623b6b8 | 43 | * @return external_function_parameters |
4615817d | 44 | * @since Moodle 2.2 |
a623b6b8 | 45 | */ |
5d1017e1 | 46 | public static function send_instant_messages_parameters() { |
a623b6b8 JM |
47 | return new external_function_parameters( |
48 | array( | |
49 | 'messages' => new external_multiple_structure( | |
50 | new external_single_structure( | |
51 | array( | |
52 | 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'), | |
93ce0e82 JM |
53 | 'text' => new external_value(PARAM_RAW, 'the text of the message'), |
54 | 'textformat' => new external_format_value('text', VALUE_DEFAULT), | |
a623b6b8 JM |
55 | '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), |
56 | ) | |
57 | ) | |
58 | ) | |
59 | ) | |
60 | ); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Send private messages from the current USER to other users | |
65 | * | |
4615817d JM |
66 | * @param array $messages An array of message to send. |
67 | * @return array | |
68 | * @since Moodle 2.2 | |
a623b6b8 | 69 | */ |
5d1017e1 | 70 | public static function send_instant_messages($messages = array()) { |
a623b6b8 JM |
71 | global $CFG, $USER, $DB; |
72 | require_once($CFG->dirroot . "/message/lib.php"); | |
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; | |
140 | $errormessage = get_string('userisblockingyounoncontact', 'message'); | |
a623b6b8 JM |
141 | } |
142 | ||
143 | //now we can send the message (at least try) | |
144 | if ($success) { | |
4615817d | 145 | //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object |
93ce0e82 JM |
146 | $success = message_post_message($USER, $tousers[$message['touserid']], |
147 | $message['text'], external_validate_format($message['textformat'])); | |
a623b6b8 JM |
148 | } |
149 | ||
150 | //build the resultmsg | |
151 | if (isset($message['clientmsgid'])) { | |
78736e5d | 152 | $resultmsg['clientmsgid'] = $message['clientmsgid']; |
a623b6b8 JM |
153 | } |
154 | if ($success) { | |
155 | $resultmsg['msgid'] = $success; | |
156 | } else { | |
93ce0e82 JM |
157 | // WARNINGS: for backward compatibility we return this errormessage. |
158 | // We should have thrown exceptions as these errors prevent results to be returned. | |
159 | // See http://docs.moodle.org/dev/Errors_handling_in_web_services#When_to_send_a_warning_on_the_server_side . | |
a623b6b8 JM |
160 | $resultmsg['msgid'] = -1; |
161 | $resultmsg['errormessage'] = $errormessage; | |
162 | } | |
163 | ||
164 | $resultmessages[] = $resultmsg; | |
165 | } | |
166 | ||
167 | return $resultmessages; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Returns description of method result value | |
4615817d | 172 | * |
a623b6b8 | 173 | * @return external_description |
4615817d | 174 | * @since Moodle 2.2 |
a623b6b8 | 175 | */ |
5d1017e1 | 176 | public static function send_instant_messages_returns() { |
a623b6b8 JM |
177 | return new external_multiple_structure( |
178 | new external_single_structure( | |
179 | array( | |
78736e5d | 180 | '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 | 181 | 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL), |
78736e5d | 182 | 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL) |
a623b6b8 JM |
183 | ) |
184 | ) | |
185 | ); | |
186 | } | |
187 | ||
d6731600 FM |
188 | /** |
189 | * Create contacts parameters description. | |
190 | * | |
191 | * @return external_function_parameters | |
5bcfd504 | 192 | * @since Moodle 2.5 |
d6731600 FM |
193 | */ |
194 | public static function create_contacts_parameters() { | |
195 | return new external_function_parameters( | |
196 | array( | |
197 | 'userids' => new external_multiple_structure( | |
198 | new external_value(PARAM_INT, 'User ID'), | |
199 | 'List of user IDs' | |
200 | ) | |
201 | ) | |
202 | ); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Create contacts. | |
207 | * | |
208 | * @param array $userids array of user IDs. | |
209 | * @return external_description | |
5bcfd504 | 210 | * @since Moodle 2.5 |
d6731600 FM |
211 | */ |
212 | public static function create_contacts($userids) { | |
436bbf89 DM |
213 | global $CFG; |
214 | ||
215 | // Check if messaging is enabled. | |
216 | if (!$CFG->messaging) { | |
217 | throw new moodle_exception('disabled', 'message'); | |
218 | } | |
219 | ||
d6731600 FM |
220 | $params = array('userids' => $userids); |
221 | $params = self::validate_parameters(self::create_contacts_parameters(), $params); | |
222 | ||
223 | $warnings = array(); | |
224 | foreach ($params['userids'] as $id) { | |
225 | if (!message_add_contact($id)) { | |
226 | $warnings[] = array( | |
227 | 'item' => 'user', | |
228 | 'itemid' => $id, | |
229 | 'warningcode' => 'contactnotcreated', | |
230 | 'message' => 'The contact could not be created' | |
231 | ); | |
232 | } | |
233 | } | |
234 | return $warnings; | |
235 | } | |
236 | ||
237 | /** | |
238 | * Create contacts return description. | |
239 | * | |
240 | * @return external_description | |
5bcfd504 | 241 | * @since Moodle 2.5 |
d6731600 FM |
242 | */ |
243 | public static function create_contacts_returns() { | |
244 | return new external_warnings(); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Delete contacts parameters description. | |
249 | * | |
250 | * @return external_function_parameters | |
5bcfd504 | 251 | * @since Moodle 2.5 |
d6731600 FM |
252 | */ |
253 | public static function delete_contacts_parameters() { | |
254 | return new external_function_parameters( | |
255 | array( | |
256 | 'userids' => new external_multiple_structure( | |
257 | new external_value(PARAM_INT, 'User ID'), | |
258 | 'List of user IDs' | |
259 | ) | |
260 | ) | |
261 | ); | |
262 | } | |
263 | ||
264 | /** | |
265 | * Delete contacts. | |
266 | * | |
267 | * @param array $userids array of user IDs. | |
268 | * @return null | |
5bcfd504 | 269 | * @since Moodle 2.5 |
d6731600 FM |
270 | */ |
271 | public static function delete_contacts($userids) { | |
436bbf89 DM |
272 | global $CFG; |
273 | ||
274 | // Check if messaging is enabled. | |
275 | if (!$CFG->messaging) { | |
276 | throw new moodle_exception('disabled', 'message'); | |
277 | } | |
278 | ||
d6731600 FM |
279 | $params = array('userids' => $userids); |
280 | $params = self::validate_parameters(self::delete_contacts_parameters(), $params); | |
281 | ||
282 | foreach ($params['userids'] as $id) { | |
283 | message_remove_contact($id); | |
284 | } | |
285 | ||
286 | return null; | |
287 | } | |
288 | ||
289 | /** | |
290 | * Delete contacts return description. | |
291 | * | |
292 | * @return external_description | |
5bcfd504 | 293 | * @since Moodle 2.5 |
d6731600 FM |
294 | */ |
295 | public static function delete_contacts_returns() { | |
296 | return null; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Block contacts parameters description. | |
301 | * | |
302 | * @return external_function_parameters | |
5bcfd504 | 303 | * @since Moodle 2.5 |
d6731600 FM |
304 | */ |
305 | public static function block_contacts_parameters() { | |
306 | return new external_function_parameters( | |
307 | array( | |
308 | 'userids' => new external_multiple_structure( | |
309 | new external_value(PARAM_INT, 'User ID'), | |
310 | 'List of user IDs' | |
311 | ) | |
312 | ) | |
313 | ); | |
314 | } | |
315 | ||
316 | /** | |
317 | * Block contacts. | |
318 | * | |
319 | * @param array $userids array of user IDs. | |
320 | * @return external_description | |
5bcfd504 | 321 | * @since Moodle 2.5 |
d6731600 FM |
322 | */ |
323 | public static function block_contacts($userids) { | |
436bbf89 DM |
324 | global $CFG; |
325 | ||
326 | // Check if messaging is enabled. | |
327 | if (!$CFG->messaging) { | |
328 | throw new moodle_exception('disabled', 'message'); | |
329 | } | |
330 | ||
d6731600 FM |
331 | $params = array('userids' => $userids); |
332 | $params = self::validate_parameters(self::block_contacts_parameters(), $params); | |
333 | ||
334 | $warnings = array(); | |
335 | foreach ($params['userids'] as $id) { | |
336 | if (!message_block_contact($id)) { | |
337 | $warnings[] = array( | |
338 | 'item' => 'user', | |
339 | 'itemid' => $id, | |
340 | 'warningcode' => 'contactnotblocked', | |
341 | 'message' => 'The contact could not be blocked' | |
342 | ); | |
343 | } | |
344 | } | |
345 | return $warnings; | |
346 | } | |
347 | ||
348 | /** | |
349 | * Block contacts return description. | |
350 | * | |
351 | * @return external_description | |
5bcfd504 | 352 | * @since Moodle 2.5 |
d6731600 FM |
353 | */ |
354 | public static function block_contacts_returns() { | |
355 | return new external_warnings(); | |
356 | } | |
357 | ||
358 | /** | |
359 | * Unblock contacts parameters description. | |
360 | * | |
361 | * @return external_function_parameters | |
5bcfd504 | 362 | * @since Moodle 2.5 |
d6731600 FM |
363 | */ |
364 | public static function unblock_contacts_parameters() { | |
365 | return new external_function_parameters( | |
366 | array( | |
367 | 'userids' => new external_multiple_structure( | |
368 | new external_value(PARAM_INT, 'User ID'), | |
369 | 'List of user IDs' | |
370 | ) | |
371 | ) | |
372 | ); | |
373 | } | |
374 | ||
375 | /** | |
376 | * Unblock contacts. | |
377 | * | |
378 | * @param array $userids array of user IDs. | |
379 | * @return null | |
5bcfd504 | 380 | * @since Moodle 2.5 |
d6731600 FM |
381 | */ |
382 | public static function unblock_contacts($userids) { | |
436bbf89 DM |
383 | global $CFG; |
384 | ||
385 | // Check if messaging is enabled. | |
386 | if (!$CFG->messaging) { | |
387 | throw new moodle_exception('disabled', 'message'); | |
388 | } | |
389 | ||
d6731600 FM |
390 | $params = array('userids' => $userids); |
391 | $params = self::validate_parameters(self::unblock_contacts_parameters(), $params); | |
392 | ||
393 | foreach ($params['userids'] as $id) { | |
394 | message_unblock_contact($id); | |
395 | } | |
396 | ||
397 | return null; | |
398 | } | |
399 | ||
400 | /** | |
401 | * Unblock contacts return description. | |
402 | * | |
403 | * @return external_description | |
5bcfd504 | 404 | * @since Moodle 2.5 |
d6731600 FM |
405 | */ |
406 | public static function unblock_contacts_returns() { | |
407 | return null; | |
408 | } | |
409 | ||
410 | /** | |
411 | * Get contacts parameters description. | |
412 | * | |
413 | * @return external_function_parameters | |
5bcfd504 | 414 | * @since Moodle 2.5 |
d6731600 FM |
415 | */ |
416 | public static function get_contacts_parameters() { | |
417 | return new external_function_parameters(array()); | |
418 | } | |
419 | ||
420 | /** | |
421 | * Get contacts. | |
422 | * | |
423 | * @param array $userids array of user IDs. | |
424 | * @return external_description | |
5bcfd504 | 425 | * @since Moodle 2.5 |
d6731600 FM |
426 | */ |
427 | public static function get_contacts() { | |
428 | global $CFG; | |
436bbf89 DM |
429 | |
430 | // Check if messaging is enabled. | |
431 | if (!$CFG->messaging) { | |
432 | throw new moodle_exception('disabled', 'message'); | |
433 | } | |
434 | ||
d6731600 FM |
435 | require_once($CFG->dirroot . '/user/lib.php'); |
436 | ||
437 | list($online, $offline, $strangers) = message_get_contacts(); | |
438 | $allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers); | |
439 | foreach ($allcontacts as $mode => $contacts) { | |
440 | foreach ($contacts as $key => $contact) { | |
441 | $newcontact = array( | |
442 | 'id' => $contact->id, | |
443 | 'fullname' => fullname($contact), | |
444 | 'unread' => $contact->messagecount | |
445 | ); | |
446 | ||
447 | // Try to get the user picture, but sometimes this method can return null. | |
448 | $userdetails = user_get_user_details($contact, null, array('profileimageurl', 'profileimageurlsmall')); | |
449 | if (!empty($userdetails)) { | |
450 | $newcontact['profileimageurl'] = $userdetails['profileimageurl']; | |
451 | $newcontact['profileimageurlsmall'] = $userdetails['profileimageurlsmall']; | |
452 | } | |
453 | ||
454 | $allcontacts[$mode][$key] = $newcontact; | |
455 | } | |
456 | } | |
457 | return $allcontacts; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Get contacts return description. | |
462 | * | |
463 | * @return external_description | |
5bcfd504 | 464 | * @since Moodle 2.5 |
d6731600 FM |
465 | */ |
466 | public static function get_contacts_returns() { | |
467 | return new external_single_structure( | |
468 | array( | |
469 | 'online' => new external_multiple_structure( | |
470 | new external_single_structure( | |
471 | array( | |
472 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
473 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
474 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
475 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
476 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
477 | ) | |
478 | ), | |
479 | 'List of online contacts' | |
480 | ), | |
481 | 'offline' => new external_multiple_structure( | |
482 | new external_single_structure( | |
483 | array( | |
484 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
485 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
486 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
487 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
488 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
489 | ) | |
490 | ), | |
491 | 'List of offline contacts' | |
492 | ), | |
493 | 'strangers' => new external_multiple_structure( | |
494 | new external_single_structure( | |
495 | array( | |
496 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
497 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
498 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
499 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL), | |
500 | 'unread' => new external_value(PARAM_INT, 'Unread message count') | |
501 | ) | |
502 | ), | |
503 | 'List of users that are not in the user\'s contact list but have sent a message' | |
504 | ) | |
505 | ) | |
506 | ); | |
507 | } | |
508 | ||
509 | /** | |
510 | * Search contacts parameters description. | |
511 | * | |
512 | * @return external_function_parameters | |
5bcfd504 | 513 | * @since Moodle 2.5 |
d6731600 FM |
514 | */ |
515 | public static function search_contacts_parameters() { | |
516 | return new external_function_parameters( | |
517 | array( | |
518 | 'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'), | |
519 | 'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses', | |
520 | VALUE_DEFAULT, false) | |
521 | ) | |
522 | ); | |
523 | } | |
524 | ||
525 | /** | |
526 | * Search contacts. | |
527 | * | |
528 | * @param string $searchtext query string. | |
529 | * @param bool $onlymycourses limit the search to the user's courses only. | |
530 | * @return external_description | |
5bcfd504 | 531 | * @since Moodle 2.5 |
d6731600 FM |
532 | */ |
533 | public static function search_contacts($searchtext, $onlymycourses = false) { | |
534 | global $CFG, $USER; | |
436bbf89 DM |
535 | |
536 | // Check if messaging is enabled. | |
537 | if (!$CFG->messaging) { | |
538 | throw new moodle_exception('disabled', 'message'); | |
539 | } | |
540 | ||
d6731600 FM |
541 | require_once($CFG->libdir . '/enrollib.php'); |
542 | ||
543 | $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses); | |
544 | $params = self::validate_parameters(self::search_contacts_parameters(), $params); | |
545 | ||
546 | // Extra validation, we do not allow empty queries. | |
547 | if ($params['searchtext'] === '') { | |
548 | throw new moodle_exception('querystringcannotbeempty'); | |
549 | } | |
550 | ||
551 | $courseids = array(); | |
552 | if ($params['onlymycourses']) { | |
553 | $mycourses = enrol_get_my_courses(array('id')); | |
554 | foreach ($mycourses as $mycourse) { | |
555 | $courseids[] = $mycourse->id; | |
556 | } | |
557 | } else { | |
558 | $courseids[] = SITEID; | |
559 | } | |
560 | ||
561 | // Retrieving the users matching the query. | |
562 | $users = message_search_users($courseids, $params['searchtext']); | |
563 | $results = array(); | |
564 | foreach ($users as $user) { | |
565 | $results[$user->id] = $user; | |
566 | } | |
567 | ||
568 | // Reorganising information. | |
569 | foreach ($results as &$user) { | |
570 | $newuser = array( | |
571 | 'id' => $user->id, | |
572 | 'fullname' => fullname($user) | |
573 | ); | |
574 | ||
575 | // Avoid undefined property notice as phone not specified. | |
576 | $user->phone1 = null; | |
577 | $user->phone2 = null; | |
578 | ||
579 | // Try to get the user picture, but sometimes this method can return null. | |
580 | $userdetails = user_get_user_details($user, null, array('profileimageurl', 'profileimageurlsmall')); | |
581 | if (!empty($userdetails)) { | |
582 | $newuser['profileimageurl'] = $userdetails['profileimageurl']; | |
583 | $newuser['profileimageurlsmall'] = $userdetails['profileimageurlsmall']; | |
584 | } | |
585 | ||
586 | $user = $newuser; | |
587 | } | |
588 | ||
589 | return $results; | |
590 | } | |
591 | ||
592 | /** | |
593 | * Search contacts return description. | |
594 | * | |
595 | * @return external_description | |
5bcfd504 | 596 | * @since Moodle 2.5 |
d6731600 FM |
597 | */ |
598 | public static function search_contacts_returns() { | |
599 | return new external_multiple_structure( | |
600 | new external_single_structure( | |
601 | array( | |
602 | 'id' => new external_value(PARAM_INT, 'User ID'), | |
603 | 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'), | |
604 | 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL), | |
605 | 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL) | |
606 | ) | |
607 | ), | |
608 | 'List of contacts' | |
609 | ); | |
610 | } | |
aff9da17 JL |
611 | |
612 | /** | |
613 | * Get messages parameters description. | |
614 | * | |
615 | * @return external_function_parameters | |
193edf7f | 616 | * @since 2.8 |
aff9da17 JL |
617 | */ |
618 | public static function get_messages_parameters() { | |
619 | return new external_function_parameters( | |
620 | array( | |
6ff4464b | 621 | 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED), |
127ef540 SH |
622 | 'useridfrom' => new external_value( |
623 | PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user', | |
624 | VALUE_DEFAULT, 0), | |
625 | 'type' => new external_value( | |
626 | PARAM_ALPHA, 'type of message to return, expected values are: notifications, conversations and both', | |
627 | VALUE_DEFAULT, 'both'), | |
6ff4464b | 628 | 'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true), |
127ef540 SH |
629 | 'newestfirst' => new external_value( |
630 | PARAM_BOOL, 'true for ordering by newest first, false for oldest first', | |
631 | VALUE_DEFAULT, true), | |
aff9da17 | 632 | 'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0), |
127ef540 SH |
633 | 'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0) |
634 | ) | |
aff9da17 JL |
635 | ); |
636 | } | |
637 | ||
638 | /** | |
639 | * Get messages function implementation. | |
127ef540 SH |
640 | * |
641 | * @since 2.8 | |
642 | * @throws invalid_parameter_exception | |
643 | * @throws moodle_exception | |
6ff4464b JL |
644 | * @param int $useridto the user id who received the message |
645 | * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user | |
193edf7f | 646 | * @param string $type type of message to return, expected values: notifications, conversations and both |
aff9da17 | 647 | * @param bool $read true for retreiving read messages, false for unread |
6ff4464b | 648 | * @param bool $newestfirst true for ordering by newest first, false for oldest first |
aff9da17 JL |
649 | * @param int $limitfrom limit from |
650 | * @param int $limitnum limit num | |
651 | * @return external_description | |
aff9da17 | 652 | */ |
193edf7f | 653 | public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, |
aff9da17 | 654 | $newestfirst = true, $limitfrom = 0, $limitnum = 0) { |
127ef540 | 655 | global $CFG, $USER; |
aff9da17 JL |
656 | require_once($CFG->dirroot . "/message/lib.php"); |
657 | ||
658 | $warnings = array(); | |
659 | ||
660 | $params = array( | |
661 | 'useridto' => $useridto, | |
6ff4464b | 662 | 'useridfrom' => $useridfrom, |
aff9da17 JL |
663 | 'type' => $type, |
664 | 'read' => $read, | |
aff9da17 JL |
665 | 'newestfirst' => $newestfirst, |
666 | 'limitfrom' => $limitfrom, | |
667 | 'limitnum' => $limitnum | |
668 | ); | |
669 | ||
670 | $params = self::validate_parameters(self::get_messages_parameters(), $params); | |
671 | ||
672 | $context = context_system::instance(); | |
673 | self::validate_context($context); | |
674 | ||
675 | $useridto = $params['useridto']; | |
6ff4464b | 676 | $useridfrom = $params['useridfrom']; |
aff9da17 JL |
677 | $type = $params['type']; |
678 | $read = $params['read']; | |
aff9da17 JL |
679 | $newestfirst = $params['newestfirst']; |
680 | $limitfrom = $params['limitfrom']; | |
681 | $limitnum = $params['limitnum']; | |
682 | ||
683 | $allowedvalues = array('notifications', 'conversations', 'both'); | |
684 | if (!in_array($type, $allowedvalues)) { | |
685 | throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . | |
686 | 'allowed values are: ' . implode(',', $allowedvalues)); | |
687 | } | |
688 | ||
689 | // Check if private messaging between users is allowed. | |
690 | if (empty($CFG->messaging)) { | |
691 | // If we are retreiving only conversations, and messaging is disabled, throw an exception. | |
aff9da17 JL |
692 | if ($type == "conversations") { |
693 | throw new moodle_exception('disabled', 'message'); | |
694 | } | |
695 | if ($type == "both") { | |
696 | $warning = array(); | |
697 | $warning['item'] = 'message'; | |
698 | $warning['itemid'] = $USER->id; | |
699 | $warning['warningcode'] = '1'; | |
700 | $warning['message'] = 'Private messages (conversations) are not enabled in this site. | |
701 | Only notifications will be returned'; | |
702 | $warnings[] = $warning; | |
703 | } | |
704 | } | |
705 | ||
706 | if (!empty($useridto)) { | |
6ff4464b JL |
707 | if (core_user::is_real_user($useridto)) { |
708 | $userto = core_user::get_user($useridto, '*', MUST_EXIST); | |
709 | } else { | |
710 | throw new moodle_exception('invaliduser'); | |
711 | } | |
aff9da17 JL |
712 | } |
713 | ||
714 | if (!empty($useridfrom)) { | |
715 | // We use get_user here because the from user can be the noreply or support user. | |
716 | $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST); | |
717 | } | |
718 | ||
719 | // Check if the current user is the sender/receiver or just a privileged user. | |
720 | if ($useridto != $USER->id and $useridfrom != $USER->id and | |
721 | !has_capability('moodle/site:readallmessages', $context)) { | |
722 | throw new moodle_exception('accessdenied', 'admin'); | |
723 | } | |
724 | ||
127ef540 | 725 | // Which type of messages to retrieve. |
193edf7f | 726 | $notifications = -1; |
aff9da17 | 727 | if ($type != 'both') { |
193edf7f | 728 | $notifications = ($type == 'notifications') ? 1 : 0; |
aff9da17 JL |
729 | } |
730 | ||
aff9da17 | 731 | $orderdirection = $newestfirst ? 'DESC' : 'ASC'; |
193edf7f | 732 | $sort = "mr.timecreated $orderdirection"; |
aff9da17 | 733 | |
193edf7f | 734 | if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) { |
aff9da17 JL |
735 | $canviewfullname = has_capability('moodle/site:viewfullnames', $context); |
736 | ||
737 | // In some cases, we don't need to get the to/from user objects from the sql query. | |
738 | $userfromfullname = ''; | |
739 | $usertofullname = ''; | |
740 | ||
741 | // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there. | |
742 | if (!empty($useridto)) { | |
743 | $usertofullname = fullname($userto, $canviewfullname); | |
744 | // The user from may or may not be filled. | |
745 | if (!empty($useridfrom)) { | |
746 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
747 | } | |
748 | } else { | |
749 | // If the useridto field is empty, the useridfrom must be filled. | |
750 | $userfromfullname = fullname($userfrom, $canviewfullname); | |
751 | } | |
aff9da17 JL |
752 | foreach ($messages as $mid => $message) { |
753 | ||
754 | // We need to get the user from the query. | |
755 | if (empty($userfromfullname)) { | |
6ff4464b JL |
756 | // Check for non-reply and support users. |
757 | if (core_user::is_real_user($message->useridfrom)) { | |
127ef540 | 758 | $user = new stdClass(); |
6ff4464b JL |
759 | $user = username_load_fields_from_object($user, $message, 'userfrom'); |
760 | $message->userfromfullname = fullname($user, $canviewfullname); | |
761 | } else { | |
762 | $user = core_user::get_user($message->useridfrom); | |
763 | $message->userfromfullname = fullname($user, $canviewfullname); | |
764 | } | |
aff9da17 JL |
765 | } else { |
766 | $message->userfromfullname = $userfromfullname; | |
767 | } | |
768 | ||
769 | // We need to get the user from the query. | |
770 | if (empty($usertofullname)) { | |
127ef540 | 771 | $user = new stdClass(); |
aff9da17 JL |
772 | $user = username_load_fields_from_object($user, $message, 'userto'); |
773 | $message->usertofullname = fullname($user, $canviewfullname); | |
774 | } else { | |
775 | $message->usertofullname = $usertofullname; | |
776 | } | |
777 | ||
193edf7f | 778 | // This field is only available in the message_read table. |
aff9da17 JL |
779 | if (!isset($message->timeread)) { |
780 | $message->timeread = 0; | |
781 | } | |
782 | ||
aff9da17 | 783 | $message->text = message_format_message_text($message); |
aff9da17 JL |
784 | $messages[$mid] = (array) $message; |
785 | } | |
786 | } | |
787 | ||
788 | $results = array( | |
789 | 'messages' => $messages, | |
790 | 'warnings' => $warnings | |
791 | ); | |
792 | ||
793 | return $results; | |
794 | } | |
795 | ||
796 | /** | |
797 | * Get messages return description. | |
798 | * | |
6ff4464b | 799 | * @return external_single_structure |
193edf7f | 800 | * @since 2.8 |
aff9da17 JL |
801 | */ |
802 | public static function get_messages_returns() { | |
803 | return new external_single_structure( | |
804 | array( | |
805 | 'messages' => new external_multiple_structure( | |
806 | new external_single_structure( | |
807 | array( | |
193edf7f | 808 | 'id' => new external_value(PARAM_INT, 'Message id'), |
aff9da17 JL |
809 | 'useridfrom' => new external_value(PARAM_INT, 'User from id'), |
810 | 'useridto' => new external_value(PARAM_INT, 'User to id'), | |
811 | 'subject' => new external_value(PARAM_TEXT, 'The message subject'), | |
812 | 'text' => new external_value(PARAM_RAW, 'The message text formated'), | |
813 | 'fullmessage' => new external_value(PARAM_RAW, 'The message'), | |
193edf7f | 814 | 'fullmessageformat' => new external_format_value('fullmessage'), |
aff9da17 JL |
815 | 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'), |
816 | 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'), | |
817 | 'notification' => new external_value(PARAM_INT, 'Is a notification?'), | |
818 | 'contexturl' => new external_value(PARAM_RAW, 'Context URL'), | |
819 | 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'), | |
820 | 'timecreated' => new external_value(PARAM_INT, 'Time created'), | |
821 | 'timeread' => new external_value(PARAM_INT, 'Time read'), | |
822 | 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'), | |
823 | 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name') | |
824 | ), 'message' | |
825 | ) | |
826 | ), | |
827 | 'warnings' => new external_warnings() | |
828 | ) | |
829 | ); | |
830 | } | |
831 | ||
a623b6b8 | 832 | } |
5d1017e1 JM |
833 | |
834 | /** | |
4615817d | 835 | * Deprecated message external functions |
6fbd60ef | 836 | * |
4615817d JM |
837 | * @package core_message |
838 | * @copyright 2011 Jerome Mouneyrac | |
839 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
840 | * @since Moodle 2.1 | |
841 | * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more. | |
4615817d | 842 | * @see core_notes_external |
5d1017e1 JM |
843 | */ |
844 | class moodle_message_external extends external_api { | |
845 | ||
846 | /** | |
847 | * Returns description of method parameters | |
6fbd60ef | 848 | * |
5d1017e1 | 849 | * @return external_function_parameters |
4615817d JM |
850 | * @since Moodle 2.1 |
851 | * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. | |
4615817d | 852 | * @see core_message_external::send_instant_messages_parameters() |
5d1017e1 JM |
853 | */ |
854 | public static function send_instantmessages_parameters() { | |
855 | return core_message_external::send_instant_messages_parameters(); | |
856 | } | |
857 | ||
858 | /** | |
859 | * Send private messages from the current USER to other users | |
6fbd60ef | 860 | * |
4615817d JM |
861 | * @param array $messages An array of message to send. |
862 | * @return array | |
863 | * @since Moodle 2.1 | |
864 | * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. | |
4615817d | 865 | * @see core_message_external::send_instant_messages() |
5d1017e1 JM |
866 | */ |
867 | public static function send_instantmessages($messages = array()) { | |
868 | return core_message_external::send_instant_messages($messages); | |
869 | } | |
870 | ||
871 | /** | |
872 | * Returns description of method result value | |
6fbd60ef | 873 | * |
5d1017e1 | 874 | * @return external_description |
4615817d JM |
875 | * @since Moodle 2.1 |
876 | * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. | |
4615817d | 877 | * @see core_message_external::send_instant_messages_returns() |
5d1017e1 JM |
878 | */ |
879 | public static function send_instantmessages_returns() { | |
880 | return core_message_external::send_instant_messages_returns(); | |
881 | } | |
882 | ||
6fbd60ef | 883 | } |