From a623b6b892b7516309e8e2c16fdbf5f6e2e32b1b Mon Sep 17 00:00:00 2001 From: Jerome Mouneyrac Date: Tue, 7 Jun 2011 16:40:55 +0800 Subject: [PATCH] MDL-27566-wip implement moodle_message_send_messages function (matching front end logic - TODO: 2 core functions to change to support bulk operations) --- lang/en/message.php | 1 + lib/db/services.php | 11 ++ message/externallib.php | 161 +++++++++++++++++++++++ version.php | 2 +- webservice/simpletest/testwebservice.php | 19 ++- 5 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 message/externallib.php diff --git a/lang/en/message.php b/lang/en/message.php index fa805a3bbf3..cabafc77814 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -112,6 +112,7 @@ $string['showmessagewindow'] = 'Popup window on new message'; $string['strftimedaydatetime'] = '%A, %d %B %Y, %I:%M %p'; $string['timenosee'] = 'Minutes since I was last seen online'; $string['timesent'] = 'Time sent'; +$string['touserdoesntexist'] = 'You can not send a message to a user id ({$a}) that doesn\'t exist'; $string['unblockcontact'] = 'Unblock contact'; $string['unreadmessages'] = 'Unread messages ({$a})'; $string['unreadnewmessages'] = 'New messages ({$a})'; diff --git a/lib/db/services.php b/lib/db/services.php index 51cf05bae67..4aeae375eee 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -206,4 +206,15 @@ $functions = array( 'capabilities'=> 'moodle/course:create,moodle/course:visibility', ), + // === message related functions === + + 'moodle_message_send_messages' => array( + 'classname' => 'moodle_message_external', + 'methodname' => 'send_messages', + 'classpath' => 'message/externallib.php', + 'description' => 'Send messages', + 'type' => 'write', + 'capabilities'=> 'moodle/site:sendmessage', + ), + ); diff --git a/message/externallib.php b/message/externallib.php new file mode 100644 index 00000000000..02dd156fa05 --- /dev/null +++ b/message/externallib.php @@ -0,0 +1,161 @@ +. + +/** + * External message API + * + * @package moodlecore + * @subpackage message + * @copyright 2011 Moodle Pty Ltd (http://moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require_once("$CFG->libdir/externallib.php"); + +class moodle_message_external extends external_api { + + /** + * Returns description of method parameters + * @return external_function_parameters + */ + public static function send_messages_parameters() { + return new external_function_parameters( + array( + 'messages' => new external_multiple_structure( + new external_single_structure( + array( + 'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'), + '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'), + '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), + ) + ) + ) + ) + ); + } + + /** + * Send private messages from the current USER to other users + * + * @param $messages An array of message to send. + * @return boolean + */ + public static function send_messages($messages = array()) { + global $CFG, $USER, $DB; + require_once($CFG->dirroot . "/message/lib.php"); + + //check if messaging is enabled + if (!$CFG->messaging) { + throw new moodle_exception('disabled', 'message'); + } + + // Ensure the current user is allowed to run this function + $context = get_context_instance(CONTEXT_SYSTEM); + self::validate_context($context); + require_capability('moodle/site:sendmessage', $context); + + $params = self::validate_parameters(self::send_messages_parameters(), array('messages' => $messages)); + + //retrieve all tousers of the messages + $touserids = array(); + foreach($params['messages'] as $message) { + $touserids[] = $message['touserid']; + } + list($sqluserids, $sqlparams) = $DB->get_in_or_equal($touserids, SQL_PARAMS_NAMED, 'userid_'); + $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams); + + //retrieve the tousers who are blocking the $USER + $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"); + + $canreadallmessages = has_capability('moodle/site:readallmessages', $context); + + $resultmessages = array(); + foreach ($params['messages'] as $message) { + $text = clean_param($message['text'], PARAM_TEXT); + $resultmsg = array(); //the infos about the success of the operation + + //we are going to do some checking + //code should match /messages/index.php checks + $success = true; + + //check the user exists + if (empty($tousers[$message['touserid']])) { + $success = false; + $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']); + } + + //check that the touser is not blocking the current user + if ($success and isset($blockingcontacts[$message['touserid']]) and !$canreadallmessages) { + $success = false; + $errormessage = get_string('userisblockingyou', 'message'); + } + + //check that user preference + //TODO: performance improvement - edit the function so we can pass an array instead userid + if ($success and empty($contact)) { + $userpreferences = get_user_preferences(NULL, NULL, $message['touserid']); + if (!empty($userpreferences['message_blocknoncontacts'])) { + $success = false; + $errormessage = get_string('userisblockingyounoncontact', 'message'); + } + } + + //now we can send the message (at least try) + if ($success) { + //TODO: performance improvement - edit the function so we can pass an array instead one touser object + $success = message_post_message($USER, $tousers[$message['touserid']], $text, FORMAT_MOODLE); + } + + //build the resultmsg + if (isset($message['clientmsgid'])) { + $resultmsg['clientmsgid'] = $message['clientmsgid']; + } + if ($success) { + $resultmsg['msgid'] = $success; + } else { + $resultmsg['msgid'] = -1; + $resultmsg['errormessage'] = $errormessage; + } + + $resultmessages[] = $resultmsg; + } + + return $resultmessages; + } + + /** + * Returns description of method result value + * @return external_description + */ + public static function send_messages_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 success: id of the created message when successed, -1 when failed'), + 'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL) + ) + ) + ); + } + +} diff --git a/version.php b/version.php index caaa6ff2258..f2338359d9b 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2011060200.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2011060200.036; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes diff --git a/webservice/simpletest/testwebservice.php b/webservice/simpletest/testwebservice.php index cbc299cd2c3..6884404a0ed 100644 --- a/webservice/simpletest/testwebservice.php +++ b/webservice/simpletest/testwebservice.php @@ -88,7 +88,8 @@ class webservice_test extends UnitTestCase { 'moodle_group_delete_groupmembers' => false, 'moodle_group_create_groups' => false, 'moodle_group_delete_groups' => false, - 'moodle_enrol_manual_enrol_users' => false + 'moodle_enrol_manual_enrol_users' => false, + 'moodle_message_send_messages' => false ); //performance testing: number of time the web service are run @@ -1482,7 +1483,23 @@ class webservice_test extends UnitTestCase { //delete the category $DB->delete_records('course_categories', array('id' => $category->id)); + } + function moodle_message_send_messages($client) { + global $DB; + $function = 'moodle_message_send_messages'; + $message = array(); + $message['text'] = 'this is a message with a link http://www.google.com'; + $message['touserid'] = 2; //replace by a existing user id + $message['clientmsgid'] = 'message_1'; + $message2 = array(); + $message2['text'] = 'this is a message with an image + http://moodle.org/pluginfile.php/51/mod_forum/post/713724/moodle2-logo.png'; + $message2['touserid'] = 2; //replace by a existing user id + $message2['clientmsgid'] = 'message_2'; + $params = array('messages' => array($message, $message2)); + $success = $client->call($function, $params); + $this->assertEqual(count($success), 2); } }