3 require_once('../../../config.php');
4 require_once('../lib.php');
6 $id = required_param('id', PARAM_INT
);
7 $groupid = optional_param('groupid', 0, PARAM_INT
); // only for teachers
8 $message = optional_param('message', '', PARAM_CLEAN
);
9 $refresh = optional_param('refresh', '', PARAM_RAW
); // force refresh
10 $last = optional_param('last', 0, PARAM_INT
); // last time refresh or sending
11 $newonly = optional_param('newonly', 0, PARAM_BOOL
); // show only new messages
13 if (!$chat = get_record('chat', 'id', $id)) {
14 error('Could not find that chat room!');
17 if (!$course = get_record('course', 'id', $chat->course
)) {
18 error('Could not find the course this belongs to!');
21 if (!$cm = get_coursemodule_from_instance('chat', $chat->id
, $course->id
)) {
22 error('Course Module ID was incorrect');
25 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
26 require_login($course->id
, false, $cm);
27 require_capability('mod/chat:chat',$context);
29 /// Check to see if groups are being used here
30 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
31 if ($groupid = get_and_set_current_group($course, $groupmode, $groupid)) {
32 if (!$group = get_record('groups', 'id', $groupid)) {
33 error("That group (id $groupid) doesn't exist!");
35 $groupname = ': '.$group->name
;
37 $groupname = ': '.get_string('allparticipants');
44 $strchat = get_string('modulename', 'chat'); // must be before current_language() in chat_login_user() to force course language!!!
45 $strchats = get_string('modulenameplural', 'chat');
46 $stridle = get_String('idle', 'chat');
47 if (!$chat_sid = chat_login_user($chat->id
, 'basic', $groupid, $course)) {
48 error('Could not log in to chat room!!');
51 if (!$chatusers = chat_get_users($chat->id
, $groupid)) {
52 error(get_string('errornousers', 'chat'));
55 set_field('chat_users', 'lastping', time(), 'sid', $chat_sid);
57 if (!isset($SESSION->chatprefs
)) {
58 $SESSION->chatprefs
= array();
60 if (!isset($SESSION->chatprefs
[$chat->id
])) {
61 $SESSION->chatprefs
[$chat->id
] = array();
62 $SESSION->chatprefs
[$chat->id
]['chatentered'] = time();
64 $chatentered = $SESSION->chatprefs
[$chat->id
]['chatentered'];
66 $refreshedmessage = '';
68 if (!empty($refresh) and data_submitted()) {
69 $refreshedmessage = $message;
71 chat_delete_old_users();
73 } else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
76 $newmessage = new object();
77 $newmessage->chatid
= $chat->id
;
78 $newmessage->userid
= $USER->id
;
79 $newmessage->groupid
= $groupid;
80 $newmessage->systrem
= 0;
81 $newmessage->message
= $message;
82 $newmessage->timestamp
= time();
83 if (!insert_record('chat_messages', $newmessage)) {
84 error('Could not insert a chat message!');
87 set_field('chat_users', 'lastmessageping', time(), 'sid', $chat_sid);
89 add_to_log($course->id
, 'chat', 'talk', "view.php?id=$cm->id", $chat->id
, $cm->id
);
92 chat_delete_old_users();
94 redirect('index.php?id='.$id.'&newonly='.$newonly.'&last='.$last);
98 print_header("$strchat: $course->shortname: ".format_string($chat->name
,true)."$groupname", '', '', 'message');
100 echo '<div id="mod-chat-gui_basic">';
101 echo '<h1>'.get_string('participants').'</h1>';
102 echo '<div id="participants"><ul>';
103 foreach($chatusers as $chu) {
105 print_user_picture($chu->id
, $course->id
, $chu->picture
, 24, false, false, '', false);
106 echo '<div class="userinfo">';
107 echo fullname($chu).' ';
108 if ($idle = time() - $chu->lastmessageping
) {
109 echo '<span class="idle">'.$stridle.' '.format_time($idle).'</span>';
111 echo '<span class="idle" />';
117 echo '<div id="send">';
118 echo '<form id="editing" method="post" action="index.php">';
120 $usehtmleditor = can_use_html_editor();
121 echo '<h1><label for="message">'.get_string('sendmessage', 'message').'</label></h1>';
123 echo '<input type="text" id="message" name="message" value="'.s($refreshedmessage, true).'" size="60" />';
125 echo '<input type="hidden" name="id" value="'.$id.'" />';
126 echo '<input type="hidden" name="groupid" value="'.$groupid.'" />';
127 echo '<input type="hidden" name="last" value="'.time().'" />';
128 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
129 echo '<input type="submit" value="'.get_string('submit').'" /> ';
130 echo '<input type="submit" name="refresh" value="'.get_string('refresh').'" />';
131 echo '<input type="checkbox" name="newonly" id="newonly" '.($newonly?
'checked="checked" ':'').'/><label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
136 echo '<div id="messages">';
137 echo '<h1>'.get_string('messages', 'chat').'</h1>';
139 $allmessages = array();
140 $options = new object();
141 $options->para
= false;
142 $options->newlines
= true;
145 $lastsql = "AND timestamp > $last";
150 $groupselect = $groupid ?
"AND (groupid='$groupid' OR groupid='0')" : "";
151 $messages = get_records_select("chat_messages",
152 "chatid = '$chat->id' AND timestamp > $chatentered $lastsql $groupselect",
156 foreach ($messages as $message) {
157 $allmessages[] = chat_format_message($message, $course->id
, $USER);
161 if (empty($allmessages)) {
162 echo get_string('nomessagesfound', 'message');
164 foreach ($allmessages as $message) {
165 echo $message->basic
;
171 print_footer('none');