emergency release 2.2dev
[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
28class moodle_message_external extends external_api {
29
30 /**
31 * Returns description of method parameters
32 * @return external_function_parameters
33 */
4de00da7 34 public static function send_instantmessages_parameters() {
a623b6b8
JM
35 return new external_function_parameters(
36 array(
37 'messages' => new external_multiple_structure(
38 new external_single_structure(
39 array(
40 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'),
41 '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'),
42 '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),
43 )
44 )
45 )
46 )
47 );
48 }
49
50 /**
51 * Send private messages from the current USER to other users
52 *
53 * @param $messages An array of message to send.
54 * @return boolean
55 */
4de00da7 56 public static function send_instantmessages($messages = array()) {
a623b6b8
JM
57 global $CFG, $USER, $DB;
58 require_once($CFG->dirroot . "/message/lib.php");
59
60 //check if messaging is enabled
61 if (!$CFG->messaging) {
62 throw new moodle_exception('disabled', 'message');
63 }
64
65 // Ensure the current user is allowed to run this function
66 $context = get_context_instance(CONTEXT_SYSTEM);
67 self::validate_context($context);
68 require_capability('moodle/site:sendmessage', $context);
69
4de00da7 70 $params = self::validate_parameters(self::send_instantmessages_parameters(), array('messages' => $messages));
a623b6b8
JM
71
72 //retrieve all tousers of the messages
4de00da7 73 $receivers = array();
a623b6b8 74 foreach($params['messages'] as $message) {
4de00da7 75 $receivers[] = $message['touserid'];
a623b6b8 76 }
4de00da7 77 list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_');
a623b6b8 78 $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
4de00da7
DC
79 $blocklist = array();
80 $contactlist = array();
a623b6b8 81 $sqlparams['contactid'] = $USER->id;
4de00da7
DC
82 $rs = $DB->get_recordset_sql("SELECT *
83 FROM {message_contacts}
84 WHERE userid $sqluserids
85 AND contactid = :contactid", $sqlparams);
86 foreach ($rs as $record) {
87 if ($record->blocked) {
88 // $record->userid is blocking current user
89 $blocklist[$record->userid] = true;
90 } else {
91 // $record->userid have current user as contact
92 $contactlist[$record->userid] = true;
93 }
94 }
95 $rs->close();
a623b6b8
JM
96
97 $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
98
99 $resultmessages = array();
100 foreach ($params['messages'] as $message) {
101 $text = clean_param($message['text'], PARAM_TEXT);
102 $resultmsg = array(); //the infos about the success of the operation
103
104 //we are going to do some checking
105 //code should match /messages/index.php checks
106 $success = true;
107
108 //check the user exists
109 if (empty($tousers[$message['touserid']])) {
110 $success = false;
111 $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
112 }
113
114 //check that the touser is not blocking the current user
4de00da7 115 if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
a623b6b8
JM
116 $success = false;
117 $errormessage = get_string('userisblockingyou', 'message');
118 }
119
78736e5d 120 // Check if the user is a contact
a623b6b8 121 //TODO: performance improvement - edit the function so we can pass an array instead userid
4de00da7
DC
122 $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
123 // message_blocknoncontacts option is on and current user is not in contact list
124 if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
78736e5d
SH
125 // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
126 $success = false;
127 $errormessage = get_string('userisblockingyounoncontact', 'message');
a623b6b8
JM
128 }
129
130 //now we can send the message (at least try)
131 if ($success) {
132 //TODO: performance improvement - edit the function so we can pass an array instead one touser object
133 $success = message_post_message($USER, $tousers[$message['touserid']], $text, FORMAT_MOODLE);
134 }
135
136 //build the resultmsg
137 if (isset($message['clientmsgid'])) {
78736e5d 138 $resultmsg['clientmsgid'] = $message['clientmsgid'];
a623b6b8
JM
139 }
140 if ($success) {
141 $resultmsg['msgid'] = $success;
142 } else {
143 $resultmsg['msgid'] = -1;
144 $resultmsg['errormessage'] = $errormessage;
145 }
146
147 $resultmessages[] = $resultmsg;
148 }
149
150 return $resultmessages;
151 }
152
153 /**
154 * Returns description of method result value
155 * @return external_description
156 */
4de00da7 157 public static function send_instantmessages_returns() {
a623b6b8
JM
158 return new external_multiple_structure(
159 new external_single_structure(
160 array(
78736e5d 161 '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 162 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
78736e5d 163 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
a623b6b8
JM
164 )
165 )
166 );
167 }
168
169}