Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/message/externallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once("$CFG->libdir/externallib.php");
00027 
00031 class core_message_external extends external_api {
00032 
00037     public static function send_instant_messages_parameters() {
00038         return new external_function_parameters(
00039             array(
00040                 'messages' => new external_multiple_structure(
00041                     new external_single_structure(
00042                         array(
00043                             'touserid' => new external_value(PARAM_INT, 'id of the user to send the private message'),
00044                             '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'),
00045                             '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),
00046                         )
00047                     )
00048                 )
00049             )
00050         );
00051     }
00052 
00059     public static function send_instant_messages($messages = array()) {
00060         global $CFG, $USER, $DB;
00061         require_once($CFG->dirroot . "/message/lib.php");
00062 
00063         //check if messaging is enabled
00064         if (!$CFG->messaging) {
00065             throw new moodle_exception('disabled', 'message');
00066         }
00067 
00068         // Ensure the current user is allowed to run this function
00069         $context = get_context_instance(CONTEXT_SYSTEM);
00070         self::validate_context($context);
00071         require_capability('moodle/site:sendmessage', $context);
00072 
00073         $params = self::validate_parameters(self::send_instant_messages_parameters(), array('messages' => $messages));
00074 
00075         //retrieve all tousers of the messages
00076         $receivers = array();
00077         foreach($params['messages'] as $message) {
00078             $receivers[] = $message['touserid'];
00079         }
00080         list($sqluserids, $sqlparams) = $DB->get_in_or_equal($receivers, SQL_PARAMS_NAMED, 'userid_');
00081         $tousers = $DB->get_records_select("user", "id " . $sqluserids . " AND deleted = 0", $sqlparams);
00082         $blocklist   = array();
00083         $contactlist = array();
00084         $sqlparams['contactid'] = $USER->id;
00085         $rs = $DB->get_recordset_sql("SELECT *
00086                                         FROM {message_contacts}
00087                                        WHERE userid $sqluserids
00088                                              AND contactid = :contactid", $sqlparams);
00089         foreach ($rs as $record) {
00090             if ($record->blocked) {
00091                 // $record->userid is blocking current user
00092                 $blocklist[$record->userid] = true;
00093             } else {
00094                 // $record->userid have current user as contact
00095                 $contactlist[$record->userid] = true;
00096             }
00097         }
00098         $rs->close();
00099 
00100         $canreadallmessages = has_capability('moodle/site:readallmessages', $context);
00101 
00102         $resultmessages = array();
00103         foreach ($params['messages'] as $message) {
00104             $text = clean_param($message['text'], PARAM_TEXT);
00105             $resultmsg = array(); //the infos about the success of the operation
00106 
00107             //we are going to do some checking
00108             //code should match /messages/index.php checks
00109             $success = true;
00110 
00111             //check the user exists
00112             if (empty($tousers[$message['touserid']])) {
00113                 $success = false;
00114                 $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']);
00115             }
00116 
00117             //check that the touser is not blocking the current user
00118             if ($success and !empty($blocklist[$message['touserid']]) and !$canreadallmessages) {
00119                 $success = false;
00120                 $errormessage = get_string('userisblockingyou', 'message');
00121             }
00122 
00123             // Check if the user is a contact
00124             //TODO: performance improvement - edit the function so we can pass an array instead userid
00125             $blocknoncontacts = get_user_preferences('message_blocknoncontacts', NULL, $message['touserid']);
00126             // message_blocknoncontacts option is on and current user is not in contact list
00127             if ($success && empty($contactlist[$message['touserid']]) && !empty($blocknoncontacts)) {
00128                 // The user isn't a contact and they have selected to block non contacts so this message won't be sent.
00129                 $success = false;
00130                 $errormessage = get_string('userisblockingyounoncontact', 'message');
00131             }
00132 
00133             //now we can send the message (at least try)
00134             if ($success) {
00135                 //TODO: performance improvement - edit the function so we can pass an array instead one touser object
00136                 $success = message_post_message($USER, $tousers[$message['touserid']], $text, FORMAT_MOODLE);
00137             }
00138 
00139             //build the resultmsg
00140             if (isset($message['clientmsgid'])) {
00141                 $resultmsg['clientmsgid'] = $message['clientmsgid'];
00142             }
00143             if ($success) {
00144                 $resultmsg['msgid'] = $success;
00145             } else {
00146                 $resultmsg['msgid'] = -1;
00147                 $resultmsg['errormessage'] = $errormessage;
00148             }
00149 
00150             $resultmessages[] = $resultmsg;
00151         }
00152 
00153         return $resultmessages;
00154     }
00155 
00160     public static function send_instant_messages_returns() {
00161         return new external_multiple_structure(
00162             new external_single_structure(
00163                 array(
00164                     'msgid' => new external_value(PARAM_INT, 'test this to know if it succeeds:  id of the created message if it succeeded, -1 when failed'),
00165                     'clientmsgid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the message', VALUE_OPTIONAL),
00166                     'errormessage' => new external_value(PARAM_TEXT, 'error message - if it failed', VALUE_OPTIONAL)
00167                 )
00168             )
00169         );
00170     }
00171 
00172 }
00173 
00178 class moodle_message_external extends external_api {
00179 
00185     public static function send_instantmessages_parameters() {
00186         return core_message_external::send_instant_messages_parameters();
00187     }
00188 
00195     public static function send_instantmessages($messages = array()) {
00196         return core_message_external::send_instant_messages($messages);
00197     }
00198 
00204     public static function send_instantmessages_returns() {
00205         return core_message_external::send_instant_messages_returns();
00206     }
00207 
00208 }
 All Data Structures Namespaces Files Functions Variables Enumerations