2011-06-07 16:40:55 +08:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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 " );
2011-10-18 12:57:33 +08:00
/**
* Message functions
*/
class core_message_external extends external_api {
2011-06-07 16:40:55 +08:00
/**
* Returns description of method parameters
* @ return external_function_parameters
*/
2011-10-18 12:57:33 +08:00
public static function send_instant_messages_parameters () {
2011-06-07 16:40:55 +08:00
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
*/
2011-10-18 12:57:33 +08:00
public static function send_instant_messages ( $messages = array ()) {
2011-06-07 16:40:55 +08:00
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 );
2011-10-18 12:57:33 +08:00
$params = self :: validate_parameters ( self :: send_instant_messages_parameters (), array ( 'messages' => $messages ));
2011-06-07 16:40:55 +08:00
//retrieve all tousers of the messages
2011-06-29 11:26:16 +08:00
$receivers = array ();
2011-06-07 16:40:55 +08:00
foreach ( $params [ 'messages' ] as $message ) {
2011-06-29 11:26:16 +08:00
$receivers [] = $message [ 'touserid' ];
2011-06-07 16:40:55 +08:00
}
2011-06-29 11:26:16 +08:00
list ( $sqluserids , $sqlparams ) = $DB -> get_in_or_equal ( $receivers , SQL_PARAMS_NAMED , 'userid_' );
2011-06-07 16:40:55 +08:00
$tousers = $DB -> get_records_select ( " user " , " id " . $sqluserids . " AND deleted = 0 " , $sqlparams );
2011-06-29 11:26:16 +08:00
$blocklist = array ();
$contactlist = array ();
2011-06-07 16:40:55 +08:00
$sqlparams [ 'contactid' ] = $USER -> id ;
2011-06-29 11:26:16 +08:00
$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 ();
2011-06-07 16:40:55 +08:00
$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
2011-06-29 11:26:16 +08:00
if ( $success and ! empty ( $blocklist [ $message [ 'touserid' ]]) and ! $canreadallmessages ) {
2011-06-07 16:40:55 +08:00
$success = false ;
$errormessage = get_string ( 'userisblockingyou' , 'message' );
}
2011-06-08 10:02:59 +08:00
// Check if the user is a contact
2011-06-07 16:40:55 +08:00
//TODO: performance improvement - edit the function so we can pass an array instead userid
2011-06-29 11:26:16 +08:00
$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 )) {
2011-06-08 10:02:59 +08:00
// 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' );
2011-06-07 16:40:55 +08:00
}
//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' ])) {
2011-06-08 10:02:59 +08:00
$resultmsg [ 'clientmsgid' ] = $message [ 'clientmsgid' ];
2011-06-07 16:40:55 +08:00
}
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
*/
2011-10-18 12:57:33 +08:00
public static function send_instant_messages_returns () {
2011-06-07 16:40:55 +08:00
return new external_multiple_structure (
new external_single_structure (
array (
2011-06-08 10:02:59 +08:00
'msgid' => new external_value ( PARAM_INT , 'test this to know if it succeeds: id of the created message if it succeeded, -1 when failed' ),
2011-06-29 11:26:16 +08:00
'clientmsgid' => new external_value ( PARAM_ALPHANUMEXT , 'your own id for the message' , VALUE_OPTIONAL ),
2011-06-08 10:02:59 +08:00
'errormessage' => new external_value ( PARAM_TEXT , 'error message - if it failed' , VALUE_OPTIONAL )
2011-06-07 16:40:55 +08:00
)
)
);
}
}
2011-10-18 12:57:33 +08:00
/**
* Deprecated message functions
* @ deprecated since Moodle 2.2 please use core_message_external instead
*/
class moodle_message_external extends external_api {
/**
* Returns description of method parameters
* @ deprecated since Moodle 2.2 please use core_message_external :: send_instant_messages_parameters instead
* @ return external_function_parameters
*/
public static function send_instantmessages_parameters () {
return core_message_external :: send_instant_messages_parameters ();
}
/**
* Send private messages from the current USER to other users
* @ deprecated since Moodle 2.2 please use core_message_external :: send_instant_messages instead
* @ param $messages An array of message to send .
* @ return boolean
*/
public static function send_instantmessages ( $messages = array ()) {
return core_message_external :: send_instant_messages ( $messages );
}
/**
* Returns description of method result value
* @ deprecated since Moodle 2.2 please use core_message_external :: send_instant_messages_returns instead
* @ return external_description
*/
public static function send_instantmessages_returns () {
return core_message_external :: send_instant_messages_returns ();
}
}