2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * External message API
21 * @package core_message
23 * @copyright 2011 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
30 * Message external functions
32 * @package core_message
34 * @copyright 2011 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_message_external
extends external_api
{
41 * Returns description of method parameters
43 * @return external_function_parameters
46 public static function send_instant_messages_parameters() {
47 return new external_function_parameters(
49 'messages' => new external_multiple_structure(
50 new external_single_structure(
52 'touserid' => new external_value(PARAM_INT
, 'id of the user to send the private message'),
53 'text' => new external_value(PARAM_RAW
, 'the text of the message'),
54 'textformat' => new external_format_value('text', VALUE_DEFAULT
),
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
),
64 * Send private messages from the current USER to other users
66 * @param array $messages An array of message to send.
70 public static function send_instant_messages($messages = array()) {
71 global $CFG, $USER, $DB;
72 require_once($CFG->dirroot
. "/message/lib.php");
74 // Check if messaging is enabled.
75 if (!$CFG->messaging
) {
76 throw new moodle_exception('disabled', 'message');
79 // Ensure the current user is allowed to run this function
80 $context = context_system
::instance();
81 self
::validate_context($context);
82 require_capability('moodle/site:sendmessage', $context);
84 $params = self
::validate_parameters(self
::send_instant_messages_parameters(), array('messages' => $messages));
86 //retrieve all tousers of the messages
88 foreach($params['messages'] as $message) {
89 $receivers[] = $message['touserid'];
91 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED
, 'userid_');
92 $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
94 $contactlist = array();
95 $sqlparams['contactid'] = $USER->id
;
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;
105 // $record->userid have current user as contact
106 $contactlist[$record->userid
] = true;
111 $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
113 $resultmessages = array();
114 foreach ($params['messages'] as $message) {
115 $resultmsg = array(); //the infos about the success of the operation
117 //we are going to do some checking
118 //code should match /messages/index.php checks
121 //check the user exists
122 if (empty($tousers[$message['touserid']])) {
124 $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
127 //check that the touser is not blocking the current user
128 if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
130 $errormessage = get_string('userisblockingyou', 'message');
133 // Check if the user is a contact
134 //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid
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)) {
138 // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
140 $errormessage = get_string('userisblockingyounoncontact', 'message');
143 //now we can send the message (at least try)
145 //TODO MDL-31118 performance improvement - edit the function so we can pass an array instead one touser object
146 $success = message_post_message($USER, $tousers[$message['touserid']],
147 $message['text'], external_validate_format($message['textformat']));
150 //build the resultmsg
151 if (isset($message['clientmsgid'])) {
152 $resultmsg['clientmsgid'] = $message['clientmsgid'];
155 $resultmsg['msgid'] = $success;
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 .
160 $resultmsg['msgid'] = -1;
161 $resultmsg['errormessage'] = $errormessage;
164 $resultmessages[] = $resultmsg;
167 return $resultmessages;
171 * Returns description of method result value
173 * @return external_description
176 public static function send_instant_messages_returns() {
177 return new external_multiple_structure(
178 new external_single_structure(
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'),
181 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT
, 'your own id for the message', VALUE_OPTIONAL
),
182 'errormessage' => new external_value(PARAM_TEXT
, 'error message - if it failed', VALUE_OPTIONAL
)
189 * Create contacts parameters description.
191 * @return external_function_parameters
194 public static function create_contacts_parameters() {
195 return new external_function_parameters(
197 'userids' => new external_multiple_structure(
198 new external_value(PARAM_INT
, 'User ID'),
208 * @param array $userids array of user IDs.
209 * @return external_description
212 public static function create_contacts($userids) {
215 // Check if messaging is enabled.
216 if (!$CFG->messaging
) {
217 throw new moodle_exception('disabled', 'message');
220 $params = array('userids' => $userids);
221 $params = self
::validate_parameters(self
::create_contacts_parameters(), $params);
224 foreach ($params['userids'] as $id) {
225 if (!message_add_contact($id)) {
229 'warningcode' => 'contactnotcreated',
230 'message' => 'The contact could not be created'
238 * Create contacts return description.
240 * @return external_description
243 public static function create_contacts_returns() {
244 return new external_warnings();
248 * Delete contacts parameters description.
250 * @return external_function_parameters
253 public static function delete_contacts_parameters() {
254 return new external_function_parameters(
256 'userids' => new external_multiple_structure(
257 new external_value(PARAM_INT
, 'User ID'),
267 * @param array $userids array of user IDs.
271 public static function delete_contacts($userids) {
274 // Check if messaging is enabled.
275 if (!$CFG->messaging
) {
276 throw new moodle_exception('disabled', 'message');
279 $params = array('userids' => $userids);
280 $params = self
::validate_parameters(self
::delete_contacts_parameters(), $params);
282 foreach ($params['userids'] as $id) {
283 message_remove_contact($id);
290 * Delete contacts return description.
292 * @return external_description
295 public static function delete_contacts_returns() {
300 * Block contacts parameters description.
302 * @return external_function_parameters
305 public static function block_contacts_parameters() {
306 return new external_function_parameters(
308 'userids' => new external_multiple_structure(
309 new external_value(PARAM_INT
, 'User ID'),
319 * @param array $userids array of user IDs.
320 * @return external_description
323 public static function block_contacts($userids) {
326 // Check if messaging is enabled.
327 if (!$CFG->messaging
) {
328 throw new moodle_exception('disabled', 'message');
331 $params = array('userids' => $userids);
332 $params = self
::validate_parameters(self
::block_contacts_parameters(), $params);
335 foreach ($params['userids'] as $id) {
336 if (!message_block_contact($id)) {
340 'warningcode' => 'contactnotblocked',
341 'message' => 'The contact could not be blocked'
349 * Block contacts return description.
351 * @return external_description
354 public static function block_contacts_returns() {
355 return new external_warnings();
359 * Unblock contacts parameters description.
361 * @return external_function_parameters
364 public static function unblock_contacts_parameters() {
365 return new external_function_parameters(
367 'userids' => new external_multiple_structure(
368 new external_value(PARAM_INT
, 'User ID'),
378 * @param array $userids array of user IDs.
382 public static function unblock_contacts($userids) {
385 // Check if messaging is enabled.
386 if (!$CFG->messaging
) {
387 throw new moodle_exception('disabled', 'message');
390 $params = array('userids' => $userids);
391 $params = self
::validate_parameters(self
::unblock_contacts_parameters(), $params);
393 foreach ($params['userids'] as $id) {
394 message_unblock_contact($id);
401 * Unblock contacts return description.
403 * @return external_description
406 public static function unblock_contacts_returns() {
411 * Get contacts parameters description.
413 * @return external_function_parameters
416 public static function get_contacts_parameters() {
417 return new external_function_parameters(array());
423 * @param array $userids array of user IDs.
424 * @return external_description
427 public static function get_contacts() {
430 // Check if messaging is enabled.
431 if (!$CFG->messaging
) {
432 throw new moodle_exception('disabled', 'message');
435 require_once($CFG->dirroot
. '/user/lib.php');
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) {
442 'id' => $contact->id
,
443 'fullname' => fullname($contact),
444 'unread' => $contact->messagecount
447 $usercontextid = context_user
::instance($contact->id
)->id
;
448 $newcontact['profileimageurl'] = moodle_url
::make_webservice_pluginfile_url(
449 $usercontextid, 'user', 'icon', null, '/', 'f1')->out(false);
450 $newcontact['profileimageurlsmall'] = moodle_url
::make_webservice_pluginfile_url(
451 $usercontextid, 'user', 'icon', null, '/', 'f2')->out(false);
453 $allcontacts[$mode][$key] = $newcontact;
460 * Get contacts return description.
462 * @return external_description
465 public static function get_contacts_returns() {
466 return new external_single_structure(
468 'online' => new external_multiple_structure(
469 new external_single_structure(
471 'id' => new external_value(PARAM_INT
, 'User ID'),
472 'fullname' => new external_value(PARAM_NOTAGS
, 'User full name'),
473 'profileimageurl' => new external_value(PARAM_URL
, 'User picture URL', VALUE_OPTIONAL
),
474 'profileimageurlsmall' => new external_value(PARAM_URL
, 'Small user picture URL', VALUE_OPTIONAL
),
475 'unread' => new external_value(PARAM_INT
, 'Unread message count')
478 'List of online contacts'
480 'offline' => new external_multiple_structure(
481 new external_single_structure(
483 'id' => new external_value(PARAM_INT
, 'User ID'),
484 'fullname' => new external_value(PARAM_NOTAGS
, 'User full name'),
485 'profileimageurl' => new external_value(PARAM_URL
, 'User picture URL', VALUE_OPTIONAL
),
486 'profileimageurlsmall' => new external_value(PARAM_URL
, 'Small user picture URL', VALUE_OPTIONAL
),
487 'unread' => new external_value(PARAM_INT
, 'Unread message count')
490 'List of offline contacts'
492 'strangers' => new external_multiple_structure(
493 new external_single_structure(
495 'id' => new external_value(PARAM_INT
, 'User ID'),
496 'fullname' => new external_value(PARAM_NOTAGS
, 'User full name'),
497 'profileimageurl' => new external_value(PARAM_URL
, 'User picture URL', VALUE_OPTIONAL
),
498 'profileimageurlsmall' => new external_value(PARAM_URL
, 'Small user picture URL', VALUE_OPTIONAL
),
499 'unread' => new external_value(PARAM_INT
, 'Unread message count')
502 'List of users that are not in the user\'s contact list but have sent a message'
509 * Search contacts parameters description.
511 * @return external_function_parameters
514 public static function search_contacts_parameters() {
515 return new external_function_parameters(
517 'searchtext' => new external_value(PARAM_CLEAN
, 'String the user\'s fullname has to match to be found'),
518 'onlymycourses' => new external_value(PARAM_BOOL
, 'Limit search to the user\'s courses',
519 VALUE_DEFAULT
, false)
527 * @param string $searchtext query string.
528 * @param bool $onlymycourses limit the search to the user's courses only.
529 * @return external_description
532 public static function search_contacts($searchtext, $onlymycourses = false) {
534 require_once($CFG->dirroot
. '/user/lib.php');
536 // Check if messaging is enabled.
537 if (!$CFG->messaging
) {
538 throw new moodle_exception('disabled', 'message');
541 require_once($CFG->libdir
. '/enrollib.php');
543 $params = array('searchtext' => $searchtext, 'onlymycourses' => $onlymycourses);
544 $params = self
::validate_parameters(self
::search_contacts_parameters(), $params);
546 // Extra validation, we do not allow empty queries.
547 if ($params['searchtext'] === '') {
548 throw new moodle_exception('querystringcannotbeempty');
551 $courseids = array();
552 if ($params['onlymycourses']) {
553 $mycourses = enrol_get_my_courses(array('id'));
554 foreach ($mycourses as $mycourse) {
555 $courseids[] = $mycourse->id
;
558 $courseids[] = SITEID
;
561 // Retrieving the users matching the query.
562 $users = message_search_users($courseids, $params['searchtext']);
564 foreach ($users as $user) {
565 $results[$user->id
] = $user;
568 // Reorganising information.
569 foreach ($results as &$user) {
572 'fullname' => fullname($user)
575 // Avoid undefined property notice as phone not specified.
576 $user->phone1
= null;
577 $user->phone2
= null;
579 $usercontextid = context_user
::instance($user->id
)->id
;
580 $newuser['profileimageurl'] = moodle_url
::make_webservice_pluginfile_url(
581 $usercontextid, 'user', 'icon', null, '/', 'f1')->out(false);
582 $newuser['profileimageurlsmall'] = moodle_url
::make_webservice_pluginfile_url(
583 $usercontextid, 'user', 'icon', null, '/', 'f2')->out(false);
592 * Search contacts return description.
594 * @return external_description
597 public static function search_contacts_returns() {
598 return new external_multiple_structure(
599 new external_single_structure(
601 'id' => new external_value(PARAM_INT
, 'User ID'),
602 'fullname' => new external_value(PARAM_NOTAGS
, 'User full name'),
603 'profileimageurl' => new external_value(PARAM_URL
, 'User picture URL', VALUE_OPTIONAL
),
604 'profileimageurlsmall' => new external_value(PARAM_URL
, 'Small user picture URL', VALUE_OPTIONAL
)
612 * Get messages parameters description.
614 * @return external_function_parameters
617 public static function get_messages_parameters() {
618 return new external_function_parameters(
620 'useridto' => new external_value(PARAM_INT
, 'the user id who received the message, 0 for any user', VALUE_REQUIRED
),
621 'useridfrom' => new external_value(
622 PARAM_INT
, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
624 'type' => new external_value(
625 PARAM_ALPHA
, 'type of message to return, expected values are: notifications, conversations and both',
626 VALUE_DEFAULT
, 'both'),
627 'read' => new external_value(PARAM_BOOL
, 'true for getting read messages, false for unread', VALUE_DEFAULT
, true),
628 'newestfirst' => new external_value(
629 PARAM_BOOL
, 'true for ordering by newest first, false for oldest first',
630 VALUE_DEFAULT
, true),
631 'limitfrom' => new external_value(PARAM_INT
, 'limit from', VALUE_DEFAULT
, 0),
632 'limitnum' => new external_value(PARAM_INT
, 'limit number', VALUE_DEFAULT
, 0)
638 * Get messages function implementation.
641 * @throws invalid_parameter_exception
642 * @throws moodle_exception
643 * @param int $useridto the user id who received the message
644 * @param int $useridfrom the user id who send the message. -10 or -20 for no-reply or support user
645 * @param string $type type of message to return, expected values: notifications, conversations and both
646 * @param bool $read true for retreiving read messages, false for unread
647 * @param bool $newestfirst true for ordering by newest first, false for oldest first
648 * @param int $limitfrom limit from
649 * @param int $limitnum limit num
650 * @return external_description
652 public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true,
653 $newestfirst = true, $limitfrom = 0, $limitnum = 0) {
655 require_once($CFG->dirroot
. "/message/lib.php");
660 'useridto' => $useridto,
661 'useridfrom' => $useridfrom,
664 'newestfirst' => $newestfirst,
665 'limitfrom' => $limitfrom,
666 'limitnum' => $limitnum
669 $params = self
::validate_parameters(self
::get_messages_parameters(), $params);
671 $context = context_system
::instance();
672 self
::validate_context($context);
674 $useridto = $params['useridto'];
675 $useridfrom = $params['useridfrom'];
676 $type = $params['type'];
677 $read = $params['read'];
678 $newestfirst = $params['newestfirst'];
679 $limitfrom = $params['limitfrom'];
680 $limitnum = $params['limitnum'];
682 $allowedvalues = array('notifications', 'conversations', 'both');
683 if (!in_array($type, $allowedvalues)) {
684 throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' .
685 'allowed values are: ' . implode(',', $allowedvalues));
688 // Check if private messaging between users is allowed.
689 if (empty($CFG->messaging
)) {
690 // If we are retreiving only conversations, and messaging is disabled, throw an exception.
691 if ($type == "conversations") {
692 throw new moodle_exception('disabled', 'message');
694 if ($type == "both") {
696 $warning['item'] = 'message';
697 $warning['itemid'] = $USER->id
;
698 $warning['warningcode'] = '1';
699 $warning['message'] = 'Private messages (conversations) are not enabled in this site.
700 Only notifications will be returned';
701 $warnings[] = $warning;
705 if (!empty($useridto)) {
706 if (core_user
::is_real_user($useridto)) {
707 $userto = core_user
::get_user($useridto, '*', MUST_EXIST
);
709 throw new moodle_exception('invaliduser');
713 if (!empty($useridfrom)) {
714 // We use get_user here because the from user can be the noreply or support user.
715 $userfrom = core_user
::get_user($useridfrom, '*', MUST_EXIST
);
718 // Check if the current user is the sender/receiver or just a privileged user.
719 if ($useridto != $USER->id
and $useridfrom != $USER->id
and
720 !has_capability('moodle/site:readallmessages', $context)) {
721 throw new moodle_exception('accessdenied', 'admin');
724 // Which type of messages to retrieve.
726 if ($type != 'both') {
727 $notifications = ($type == 'notifications') ?
1 : 0;
730 $orderdirection = $newestfirst ?
'DESC' : 'ASC';
731 $sort = "mr.timecreated $orderdirection";
733 if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) {
734 $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
736 // In some cases, we don't need to get the to/from user objects from the sql query.
737 $userfromfullname = '';
738 $usertofullname = '';
740 // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
741 if (!empty($useridto)) {
742 $usertofullname = fullname($userto, $canviewfullname);
743 // The user from may or may not be filled.
744 if (!empty($useridfrom)) {
745 $userfromfullname = fullname($userfrom, $canviewfullname);
748 // If the useridto field is empty, the useridfrom must be filled.
749 $userfromfullname = fullname($userfrom, $canviewfullname);
751 foreach ($messages as $mid => $message) {
753 // We need to get the user from the query.
754 if (empty($userfromfullname)) {
755 // Check for non-reply and support users.
756 if (core_user
::is_real_user($message->useridfrom
)) {
757 $user = new stdClass();
758 $user = username_load_fields_from_object($user, $message, 'userfrom');
759 $message->userfromfullname
= fullname($user, $canviewfullname);
761 $user = core_user
::get_user($message->useridfrom
);
762 $message->userfromfullname
= fullname($user, $canviewfullname);
765 $message->userfromfullname
= $userfromfullname;
768 // We need to get the user from the query.
769 if (empty($usertofullname)) {
770 $user = new stdClass();
771 $user = username_load_fields_from_object($user, $message, 'userto');
772 $message->usertofullname
= fullname($user, $canviewfullname);
774 $message->usertofullname
= $usertofullname;
777 // This field is only available in the message_read table.
778 if (!isset($message->timeread
)) {
779 $message->timeread
= 0;
782 $message->text
= message_format_message_text($message);
783 $messages[$mid] = (array) $message;
788 'messages' => $messages,
789 'warnings' => $warnings
796 * Get messages return description.
798 * @return external_single_structure
801 public static function get_messages_returns() {
802 return new external_single_structure(
804 'messages' => new external_multiple_structure(
805 new external_single_structure(
807 'id' => new external_value(PARAM_INT
, 'Message id'),
808 'useridfrom' => new external_value(PARAM_INT
, 'User from id'),
809 'useridto' => new external_value(PARAM_INT
, 'User to id'),
810 'subject' => new external_value(PARAM_TEXT
, 'The message subject'),
811 'text' => new external_value(PARAM_RAW
, 'The message text formated'),
812 'fullmessage' => new external_value(PARAM_RAW
, 'The message'),
813 'fullmessageformat' => new external_format_value('fullmessage'),
814 'fullmessagehtml' => new external_value(PARAM_RAW
, 'The message in html'),
815 'smallmessage' => new external_value(PARAM_RAW
, 'The shorten message'),
816 'notification' => new external_value(PARAM_INT
, 'Is a notification?'),
817 'contexturl' => new external_value(PARAM_RAW
, 'Context URL'),
818 'contexturlname' => new external_value(PARAM_TEXT
, 'Context URL link name'),
819 'timecreated' => new external_value(PARAM_INT
, 'Time created'),
820 'timeread' => new external_value(PARAM_INT
, 'Time read'),
821 'usertofullname' => new external_value(PARAM_TEXT
, 'User to full name'),
822 'userfromfullname' => new external_value(PARAM_TEXT
, 'User from full name')
826 'warnings' => new external_warnings()
832 * Get blocked users parameters description.
834 * @return external_function_parameters
837 public static function get_blocked_users_parameters() {
838 return new external_function_parameters(
840 'userid' => new external_value(PARAM_INT
,
841 'the user whose blocked users we want to retrieve',
848 * Retrieve a list of users blocked
850 * @param int $userid the user whose blocked users we want to retrieve
851 * @return external_description
854 public static function get_blocked_users($userid) {
856 require_once($CFG->dirroot
. "/message/lib.php");
858 // Warnings array, it can be empty at the end but is mandatory.
865 $params = self
::validate_parameters(self
::get_blocked_users_parameters(), $params);
866 $userid = $params['userid'];
869 $context = context_system
::instance();
870 self
::validate_context($context);
872 // Check if private messaging between users is allowed.
873 if (empty($CFG->messaging
)) {
874 throw new moodle_exception('disabled', 'message');
877 $user = core_user
::get_user($userid, 'id', MUST_EXIST
);
879 // Check if we have permissions for retrieve the information.
880 if ($userid != $USER->id
and !has_capability('moodle/site:readallmessages', $context)) {
881 throw new moodle_exception('accessdenied', 'admin');
884 // Now, we can get safely all the blocked users.
885 $users = message_get_blocked_users($user);
887 $blockedusers = array();
888 foreach ($users as $user) {
891 'fullname' => fullname($user),
893 $newuser['profileimageurl'] = moodle_url
::make_webservice_pluginfile_url(
894 context_user
::instance($user->id
)->id
, 'user', 'icon', null, '/', 'f1')->out(false);
896 $blockedusers[] = $newuser;
900 'users' => $blockedusers,
901 'warnings' => $warnings
907 * Get blocked users return description.
909 * @return external_single_structure
912 public static function get_blocked_users_returns() {
913 return new external_single_structure(
915 'users' => new external_multiple_structure(
916 new external_single_structure(
918 'id' => new external_value(PARAM_INT
, 'User ID'),
919 'fullname' => new external_value(PARAM_NOTAGS
, 'User full name'),
920 'profileimageurl' => new external_value(PARAM_URL
, 'User picture URL', VALUE_OPTIONAL
)
923 'List of blocked users'
925 'warnings' => new external_warnings()
931 * Returns description of method parameters
933 * @return external_function_parameters
936 public static function mark_message_read_parameters() {
937 return new external_function_parameters(
939 'messageid' => new external_value(PARAM_INT
, 'id of the message (in the message table)'),
940 'timeread' => new external_value(PARAM_INT
, 'timestamp for when the message should be marked read')
946 * Mark a single message as read, trigger message_viewed event
948 * @param int $messageid id of the message (in the message table)
949 * @param int $timeread timestamp for when the message should be marked read
950 * @return external_description
951 * @throws invalid_parameter_exception
952 * @throws moodle_exception
955 public static function mark_message_read($messageid, $timeread) {
956 global $CFG, $DB, $USER;
957 require_once($CFG->dirroot
. "/message/lib.php");
959 // Check if private messaging between users is allowed.
960 if (empty($CFG->messaging
)) {
961 throw new moodle_exception('disabled', 'message');
964 // Warnings array, it can be empty at the end but is mandatory.
969 'messageid' => $messageid,
970 'timeread' => $timeread
972 $params = self
::validate_parameters(self
::mark_message_read_parameters(), $params);
975 $context = context_system
::instance();
976 self
::validate_context($context);
978 $message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST
);
980 if ($message->useridto
!= $USER->id
) {
981 throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read');
984 $messageid = message_mark_message_read($message, $params['timeread']);
987 'messageid' => $messageid,
988 'warnings' => $warnings
994 * Returns description of method result value
996 * @return external_description
999 public static function mark_message_read_returns() {
1000 return new external_single_structure(
1002 'messageid' => new external_value(PARAM_INT
, 'the id of the message in the message_read table'),
1003 'warnings' => new external_warnings()
1011 * Deprecated message external functions
1013 * @package core_message
1014 * @copyright 2011 Jerome Mouneyrac
1015 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1017 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1018 * @see core_notes_external
1020 class moodle_message_external
extends external_api
{
1023 * Returns description of method parameters
1025 * @return external_function_parameters
1027 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1028 * @see core_message_external::send_instant_messages_parameters()
1030 public static function send_instantmessages_parameters() {
1031 return core_message_external
::send_instant_messages_parameters();
1035 * Send private messages from the current USER to other users
1037 * @param array $messages An array of message to send.
1040 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1041 * @see core_message_external::send_instant_messages()
1043 public static function send_instantmessages($messages = array()) {
1044 return core_message_external
::send_instant_messages($messages);
1048 * Returns description of method result value
1050 * @return external_description
1052 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1053 * @see core_message_external::send_instant_messages_returns()
1055 public static function send_instantmessages_returns() {
1056 return core_message_external
::send_instant_messages_returns();
1060 * Marking the method as deprecated.
1064 public static function send_instantmessages_is_deprecated() {