Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  define('NO_MOODLE_COOKIES', true); // Session not used here.
  18  
  19  require_once('../../../config.php');
  20  require_once($CFG->dirroot.'/mod/chat/lib.php');
  21  
  22  $chatsid   = required_param('chat_sid', PARAM_ALPHANUM);
  23  $beep       = optional_param('beep', 0, PARAM_INT);  // Beep target.
  24  
  25  $PAGE->set_url('/mod/chat/gui_header_js/users.php', array('chat_sid' => $chatsid));
  26  $PAGE->set_popup_notification_allowed(false);
  27  
  28  if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
  29      print_error('notlogged', 'chat');
  30  }
  31  
  32  // Get the minimal course.
  33  if (!$course = $DB->get_record('course', array('id' => $chatuser->course))) {
  34      print_error('invalidcourseid');
  35  }
  36  
  37  // Get the user theme and enough info to be used in chat_format_message() which passes it along to.
  38  // No optimisation here, it would break again in future!
  39  if (!$user = $DB->get_record('user', array('id' => $chatuser->userid, 'deleted' => 0, 'suspended' => 0))) {
  40      print_error('invaliduser');
  41  }
  42  \core\session\manager::set_user($user);
  43  
  44  $PAGE->set_pagelayout('embedded');
  45  
  46  // Setup course, lang and theme.
  47  $PAGE->set_course($course);
  48  
  49  $courseid = $chatuser->course;
  50  
  51  if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $courseid)) {
  52      print_error('invalidcoursemodule');
  53  }
  54  
  55  if ($beep) {
  56      chat_send_chatmessage($chatuser, "beep $beep", 0, $cm);
  57      $chatuser->lastmessageping = time(); // A beep is a ping.
  58  }
  59  
  60  $chatuser->lastping = time();
  61  $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id' => $chatuser->id));
  62  
  63  $refreshurl = "users.php?chat_sid=$chatsid";
  64  
  65  // Get list of users.
  66  
  67  if (!$chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid)) {
  68      print_error('errornousers', 'chat');
  69  }
  70  
  71  $uidles = Array();
  72  foreach ($chatusers as $chatuser) {
  73      $uidles[] = $chatuser->id;
  74  }
  75  
  76  $module = array(
  77      'name'      => 'mod_chat_header',
  78      'fullpath'  => '/mod/chat/gui_header_js/module.js',
  79      'requires'  => array('node')
  80  );
  81  $PAGE->requires->js_init_call('M.mod_chat_header.init_users', array($uidles), false, $module);
  82  
  83  // Print user panel body.
  84  $timenow    = time();
  85  $stridle    = get_string('idle', 'chat');
  86  $strbeep    = get_string('beep', 'chat');
  87  
  88  $table = new html_table();
  89  $table->width = '100%';
  90  $table->data = array();
  91  foreach ($chatusers as $chatuser) {
  92      $lastping = $timenow - $chatuser->lastmessageping;
  93      $min = (int) ($lastping / 60);
  94      $sec = $lastping - ($min * 60);
  95      $min = $min < 10 ? '0'.$min : $min;
  96      $sec = $sec < 10 ? '0'.$sec : $sec;
  97      $idle = $min.':'.$sec;
  98  
  99      $row = array();
 100      $row[0] = $OUTPUT->user_picture($chatuser, array('courseid' => $courseid, 'popup' => true));
 101      $row[1]  = html_writer::start_tag('p');
 102      $row[1] .= html_writer::start_tag('font', array('size' => '1'));
 103      $row[1] .= fullname($chatuser).'<br />';
 104      $row[1] .= html_writer::tag('span', $stridle . html_writer::tag('span',
 105                                                                      $idle,
 106                                                                      array('name' => 'uidles', 'id' => 'uidle'.$chatuser->id)),
 107                                  array('class' => 'dimmed_text')) . ' ';
 108      $row[1] .= html_writer::tag('a', $strbeep, array('href' => new moodle_url('/mod/chat/gui_header_js/users.php',
 109                                                                                array('chat_sid' => $chatsid,
 110                                                                                'beep' => $chatuser->id))));
 111      $row[1] .= html_writer::end_tag('font');
 112      $row[1] .= html_writer::end_tag('p');
 113      $table->data[] = $row;
 114  }
 115  
 116  ob_start();
 117  echo $OUTPUT->header();
 118  echo html_writer::tag('div', html_writer::tag('a', get_string('refresh'),
 119          array('href' => $refreshurl, 'id' => 'refreshLink')), array('style' => 'display:none'));
 120  echo html_writer::table($table);
 121  echo $OUTPUT->footer();
 122  
 123  // Support HTTP Keep-Alive by printing Content-Length
 124  //
 125  // If the user pane is refreshing often, using keepalives
 126  // is lighter on the server and faster for most clients.
 127  //
 128  // Apache is normally configured to have a 15s timeout on
 129  // keepalives, so let's observe that. Unfortunately, we cannot
 130  // autodetect the keepalive timeout.
 131  //
 132  // Using keepalives when the refresh is longer than the timeout
 133  // wastes server resources keeping an apache child around on a
 134  // connection that will timeout. So we don't.
 135  if ($CFG->chat_refresh_userlist < 15) {
 136      header("Content-Length: " . ob_get_length() );
 137      ob_end_flush();
 138  }
 139  
 140  exit; // No further output.
 141