Merge branch 's11_MDL-27936_send_message_master' of github.com:dongsheng/moodle

Conflicts:
	lib/db/services.php
	version.php
This commit is contained in:
Eloy Lafuente (stronk7) 2011-06-29 20:00:49 +02:00
commit 1333c05ebe
3 changed files with 33 additions and 23 deletions

View file

@ -226,11 +226,11 @@ $functions = array(
// === message related functions === // === message related functions ===
'moodle_message_send_messages' => array( 'moodle_message_send_instantmessages' => array(
'classname' => 'moodle_message_external', 'classname' => 'moodle_message_external',
'methodname' => 'send_messages', 'methodname' => 'send_instantmessages',
'classpath' => 'message/externallib.php', 'classpath' => 'message/externallib.php',
'description' => 'Send messages', 'description' => 'Send instant messages',
'type' => 'write', 'type' => 'write',
'capabilities'=> 'moodle/site:sendmessage', 'capabilities'=> 'moodle/site:sendmessage',
), ),
@ -268,7 +268,7 @@ $services = array(
'moodle_notes_create_notes', 'moodle_notes_create_notes',
'moodle_user_get_course_participants_by_id', 'moodle_user_get_course_participants_by_id',
'moodle_user_get_users_by_courseid', 'moodle_user_get_users_by_courseid',
'moodle_message_send_messages'), 'moodle_message_send_instantmessages'),
'enabled' => 0, 'enabled' => 0,
'restrictedusers' => 0, 'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE 'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE

View file

@ -31,7 +31,7 @@ class moodle_message_external extends external_api {
* Returns description of method parameters * Returns description of method parameters
* @return external_function_parameters * @return external_function_parameters
*/ */
public static function send_messages_parameters() { public static function send_instantmessages_parameters() {
return new external_function_parameters( return new external_function_parameters(
array( array(
'messages' => new external_multiple_structure( 'messages' => new external_multiple_structure(
@ -53,7 +53,7 @@ class moodle_message_external extends external_api {
* @param $messages An array of message to send. * @param $messages An array of message to send.
* @return boolean * @return boolean
*/ */
public static function send_messages($messages = array()) { public static function send_instantmessages($messages = array()) {
global $CFG, $USER, $DB; global $CFG, $USER, $DB;
require_once($CFG->dirroot . "/message/lib.php"); require_once($CFG->dirroot . "/message/lib.php");
@ -67,24 +67,32 @@ class moodle_message_external extends external_api {
self::validate_context($context); self::validate_context($context);
require_capability('moodle/site:sendmessage', $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 //retrieve all tousers of the messages
$touserids = array(); $receivers = array();
foreach($params['messages'] as $message) { 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); $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
$blocklist = array();
//retrieve the tousers who are blocking the $USER $contactlist = array();
$sqlparams['contactid'] = $USER->id; $sqlparams['contactid'] = $USER->id;
$sqlparams['blocked'] = 1; $rs = $DB->get_recordset_sql("SELECT *
//Note: return userid field should be unique for the below request, FROM {message_contacts}
//so we'll use this field as key of $blockingcontacts WHERE userid $sqluserids
$blockingcontacts = $DB->get_records_select("message_contacts", AND contactid = :contactid", $sqlparams);
"userid " . $sqluserids . " AND contactid = :contactid AND blocked = :blocked", foreach ($rs as $record) {
$sqlparams, '', "userid"); 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); $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
@ -104,14 +112,16 @@ class moodle_message_external extends external_api {
} }
//check that the touser is not blocking the current user //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; $success = false;
$errormessage = get_string('userisblockingyou', 'message'); $errormessage = get_string('userisblockingyou', 'message');
} }
// Check if the user is a contact // Check if the user is a contact
//TODO: performance improvement - edit the function so we can pass an array instead userid //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. // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
$success = false; $success = false;
$errormessage = get_string('userisblockingyounoncontact', 'message'); $errormessage = get_string('userisblockingyounoncontact', 'message');
@ -144,12 +154,12 @@ class moodle_message_external extends external_api {
* Returns description of method result value * Returns description of method result value
* @return external_description * @return external_description
*/ */
public static function send_messages_returns() { public static function send_instantmessages_returns() {
return new external_multiple_structure( return new external_multiple_structure(
new external_single_structure( new external_single_structure(
array( 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'), '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) 'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
) )
) )

View file

@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die();
$version = 2011062700.03; // YYYYMMDD = weekly release date of this DEV branch $version = 2011062700.04; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches // RR = release increments - 00 in DEV branches
// .XX = incremental changes // .XX = incremental changes