|
Moodle
2.2.1
http://www.collinsharper.com
|
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('../config.php'); 00027 require_once('lib.php'); 00028 require_once('send_form.php'); 00029 00030 require_login(0, false); 00031 00032 if (isguestuser()) { 00033 redirect($CFG->wwwroot); 00034 } 00035 00036 if (empty($CFG->messaging)) { 00037 print_error('disabled', 'message'); 00038 } 00039 00040 //'viewing' is the preferred URL parameter but we'll still accept usergroup in case its referenced externally 00041 $usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT); 00042 $viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT); 00043 00044 $history = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT); 00045 $search = optional_param('search', '', PARAM_CLEAN); //TODO: use PARAM_RAW, but make sure we use s() and p() properly 00046 00047 //the same param as 1.9 and the param we have been logging. Use this parameter. 00048 $user1id = optional_param('user1', $USER->id, PARAM_INT); 00049 //2.0 shipped using this param. Retaining it only for compatibility. It should be removed. 00050 $user1id = optional_param('user', $user1id, PARAM_INT); 00051 00052 //the same param as 1.9 and the param we have been logging. Use this parameter. 00053 $user2id = optional_param('user2', 0, PARAM_INT); 00054 //The class send_form supplies the receiving user id as 'id' 00055 $user2id = optional_param('id', $user2id, PARAM_INT); 00056 00057 $addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact 00058 $removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact 00059 $blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact 00060 $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact 00061 00062 //for search 00063 $advancedsearch = optional_param('advanced', 0, PARAM_INT); 00064 00065 //if they have numerous contacts or are viewing course participants we might need to page through them 00066 $page = optional_param('page', 0, PARAM_INT); 00067 00068 $url = new moodle_url('/message/index.php'); 00069 00070 if ($user2id !== 0) { 00071 $url->param('user2', $user2id); 00072 } 00073 00074 if ($user2id !== 0) { 00075 //Switch view back to contacts if: 00076 //1) theyve searched and selected a user 00077 //2) they've viewed recent messages or notifications and clicked through to a user 00078 if ($viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) { 00079 $viewing = MESSAGE_VIEW_CONTACTS; 00080 } 00081 } 00082 $url->param('viewing', $viewing); 00083 00084 $PAGE->set_url($url); 00085 00086 $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id)); 00087 $PAGE->navigation->extend_for_user($USER); 00088 $PAGE->set_pagelayout('course'); 00089 00090 // Disable message notification popups while the user is viewing their messages 00091 $PAGE->set_popup_notification_allowed(false); 00092 00093 $context = get_context_instance(CONTEXT_SYSTEM); 00094 00095 $user1 = null; 00096 $currentuser = true; 00097 $showcontactactionlinks = true; 00098 if ($user1id != $USER->id) { 00099 $user1 = $DB->get_record('user', array('id' => $user1id)); 00100 if (!$user1) { 00101 print_error('invaliduserid'); 00102 } 00103 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements 00104 $showcontactactionlinks = false; 00105 } else { 00106 $user1 = $USER; 00107 } 00108 unset($user1id); 00109 00110 $user2 = null; 00111 if (!empty($user2id)) { 00112 $user2 = $DB->get_record("user", array("id" => $user2id)); 00113 if (!$user2) { 00114 print_error('invaliduserid'); 00115 } 00116 } 00117 unset($user2id); 00118 00119 //the current user isnt involved in this discussion at all 00120 if ($user1->id != $USER->id && (!empty($user2) && $user2->id != $USER->id) && !has_capability('moodle/site:readallmessages', $context)) { 00121 print_error('accessdenied','admin'); 00122 } 00123 00125 if ($addcontact and confirm_sesskey()) { 00126 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&user2='.$USER->id, $addcontact); 00127 message_add_contact($addcontact); 00128 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact); 00129 } 00130 if ($removecontact and confirm_sesskey()) { 00131 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&user2='.$USER->id, $removecontact); 00132 message_remove_contact($removecontact); 00133 } 00134 if ($blockcontact and confirm_sesskey()) { 00135 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&user2='.$USER->id, $blockcontact); 00136 message_block_contact($blockcontact); 00137 } 00138 if ($unblockcontact and confirm_sesskey()) { 00139 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&user2='.$USER->id, $unblockcontact); 00140 message_unblock_contact($unblockcontact); 00141 } 00142 00143 //was a message sent? Do NOT allow someone looking at someone else's messages to send them. 00144 $messageerror = null; 00145 if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) { 00146 // Check that the user is not blocking us!! 00147 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) { 00148 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) { 00149 $messageerror = get_string('userisblockingyou', 'message'); 00150 } 00151 } 00152 $userpreferences = get_user_preferences(NULL, NULL, $user2->id); 00153 00154 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts 00155 if (empty($contact)) { // We are not a contact! 00156 $messageerror = get_string('userisblockingyounoncontact', 'message'); 00157 } 00158 } 00159 00160 if (empty($messageerror)) { 00161 $mform = new send_form(); 00162 $defaultmessage = new stdClass; 00163 $defaultmessage->id = $user2->id; 00164 $defaultmessage->message = ''; 00165 00166 //Check if the current user has sent a message 00167 $data = $mform->get_data(); 00168 if (!empty($data) && !empty($data->message)) { 00169 if (!confirm_sesskey()) { 00170 print_error('invalidsesskey'); 00171 } 00172 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE); 00173 if (!empty($messageid)) { 00174 //including the id of the user sending the message in the logged URL so the URL works for admins 00175 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read 00176 add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id); 00177 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id); 00178 } 00179 } 00180 } 00181 } 00182 00183 $strmessages = get_string('messages', 'message'); 00184 if (!empty($user2)) { 00185 $user2fullname = fullname($user2); 00186 00187 $PAGE->set_title("$strmessages: $user2fullname"); 00188 $PAGE->set_heading("$strmessages: $user2fullname"); 00189 } else { 00190 $PAGE->set_title("{$SITE->shortname}: $strmessages"); 00191 $PAGE->set_heading("{$SITE->shortname}: $strmessages"); 00192 } 00193 00194 //now the page contents 00195 echo $OUTPUT->header(); 00196 00197 echo $OUTPUT->box_start('message'); 00198 00199 $countunread = 0; //count of unread messages from $user2 00200 $countunreadtotal = 0; //count of unread messages from all users 00201 00202 //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread 00203 $viewingnewmessages = false; 00204 if (!empty($user2)) { 00205 //are there any unread messages from $user2 00206 $countunread = message_count_unread_messages($user1, $user2); 00207 if ($countunread>0) { 00208 //mark the messages we're going to display as read 00209 message_mark_messages_read($user1->id, $user2->id); 00210 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { 00211 $viewingnewmessages = true; 00212 } 00213 } 00214 } 00215 $countunreadtotal = message_count_unread_messages($user1); 00216 00217 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) { 00218 //default to showing the search 00219 $viewing = MESSAGE_VIEW_SEARCH; 00220 } 00221 00222 $blockedusers = message_get_blocked_users($user1, $user2); 00223 $countblocked = count($blockedusers); 00224 00225 list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2); 00226 00227 message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page); 00228 00229 echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align')); 00230 if (!empty($user2)) { 00231 00232 echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory')); 00233 00234 $visible = 'visible'; 00235 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else 00236 00237 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible; 00238 if ($history == MESSAGE_HISTORY_ALL) { 00239 $displaycount = 0; 00240 00241 $recentlabelclass = $historylinkclass = $hidden; 00242 } else if($viewingnewmessages) { 00243 //if user is viewing new messages only show them the new messages 00244 $displaycount = $countunread; 00245 00246 $recentlabelclass = $historylabelclass = $hidden; 00247 } else { 00248 //default to only showing the last few messages 00249 $displaycount = MESSAGE_SHORTVIEW_LIMIT; 00250 00251 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) { 00252 $displaycount = $countunread; 00253 } 00254 00255 $recentlinkclass = $historylabelclass = $hidden; 00256 } 00257 00258 $messagehistorylink = html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype')); 00259 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL, 00260 get_string('messagehistoryfull','message'), 00261 array('class' => $historylinkclass)); 00262 00263 $messagehistorylink .= html_writer::start_tag('span', array('class' => $historylabelclass)); 00264 $messagehistorylink .= get_string('messagehistoryfull','message'); 00265 $messagehistorylink .= html_writer::end_tag('span'); 00266 00267 $messagehistorylink .= ' | '.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT, 00268 get_string('mostrecent','message'), 00269 array('class' => $recentlinkclass)); 00270 00271 $messagehistorylink .= html_writer::start_tag('span', array('class' => $recentlabelclass)); 00272 $messagehistorylink .= get_string('mostrecent','message'); 00273 $messagehistorylink .= html_writer::end_tag('span'); 00274 00275 if ($viewingnewmessages) { 00276 $messagehistorylink .= ' | '.html_writer::start_tag('span');//, array('class' => $historyclass) 00277 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount); 00278 $messagehistorylink .= html_writer::end_tag('span'); 00279 } 00280 00281 $messagehistorylink .= html_writer::end_tag('div'); 00282 00283 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages); 00284 echo html_writer::end_tag('div'); 00285 00286 //send message form 00287 if ($currentuser && has_capability('moodle/site:sendmessage', $context)) { 00288 echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend')); 00289 if (!empty($messageerror)) { 00290 echo $OUTPUT->heading($messageerror, 3); 00291 } else { 00292 $mform = new send_form(); 00293 $defaultmessage = new stdClass; 00294 $defaultmessage->id = $user2->id; 00295 $defaultmessage->message = ''; 00296 //$defaultmessage->messageformat = FORMAT_MOODLE; 00297 $mform->set_data($defaultmessage); 00298 $mform->display(); 00299 } 00300 echo html_writer::end_tag('div'); 00301 } 00302 } else if ($viewing == MESSAGE_VIEW_SEARCH) { 00303 message_print_search($advancedsearch, $user1); 00304 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) { 00305 message_print_recent_conversations($user1); 00306 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) { 00307 message_print_recent_notifications($user1); 00308 } 00309 echo html_writer::end_tag('div'); 00310 00311 echo $OUTPUT->box_end(); 00312 00313 echo $OUTPUT->footer(); 00314 00315