weekly release 3.2dev
[moodle.git] / message / externallib.php
CommitLineData
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 27require_once("$CFG->libdir/externallib.php");
705afe6f 28require_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 */
39class core_message_external extends external_api {
a623b6b8
JM
40
41 /**
42 * Returns description of method parameters
4615817d 43 *
a623b6b8 44 * @return external_function_parameters
4615817d 45 * @since Moodle 2.2
a623b6b8 46 */
5d1017e1 47 public static function send_instant_messages_parameters() {
a623b6b8
JM
48 return new external_function_parameters(
49 array(
50 'messages' => new external_multiple_structure(
51 new external_single_structure(
52 array(
53 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'),
93ce0e82 54 'text' => new external_value(PARAM_RAW, 'the text of the message'),
14968ca9 55 'textformat' => new external_format_value('text', VALUE_DEFAULT, FORMAT_MOODLE),
a623b6b8
JM
56 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own client id for the message. If this id is provided, the fail message id will be returned to you', VALUE_OPTIONAL),
57 )
58 )
59 )
60 )
61 );
62 }
63
64 /**
65 * Send private messages from the current USER to other users
66 *
4615817d
JM
67 * @param array $messages An array of message to send.
68 * @return array
69 * @since Moodle 2.2
a623b6b8 70 */
5d1017e1 71 public static function send_instant_messages($messages = array()) {
a623b6b8 72 global $CFG, $USER, $DB;
a623b6b8 73
436bbf89 74 // Check if messaging is enabled.
837941e9 75 if (empty($CFG->messaging)) {
a623b6b8
JM
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.
837941e9 220 if (empty($CFG->messaging)) {
436bbf89
DM
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.
837941e9 282 if (empty($CFG->messaging)) {
436bbf89
DM
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.
837941e9 337 if (empty($CFG->messaging)) {
436bbf89
DM
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.
837941e9 399 if (empty($CFG->messaging)) {
436bbf89
DM
400 throw new moodle_exception('disabled', 'message');
401 }
402
34c2f347 403 $params = array('userids' => $userids, 'userid' => $userid);
d6731600
FM
404 $params = self::validate_parameters(self::unblock_contacts_parameters(), $params);
405
406 foreach ($params['userids'] as $id) {
34c2f347 407 message_unblock_contact($id, $userid);
d6731600
FM
408 }
409
410 return null;
411 }
412
413 /**
414 * Unblock contacts return description.
415 *
416 * @return external_description
5bcfd504 417 * @since Moodle 2.5
d6731600
FM
418 */
419 public static function unblock_contacts_returns() {
420 return null;
421 }
422
a3e3a3a1
MN
423 /**
424 * Return the structure of a message area contact.
425 *
426 * @return external_single_structure
427 * @since Moodle 3.2
428 */
429 private static function get_messagearea_contact_structure() {
430 return new external_single_structure(
431 array(
432 'userid' => new external_value(PARAM_INT, 'The user\'s id'),
433 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'),
434 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'),
435 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'),
5bf0ff27 436 'ismessaging' => new external_value(PARAM_BOOL, 'If we are messaging the user'),
89a70ba1 437 'sentfromcurrentuser' => new external_value(PARAM_BOOL, 'Was the last message sent from the current user?'),
a3e3a3a1
MN
438 'lastmessage' => new external_value(PARAM_NOTAGS, 'The user\'s last message'),
439 'messageid' => new external_value(PARAM_INT, 'The unique search message id', VALUE_DEFAULT, null),
440 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
441 'isread' => new external_value(PARAM_BOOL, 'If the user has read the message'),
dd0c1403 442 'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
a3e3a3a1
MN
443 'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation',
444 VALUE_DEFAULT, null),
445 )
446 );
447 }
448
94e1db61
MN
449 /**
450 * Return the structure of a message area message.
451 *
452 * @return external_single_structure
453 * @since Moodle 3.2
454 */
455 private static function get_messagearea_message_structure() {
456 return new external_single_structure(
457 array(
458 'id' => new external_value(PARAM_INT, 'The id of the message'),
89a70ba1
MN
459 'useridfrom' => new external_value(PARAM_INT, 'The id of the user who sent the message'),
460 'useridto' => new external_value(PARAM_INT, 'The id of the user who received the message'),
94e1db61
MN
461 'text' => new external_value(PARAM_RAW, 'The text of the message'),
462 'displayblocktime' => new external_value(PARAM_BOOL, 'Should we display the block time?'),
463 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'),
464 'position' => new external_value(PARAM_ALPHA, 'The position of the text'),
465 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'),
466 'isread' => new external_value(PARAM_INT, 'Determines if the message was read or not'),
467 )
468 );
469 }
470
cd03b8d7 471 /**
48e8bdba 472 * Get messagearea search users in course parameters.
cd03b8d7
MN
473 *
474 * @return external_function_parameters
475 * @since 3.2
476 */
48e8bdba 477 public static function data_for_messagearea_search_users_in_course_parameters() {
cd03b8d7
MN
478 return new external_function_parameters(
479 array(
480 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
481 'courseid' => new external_value(PARAM_INT, 'The id of the course'),
482 'search' => new external_value(PARAM_RAW, 'The string being searched'),
483 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
484 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
485 )
486 );
487 }
488
489 /**
48e8bdba 490 * Get messagearea search users in course results.
cd03b8d7
MN
491 *
492 * @param int $userid The id of the user who is performing the search
493 * @param int $courseid The id of the course
494 * @param string $search The string being searched
495 * @param int $limitfrom
496 * @param int $limitnum
497 * @return stdClass
498 * @throws moodle_exception
499 * @since 3.2
500 */
48e8bdba 501 public static function data_for_messagearea_search_users_in_course($userid, $courseid, $search, $limitfrom = 0,
de55cb1b 502 $limitnum = 0) {
837941e9 503 global $CFG, $PAGE, $USER;
cd03b8d7
MN
504
505 // Check if messaging is enabled.
837941e9 506 if (empty($CFG->messaging)) {
cd03b8d7
MN
507 throw new moodle_exception('disabled', 'message');
508 }
509
837941e9
MN
510 $systemcontext = context_system::instance();
511
cd03b8d7
MN
512 $params = array(
513 'userid' => $userid,
514 'courseid' => $courseid,
515 'search' => $search,
516 'limitfrom' => $limitfrom,
517 'limitnum' => $limitnum
518 );
48e8bdba 519 self::validate_parameters(self::data_for_messagearea_search_users_in_course_parameters(), $params);
837941e9 520 self::validate_context($systemcontext);
cd03b8d7 521
837941e9
MN
522 if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
523 throw new moodle_exception('You do not have permission to perform this action.');
524 }
cd03b8d7 525
de55cb1b
MN
526 $users = \core_message\api::search_users_in_course($userid, $courseid, $search, $limitfrom, $limitnum);
527 $results = new \core_message\output\messagearea\user_search_results($users);
cd03b8d7
MN
528
529 $renderer = $PAGE->get_renderer('core_message');
de55cb1b 530 return $results->export_for_template($renderer);
cd03b8d7
MN
531 }
532
533 /**
48e8bdba 534 * Get messagearea search users in course returns.
cd03b8d7
MN
535 *
536 * @return external_single_structure
537 * @since 3.2
538 */
48e8bdba 539 public static function data_for_messagearea_search_users_in_course_returns() {
cd03b8d7
MN
540 return new external_single_structure(
541 array(
cd03b8d7 542 'contacts' => new external_multiple_structure(
a3e3a3a1 543 self::get_messagearea_contact_structure()
cd03b8d7
MN
544 ),
545 )
546 );
547 }
548
549 /**
48e8bdba 550 * Get messagearea search users parameters.
cd03b8d7
MN
551 *
552 * @return external_function_parameters
553 * @since 3.2
554 */
48e8bdba 555 public static function data_for_messagearea_search_users_parameters() {
cd03b8d7
MN
556 return new external_function_parameters(
557 array(
558 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
559 'search' => new external_value(PARAM_RAW, 'The string being searched'),
560 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
561 )
562 );
563 }
564
565 /**
48e8bdba 566 * Get messagearea search users results.
cd03b8d7
MN
567 *
568 * @param int $userid The id of the user who is performing the search
569 * @param string $search The string being searched
570 * @param int $limitnum
571 * @return stdClass
572 * @throws moodle_exception
573 * @since 3.2
574 */
48e8bdba 575 public static function data_for_messagearea_search_users($userid, $search, $limitnum = 0) {
837941e9 576 global $CFG, $PAGE, $USER;
cd03b8d7
MN
577
578 // Check if messaging is enabled.
837941e9 579 if (empty($CFG->messaging)) {
cd03b8d7
MN
580 throw new moodle_exception('disabled', 'message');
581 }
582
837941e9
MN
583 $systemcontext = context_system::instance();
584
cd03b8d7
MN
585 $params = array(
586 'userid' => $userid,
587 'search' => $search,
588 'limitnum' => $limitnum
589 );
48e8bdba 590 self::validate_parameters(self::data_for_messagearea_search_users_parameters(), $params);
837941e9 591 self::validate_context($systemcontext);
cd03b8d7 592
837941e9
MN
593 if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
594 throw new moodle_exception('You do not have permission to perform this action.');
595 }
cd03b8d7 596
de55cb1b
MN
597 list($contacts, $courses, $noncontacts) = \core_message\api::search_users($userid, $search, $limitnum);
598 $search = new \core_message\output\messagearea\user_search_results($contacts, $courses, $noncontacts);
cd03b8d7
MN
599
600 $renderer = $PAGE->get_renderer('core_message');
601 return $search->export_for_template($renderer);
602 }
603
604 /**
48e8bdba 605 * Get messagearea search users returns.
cd03b8d7
MN
606 *
607 * @return external_single_structure
608 * @since 3.2
609 */
48e8bdba 610 public static function data_for_messagearea_search_users_returns() {
cd03b8d7
MN
611 return new external_single_structure(
612 array(
cd03b8d7 613 'contacts' => new external_multiple_structure(
a3e3a3a1 614 self::get_messagearea_contact_structure()
cd03b8d7 615 ),
cd03b8d7
MN
616 'courses' => new external_multiple_structure(
617 new external_single_structure(
618 array(
619 'id' => new external_value(PARAM_INT, 'The course id'),
620 'shortname' => new external_value(PARAM_NOTAGS, 'The course shortname'),
621 'fullname' => new external_value(PARAM_NOTAGS, 'The course fullname'),
622 )
623 )
624 ),
cd03b8d7 625 'noncontacts' => new external_multiple_structure(
a3e3a3a1 626 self::get_messagearea_contact_structure()
cd03b8d7
MN
627 )
628 )
629 );
630 }
631
632 /**
633 * Get messagearea search messages parameters.
634 *
635 * @return external_function_parameters
636 * @since 3.2
637 */
638 public static function data_for_messagearea_search_messages_parameters() {
639 return new external_function_parameters(
640 array(
641 'userid' => new external_value(PARAM_INT, 'The id of the user who is performing the search'),
642 'search' => new external_value(PARAM_RAW, 'The string being searched'),
643 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
644 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
645 )
646 );
647 }
648
649 /**
650 * Get messagearea search messages results.
651 *
652 * @param int $userid The id of the user who is performing the search
653 * @param string $search The string being searched
654 * @param int $limitfrom
655 * @param int $limitnum
656 * @return stdClass
657 * @throws moodle_exception
658 * @since 3.2
659 */
660 public static function data_for_messagearea_search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) {
837941e9 661 global $CFG, $PAGE, $USER;
cd03b8d7
MN
662
663 // Check if messaging is enabled.
837941e9 664 if (empty($CFG->messaging)) {
cd03b8d7
MN
665 throw new moodle_exception('disabled', 'message');
666 }
667
837941e9
MN
668 $systemcontext = context_system::instance();
669
cd03b8d7
MN
670 $params = array(
671 'userid' => $userid,
672 'search' => $search,
673 'limitfrom' => $limitfrom,
674 'limitnum' => $limitnum
675
676 );
677 self::validate_parameters(self::data_for_messagearea_search_messages_parameters(), $params);
837941e9 678 self::validate_context($systemcontext);
cd03b8d7 679
837941e9
MN
680 if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
681 throw new moodle_exception('You do not have permission to perform this action.');
682 }
cd03b8d7 683
de55cb1b
MN
684 $messages = \core_message\api::search_messages($userid, $search, $limitfrom, $limitnum);
685 $results = new \core_message\output\messagearea\message_search_results($messages);
cd03b8d7
MN
686
687 $renderer = $PAGE->get_renderer('core_message');
de55cb1b 688 return $results->export_for_template($renderer);
cd03b8d7
MN
689 }
690
691 /**
692 * Get messagearea search messages returns.
693 *
694 * @return external_single_structure
695 * @since 3.2
696 */
697 public static function data_for_messagearea_search_messages_returns() {
698 return new external_single_structure(
699 array(
cd03b8d7 700 'contacts' => new external_multiple_structure(
a3e3a3a1 701 self::get_messagearea_contact_structure()
cd03b8d7
MN
702 )
703 )
704 );
705 }
706
9aa012b5 707 /**
49aaadc3 708 * The messagearea conversations parameters.
9aa012b5
MN
709 *
710 * @return external_function_parameters
49aaadc3 711 * @since 3.2
9aa012b5
MN
712 */
713 public static function data_for_messagearea_conversations_parameters() {
714 return new external_function_parameters(
715 array(
716 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'),
717 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
718 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
719 )
720 );
721 }
722
723 /**
724 * Get messagearea conversations.
725 *
726 * @param int $userid The id of the user who we are viewing conversations for
727 * @param int $limitfrom
728 * @param int $limitnum
49aaadc3
MN
729 * @return stdClass
730 * @throws moodle_exception
731 * @since 3.2
9aa012b5
MN
732 */
733 public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) {
837941e9 734 global $CFG, $PAGE, $USER;
9aa012b5
MN
735
736 // Check if messaging is enabled.
837941e9 737 if (empty($CFG->messaging)) {
9aa012b5
MN
738 throw new moodle_exception('disabled', 'message');
739 }
740
837941e9
MN
741 $systemcontext = context_system::instance();
742
9aa012b5
MN
743 $params = array(
744 'userid' => $userid,
745 'limitfrom' => $limitfrom,
746 'limitnum' => $limitnum
747 );
748 self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params);
837941e9 749 self::validate_context($systemcontext);
9aa012b5 750
837941e9
MN
751 if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
752 throw new moodle_exception('You do not have permission to perform this action.');
753 }
9aa012b5 754
de55cb1b
MN
755 $conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum);
756 $conversations = new \core_message\output\messagearea\contacts(null, $conversations);
9aa012b5
MN
757
758 $renderer = $PAGE->get_renderer('core_message');
de55cb1b 759 return $conversations->export_for_template($renderer);
9aa012b5
MN
760 }
761
762 /**
49aaadc3 763 * The messagearea conversations return structure.
9aa012b5 764 *
49aaadc3
MN
765 * @return external_single_structure
766 * @since 3.2
9aa012b5
MN
767 */
768 public static function data_for_messagearea_conversations_returns() {
49aaadc3 769 return new external_single_structure(
9aa012b5 770 array(
9aa012b5 771 'contacts' => new external_multiple_structure(
a3e3a3a1 772 self::get_messagearea_contact_structure()
9aa012b5
MN
773 )
774 )
775 );
776 }
777
778 /**
49aaadc3 779 * The messagearea contacts return parameters.
9aa012b5
MN
780 *
781 * @return external_function_parameters
49aaadc3 782 * @since 3.2
9aa012b5
MN
783 */
784 public static function data_for_messagearea_contacts_parameters() {
785 return self::data_for_messagearea_conversations_parameters();
786 }
787
788 /**
789 * Get messagearea contacts parameters.
790 *
791 * @param int $userid The id of the user who we are viewing conversations for
792 * @param int $limitfrom
793 * @param int $limitnum
49aaadc3
MN
794 * @return stdClass
795 * @throws moodle_exception
796 * @since 3.2
9aa012b5
MN
797 */
798 public static function data_for_messagearea_contacts($userid, $limitfrom = 0, $limitnum = 0) {
837941e9 799 global $CFG, $PAGE, $USER;
9aa012b5
MN
800
801 // Check if messaging is enabled.
837941e9 802 if (empty($CFG->messaging)) {
9aa012b5
MN
803 throw new moodle_exception('disabled', 'message');
804 }
805
837941e9
MN
806 $systemcontext = context_system::instance();
807
9aa012b5
MN
808 $params = array(
809 'userid' => $userid,
810 'limitfrom' => $limitfrom,
811 'limitnum' => $limitnum
812 );
813 self::validate_parameters(self::data_for_messagearea_contacts_parameters(), $params);
837941e9 814 self::validate_context($systemcontext);
9aa012b5 815
837941e9
MN
816 if (($USER->id != $userid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
817 throw new moodle_exception('You do not have permission to perform this action.');
818 }
9aa012b5
MN
819
820 $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum);
de55cb1b 821 $contacts = new \core_message\output\messagearea\contacts(null, $contacts);
9aa012b5
MN
822
823 $renderer = $PAGE->get_renderer('core_message');
824 return $contacts->export_for_template($renderer);
825 }
826
827 /**
49aaadc3 828 * The messagearea contacts return structure.
9aa012b5 829 *
49aaadc3
MN
830 * @return external_single_structure
831 * @since 3.2
9aa012b5
MN
832 */
833 public static function data_for_messagearea_contacts_returns() {
834 return self::data_for_messagearea_conversations_returns();
835 }
836
3cd13828 837 /**
49aaadc3 838 * The messagearea messages parameters.
3cd13828
MN
839 *
840 * @return external_function_parameters
49aaadc3 841 * @since 3.2
3cd13828
MN
842 */
843 public static function data_for_messagearea_messages_parameters() {
844 return new external_function_parameters(
845 array(
846 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
847 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
848 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
8ec78c48
MN
849 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0),
850 'newest' => new external_value(PARAM_BOOL, 'Newest first?', VALUE_DEFAULT, false),
3cd13828
MN
851 )
852 );
853 }
854
855 /**
856 * Get messagearea messages.
857 *
858 * @param int $currentuserid The current user's id
859 * @param int $otheruserid The other user's id
860 * @param int $limitfrom
861 * @param int $limitnum
8ec78c48 862 * @param boolean $newest
49aaadc3
MN
863 * @return stdClass
864 * @throws moodle_exception
865 * @since 3.2
3cd13828 866 */
7b55aaa1
MN
867 public static function data_for_messagearea_messages($currentuserid, $otheruserid, $limitfrom = 0, $limitnum = 0,
868 $newest = false) {
837941e9 869 global $CFG, $PAGE, $USER;
3cd13828
MN
870
871 // Check if messaging is enabled.
837941e9 872 if (empty($CFG->messaging)) {
3cd13828
MN
873 throw new moodle_exception('disabled', 'message');
874 }
875
837941e9
MN
876 $systemcontext = context_system::instance();
877
3cd13828
MN
878 $params = array(
879 'currentuserid' => $currentuserid,
880 'otheruserid' => $otheruserid,
881 'limitfrom' => $limitfrom,
8ec78c48
MN
882 'limitnum' => $limitnum,
883 'newest' => $newest
3cd13828
MN
884 );
885 self::validate_parameters(self::data_for_messagearea_messages_parameters(), $params);
837941e9 886 self::validate_context($systemcontext);
3cd13828 887
837941e9
MN
888 if (($USER->id != $currentuserid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
889 throw new moodle_exception('You do not have permission to perform this action.');
890 }
3cd13828 891
8ec78c48
MN
892 if ($newest) {
893 $sort = 'timecreated DESC';
894 } else {
895 $sort = 'timecreated ASC';
896 }
897 $messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom, $limitnum, $sort);
de55cb1b 898 $messages = new \core_message\output\messagearea\messages($currentuserid, $otheruserid, $messages);
3cd13828
MN
899
900 $renderer = $PAGE->get_renderer('core_message');
901 return $messages->export_for_template($renderer);
902 }
903
904 /**
49aaadc3 905 * The messagearea messages return structure.
3cd13828 906 *
49aaadc3
MN
907 * @return external_single_structure
908 * @since 3.2
3cd13828
MN
909 */
910 public static function data_for_messagearea_messages_returns() {
49aaadc3 911 return new external_single_structure(
3cd13828 912 array(
7b55aaa1
MN
913 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing
914 the messages on behalf of?'),
3cd13828
MN
915 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
916 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
917 'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'),
bf58081d 918 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
3cd13828 919 'messages' => new external_multiple_structure(
94e1db61 920 self::get_messagearea_message_structure()
3cd13828
MN
921 )
922 )
923 );
924 }
925
c060cd49 926 /**
49aaadc3 927 * The get most recent message return parameters.
c060cd49
MN
928 *
929 * @return external_function_parameters
49aaadc3 930 * @since 3.2
c060cd49
MN
931 */
932 public static function data_for_messagearea_get_most_recent_message_parameters() {
933 return new external_function_parameters(
934 array(
935 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
936 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
937 )
938 );
939 }
940
941 /**
942 * Get the most recent message in a conversation.
943 *
944 * @param int $currentuserid The current user's id
945 * @param int $otheruserid The other user's id
49aaadc3
MN
946 * @return stdClass
947 * @throws moodle_exception
948 * @since 3.2
c060cd49
MN
949 */
950 public static function data_for_messagearea_get_most_recent_message($currentuserid, $otheruserid) {
837941e9 951 global $CFG, $PAGE, $USER;
c060cd49
MN
952
953 // Check if messaging is enabled.
837941e9 954 if (empty($CFG->messaging)) {
c060cd49
MN
955 throw new moodle_exception('disabled', 'message');
956 }
957
837941e9
MN
958 $systemcontext = context_system::instance();
959
c060cd49
MN
960 $params = array(
961 'currentuserid' => $currentuserid,
962 'otheruserid' => $otheruserid
963 );
964 self::validate_parameters(self::data_for_messagearea_get_most_recent_message_parameters(), $params);
837941e9 965 self::validate_context($systemcontext);
c060cd49 966
837941e9
MN
967 if (($USER->id != $currentuserid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
968 throw new moodle_exception('You do not have permission to perform this action.');
969 }
c060cd49
MN
970
971 $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid);
de55cb1b 972 $message = new \core_message\output\messagearea\message($message);
c060cd49
MN
973
974 $renderer = $PAGE->get_renderer('core_message');
975 return $message->export_for_template($renderer);
976 }
977
978 /**
49aaadc3 979 * The get most recent message return structure.
c060cd49
MN
980 *
981 * @return external_single_structure
49aaadc3 982 * @since 3.2
c060cd49
MN
983 */
984 public static function data_for_messagearea_get_most_recent_message_returns() {
94e1db61 985 return self::get_messagearea_message_structure();
c060cd49
MN
986 }
987
c6e97f54
MN
988 /**
989 * The get profile parameters.
990 *
991 * @return external_function_parameters
49aaadc3 992 * @since 3.2
c6e97f54
MN
993 */
994 public static function data_for_messagearea_get_profile_parameters() {
995 return new external_function_parameters(
996 array(
997 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
998 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we want to view'),
999 )
1000 );
1001 }
1002
1003 /**
1004 * Get the profile information for a contact.
1005 *
1006 * @param int $currentuserid The current user's id
1007 * @param int $otheruserid The id of the user whose profile we are viewing
49aaadc3
MN
1008 * @return stdClass
1009 * @throws moodle_exception
1010 * @since 3.2
c6e97f54
MN
1011 */
1012 public static function data_for_messagearea_get_profile($currentuserid, $otheruserid) {
837941e9 1013 global $CFG, $PAGE, $USER;
c6e97f54
MN
1014
1015 // Check if messaging is enabled.
837941e9 1016 if (empty($CFG->messaging)) {
c6e97f54
MN
1017 throw new moodle_exception('disabled', 'message');
1018 }
1019
837941e9
MN
1020 $systemcontext = context_system::instance();
1021
c6e97f54
MN
1022 $params = array(
1023 'currentuserid' => $currentuserid,
1024 'otheruserid' => $otheruserid
1025 );
1026 self::validate_parameters(self::data_for_messagearea_get_profile_parameters(), $params);
837941e9 1027 self::validate_context($systemcontext);
c6e97f54 1028
837941e9
MN
1029 if (($USER->id != $currentuserid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
1030 throw new moodle_exception('You do not have permission to perform this action.');
1031 }
c6e97f54
MN
1032
1033 $profile = \core_message\api::get_profile($currentuserid, $otheruserid);
de55cb1b 1034 $profile = new \core_message\output\messagearea\profile($profile);
c6e97f54
MN
1035
1036 $renderer = $PAGE->get_renderer('core_message');
1037 return $profile->export_for_template($renderer);
1038 }
1039
1040 /**
49aaadc3 1041 * The get profile return structure.
c6e97f54
MN
1042 *
1043 * @return external_single_structure
49aaadc3 1044 * @since 3.2
c6e97f54
MN
1045 */
1046 public static function data_for_messagearea_get_profile_returns() {
1047 return new external_single_structure(
1048 array(
de55cb1b 1049 'userid' => new external_value(PARAM_INT, 'The id of the user whose profile we are viewing'),
c6e97f54 1050 'email' => new external_value(core_user::get_property_type('email'), 'An email address'),
cc22b515 1051 'country' => new external_value(PARAM_TEXT, 'Home country of the user'),
c6e97f54
MN
1052 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user'),
1053 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'),
1054 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'),
1055 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'),
bf58081d 1056 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
c6e97f54
MN
1057 'isblocked' => new external_value(PARAM_BOOL, 'Is the user blocked?'),
1058 'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?')
1059 )
1060 );
1061 }
1062
d6731600
FM
1063 /**
1064 * Get contacts parameters description.
1065 *
1066 * @return external_function_parameters
5bcfd504 1067 * @since Moodle 2.5
d6731600
FM
1068 */
1069 public static function get_contacts_parameters() {
1070 return new external_function_parameters(array());
1071 }
1072
1073 /**
1074 * Get contacts.
1075 *
d6731600 1076 * @return external_description
5bcfd504 1077 * @since Moodle 2.5
d6731600
FM
1078 */
1079 public static function get_contacts() {
d85bedf7 1080 global $CFG, $PAGE;
436bbf89
DM
1081
1082 // Check if messaging is enabled.
837941e9 1083 if (empty($CFG->messaging)) {
436bbf89
DM
1084 throw new moodle_exception('disabled', 'message');
1085 }
1086
d6731600
FM
1087 require_once($CFG->dirroot . '/user/lib.php');
1088
1089 list($online, $offline, $strangers) = message_get_contacts();
1090 $allcontacts = array('online' => $online, 'offline' => $offline, 'strangers' => $strangers);
1091 foreach ($allcontacts as $mode => $contacts) {
1092 foreach ($contacts as $key => $contact) {
1093 $newcontact = array(
1094 'id' => $contact->id,
1095 'fullname' => fullname($contact),
1096 'unread' => $contact->messagecount
1097 );
1098
d85bedf7
JL
1099 $userpicture = new user_picture($contact);
1100 $userpicture->size = 1; // Size f1.
1101 $newcontact['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
1102 $userpicture->size = 0; // Size f2.
1103 $newcontact['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false);
d6731600
FM
1104
1105 $allcontacts[$mode][$key] = $newcontact;
1106 }
1107 }
1108 return $allcontacts;
1109 }
1110
1111 /**
1112 * Get contacts return description.
1113 *
1114 * @return external_description
5bcfd504 1115 * @since Moodle 2.5
d6731600
FM
1116 */
1117 public static function get_contacts_returns() {
1118 return new external_single_structure(
1119 array(
1120 'online' => new external_multiple_structure(
1121 new external_single_structure(
1122 array(
1123 'id' => new external_value(PARAM_INT, 'User ID'),
1124 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
1125 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL),
1126 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
1127 'unread' => new external_value(PARAM_INT, 'Unread message count')
1128 )
1129 ),
1130 'List of online contacts'
1131 ),
1132 'offline' => new external_multiple_structure(
1133 new external_single_structure(
1134 array(
1135 'id' => new external_value(PARAM_INT, 'User ID'),
1136 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
1137 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL),
1138 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
1139 'unread' => new external_value(PARAM_INT, 'Unread message count')
1140 )
1141 ),
1142 'List of offline contacts'
1143 ),
1144 'strangers' => new external_multiple_structure(
1145 new external_single_structure(
1146 array(
1147 'id' => new external_value(PARAM_INT, 'User ID'),
1148 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
1149 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL),
1150 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL),
1151 'unread' => new external_value(PARAM_INT, 'Unread message count')
1152 )
1153 ),
1154 'List of users that are not in the user\'s contact list but have sent a message'
1155 )
1156 )
1157 );
1158 }
1159
1160 /**
1161 * Search contacts parameters description.
1162 *
1163 * @return external_function_parameters
5bcfd504 1164 * @since Moodle 2.5
d6731600
FM
1165 */
1166 public static function search_contacts_parameters() {
1167 return new external_function_parameters(
1168 array(
1169 'searchtext' => new external_value(PARAM_CLEAN, 'String the user\'s fullname has to match to be found'),
1170 'onlymycourses' => new external_value(PARAM_BOOL, 'Limit search to the user\'s courses',
1171 VALUE_DEFAULT, false)
1172 )
1173 );
1174 }
1175
1176 /**
1177 * Search contacts.
1178 *
1179 * @param string $searchtext query string.
1180 * @param bool $onlymycourses limit the search to the user's courses only.
1181 * @return external_description
5bcfd504 1182 * @since Moodle 2.5
d6731600
FM
1183 */
1184 public static function search_contacts($searchtext, $onlymycourses = false) {
d85bedf7 1185 global $CFG, $USER, $PAGE;
11d83ab3 1186 require_once($CFG->dirroot . '/user/lib.php');
436bbf89
DM
1187
1188 // Check if messaging is enabled.
837941e9 1189 if (empty($CFG->messaging)) {
436bbf89
DM
1190 throw new moodle_exception('disabled', 'message');
1191 }
1192
d6731600
FM
1193 require_once($CFG->libdir . '/enrollib.php');
1194
1195 $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses);
1196 $params = self::validate_parameters(self::search_contacts_parameters(), $params);
1197
1198 // Extra validation, we do not allow empty queries.
1199 if ($params['searchtext'] === '') {
1200 throw new moodle_exception('querystringcannotbeempty');
1201 }
1202
1203 $courseids = array();
1204 if ($params['onlymycourses']) {
1205 $mycourses = enrol_get_my_courses(array('id'));
1206 foreach ($mycourses as $mycourse) {
1207 $courseids[] = $mycourse->id;
1208 }
1209 } else {
1210 $courseids[] = SITEID;
1211 }
1212
1213 // Retrieving the users matching the query.
1214 $users = message_search_users($courseids, $params['searchtext']);
1215 $results = array();
1216 foreach ($users as $user) {
1217 $results[$user->id] = $user;
1218 }
1219
1220 // Reorganising information.
1221 foreach ($results as &$user) {
1222 $newuser = array(
1223 'id' => $user->id,
1224 'fullname' => fullname($user)
1225 );
1226
1227 // Avoid undefined property notice as phone not specified.
1228 $user->phone1 = null;
1229 $user->phone2 = null;
1230
d85bedf7
JL
1231 $userpicture = new user_picture($user);
1232 $userpicture->size = 1; // Size f1.
1233 $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
1234 $userpicture->size = 0; // Size f2.
1235 $newuser['profileimageurlsmall'] = $userpicture->get_url($PAGE)->out(false);
d6731600
FM
1236
1237 $user = $newuser;
1238 }
1239
1240 return $results;
1241 }
1242
1243 /**
1244 * Search contacts return description.
1245 *
1246 * @return external_description
5bcfd504 1247 * @since Moodle 2.5
d6731600
FM
1248 */
1249 public static function search_contacts_returns() {
1250 return new external_multiple_structure(
1251 new external_single_structure(
1252 array(
1253 'id' => new external_value(PARAM_INT, 'User ID'),
1254 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
1255 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL),
1256 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL', VALUE_OPTIONAL)
1257 )
1258 ),
1259 'List of contacts'
1260 );
1261 }
aff9da17
JL
1262
1263 /**
1264 * Get messages parameters description.
1265 *
1266 * @return external_function_parameters
193edf7f 1267 * @since 2.8
aff9da17
JL
1268 */
1269 public static function get_messages_parameters() {
1270 return new external_function_parameters(
1271 array(
6ff4464b 1272 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
127ef540
SH
1273 'useridfrom' => new external_value(
1274 PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
1275 VALUE_DEFAULT, 0),
1276 'type' => new external_value(
1277 PARAM_ALPHA, 'type of message to return, expected values are: notifications, conversations and both',
1278 VALUE_DEFAULT, 'both'),
6ff4464b 1279 'read' => new external_value(PARAM_BOOL, 'true for getting read messages, false for unread', VALUE_DEFAULT, true),
127ef540
SH
1280 'newestfirst' => new external_value(
1281 PARAM_BOOL, 'true for ordering by newest first, false for oldest first',
1282 VALUE_DEFAULT, true),
aff9da17 1283 'limitfrom' => new external_value(PARAM_INT, 'limit from', VALUE_DEFAULT, 0),
127ef540
SH
1284 'limitnum' => new external_value(PARAM_INT, 'limit number', VALUE_DEFAULT, 0)
1285 )
aff9da17
JL
1286 );
1287 }
1288
1289 /**
1290 * Get messages function implementation.
127ef540
SH
1291 *
1292 * @since 2.8
1293 * @throws invalid_parameter_exception
1294 * @throws moodle_exception
6ff4464b
JL
1295 * @param int $useridto the user id who received the message
1296 * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
193edf7f 1297 * @param string $type type of message to return, expected values: notifications, conversations and both
aff9da17 1298 * @param bool $read true for retreiving read messages, false for unread
6ff4464b 1299 * @param bool $newestfirst true for ordering by newest first, false for oldest first
aff9da17
JL
1300 * @param int $limitfrom limit from
1301 * @param int $limitnum limit num
1302 * @return external_description
aff9da17 1303 */
193edf7f 1304 public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true,
aff9da17 1305 $newestfirst = true, $limitfrom = 0, $limitnum = 0) {
127ef540 1306 global $CFG, $USER;
aff9da17
JL
1307
1308 $warnings = array();
1309
1310 $params = array(
1311 'useridto' => $useridto,
6ff4464b 1312 'useridfrom' => $useridfrom,
aff9da17
JL
1313 'type' => $type,
1314 'read' => $read,
aff9da17
JL
1315 'newestfirst' => $newestfirst,
1316 'limitfrom' => $limitfrom,
1317 'limitnum' => $limitnum
1318 );
1319
1320 $params = self::validate_parameters(self::get_messages_parameters(), $params);
1321
1322 $context = context_system::instance();
1323 self::validate_context($context);
1324
1325 $useridto = $params['useridto'];
6ff4464b 1326 $useridfrom = $params['useridfrom'];
aff9da17
JL
1327 $type = $params['type'];
1328 $read = $params['read'];
aff9da17
JL
1329 $newestfirst = $params['newestfirst'];
1330 $limitfrom = $params['limitfrom'];
1331 $limitnum = $params['limitnum'];
1332
1333 $allowedvalues = array('notifications', 'conversations', 'both');
1334 if (!in_array($type, $allowedvalues)) {
1335 throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' .
1336 'allowed values are: ' . implode(',', $allowedvalues));
1337 }
1338
1339 // Check if private messaging between users is allowed.
1340 if (empty($CFG->messaging)) {
1341 // If we are retreiving only conversations, and messaging is disabled, throw an exception.
aff9da17
JL
1342 if ($type == "conversations") {
1343 throw new moodle_exception('disabled', 'message');
1344 }
1345 if ($type == "both") {
1346 $warning = array();
1347 $warning['item'] = 'message';
1348 $warning['itemid'] = $USER->id;
1349 $warning['warningcode'] = '1';
1350 $warning['message'] = 'Private messages (conversations) are not enabled in this site.
1351 Only notifications will be returned';
1352 $warnings[] = $warning;
1353 }
1354 }
1355
1356 if (!empty($useridto)) {
6ff4464b
JL
1357 if (core_user::is_real_user($useridto)) {
1358 $userto = core_user::get_user($useridto, '*', MUST_EXIST);
1359 } else {
1360 throw new moodle_exception('invaliduser');
1361 }
aff9da17
JL
1362 }
1363
1364 if (!empty($useridfrom)) {
1365 // We use get_user here because the from user can be the noreply or support user.
1366 $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
1367 }
1368
1369 // Check if the current user is the sender/receiver or just a privileged user.
1370 if ($useridto != $USER->id and $useridfrom != $USER->id and
1371 !has_capability('moodle/site:readallmessages', $context)) {
1372 throw new moodle_exception('accessdenied', 'admin');
1373 }
1374
127ef540 1375 // Which type of messages to retrieve.
193edf7f 1376 $notifications = -1;
aff9da17 1377 if ($type != 'both') {
193edf7f 1378 $notifications = ($type == 'notifications') ? 1 : 0;
aff9da17
JL
1379 }
1380
aff9da17 1381 $orderdirection = $newestfirst ? 'DESC' : 'ASC';
193edf7f 1382 $sort = "mr.timecreated $orderdirection";
aff9da17 1383
193edf7f 1384 if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) {
aff9da17
JL
1385 $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
1386
1387 // In some cases, we don't need to get the to/from user objects from the sql query.
1388 $userfromfullname = '';
1389 $usertofullname = '';
1390
1391 // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
1392 if (!empty($useridto)) {
1393 $usertofullname = fullname($userto, $canviewfullname);
1394 // The user from may or may not be filled.
1395 if (!empty($useridfrom)) {
1396 $userfromfullname = fullname($userfrom, $canviewfullname);
1397 }
1398 } else {
1399 // If the useridto field is empty, the useridfrom must be filled.
1400 $userfromfullname = fullname($userfrom, $canviewfullname);
1401 }
aff9da17
JL
1402 foreach ($messages as $mid => $message) {
1403
ea21d637
JL
1404 // Do not return deleted messages.
1405 if (($useridto == $USER->id and $message->timeusertodeleted) or
1406 ($useridfrom == $USER->id and $message->timeuserfromdeleted)) {
1407
1408 unset($messages[$mid]);
1409 continue;
1410 }
1411
aff9da17
JL
1412 // We need to get the user from the query.
1413 if (empty($userfromfullname)) {
6ff4464b
JL
1414 // Check for non-reply and support users.
1415 if (core_user::is_real_user($message->useridfrom)) {
127ef540 1416 $user = new stdClass();
6ff4464b
JL
1417 $user = username_load_fields_from_object($user, $message, 'userfrom');
1418 $message->userfromfullname = fullname($user, $canviewfullname);
1419 } else {
1420 $user = core_user::get_user($message->useridfrom);
1421 $message->userfromfullname = fullname($user, $canviewfullname);
1422 }
aff9da17
JL
1423 } else {
1424 $message->userfromfullname = $userfromfullname;
1425 }
1426
1427 // We need to get the user from the query.
1428 if (empty($usertofullname)) {
127ef540 1429 $user = new stdClass();
aff9da17
JL
1430 $user = username_load_fields_from_object($user, $message, 'userto');
1431 $message->usertofullname = fullname($user, $canviewfullname);
1432 } else {
1433 $message->usertofullname = $usertofullname;
1434 }
1435
193edf7f 1436 // This field is only available in the message_read table.
aff9da17
JL
1437 if (!isset($message->timeread)) {
1438 $message->timeread = 0;
1439 }
1440
aff9da17 1441 $message->text = message_format_message_text($message);
aff9da17
JL
1442 $messages[$mid] = (array) $message;
1443 }
1444 }
1445
1446 $results = array(
1447 'messages' => $messages,
1448 'warnings' => $warnings
1449 );
1450
1451 return $results;
1452 }
1453
1454 /**
1455 * Get messages return description.
1456 *
6ff4464b 1457 * @return external_single_structure
193edf7f 1458 * @since 2.8
aff9da17
JL
1459 */
1460 public static function get_messages_returns() {
1461 return new external_single_structure(
1462 array(
1463 'messages' => new external_multiple_structure(
1464 new external_single_structure(
1465 array(
193edf7f 1466 'id' => new external_value(PARAM_INT, 'Message id'),
aff9da17
JL
1467 'useridfrom' => new external_value(PARAM_INT, 'User from id'),
1468 'useridto' => new external_value(PARAM_INT, 'User to id'),
1469 'subject' => new external_value(PARAM_TEXT, 'The message subject'),
1470 'text' => new external_value(PARAM_RAW, 'The message text formated'),
1471 'fullmessage' => new external_value(PARAM_RAW, 'The message'),
193edf7f 1472 'fullmessageformat' => new external_format_value('fullmessage'),
aff9da17
JL
1473 'fullmessagehtml' => new external_value(PARAM_RAW, 'The message in html'),
1474 'smallmessage' => new external_value(PARAM_RAW, 'The shorten message'),
1475 'notification' => new external_value(PARAM_INT, 'Is a notification?'),
1476 'contexturl' => new external_value(PARAM_RAW, 'Context URL'),
1477 'contexturlname' => new external_value(PARAM_TEXT, 'Context URL link name'),
1478 'timecreated' => new external_value(PARAM_INT, 'Time created'),
1479 'timeread' => new external_value(PARAM_INT, 'Time read'),
1480 'usertofullname' => new external_value(PARAM_TEXT, 'User to full name'),
1481 'userfromfullname' => new external_value(PARAM_TEXT, 'User from full name')
1482 ), 'message'
1483 )
1484 ),
1485 'warnings' => new external_warnings()
1486 )
1487 );
3274d5ca
RW
1488 }
1489
3274d5ca
RW
1490 /**
1491 * Mark all notifications as read parameters description.
1492 *
1493 * @return external_function_parameters
1494 * @since 3.2
1495 */
1496 public static function mark_all_notifications_as_read_parameters() {
1497 return new external_function_parameters(
1498 array(
1499 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
1500 'useridfrom' => new external_value(
1501 PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
1502 VALUE_DEFAULT, 0),
1503 )
1504 );
1505 }
1506
1507 /**
1508 * Mark all notifications as read function.
1509 *
1510 * @since 3.2
1511 * @throws invalid_parameter_exception
1512 * @throws moodle_exception
1513 * @param int $useridto the user id who received the message
1514 * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
1515 * @return external_description
1516 */
1517 public static function mark_all_notifications_as_read($useridto, $useridfrom) {
837941e9 1518 global $USER;
3274d5ca
RW
1519
1520 $params = self::validate_parameters(
1521 self::mark_all_notifications_as_read_parameters(),
1522 array(
1523 'useridto' => $useridto,
1524 'useridfrom' => $useridfrom,
1525 )
1526 );
1527
1528 $context = context_system::instance();
1529 self::validate_context($context);
1530
1531 $useridto = $params['useridto'];
1532 $useridfrom = $params['useridfrom'];
1533
1534 if (!empty($useridto)) {
1535 if (core_user::is_real_user($useridto)) {
1536 $userto = core_user::get_user($useridto, '*', MUST_EXIST);
1537 } else {
1538 throw new moodle_exception('invaliduser');
1539 }
1540 }
1541
1542 if (!empty($useridfrom)) {
1543 // We use get_user here because the from user can be the noreply or support user.
1544 $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
1545 }
1546
1547 // Check if the current user is the sender/receiver or just a privileged user.
1548 if ($useridto != $USER->id and $useridfrom != $USER->id and
7b55aaa1 1549 // The deleteanymessage cap seems more reasonable here than readallmessages.
3274d5ca
RW
1550 !has_capability('moodle/site:deleteanymessage', $context)) {
1551 throw new moodle_exception('accessdenied', 'admin');
1552 }
1553
79f6c36c 1554 \core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION);
3274d5ca
RW
1555
1556 return true;
1557 }
1558
1559 /**
1560 * Mark all notifications as read return description.
1561 *
1562 * @return external_single_structure
1563 * @since 3.2
1564 */
1565 public static function mark_all_notifications_as_read_returns() {
1566 return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise');
1567 }
1568
8c55bd6c
RW
1569 /**
1570 * Get unread conversations count parameters description.
1571 *
1572 * @return external_function_parameters
1573 * @since 3.2
1574 */
1575 public static function get_unread_conversations_count_parameters() {
1576 return new external_function_parameters(
1577 array(
1578 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
1579 )
1580 );
1581 }
1582
1583 /**
1584 * Get unread messages count function.
1585 *
1586 * @since 3.2
1587 * @throws invalid_parameter_exception
1588 * @throws moodle_exception
1589 * @param int $useridto the user id who received the message
1590 * @return external_description
1591 */
1592 public static function get_unread_conversations_count($useridto) {
1593 global $USER;
1594
1595 $params = self::validate_parameters(
1596 self::get_unread_conversations_count_parameters(),
1597 array('useridto' => $useridto)
1598 );
1599
1600 $context = context_system::instance();
1601 self::validate_context($context);
1602
1603 $useridto = $params['useridto'];
1604
1605 if (!empty($useridto)) {
1606 if (core_user::is_real_user($useridto)) {
1607 $userto = core_user::get_user($useridto, '*', MUST_EXIST);
1608 } else {
1609 throw new moodle_exception('invaliduser');
1610 }
1611 } else {
1612 $useridto = $USER->id;
1613 }
1614
1615 // Check if the current user is the receiver or just a privileged user.
1616 if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
1617 throw new moodle_exception('accessdenied', 'admin');
1618 }
1619
79f6c36c 1620 return \core_message\api::count_unread_conversations($userto);
8c55bd6c
RW
1621 }
1622
1623 /**
1624 * Get unread conversations count return description.
1625 *
1626 * @return external_single_structure
1627 * @since 3.2
1628 */
1629 public static function get_unread_conversations_count_returns() {
1630 return new external_value(PARAM_INT, 'The count of unread messages for the user');
aff9da17
JL
1631 }
1632
60ab2e1b
JL
1633 /**
1634 * Get blocked users parameters description.
1635 *
1636 * @return external_function_parameters
1637 * @since 2.9
1638 */
1639 public static function get_blocked_users_parameters() {
1640 return new external_function_parameters(
1641 array(
1642 'userid' => new external_value(PARAM_INT,
1643 'the user whose blocked users we want to retrieve',
1644 VALUE_REQUIRED),
1645 )
1646 );
1647 }
1648
1649 /**
1650 * Retrieve a list of users blocked
1651 *
1652 * @param int $userid the user whose blocked users we want to retrieve
1653 * @return external_description
1654 * @since 2.9
1655 */
1656 public static function get_blocked_users($userid) {
d85bedf7 1657 global $CFG, $USER, $PAGE;
60ab2e1b
JL
1658
1659 // Warnings array, it can be empty at the end but is mandatory.
1660 $warnings = array();
1661
1662 // Validate params.
1663 $params = array(
1664 'userid' => $userid
1665 );
1666 $params = self::validate_parameters(self::get_blocked_users_parameters(), $params);
1667 $userid = $params['userid'];
1668
1669 // Validate context.
1670 $context = context_system::instance();
1671 self::validate_context($context);
1672
1673 // Check if private messaging between users is allowed.
1674 if (empty($CFG->messaging)) {
1675 throw new moodle_exception('disabled', 'message');
1676 }
1677
4485f7c5
JL
1678 $user = core_user::get_user($userid, '*', MUST_EXIST);
1679 core_user::require_active_user($user);
60ab2e1b
JL
1680
1681 // Check if we have permissions for retrieve the information.
1682 if ($userid != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
1683 throw new moodle_exception('accessdenied', 'admin');
1684 }
1685
1686 // Now, we can get safely all the blocked users.
1687 $users = message_get_blocked_users($user);
1688
1689 $blockedusers = array();
1690 foreach ($users as $user) {
1691 $newuser = array(
1692 'id' => $user->id,
1693 'fullname' => fullname($user),
1694 );
0b074e88 1695
d85bedf7
JL
1696 $userpicture = new user_picture($user);
1697 $userpicture->size = 1; // Size f1.
1698 $newuser['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
60ab2e1b
JL
1699
1700 $blockedusers[] = $newuser;
1701 }
1702
1703 $results = array(
1704 'users' => $blockedusers,
1705 'warnings' => $warnings
1706 );
1707 return $results;
1708 }
1709
1710 /**
1711 * Get blocked users return description.
1712 *
1713 * @return external_single_structure
1714 * @since 2.9
1715 */
1716 public static function get_blocked_users_returns() {
1717 return new external_single_structure(
1718 array(
1719 'users' => new external_multiple_structure(
1720 new external_single_structure(
1721 array(
1722 'id' => new external_value(PARAM_INT, 'User ID'),
1723 'fullname' => new external_value(PARAM_NOTAGS, 'User full name'),
1724 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL', VALUE_OPTIONAL)
1725 )
1726 ),
1727 'List of blocked users'
1728 ),
1729 'warnings' => new external_warnings()
1730 )
1731 );
1732 }
1733
31c474da
JL
1734 /**
1735 * Returns description of method parameters
1736 *
1737 * @return external_function_parameters
1738 * @since 2.9
1739 */
1740 public static function mark_message_read_parameters() {
1741 return new external_function_parameters(
1742 array(
1743 'messageid' => new external_value(PARAM_INT, 'id of the message (in the message table)'),
7b55aaa1
MN
1744 'timeread' => new external_value(PARAM_INT, 'timestamp for when the message should be marked read',
1745 VALUE_DEFAULT, 0)
31c474da
JL
1746 )
1747 );
1748 }
1749
1750 /**
1751 * Mark a single message as read, trigger message_viewed event
1752 *
1753 * @param int $messageid id of the message (in the message table)
1754 * @param int $timeread timestamp for when the message should be marked read
1755 * @return external_description
1756 * @throws invalid_parameter_exception
1757 * @throws moodle_exception
1758 * @since 2.9
1759 */
1760 public static function mark_message_read($messageid, $timeread) {
1761 global $CFG, $DB, $USER;
31c474da
JL
1762
1763 // Check if private messaging between users is allowed.
1764 if (empty($CFG->messaging)) {
1765 throw new moodle_exception('disabled', 'message');
1766 }
1767
1768 // Warnings array, it can be empty at the end but is mandatory.
1769 $warnings = array();
1770
1771 // Validate params.
1772 $params = array(
1773 'messageid' => $messageid,
1774 'timeread' => $timeread
1775 );
1776 $params = self::validate_parameters(self::mark_message_read_parameters(), $params);
1777
0b19d048
RW
1778 if (empty($params['timeread'])) {
1779 $timeread = time();
1780 } else {
1781 $timeread = $params['timeread'];
1782 }
1783
31c474da
JL
1784 // Validate context.
1785 $context = context_system::instance();
1786 self::validate_context($context);
1787
1788 $message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST);
1789
1790 if ($message->useridto != $USER->id) {
1791 throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read');
1792 }
1793
0b19d048 1794 $messageid = message_mark_message_read($message, $timeread);
31c474da
JL
1795
1796 $results = array(
1797 'messageid' => $messageid,
1798 'warnings' => $warnings
1799 );
1800 return $results;
1801 }
1802
1803 /**
1804 * Returns description of method result value
1805 *
1806 * @return external_description
1807 * @since 2.9
1808 */
1809 public static function mark_message_read_returns() {
1810 return new external_single_structure(
1811 array(
1812 'messageid' => new external_value(PARAM_INT, 'the id of the message in the message_read table'),
1813 'warnings' => new external_warnings()
1814 )
1815 );
1816 }
1817
8c55bd6c
RW
1818 /**
1819 * Mark all messages as read parameters description.
1820 *
1821 * @return external_function_parameters
1822 * @since 3.2
1823 */
1824 public static function mark_all_messages_as_read_parameters() {
1825 return new external_function_parameters(
1826 array(
1827 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
1828 'useridfrom' => new external_value(
1829 PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
1830 VALUE_DEFAULT, 0),
1831 )
1832 );
1833 }
1834
1835 /**
1836 * Mark all notifications as read function.
1837 *
1838 * @since 3.2
1839 * @throws invalid_parameter_exception
1840 * @throws moodle_exception
1841 * @param int $useridto the user id who received the message
1842 * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
1843 * @return external_description
1844 */
1845 public static function mark_all_messages_as_read($useridto, $useridfrom) {
837941e9 1846 global $USER;
8c55bd6c
RW
1847
1848 $params = self::validate_parameters(
1849 self::mark_all_messages_as_read_parameters(),
1850 array(
1851 'useridto' => $useridto,
1852 'useridfrom' => $useridfrom,
1853 )
1854 );
1855
1856 $context = context_system::instance();
1857 self::validate_context($context);
1858
1859 $useridto = $params['useridto'];
1860 $useridfrom = $params['useridfrom'];
1861
1862 if (!empty($useridto)) {
1863 if (core_user::is_real_user($useridto)) {
1864 $userto = core_user::get_user($useridto, '*', MUST_EXIST);
1865 } else {
1866 throw new moodle_exception('invaliduser');
1867 }
1868 }
1869
1870 if (!empty($useridfrom)) {
1871 // We use get_user here because the from user can be the noreply or support user.
1872 $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
1873 }
1874
1875 // Check if the current user is the sender/receiver or just a privileged user.
1876 if ($useridto != $USER->id and $useridfrom != $USER->id and
7b55aaa1 1877 // The deleteanymessage cap seems more reasonable here than readallmessages.
8c55bd6c
RW
1878 !has_capability('moodle/site:deleteanymessage', $context)) {
1879 throw new moodle_exception('accessdenied', 'admin');
1880 }
1881
79f6c36c 1882 \core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE);
8c55bd6c
RW
1883
1884 return true;
1885 }
1886
1887 /**
1888 * Mark all notifications as read return description.
1889 *
1890 * @return external_single_structure
1891 * @since 3.2
1892 */
1893 public static function mark_all_messages_as_read_returns() {
1894 return new external_value(PARAM_BOOL, 'True if the messages were marked read, false otherwise');
1895 }
1896
dec0cd99
MN
1897 /**
1898 * Returns description of method parameters.
1899 *
1900 * @return external_function_parameters
1901 * @since 3.2
1902 */
1903 public static function delete_conversation_parameters() {
1904 return new external_function_parameters(
1905 array(
1906 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the conversation for'),
1907 'otheruserid' => new external_value(PARAM_INT, 'The user id of the other user in the conversation'),
1908 )
1909 );
1910 }
1911
1912 /**
1913 * Deletes a conversation.
1914 *
1915 * @param int $userid The user id of who we want to delete the conversation for
1916 * @param int $otheruserid The user id of the other user in the conversation
1917 * @return array
1918 * @throws moodle_exception
1919 * @since 3.2
1920 */
1921 public static function delete_conversation($userid, $otheruserid) {
1922 global $CFG;
1923
1924 // Check if private messaging between users is allowed.
1925 if (empty($CFG->messaging)) {
1926 throw new moodle_exception('disabled', 'message');
1927 }
1928
1929 // Warnings array, it can be empty at the end but is mandatory.
1930 $warnings = array();
1931
1932 // Validate params.
1933 $params = array(
1934 'userid' => $userid,
1935 'otheruserid' => $otheruserid,
1936 );
1937 $params = self::validate_parameters(self::delete_conversation_parameters(), $params);
1938
1939 // Validate context.
1940 $context = context_system::instance();
1941 self::validate_context($context);
1942
1943 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
1944 core_user::require_active_user($user);
1945
1946 if (\core_message\api::can_delete_conversation($user->id)) {
1947 $status = \core_message\api::delete_conversation($user->id, $otheruserid);
1948 } else {
1949 throw new moodle_exception('You do not have permission to delete messages');
1950 }
1951
1952 $results = array(
1953 'status' => $status,
1954 'warnings' => $warnings
1955 );
1956
1957 return $results;
1958 }
1959
1960 /**
1961 * Returns description of method result value.
1962 *
1963 * @return external_description
1964 * @since 3.2
1965 */
1966 public static function delete_conversation_returns() {
1967 return new external_single_structure(
1968 array(
1969 'status' => new external_value(PARAM_BOOL, 'True if the conversation was deleted, false otherwise'),
1970 'warnings' => new external_warnings()
1971 )
1972 );
1973 }
1974
419b1128
JL
1975 /**
1976 * Returns description of method parameters
1977 *
1978 * @return external_function_parameters
1979 * @since 3.1
1980 */
1981 public static function delete_message_parameters() {
1982 return new external_function_parameters(
1983 array(
1984 'messageid' => new external_value(PARAM_INT, 'The message id'),
1985 'userid' => new external_value(PARAM_INT, 'The user id of who we want to delete the message for'),
1986 'read' => new external_value(PARAM_BOOL, 'If is a message read', VALUE_DEFAULT, true)
1987 )
1988 );
1989 }
1990
1991 /**
1992 * Deletes a message
1993 *
1994 * @param int $messageid the message id
1995 * @param int $userid the user id of who we want to delete the message for
1996 * @param bool $read if is a message read (default to true)
1997 * @return external_description
1998 * @throws moodle_exception
1999 * @since 3.1
2000 */
2001 public static function delete_message($messageid, $userid, $read = true) {
2002 global $CFG, $DB;
419b1128
JL
2003
2004 // Check if private messaging between users is allowed.
2005 if (empty($CFG->messaging)) {
2006 throw new moodle_exception('disabled', 'message');
2007 }
2008
2009 // Warnings array, it can be empty at the end but is mandatory.
2010 $warnings = array();
2011
2012 // Validate params.
2013 $params = array(
2014 'messageid' => $messageid,
2015 'userid' => $userid,
2016 'read' => $read
2017 );
2018 $params = self::validate_parameters(self::delete_message_parameters(), $params);
2019
2020 // Validate context.
2021 $context = context_system::instance();
2022 self::validate_context($context);
2023
2024 $messagestable = $params['read'] ? 'message_read' : 'message';
2025 $message = $DB->get_record($messagestable, array('id' => $params['messageid']), '*', MUST_EXIST);
2026
2027 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
2028 core_user::require_active_user($user);
2029
2030 $status = false;
2031 if (message_can_delete_message($message, $user->id)) {
2032 $status = message_delete_message($message, $user->id);;
2033 } else {
2034 throw new moodle_exception('You do not have permission to delete this message');
2035 }
2036
2037 $results = array(
2038 'status' => $status,
2039 'warnings' => $warnings
2040 );
2041 return $results;
2042 }
2043
2044 /**
2045 * Returns description of method result value
2046 *
2047 * @return external_description
2048 * @since 3.1
2049 */
2050 public static function delete_message_returns() {
2051 return new external_single_structure(
2052 array(
2053 'status' => new external_value(PARAM_BOOL, 'True if the message was deleted, false otherwise'),
2054 'warnings' => new external_warnings()
2055 )
2056 );
2057 }
2058
a0eabdd3
RW
2059 /**
2060 * Returns description of method parameters
2061 *
2062 * @return external_function_parameters
2063 * @since 3.2
2064 */
2065 public static function message_processor_config_form_parameters() {
2066 return new external_function_parameters(
2067 array(
2068 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED),
2069 'name' => new external_value(PARAM_TEXT, 'The name of the message processor'),
2070 'formvalues' => new external_multiple_structure(
2071 new external_single_structure(
2072 array(
2073 'name' => new external_value(PARAM_TEXT, 'name of the form element', VALUE_REQUIRED),
2074 'value' => new external_value(PARAM_RAW, 'value of the form element', VALUE_REQUIRED),
2075 )
2076 ),
2077 'Config form values',
2078 VALUE_REQUIRED
2079 ),
2080 )
2081 );
2082 }
2083
2084 /**
2085 * Processes a message processor config form.
2086 *
2087 * @param int $userid the user id
2088 * @param string $name the name of the processor
2089 * @param array $formvalues the form values
2090 * @return external_description
2091 * @throws moodle_exception
2092 * @since 3.2
2093 */
2094 public static function message_processor_config_form($userid, $name, $formvalues) {
8c125526
RW
2095 global $USER;
2096
a0eabdd3
RW
2097 $params = self::validate_parameters(
2098 self::message_processor_config_form_parameters(),
2099 array(
2100 'userid' => $userid,
2101 'name' => $name,
2102 'formvalues' => $formvalues,
2103 )
2104 );
2105
2106 if (empty($params['userid'])) {
2107 $params['userid'] = $USER->id;
2108 }
2109
2110 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
2111 core_user::require_active_user($user);
2112
2113 $processor = get_message_processor($name);
2114 $preferences = [];
2115 $form = new stdClass();
2116
2117 foreach ($formvalues as $formvalue) {
cdff0944
RW
2118 // Curly braces to ensure interpretation is consistent between
2119 // php 5 and php 7.
2120 $form->{$formvalue['name']} = $formvalue['value'];
a0eabdd3
RW
2121 }
2122
2123 $processor->process_form($form, $preferences);
2124
2125 if (!empty($preferences)) {
2126 set_user_preferences($preferences, $userid);
2127 }
2128 }
2129
2130 /**
2131 * Returns description of method result value
2132 *
2133 * @return external_description
2134 * @since 3.2
2135 */
2136 public static function message_processor_config_form_returns() {
2137 return null;
2138 }
8c125526
RW
2139
2140 /**
2141 * Returns description of method parameters
2142 *
2143 * @return external_function_parameters
2144 * @since 3.2
2145 */
2146 public static function get_message_processor_parameters() {
2147 return new external_function_parameters(
2148 array(
2149 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user'),
2150 'name' => new external_value(PARAM_TEXT, 'The name of the message processor', VALUE_REQUIRED),
2151 )
2152 );
2153 }
2154
2155 /**
2156 * Get a message processor.
2157 *
7b55aaa1
MN
2158 * @param int $userid
2159 * @param string $name the name of the processor
8c125526
RW
2160 * @return external_description
2161 * @throws moodle_exception
2162 * @since 3.2
2163 */
2164 public static function get_message_processor($userid = 0, $name) {
2165 global $USER, $PAGE;
2166
2167 $params = self::validate_parameters(
2168 self::get_message_processor_parameters(),
2169 array(
2170 'userid' => $userid,
2171 'name' => $name,
2172 )
2173 );
2174
2175 if (empty($params['userid'])) {
2176 $params['userid'] = $USER->id;
2177 }
2178
2179 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
2180 core_user::require_active_user($user);
2181 self::validate_context(context_user::instance($params['userid']));
2182
2183 $processor = get_message_processor($name);
2184
2185 $processoroutput = new \core_message\output\processor($processor, $user);
2186 $renderer = $PAGE->get_renderer('core_message');
2187
2188 return $processoroutput->export_for_template($renderer);
2189 }
2190
2191 /**
2192 * Returns description of method result value
2193 *
2194 * @return external_description
2195 * @since 3.2
2196 */
2197 public static function get_message_processor_returns() {
2198 return new external_function_parameters(
2199 array(
2200 'systemconfigured' => new external_value(PARAM_BOOL, 'Site configuration status'),
2201 'userconfigured' => new external_value(PARAM_BOOL, 'The user configuration status'),
2202 )
2203 );
2204 }
e86f0cb4
JL
2205
2206 /**
2207 * Returns description of method parameters
2208 *
2209 * @return external_function_parameters
2210 * @since 3.2
2211 */
2212 public static function get_user_notification_preferences_parameters() {
2213 return new external_function_parameters(
2214 array(
2215 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_DEFAULT, 0)
2216 )
2217 );
2218 }
2219
2220 /**
2221 * Get the notification preferences for a given user.
2222 *
2223 * @param int $userid id of the user, 0 for current user
2224 * @return external_description
2225 * @throws moodle_exception
2226 * @since 3.2
2227 */
2228 public static function get_user_notification_preferences($userid = 0) {
2229 global $USER, $PAGE;
2230
2231 $params = self::validate_parameters(
2232 self::get_user_notification_preferences_parameters(),
2233 array(
2234 'userid' => $userid,
2235 )
2236 );
2237
2238 if (empty($params['userid'])) {
2239 $user = $USER;
2240 } else {
2241 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
2242 core_user::require_active_user($user);
2243 }
2244
2245 $systemcontext = context_system::instance();
2246 self::validate_context($systemcontext);
2247
2248 // Check access control.
2249 if ($user->id == $USER->id) {
2250 // Editing own message profile.
2251 require_capability('moodle/user:editownmessageprofile', $systemcontext);
2252 } else {
2253 // Teachers, parents, etc.
2254 $personalcontext = context_user::instance($user->id);
2255 require_capability('moodle/user:editmessageprofile', $personalcontext);
2256 }
2257
2258 $processors = get_message_processors();
2259 $providers = message_get_providers_for_user($user->id);
1686d93a 2260 $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
e86f0cb4
JL
2261 $notificationlist = new \core_message\output\preferences\notification_list($processors, $providers, $preferences, $user);
2262
2263 $renderer = $PAGE->get_renderer('core_message');
2264
2265 $result = array(
2266 'warnings' => array(),
2267 'preferences' => $notificationlist->export_for_template($renderer)
2268 );
2269 return $result;
2270 }
2271
2272 /**
2273 * Returns description of method result value
2274 *
2275 * @return external_description
2276 * @since 3.2
2277 */
2278 public static function get_user_notification_preferences_returns() {
2279 return new external_function_parameters(
2280 array(
2281 'preferences' => new external_single_structure(
2282 array(
2283 'userid' => new external_value(PARAM_INT, 'User id'),
2284 'disableall' => new external_value(PARAM_INT, 'Whether all the preferences are disabled'),
2285 'processors' => new external_multiple_structure(
2286 new external_single_structure(
2287 array(
2288 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
e86f0cb4
JL
2289 'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
2290 'hassettings' => new external_value(PARAM_BOOL, 'Whether has settings'),
2291 'contextid' => new external_value(PARAM_INT, 'Context id'),
2292 'userconfigured' => new external_value(PARAM_INT, 'Whether is configured by the user'),
2293 )
2294 ),
2295 'Config form values'
2296 ),
2297 'components' => new external_multiple_structure(
2298 new external_single_structure(
2299 array(
2300 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
2301 'notifications' => new external_multiple_structure(
2302 new external_single_structure(
2303 array(
2304 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
2305 'preferencekey' => new external_value(PARAM_ALPHANUMEXT, 'Preference key'),
2306 'processors' => new external_multiple_structure(
2307 new external_single_structure(
2308 array(
2309 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
e86f0cb4
JL
2310 'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
2311 'locked' => new external_value(PARAM_BOOL, 'Is locked by admin?'),
2312 'userconfigured' => new external_value(PARAM_INT, 'Is configured?'),
2313 'loggedin' => new external_single_structure(
2314 array(
2315 'name' => new external_value(PARAM_NOTAGS, 'Name'),
2316 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
2317 'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
2318 )
2319 ),
2320 'loggedoff' => new external_single_structure(
2321 array(
2322 'name' => new external_value(PARAM_NOTAGS, 'Name'),
2323 'displayname' => new external_value(PARAM_TEXT, 'Display name'),
2324 'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
2325 )
2326 ),
2327 )
2328 ),
2329 'Processors values for this notification'
2330 ),
2331 )
2332 ),
2333 'List of notificaitons for the component'
2334 ),
2335 )
2336 ),
2337 'Available components'
2338 ),
2339 )
2340 ),
2341 'warnings' => new external_warnings(),
2342 )
2343 );
2344 }
a623b6b8 2345}