Merge branch 'wip-mdl-30121-m22' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / message / externallib.php
blob3e5a03a8b548ec370a3548b8120ec3de709af78c
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * External message API
21 * @package moodlecore
22 * @subpackage message
23 * @copyright 2011 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->libdir/externallib.php");
28 /**
29 * Message functions
31 class core_message_external extends external_api {
33 /**
34 * Returns description of method parameters
35 * @return external_function_parameters
37 public static function send_instant_messages_parameters() {
38 return new external_function_parameters(
39 array(
40 'messages' => new external_multiple_structure(
41 new external_single_structure(
42 array(
43 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'),
44 'text' => new external_value(PARAM_RAW, 'the text of the message - not that you can send anything it will be automatically cleaned to PARAM_TEXT and used againt MOODLE_FORMAT'),
45 '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),
53 /**
54 * Send private messages from the current USER to other users
56 * @param $messages An array of message to send.
57 * @return boolean
59 public static function send_instant_messages($messages = array()) {
60 global $CFG, $USER, $DB;
61 require_once($CFG->dirroot . "/message/lib.php");
63 //check if messaging is enabled
64 if (!$CFG->messaging) {
65 throw new moodle_exception('disabled', 'message');
68 // Ensure the current user is allowed to run this function
69 $context = get_context_instance(CONTEXT_SYSTEM);
70 self::validate_context($context);
71 require_capability('moodle/site:sendmessage', $context);
73 $params = self::validate_parameters(self::send_instant_messages_parameters(), array('messages' => $messages));
75 //retrieve all tousers of the messages
76 $receivers = array();
77 foreach($params['messages'] as $message) {
78 $receivers[] = $message['touserid'];
80 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_');
81 $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
82 $blocklist = array();
83 $contactlist = array();
84 $sqlparams['contactid'] = $USER->id;
85 $rs = $DB->get_recordset_sql("SELECT *
86 FROM {message_contacts}
87 WHERE userid $sqluserids
88 AND contactid = :contactid", $sqlparams);
89 foreach ($rs as $record) {
90 if ($record->blocked) {
91 // $record->userid is blocking current user
92 $blocklist[$record->userid] = true;
93 } else {
94 // $record->userid have current user as contact
95 $contactlist[$record->userid] = true;
98 $rs->close();
100 $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
102 $resultmessages = array();
103 foreach ($params['messages'] as $message) {
104 $text = clean_param($message['text'], PARAM_TEXT);
105 $resultmsg = array(); //the infos about the success of the operation
107 //we are going to do some checking
108 //code should match /messages/index.php checks
109 $success = true;
111 //check the user exists
112 if (empty($tousers[$message['touserid']])) {
113 $success = false;
114 $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
117 //check that the touser is not blocking the current user
118 if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
119 $success = false;
120 $errormessage = get_string('userisblockingyou', 'message');
123 // Check if the user is a contact
124 //TODO: performance improvement - edit the function so we can pass an array instead userid
125 $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
126 // message_blocknoncontacts option is on and current user is not in contact list
127 if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
128 // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
129 $success = false;
130 $errormessage = get_string('userisblockingyounoncontact', 'message');
133 //now we can send the message (at least try)
134 if ($success) {
135 //TODO: performance improvement - edit the function so we can pass an array instead one touser object
136 $success = message_post_message($USER, $tousers[$message['touserid']], $text, FORMAT_MOODLE);
139 //build the resultmsg
140 if (isset($message['clientmsgid'])) {
141 $resultmsg['clientmsgid'] = $message['clientmsgid'];
143 if ($success) {
144 $resultmsg['msgid'] = $success;
145 } else {
146 $resultmsg['msgid'] = -1;
147 $resultmsg['errormessage'] = $errormessage;
150 $resultmessages[] = $resultmsg;
153 return $resultmessages;
157 * Returns description of method result value
158 * @return external_description
160 public static function send_instant_messages_returns() {
161 return new external_multiple_structure(
162 new external_single_structure(
163 array(
164 'msgid' => new external_value(PARAM_INT, 'test this to know if it succeeds: id of the created message if it succeeded, -1 when failed'),
165 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
166 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
175 * Deprecated message functions
176 * @deprecated since Moodle 2.2 please use core_message_external instead
178 class moodle_message_external extends external_api {
181 * Returns description of method parameters
182 * @deprecated since Moodle 2.2 please use core_message_external::send_instant_messages_parameters instead
183 * @return external_function_parameters
185 public static function send_instantmessages_parameters() {
186 return core_message_external::send_instant_messages_parameters();
190 * Send private messages from the current USER to other users
191 * @deprecated since Moodle 2.2 please use core_message_external::send_instant_messages instead
192 * @param $messages An array of message to send.
193 * @return boolean
195 public static function send_instantmessages($messages = array()) {
196 return core_message_external::send_instant_messages($messages);
200 * Returns description of method result value
201 * @deprecated since Moodle 2.2 please use core_message_external::send_instant_messages_returns instead
202 * @return external_description
204 public static function send_instantmessages_returns() {
205 return core_message_external::send_instant_messages_returns();