MDL-42434 mod_chat: add again the usingchat help icon
[moodle.git] / mod / chat / gui_basic / index.php
CommitLineData
eb588b22 1<?php
c52b4db5
AD
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/>.
eb588b22 16
17require_once('../../../config.php');
18require_once('../lib.php');
19
20$id = required_param('id', PARAM_INT);
c52b4db5 21$groupid = optional_param('groupid', 0, PARAM_INT); // Only for teachers.
f4091f8e 22$message = optional_param('message', '', PARAM_CLEANHTML);
c52b4db5
AD
23$refresh = optional_param('refresh', '', PARAM_RAW); // Force refresh.
24$last = optional_param('last', 0, PARAM_INT); // Last time refresh or sending.
25$newonly = optional_param('newonly', 0, PARAM_BOOL); // Show only new messages.
eb588b22 26
c52b4db5 27$url = new moodle_url('/mod/chat/gui_basic/index.php', array('id' => $id));
eb588b22 28if ($groupid !== 0) {
29 $url->param('groupid', $groupid);
30}
31if ($message !== 0) {
32 $url->param('message', $message);
33}
34if ($refresh !== 0) {
35 $url->param('refresh', $refresh);
36}
37if ($last !== 0) {
38 $url->param('last', $last);
39}
40if ($newonly !== 0) {
41 $url->param('newonly', $newonly);
42}
43$PAGE->set_url($url);
44
c52b4db5 45if (!$chat = $DB->get_record('chat', array('id' => $id))) {
eb588b22 46 print_error('invalidid', 'chat');
47}
48
c52b4db5 49if (!$course = $DB->get_record('course', array('id' => $chat->course))) {
eb588b22 50 print_error('invalidcourseid');
51}
52
53if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
54 print_error('invalidcoursemodule');
55}
56
6536217c 57$context = context_module::instance($cm->id);
cdbea7ee 58require_login($course, false, $cm);
1d5bd3d2 59require_capability('mod/chat:chat', $context);
a2505cfd 60$PAGE->set_pagelayout('popup');
dc514c1a 61$PAGE->set_popup_notification_allowed(false);
6ee78cee 62
a2505cfd 63// Check to see if groups are being used here.
c52b4db5 64if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
eb588b22 65 if ($groupid = groups_get_activity_group($cm)) {
9250bf81 66 if (!$group = groups_get_group($groupid)) {
eb588b22 67 print_error('invalidgroupid');
6ee78cee 68 }
eb588b22 69 $groupname = ': '.$group->name;
6ee78cee 70 } else {
eb588b22 71 $groupname = ': '.get_string('allparticipants');
6ee78cee 72 }
eb588b22 73} else {
74 $groupid = 0;
75 $groupname = '';
76}
77
c52b4db5 78$strchat = get_string('modulename', 'chat'); // Must be before current_language() in chat_login_user() to force course language!
eb588b22 79$strchats = get_string('modulenameplural', 'chat');
c52b4db5
AD
80$stridle = get_string('idle', 'chat');
81if (!$chatsid = chat_login_user($chat->id, 'basic', $groupid, $course)) {
eb588b22 82 print_error('cantlogin', 'chat');
83}
84
85if (!$chatusers = chat_get_users($chat->id, $groupid, $cm->groupingid)) {
86 print_error('errornousers', 'chat');
87}
88
c52b4db5 89$DB->set_field('chat_users', 'lastping', time(), array('sid' => $chatsid));
eb588b22 90
91if (!isset($SESSION->chatprefs)) {
92 $SESSION->chatprefs = array();
93}
94if (!isset($SESSION->chatprefs[$chat->id])) {
95 $SESSION->chatprefs[$chat->id] = array();
96 $SESSION->chatprefs[$chat->id]['chatentered'] = time();
97}
98$chatentered = $SESSION->chatprefs[$chat->id]['chatentered'];
99
100$refreshedmessage = '';
101
102if (!empty($refresh) and data_submitted()) {
103 $refreshedmessage = $message;
104
105 chat_delete_old_users();
106
107} else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
108
c52b4db5 109 if ($message != '') {
eb588b22 110
c52b4db5 111 $chatuser = $DB->get_record('chat_users', array('sid' => $chatsid));
f49d814a 112 chat_send_chatmessage($chatuser, $message, 0, $cm);
eb588b22 113
c52b4db5 114 $DB->set_field('chat_users', 'lastmessageping', time(), array('sid' => $chatsid));
6ee78cee 115 }
116
eb588b22 117 chat_delete_old_users();
118
c52b4db5 119 $url = new moodle_url('/mod/chat/gui_basic/index.php', array('id' => $id, 'newonly' => $newonly, 'last' => $last));
0cc5b5a3 120 redirect($url);
eb588b22 121}
122
c52b4db5 123$PAGE->set_title("$strchat: $course->shortname: ".format_string($chat->name, true)."$groupname");
eb588b22 124echo $OUTPUT->header();
0cc5b5a3 125echo $OUTPUT->container_start(null, 'page-mod-chat-gui_basic');
66e2b9f8
AD
126
127echo $OUTPUT->heading(format_string($course->shortname), 1);
128echo $OUTPUT->heading(format_string($chat->name), 2);
129
130echo $OUTPUT->heading(get_string('participants'), 3);
0cc5b5a3
DC
131
132echo $OUTPUT->box_start('generalbox', 'participants');
133echo '<ul>';
c52b4db5 134foreach ($chatusers as $chu) {
04d2e674 135 echo '<li class="clearfix">';
c52b4db5 136 echo $OUTPUT->user_picture($chu, array('size' => 24, 'courseid' => $course->id));
eb588b22 137 echo '<div class="userinfo">';
138 echo fullname($chu).' ';
139 if ($idle = time() - $chu->lastmessageping) {
140 echo '<span class="idle">'.$stridle.' '.format_time($idle).'</span>';
6ee78cee 141 } else {
eb588b22 142 echo '<span class="idle" />';
6ee78cee 143 }
eb588b22 144 echo '</div>';
145 echo '</li>';
146}
0cc5b5a3
DC
147echo '</ul>';
148echo $OUTPUT->box_end();
eb588b22 149echo '<div id="send">';
150echo '<form id="editing" method="post" action="index.php">';
151
522b89ce
SA
152echo '<h2><label for="message">' . get_string('sendmessage', 'message');
153echo $OUTPUT->help_icon('usingchat', 'chat');
154echo '</label></h2>';
5cac5fa4 155echo '<div class="mb-1">';
f619064b 156echo '<input type="text" id="message" class="form-control" name="message" value="'.s($refreshedmessage, true).'" size="60" />';
5cac5fa4 157echo '</div><div class="mb-1">';
eb588b22 158echo '<input type="hidden" name="id" value="'.$id.'" />';
159echo '<input type="hidden" name="groupid" value="'.$groupid.'" />';
160echo '<input type="hidden" name="last" value="'.time().'" />';
161echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
f619064b
DW
162echo '<input type="submit" class="btn btn-primary" value="'.get_string('submit').'" />&nbsp;';
163echo '<input type="submit" class="btn btn-secondary" name="refresh" value="'.get_string('refresh').'" />';
5cac5fa4 164echo '<input type="checkbox" class="mx-1" name="newonly" id="newonly" '.($newonly ? 'checked="checked" ' : '').'/>';
c52b4db5 165echo '<label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
eb588b22 166echo '</div>';
167echo '</form>';
168echo '</div>';
169
170echo '<div id="messages">';
66e2b9f8 171echo $OUTPUT->heading(get_string('messages', 'chat'), 3);
eb588b22 172
173$allmessages = array();
39790bd8 174$options = new stdClass();
eb588b22 175$options->para = false;
176$options->newlines = true;
177
c52b4db5 178$params = array('last' => $last, 'groupid' => $groupid, 'chatid' => $chat->id, 'chatentered' => $chatentered);
eb588b22 179
180if ($newonly) {
181 $lastsql = "AND timestamp > :last";
182} else {
183 $lastsql = "";
184}
185
186$groupselect = $groupid ? "AND (groupid=:groupid OR groupid=0)" : "";
187
188$messages = $DB->get_records_select("chat_messages_current",
189 "chatid = :chatid AND timestamp > :chatentered $lastsql $groupselect", $params,
190 "timestamp DESC");
191
192if ($messages) {
193 foreach ($messages as $message) {
194 $allmessages[] = chat_format_message($message, $course->id, $USER);
6ee78cee 195 }
eb588b22 196}
83f47164
JM
197echo '<table class="generaltable"><tbody>';
198echo '<tr>
199 <th scope="col" class="cell">' . get_string('from') . '</th>
200 <th scope="col" class="cell">' . get_string('message', 'message') . '</th>
201 <th scope="col" class="cell">' . get_string('time') . '</th>
202 </tr>';
eb588b22 203if (empty($allmessages)) {
204 echo get_string('nomessagesfound', 'message');
205} else {
206 foreach ($allmessages as $message) {
207 echo $message->basic;
6ee78cee 208 }
eb588b22 209}
83f47164 210echo '</tbody></table>';
0cc5b5a3
DC
211echo '</div>';
212echo $OUTPUT->container_end();
eb588b22 213echo $OUTPUT->footer();