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 ''; -echo ''; -echo '
'; -echo $OUTPUT->user_picture($user1, array('size'=>100, 'courseid'=>SITEID)).'
'; -echo fullname($user1); -echo '
'; -echo ''.get_string('from').''; -echo ''.get_string('to').''; -echo ''; -echo $OUTPUT->user_picture($user2, array('size'=>100, 'courseid'=>SITEID)).'
'; -echo fullname($user2); -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 ""; + } + + if (empty($titletodisplay)) { + 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 '
$titletodisplay
'; + echo get_string('participants'); + 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 ''; + + if (!empty($titletodisplay)) { + echo ""; + } + + if ($countblocked) { + echo ''; + + foreach ($blockedusers as $blockeduser) { + message_print_contactlist_user($blockeduser, IS_NOT_CONTACT, IS_BLOCKED, $contactselecturl, $showactionlinks); + } + } + + echo '
$titletodisplay
'; + echo get_string('blockedusers', 'message', $countblocked); + 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 ''; + if (!empty($titletodisplay)) { + echo ""; + } + if($countonlinecontacts) { /// print out list of online contacts - echo ''; + if (empty($titletodisplay)) { + echo ''; + } foreach ($onlinecontacts as $contact) { - message_print_contactlist_user($contact); + if ($minmessages==0 || $contact->messagecount>=$minmessages) { + message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); + } } } - echo ''; if ($countofflinecontacts) { /// print out list of offline contacts - echo ''; + if (empty($titletodisplay)) { + echo ''; + } foreach ($offlinecontacts as $contact) { - message_print_contactlist_user($contact); + if ($minmessages==0 || $contact->messagecount>=$minmessages) { + message_print_contactlist_user($contact, IS_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); + } } echo ''; } @@ -154,7 +349,9 @@ function message_print_contacts() { echo ''; foreach ($strangers as $stranger) { - message_print_contactlist_user($stranger, false); + if ($minmessages==0 || $contact->messagecount>=$minmessages) { + message_print_contactlist_user($stranger, IS_NOT_CONTACT, IS_NOT_BLOCKED, $contactselecturl, $showactionlinks); + } } } @@ -168,17 +365,83 @@ function message_print_contacts() { echo '
'; - $PAGE->requires->js_init_call('M.core_message.init_refresh_page', array(60*1000, $PAGE->url->out(false))); + if ($refresh) { + $PAGE->requires->js_init_call('M.core_message.init_refresh_page', array(60*1000, $PAGE->url->out(false))); - echo $OUTPUT->container_start('messagejsautorefresh note center'); - echo get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh); - echo $OUTPUT->container_end(); + echo $OUTPUT->container_start('messagejsautorefresh note center'); + echo get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh); + echo $OUTPUT->container_end(); + } echo $OUTPUT->container_start('messagejsmanualrefresh aligncenter'); - echo $OUTPUT->single_button('index.php', get_string('refresh')); + echo $OUTPUT->single_button(message_remove_url_params($PAGE->url), get_string('refresh')); echo $OUTPUT->container_end(); } +function message_print_usergroup_selector($usergroup, &$courses, &$coursecontexts, $countunreadtotal, $countblocked) { + $strunreadmessages = $strblockedusers = null; + + if ($countunreadtotal>0) { //if there are unread messages + $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal); + $options = array(VIEW_UNREAD_MESSAGES=>$strunreadmessages); + } else { + //if 0 unread messages and they've requested unread messages then show contacts + if ($usergroup==VIEW_UNREAD_MESSAGES) { + $usergroup = VIEW_CONTACTS; + } + } + + if ($countblocked>0) { + $strblockedusers = get_string('blockedusers','message', $countblocked); + $options = array(VIEW_BLOCKED=>$strblockedusers); + } else { + //if 0 blocked users and they've requested blocked users then show contacts + if ($usergroup==VIEW_BLOCKED) { + $usergroup = VIEW_CONTACTS; + } + } + + $strcontacts = get_string('mycontacts', 'message'); + $options[VIEW_CONTACTS] = $strcontacts; + + if (!empty($courses)) { + $courses_options = array(); + + foreach($courses as $course) { + if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) { + $courses_options[VIEW_COURSE.$course->id] = $course->fullname; + } + } + + if (!empty($courses_options)) { + $options[] = array(get_string('courses')=>$courses_options); + } + } + + echo html_writer::start_tag('form', array('id'=>'usergroupform','method'=>'get','action'=>'')); + echo html_writer::select($options, 'usergroup', $usergroup, false, array('id'=>'usergroup','onchange'=>'this.form.submit()')); + echo html_writer::end_tag('form'); +} + +function message_get_course_contexts(&$courses) { + $coursecontexts = array(); + + foreach($courses as $course) { + $coursecontexts[$course->id] = get_context_instance(CONTEXT_COURSE, $course->id); + } + + return $coursecontexts; +} + +function message_remove_url_params($moodleurl) { + //this should work but doesnt for some reason + //return $PAGE->url->out(true); + + $newurl = new moodle_url($moodleurl); + $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); + return $newurl->out(); +} + /// $messagearray is an array of objects /// $field is a valid property of object @@ -196,14 +459,91 @@ function message_count_messages($messagearray, $field='', $value='') { return $count; } +/** + * Returns the count of unread messages for user. Either from a specific user or from all users. + * @global $USER + * @global $DB + * @param object $user1 the first user. Defaults to $USER + * @param object $user2 the second user. If null this function will count all of user 1's unread messages. + * @return int the count of $user1's unread messages + */ +function message_count_unread_messages($user1=null, $user2=null) { + global $USER, $DB; -function message_print_search() { - global $USER, $OUTPUT; + if (empty($user1)) { + $user1 = $USER; + } + + if (!empty($user2)) { + return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", + array($user1->id, $user2->id), "COUNT('id')"); + } else { + return $DB->count_records_select('message', "useridto = ?", + array($user1->id), "COUNT('id')"); + } +} + +function message_count_blocked_users($user1=null) { + global $USER, $DB; + + if (empty($user1)) { + $user1 = $USER; + } + + $sql = "SELECT count(mc.id) + FROM {message_contacts} mc + WHERE mc.userid = :userid AND mc.blocked = 1"; + $params = array('userid'=>$user1->id); + + return $DB->count_records_sql($sql, $params); +} + +/** + * + * @global $USER + * @global $PAGE + * @global $OUTPUT + * @param boolean advancedsearch show basic or advanced search form + * @return boolean was a search performed? + */ +function message_print_search($advancedsearch = false, $user1=null) { + global $USER, $PAGE, $OUTPUT; + + $frm = data_submitted(); + + $doingsearch = false; + if ($frm) { + $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name)); + } - if ($frm = data_submitted()) { + if (!empty($frm->combinedsearch)) { + $combinedsearchstring = $frm->combinedsearch; + } else { + $combinedsearchstring = get_string('searchcombined','message').'...'; + } - message_print_search_results($frm); + $PAGE->requires->js_init_call('M.core_message.init_search_page', array($combinedsearchstring)); + if ($doingsearch) { + if ($advancedsearch) { + + $messagesearch = ''; + if (!empty($frm->keywords)) { + $messagesearch = $frm->keywords; + } + $personsearch = ''; + if (!empty($frm->name)) { + $personsearch = $frm->name; + } + include('search_advanced.html'); + } else { + include('search.html'); + } + + $showicontext = false; + message_print_search_results($frm, $showicontext, $user1); + + return true; } else { /* /// unfinished buggy code disabled in search.html anyway @@ -217,7 +557,14 @@ function message_print_search() { $cs .= ''; } */ - include('search.html'); + + if ($advancedsearch) { + $personsearch = $messagesearch = ''; + include('search_advanced.html'); + } else { + include('search.html'); + } + return false; } } @@ -239,7 +586,7 @@ function message_print_settings() { set_user_preferences($pref); - redirect('index.php', get_string('settingssaved', 'message'), 1); + redirect(message_remove_url_params($PAGE->url), get_string('settingssaved', 'message'), 1); } $cbshowmessagewindow = (get_user_preferences('message_showmessagewindow', 1) == '1') ? 'checked="checked"' : ''; @@ -309,60 +656,81 @@ function message_get_contact($contactid) { -function message_print_search_results($frm) { - global $USER, $CFG, $DB, $OUTPUT; +function message_print_search_results($frm, $showicontext=false, $user1=null) { + global $USER, $CFG, $DB, $OUTPUT, $PAGE; + + if (empty($user1)) { + $user1 = $USER; + } - echo '
'; + echo '
'; - /// search for person + $personsearch = false; + $personsearchstring = null; if (!empty($frm->personsubmit) and !empty($frm->name)) { + $personsearch = true; + $personsearchstring = $frm->name; + } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { + $personsearch = true; + $personsearchstring = $frm->combinedsearch; + } + /// search for person + if ($personsearch) { if (optional_param('mycourses', 0, PARAM_BOOL)) { $users = array(); $mycourses = enrol_get_my_courses(); foreach ($mycourses as $mycourse) { - if (is_array($susers = message_search_users($mycourse->id, $frm->name))) { + if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) { foreach ($susers as $suser) $users[$suser->id] = $suser; } } } else { - $users = message_search_users(SITEID, $frm->name); + $users = message_search_users(SITEID, $personsearchstring); } if (!empty($users)) { echo ''.get_string('userssearchresults', 'message', count($users)).''; - echo '
$titletodisplay
'; - echo get_string('onlinecontacts', 'message', $countonlinecontacts); - echo '
'; + echo get_string('onlinecontacts', 'message', $countonlinecontacts); + echo '
 
'; - echo get_string('offlinecontacts', 'message', $countofflinecontacts); - echo '
'; + echo get_string('offlinecontacts', 'message', $countofflinecontacts); + echo '
 
'; + echo '
'; foreach ($users as $user) { if ( $user->contactlistid ) { if ($user->blocked == 0) { /// not blocked - $strcontact = message_contact_link($user->id, 'remove', true); - $strblock = message_contact_link($user->id, 'block', true); + $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext); + $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); } else { // blocked - $strcontact = message_contact_link($user->id, 'add', true); - $strblock = message_contact_link($user->id, 'unblock', true); + $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); + $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext); } } else { - $strcontact = message_contact_link($user->id, 'add', true); - $strblock = message_contact_link($user->id, 'block', true); + $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); + $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); + } + + //should we show just the icon or icon and text? + $histicontext = 'icon'; + if ($showicontext) { + $histicontext = 'both'; } - $strhistory = message_history_link($user->id, 0, true, '', '', 'icon'); + $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext); echo ''; echo ''; @@ -375,15 +743,24 @@ function message_print_search_results($frm) { echo '
'; echo $OUTPUT->user_picture($user, array('size'=>20, 'courseid'=>SITEID)); echo ''; $popupoptions = array( - 'height' => 500, - 'width' => 500, + 'height' => MESSAGE_DISCUSSION_HEIGHT, + 'width' => MESSAGE_DISCUSSION_WIDTH, 'menubar' => false, 'location' => false, 'status' => true, 'scrollbars' => true, 'resizable' => true); - $link = new moodle_url("/message/discussion.php?id=$user->id"); - $action = new popup_action('click', $link, "message_$user->id", $popupoptions); + //$link = new moodle_url("/message/discussion.php?id=$user->id"); + $link = new moodle_url("/message/contacts_messages.php?id=$user->id"); + //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); + $action = null; echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user)))); echo '
'; } else { - echo $OUTPUT->notification(get_string('nosearchresults', 'message')); + echo get_string('userssearchresults', 'message', 0).'

'; } + } + // search messages for keywords + $messagesearch = false; + $messagesearchstring = null; + if (!empty($frm->keywords)) { + $messagesearch = true; + $messagesearchstring = clean_text(trim($frm->keywords)); + } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { + $messagesearch = true; + $messagesearchstring = clean_text(trim($frm->combinedsearch)); + } - /// search messages for keywords - } else if (!empty($frm->keywordssubmit)) { - $keywordstring = clean_text(trim($frm->keywords)); - if ($keywordstring) { - $keywords = explode(' ', $keywordstring); + if ($messagesearch) { + if ($messagesearchstring) { + $keywords = explode(' ', $messagesearchstring); } else { $keywords = array(); } @@ -391,6 +768,10 @@ function message_print_search_results($frm) { $fromme = false; $courseid = 'none'; + if (empty($frm->keywordsoption)) { + $frm->keywordsoption = 'allmine'; + } + switch ($frm->keywordsoption) { case 'tome': $tome = true; @@ -421,10 +802,10 @@ function message_print_search_results($frm) { } /// print heading with number of results - echo '

'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($keywordstring).'")

'; + echo '

'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($messagesearchstring).'")

'; /// print table headings - echo ''; + echo '
'; echo ''; echo ''; echo ''; @@ -480,15 +861,23 @@ function message_print_search_results($frm) { /// print out message row echo ''; echo ''; echo ''; echo ''; echo ''; @@ -502,54 +891,59 @@ function message_print_search_results($frm) { echo '
'.get_string('from').''.get_string('to').'
'; - message_print_user($userfrom, $fromcontact, $fromblocked); + message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext); echo ''; - message_print_user($userto, $tocontact, $toblocked); + message_print_user($userto, $tocontact, $toblocked, $showicontext); echo ''.message_get_fragment($message->fullmessage, $keywords); echo '
'; echo '
'.userdate($message->timecreated, $dateformat).'
'; } else { - echo $OUTPUT->notification(get_string('nosearchresults', 'message')); + echo '

'.get_string('keywordssearchresults', 'message', 0).' ("'.s($messagesearchstring).'")

'; } + } - - /// what the ????, probably an empty search string, duh! - } else { + if (!$personsearch && !$messagesearch) { + //they didnt enter any search terms echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); } echo '
'; - echo $OUTPUT->single_button(new moodle_url('index.php', array('tab' => 'search')), get_string('newsearch', 'message')); + echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('tab' => 'search')), get_string('newsearch', 'message')); echo ''; } -function message_print_user ($user=false, $iscontact=false, $isblocked=false) { +function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) { global $USER, $OUTPUT; if ($user === false) { echo $OUTPUT->user_picture($USER, array('size'=>20, 'courseid'=>SITEID)); } else { - echo $OUTPUT->user_picture($USE, array('size'=>20, 'courseid'=>SITEID)); + echo $OUTPUT->user_picture($USER, array('size'=>20, 'courseid'=>SITEID)); echo ' '; + + $return = false; + $script = null; if ($iscontact) { - message_contact_link($user->id, 'remove'); + message_contact_link($user->id, 'remove', $return, $script, $includeicontext); } else { - message_contact_link($user->id, 'add'); + message_contact_link($user->id, 'add', $return, $script, $includeicontext); } echo ' '; if ($isblocked) { - message_contact_link($user->id, 'unblock'); + message_contact_link($user->id, 'unblock', $return, $script, $includeicontext); } else { - message_contact_link($user->id, 'block'); + message_contact_link($user->id, 'block', $return, $script, $includeicontext); } echo '
'; $popupoptions = array( - 'height' => 500, - 'width' => 500, + 'height' => MESSAGE_DISCUSSION_HEIGHT, + 'width' => MESSAGE_DISCUSSION_WIDTH, 'menubar' => false, 'location' => false, 'status' => true, 'scrollbars' => true, 'resizable' => true); - $link = new moodle_url("/message/discussion.php?id=$user->id"); - $action = new popup_action('click', $link, "message_$user->id", $popupoptions); + //$link = new moodle_url("/message/discussion.php?id=$user->id"); + $link = new moodle_url("/message/contacts_messages.php?id=$user->id"); + //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); + $action = null; echo $OUTPUT->action_link($link, fullname($user), $action, array('title'=>get_string('sendmessageto', 'message', fullname($user)))); } @@ -557,11 +951,16 @@ function message_print_user ($user=false, $iscontact=false, $isblocked=false) { /// linktype can be: add, remove, block, unblock -function message_contact_link($userid, $linktype='add', $return=false, $script="index.php?tab=contacts", $text=false) { - global $USER, $CFG, $OUTPUT; +function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { + global $USER, $CFG, $OUTPUT, $PAGE; static $str; + if (empty($script)) { + //$script = "index.php?tab=contacts"; + $script = message_remove_url_params($PAGE->url).'?tab=contacts'; + } + if (empty($str->blockcontact)) { $str->blockcontact = get_string('blockcontact', 'message'); $str->unblockcontact = get_string('unblockcontact', 'message'); @@ -574,25 +973,31 @@ function message_contact_link($userid, $linktype='add', $return=false, $script=" $alttext = $text ? '' : $string; $text = $text ? ' '.$string : ''; + $iconpath = null; switch ($linktype) { case 'block': - $icon = 't/go'; + $iconpath = 't/block'; break; case 'unblock': - $icon = 't/stop'; + $iconpath = 't/userblue'; break; case 'remove': - $icon = 't/user'; + $iconpath = 'i/cross_red_big'; break; case 'add': default: - $icon = 't/usernot'; + $iconpath = 't/addgreen'; + } + + $img = ''; + if ($icon) { + $img = ''.s($alttext).''; } - $output = ''. + $output = ''. ''. - ''.s($alttext).''. + $img. $text.''; if ($return) { @@ -603,7 +1008,7 @@ function message_contact_link($userid, $linktype='add', $return=false, $script=" } } -function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords='', $position='', $linktext='') { +function message_history_link($userid1, $userid2, $returnstr=false, $keywords='', $position='', $linktext='') { global $USER, $CFG, $OUTPUT; static $strmessagehistory; @@ -612,9 +1017,10 @@ function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords= $strmessagehistory = get_string('messagehistory', 'message'); } - if (!$userid2) { - $userid2 = $USER->id; - } +//todo andrew user1 is generally the current user + //if (!$userid2) { +// $userid2 = $USER->id; + //} if ($position) { $position = "#$position"; } @@ -642,8 +1048,9 @@ function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords= 'scrollbars' => true, 'resizable' => true); - $link = new moodle_url("/message/history.php?user1=$userid1&user2=$userid2$keywords$position"); - $action = new popup_action('click', $link, "message_history_$userid1", $popupoptions); + $link = new moodle_url('/message/contacts_messages.php?history='.MESSAGE_HISTORY_ALL."&user=$userid1&id=$userid2$keywords$position"); + //$action = new popup_action('click', $link, "message_history_$userid1", $popupoptions); + $action = null; $str = $OUTPUT->action_link($link, $fulllink, $action, array('title'=>$strmessagehistory)); $str = ''.$str.''; @@ -791,7 +1198,6 @@ function message_search($searchterms, $fromme=true, $tome=true, $courseid='none' $searchcond = implode(" AND ", $searchcond); } - /// There are several possibilities /// 1. courseid = SITEID : The admin is searching messages by all users /// 2. courseid = ?? : A teacher is searching messages by users in @@ -953,23 +1359,113 @@ function message_get_fragment($message, $keywords) { return $fragment; } - -function message_get_history($user1, $user2) { +//retrieve the messages between two users +function message_get_history($user1, $user2, $limitnum=0) { global $DB; - $messages = $DB->get_records_select('message_read', "(useridto = ? AND useridfrom = ?) OR + $messages = array(); + + //we want messages sorted oldest to newest but if getting a subset of messages we need to sort + //desc to get the last $limitnum messages then flip the order in php + $sort = 'asc'; + if ($limitnum>0) { + $sort = 'desc'; + } + + if ($messages_read = $DB->get_records_select('message_read', "(useridto = ? AND useridfrom = ?) OR (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), - 'timecreated'); + "timecreated $sort", '*', 0, $limitnum)) { + foreach ($messages_read as $message) { + $messages[$message->timecreated] = $message; + } + } if ($messages_new = $DB->get_records_select('message', "(useridto = ? AND useridfrom = ?) OR (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), - 'timecreated')) { + "timecreated $sort", '*', 0, $limitnum)) { foreach ($messages_new as $message) { - $messages[] = $message; + $messages[$message->timecreated] = $message; } } + + //if we only want the last $limitnum messages + if ($limitnum>0) { + ksort($messages); + $messages = array_slice($messages, count($messages)-$limitnum, $limitnum, true); + } + return $messages; } +function message_print_message_history($user1,$user2,$search='',$messagelimit=0, $messagehistorylink=false) { + global $CFG, $OUTPUT; + + echo $OUTPUT->box_start('center'); + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + echo $OUTPUT->user_picture($user1, array('size'=>100, 'courseid'=>SITEID)); + echo '
'.fullname($user1).'
'; + echo '
'; + echo ''.get_string('from').''; + echo ''.get_string('to').''; + echo ''; + echo $OUTPUT->user_picture($user2, array('size'=>100, 'courseid'=>SITEID)); + echo '
'.fullname($user2).'
'; + + if (isset($user2->iscontact) && isset($user2->isblocked)) { + $incontactlist = $user2->iscontact; + $isblocked = $user2->isblocked; + + $script = null; + $text = true; + $icon = false; + + $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $user2, $script, $text, $icon); + $strblock = message_get_contact_block_link($incontactlist, $isblocked, $user2, $script, $text, $icon); + echo $strcontact.$strblock; + } + + echo '
'; + echo $OUTPUT->box_end(); + + if (!empty($messagehistorylink)) { + echo $messagehistorylink; + } + + /// Get all the messages and print them + if ($messages = message_get_history($user1, $user2, $messagelimit)) { + $tablecontents = ''; + + $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']; + + $tablecontents .= '
'; + $tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'center').'
'; + } + if ($message->useridfrom == $user1->id) { + $tablecontents .= '
'.message_format_message($message, $user1, $messagedate, $search, 'me').'

'; + } else { + $tablecontents .= '
'.message_format_message($message, $user2, $messagedate, $search, 'other').'

'; + } + } + + echo html_writer::nonempty_tag('div', $tablecontents, array('class'=>'mdl-left messagehistory')); + } else { + echo $OUTPUT->heading(get_string('nomessagesfound', 'message'), 1); + } +} + function message_format_message(&$message, &$user, $format='', $keywords='', $class='other') { static $dateformat; @@ -988,7 +1484,8 @@ function message_format_message(&$message, &$user, $format='', $keywords='', $cl if ($keywords) { $messagetext = highlight($keywords, $messagetext); } - return '
'.s(fullname($user)).' ['.$time.']: '.$messagetext.'
'; + //return '
'.s(fullname($user)).' ['.$time.']: '.$messagetext.'
'; + return '
['.$time.']: '.$messagetext.'
'; } /** @@ -1005,7 +1502,7 @@ function message_post_message($userfrom, $userto, $message, $format, $messagetyp $eventdata->userto = $userto; $eventdata->subject = "IM"; $eventdata->fullmessage = $message; - $eventdata->fullmessageformat = FORMAT_PLAIN; + $eventdata->fullmessageformat = $format; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; $eventdata->timecreated = time(); @@ -1035,9 +1532,10 @@ function message_get_participants() { * block links etc * @param $contact contact object containing all fields required for $OUTPUT->user_picture() * @param $incontactlist is the user a contact of ours? + * @param $selectcontacturl string the url to send the user to when a contact's name is clicked */ -function message_print_contactlist_user($contact, $incontactlist = true){ - global $OUTPUT; +function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true) { + global $OUTPUT, $USER; $fullname = fullname($contact); $fullnamelink = $fullname; @@ -1046,33 +1544,36 @@ function message_print_contactlist_user($contact, $incontactlist = true){ $fullnamelink = ''.$fullnamelink.' ('.$contact->messagecount.')'; } + $strcontact = $strblock = $strhistory = null; - if($incontactlist){ - $strcontact = message_contact_link($contact->id, 'remove', true); - $strblock = ''; - }else{ - $strcontact = message_contact_link($contact->id, 'add', true); - $strblock = ' '. message_contact_link($contact->id, 'block', true); + if ($showactionlinks) { + $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact); + $strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact); + $strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon'); } - $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); - echo ''; echo $OUTPUT->user_picture($contact, array('size'=>20, 'courseid'=>SITEID)); echo ''; echo ''; $popupoptions = array( - 'height' => 500, - 'width' => 500, + 'height' => MESSAGE_DISCUSSION_HEIGHT, + 'width' => MESSAGE_DISCUSSION_WIDTH, 'menubar' => false, 'location' => false, 'status' => true, 'scrollbars' => true, 'resizable' => true); - $link = new moodle_url("/message/discussion.php?id=$contact->id"); - $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); + $link = $action = null; + if (!empty($selectcontacturl)) { + $link = new moodle_url($selectcontacturl.'&'.CONTACT_ID.'='.$contact->id); + } else { + //I suspect this else clause can be removed and maybe the $selectcontacturl arg can be removed and hardcoded + $link = new moodle_url("/message/discussion.php?id=$contact->id"); + $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); + } echo $OUTPUT->action_link($link, $fullnamelink, $action, array('title'=>get_string('sendmessageto', 'message', $fullname))); echo ''; @@ -1080,6 +1581,36 @@ function message_print_contactlist_user($contact, $incontactlist = true){ echo ''; } +function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { + $strcontact = ''; + + if($incontactlist){ + $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); + } else if ($isblocked) { + $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); + } else{ + $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); + } + + return $strcontact; +} + +function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { + $strblock = ''; + + //commented out to allow the user to block a contact without having to remove them first + /*if ($incontactlist) { + //$strblock = ''; + } else*/ + if ($isblocked) { + $strblock = ' '.message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); + } else{ + $strblock = ' '.message_contact_link($contact->id, 'block', true, $script, $text, $icon); + } + + return $strblock; +} + /** * Moves unread messages from message table to message_read for a given from user * @param object $userid User id @@ -1137,4 +1668,26 @@ function message_get_popup_messages($destuserid, $fromuserid=NULL){ } } return $messages; +} + +//marks all messages being sent from $fromuserid to $destuserid as read +function message_mark_messages_read($destuserid, $fromuserid){ + global $DB; + + $sql = 'SELECT m.*, mw.id AS mwid FROM {message} m JOIN {message_working} mw ON m.id=mw.unreadmessageid WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom'; + $messages = $DB->get_recordset_sql($sql, array('useridto'=>$destuserid,'useridfrom'=>$fromuserid)); + + foreach ($messages as $message) { + $message->timeread = time(); + $messageid = $message->id; + unset($message->id);//unset because it will get a new id on insert into message_read + + //delete what we've processed and check if can move message + $DB->delete_records('message_working', array('id' => $message->mwid)); + if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){ + if ($DB->insert_record('message_read', $message)) { + $DB->delete_records('message', array('id' => $messageid)); + } + } + } } \ No newline at end of file diff --git a/message/module.js b/message/module.js index 50835cb86d937..7f6cd8ba34aca 100644 --- a/message/module.js +++ b/message/module.js @@ -5,7 +5,6 @@ M.core_message.init_focus = function(Y, eid) { } M.core_message.init_refresh_parent_frame = function(Y, msgcount, msg) { - var add_message = function (messagestr) { var messageblock = parent.messages.document.getElementById('messages'); var message = document.createElement('div'); @@ -28,3 +27,18 @@ M.core_message.init_refresh_page = function(Y, delay, url) { } setTimeout(delay_callback, delay); } + +M.core_message.init_search_page = function(Y, defaultsearchterm) { + this.Y = Y; + this.defaultsearchterm = defaultsearchterm; + + var combinedsearchbox = this.Y.one('#combinedsearch'); + combinedsearchbox.on('focus', this.combinedsearchgotfocus, this); +} + + +M.core_message.combinedsearchgotfocus = function(e) { + if (e.target.get('value')==this.defaultsearchterm) { + e.target.select(); + } +} \ No newline at end of file diff --git a/message/output/popup/message_output_popup.php b/message/output/popup/message_output_popup.php index 08a0dca8a1be1..bbb4ae65b7f55 100644 --- a/message/output/popup/message_output_popup.php +++ b/message/output/popup/message_output_popup.php @@ -56,22 +56,42 @@ function config_form($preferences) { ''.get_string('showmessagewindow', 'message').':showmessagewindow==1?" checked=\"checked\"":"").' />'. ''.get_string('blocknoncontacts', 'message').':blocknoncontacts==1?" checked=\"checked\"":"").' />'. ''.get_string('beepnewmessage', 'message').':beepnewmessage==1?" checked=\"checked\"":"").' />'. + ''.get_string('htmleditor').':usehtmleditor==1?" checked=\"checked\"":"").' />'. ''.get_string('noframesjs', 'message').':noframesjs==1?" checked=\"checked\"":"").' />'. + ''.get_string('emailmessages', 'message').':emailmessages==1?" checked=\"checked\"":"").' />'. + ''.get_string('formorethan', 'message').': '.get_string('mins').''. + ''.get_string('email').':'. + ''.get_string('format').':'.$preferences->formatselect.''. ''; } public function process_form($form, &$preferences) { - $preferences['message_showmessagewindow'] = $form->showmessagewindow?1:0; - $preferences['message_blocknoncontacts'] = $form->blocknoncontacts?1:0; - $preferences['message_beepnewmessage'] = $form->beepnewmessage?1:0; - $preferences['message_noframesjs'] = $form->noframesjs?1:0; + $preferences['message_showmessagewindow'] = !empty($form->showmessagewindow)?1:0; + $preferences['message_blocknoncontacts'] = !empty($form->blocknoncontacts)?1:0; + $preferences['message_beepnewmessage'] = !empty($form->beepnewmessage)?1:0; + $preferences['message_usehtmleditor'] = !empty($form->usehtmleditor)?1:0; + $preferences['message_noframesjs'] = !empty($form->noframesjs)?1:0; + $preferences['message_emailmessages'] = !empty($form->emailmessages)?1:0; + $preferences['message_emailtimenosee'] = $form->emailtimenosee; + $preferences['message_emailaddress'] = $form->emailaddress; + $preferences['message_emailformat'] = $form->emailformat; + return true; } public function load_data(&$preferences, $userid) { + global $USER; $preferences->showmessagewindow = get_user_preferences( 'message_showmessagewindow', 1, $userid); $preferences->blocknoncontacts = get_user_preferences( 'message_blocknoncontacts', '', $userid); $preferences->beepnewmessage = get_user_preferences( 'message_beepnewmessage', '', $userid); + $preferences->usehtmleditor = get_user_preferences( 'message_usehtmleditor', '', $userid); $preferences->noframesjs = get_user_preferences( 'message_noframesjs', '', $userid); + $preferences->emailmessages = get_user_preferences( 'message_emailmessages', 1, $userid); + $preferences->emailtimenosee = get_user_preferences( 'message_emailtimenosee', 10, $userid); + $preferences->emailaddress = get_user_preferences( 'message_emailaddress', $USER->email, $userid); + $preferences->formatselect = html_writer::select(array(FORMAT_PLAIN => get_string('formatplain'), + FORMAT_HTML => get_string('formathtml')), + 'emailformat', get_user_preferences('message_emailformat', FORMAT_PLAIN)); + return true; } } \ No newline at end of file diff --git a/message/refresh.php b/message/refresh.php index 61f69d329226f..cde70e5082b3a 100644 --- a/message/refresh.php +++ b/message/refresh.php @@ -76,7 +76,7 @@ } } -$PAGE->requires->js_init_call('M.core_message.init_refresh_parent_frame', array($jsmessages, $jsmessages)); +$PAGE->requires->js_init_call('M.core_message.init_refresh_parent_frame', array(count($jsmessages), $jsmessages)); echo $OUTPUT->header(); if (!empty($playbeep)) { diff --git a/message/search.html b/message/search.html index 92c0a63b35962..ab1049744a538 100644 --- a/message/search.html +++ b/message/search.html @@ -1,58 +1,16 @@ -
+
- - - - - - - - - - - - - - - - - - - - +
+
heading(get_string('searchforperson', 'message')) ?>
  -
- + - - - - - - - - - - - - - - - - - - - -'; - -*/ - -?> -
heading(get_string('searchmessages', 'message')) ?> + + + +
  -
 
 
 
 
'.get_string('allstudents', 'message').'
   '.$cs.';
+
diff --git a/message/search_advanced.html b/message/search_advanced.html new file mode 100644 index 0000000000000..84d744da80517 --- /dev/null +++ b/message/search_advanced.html @@ -0,0 +1,61 @@ +
+
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +'; + +*/ + +?> + +

  +

  +
 
 
 
 
'.get_string('allstudents', 'message').'
   '.$cs.';
+
+ +
diff --git a/message/send.php b/message/send.php index 1443c6dded4eb..dd8df2a1afd8c 100644 --- a/message/send.php +++ b/message/send.php @@ -102,7 +102,7 @@ '['.$time.']: '. ''.$message.''; //$PAGE->requires->js_function_call('parent.messages.document.write', Array($message)); - $PAGE->requires->js_function_call('add_message', Array($message)); + $PAGE->requires->js_function_call('parent.refresh.add_message', Array($message)); $PAGE->requires->js_function_call('parent.messages.scroll', Array(1,5000000)); add_to_log(SITEID, 'message', 'write', 'history.php?user1='.$user->id.'&user2='.$USER->id.'#m'.$messageid, $user->id); diff --git a/message/send_form.php b/message/send_form.php index 5eff71998cfb6..88dd4b2e0d7b7 100644 --- a/message/send_form.php +++ b/message/send_form.php @@ -12,19 +12,21 @@ function definition () { $mform =& $this->_form; - $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false); + //$editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false); + $editoroptions = array(); + + //width handled by css so cols is empty. Still present so the page validates. + $displayoptions = array('rows'=>'4', 'cols'=>'', 'class'=>'messagesendbox'); $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); - $systemcontext = get_context_instance(CONTEXT_SYSTEM); - - $mform->addElement('html', '
'); - $mform->addElement('editor', 'message_editor', get_string('message', 'message'), null, $editoroptions); - $mform->addElement('html', '
'); + //$mform->addElement('html', '
'); + $mform->addElement('textarea', 'message', get_string('message', 'message'), $displayoptions, $editoroptions); + //$mform->addElement('editor', 'message_editor', get_string('message', 'message'), null, $editoroptions); + //$mform->addElement('html', '
'); $this->add_action_buttons(false, get_string('sendmessage', 'message')); - } /** @@ -34,7 +36,7 @@ function definition () { */ function set_data($data) { - $data->message = array('text'=>$data->message, 'format'=>$data->messageformat); + //$data->message = array('text'=>$data->message, 'format'=>$data->messageformat); parent::set_data($data); } @@ -47,10 +49,10 @@ function set_data($data) { function get_data() { $data = parent::get_data(); - if ($data !== null) { - $data->messageformat = $data->message_editor['format']; - $data->message = clean_text($data->message_editor['text'], $data->messageformat); - } + /*if ($data !== null) { + //$data->messageformat = $data->message_editor['format']; + //$data->message = clean_text($data->message_editor['text'], $data->messageformat); + }*/ return $data; } @@ -62,9 +64,9 @@ function get_data() { * re-display the form but with an empty message so the user can type the next * thing into it */ - function reset_message() { - $this->_form->_elements[$this->_form->_elementIndex['message_editor']]->setValue(array('text'=>'')); - } + //function reset_message() { + //$this->_form->_elements[$this->_form->_elementIndex['message']]->setValue(array('text'=>'')); + //} }