1 |
efrain |
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 |
* Library of functions and constants for module chat
|
|
|
19 |
*
|
|
|
20 |
* @package mod_chat
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot.'/calendar/lib.php');
|
|
|
28 |
|
|
|
29 |
// Event types.
|
|
|
30 |
define('CHAT_EVENT_TYPE_CHATTIME', 'chattime');
|
|
|
31 |
|
|
|
32 |
// Gap between sessions. 5 minutes or more of idleness between messages in a chat means the messages belong in different sessions.
|
|
|
33 |
define('CHAT_SESSION_GAP', 300);
|
|
|
34 |
// Don't publish next chat time
|
|
|
35 |
define('CHAT_SCHEDULE_NONE', 0);
|
|
|
36 |
// Publish the specified time only.
|
|
|
37 |
define('CHAT_SCHEDULE_SINGLE', 1);
|
|
|
38 |
// Repeat chat session at the same time daily.
|
|
|
39 |
define('CHAT_SCHEDULE_DAILY', 2);
|
|
|
40 |
// Repeat chat session at the same time weekly.
|
|
|
41 |
define('CHAT_SCHEDULE_WEEKLY', 3);
|
|
|
42 |
|
|
|
43 |
// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output.
|
|
|
44 |
global $CHAT_HTMLHEAD;
|
|
|
45 |
$CHAT_HTMLHEAD = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head></head>\n<body>\n\n".padding(200);
|
|
|
46 |
|
|
|
47 |
// The HTML head for the message window to start with (with js scrolling).
|
|
|
48 |
global $CHAT_HTMLHEAD_JS;
|
|
|
49 |
$CHAT_HTMLHEAD_JS = <<<EOD
|
|
|
50 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
|
|
51 |
<html><head><script type="text/javascript">
|
|
|
52 |
//<![CDATA[
|
|
|
53 |
function move() {
|
|
|
54 |
if (scroll_active)
|
|
|
55 |
window.scroll(1,400000);
|
|
|
56 |
window.setTimeout("move()",100);
|
|
|
57 |
}
|
|
|
58 |
var scroll_active = true;
|
|
|
59 |
move();
|
|
|
60 |
//]]>
|
|
|
61 |
</script>
|
|
|
62 |
</head>
|
|
|
63 |
<body onBlur="scroll_active = true" onFocus="scroll_active = false">
|
|
|
64 |
EOD;
|
|
|
65 |
global $CHAT_HTMLHEAD_JS;
|
|
|
66 |
$CHAT_HTMLHEAD_JS .= padding(200);
|
|
|
67 |
|
|
|
68 |
// The HTML code for standard empty pages (e.g. if a user was kicked out).
|
|
|
69 |
global $CHAT_HTMLHEAD_OUT;
|
|
|
70 |
$CHAT_HTMLHEAD_OUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>You are out!</title></head><body></body></html>";
|
|
|
71 |
|
|
|
72 |
// The HTML head for the message input page.
|
|
|
73 |
global $CHAT_HTMLHEAD_MSGINPUT;
|
|
|
74 |
$CHAT_HTMLHEAD_MSGINPUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>Message Input</title></head><body>";
|
|
|
75 |
|
|
|
76 |
// The HTML code for the message input page, with JavaScript.
|
|
|
77 |
global $CHAT_HTMLHEAD_MSGINPUT_JS;
|
|
|
78 |
$CHAT_HTMLHEAD_MSGINPUT_JS = <<<EOD
|
|
|
79 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
|
|
80 |
<html>
|
|
|
81 |
<head><title>Message Input</title>
|
|
|
82 |
<script type="text/javascript">
|
|
|
83 |
//<![CDATA[
|
|
|
84 |
scroll_active = true;
|
|
|
85 |
function empty_field_and_submit() {
|
|
|
86 |
document.fdummy.arsc_message.value=document.f.arsc_message.value;
|
|
|
87 |
document.fdummy.submit();
|
|
|
88 |
document.f.arsc_message.focus();
|
|
|
89 |
document.f.arsc_message.select();
|
|
|
90 |
return false;
|
|
|
91 |
}
|
|
|
92 |
//]]>
|
|
|
93 |
</script>
|
|
|
94 |
</head><body OnLoad="document.f.arsc_message.focus();document.f.arsc_message.select();">;
|
|
|
95 |
EOD;
|
|
|
96 |
|
|
|
97 |
// Dummy data that gets output to the browser as needed, in order to make it show output.
|
|
|
98 |
global $CHAT_DUMMY_DATA;
|
|
|
99 |
$CHAT_DUMMY_DATA = padding(200);
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* @param int $n
|
|
|
103 |
* @return string
|
|
|
104 |
*/
|
|
|
105 |
function padding($n) {
|
|
|
106 |
$str = '';
|
|
|
107 |
for ($i = 0; $i < $n; $i++) {
|
|
|
108 |
$str .= "<!-- nix -->\n";
|
|
|
109 |
}
|
|
|
110 |
return $str;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Given an object containing all the necessary data,
|
|
|
115 |
* (defined by the form in mod_form.php) this function
|
|
|
116 |
* will create a new instance and return the id number
|
|
|
117 |
* of the new instance.
|
|
|
118 |
*
|
|
|
119 |
* @global object
|
|
|
120 |
* @param object $chat
|
|
|
121 |
* @return int
|
|
|
122 |
*/
|
|
|
123 |
function chat_add_instance($chat) {
|
|
|
124 |
global $DB, $CFG;
|
|
|
125 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
126 |
|
|
|
127 |
$chat->timemodified = time();
|
|
|
128 |
$chat->chattime = chat_calculate_next_chat_time($chat->schedule, $chat->chattime);
|
|
|
129 |
|
|
|
130 |
$returnid = $DB->insert_record("chat", $chat);
|
|
|
131 |
|
|
|
132 |
if ($chat->schedule > 0) {
|
|
|
133 |
$event = new stdClass();
|
|
|
134 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
135 |
$event->name = $chat->name;
|
|
|
136 |
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
|
|
|
137 |
$event->format = FORMAT_HTML;
|
|
|
138 |
$event->courseid = $chat->course;
|
|
|
139 |
$event->groupid = 0;
|
|
|
140 |
$event->userid = 0;
|
|
|
141 |
$event->modulename = 'chat';
|
|
|
142 |
$event->instance = $returnid;
|
|
|
143 |
$event->eventtype = CHAT_EVENT_TYPE_CHATTIME;
|
|
|
144 |
$event->timestart = $chat->chattime;
|
|
|
145 |
$event->timesort = $chat->chattime;
|
|
|
146 |
$event->timeduration = 0;
|
|
|
147 |
|
|
|
148 |
calendar_event::create($event, false);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if (!empty($chat->completionexpected)) {
|
|
|
152 |
\core_completion\api::update_completion_date_event($chat->coursemodule, 'chat', $returnid, $chat->completionexpected);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
return $returnid;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Given an object containing all the necessary data,
|
|
|
160 |
* (defined by the form in mod_form.php) this function
|
|
|
161 |
* will update an existing instance with new data.
|
|
|
162 |
*
|
|
|
163 |
* @global object
|
|
|
164 |
* @param object $chat
|
|
|
165 |
* @return bool
|
|
|
166 |
*/
|
|
|
167 |
function chat_update_instance($chat) {
|
|
|
168 |
global $DB;
|
|
|
169 |
|
|
|
170 |
$chat->timemodified = time();
|
|
|
171 |
$chat->id = $chat->instance;
|
|
|
172 |
$chat->chattime = chat_calculate_next_chat_time($chat->schedule, $chat->chattime);
|
|
|
173 |
|
|
|
174 |
$DB->update_record("chat", $chat);
|
|
|
175 |
|
|
|
176 |
$event = new stdClass();
|
|
|
177 |
|
|
|
178 |
if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat',
|
|
|
179 |
'instance' => $chat->id, 'eventtype' => CHAT_EVENT_TYPE_CHATTIME))) {
|
|
|
180 |
|
|
|
181 |
if ($chat->schedule > 0) {
|
|
|
182 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
183 |
$event->name = $chat->name;
|
|
|
184 |
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
|
|
|
185 |
$event->format = FORMAT_HTML;
|
|
|
186 |
$event->timestart = $chat->chattime;
|
|
|
187 |
$event->timesort = $chat->chattime;
|
|
|
188 |
|
|
|
189 |
$calendarevent = calendar_event::load($event->id);
|
|
|
190 |
$calendarevent->update($event, false);
|
|
|
191 |
} else {
|
|
|
192 |
// Do not publish this event, so delete it.
|
|
|
193 |
$calendarevent = calendar_event::load($event->id);
|
|
|
194 |
$calendarevent->delete();
|
|
|
195 |
}
|
|
|
196 |
} else {
|
|
|
197 |
// No event, do we need to create one?
|
|
|
198 |
if ($chat->schedule > 0) {
|
|
|
199 |
$event = new stdClass();
|
|
|
200 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
201 |
$event->name = $chat->name;
|
|
|
202 |
$event->description = format_module_intro('chat', $chat, $chat->coursemodule, false);
|
|
|
203 |
$event->format = FORMAT_HTML;
|
|
|
204 |
$event->courseid = $chat->course;
|
|
|
205 |
$event->groupid = 0;
|
|
|
206 |
$event->userid = 0;
|
|
|
207 |
$event->modulename = 'chat';
|
|
|
208 |
$event->instance = $chat->id;
|
|
|
209 |
$event->eventtype = CHAT_EVENT_TYPE_CHATTIME;
|
|
|
210 |
$event->timestart = $chat->chattime;
|
|
|
211 |
$event->timesort = $chat->chattime;
|
|
|
212 |
$event->timeduration = 0;
|
|
|
213 |
|
|
|
214 |
calendar_event::create($event, false);
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$completionexpected = (!empty($chat->completionexpected)) ? $chat->completionexpected : null;
|
|
|
219 |
\core_completion\api::update_completion_date_event($chat->coursemodule, 'chat', $chat->id, $completionexpected);
|
|
|
220 |
|
|
|
221 |
return true;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Given an ID of an instance of this module,
|
|
|
226 |
* this function will permanently delete the instance
|
|
|
227 |
* and any data that depends on it.
|
|
|
228 |
*
|
|
|
229 |
* @global object
|
|
|
230 |
* @param int $id
|
|
|
231 |
* @return bool
|
|
|
232 |
*/
|
|
|
233 |
function chat_delete_instance($id) {
|
|
|
234 |
global $DB;
|
|
|
235 |
|
|
|
236 |
if (! $chat = $DB->get_record('chat', array('id' => $id))) {
|
|
|
237 |
return false;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
$result = true;
|
|
|
241 |
|
|
|
242 |
// Delete any dependent records here.
|
|
|
243 |
|
|
|
244 |
if (! $DB->delete_records('chat', array('id' => $chat->id))) {
|
|
|
245 |
$result = false;
|
|
|
246 |
}
|
|
|
247 |
if (! $DB->delete_records('chat_messages', array('chatid' => $chat->id))) {
|
|
|
248 |
$result = false;
|
|
|
249 |
}
|
|
|
250 |
if (! $DB->delete_records('chat_messages_current', array('chatid' => $chat->id))) {
|
|
|
251 |
$result = false;
|
|
|
252 |
}
|
|
|
253 |
if (! $DB->delete_records('chat_users', array('chatid' => $chat->id))) {
|
|
|
254 |
$result = false;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if (! $DB->delete_records('event', array('modulename' => 'chat', 'instance' => $chat->id))) {
|
|
|
258 |
$result = false;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
return $result;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* Given a course and a date, prints a summary of all chat rooms past and present
|
|
|
266 |
* This function is called from block_recent_activity
|
|
|
267 |
*
|
|
|
268 |
* @global object
|
|
|
269 |
* @global object
|
|
|
270 |
* @global object
|
|
|
271 |
* @param object $course
|
|
|
272 |
* @param bool $viewfullnames
|
|
|
273 |
* @param int|string $timestart Timestamp
|
|
|
274 |
* @return bool
|
|
|
275 |
*/
|
|
|
276 |
function chat_print_recent_activity($course, $viewfullnames, $timestart) {
|
|
|
277 |
global $CFG, $USER, $DB, $OUTPUT;
|
|
|
278 |
|
|
|
279 |
// This is approximate only, but it is really fast.
|
|
|
280 |
$timeout = $CFG->chat_old_ping * 10;
|
|
|
281 |
|
|
|
282 |
if (!$mcms = $DB->get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
|
|
|
283 |
FROM {course_modules} cm
|
|
|
284 |
JOIN {modules} md ON md.id = cm.module
|
|
|
285 |
JOIN {chat} ch ON ch.id = cm.instance
|
|
|
286 |
JOIN {chat_messages} chm ON chm.chatid = ch.id
|
|
|
287 |
WHERE chm.timestamp > ? AND ch.course = ? AND md.name = 'chat'
|
|
|
288 |
GROUP BY cm.id
|
|
|
289 |
ORDER BY lasttime ASC", array($timestart, $course->id))) {
|
|
|
290 |
return false;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
$past = array();
|
|
|
294 |
$current = array();
|
|
|
295 |
$modinfo = get_fast_modinfo($course); // Reference needed because we might load the groups.
|
|
|
296 |
|
|
|
297 |
foreach ($mcms as $cmid => $mcm) {
|
|
|
298 |
if (!array_key_exists($cmid, $modinfo->cms)) {
|
|
|
299 |
continue;
|
|
|
300 |
}
|
|
|
301 |
$cm = $modinfo->cms[$cmid];
|
|
|
302 |
if (!$modinfo->cms[$cm->id]->uservisible) {
|
|
|
303 |
continue;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
if (groups_get_activity_groupmode($cm) != SEPARATEGROUPS
|
|
|
307 |
or has_capability('moodle/site:accessallgroups', context_module::instance($cm->id))) {
|
|
|
308 |
if ($timeout > time() - $mcm->lasttime) {
|
|
|
309 |
$current[] = $cm;
|
|
|
310 |
} else {
|
|
|
311 |
$past[] = $cm;
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
continue;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
// Verify groups in separate mode.
|
|
|
318 |
if (!$mygroupids = $modinfo->get_groups($cm->groupingid)) {
|
|
|
319 |
continue;
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
// Ok, last post was not for my group - we have to query db to get last message from one of my groups.
|
|
|
323 |
// The only minor problem is that the order will not be correct.
|
|
|
324 |
$mygroupids = implode(',', $mygroupids);
|
|
|
325 |
|
|
|
326 |
if (!$mcm = $DB->get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
|
|
|
327 |
FROM {course_modules} cm
|
|
|
328 |
JOIN {chat} ch ON ch.id = cm.instance
|
|
|
329 |
JOIN {chat_messages_current} chm ON chm.chatid = ch.id
|
|
|
330 |
WHERE chm.timestamp > ? AND cm.id = ? AND
|
|
|
331 |
(chm.groupid IN ($mygroupids) OR chm.groupid = 0)
|
|
|
332 |
GROUP BY cm.id", array($timestart, $cm->id))) {
|
|
|
333 |
continue;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
$mcms[$cmid]->lasttime = $mcm->lasttime;
|
|
|
337 |
if ($timeout > time() - $mcm->lasttime) {
|
|
|
338 |
$current[] = $cm;
|
|
|
339 |
} else {
|
|
|
340 |
$past[] = $cm;
|
|
|
341 |
}
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
if (!$past and !$current) {
|
|
|
345 |
return false;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
$strftimerecent = get_string('strftimerecent');
|
|
|
349 |
|
|
|
350 |
if ($past) {
|
|
|
351 |
echo $OUTPUT->heading(get_string("pastchats", 'chat') . ':', 6);
|
|
|
352 |
|
|
|
353 |
foreach ($past as $cm) {
|
|
|
354 |
$link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
|
|
|
355 |
$date = userdate($mcms[$cm->id]->lasttime, $strftimerecent);
|
|
|
356 |
echo '<div class="head"><div class="date">'.$date.'</div></div>';
|
|
|
357 |
echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name, true).'</a></div>';
|
|
|
358 |
}
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
if ($current) {
|
|
|
362 |
echo $OUTPUT->heading(get_string("currentchats", 'chat') . ':', 6);
|
|
|
363 |
|
|
|
364 |
$oldest = floor((time() - $CFG->chat_old_ping) / 10) * 10; // Better db caching.
|
|
|
365 |
|
|
|
366 |
$timeold = time() - $CFG->chat_old_ping;
|
|
|
367 |
$timeold = floor($timeold / 10) * 10; // Better db caching.
|
|
|
368 |
$timeoldext = time() - ($CFG->chat_old_ping * 10); // JSless gui_basic needs much longer timeouts.
|
|
|
369 |
$timeoldext = floor($timeoldext / 10) * 10; // Better db caching.
|
|
|
370 |
|
|
|
371 |
$params = array('timeold' => $timeold, 'timeoldext' => $timeoldext, 'cmid' => $cm->id);
|
|
|
372 |
|
|
|
373 |
$timeout = "AND ((chu.version<>'basic' AND chu.lastping>:timeold) OR (chu.version='basic' AND chu.lastping>:timeoldext))";
|
|
|
374 |
|
|
|
375 |
foreach ($current as $cm) {
|
|
|
376 |
// Count users first.
|
|
|
377 |
$mygroupids = $modinfo->groups[$cm->groupingid];
|
|
|
378 |
if (!empty($mygroupids)) {
|
|
|
379 |
list($subquery, $subparams) = $DB->get_in_or_equal($mygroupids, SQL_PARAMS_NAMED, 'gid');
|
|
|
380 |
$params += $subparams;
|
|
|
381 |
$groupselect = "AND (chu.groupid $subquery OR chu.groupid = 0)";
|
|
|
382 |
} else {
|
|
|
383 |
$groupselect = "";
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
$userfieldsapi = \core_user\fields::for_userpic();
|
|
|
387 |
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
|
|
388 |
if (!$users = $DB->get_records_sql("SELECT $userfields
|
|
|
389 |
FROM {course_modules} cm
|
|
|
390 |
JOIN {chat} ch ON ch.id = cm.instance
|
|
|
391 |
JOIN {chat_users} chu ON chu.chatid = ch.id
|
|
|
392 |
JOIN {user} u ON u.id = chu.userid
|
|
|
393 |
WHERE cm.id = :cmid $timeout $groupselect
|
|
|
394 |
GROUP BY $userfields", $params)) {
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
$link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
|
|
|
398 |
$date = userdate($mcms[$cm->id]->lasttime, $strftimerecent);
|
|
|
399 |
|
|
|
400 |
echo '<div class="head"><div class="date">'.$date.'</div></div>';
|
|
|
401 |
echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name, true).'</a></div>';
|
|
|
402 |
echo '<div class="userlist">';
|
|
|
403 |
if ($users) {
|
|
|
404 |
echo '<ul>';
|
|
|
405 |
foreach ($users as $user) {
|
|
|
406 |
echo '<li>'.fullname($user, $viewfullnames).'</li>';
|
|
|
407 |
}
|
|
|
408 |
echo '</ul>';
|
|
|
409 |
}
|
|
|
410 |
echo '</div>';
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
return true;
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
/**
|
|
|
418 |
* This standard function will check all instances of this module
|
|
|
419 |
* and make sure there are up-to-date events created for each of them.
|
|
|
420 |
* If courseid = 0, then every chat event in the site is checked, else
|
|
|
421 |
* only chat events belonging to the course specified are checked.
|
|
|
422 |
* This function is used, in its new format, by restore_refresh_events()
|
|
|
423 |
*
|
|
|
424 |
* @global object
|
|
|
425 |
* @param int $courseid
|
|
|
426 |
* @param int|stdClass $instance Chat module instance or ID.
|
|
|
427 |
* @param int|stdClass $cm Course module object or ID.
|
|
|
428 |
* @return bool
|
|
|
429 |
*/
|
|
|
430 |
function chat_refresh_events($courseid = 0, $instance = null, $cm = null) {
|
|
|
431 |
global $DB;
|
|
|
432 |
|
|
|
433 |
// If we have instance information then we can just update the one event instead of updating all events.
|
|
|
434 |
if (isset($instance)) {
|
|
|
435 |
if (!is_object($instance)) {
|
|
|
436 |
$instance = $DB->get_record('chat', array('id' => $instance), '*', MUST_EXIST);
|
|
|
437 |
}
|
|
|
438 |
if (isset($cm)) {
|
|
|
439 |
if (!is_object($cm)) {
|
|
|
440 |
chat_prepare_update_events($instance);
|
|
|
441 |
return true;
|
|
|
442 |
} else {
|
|
|
443 |
chat_prepare_update_events($instance, $cm);
|
|
|
444 |
return true;
|
|
|
445 |
}
|
|
|
446 |
}
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
if ($courseid) {
|
|
|
450 |
if (! $chats = $DB->get_records("chat", array("course" => $courseid))) {
|
|
|
451 |
return true;
|
|
|
452 |
}
|
|
|
453 |
} else {
|
|
|
454 |
if (! $chats = $DB->get_records("chat")) {
|
|
|
455 |
return true;
|
|
|
456 |
}
|
|
|
457 |
}
|
|
|
458 |
foreach ($chats as $chat) {
|
|
|
459 |
chat_prepare_update_events($chat);
|
|
|
460 |
}
|
|
|
461 |
return true;
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
/**
|
|
|
465 |
* Updates both the normal and completion calendar events for chat.
|
|
|
466 |
*
|
|
|
467 |
* @param stdClass $chat The chat object (from the DB)
|
|
|
468 |
* @param stdClass $cm The course module object.
|
|
|
469 |
*/
|
|
|
470 |
function chat_prepare_update_events($chat, $cm = null) {
|
|
|
471 |
global $DB;
|
|
|
472 |
if (!isset($cm)) {
|
|
|
473 |
$cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course);
|
|
|
474 |
}
|
|
|
475 |
$event = new stdClass();
|
|
|
476 |
$event->name = $chat->name;
|
|
|
477 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
478 |
$event->description = format_module_intro('chat', $chat, $cm->id, false);
|
|
|
479 |
$event->format = FORMAT_HTML;
|
|
|
480 |
$event->timestart = $chat->chattime;
|
|
|
481 |
$event->timesort = $chat->chattime;
|
|
|
482 |
if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id,
|
|
|
483 |
'eventtype' => CHAT_EVENT_TYPE_CHATTIME))) {
|
|
|
484 |
$calendarevent = calendar_event::load($event->id);
|
|
|
485 |
$calendarevent->update($event, false);
|
|
|
486 |
} else if ($chat->schedule > 0) {
|
|
|
487 |
// The chat is scheduled and the event should be published.
|
|
|
488 |
$event->courseid = $chat->course;
|
|
|
489 |
$event->groupid = 0;
|
|
|
490 |
$event->userid = 0;
|
|
|
491 |
$event->modulename = 'chat';
|
|
|
492 |
$event->instance = $chat->id;
|
|
|
493 |
$event->eventtype = CHAT_EVENT_TYPE_CHATTIME;
|
|
|
494 |
$event->timeduration = 0;
|
|
|
495 |
$event->visible = $cm->visible;
|
|
|
496 |
calendar_event::create($event, false);
|
|
|
497 |
}
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
// Functions that require some SQL.
|
|
|
501 |
|
|
|
502 |
/**
|
|
|
503 |
* @global object
|
|
|
504 |
* @param int $chatid
|
|
|
505 |
* @param int $groupid
|
|
|
506 |
* @param int $groupingid
|
|
|
507 |
* @return array
|
|
|
508 |
*/
|
|
|
509 |
function chat_get_users($chatid, $groupid=0, $groupingid=0) {
|
|
|
510 |
global $DB;
|
|
|
511 |
|
|
|
512 |
$params = array('chatid' => $chatid, 'groupid' => $groupid, 'groupingid' => $groupingid);
|
|
|
513 |
|
|
|
514 |
if ($groupid) {
|
|
|
515 |
$groupselect = " AND (c.groupid=:groupid OR c.groupid='0')";
|
|
|
516 |
} else {
|
|
|
517 |
$groupselect = "";
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
if (!empty($groupingid)) {
|
|
|
521 |
$groupingjoin = "JOIN {groups_members} gm ON u.id = gm.userid
|
|
|
522 |
JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid ";
|
|
|
523 |
|
|
|
524 |
} else {
|
|
|
525 |
$groupingjoin = '';
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
$userfieldsapi = \core_user\fields::for_userpic();
|
|
|
529 |
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
|
|
530 |
return $DB->get_records_sql("SELECT DISTINCT $ufields, c.lastmessageping, c.firstping
|
|
|
531 |
FROM {chat_users} c
|
|
|
532 |
JOIN {user} u ON u.id = c.userid $groupingjoin
|
|
|
533 |
WHERE c.chatid = :chatid $groupselect
|
|
|
534 |
ORDER BY c.firstping ASC", $params);
|
|
|
535 |
}
|
|
|
536 |
|
|
|
537 |
/**
|
|
|
538 |
* @global object
|
|
|
539 |
* @param int $chatid
|
|
|
540 |
* @param int $groupid
|
|
|
541 |
* @return array
|
|
|
542 |
*/
|
|
|
543 |
function chat_get_latest_message($chatid, $groupid=0) {
|
|
|
544 |
global $DB;
|
|
|
545 |
|
|
|
546 |
$params = array('chatid' => $chatid, 'groupid' => $groupid);
|
|
|
547 |
|
|
|
548 |
if ($groupid) {
|
|
|
549 |
$groupselect = "AND (groupid=:groupid OR groupid=0)";
|
|
|
550 |
} else {
|
|
|
551 |
$groupselect = "";
|
|
|
552 |
}
|
|
|
553 |
|
|
|
554 |
$sql = "SELECT *
|
|
|
555 |
FROM {chat_messages_current} WHERE chatid = :chatid $groupselect
|
|
|
556 |
ORDER BY timestamp DESC, id DESC";
|
|
|
557 |
|
|
|
558 |
// Return the lastest one message.
|
|
|
559 |
return $DB->get_record_sql($sql, $params, true);
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* login if not already logged in
|
|
|
564 |
*
|
|
|
565 |
* @global object
|
|
|
566 |
* @global object
|
|
|
567 |
* @param int $chatid
|
|
|
568 |
* @param string $version
|
|
|
569 |
* @param int $groupid
|
|
|
570 |
* @param object $course
|
|
|
571 |
* @return bool|int Returns the chat users sid or false
|
|
|
572 |
*/
|
|
|
573 |
function chat_login_user($chatid, $version, $groupid, $course) {
|
|
|
574 |
global $USER, $DB;
|
|
|
575 |
|
|
|
576 |
if (($version != 'sockets') and $chatuser = $DB->get_record('chat_users', array('chatid' => $chatid,
|
|
|
577 |
'userid' => $USER->id,
|
|
|
578 |
'groupid' => $groupid))) {
|
|
|
579 |
// This will update logged user information.
|
|
|
580 |
$chatuser->version = $version;
|
|
|
581 |
$chatuser->ip = $USER->lastip;
|
|
|
582 |
$chatuser->lastping = time();
|
|
|
583 |
$chatuser->lang = current_language();
|
|
|
584 |
|
|
|
585 |
// Sometimes $USER->lastip is not setup properly during login.
|
|
|
586 |
// Update with current value if possible or provide a dummy value for the db.
|
|
|
587 |
if (empty($chatuser->ip)) {
|
|
|
588 |
$chatuser->ip = getremoteaddr();
|
|
|
589 |
}
|
|
|
590 |
|
|
|
591 |
if (($chatuser->course != $course->id) or ($chatuser->userid != $USER->id)) {
|
|
|
592 |
return false;
|
|
|
593 |
}
|
|
|
594 |
$DB->update_record('chat_users', $chatuser);
|
|
|
595 |
|
|
|
596 |
} else {
|
|
|
597 |
$chatuser = new stdClass();
|
|
|
598 |
$chatuser->chatid = $chatid;
|
|
|
599 |
$chatuser->userid = $USER->id;
|
|
|
600 |
$chatuser->groupid = $groupid;
|
|
|
601 |
$chatuser->version = $version;
|
|
|
602 |
$chatuser->ip = $USER->lastip;
|
|
|
603 |
$chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
|
|
|
604 |
$chatuser->sid = random_string(32);
|
|
|
605 |
$chatuser->course = $course->id; // Caching - needed for current_language too.
|
|
|
606 |
$chatuser->lang = current_language(); // Caching - to resource intensive to find out later.
|
|
|
607 |
|
|
|
608 |
// Sometimes $USER->lastip is not setup properly during login.
|
|
|
609 |
// Update with current value if possible or provide a dummy value for the db.
|
|
|
610 |
if (empty($chatuser->ip)) {
|
|
|
611 |
$chatuser->ip = getremoteaddr();
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
$DB->insert_record('chat_users', $chatuser);
|
|
|
615 |
|
|
|
616 |
if ($version == 'sockets') {
|
|
|
617 |
// Do not send 'enter' message, chatd will do it.
|
|
|
618 |
} else {
|
|
|
619 |
chat_send_chatmessage($chatuser, 'enter', true);
|
|
|
620 |
}
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
return $chatuser->sid;
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
/**
|
|
|
627 |
* Delete the old and in the way
|
|
|
628 |
*
|
|
|
629 |
* @global object
|
|
|
630 |
* @global object
|
|
|
631 |
*/
|
|
|
632 |
function chat_delete_old_users() {
|
|
|
633 |
// Delete the old and in the way.
|
|
|
634 |
global $CFG, $DB;
|
|
|
635 |
|
|
|
636 |
$timeold = time() - $CFG->chat_old_ping;
|
|
|
637 |
$timeoldext = time() - ($CFG->chat_old_ping * 10); // JSless gui_basic needs much longer timeouts.
|
|
|
638 |
|
|
|
639 |
$query = "(version<>'basic' AND lastping<?) OR (version='basic' AND lastping<?)";
|
|
|
640 |
$params = array($timeold, $timeoldext);
|
|
|
641 |
|
|
|
642 |
if ($oldusers = $DB->get_records_select('chat_users', $query, $params) ) {
|
|
|
643 |
$DB->delete_records_select('chat_users', $query, $params);
|
|
|
644 |
foreach ($oldusers as $olduser) {
|
|
|
645 |
chat_send_chatmessage($olduser, 'exit', true);
|
|
|
646 |
}
|
|
|
647 |
}
|
|
|
648 |
}
|
|
|
649 |
|
|
|
650 |
/**
|
|
|
651 |
* Calculate next chat session time based on schedule.
|
|
|
652 |
*
|
|
|
653 |
* @param int $schedule
|
|
|
654 |
* @param int $chattime
|
|
|
655 |
*
|
|
|
656 |
* @return int timestamp
|
|
|
657 |
*/
|
|
|
658 |
function chat_calculate_next_chat_time(int $schedule, int $chattime): int {
|
|
|
659 |
$timenow = time();
|
|
|
660 |
|
|
|
661 |
switch ($schedule) {
|
|
|
662 |
case CHAT_SCHEDULE_DAILY: { // Repeat daily.
|
|
|
663 |
while ($chattime <= $timenow) {
|
|
|
664 |
$chattime += DAYSECS;
|
|
|
665 |
}
|
|
|
666 |
break;
|
|
|
667 |
}
|
|
|
668 |
case CHAT_SCHEDULE_WEEKLY: { // Repeat weekly.
|
|
|
669 |
while ($chattime <= $timenow) {
|
|
|
670 |
$chattime += WEEKSECS;
|
|
|
671 |
}
|
|
|
672 |
break;
|
|
|
673 |
}
|
|
|
674 |
}
|
|
|
675 |
|
|
|
676 |
return $chattime;
|
|
|
677 |
}
|
|
|
678 |
|
|
|
679 |
/**
|
|
|
680 |
* Updates chat records so that the next chat time is correct
|
|
|
681 |
*
|
|
|
682 |
* @global object
|
|
|
683 |
* @param int $chatid
|
|
|
684 |
* @return void
|
|
|
685 |
*/
|
|
|
686 |
function chat_update_chat_times($chatid=0) {
|
|
|
687 |
// Updates chat records so that the next chat time is correct.
|
|
|
688 |
global $DB;
|
|
|
689 |
|
|
|
690 |
$timenow = time();
|
|
|
691 |
|
|
|
692 |
$params = array('timenow' => $timenow, 'chatid' => $chatid);
|
|
|
693 |
|
|
|
694 |
if ($chatid) {
|
|
|
695 |
if (!$chats[] = $DB->get_record_select("chat", "id = :chatid AND chattime <= :timenow AND schedule > 0", $params)) {
|
|
|
696 |
return;
|
|
|
697 |
}
|
|
|
698 |
} else {
|
|
|
699 |
if (!$chats = $DB->get_records_select("chat", "chattime <= :timenow AND schedule > 0", $params)) {
|
|
|
700 |
return;
|
|
|
701 |
}
|
|
|
702 |
}
|
|
|
703 |
|
|
|
704 |
$courseids = [];
|
|
|
705 |
foreach ($chats as $chat) {
|
|
|
706 |
$originalchattime = $chat->chattime;
|
|
|
707 |
$chat->chattime = chat_calculate_next_chat_time($chat->schedule, $chat->chattime);
|
|
|
708 |
if ($originalchattime != $chat->chattime) {
|
|
|
709 |
$courseids[] = $chat->course;
|
|
|
710 |
$DB->update_record("chat", $chat);
|
|
|
711 |
|
|
|
712 |
$cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course);
|
|
|
713 |
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
$event = new stdClass(); // Update calendar too.
|
|
|
717 |
$cond = "modulename='chat' AND eventtype = :eventtype AND instance = :chatid AND timestart <> :chattime";
|
|
|
718 |
$params = ['chattime' => $chat->chattime, 'eventtype' => CHAT_EVENT_TYPE_CHATTIME, 'chatid' => $chat->id];
|
|
|
719 |
|
|
|
720 |
if ($event->id = $DB->get_field_select('event', 'id', $cond, $params)) {
|
|
|
721 |
$event->timestart = $chat->chattime;
|
|
|
722 |
$event->timesort = $chat->chattime;
|
|
|
723 |
$calendarevent = calendar_event::load($event->id);
|
|
|
724 |
$calendarevent->update($event, false);
|
|
|
725 |
}
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
$courseids = array_unique($courseids);
|
|
|
729 |
foreach ($courseids as $courseid) {
|
|
|
730 |
rebuild_course_cache($courseid, true, true);
|
|
|
731 |
}
|
|
|
732 |
}
|
|
|
733 |
|
|
|
734 |
/**
|
|
|
735 |
* Send a message on the chat.
|
|
|
736 |
*
|
|
|
737 |
* @param object $chatuser The chat user record.
|
|
|
738 |
* @param string $messagetext The message to be sent.
|
|
|
739 |
* @param bool $issystem False for non-system messages, true for system messages.
|
|
|
740 |
* @param object $cm The course module object, pass it to save a database query when we trigger the event.
|
|
|
741 |
* @return int The message ID.
|
|
|
742 |
* @since Moodle 2.6
|
|
|
743 |
*/
|
|
|
744 |
function chat_send_chatmessage($chatuser, $messagetext, $issystem = false, $cm = null) {
|
|
|
745 |
global $DB;
|
|
|
746 |
|
|
|
747 |
$message = new stdClass();
|
|
|
748 |
$message->chatid = $chatuser->chatid;
|
|
|
749 |
$message->userid = $chatuser->userid;
|
|
|
750 |
$message->groupid = $chatuser->groupid;
|
|
|
751 |
$message->message = $messagetext;
|
|
|
752 |
$message->issystem = $issystem ? 1 : 0;
|
|
|
753 |
$message->timestamp = time();
|
|
|
754 |
|
|
|
755 |
$messageid = $DB->insert_record('chat_messages', $message);
|
|
|
756 |
$DB->insert_record('chat_messages_current', $message);
|
|
|
757 |
$message->id = $messageid;
|
|
|
758 |
|
|
|
759 |
if (!$issystem) {
|
|
|
760 |
|
|
|
761 |
if (empty($cm)) {
|
|
|
762 |
$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $chatuser->course);
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
$params = array(
|
|
|
766 |
'context' => context_module::instance($cm->id),
|
|
|
767 |
'objectid' => $message->id,
|
|
|
768 |
// We set relateduserid, because when triggered from the chat daemon, the event userid is null.
|
|
|
769 |
'relateduserid' => $chatuser->userid
|
|
|
770 |
);
|
|
|
771 |
$event = \mod_chat\event\message_sent::create($params);
|
|
|
772 |
$event->add_record_snapshot('chat_messages', $message);
|
|
|
773 |
$event->trigger();
|
|
|
774 |
}
|
|
|
775 |
|
|
|
776 |
return $message->id;
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
/**
|
|
|
780 |
* @global object
|
|
|
781 |
* @global object
|
|
|
782 |
* @param object $message
|
|
|
783 |
* @param int $courseid
|
|
|
784 |
* @param object $sender
|
|
|
785 |
* @param object $currentuser
|
|
|
786 |
* @param string $chatlastrow
|
|
|
787 |
* @return bool|string Returns HTML or false
|
|
|
788 |
*/
|
|
|
789 |
function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chatlastrow = null) {
|
|
|
790 |
global $CFG, $USER, $OUTPUT;
|
|
|
791 |
|
|
|
792 |
$output = new stdClass();
|
|
|
793 |
$output->beep = false; // By default.
|
|
|
794 |
$output->refreshusers = false; // By default.
|
|
|
795 |
|
|
|
796 |
// Find the correct timezone for displaying this message.
|
|
|
797 |
$tz = core_date::get_user_timezone($currentuser);
|
|
|
798 |
|
|
|
799 |
$message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
|
|
|
800 |
|
|
|
801 |
$message->picture = $OUTPUT->user_picture($sender, array('size' => false, 'courseid' => $courseid, 'link' => false));
|
|
|
802 |
|
|
|
803 |
if ($courseid) {
|
|
|
804 |
$message->picture = "<a onclick=\"window.open('$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid')\"".
|
|
|
805 |
" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture</a>";
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
// Calculate the row class.
|
|
|
809 |
if ($chatlastrow !== null) {
|
|
|
810 |
$rowclass = ' class="r'.$chatlastrow.'" ';
|
|
|
811 |
} else {
|
|
|
812 |
$rowclass = '';
|
|
|
813 |
}
|
|
|
814 |
|
|
|
815 |
// Start processing the message.
|
|
|
816 |
|
|
|
817 |
if (!empty($message->issystem)) {
|
|
|
818 |
// System event.
|
|
|
819 |
$output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
|
|
|
820 |
$output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td>';
|
|
|
821 |
$output->html .= '<td class="text"><span class="event">'.$output->text.'</span></td></tr></table>';
|
|
|
822 |
$output->basic = '<tr class="r1">
|
|
|
823 |
<th scope="row" class="cell c1 title"></th>
|
|
|
824 |
<td class="cell c2 text">' . get_string('message'.$message->message, 'chat', fullname($sender)) . '</td>
|
|
|
825 |
<td class="cell c3">' . $message->strtime . '</td>
|
|
|
826 |
</tr>';
|
|
|
827 |
if ($message->message == 'exit' or $message->message == 'enter') {
|
|
|
828 |
$output->refreshusers = true; // Force user panel refresh ASAP.
|
|
|
829 |
}
|
|
|
830 |
return $output;
|
|
|
831 |
}
|
|
|
832 |
|
|
|
833 |
// It's not a system event.
|
|
|
834 |
$rawtext = trim($message->message);
|
|
|
835 |
|
|
|
836 |
// Options for format_text, when we get to it...
|
|
|
837 |
// format_text call will parse the text to clean and filter it.
|
|
|
838 |
// It cannot be called here as HTML-isation interferes with special case
|
|
|
839 |
// recognition, but *must* be called on any user-sourced text to be inserted
|
|
|
840 |
// into $outmain.
|
|
|
841 |
$options = new stdClass();
|
|
|
842 |
$options->para = false;
|
|
|
843 |
$options->blanktarget = true;
|
|
|
844 |
|
|
|
845 |
// And now check for special cases.
|
|
|
846 |
$patternto = '#^\s*To\s([^:]+):(.*)#';
|
|
|
847 |
$special = false;
|
|
|
848 |
|
|
|
849 |
if (substr($rawtext, 0, 5) == 'beep ') {
|
|
|
850 |
// It's a beep!
|
|
|
851 |
$special = true;
|
|
|
852 |
$beepwho = trim(substr($rawtext, 5));
|
|
|
853 |
|
|
|
854 |
if ($beepwho == 'all') { // Everyone.
|
|
|
855 |
$outinfobasic = get_string('messagebeepseveryone', 'chat', fullname($sender));
|
|
|
856 |
$outinfo = $message->strtime . ': ' . $outinfobasic;
|
|
|
857 |
$outmain = '';
|
|
|
858 |
|
|
|
859 |
$output->beep = true; // Eventually this should be set to a filename uploaded by the user.
|
|
|
860 |
|
|
|
861 |
} else if ($beepwho == $currentuser->id) { // Current user.
|
|
|
862 |
$outinfobasic = get_string('messagebeepsyou', 'chat', fullname($sender));
|
|
|
863 |
$outinfo = $message->strtime . ': ' . $outinfobasic;
|
|
|
864 |
$outmain = '';
|
|
|
865 |
$output->beep = true;
|
|
|
866 |
|
|
|
867 |
} else { // Something is not caught?
|
|
|
868 |
return false;
|
|
|
869 |
}
|
|
|
870 |
} else if (substr($rawtext, 0, 1) == '/') { // It's a user command.
|
|
|
871 |
$special = true;
|
|
|
872 |
$pattern = '#(^\/)(\w+).*#';
|
|
|
873 |
preg_match($pattern, $rawtext, $matches);
|
|
|
874 |
$command = isset($matches[2]) ? $matches[2] : false;
|
|
|
875 |
// Support some IRC commands.
|
|
|
876 |
switch ($command) {
|
|
|
877 |
case 'me':
|
|
|
878 |
$outinfo = $message->strtime;
|
|
|
879 |
$text = '*** <b>'.$sender->firstname.' '.substr($rawtext, 4).'</b>';
|
|
|
880 |
$outmain = format_text($text, FORMAT_MOODLE, array_merge((array) $options, [
|
|
|
881 |
'context' => \core\context\course::instance($courseid),
|
|
|
882 |
]));
|
|
|
883 |
break;
|
|
|
884 |
default:
|
|
|
885 |
// Error, we set special back to false to use the classic message output.
|
|
|
886 |
$special = false;
|
|
|
887 |
break;
|
|
|
888 |
}
|
|
|
889 |
} else if (preg_match($patternto, $rawtext)) {
|
|
|
890 |
$special = true;
|
|
|
891 |
$matches = array();
|
|
|
892 |
preg_match($patternto, $rawtext, $matches);
|
|
|
893 |
if (isset($matches[1]) && isset($matches[2])) {
|
|
|
894 |
$text = format_text($matches[2], FORMAT_MOODLE, array_merge((array) $options, [
|
|
|
895 |
'context' => \core\context\course::instance($courseid),
|
|
|
896 |
]));
|
|
|
897 |
$outinfo = $message->strtime;
|
|
|
898 |
$outmain = $sender->firstname.' '.get_string('saidto', 'chat').' <i>'.$matches[1].'</i>: '.$text;
|
|
|
899 |
} else {
|
|
|
900 |
// Error, we set special back to false to use the classic message output.
|
|
|
901 |
$special = false;
|
|
|
902 |
}
|
|
|
903 |
}
|
|
|
904 |
|
|
|
905 |
if (!$special) {
|
|
|
906 |
$text = format_text($rawtext, FORMAT_MOODLE, array_merge((array) $options, [
|
|
|
907 |
'context' => \core\context\course::instance($courseid),
|
|
|
908 |
]));
|
|
|
909 |
$outinfo = $message->strtime.' '.$sender->firstname;
|
|
|
910 |
$outmain = $text;
|
|
|
911 |
}
|
|
|
912 |
|
|
|
913 |
// Format the message as a small table.
|
|
|
914 |
|
|
|
915 |
$output->text = strip_tags($outinfo.': '.$outmain);
|
|
|
916 |
|
|
|
917 |
$output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td>";
|
|
|
918 |
$output->html .= "<td class=\"text\"><span class=\"title\">$outinfo</span>";
|
|
|
919 |
if ($outmain) {
|
|
|
920 |
$output->html .= ": $outmain";
|
|
|
921 |
$output->basic = '<tr class="r0">
|
|
|
922 |
<th scope="row" class="cell c1 title">' . $sender->firstname . '</th>
|
|
|
923 |
<td class="cell c2 text">' . $outmain . '</td>
|
|
|
924 |
<td class="cell c3">' . $message->strtime . '</td>
|
|
|
925 |
</tr>';
|
|
|
926 |
} else {
|
|
|
927 |
$output->basic = '<tr class="r1">
|
|
|
928 |
<th scope="row" class="cell c1 title"></th>
|
|
|
929 |
<td class="cell c2 text">' . $outinfobasic . '</td>
|
|
|
930 |
<td class="cell c3">' . $message->strtime . '</td>
|
|
|
931 |
</tr>';
|
|
|
932 |
}
|
|
|
933 |
$output->html .= "</td></tr></table>";
|
|
|
934 |
return $output;
|
|
|
935 |
}
|
|
|
936 |
|
|
|
937 |
/**
|
|
|
938 |
* Given a message object this function formats it appropriately into text and html then returns the formatted data
|
|
|
939 |
* @global object
|
|
|
940 |
* @param object $message
|
|
|
941 |
* @param int $courseid
|
|
|
942 |
* @param object $currentuser
|
|
|
943 |
* @param string $chatlastrow
|
|
|
944 |
* @return bool|string Returns HTML or false
|
|
|
945 |
*/
|
|
|
946 |
function chat_format_message($message, $courseid, $currentuser, $chatlastrow=null) {
|
|
|
947 |
global $DB;
|
|
|
948 |
|
|
|
949 |
static $users; // Cache user lookups.
|
|
|
950 |
|
|
|
951 |
if (isset($users[$message->userid])) {
|
|
|
952 |
$user = $users[$message->userid];
|
|
|
953 |
} else if ($user = $DB->get_record('user', ['id' => $message->userid], implode(',', \core_user\fields::get_picture_fields()))) {
|
|
|
954 |
$users[$message->userid] = $user;
|
|
|
955 |
} else {
|
|
|
956 |
return null;
|
|
|
957 |
}
|
|
|
958 |
return chat_format_message_manually($message, $courseid, $user, $currentuser, $chatlastrow);
|
|
|
959 |
}
|
|
|
960 |
|
|
|
961 |
/**
|
|
|
962 |
* @global object
|
|
|
963 |
* @param object $message message to be displayed.
|
|
|
964 |
* @param mixed $chatuser user chat data
|
|
|
965 |
* @param object $currentuser current user for whom the message should be displayed.
|
|
|
966 |
* @param int $groupingid course module grouping id
|
|
|
967 |
* @param string $theme name of the chat theme.
|
|
|
968 |
* @return bool|string Returns HTML or false
|
|
|
969 |
*/
|
|
|
970 |
function chat_format_message_theme ($message, $chatuser, $currentuser, $groupingid, $theme = 'bubble') {
|
|
|
971 |
global $CFG, $USER, $OUTPUT, $COURSE, $DB, $PAGE;
|
|
|
972 |
require_once($CFG->dirroot.'/mod/chat/locallib.php');
|
|
|
973 |
|
|
|
974 |
static $users; // Cache user lookups.
|
|
|
975 |
|
|
|
976 |
$result = new stdClass();
|
|
|
977 |
|
|
|
978 |
if (file_exists($CFG->dirroot . '/mod/chat/gui_ajax/theme/'.$theme.'/config.php')) {
|
|
|
979 |
include($CFG->dirroot . '/mod/chat/gui_ajax/theme/'.$theme.'/config.php');
|
|
|
980 |
}
|
|
|
981 |
|
|
|
982 |
if (isset($users[$message->userid])) {
|
|
|
983 |
$sender = $users[$message->userid];
|
|
|
984 |
} else if ($sender = $DB->get_record('user', array('id' => $message->userid),
|
|
|
985 |
implode(',', \core_user\fields::get_picture_fields()))) {
|
|
|
986 |
$users[$message->userid] = $sender;
|
|
|
987 |
} else {
|
|
|
988 |
return null;
|
|
|
989 |
}
|
|
|
990 |
|
|
|
991 |
// Find the correct timezone for displaying this message.
|
|
|
992 |
$tz = core_date::get_user_timezone($currentuser);
|
|
|
993 |
|
|
|
994 |
if (empty($chatuser->course)) {
|
|
|
995 |
$courseid = $COURSE->id;
|
|
|
996 |
} else {
|
|
|
997 |
$courseid = $chatuser->course;
|
|
|
998 |
}
|
|
|
999 |
|
|
|
1000 |
$message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
|
|
|
1001 |
$message->picture = $OUTPUT->user_picture($sender, array('courseid' => $courseid));
|
|
|
1002 |
|
|
|
1003 |
$message->picture = "<a target='_blank'".
|
|
|
1004 |
" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture</a>";
|
|
|
1005 |
|
|
|
1006 |
// Start processing the message.
|
|
|
1007 |
if (!empty($message->issystem)) {
|
|
|
1008 |
$result->type = 'system';
|
|
|
1009 |
|
|
|
1010 |
$senderprofile = $CFG->wwwroot.'/user/view.php?id='.$sender->id.'&course='.$courseid;
|
|
|
1011 |
$event = get_string('message'.$message->message, 'chat', fullname($sender));
|
|
|
1012 |
$eventmessage = new event_message($senderprofile, fullname($sender), $message->strtime, $event, $theme);
|
|
|
1013 |
|
|
|
1014 |
$output = $PAGE->get_renderer('mod_chat');
|
|
|
1015 |
$result->html = $output->render($eventmessage);
|
|
|
1016 |
|
|
|
1017 |
return $result;
|
|
|
1018 |
}
|
|
|
1019 |
|
|
|
1020 |
// It's not a system event.
|
|
|
1021 |
$rawtext = trim($message->message);
|
|
|
1022 |
|
|
|
1023 |
// Options for format_text, when we get to it...
|
|
|
1024 |
// format_text call will parse the text to clean and filter it.
|
|
|
1025 |
// It cannot be called here as HTML-isation interferes with special case
|
|
|
1026 |
// recognition, but *must* be called on any user-sourced text to be inserted
|
|
|
1027 |
// into $outmain.
|
|
|
1028 |
$options = [
|
|
|
1029 |
'para' => false,
|
|
|
1030 |
'blanktarget' => true,
|
|
|
1031 |
];
|
|
|
1032 |
|
|
|
1033 |
// And now check for special cases.
|
|
|
1034 |
$special = false;
|
|
|
1035 |
$outtime = $message->strtime;
|
|
|
1036 |
|
|
|
1037 |
// Initialise variables.
|
|
|
1038 |
$outmain = '';
|
|
|
1039 |
$patternto = '#^\s*To\s([^:]+):(.*)#';
|
|
|
1040 |
|
|
|
1041 |
if (substr($rawtext, 0, 5) == 'beep ') {
|
|
|
1042 |
$special = true;
|
|
|
1043 |
// It's a beep!
|
|
|
1044 |
$result->type = 'beep';
|
|
|
1045 |
$beepwho = trim(substr($rawtext, 5));
|
|
|
1046 |
|
|
|
1047 |
if ($beepwho == 'all') { // Everyone.
|
|
|
1048 |
$outmain = get_string('messagebeepseveryone', 'chat', fullname($sender));
|
|
|
1049 |
} else if ($beepwho == $currentuser->id) { // Current user.
|
|
|
1050 |
$outmain = get_string('messagebeepsyou', 'chat', fullname($sender));
|
|
|
1051 |
} else if ($sender->id == $currentuser->id) { // Something is not caught?
|
|
|
1052 |
// Allow beep for a active chat user only, else user can beep anyone and get fullname.
|
|
|
1053 |
if (!empty($chatuser) && is_numeric($beepwho)) {
|
|
|
1054 |
$chatusers = chat_get_users($chatuser->chatid, $chatuser->groupid, $groupingid);
|
|
|
1055 |
if (array_key_exists($beepwho, $chatusers)) {
|
|
|
1056 |
$outmain = get_string('messageyoubeep', 'chat', fullname($chatusers[$beepwho]));
|
|
|
1057 |
} else {
|
|
|
1058 |
$outmain = get_string('messageyoubeep', 'chat', $beepwho);
|
|
|
1059 |
}
|
|
|
1060 |
} else {
|
|
|
1061 |
$outmain = get_string('messageyoubeep', 'chat', $beepwho);
|
|
|
1062 |
}
|
|
|
1063 |
}
|
|
|
1064 |
} else if (substr($rawtext, 0, 1) == '/') { // It's a user command.
|
|
|
1065 |
$special = true;
|
|
|
1066 |
$result->type = 'command';
|
|
|
1067 |
$pattern = '#(^\/)(\w+).*#';
|
|
|
1068 |
preg_match($pattern, $rawtext, $matches);
|
|
|
1069 |
$command = isset($matches[2]) ? $matches[2] : false;
|
|
|
1070 |
// Support some IRC commands.
|
|
|
1071 |
switch ($command) {
|
|
|
1072 |
case 'me':
|
|
|
1073 |
$text = '*** <b>'.$sender->firstname.' '.substr($rawtext, 4).'</b>';
|
|
|
1074 |
$outmain = format_text($text, FORMAT_MOODLE, array_merge($options, [
|
|
|
1075 |
'context' => \core\context\course::instance($courseid),
|
|
|
1076 |
]));
|
|
|
1077 |
break;
|
|
|
1078 |
default:
|
|
|
1079 |
// Error, we set special back to false to use the classic message output.
|
|
|
1080 |
$special = false;
|
|
|
1081 |
break;
|
|
|
1082 |
}
|
|
|
1083 |
} else if (preg_match($patternto, $rawtext)) {
|
|
|
1084 |
$special = true;
|
|
|
1085 |
$result->type = 'dialogue';
|
|
|
1086 |
$matches = array();
|
|
|
1087 |
preg_match($patternto, $rawtext, $matches);
|
|
|
1088 |
if (isset($matches[1]) && isset($matches[2])) {
|
|
|
1089 |
$text = format_text($matches[2], FORMAT_MOODLE, array_merge($options, [
|
|
|
1090 |
'context' => \core\context\course::instance($courseid),
|
|
|
1091 |
]));
|
|
|
1092 |
$outmain = $sender->firstname.' <b>'.get_string('saidto', 'chat').'</b> <i>'.$matches[1].'</i>: '.$text;
|
|
|
1093 |
} else {
|
|
|
1094 |
// Error, we set special back to false to use the classic message output.
|
|
|
1095 |
$special = false;
|
|
|
1096 |
}
|
|
|
1097 |
}
|
|
|
1098 |
|
|
|
1099 |
if (!$special) {
|
|
|
1100 |
$text = format_text($rawtext, FORMAT_MOODLE, array_merge($options, [
|
|
|
1101 |
'context' => \core\context\course::instance($courseid),
|
|
|
1102 |
]));
|
|
|
1103 |
$outmain = $text;
|
|
|
1104 |
}
|
|
|
1105 |
|
|
|
1106 |
$result->text = strip_tags($outtime.': '.$outmain);
|
|
|
1107 |
|
|
|
1108 |
$mymessageclass = '';
|
|
|
1109 |
if ($sender->id == $USER->id) {
|
|
|
1110 |
$mymessageclass = 'chat-message-mymessage';
|
|
|
1111 |
}
|
|
|
1112 |
|
|
|
1113 |
$senderprofile = $CFG->wwwroot.'/user/view.php?id='.$sender->id.'&course='.$courseid;
|
|
|
1114 |
$usermessage = new user_message($senderprofile, fullname($sender), $message->picture,
|
|
|
1115 |
$mymessageclass, $outtime, $outmain, $theme);
|
|
|
1116 |
|
|
|
1117 |
$output = $PAGE->get_renderer('mod_chat');
|
|
|
1118 |
$result->html = $output->render($usermessage);
|
|
|
1119 |
|
|
|
1120 |
// When user beeps other user, then don't show any timestamp to other users in chat.
|
|
|
1121 |
if (('' === $outmain) && $special) {
|
|
|
1122 |
return false;
|
|
|
1123 |
} else {
|
|
|
1124 |
return $result;
|
|
|
1125 |
}
|
|
|
1126 |
}
|
|
|
1127 |
|
|
|
1128 |
/**
|
|
|
1129 |
* @global object $DB
|
|
|
1130 |
* @global object $CFG
|
|
|
1131 |
* @global object $COURSE
|
|
|
1132 |
* @global object $OUTPUT
|
|
|
1133 |
* @param object $users
|
|
|
1134 |
* @param object $course
|
|
|
1135 |
* @return array return formatted user list
|
|
|
1136 |
*/
|
|
|
1137 |
function chat_format_userlist($users, $course) {
|
|
|
1138 |
global $CFG, $DB, $COURSE, $OUTPUT;
|
|
|
1139 |
$result = array();
|
|
|
1140 |
foreach ($users as $user) {
|
|
|
1141 |
$item = array();
|
|
|
1142 |
$item['name'] = fullname($user);
|
|
|
1143 |
$item['url'] = $CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.$course->id;
|
|
|
1144 |
$item['picture'] = $OUTPUT->user_picture($user);
|
|
|
1145 |
$item['id'] = $user->id;
|
|
|
1146 |
$result[] = $item;
|
|
|
1147 |
}
|
|
|
1148 |
return $result;
|
|
|
1149 |
}
|
|
|
1150 |
|
|
|
1151 |
/**
|
|
|
1152 |
* Print json format error
|
|
|
1153 |
* @param string $level
|
|
|
1154 |
* @param string $msg
|
|
|
1155 |
*/
|
|
|
1156 |
function chat_print_error($level, $msg) {
|
|
|
1157 |
header('Content-Length: ' . ob_get_length() );
|
|
|
1158 |
$error = new stdClass();
|
|
|
1159 |
$error->level = $level;
|
|
|
1160 |
$error->msg = $msg;
|
|
|
1161 |
$response['error'] = $error;
|
|
|
1162 |
echo json_encode($response);
|
|
|
1163 |
ob_end_flush();
|
|
|
1164 |
exit;
|
|
|
1165 |
}
|
|
|
1166 |
|
|
|
1167 |
/**
|
|
|
1168 |
* List the actions that correspond to a view of this module.
|
|
|
1169 |
* This is used by the participation report.
|
|
|
1170 |
*
|
|
|
1171 |
* Note: This is not used by new logging system. Event with
|
|
|
1172 |
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
|
|
|
1173 |
* be considered as view action.
|
|
|
1174 |
*
|
|
|
1175 |
* @return array
|
|
|
1176 |
*/
|
|
|
1177 |
function chat_get_view_actions() {
|
|
|
1178 |
return array('view', 'view all', 'report');
|
|
|
1179 |
}
|
|
|
1180 |
|
|
|
1181 |
/**
|
|
|
1182 |
* List the actions that correspond to a post of this module.
|
|
|
1183 |
* This is used by the participation report.
|
|
|
1184 |
*
|
|
|
1185 |
* Note: This is not used by new logging system. Event with
|
|
|
1186 |
* crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
|
|
|
1187 |
* will be considered as post action.
|
|
|
1188 |
*
|
|
|
1189 |
* @return array
|
|
|
1190 |
*/
|
|
|
1191 |
function chat_get_post_actions() {
|
|
|
1192 |
return array('talk');
|
|
|
1193 |
}
|
|
|
1194 |
|
|
|
1195 |
/**
|
|
|
1196 |
* @deprecated since Moodle 3.3, when the block_course_overview block was removed.
|
|
|
1197 |
*/
|
|
|
1198 |
function chat_print_overview() {
|
|
|
1199 |
throw new coding_exception('chat_print_overview() can not be used any more and is obsolete.');
|
|
|
1200 |
}
|
|
|
1201 |
|
|
|
1202 |
|
|
|
1203 |
/**
|
|
|
1204 |
* Implementation of the function for printing the form elements that control
|
|
|
1205 |
* whether the course reset functionality affects the chat.
|
|
|
1206 |
*
|
|
|
1207 |
* @param MoodleQuickForm $mform form passed by reference
|
|
|
1208 |
*/
|
|
|
1209 |
function chat_reset_course_form_definition(&$mform) {
|
|
|
1210 |
$mform->addElement('header', 'chatheader', get_string('modulenameplural', 'chat'));
|
|
|
1211 |
$mform->addElement('advcheckbox', 'reset_chat', get_string('removemessages', 'chat'));
|
|
|
1212 |
}
|
|
|
1213 |
|
|
|
1214 |
/**
|
|
|
1215 |
* Course reset form defaults.
|
|
|
1216 |
*
|
|
|
1217 |
* @param object $course
|
|
|
1218 |
* @return array
|
|
|
1219 |
*/
|
|
|
1220 |
function chat_reset_course_form_defaults($course) {
|
|
|
1221 |
return array('reset_chat' => 1);
|
|
|
1222 |
}
|
|
|
1223 |
|
|
|
1224 |
/**
|
|
|
1225 |
* Actual implementation of the reset course functionality, delete all the
|
|
|
1226 |
* chat messages for course $data->courseid.
|
|
|
1227 |
*
|
|
|
1228 |
* @global object
|
|
|
1229 |
* @global object
|
|
|
1230 |
* @param object $data the data submitted from the reset course.
|
|
|
1231 |
* @return array status array
|
|
|
1232 |
*/
|
|
|
1233 |
function chat_reset_userdata($data) {
|
|
|
1234 |
global $CFG, $DB;
|
|
|
1235 |
|
|
|
1236 |
$componentstr = get_string('modulenameplural', 'chat');
|
|
|
1237 |
$status = array();
|
|
|
1238 |
|
|
|
1239 |
if (!empty($data->reset_chat)) {
|
|
|
1240 |
$chatessql = "SELECT ch.id
|
|
|
1241 |
FROM {chat} ch
|
|
|
1242 |
WHERE ch.course=?";
|
|
|
1243 |
$params = array($data->courseid);
|
|
|
1244 |
|
|
|
1245 |
$DB->delete_records_select('chat_messages', "chatid IN ($chatessql)", $params);
|
|
|
1246 |
$DB->delete_records_select('chat_messages_current', "chatid IN ($chatessql)", $params);
|
|
|
1247 |
$DB->delete_records_select('chat_users', "chatid IN ($chatessql)", $params);
|
|
|
1248 |
$status[] = array('component' => $componentstr, 'item' => get_string('removemessages', 'chat'), 'error' => false);
|
|
|
1249 |
}
|
|
|
1250 |
|
|
|
1251 |
// Updating dates - shift may be negative too.
|
|
|
1252 |
if ($data->timeshift) {
|
|
|
1253 |
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
|
|
|
1254 |
// See MDL-9367.
|
|
|
1255 |
shift_course_mod_dates('chat', array('chattime'), $data->timeshift, $data->courseid);
|
|
|
1256 |
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
|
|
|
1257 |
}
|
|
|
1258 |
|
|
|
1259 |
return $status;
|
|
|
1260 |
}
|
|
|
1261 |
|
|
|
1262 |
/**
|
|
|
1263 |
* @param string $feature FEATURE_xx constant for requested feature
|
|
|
1264 |
* @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
|
|
|
1265 |
*/
|
|
|
1266 |
function chat_supports($feature) {
|
|
|
1267 |
switch($feature) {
|
|
|
1268 |
case FEATURE_GROUPS:
|
|
|
1269 |
return true;
|
|
|
1270 |
case FEATURE_GROUPINGS:
|
|
|
1271 |
return true;
|
|
|
1272 |
case FEATURE_MOD_INTRO:
|
|
|
1273 |
return true;
|
|
|
1274 |
case FEATURE_BACKUP_MOODLE2:
|
|
|
1275 |
return true;
|
|
|
1276 |
case FEATURE_COMPLETION_TRACKS_VIEWS:
|
|
|
1277 |
return true;
|
|
|
1278 |
case FEATURE_GRADE_HAS_GRADE:
|
|
|
1279 |
return false;
|
|
|
1280 |
case FEATURE_GRADE_OUTCOMES:
|
|
|
1281 |
return true;
|
|
|
1282 |
case FEATURE_SHOW_DESCRIPTION:
|
|
|
1283 |
return true;
|
|
|
1284 |
case FEATURE_MOD_PURPOSE:
|
|
|
1285 |
return MOD_PURPOSE_COMMUNICATION;
|
|
|
1286 |
default:
|
|
|
1287 |
return null;
|
|
|
1288 |
}
|
|
|
1289 |
}
|
|
|
1290 |
|
|
|
1291 |
function chat_extend_navigation($navigation, $course, $module, $cm) {
|
|
|
1292 |
global $CFG;
|
|
|
1293 |
|
|
|
1294 |
$currentgroup = groups_get_activity_group($cm, true);
|
|
|
1295 |
|
|
|
1296 |
if (has_capability('mod/chat:chat', context_module::instance($cm->id))) {
|
|
|
1297 |
$strenterchat = get_string('enterchat', 'chat');
|
|
|
1298 |
|
|
|
1299 |
$target = $CFG->wwwroot.'/mod/chat/';
|
|
|
1300 |
$params = array('id' => $cm->instance);
|
|
|
1301 |
|
|
|
1302 |
if ($currentgroup) {
|
|
|
1303 |
$params['groupid'] = $currentgroup;
|
|
|
1304 |
}
|
|
|
1305 |
|
|
|
1306 |
$links = array();
|
|
|
1307 |
|
|
|
1308 |
$url = new moodle_url($target.'gui_'.$CFG->chat_method.'/index.php', $params);
|
|
|
1309 |
$action = new popup_action('click', $url, 'chat'.$course->id.$cm->instance.$currentgroup,
|
|
|
1310 |
array('height' => 500, 'width' => 700));
|
|
|
1311 |
$links[] = new action_link($url, $strenterchat, $action);
|
|
|
1312 |
|
|
|
1313 |
$url = new moodle_url($target.'gui_basic/index.php', $params);
|
|
|
1314 |
$action = new popup_action('click', $url, 'chat'.$course->id.$cm->instance.$currentgroup,
|
|
|
1315 |
array('height' => 500, 'width' => 700));
|
|
|
1316 |
$links[] = new action_link($url, get_string('noframesjs', 'message'), $action);
|
|
|
1317 |
|
|
|
1318 |
foreach ($links as $link) {
|
|
|
1319 |
$navigation->add($link->text, $link, navigation_node::TYPE_SETTING, null , null, new pix_icon('i/group' , ''));
|
|
|
1320 |
}
|
|
|
1321 |
}
|
|
|
1322 |
|
|
|
1323 |
$chatusers = chat_get_users($cm->instance, $currentgroup, $cm->groupingid);
|
|
|
1324 |
if (is_array($chatusers) && count($chatusers) > 0) {
|
|
|
1325 |
$users = $navigation->add(get_string('currentusers', 'chat'));
|
|
|
1326 |
foreach ($chatusers as $chatuser) {
|
|
|
1327 |
$userlink = new moodle_url('/user/view.php', array('id' => $chatuser->id, 'course' => $course->id));
|
|
|
1328 |
$users->add(fullname($chatuser).' '.format_time(time() - $chatuser->lastmessageping),
|
|
|
1329 |
$userlink, navigation_node::TYPE_USER, null, null, new pix_icon('i/user', ''));
|
|
|
1330 |
}
|
|
|
1331 |
}
|
|
|
1332 |
}
|
|
|
1333 |
|
|
|
1334 |
/**
|
|
|
1335 |
* Adds module specific settings to the settings block
|
|
|
1336 |
*
|
|
|
1337 |
* @param settings_navigation $settings The settings navigation object
|
|
|
1338 |
* @param navigation_node $chatnode The node to add module settings to
|
|
|
1339 |
*/
|
|
|
1340 |
function chat_extend_settings_navigation(settings_navigation $settings, navigation_node $chatnode) {
|
|
|
1341 |
global $DB;
|
|
|
1342 |
$chat = $DB->get_record("chat", array("id" => $settings->get_page()->cm->instance));
|
|
|
1343 |
|
|
|
1344 |
$currentgroup = groups_get_activity_group($settings->get_page()->cm, true);
|
|
|
1345 |
if ($currentgroup) {
|
|
|
1346 |
$groupselect = " AND groupid = '$currentgroup'";
|
|
|
1347 |
} else {
|
|
|
1348 |
$groupselect = '';
|
|
|
1349 |
}
|
|
|
1350 |
|
|
|
1351 |
if ($chat->studentlogs || has_capability('mod/chat:readlog', $settings->get_page()->cm->context)) {
|
|
|
1352 |
if ($DB->get_records_select('chat_messages', "chatid = ? $groupselect", array($chat->id))) {
|
|
|
1353 |
$chatnode->add(get_string('pastsessions', 'chat'),
|
|
|
1354 |
new moodle_url('/mod/chat/report.php', array('id' => $settings->get_page()->cm->id)),
|
|
|
1355 |
navigation_node::TYPE_SETTING, null, 'pastsessions');
|
|
|
1356 |
}
|
|
|
1357 |
}
|
|
|
1358 |
}
|
|
|
1359 |
|
|
|
1360 |
/**
|
|
|
1361 |
* user logout event handler
|
|
|
1362 |
*
|
|
|
1363 |
* @param \core\event\user_loggedout $event The event.
|
|
|
1364 |
* @return void
|
|
|
1365 |
*/
|
|
|
1366 |
function chat_user_logout(\core\event\user_loggedout $event) {
|
|
|
1367 |
global $DB;
|
|
|
1368 |
$DB->delete_records('chat_users', array('userid' => $event->objectid));
|
|
|
1369 |
}
|
|
|
1370 |
|
|
|
1371 |
/**
|
|
|
1372 |
* Return a list of page types
|
|
|
1373 |
* @param string $pagetype current page type
|
|
|
1374 |
* @param stdClass $parentcontext Block's parent context
|
|
|
1375 |
* @param stdClass $currentcontext Current context of block
|
|
|
1376 |
*/
|
|
|
1377 |
function chat_page_type_list($pagetype, $parentcontext, $currentcontext) {
|
|
|
1378 |
$modulepagetype = array('mod-chat-*' => get_string('page-mod-chat-x', 'chat'));
|
|
|
1379 |
return $modulepagetype;
|
|
|
1380 |
}
|
|
|
1381 |
|
|
|
1382 |
/**
|
|
|
1383 |
* Return a list of the latest messages in the given chat session.
|
|
|
1384 |
*
|
|
|
1385 |
* @param stdClass $chatuser chat user session data
|
|
|
1386 |
* @param int $chatlasttime last time messages were retrieved
|
|
|
1387 |
* @return array list of messages
|
|
|
1388 |
* @since Moodle 3.0
|
|
|
1389 |
*/
|
|
|
1390 |
function chat_get_latest_messages($chatuser, $chatlasttime) {
|
|
|
1391 |
global $DB;
|
|
|
1392 |
|
|
|
1393 |
$params = array('groupid' => $chatuser->groupid, 'chatid' => $chatuser->chatid, 'lasttime' => $chatlasttime);
|
|
|
1394 |
|
|
|
1395 |
$groupselect = $chatuser->groupid ? " AND (groupid=" . $chatuser->groupid . " OR groupid=0) " : "";
|
|
|
1396 |
|
|
|
1397 |
return $DB->get_records_select('chat_messages_current', 'chatid = :chatid AND timestamp > :lasttime ' . $groupselect,
|
|
|
1398 |
$params, 'timestamp ASC');
|
|
|
1399 |
}
|
|
|
1400 |
|
|
|
1401 |
/**
|
|
|
1402 |
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
|
|
1403 |
*
|
|
|
1404 |
* @param stdClass $chat chat object
|
|
|
1405 |
* @param stdClass $course course object
|
|
|
1406 |
* @param stdClass $cm course module object
|
|
|
1407 |
* @param stdClass $context context object
|
|
|
1408 |
* @since Moodle 3.0
|
|
|
1409 |
*/
|
|
|
1410 |
function chat_view($chat, $course, $cm, $context) {
|
|
|
1411 |
|
|
|
1412 |
// Trigger course_module_viewed event.
|
|
|
1413 |
$params = array(
|
|
|
1414 |
'context' => $context,
|
|
|
1415 |
'objectid' => $chat->id
|
|
|
1416 |
);
|
|
|
1417 |
|
|
|
1418 |
$event = \mod_chat\event\course_module_viewed::create($params);
|
|
|
1419 |
$event->add_record_snapshot('course_modules', $cm);
|
|
|
1420 |
$event->add_record_snapshot('course', $course);
|
|
|
1421 |
$event->add_record_snapshot('chat', $chat);
|
|
|
1422 |
$event->trigger();
|
|
|
1423 |
|
|
|
1424 |
// Completion.
|
|
|
1425 |
$completion = new completion_info($course);
|
|
|
1426 |
$completion->set_module_viewed($cm);
|
|
|
1427 |
}
|
|
|
1428 |
|
|
|
1429 |
/**
|
|
|
1430 |
* This function receives a calendar event and returns the action associated with it, or null if there is none.
|
|
|
1431 |
*
|
|
|
1432 |
* This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
|
|
|
1433 |
* is not displayed on the block.
|
|
|
1434 |
*
|
|
|
1435 |
* @param calendar_event $event
|
|
|
1436 |
* @param \core_calendar\action_factory $factory
|
|
|
1437 |
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
|
|
|
1438 |
* @return \core_calendar\local\event\entities\action_interface|null
|
|
|
1439 |
*/
|
|
|
1440 |
function mod_chat_core_calendar_provide_event_action(calendar_event $event,
|
|
|
1441 |
\core_calendar\action_factory $factory,
|
|
|
1442 |
int $userid = 0) {
|
|
|
1443 |
global $USER, $DB;
|
|
|
1444 |
|
|
|
1445 |
if ($userid) {
|
|
|
1446 |
$user = core_user::get_user($userid, 'id, timezone');
|
|
|
1447 |
} else {
|
|
|
1448 |
$user = $USER;
|
|
|
1449 |
}
|
|
|
1450 |
|
|
|
1451 |
$cm = get_fast_modinfo($event->courseid, $user->id)->instances['chat'][$event->instance];
|
|
|
1452 |
|
|
|
1453 |
if (!$cm->uservisible) {
|
|
|
1454 |
// The module is not visible to the user for any reason.
|
|
|
1455 |
return null;
|
|
|
1456 |
}
|
|
|
1457 |
|
|
|
1458 |
$completion = new \completion_info($cm->get_course());
|
|
|
1459 |
|
|
|
1460 |
$completiondata = $completion->get_data($cm, false, $userid);
|
|
|
1461 |
|
|
|
1462 |
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
|
|
|
1463 |
return null;
|
|
|
1464 |
}
|
|
|
1465 |
|
|
|
1466 |
$chattime = $DB->get_field('chat', 'chattime', array('id' => $event->instance));
|
|
|
1467 |
$usertimezone = core_date::get_user_timezone($user);
|
|
|
1468 |
$chattimemidnight = usergetmidnight($chattime, $usertimezone);
|
|
|
1469 |
$todaymidnight = usergetmidnight(time(), $usertimezone);
|
|
|
1470 |
|
|
|
1471 |
if ($chattime < $todaymidnight) {
|
|
|
1472 |
// The chat is before today. Do not show at all.
|
|
|
1473 |
return null;
|
|
|
1474 |
} else {
|
|
|
1475 |
// The chat is actionable if it is at some point today.
|
|
|
1476 |
$actionable = $chattimemidnight == $todaymidnight;
|
|
|
1477 |
|
|
|
1478 |
return $factory->create_instance(
|
|
|
1479 |
get_string('enterchat', 'chat'),
|
|
|
1480 |
new \moodle_url('/mod/chat/view.php', array('id' => $cm->id)),
|
|
|
1481 |
1,
|
|
|
1482 |
$actionable
|
|
|
1483 |
);
|
|
|
1484 |
}
|
|
|
1485 |
}
|
|
|
1486 |
|
|
|
1487 |
/**
|
|
|
1488 |
* Given a set of messages for a chat, return the completed chat sessions (including optionally not completed ones).
|
|
|
1489 |
*
|
|
|
1490 |
* @param array $messages list of messages from a chat. It is assumed that these are sorted by timestamp in DESCENDING order.
|
|
|
1491 |
* @param bool $showall whether to include incomplete sessions or not
|
|
|
1492 |
* @return array the list of sessions
|
|
|
1493 |
* @since Moodle 3.5
|
|
|
1494 |
*/
|
|
|
1495 |
function chat_get_sessions($messages, $showall = false) {
|
|
|
1496 |
$sessions = [];
|
|
|
1497 |
$start = 0;
|
|
|
1498 |
$end = 0;
|
|
|
1499 |
$sessiontimes = [];
|
|
|
1500 |
|
|
|
1501 |
// Group messages by session times.
|
|
|
1502 |
foreach ($messages as $message) {
|
|
|
1503 |
// Initialise values start-end times if necessary.
|
|
|
1504 |
if (empty($start)) {
|
|
|
1505 |
$start = $message->timestamp;
|
|
|
1506 |
}
|
|
|
1507 |
if (empty($end)) {
|
|
|
1508 |
$end = $message->timestamp;
|
|
|
1509 |
}
|
|
|
1510 |
|
|
|
1511 |
// If this message's timestamp has been more than the gap, it means it's been idle.
|
|
|
1512 |
if ($start - $message->timestamp > CHAT_SESSION_GAP) {
|
|
|
1513 |
// Mark this as the session end of the next session.
|
|
|
1514 |
$end = $message->timestamp;
|
|
|
1515 |
}
|
|
|
1516 |
// Use this time as the session's start (until it gets overwritten on the next iteration, if needed).
|
|
|
1517 |
$start = $message->timestamp;
|
|
|
1518 |
|
|
|
1519 |
// Set this start-end pair in our list of session times.
|
|
|
1520 |
$sessiontimes[$end]['sessionstart'] = $start;
|
|
|
1521 |
if (!isset($sessiontimes[$end]['sessionend'])) {
|
|
|
1522 |
$sessiontimes[$end]['sessionend'] = $end;
|
|
|
1523 |
}
|
|
|
1524 |
if ($message->userid && !$message->issystem) {
|
|
|
1525 |
if (!isset($sessiontimes[$end]['sessionusers'][$message->userid])) {
|
|
|
1526 |
$sessiontimes[$end]['sessionusers'][$message->userid] = 1;
|
|
|
1527 |
} else {
|
|
|
1528 |
$sessiontimes[$end]['sessionusers'][$message->userid]++;
|
|
|
1529 |
}
|
|
|
1530 |
}
|
|
|
1531 |
}
|
|
|
1532 |
|
|
|
1533 |
// Go through each session time and prepare the session data to be returned.
|
|
|
1534 |
foreach ($sessiontimes as $sessionend => $sessiondata) {
|
|
|
1535 |
if (!isset($sessiondata['sessionusers'])) {
|
|
|
1536 |
$sessiondata['sessionusers'] = [];
|
|
|
1537 |
}
|
|
|
1538 |
$sessionusers = $sessiondata['sessionusers'];
|
|
|
1539 |
$sessionstart = $sessiondata['sessionstart'];
|
|
|
1540 |
|
|
|
1541 |
$iscomplete = $sessionend - $sessionstart > 60 && count($sessionusers) > 1;
|
|
|
1542 |
if ($showall || $iscomplete) {
|
|
|
1543 |
$sessions[] = (object) ($sessiondata + ['iscomplete' => $iscomplete]);
|
|
|
1544 |
}
|
|
|
1545 |
}
|
|
|
1546 |
|
|
|
1547 |
return $sessions;
|
|
|
1548 |
}
|
|
|
1549 |
|
|
|
1550 |
/**
|
|
|
1551 |
* Return the messages of the given chat session.
|
|
|
1552 |
*
|
|
|
1553 |
* @param int $chatid the chat id
|
|
|
1554 |
* @param mixed $group false if groups not used, int if groups used, 0 means all groups
|
|
|
1555 |
* @param int $start the session start timestamp (0 to not filter by time)
|
|
|
1556 |
* @param int $end the session end timestamp (0 to not filter by time)
|
|
|
1557 |
* @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter)
|
|
|
1558 |
* @return array session messages
|
|
|
1559 |
* @since Moodle 3.5
|
|
|
1560 |
*/
|
|
|
1561 |
function chat_get_session_messages($chatid, $group = false, $start = 0, $end = 0, $sort = '') {
|
|
|
1562 |
global $DB;
|
|
|
1563 |
|
|
|
1564 |
$params = array('chatid' => $chatid);
|
|
|
1565 |
|
|
|
1566 |
// If the user is allocated to a group, only show messages from people in the same group, or no group.
|
|
|
1567 |
if ($group) {
|
|
|
1568 |
$groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
|
|
|
1569 |
$params['currentgroup'] = $group;
|
|
|
1570 |
} else {
|
|
|
1571 |
$groupselect = "";
|
|
|
1572 |
}
|
|
|
1573 |
|
|
|
1574 |
$select = "chatid = :chatid $groupselect";
|
|
|
1575 |
if (!empty($start)) {
|
|
|
1576 |
$select .= ' AND timestamp >= :start';
|
|
|
1577 |
$params['start'] = $start;
|
|
|
1578 |
}
|
|
|
1579 |
if (!empty($end)) {
|
|
|
1580 |
$select .= ' AND timestamp <= :end';
|
|
|
1581 |
$params['end'] = $end;
|
|
|
1582 |
}
|
|
|
1583 |
|
|
|
1584 |
return $DB->get_records_select('chat_messages', $select, $params, $sort);
|
|
|
1585 |
}
|
|
|
1586 |
|
|
|
1587 |
/**
|
|
|
1588 |
* Add a get_coursemodule_info function in case chat instance wants to add 'extra' information
|
|
|
1589 |
* for the course (see resource).
|
|
|
1590 |
*
|
|
|
1591 |
* Given a course_module object, this function returns any "extra" information that may be needed
|
|
|
1592 |
* when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
|
|
|
1593 |
*
|
|
|
1594 |
* @param stdClass $coursemodule The coursemodule object (record).
|
|
|
1595 |
* @return cached_cm_info An object on information that the courses
|
|
|
1596 |
* will know about (most noticeably, an icon).
|
|
|
1597 |
*/
|
|
|
1598 |
function chat_get_coursemodule_info($coursemodule) {
|
|
|
1599 |
global $DB;
|
|
|
1600 |
|
|
|
1601 |
$dbparams = ['id' => $coursemodule->instance];
|
|
|
1602 |
$fields = 'id, name, intro, introformat, chattime, schedule';
|
|
|
1603 |
if (!$chat = $DB->get_record('chat', $dbparams, $fields)) {
|
|
|
1604 |
return false;
|
|
|
1605 |
}
|
|
|
1606 |
|
|
|
1607 |
$result = new cached_cm_info();
|
|
|
1608 |
$result->name = $chat->name;
|
|
|
1609 |
if ($coursemodule->showdescription) {
|
|
|
1610 |
// Convert intro to html. Do not filter cached version, filters run at display time.
|
|
|
1611 |
$result->content = format_module_intro('chat', $chat, $coursemodule->id, false);
|
|
|
1612 |
}
|
|
|
1613 |
|
|
|
1614 |
// Populate some other values that can be used in calendar or on dashboard.
|
|
|
1615 |
if ($chat->chattime) {
|
|
|
1616 |
$result->customdata['chattime'] = $chat->chattime;
|
|
|
1617 |
$result->customdata['schedule'] = $chat->schedule;
|
|
|
1618 |
}
|
|
|
1619 |
|
|
|
1620 |
return $result;
|
|
|
1621 |
}
|