[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/message/ -> index.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * A page displaying the user's contacts and messages
  19   *
  20   * @package    core_message
  21   * @copyright  2010 Andrew Davis
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once('../config.php');
  26  require_once ('lib.php');
  27  require_once ('send_form.php');
  28  
  29  require_login(0, false);
  30  
  31  if (isguestuser()) {
  32      redirect($CFG->wwwroot);
  33  }
  34  
  35  if (empty($CFG->messaging)) {
  36      print_error('disabled', 'message');
  37  }
  38  
  39  //'viewing' is the preferred URL parameter but we'll still accept usergroup in case its referenced externally
  40  $usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
  41  $viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT);
  42  
  43  $history   = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT);
  44  $search    = optional_param('search', '', PARAM_CLEAN); //TODO: use PARAM_RAW, but make sure we use s() and p() properly
  45  
  46  //the same param as 1.9 and the param we have been logging. Use this parameter.
  47  $user1id   = optional_param('user1', $USER->id, PARAM_INT);
  48  //2.0 shipped using this param. Retaining it only for compatibility. It should be removed.
  49  $user1id   = optional_param('user', $user1id, PARAM_INT);
  50  
  51  //the same param as 1.9 and the param we have been logging. Use this parameter.
  52  $user2id   = optional_param('user2', 0, PARAM_INT);
  53  //The class send_form supplies the receiving user id as 'id'
  54  $user2id   = optional_param('id', $user2id, PARAM_INT);
  55  
  56  $addcontact     = optional_param('addcontact',     0, PARAM_INT); // adding a contact
  57  $removecontact  = optional_param('removecontact',  0, PARAM_INT); // removing a contact
  58  $blockcontact   = optional_param('blockcontact',   0, PARAM_INT); // blocking a contact
  59  $unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
  60  $deletemessageid = optional_param('deletemessageid', 0, PARAM_INT);
  61  $deletemessageconfirm = optional_param('deletemessageconfirm', 0, PARAM_BOOL);
  62  if ($deletemessageid) {
  63      $deletemessagetype = required_param('deletemessagetype', PARAM_ALPHAEXT);
  64  }
  65  
  66  //for search
  67  $advancedsearch = optional_param('advanced', 0, PARAM_INT);
  68  
  69  //if they have numerous contacts or are viewing course participants we might need to page through them
  70  $page = optional_param('page', 0, PARAM_INT);
  71  
  72  $url = new moodle_url('/message/index.php', array('user1' => $user1id));
  73  
  74  if ($user2id !== 0) {
  75      $url->param('user2', $user2id);
  76  
  77      //Switch view back to contacts if:
  78      //1) theyve searched and selected a user
  79      //2) they've viewed recent messages or notifications and clicked through to a user
  80      if ($viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
  81          $viewing = MESSAGE_VIEW_CONTACTS;
  82      }
  83  }
  84  
  85  if ($viewing != MESSAGE_VIEW_UNREAD_MESSAGES) {
  86      $url->param('viewing', $viewing);
  87  }
  88  
  89  $PAGE->set_url($url);
  90  
  91  // Disable message notification popups while the user is viewing their messages
  92  $PAGE->set_popup_notification_allowed(false);
  93  
  94  $user1 = null;
  95  $currentuser = true;
  96  $showactionlinks = true;
  97  if ($user1id != $USER->id) {
  98      $user1 = $DB->get_record('user', array('id' => $user1id));
  99      if (!$user1) {
 100          print_error('invaliduserid');
 101      }
 102      $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
 103      $showactionlinks = false;
 104  } else {
 105      $user1 = $USER;
 106  }
 107  unset($user1id);
 108  
 109  $user2 = null;
 110  if (!empty($user2id)) {
 111      $user2 = core_user::get_user($user2id);
 112      if (!$user2) {
 113          print_error('invaliduserid');
 114      }
 115  }
 116  unset($user2id);
 117  
 118  $user2realuser = !empty($user2) && core_user::is_real_user($user2->id);
 119  $showactionlinks = $showactionlinks && $user2realuser;
 120  $systemcontext = context_system::instance();
 121  
 122  if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
 123      print_error('accessdenied','admin');
 124  }
 125  
 126  if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
 127      $courseid = intval(substr($viewing, 7));
 128      require_login($courseid);
 129      require_capability('moodle/course:viewparticipants', context_course::instance($courseid));
 130      $PAGE->set_pagelayout('incourse');
 131  } else {
 132      $PAGE->set_pagelayout('standard');
 133  }
 134  // Page context should always be set to user.
 135  $PAGE->set_context(context_user::instance($user1->id));
 136  if (!empty($user1->id) && $user1->id != $USER->id) {
 137      $PAGE->navigation->extend_for_user($user1);
 138  }
 139  if (!empty($user2->id) && $user2realuser && ($user2->id != $USER->id)) {
 140      $PAGE->navigation->extend_for_user($user2);
 141  }
 142  
 143  $strmessages = get_string('messages', 'message');
 144  if ($user2realuser) {
 145      $user2fullname = fullname($user2);
 146  
 147      $PAGE->set_title("$strmessages: $user2fullname");
 148      $PAGE->set_heading("$strmessages: $user2fullname");
 149  } else {
 150      $PAGE->set_title("{$SITE->shortname}: $strmessages");
 151      $PAGE->set_heading("{$SITE->shortname}: $strmessages");
 152  }
 153  
 154  /// Process any contact maintenance requests there may be
 155  if ($addcontact and confirm_sesskey()) {
 156      message_add_contact($addcontact);
 157      redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
 158  }
 159  if ($removecontact and confirm_sesskey()) {
 160      message_remove_contact($removecontact);
 161  }
 162  if ($blockcontact and confirm_sesskey()) {
 163      message_block_contact($blockcontact);
 164  }
 165  if ($unblockcontact and confirm_sesskey()) {
 166      message_unblock_contact($unblockcontact);
 167  }
 168  if ($deletemessageid and confirm_sesskey()) {
 169      // Check that the message actually exists.
 170      if ($message = $DB->get_record($deletemessagetype, array('id' => $deletemessageid))) {
 171          // Check that we are allowed to delete this message.
 172          if (message_can_delete_message($message, $user1->id)) {
 173              if (!$deletemessageconfirm) {
 174                  $confirmurl = new moodle_url('/message/index.php', array('user1' => $user1->id, 'user2' => $user2->id,
 175                      'viewing' => $viewing, 'deletemessageid' => $message->id, 'deletemessagetype' => $deletemessagetype,
 176                      'deletemessageconfirm' => 1, 'sesskey' => sesskey()));
 177                  $confirmbutton = new single_button($confirmurl, get_string('delete'), 'post');
 178                  $strdeletemessage = get_string('deletemessage', 'message');
 179                  $PAGE->set_title($strdeletemessage);
 180                  echo $OUTPUT->header();
 181                  echo $OUTPUT->heading($strdeletemessage);
 182                  echo $OUTPUT->confirm(get_string('deletemessageconfirmation', 'message'), $confirmbutton, $url);
 183                  echo $OUTPUT->footer();
 184                  exit();
 185              }
 186              message_delete_message($message, $user1->id);
 187          }
 188      }
 189      redirect($url);
 190  }
 191  
 192  //was a message sent? Do NOT allow someone looking at someone else's messages to send them.
 193  $messageerror = null;
 194  if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $systemcontext)) {
 195      // Check that the user is not blocking us!!
 196      if (message_is_user_blocked($user2, $user1)) {
 197          $messageerror = get_string('userisblockingyou', 'message');
 198      }
 199      // Check that we're not non-contact block by the user.
 200      if (message_is_user_non_contact_blocked($user2, $user1)) {
 201          $messageerror = get_string('userisblockingyounoncontact', 'message', fullname($user2));
 202      }
 203  
 204      if (empty($messageerror)) {
 205          $mform = new send_form();
 206          $defaultmessage = new stdClass;
 207          $defaultmessage->id = $user2->id;
 208          $defaultmessage->viewing = $viewing;
 209          $defaultmessage->message = '';
 210  
 211          //Check if the current user has sent a message
 212          $data = $mform->get_data();
 213          if (!empty($data) && !empty($data->message)) {
 214              if (!confirm_sesskey()) {
 215                  print_error('invalidsesskey');
 216              }
 217              $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
 218              if (!empty($messageid)) {
 219                  //including the id of the user sending the message in the logged URL so the URL works for admins
 220                  //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
 221                  redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
 222              }
 223          }
 224      }
 225  }
 226  
 227  $strmessages = get_string('messages', 'message');
 228  if ($user2realuser) {
 229      $user2fullname = fullname($user2);
 230  
 231      $PAGE->set_title("$strmessages: $user2fullname");
 232  } else {
 233      $PAGE->set_title("{$SITE->shortname}: $strmessages");
 234  }
 235  $PAGE->set_heading(fullname($user1));
 236  
 237  // Remove the user node from the main navigation for this page.
 238  $usernode = $PAGE->navigation->find('users', null);
 239  $usernode->remove();
 240  
 241  $settings = $PAGE->settingsnav->find('messages', null);
 242  // Add the user we are contacting to the breadcrumb.
 243  if (!empty($user2realuser)) {
 244      $usernode = $settings->add(fullname($user2), $url);
 245      $usernode->make_active();
 246  } else {
 247      $settings->make_active();
 248  }
 249  
 250  //now the page contents
 251  echo $OUTPUT->header();
 252  
 253  echo $OUTPUT->box_start('message');
 254  
 255  $countunread = 0; //count of unread messages from $user2
 256  $countunreadtotal = 0; //count of unread messages from all users
 257  
 258  //we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
 259  $viewingnewmessages = false;
 260  if (!empty($user2)) {
 261      //are there any unread messages from $user2
 262      $countunread = message_count_unread_messages($user1, $user2);
 263      if ($countunread>0) {
 264          //mark the messages we're going to display as read
 265          message_mark_messages_read($user1->id, $user2->id);
 266           if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
 267               $viewingnewmessages = true;
 268           }
 269      }
 270  }
 271  $countunreadtotal = message_count_unread_messages($user1);
 272  
 273  if ($currentuser && $countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) {
 274      // If the user has no unread messages, show the search box.
 275      // We don't do this when a user is viewing another user's messages as search doesn't
 276      // handle user A searching user B's messages properly.
 277      $viewing = MESSAGE_VIEW_SEARCH;
 278  }
 279  
 280  $blockedusers = message_get_blocked_users($user1, $user2);
 281  $countblocked = count($blockedusers);
 282  
 283  list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
 284  
 285  message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page);
 286  
 287  echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align'));
 288      if (!empty($user2)) {
 289  
 290          echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory'));
 291  
 292              $visible = 'visible';
 293              $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
 294  
 295              $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
 296              if ($history == MESSAGE_HISTORY_ALL) {
 297                  $displaycount = 0;
 298  
 299                  $recentlabelclass = $historylinkclass = $hidden;
 300              } else if($viewingnewmessages) {
 301                  //if user is viewing new messages only show them the new messages
 302                  $displaycount = $countunread;
 303  
 304                  $recentlabelclass = $historylabelclass = $hidden;
 305              } else {
 306                  //default to only showing the last few messages
 307                  $displaycount = MESSAGE_SHORTVIEW_LIMIT;
 308  
 309                  if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
 310                      $displaycount = $countunread;
 311                  }
 312  
 313                  $recentlinkclass = $historylabelclass = $hidden;
 314              }
 315  
 316              $messagehistorylink =  html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
 317                  $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
 318                      get_string('messagehistoryfull','message'),
 319                      array('class' => $historylinkclass));
 320  
 321                  $messagehistorylink .=  html_writer::start_tag('span', array('class' => $historylabelclass));
 322                      $messagehistorylink .= get_string('messagehistoryfull','message');
 323                  $messagehistorylink .= html_writer::end_tag('span');
 324  
 325                  $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
 326                      get_string('mostrecent','message'),
 327                      array('class' => $recentlinkclass));
 328  
 329                  $messagehistorylink .=  html_writer::start_tag('span', array('class' => $recentlabelclass));
 330                      $messagehistorylink .= get_string('mostrecent','message');
 331                  $messagehistorylink .= html_writer::end_tag('span');
 332  
 333                  if ($viewingnewmessages) {
 334                      $messagehistorylink .=  '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class' => $historyclass)
 335                          $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
 336                      $messagehistorylink .= html_writer::end_tag('span');
 337                  }
 338  
 339              $messagehistorylink .= html_writer::end_tag('div');
 340  
 341              message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages, $showactionlinks);
 342          echo html_writer::end_tag('div');
 343  
 344          //send message form
 345          if ($currentuser && has_capability('moodle/site:sendmessage', $systemcontext) && $user2realuser) {
 346              echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend'));
 347                  if (!empty($messageerror)) {
 348                      echo html_writer::tag('span', $messageerror, array('id' => 'messagewarning'));
 349                  } else {
 350                      // Display a warning if the current user is blocking non-contacts and is about to message to a non-contact
 351                      // Otherwise they may wonder why they never get a reply
 352                      if (message_is_user_non_contact_blocked($user1, $user2)) {
 353                          $msg = get_string('messagingblockednoncontact', 'message', fullname($user2));
 354                          echo html_writer::tag('span', $msg, array('id' => 'messagewarning'));
 355                      }
 356  
 357                      $mform = new send_form();
 358                      $defaultmessage = new stdClass;
 359                      $defaultmessage->id = $user2->id;
 360                      $defaultmessage->viewing = $viewing;
 361                      $defaultmessage->message = '';
 362                      //$defaultmessage->messageformat = FORMAT_MOODLE;
 363                      $mform->set_data($defaultmessage);
 364                      $mform->display();
 365                  }
 366              echo html_writer::end_tag('div');
 367          }
 368      } else if ($viewing == MESSAGE_VIEW_SEARCH) {
 369          message_print_search($advancedsearch, $user1);
 370      } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) {
 371          message_print_recent_conversations($user1, false, $showactionlinks);
 372      } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
 373          message_print_recent_notifications($user1);
 374      }
 375  echo html_writer::end_tag('div');
 376  
 377  echo $OUTPUT->box_end();
 378  
 379  echo $OUTPUT->footer();
 380  
 381  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1