Permalink
Browse files

MDL-27936, send_message webservice should respect contact settings, i…

…mprove readability, rename it to moodle_message_send_instantmessages
  • Loading branch information...
dcai committed Jun 29, 2011
1 parent 7fde489 commit 4de00da79112722e95fb6d939691342980856559
Showing with 33 additions and 23 deletions.
  1. +4 −4 lib/db/services.php
  2. +28 −18 message/externallib.php
  3. +1 −1 version.php
View
@@ -217,11 +217,11 @@
// === message related functions ===
- 'moodle_message_send_messages' => array(
+ 'moodle_message_send_instantmessages' => array(
'classname' => 'moodle_message_external',
- 'methodname' => 'send_messages',
+ 'methodname' => 'send_instantmessages',
'classpath' => 'message/externallib.php',
- 'description' => 'Send messages',
+ 'description' => 'Send instant messages',
'type' => 'write',
'capabilities'=> 'moodle/site:sendmessage',
),
@@ -258,7 +258,7 @@
'moodle_webservice_get_siteinfo',
'moodle_notes_create_notes',
'moodle_user_get_course_participants_by_id',
- 'moodle_message_send_messages'),
+ 'moodle_message_send_instantmessages'),
'enabled' => 0,
'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE
View
@@ -31,7 +31,7 @@ class moodle_message_external extends external_api {
* Returns description of method parameters
* @return external_function_parameters
*/
- public static function send_messages_parameters() {
+ public static function send_instantmessages_parameters() {
return new external_function_parameters(
array(
'messages' => new external_multiple_structure(
@@ -53,7 +53,7 @@ public static function send_messages_parameters() {
* @param $messages An array of message to send.
* @return boolean
*/
- public static function send_messages($messages = array()) {
+ public static function send_instantmessages($messages = array()) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . "/message/lib.php");
@@ -67,24 +67,32 @@ public static function send_messages($messages = array()) {
self::validate_context($context);
require_capability('moodle/site:sendmessage', $context);
- $params = self::validate_parameters(self::send_messages_parameters(), array('messages' => $messages));
+ $params = self::validate_parameters(self::send_instantmessages_parameters(), array('messages' => $messages));
//retrieve all tousers of the messages
- $touserids = array();
+ $receivers = array();
foreach($params['messages'] as $message) {
- $touserids[] = $message['touserid'];
+ $receivers[] = $message['touserid'];
}
- list($sqluserids, $sqlparams) = $DB->get_in_or_equal($touserids, SQL_PARAMS_NAMED, 'userid_');
+ list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_');
$tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
-
- //retrieve the tousers who are blocking the $USER
+ $blocklist = array();
+ $contactlist = array();
$sqlparams['contactid'] = $USER->id;
- $sqlparams['blocked'] = 1;
- //Note: return userid field should be unique for the below request,
- //so we'll use this field as key of $blockingcontacts
- $blockingcontacts = $DB->get_records_select("message_contacts",
- "userid " . $sqluserids . " AND contactid = :contactid AND blocked = :blocked",
- $sqlparams, '', "userid");
+ $rs = $DB->get_recordset_sql("SELECT *
+ FROM {message_contacts}
+ WHERE userid $sqluserids
+ AND contactid = :contactid", $sqlparams);
+ foreach ($rs as $record) {
+ if ($record->blocked) {
+ // $record->userid is blocking current user
+ $blocklist[$record->userid] = true;
+ } else {
+ // $record->userid have current user as contact
+ $contactlist[$record->userid] = true;
+ }
+ }
+ $rs->close();
$canreadallmessages = has_capability('moodle/site:readallmessages', $context);
@@ -104,14 +112,16 @@ public static function send_messages($messages = array()) {
}
//check that the touser is not blocking the current user
- if ($success and isset($blockingcontacts[$message['touserid']]) and !$canreadallmessages) {
+ if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
$success = false;
$errormessage = get_string('userisblockingyou', 'message');
}
// Check if the user is a contact
//TODO: performance improvement - edit the function so we can pass an array instead userid
- if ($success && empty($contact) && get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']) == null) {
+ $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
+ // message_blocknoncontacts option is on and current user is not in contact list
+ if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
// The user isn't a contact and they have selected to block non contacts so this message won't be sent.
$success = false;
$errormessage = get_string('userisblockingyounoncontact', 'message');
@@ -144,12 +154,12 @@ public static function send_messages($messages = array()) {
* Returns description of method result value
* @return external_description
*/
- public static function send_messages_returns() {
+ public static function send_instantmessages_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
- 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
'msgid' => new external_value(PARAM_INT, 'test this to know if it succeeds: id of the created message if it succeeded, -1 when failed'),
+ 'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
)
)
View
@@ -31,7 +31,7 @@
-$version = 2011062700.00; // YYYYMMDD = weekly release date of this DEV branch
+$version = 2011062700.02; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

0 comments on commit 4de00da

Please sign in to comment.