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