diff --git a/message/contacts.php b/message/contacts.php
new file mode 100644
index 0000000000000..0ae057e9c0fe7
--- /dev/null
+++ b/message/contacts.php
@@ -0,0 +1,131 @@
+.
+
+/**
+ * A page displaying the user's contacts. Similar to index.php but not a popup.
+ *
+ * @package moodlecore
+ * @copyright 2010 Andrew Davis
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require('../config.php');
+require('lib.php');
+
+require_login(0, false);
+
+if (isguestuser()) {
+ redirect($CFG->wwwroot);
+}
+
+if (empty($CFG->messaging)) {
+ print_error('disabled', 'message');
+}
+
+/// Optional variables that may be passed in
+$addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
+$removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
+$blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
+$unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
+$advancedsearch = optional_param('advanced', 0, PARAM_INT);
+$usergroup = optional_param('usergroup', VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
+
+$url = new moodle_url('/message/contacts.php');
+/*if ($addcontact !== 0) {
+ $url->param('addcontact', $addcontact);
+}
+if ($removecontact !== 0) {
+ $url->param('removecontact', $removecontact);
+}
+if ($blockcontact !== 0) {
+ $url->param('blockcontact', $blockcontact);
+}
+if ($unblockcontact !== 0) {
+ $url->param('unblockcontact', $unblockcontact);
+}*/
+if ($usergroup !== 0) {
+ $url->param('usergroup', $usergroup);
+}
+if ($advancedsearch !== 0) {
+ $url->param('advanced', $advancedsearch);
+}
+$PAGE->set_url($url);
+
+/// Process any contact maintenance requests there may be
+if ($addcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'add contact', 'history.php?user1='.$addcontact.'&user2='.$USER->id, $addcontact);
+ message_add_contact($addcontact);
+ redirect($CFG->wwwroot . '/message/contacts_messages.php?usergroup=contacts&id='.$addcontact);
+}
+if ($removecontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'remove contact', 'history.php?user1='.$removecontact.'&user2='.$USER->id, $removecontact);
+ message_remove_contact($removecontact);
+}
+if ($blockcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'block contact', 'history.php?user1='.$blockcontact.'&user2='.$USER->id, $blockcontact);
+ message_block_contact($blockcontact);
+}
+if ($unblockcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'unblock contact', 'history.php?user1='.$unblockcontact.'&user2='.$USER->id, $unblockcontact);
+ message_unblock_contact($unblockcontact);
+}
+
+//$PAGE->blocks->add_region('content');
+$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
+$PAGE->navigation->extend_for_user($USER);
+$PAGE->set_pagelayout('course');
+
+$context = get_context_instance(CONTEXT_SYSTEM);
+
+$strmycontacts = get_string('mycontacts', 'message');
+$strcontacts = get_string('contacts', 'message');
+
+$PAGE->navbar->add(get_string('myprofile'));
+$PAGE->navbar->add(get_string('messages','message'), 'contacts_messages.php');
+$PAGE->navbar->add($strcontacts);
+
+$PAGE->set_title(fullname($USER).': '.$strcontacts);
+$PAGE->set_heading("$SITE->shortname: $strcontacts");
+
+//now the page contents
+echo $OUTPUT->header();
+
+echo $OUTPUT->box_start('message');
+
+/*echo html_writer::start_tag('div', array('class'=>'contactselector mdl-align'));
+ $refreshpage = false;
+ $showcontactactionlinks = true;
+ message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, 'contacts_messages.php?usergroup=contacts', 0, $showactionlinks);
+echo html_writer::end_tag('div');*/
+
+$user1 = $USER;//we'll need a way to specify this if we want to view this page as a different user
+$user2 = null;
+
+$countunreadtotal = message_count_unread_messages($user1);
+$blockedusers = message_get_blocked_users($user1, $user2);
+list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
+$showcontactactionlinks = true;
+message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks);
+
+echo html_writer::start_tag('div', array('class'=>'messagearea mdl-align'));
+ message_print_search($advancedsearch, $user1);
+echo html_writer::end_tag('div');
+
+echo $OUTPUT->box_end();
+
+echo $OUTPUT->footer();
+
diff --git a/message/contacts_messages.php b/message/contacts_messages.php
new file mode 100644
index 0000000000000..84898b61909ca
--- /dev/null
+++ b/message/contacts_messages.php
@@ -0,0 +1,262 @@
+.
+
+/**
+ * A page displaying the user's contacts. Similar to index.php but not a popup.
+ *
+ * @package moodlecore
+ * @copyright 2010 Andrew Davis
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once('../config.php');
+require_once('lib.php');
+require_once('send_form.php');
+
+require_login(0, false);
+
+if (isguestuser()) {
+ redirect($CFG->wwwroot);
+}
+
+if (empty($CFG->messaging)) {
+ print_error('disabled', 'message');
+}
+
+$usergroup = optional_param('usergroup', VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
+$history = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT);
+$search = optional_param('search', '', PARAM_CLEAN);
+
+$user1id = optional_param('user', $USER->id, PARAM_INT);
+$user2id = optional_param('id', 0, PARAM_INT);
+
+$addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
+$removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
+$blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
+$unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
+
+$url = new moodle_url('/message/contacts_messages.php');
+
+if ($usergroup !== 0) {
+ $url->param('usergroup', $usergroup);
+}
+if ($user2id !== 0) {
+ $url->param('id', $user2id);
+}
+
+/*if ($addcontact !== 0) {
+ $url->param('addcontact', $addcontact);
+}
+if ($removecontact !== 0) {
+ $url->param('removecontact', $removecontact);
+}
+if ($blockcontact !== 0) {
+ $url->param('blockcontact', $blockcontact);
+}
+if ($unblockcontact !== 0) {
+ $url->param('unblockcontact', $unblockcontact);
+}*/
+
+$PAGE->set_url($url);
+
+/// Process any contact maintenance requests there may be
+if ($addcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'add contact', 'history.php?user1='.$addcontact.'&user2='.$USER->id, $addcontact);
+ message_add_contact($addcontact);
+ redirect($CFG->wwwroot . '/message/contacts_messages.php?usergroup=contacts&id='.$addcontact);
+}
+if ($removecontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'remove contact', 'history.php?user1='.$removecontact.'&user2='.$USER->id, $removecontact);
+ message_remove_contact($removecontact);
+}
+if ($blockcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'block contact', 'history.php?user1='.$blockcontact.'&user2='.$USER->id, $blockcontact);
+ message_block_contact($blockcontact);
+}
+if ($unblockcontact and confirm_sesskey()) {
+ add_to_log(SITEID, 'message', 'unblock contact', 'history.php?user1='.$unblockcontact.'&user2='.$USER->id, $unblockcontact);
+ message_unblock_contact($unblockcontact);
+}
+
+$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
+$PAGE->navigation->extend_for_user($USER);
+$PAGE->set_pagelayout('course');
+
+$context = get_context_instance(CONTEXT_SYSTEM);
+
+$user1 = null;
+$currentuser = true;
+$showcontactactionlinks = SHOW_ACTION_LINKS_IN_CONTACT_LIST;
+if ($user1id!=$USER->id) {
+ $user1 = $DB->get_record('user', array('id'=>$user1id));
+ if (!$user1) {
+ print_error('invaliduserid');
+ }
+ $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
+ $showcontactactionlinks = false;
+} else {
+ $user1 = $USER;
+}
+unset($user1id);
+
+$user2 = null;
+if (!empty($user2id)) {
+ $user2 = $DB->get_record("user", array("id"=>$user2id));
+ if (!$user2) {
+ print_error('invaliduserid');
+ }
+}
+unset($user2id);
+
+//was a message sent? Do NOT allow someone looking at somone elses messages to send them.
+$messageerror = null;
+if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $context)) {
+
+ // Check that the user is not blocking us!!
+ if ($contact = $DB->get_record('message_contacts', array('userid'=>$user2->id, 'contactid'=>$user1->id))) {
+ if ($contact->blocked and !has_capability('moodle/site:readallmessages', $context)) {
+ $messageerror = get_string('userisblockingyou', 'message');
+ }
+ }
+ $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
+
+ if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
+ if (empty($contact)) { // We are not a contact!
+ $messageerror = get_string('userisblockingyounoncontact', 'message');
+ }
+ }
+
+ if (empty($messageerror)) {
+ $mform = new send_form();
+ $defaultmessage = new stdClass;
+ $defaultmessage->id = $user2->id;
+ $defaultmessage->message = '';
+
+ $data = $mform->get_data();
+ if (!empty($data)) { /// Current user has just sent a message
+ if (!confirm_sesskey()) {
+ print_error('invalidsesskey');
+ }
+
+ /// Save it to the database...
+ $messageid = message_post_message($user1, $user2, $data->message, FORMAT_PLAIN, 'direct');
+ if (!empty($messageid)) {
+ redirect($CFG->wwwroot . '/message/contacts_messages.php?usergroup='.$usergroup.'&id='.$user2->id);
+ }
+ }
+ }
+}
+if (!empty($messageerror)) {
+ echo $OUTPUT->header();
+ echo $OUTPUT->heading($messageerror, 1);
+ echo $OUTPUT->footer();
+ exit;
+}
+
+$strmessages = get_string('messages', 'message');
+$PAGE->set_title("$SITE->shortname: $strmessages");
+$PAGE->set_heading("$SITE->shortname: $strmessages");
+
+//now the page contents
+echo $OUTPUT->header();
+
+echo $OUTPUT->box_start('message');
+
+$countunread = 0; //count of unread messages from $user2
+$countunreadtotal = 0; //count of unread messages from all users
+
+//we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
+if (!empty($user2)) {
+ //are there any unread messages from $user2
+ $countunread = message_count_unread_messages($user1, $user2);
+ if ($countunread>0) {
+ //mark the messages we're going to display as read
+ message_mark_messages_read($user1->id, $user2->id);
+ }
+}
+$countunreadtotal = message_count_unread_messages($user1);
+
+$blockedusers = message_get_blocked_users($user1, $user2);
+$countblocked = count($blockedusers);
+
+list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
+
+message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks);
+
+echo html_writer::start_tag('div', array('class'=>'messagearea mdl-align'));
+ if (!empty($user2)) {
+
+ echo html_writer::start_tag('div', array('class'=>'mdl-left messagehistory'));
+
+ $historyclass = 'visible';
+ $recentclass = 'hiddenelement';//cant just use hidden as mform adds that class to its fieldset
+ if ($history==MESSAGE_HISTORY_ALL) {
+ $displaycount = 0;
+
+ $historyclass = 'hiddenelement';
+ $recentclass = 'visible';
+ } else {
+ //default to only showing a few messages unless explicitly told not to
+ $displaycount = MESSAGE_SHORTVIEW_LIMIT;
+
+ if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
+ $displaycount = $countunread;
+ }
+ }
+
+ $messagehistorylink = html_writer::start_tag('div', array('class'=>'mdl-align','style'=>'clear:both;padding-bottom:20px;'));
+ $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
+ get_string('messagehistoryfull','message'),
+ array('class'=>$historyclass));
+
+ $messagehistorylink .= html_writer::start_tag('span', array('class'=>$recentclass));
+ $messagehistorylink .= get_string('messagehistoryfull','message');
+ $messagehistorylink .= html_writer::end_tag('span');
+
+ $messagehistorylink .= '/'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
+ get_string('mostrecent','message'),
+ array('class'=>$recentclass));
+
+ $messagehistorylink .= html_writer::start_tag('span', array('class'=>$historyclass));
+ $messagehistorylink .= get_string('mostrecent','message');
+ $messagehistorylink .= html_writer::end_tag('span');
+
+ $messagehistorylink .= html_writer::end_tag('div');
+
+ message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink);
+ echo html_writer::end_tag('div');
+
+ //send message form
+ if ($currentuser && has_capability('moodle/site:sendmessage', $context)) {
+ echo html_writer::start_tag('div', array('class'=>'mdl-align messagesend'));
+ $mform = new send_form();
+ $defaultmessage = new stdClass;
+ $defaultmessage->id = $user2->id;
+ $defaultmessage->message = '';
+ //$defaultmessage->messageformat = FORMAT_MOODLE;
+ $mform->set_data($defaultmessage);
+ $mform->display();
+ echo html_writer::end_tag('div');
+ }
+ }
+echo html_writer::end_tag('div');
+
+echo $OUTPUT->box_end();
+
+echo $OUTPUT->footer();
+
+
diff --git a/message/edit.php b/message/edit.php
index b6db29b1ba18e..6bbee0afa3249 100644
--- a/message/edit.php
+++ b/message/edit.php
@@ -65,6 +65,8 @@
$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
+$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
+$PAGE->set_pagelayout('course');
// check access control
if ($user->id == $USER->id) {
@@ -95,11 +97,14 @@
foreach ( $providers as $providerid => $provider){
foreach (array('loggedin', 'loggedoff') as $state){
$linepref = '';
- foreach ($form->{$provider->component.'_'.$provider->name.'_'.$state} as $process=>$one){
- if ($linepref == ''){
- $linepref = $process;
- } else {
- $linepref .= ','.$process;
+ $componentproviderstate = $provider->component.'_'.$provider->name.'_'.$state;
+ if (array_key_exists($componentproviderstate, $form)) {
+ foreach ($form->{$componentproviderstate} as $process=>$one){
+ if ($linepref == ''){
+ $linepref = $process;
+ } else {
+ $linepref .= ','.$process;
+ }
}
}
$preferences['message_provider_'.$provider->component.'_'.$provider->name.'_'.$state] = $linepref;
@@ -171,12 +176,17 @@
$strparticipants = get_string('participants');
$userfullname = fullname($user, true);
-if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
- has_capability('moodle/site:viewparticipants', $systemcontext)) {
- $PAGE->navbar->add($strparticipants, new moodle_url('/message/index.php', array('id'=>$course->id)));
+if ($user->id==$USER->id) {
+ $PAGE->navigation->extend_for_user($USER);
+} else {
+ if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
+ has_capability('moodle/site:viewparticipants', $systemcontext)) {
+ $PAGE->navbar->add($strparticipants, new moodle_url('/message/index.php', array('id'=>$course->id)));
+ }
+ $PAGE->navbar->add($userfullname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$course->id)));
+ $PAGE->navbar->add($streditmymessage);
}
-$PAGE->navbar->add($userfullname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$course->id)));
-$PAGE->navbar->add($streditmymessage);
+
$PAGE->set_title("$course->shortname: $streditmymessage");
if ($course->id != SITEID) {
$PAGE->set_heading("$course->fullname: $streditmymessage");
diff --git a/message/history.php b/message/history.php
index e96cd400876f0..39d18abc0affe 100644
--- a/message/history.php
+++ b/message/history.php
@@ -36,38 +36,73 @@
print_error('disabled', 'message');
}
-$PAGE->set_title(get_string('messagehistory', 'message'));
-
/// Script parameters
-$userid1 = required_param('user1', PARAM_INT);
-$PAGE->set_url('/message/history.php', array('user1'=>$userid1));
-if (! $user1 = $DB->get_record("user", array("id"=>$userid1))) { // Check it's correct
- print_error('invaliduserid');
+$userid1 = optional_param('user1', $USER->id, PARAM_INT);
+$userid2 = required_param('user2', PARAM_INT);
+$popup = optional_param('popup', 0, PARAM_INT);
+
+$url = new moodle_url('/message/history.php');
+$url->param('user1', $userid1);
+if (!empty($userid2)) {
+ $url->param('user2', $userid2);
}
+$PAGE->set_url($url);
-if ($user1->deleted) {
- echo $OUTPUT->header();
- echo $OUTPUT->heading(get_string('userdeleted').': '.$userid1, 1);
- echo $OUTPUT->footer();
- die;
+$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
+
+$iscurrentuser = $USER->id == $userid1;
+
+if ($iscurrentuser) {
+ $PAGE->navigation->extend_for_user($USER);
+} else {
+ $PAGE->navigation->extend_for_user($DB->get_record('user',array('id'=>$userid1)));
+}
+$PAGE->navigation->extend_for_user($DB->get_record('user',array('id'=>$userid2)));
+
+
+$strmessagehistory = get_string('messagehistory', 'message');
+if (!$popup) {
+ if ($iscurrentuser) {
+ $PAGE->navigation->get('myprofile')->get('messages')->make_active();
+ } else {
+ $PAGE->navigation->find($userid1,navigation_node::TYPE_USER)->make_active();
+ }
+
+ $PAGE->navbar->add($strmessagehistory);
+
+ $PAGE->set_pagelayout('course');
+ $PAGE->set_heading($strmessagehistory);
}
+$PAGE->set_title($strmessagehistory);
-if (has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) { // Able to see any discussion
- $userid2 = optional_param('user2', $USER->id, PARAM_INT);
- $PAGE->url->param('user2', $userid2);
- if (! $user2 = $DB->get_record("user", array("id"=>$userid2))) { // Check
+
+// Are we able to see other user's discussions?
+if (has_capability('moodle/site:readallmessages', get_context_instance(CONTEXT_SYSTEM))) {
+ if (! $user1 = $DB->get_record("user", array("id"=>$userid1))) {
print_error('invaliduserid');
}
- if ($user2->deleted) {
+ if ($user1->deleted) {
echo $OUTPUT->header();
- echo $OUTPUT->heading(get_string('userdeleted').': '.$userid2, 1);
+ echo $OUTPUT->heading(get_string('userdeleted').': '.$userid1, 1);
echo $OUTPUT->footer();
die;
}
} else {
- $userid2 = $USER->id; // Can only see messages involving yourself
- $user2 = $USER;
+ //User can only see their own discussions
+ $userid1 = $USER->id;
+ $user1 = $USER;
+}
+
+if (! $user2 = $DB->get_record("user", array("id"=>$userid2))) { // Check
+ print_error('invaliduserid');
}
+if ($user2->deleted) {
+ echo $OUTPUT->header();
+ echo $OUTPUT->heading(get_string('userdeleted').': '.$userid2, 1);
+ echo $OUTPUT->footer();
+ die;
+}
+
$search = optional_param('search', '', PARAM_CLEAN);
add_to_log(SITEID, 'message', 'history', 'history.php?user1='.$userid1.'&user2='.$userid2, $userid1);
@@ -77,51 +112,7 @@
echo $OUTPUT->header();
/// Print out a heading including the users we are looking at
-
-echo $OUTPUT->box_start('center');
-echo '
';
-echo '';
-echo $OUTPUT->user_picture($user1, array('size'=>100, 'courseid'=>SITEID)).' ';
-echo fullname($user1);
-echo ' | ';
-echo '';
-echo '';
-echo '';
-echo ' | ';
-echo '';
-echo $OUTPUT->user_picture($user2, array('size'=>100, 'courseid'=>SITEID)).' ';
-echo fullname($user2);
-echo ' | ';
-echo '
';
-echo $OUTPUT->box_end();
-
-
-/// Get all the messages and print them
-
-if ($messages = message_get_history($user1, $user2)) {
- $current->mday = '';
- $current->month = '';
- $current->year = '';
- $messagedate = get_string('strftimetime');
- $blockdate = get_string('strftimedaydate');
- foreach ($messages as $message) {
- $date = usergetdate($message->timecreated);
- if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) {
- $current->mday = $date['mday'];
- $current->month = $date['month'];
- $current->year = $date['year'];
- echo '';
- echo $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'center');
- }
- if ($message->useridfrom == $user1->id) {
- echo message_format_message($message, $user1, $messagedate, $search, 'other');
- } else {
- echo message_format_message($message, $user2, $messagedate, $search, 'me');
- }
- }
-} else {
- echo $OUTPUT->heading(get_string('nomessagesfound', 'message'), 1);
-}
+message_print_message_history($user1, $user2, $search);
echo $OUTPUT->footer();
diff --git a/message/index.php b/message/index.php
index bfd26a0225c6a..65c6eb0ce128a 100644
--- a/message/index.php
+++ b/message/index.php
@@ -112,8 +112,6 @@
get_string('contacts', 'message'));
$tabrow[] = new tabobject('search', $CFG->wwwroot.'/message/index.php?tab=search',
get_string('search', 'message'));
-$tabrow[] = new tabobject('settings', $CFG->wwwroot.'/message/index.php?tab=settings',
- get_string('settings', 'message'));
$tabrows = array($tabrow);
print_tabs($tabrows, $tab);
@@ -129,7 +127,7 @@
/// a print function is associated with each tab
$tabprintfunction = 'message_print_'.$tab;
if (function_exists($tabprintfunction)) {
- $tabprintfunction();
+ $tabprintfunction('index.php');
}
echo ' ';
diff --git a/message/lib.php b/message/lib.php
index 267500bf497e4..dea9326e2dc01 100644
--- a/message/lib.php
+++ b/message/lib.php
@@ -29,6 +29,30 @@
define ('MESSAGE_SHORTLENGTH', 300);
define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!)
+define ('MESSAGE_DISCUSSION_WIDTH',600);
+define ('MESSAGE_DISCUSSION_HEIGHT',500);
+
+define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history
+
+define ('CONTACT_ID','id');
+
+define('MESSAGE_HISTORY_SHORT',0);
+define('MESSAGE_HISTORY_ALL',1);
+
+//some constants used as function arguments. Just to make function calls a bit more understandable
+define('IS_CONTACT',true);
+define('IS_NOT_CONTACT',false);
+
+define('IS_BLOCKED',true);
+define('IS_NOT_BLOCKED',false);
+
+define('VIEW_UNREAD_MESSAGES','unread');
+define('VIEW_CONTACTS','contacts');
+define('VIEW_BLOCKED','blockedusers');
+define('VIEW_COURSE','course_');
+
+define('SHOW_ACTION_LINKS_IN_CONTACT_LIST', false);
+
if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds
$CFG->message_contacts_refresh = 60;
}
@@ -39,9 +63,160 @@
$CFG->message_offline_time = 300;
}
+function message_print_contact_selector($countunreadtotal, $usergroup, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks) {
+ global $PAGE;
+
+ echo html_writer::start_tag('div', array('class'=>'contactselector mdl-align'));
+
+ //if 0 unread messages and they've requested unread messages then show contacts
+ if ($countunreadtotal==0 && $usergroup==VIEW_UNREAD_MESSAGES) {
+ $usergroup = VIEW_CONTACTS;
+ }
+
+ $onlyactivecourses = true;
+ $courses = enrol_get_users_courses($user1->id, $onlyactivecourses);
+ $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them
+
+ message_print_usergroup_selector($usergroup, $courses, $coursecontexts, $countunreadtotal, count($blockedusers));
+
+ $refreshpage = false;
+
+ if ($usergroup==VIEW_UNREAD_MESSAGES) {
+ message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 1, $showcontactactionlinks,$strunreadmessages, $user2);
+ } else if ($usergroup==VIEW_CONTACTS) {
+ message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refreshpage, $PAGE->url, 0, $showcontactactionlinks, null, $user2);
+ } else if ($usergroup==VIEW_BLOCKED) {
+ message_print_blocked_users($blockedusers, $PAGE->url, $showcontactactionlinks, null, $user2);
+ } else if (substr($usergroup, 0, 7)==VIEW_COURSE) {
+ $courseidtoshow = intval(substr($usergroup, 7));
+
+ if (!empty($courseidtoshow)
+ && array_key_exists($courseidtoshow, $coursecontexts)
+ && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) {
+
+ message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showcontactactionlinks);
+ } else {
+ //shouldnt get here. User trying to access a course theyre not in perhaps.
+ add_to_log(SITEID, 'message', 'view', 'contacts_messages.php', $usergroup);
+ }
+ }
+
+ echo html_writer::start_tag('form', array('action'=>'contacts.php?advanced=1'));
+ echo html_writer::start_tag('fieldset');
+ $managebuttonclass = 'hiddenelement';
+ if ($usergroup == VIEW_CONTACTS) {
+ $managebuttonclass = 'visible';
+ }
+ $strmanagecontacts = get_string('managecontacts','message');
+ echo html_writer::empty_tag('input', array('type'=>'submit','value'=>$strmanagecontacts,'class'=>$managebuttonclass));
+ echo html_writer::end_tag('fieldset');
+ echo html_writer::end_tag('form');
+ echo html_writer::end_tag('div');
+}
+
+function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null) {
+ global $DB, $USER;
+
+ $participants = get_enrolled_users($context);
+
+ echo '';
+
+ if (!empty($titletodisplay)) {
+ echo "$titletodisplay |
";
+ }
+
+ if (empty($titletodisplay)) {
+ echo '';
+ echo get_string('participants');
+ echo ' |
';
+ }
+
+ //todo these need to come from somewhere if the course participants list is to show users with unread messages
+ $iscontact = true;
+ $isblocked = false;
+ foreach ($participants as $participant) {
+ if ($participant->id != $USER->id) {
+ $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages
+ message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks);
+ }
+ }
+ //$participants->close();
+
+ echo '
';
+}
+
+function message_get_blocked_users($user1=null, &$user2=null) {
+ global $DB, $USER;
+
+ if (empty($user1)) {
+ $user1 = $USER;
+ }
+
+ if (!empty($user2)) {
+ $user2->isblocked = false;
+ }
+
+ $blockeduserssql = "SELECT u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess, count(m.id) as messagecount
+ FROM {message_contacts} mc
+ JOIN {user} u ON u.id = mc.contactid
+ LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1
+ WHERE mc.userid = :user1id2 AND mc.blocked = 1
+ GROUP BY u.id, u.firstname, u.lastname, u.picture,
+ u.imagealt, u.lastaccess
+ ORDER BY u.firstname ASC";
+ $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1'=>$user1->id, 'user1id2'=>$user1->id));
+
+ $blockedusers = array();
+ if (!empty($rs)) {
+ foreach($rs as $rd) {
+ $blockedusers[] = $rd;
+
+ if (!empty($user2) && $user2->id==$rd->id) {
+ $user2->isblocked = true;
+ }
+ }
+ unset($rd);
+ $rs->close();
+ }
+
+ return $blockedusers;
+}
+
+function message_print_blocked_users(&$blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null) {
+ global $DB, $USER;
+
+ $countblocked = count($blockedusers);
+
+ echo '';
+}
+
+function message_get_contacts($user1=null, &$user2=null) {
+ global $DB, $CFG, $USER;
-function message_print_contacts() {
- global $USER, $CFG, $DB, $PAGE, $OUTPUT;
+ if (empty($user1)) {
+ $user1 = $USER;
+ }
+
+ if (!empty($user2)) {
+ $user2->iscontact = false;
+ }
$timetoshowusers = 300; //Seconds default
if (isset($CFG->block_online_users_timetosee)) {
@@ -56,8 +231,7 @@ function message_print_contacts() {
// people in our contactlist who are offline
$offlinecontacts = array();
// people who are not in our contactlist but have sent us a message
- $strangers = array();
-
+ $strangers = array();
// get all in our contactlist who are not blocked in our contact list
// and count messages we have waiting from each of them
@@ -71,21 +245,25 @@ function message_print_contacts() {
u.imagealt, u.lastaccess
ORDER BY u.firstname ASC";
- if ($rs = $DB->get_recordset_sql($contactsql, array($USER->id, $USER->id))){
+ if ($rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id))){
foreach($rs as $rd){
if($rd->lastaccess >= $timefrom){
// they have been active recently, so are counted online
$onlinecontacts[] = $rd;
+
}else{
$offlinecontacts[] = $rd;
}
+
+ if (!empty($user2) && $user2->id==$rd->id) {
+ $user2->iscontact = true;
+ }
}
unset($rd);
$rs->close();
}
-
// get messages from anyone who isn't in our contact list and count the number
// of messages we have from each of them
$strangersql = "SELECT u.id, u.firstname, u.lastname, u.picture,
@@ -106,6 +284,12 @@ function message_print_contacts() {
$rs->close();
}
+ return array($onlinecontacts, $offlinecontacts, $strangers);
+}
+
+function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $refresh=true, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null) {
+ global $CFG, $PAGE, $OUTPUT;
+
$countonlinecontacts = count($onlinecontacts);
$countofflinecontacts = count($offlinecontacts);
$countstrangers = count($strangers);
@@ -115,34 +299,45 @@ function message_print_contacts() {
print_string('contactlistempty', 'message');
echo '';
echo '';
- print_string('addsomecontacts', 'message', $CFG->wwwroot.'/message/index.php?tab=search');
+ print_string('addsomecontacts', 'message', message_remove_url_params($PAGE->url).'?tab=search');
echo '
';
}
echo '