weekly release 2.3dev (blame stronk7 for 0202 mistake)
[moodle.git] / message / externallib.php
CommitLineData
a623b6b8
JM
1<?php
2
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.
14//
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/>.
17
18/**
19 * External message API
20 *
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
25 */
26require_once("$CFG->libdir/externallib.php");
27
5d1017e1
JM
28/**
29 * Message functions
30 */
31class core_message_external extends external_api {
a623b6b8
JM
32
33 /**
34 * Returns description of method parameters
35 * @return external_function_parameters
36 */
5d1017e1 37 public static function send_instant_messages_parameters() {
a623b6b8
JM
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),
46 )
47 )
48 )
49 )
50 );
51 }
52
53 /**
54 * Send private messages from the current USER to other users
55 *
56 * @param $messages An array of message to send.
57 * @return boolean
58 */
5d1017e1 59 public static function send_instant_messages($messages = array()) {
a623b6b8
JM
60 global $CFG, $USER, $DB;
61 require_once($CFG->dirroot . "/message/lib.php");
62
63 //check if messaging is enabled
64 if (!$CFG->messaging) {
65 throw new moodle_exception('disabled', 'message');
66 }
67
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);
72
5d1017e1 73 $params = self::validate_parameters(self::send_instant_messages_parameters(), array('messages' => $messages));
a623b6b8
JM
74
75 //retrieve all tousers of the messages
4de00da7 76 $receivers = array();
a623b6b8 77 foreach($params['messages'] as $message) {
4de00da7 78 $receivers[] = $message['touserid'];
a623b6b8 79 }
4de00da7 80 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_');
a623b6b8 81 $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
4de00da7
DC
82 $blocklist = array();
83 $contactlist = array();
a623b6b8 84 $sqlparams['contactid'] = $USER->id;
4de00da7
DC
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;
96 }
97 }
98 $rs->close();
a623b6b8
JM
99
100 $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
101
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
106
107 //we are going to do some checking
108 //code should match /messages/index.php checks
109 $success = true;
110
111 //check the user exists
112 if (empty($tousers[$message['touserid']])) {
113 $success = false;
114 $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
115 }
116
117 //check that the touser is not blocking the current user
4de00da7 118 if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
a623b6b8
JM
119 $success = false;
120 $errormessage = get_string('userisblockingyou', 'message');
121 }
122
78736e5d 123 // Check if the user is a contact
a623b6b8 124 //TODO: performance improvement - edit the function so we can pass an array instead userid
4de00da7
DC
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)) {
78736e5d
SH
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');
a623b6b8
JM
131 }
132
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);
137 }
138
139 //build the resultmsg
140 if (isset($message['clientmsgid'])) {
78736e5d 141 $resultmsg['clientmsgid'] = $message['clientmsgid'];
a623b6b8
JM
142 }
143 if ($success) {
144 $resultmsg['msgid'] = $success;
145 } else {
146 $resultmsg['msgid'] = -1;
147 $resultmsg['errormessage'] = $errormessage;
148 }
149
150 $resultmessages[] = $resultmsg;
151 }
152
153 return $resultmessages;
154 }
155
156 /**
157 * Returns description of method result value
158 * @return external_description
159 */
5d1017e1 160 public static function send_instant_messages_returns() {
a623b6b8
JM
161 return new external_multiple_structure(
162 new external_single_structure(
163 array(
78736e5d 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'),
4de00da7 165 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
78736e5d 166 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
a623b6b8
JM
167 )
168 )
169 );
170 }
171
172}
5d1017e1
JM
173
174/**
175 * Deprecated message functions
176 * @deprecated since Moodle 2.2 please use core_message_external instead
177 */
178class moodle_message_external extends external_api {
179
180 /**
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
184 */
185 public static function send_instantmessages_parameters() {
186 return core_message_external::send_instant_messages_parameters();
187 }
188
189 /**
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
194 */
195 public static function send_instantmessages($messages = array()) {
196 return core_message_external::send_instant_messages($messages);
197 }
198
199 /**
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
203 */
204 public static function send_instantmessages_returns() {
205 return core_message_external::send_instant_messages_returns();
206 }
207
208}