2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Library of functions and constants for module 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
25 require_once($CFG->dirroot
.'/calendar/lib.php');
27 // The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output.
28 global $CHAT_HTMLHEAD;
29 $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);
31 // The HTML head for the message window to start with (with js scrolling).
32 global $CHAT_HTMLHEAD_JS;
33 $CHAT_HTMLHEAD_JS = <<<EOD
34 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
35 <html><head><script type="text/javascript">
39 window.scroll(1,400000);
40 window.setTimeout("move()",100);
42 var scroll_active = true;
47 <body onBlur="scroll_active = true" onFocus="scroll_active = false">
49 global $CHAT_HTMLHEAD_JS;
50 $CHAT_HTMLHEAD_JS .= padding(200);
52 // The HTML code for standard empty pages (e.g. if a user was kicked out).
53 global $CHAT_HTMLHEAD_OUT;
54 $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>";
56 // The HTML head for the message input page.
57 global $CHAT_HTMLHEAD_MSGINPUT;
58 $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>";
60 // The HTML code for the message input page, with JavaScript.
61 global $CHAT_HTMLHEAD_MSGINPUT_JS;
62 $CHAT_HTMLHEAD_MSGINPUT_JS = <<<EOD
63 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
65 <head><title>Message Input</title>
66 <script type="text/javascript">
69 function empty_field_and_submit() {
70 document.fdummy.arsc_message.value=document.f.arsc_message.value;
71 document.fdummy.submit();
72 document.f.arsc_message.focus();
73 document.f.arsc_message.select();
78 </head><body OnLoad="document.f.arsc_message.focus();document.f.arsc_message.select();">;
81 // Dummy data that gets output to the browser as needed, in order to make it show output.
82 global $CHAT_DUMMY_DATA;
83 $CHAT_DUMMY_DATA = padding(200);
89 function padding($n) {
91 for ($i = 0; $i < $n; $i++
) {
92 $str .= "<!-- nix -->\n";
98 * Given an object containing all the necessary data,
99 * (defined by the form in mod_form.php) this function
100 * will create a new instance and return the id number
101 * of the new instance.
104 * @param object $chat
107 function chat_add_instance($chat) {
110 $chat->timemodified
= time();
112 $returnid = $DB->insert_record("chat", $chat);
114 $event = new stdClass();
115 $event->name
= $chat->name
;
116 $event->description
= format_module_intro('chat', $chat, $chat->coursemodule
);
117 $event->courseid
= $chat->course
;
120 $event->modulename
= 'chat';
121 $event->instance
= $returnid;
122 $event->eventtype
= 'chattime';
123 $event->timestart
= $chat->chattime
;
124 $event->timeduration
= 0;
126 calendar_event
::create($event);
132 * Given an object containing all the necessary data,
133 * (defined by the form in mod_form.php) this function
134 * will update an existing instance with new data.
137 * @param object $chat
140 function chat_update_instance($chat) {
143 $chat->timemodified
= time();
144 $chat->id
= $chat->instance
;
146 $DB->update_record("chat", $chat);
148 $event = new stdClass();
150 if ($event->id
= $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id
))) {
152 $event->name
= $chat->name
;
153 $event->description
= format_module_intro('chat', $chat, $chat->coursemodule
);
154 $event->timestart
= $chat->chattime
;
156 $calendarevent = calendar_event
::load($event->id
);
157 $calendarevent->update($event);
164 * Given an ID of an instance of this module,
165 * this function will permanently delete the instance
166 * and any data that depends on it.
172 function chat_delete_instance($id) {
175 if (! $chat = $DB->get_record('chat', array('id' => $id))) {
181 // Delete any dependent records here.
183 if (! $DB->delete_records('chat', array('id' => $chat->id
))) {
186 if (! $DB->delete_records('chat_messages', array('chatid' => $chat->id
))) {
189 if (! $DB->delete_records('chat_messages_current', array('chatid' => $chat->id
))) {
192 if (! $DB->delete_records('chat_users', array('chatid' => $chat->id
))) {
196 if (! $DB->delete_records('event', array('modulename' => 'chat', 'instance' => $chat->id
))) {
204 * Given a course and a date, prints a summary of all chat rooms past and present
205 * This function is called from block_recent_activity
210 * @param object $course
211 * @param bool $viewfullnames
212 * @param int|string $timestart Timestamp
215 function chat_print_recent_activity($course, $viewfullnames, $timestart) {
216 global $CFG, $USER, $DB, $OUTPUT;
218 // This is approximate only, but it is really fast.
219 $timeout = $CFG->chat_old_ping
* 10;
221 if (!$mcms = $DB->get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
222 FROM {course_modules} cm
223 JOIN {modules} md ON md.id = cm.module
224 JOIN {chat} ch ON ch.id = cm.instance
225 JOIN {chat_messages} chm ON chm.chatid = ch.id
226 WHERE chm.timestamp > ? AND ch.course = ? AND md.name = 'chat'
228 ORDER BY lasttime ASC", array($timestart, $course->id
))) {
234 $modinfo = get_fast_modinfo($course); // Reference needed because we might load the groups.
236 foreach ($mcms as $cmid => $mcm) {
237 if (!array_key_exists($cmid, $modinfo->cms
)) {
240 $cm = $modinfo->cms
[$cmid];
241 if (!$modinfo->cms
[$cm->id
]->uservisible
) {
245 if (groups_get_activity_groupmode($cm) != SEPARATEGROUPS
246 or has_capability('moodle/site:accessallgroups', context_module
::instance($cm->id
))) {
247 if ($timeout > time() - $mcm->lasttime
) {
256 // Verify groups in separate mode.
257 if (!$mygroupids = $modinfo->get_groups($cm->groupingid
)) {
261 // Ok, last post was not for my group - we have to query db to get last message from one of my groups.
262 // The only minor problem is that the order will not be correct.
263 $mygroupids = implode(',', $mygroupids);
265 if (!$mcm = $DB->get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
266 FROM {course_modules} cm
267 JOIN {chat} ch ON ch.id = cm.instance
268 JOIN {chat_messages_current} chm ON chm.chatid = ch.id
269 WHERE chm.timestamp > ? AND cm.id = ? AND
270 (chm.groupid IN ($mygroupids) OR chm.groupid = 0)
271 GROUP BY cm.id", array($timestart, $cm->id
))) {
275 $mcms[$cmid]->lasttime
= $mcm->lasttime
;
276 if ($timeout > time() - $mcm->lasttime
) {
283 if (!$past and !$current) {
287 $strftimerecent = get_string('strftimerecent');
290 echo $OUTPUT->heading(get_string("pastchats", 'chat').':', 3);
292 foreach ($past as $cm) {
293 $link = $CFG->wwwroot
.'/mod/chat/view.php?id='.$cm->id
;
294 $date = userdate($mcms[$cm->id
]->lasttime
, $strftimerecent);
295 echo '<div class="head"><div class="date">'.$date.'</div></div>';
296 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name
, true).'</a></div>';
301 echo $OUTPUT->heading(get_string("currentchats", 'chat').':', 3);
303 $oldest = floor((time() - $CFG->chat_old_ping
) / 10) * 10; // Better db caching.
305 $timeold = time() - $CFG->chat_old_ping
;
306 $timeold = floor($timeold / 10) * 10; // Better db caching.
307 $timeoldext = time() - ($CFG->chat_old_ping
* 10); // JSless gui_basic needs much longer timeouts.
308 $timeoldext = floor($timeoldext / 10) * 10; // Better db caching.
310 $params = array('timeold' => $timeold, 'timeoldext' => $timeoldext, 'cmid' => $cm->id
);
312 $timeout = "AND ((chu.version<>'basic' AND chu.lastping>:timeold) OR (chu.version='basic' AND chu.lastping>:timeoldext))";
314 foreach ($current as $cm) {
315 // Count users first.
316 $mygroupids = $modinfo->groups
[$cm->groupingid
];
317 if (!empty($mygroupids)) {
318 list($subquery, $subparams) = $DB->get_in_or_equal($mygroupids, SQL_PARAMS_NAMED
, 'gid');
319 $params +
= $subparams;
320 $groupselect = "AND (chu.groupid $subquery OR chu.groupid = 0)";
325 $userfields = user_picture
::fields('u');
326 if (!$users = $DB->get_records_sql("SELECT $userfields
327 FROM {course_modules} cm
328 JOIN {chat} ch ON ch.id = cm.instance
329 JOIN {chat_users} chu ON chu.chatid = ch.id
330 JOIN {user} u ON u.id = chu.userid
331 WHERE cm.id = :cmid $timeout $groupselect
332 GROUP BY $userfields", $params)) {
335 $link = $CFG->wwwroot
.'/mod/chat/view.php?id='.$cm->id
;
336 $date = userdate($mcms[$cm->id
]->lasttime
, $strftimerecent);
338 echo '<div class="head"><div class="date">'.$date.'</div></div>';
339 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name
, true).'</a></div>';
340 echo '<div class="userlist">';
343 foreach ($users as $user) {
344 echo '<li>'.fullname($user, $viewfullnames).'</li>';
356 * Function to be run periodically according to the moodle cron
357 * This function searches for things that need to be done, such
358 * as sending out mail, toggling flags etc ...
363 function chat_cron () {
366 chat_update_chat_times();
368 chat_delete_old_users();
370 // Delete old messages with a single SQL query.
371 $subselect = "SELECT c.keepdays
373 WHERE c.id = {chat_messages}.chatid";
377 WHERE ($subselect) > 0 AND timestamp < ( ".time()." -($subselect) * 24 * 3600)";
382 FROM {chat_messages_current}
383 WHERE timestamp < ( ".time()." - 8 * 3600)";
391 * This standard function will check all instances of this module
392 * and make sure there are up-to-date events created for each of them.
393 * If courseid = 0, then every chat event in the site is checked, else
394 * only chat events belonging to the course specified are checked.
395 * This function is used, in its new format, by restore_refresh_events()
398 * @param int $courseid
401 function chat_refresh_events($courseid = 0) {
405 if (! $chats = $DB->get_records("chat", array("course" => $courseid))) {
409 if (! $chats = $DB->get_records("chat")) {
413 $moduleid = $DB->get_field('modules', 'id', array('name' => 'chat'));
415 foreach ($chats as $chat) {
416 $cm = get_coursemodule_from_id('chat', $chat->id
);
417 $event = new stdClass();
418 $event->name
= $chat->name
;
419 $event->description
= format_module_intro('chat', $chat, $cm->id
);
420 $event->timestart
= $chat->chattime
;
422 if ($event->id
= $DB->get_field('event', 'id', array('modulename' => 'chat', 'instance' => $chat->id
))) {
423 $calendarevent = calendar_event
::load($event->id
);
424 $calendarevent->update($event);
426 $event->courseid
= $chat->course
;
429 $event->modulename
= 'chat';
430 $event->instance
= $chat->id
;
431 $event->eventtype
= 'chattime';
432 $event->timeduration
= 0;
433 $event->visible
= $DB->get_field('course_modules', 'visible', array('module' => $moduleid, 'instance' => $chat->id
));
435 calendar_event
::create($event);
441 // Functions that require some SQL.
446 * @param int $groupid
447 * @param int $groupingid
450 function chat_get_users($chatid, $groupid=0, $groupingid=0) {
453 $params = array('chatid' => $chatid, 'groupid' => $groupid, 'groupingid' => $groupingid);
456 $groupselect = " AND (c.groupid=:groupid OR c.groupid='0')";
461 if (!empty($groupingid)) {
462 $groupingjoin = "JOIN {groups_members} gm ON u.id = gm.userid
463 JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid ";
469 $ufields = user_picture
::fields('u');
470 return $DB->get_records_sql("SELECT DISTINCT $ufields, c.lastmessageping, c.firstping
472 JOIN {user} u ON u.id = c.userid $groupingjoin
473 WHERE c.chatid = :chatid $groupselect
474 ORDER BY c.firstping ASC", $params);
480 * @param int $groupid
483 function chat_get_latest_message($chatid, $groupid=0) {
486 $params = array('chatid' => $chatid, 'groupid' => $groupid);
489 $groupselect = "AND (groupid=:groupid OR groupid=0)";
495 FROM {chat_messages_current} WHERE chatid = :chatid $groupselect
496 ORDER BY timestamp DESC";
498 // Return the lastest one message.
499 return $DB->get_record_sql($sql, $params, true);
503 * login if not already logged in
508 * @param string $version
509 * @param int $groupid
510 * @param object $course
511 * @return bool|int Returns the chat users sid or false
513 function chat_login_user($chatid, $version, $groupid, $course) {
516 if (($version != 'sockets') and $chatuser = $DB->get_record('chat_users', array('chatid' => $chatid,
517 'userid' => $USER->id
,
518 'groupid' => $groupid))) {
519 // This will update logged user information.
520 $chatuser->version
= $version;
521 $chatuser->ip
= $USER->lastip
;
522 $chatuser->lastping
= time();
523 $chatuser->lang
= current_language();
525 // Sometimes $USER->lastip is not setup properly during login.
526 // Update with current value if possible or provide a dummy value for the db.
527 if (empty($chatuser->ip
)) {
528 $chatuser->ip
= getremoteaddr();
531 if (($chatuser->course
!= $course->id
) or ($chatuser->userid
!= $USER->id
)) {
534 $DB->update_record('chat_users', $chatuser);
537 $chatuser = new stdClass();
538 $chatuser->chatid
= $chatid;
539 $chatuser->userid
= $USER->id
;
540 $chatuser->groupid
= $groupid;
541 $chatuser->version
= $version;
542 $chatuser->ip
= $USER->lastip
;
543 $chatuser->lastping
= $chatuser->firstping
= $chatuser->lastmessageping
= time();
544 $chatuser->sid
= random_string(32);
545 $chatuser->course
= $course->id
; // Caching - needed for current_language too.
546 $chatuser->lang
= current_language(); // Caching - to resource intensive to find out later.
548 // Sometimes $USER->lastip is not setup properly during login.
549 // Update with current value if possible or provide a dummy value for the db.
550 if (empty($chatuser->ip
)) {
551 $chatuser->ip
= getremoteaddr();
554 $DB->insert_record('chat_users', $chatuser);
556 if ($version == 'sockets') {
557 // Do not send 'enter' message, chatd will do it.
559 chat_send_chatmessage($chatuser, 'enter', true);
563 return $chatuser->sid
;
567 * Delete the old and in the way
572 function chat_delete_old_users() {
573 // Delete the old and in the way.
576 $timeold = time() - $CFG->chat_old_ping
;
577 $timeoldext = time() - ($CFG->chat_old_ping
* 10); // JSless gui_basic needs much longer timeouts.
579 $query = "(version<>'basic' AND lastping<?) OR (version='basic' AND lastping<?)";
580 $params = array($timeold, $timeoldext);
582 if ($oldusers = $DB->get_records_select('chat_users', $query, $params) ) {
583 $DB->delete_records_select('chat_users', $query, $params);
584 foreach ($oldusers as $olduser) {
585 chat_send_chatmessage($olduser, 'exit', true);
591 * Updates chat records so that the next chat time is correct
597 function chat_update_chat_times($chatid=0) {
598 // Updates chat records so that the next chat time is correct.
603 $params = array('timenow' => $timenow, 'chatid' => $chatid);
606 if (!$chats[] = $DB->get_record_select("chat", "id = :chatid AND chattime <= :timenow AND schedule > 0", $params)) {
610 if (!$chats = $DB->get_records_select("chat", "chattime <= :timenow AND schedule > 0", $params)) {
615 foreach ($chats as $chat) {
616 switch ($chat->schedule
) {
617 case 1: // Single event - turn off schedule and disable.
621 case 2: // Repeat daily.
622 while ($chat->chattime
<= $timenow) {
623 $chat->chattime +
= 24 * 3600;
626 case 3: // Repeat weekly.
627 while ($chat->chattime
<= $timenow) {
628 $chat->chattime +
= 7 * 24 * 3600;
632 $DB->update_record("chat", $chat);
634 $event = new stdClass(); // Update calendar too.
636 $cond = "modulename='chat' AND instance = :chatid AND timestart <> :chattime";
637 $params = array('chattime' => $chat->chattime
, 'chatid' => $chatid);
639 if ($event->id
= $DB->get_field_select('event', 'id', $cond, $params)) {
640 $event->timestart
= $chat->chattime
;
641 $calendarevent = calendar_event
::load($event->id
);
642 $calendarevent->update($event, false);
648 * Send a message on the chat.
650 * @param object $chatuser The chat user record.
651 * @param string $messagetext The message to be sent.
652 * @param bool $system False for non-system messages, true for system messages.
653 * @param object $cm The course module object, pass it to save a database query when we trigger the event.
654 * @return int The message ID.
657 function chat_send_chatmessage($chatuser, $messagetext, $system = false, $cm = null) {
660 $message = new stdClass();
661 $message->chatid
= $chatuser->chatid
;
662 $message->userid
= $chatuser->userid
;
663 $message->groupid
= $chatuser->groupid
;
664 $message->message
= $messagetext;
665 $message->system
= $system ?
1 : 0;
666 $message->timestamp
= time();
668 $messageid = $DB->insert_record('chat_messages', $message);
669 $DB->insert_record('chat_messages_current', $message);
670 $message->id
= $messageid;
675 $cm = get_coursemodule_from_instance('chat', $chatuser->chatid
, $chatuser->course
);
679 'context' => context_module
::instance($cm->id
),
680 'objectid' => $message->id
,
681 // We set relateduserid, because when triggered from the chat daemon, the event userid is null.
682 'relateduserid' => $chatuser->userid
684 $event = \mod_chat\event\message_sent
::create($params);
685 $event->add_record_snapshot('chat_messages', $message);
695 * @param object $message
696 * @param int $courseid
697 * @param object $sender
698 * @param object $currentuser
699 * @param string $chatlastrow
700 * @return bool|string Returns HTML or false
702 function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chatlastrow = null) {
703 global $CFG, $USER, $OUTPUT;
705 $output = new stdClass();
706 $output->beep
= false; // By default.
707 $output->refreshusers
= false; // By default.
709 // Use get_user_timezone() to find the correct timezone for displaying this message.
710 // It's either the current user's timezone or else decided by some Moodle config setting.
711 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
712 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone.
713 $USER->timezone
= 99;
714 $tz = get_user_timezone($currentuser->timezone
);
716 // Before formatting the message time string, set $USER->timezone to the above.
717 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
718 // message times appear off because DST is not taken into account when it should be.
719 $USER->timezone
= $tz;
720 $message->strtime
= userdate($message->timestamp
, get_string('strftimemessage', 'chat'), $tz);
722 $message->picture
= $OUTPUT->user_picture($sender, array('size' => false, 'courseid' => $courseid, 'link' => false));
725 $message->picture
= "<a onclick=\"window.open('$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid')\"".
726 " href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture</a>";
729 // Calculate the row class.
730 if ($chatlastrow !== null) {
731 $rowclass = ' class="r'.$chatlastrow.'" ';
736 // Start processing the message.
738 if (!empty($message->system
)) {
740 $output->text
= $message->strtime
.': '.get_string('message'.$message->message
, 'chat', fullname($sender));
741 $output->html
= '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture
.'</td>';
742 $output->html
.= '<td class="text"><span class="event">'.$output->text
.'</span></td></tr></table>';
743 $output->basic
= '<tr class="r1">
744 <th scope="row" class="cell c1 title"></th>
745 <td class="cell c2 text">' . get_string('message'.$message->message
, 'chat', fullname($sender)) . '</td>
746 <td class="cell c3">' . $message->strtime
. '</td>
748 if ($message->message
== 'exit' or $message->message
== 'enter') {
749 $output->refreshusers
= true; // Force user panel refresh ASAP.
754 // It's not a system event.
755 $text = trim($message->message
);
757 // Parse the text to clean and filter it.
758 $options = new stdClass();
759 $options->para
= false;
760 $text = format_text($text, FORMAT_MOODLE
, $options, $courseid);
762 // And now check for special cases.
763 $patternto = '#^\s*To\s([^:]+):(.*)#';
766 if (substr($text, 0, 5) == 'beep ') {
769 $beepwho = trim(substr($text, 5));
771 if ($beepwho == 'all') { // Everyone.
772 $outinfobasic = get_string('messagebeepseveryone', 'chat', fullname($sender));
773 $outinfo = $message->strtime
. ': ' . $outinfobasic;
776 $output->beep
= true; // Eventually this should be set to a filename uploaded by the user.
778 } else if ($beepwho == $currentuser->id
) { // Current user.
779 $outinfobasic = get_string('messagebeepsyou', 'chat', fullname($sender));
780 $outinfo = $message->strtime
. ': ' . $outinfobasic;
782 $output->beep
= true;
784 } else { // Something is not caught?
787 } else if (substr($text, 0, 1) == '/') { // It's a user command.
789 $pattern = '#(^\/)(\w+).*#';
790 preg_match($pattern, $text, $matches);
791 $command = isset($matches[2]) ?
$matches[2] : false;
792 // Support some IRC commands.
795 $outinfo = $message->strtime
;
796 $outmain = '*** <b>'.$sender->firstname
.' '.substr($text, 4).'</b>';
799 // Error, we set special back to false to use the classic message output.
803 } else if (preg_match($patternto, $text)) {
806 preg_match($patternto, $text, $matches);
807 if (isset($matches[1]) && isset($matches[2])) {
808 $outinfo = $message->strtime
;
809 $outmain = $sender->firstname
.' '.get_string('saidto', 'chat').' <i>'.$matches[1].'</i>: '.$matches[2];
811 // Error, we set special back to false to use the classic message output.
817 $outinfo = $message->strtime
.' '.$sender->firstname
;
821 // Format the message as a small table.
823 $output->text
= strip_tags($outinfo.': '.$outmain);
825 $output->html
= "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td>";
826 $output->html
.= "<td class=\"text\"><span class=\"title\">$outinfo</span>";
828 $output->html
.= ": $outmain";
829 $output->basic
= '<tr class="r0">
830 <th scope="row" class="cell c1 title">' . $sender->firstname
. '</th>
831 <td class="cell c2 text">' . $outmain . '</td>
832 <td class="cell c3">' . $message->strtime
. '</td>
835 $output->basic
= '<tr class="r1">
836 <th scope="row" class="cell c1 title"></th>
837 <td class="cell c2 text">' . $outinfobasic . '</td>
838 <td class="cell c3">' . $message->strtime
. '</td>
841 $output->html
.= "</td></tr></table>";
846 * Given a message object this function formats it appropriately into text and html then returns the formatted data
848 * @param object $message
849 * @param int $courseid
850 * @param object $currentuser
851 * @param string $chatlastrow
852 * @return bool|string Returns HTML or false
854 function chat_format_message($message, $courseid, $currentuser, $chatlastrow=null) {
857 static $users; // Cache user lookups.
859 if (isset($users[$message->userid
])) {
860 $user = $users[$message->userid
];
861 } else if ($user = $DB->get_record('user', array('id' => $message->userid
), user_picture
::fields())) {
862 $users[$message->userid
] = $user;
866 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chatlastrow);
871 * @param object $message message to be displayed.
872 * @param mixed $chatuser user chat data
873 * @param object $currentuser current user for whom the message should be displayed.
874 * @param int $groupingid course module grouping id
875 * @param string $theme name of the chat theme.
876 * @return bool|string Returns HTML or false
878 function chat_format_message_theme ($message, $chatuser, $currentuser, $groupingid, $theme = 'bubble') {
879 global $CFG, $USER, $OUTPUT, $COURSE, $DB, $PAGE;
880 require_once($CFG->dirroot
.'/mod/chat/locallib.php');
882 static $users; // Cache user lookups.
884 $result = new stdClass();
886 if (file_exists($CFG->dirroot
. '/mod/chat/gui_ajax/theme/'.$theme.'/config.php')) {
887 include($CFG->dirroot
. '/mod/chat/gui_ajax/theme/'.$theme.'/config.php');
890 if (isset($users[$message->userid
])) {
891 $sender = $users[$message->userid
];
892 } else if ($sender = $DB->get_record('user', array('id' => $message->userid
), user_picture
::fields())) {
893 $users[$message->userid
] = $sender;
898 $USER->timezone
= 99;
899 $tz = get_user_timezone($currentuser->timezone
);
900 $USER->timezone
= $tz;
902 if (empty($chatuser->course
)) {
903 $courseid = $COURSE->id
;
905 $courseid = $chatuser->course
;
908 $message->strtime
= userdate($message->timestamp
, get_string('strftimemessage', 'chat'), $tz);
909 $message->picture
= $OUTPUT->user_picture($sender, array('courseid' => $courseid));
911 $message->picture
= "<a target='_blank'".
912 " href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture</a>";
914 // Start processing the message.
915 if (!empty($message->system
)) {
916 $result->type
= 'system';
918 $senderprofile = $CFG->wwwroot
.'/user/view.php?id='.$sender->id
.'&course='.$courseid;
919 $event = get_string('message'.$message->message
, 'chat', fullname($sender));
920 $eventmessage = new event_message($senderprofile, fullname($sender), $message->strtime
, $event, $theme);
922 $output = $PAGE->get_renderer('mod_chat');
923 $result->html
= $output->render($eventmessage);
928 // It's not a system event.
929 $text = trim($message->message
);
931 // Parse the text to clean and filter it.
932 $options = new stdClass();
933 $options->para
= false;
934 $text = format_text($text, FORMAT_MOODLE
, $options, $courseid);
936 // And now check for special cases.
938 $outtime = $message->strtime
;
940 // Initialise variables.
942 $patternto = '#^\s*To\s([^:]+):(.*)#';
944 if (substr($text, 0, 5) == 'beep ') {
947 $result->type
= 'beep';
948 $beepwho = trim(substr($text, 5));
950 if ($beepwho == 'all') { // Everyone.
951 $outmain = get_string('messagebeepseveryone', 'chat', fullname($sender));
952 } else if ($beepwho == $currentuser->id
) { // Current user.
953 $outmain = get_string('messagebeepsyou', 'chat', fullname($sender));
954 } else if ($sender->id
== $currentuser->id
) { // Something is not caught?
955 // Allow beep for a active chat user only, else user can beep anyone and get fullname.
956 if (!empty($chatuser) && is_numeric($beepwho)) {
957 $chatusers = chat_get_users($chatuser->chatid
, $chatuser->groupid
, $groupingid);
958 if (array_key_exists($beepwho, $chatusers)) {
959 $outmain = get_string('messageyoubeep', 'chat', fullname($chatusers[$beepwho]));
961 $outmain = get_string('messageyoubeep', 'chat', $beepwho);
964 $outmain = get_string('messageyoubeep', 'chat', $beepwho);
967 } else if (substr($text, 0, 1) == '/') { // It's a user command.
969 $result->type
= 'command';
970 $pattern = '#(^\/)(\w+).*#';
971 preg_match($pattern, $text, $matches);
972 $command = isset($matches[2]) ?
$matches[2] : false;
973 // Support some IRC commands.
976 $outmain = '*** <b>'.$sender->firstname
.' '.substr($text, 4).'</b>';
979 // Error, we set special back to false to use the classic message output.
983 } else if (preg_match($patternto, $text)) {
985 $result->type
= 'dialogue';
987 preg_match($patternto, $text, $matches);
988 if (isset($matches[1]) && isset($matches[2])) {
989 $outmain = $sender->firstname
.' <b>'.get_string('saidto', 'chat').'</b> <i>'.$matches[1].'</i>: '.$matches[2];
991 // Error, we set special back to false to use the classic message output.
1000 $result->text
= strip_tags($outtime.': '.$outmain);
1002 $mymessageclass = '';
1003 if ($sender->id
== $USER->id
) {
1004 $mymessageclass = 'chat-message-mymessage';
1007 $senderprofile = $CFG->wwwroot
.'/user/view.php?id='.$sender->id
.'&course='.$courseid;
1008 $usermessage = new user_message($senderprofile, fullname($sender), $message->picture
,
1009 $mymessageclass, $outtime, $outmain, $theme);
1011 $output = $PAGE->get_renderer('mod_chat');
1012 $result->html
= $output->render($usermessage);
1014 // When user beeps other user, then don't show any timestamp to other users in chat.
1015 if (('' === $outmain) && $special) {
1023 * @global object $DB
1024 * @global object $CFG
1025 * @global object $COURSE
1026 * @global object $OUTPUT
1027 * @param object $users
1028 * @param object $course
1029 * @return array return formatted user list
1031 function chat_format_userlist($users, $course) {
1032 global $CFG, $DB, $COURSE, $OUTPUT;
1034 foreach ($users as $user) {
1036 $item['name'] = fullname($user);
1037 $item['url'] = $CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$course->id
;
1038 $item['picture'] = $OUTPUT->user_picture($user);
1039 $item['id'] = $user->id
;
1046 * Print json format error
1047 * @param string $level
1048 * @param string $msg
1050 function chat_print_error($level, $msg) {
1051 header('Content-Length: ' . ob_get_length() );
1052 $error = new stdClass();
1053 $error->level
= $level;
1055 $response['error'] = $error;
1056 echo json_encode($response);
1062 * List the actions that correspond to a view of this module.
1063 * This is used by the participation report.
1065 * Note: This is not used by new logging system. Event with
1066 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
1067 * be considered as view action.
1071 function chat_get_view_actions() {
1072 return array('view', 'view all', 'report');
1076 * List the actions that correspond to a post of this module.
1077 * This is used by the participation report.
1079 * Note: This is not used by new logging system. Event with
1080 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
1081 * will be considered as post action.
1085 function chat_get_post_actions() {
1086 return array('talk');
1092 * @param array $courses
1093 * @param array $htmlarray Passed by reference
1095 function chat_print_overview($courses, &$htmlarray) {
1098 if (empty($courses) ||
!is_array($courses) ||
count($courses) == 0) {
1102 if (!$chats = get_all_instances_in_courses('chat', $courses)) {
1106 $strchat = get_string('modulename', 'chat');
1107 $strnextsession = get_string('nextsession', 'chat');
1109 foreach ($chats as $chat) {
1110 if ($chat->chattime
and $chat->schedule
) { // A chat is scheduled.
1111 $str = '<div class="chat overview"><div class="name">'.
1112 $strchat.': <a '.($chat->visible ?
'' : ' class="dimmed"').
1113 ' href="'.$CFG->wwwroot
.'/mod/chat/view.php?id='.$chat->coursemodule
.'">'.
1114 $chat->name
.'</a></div>';
1115 $str .= '<div class="info">'.$strnextsession.': '.userdate($chat->chattime
).'</div></div>';
1117 if (empty($htmlarray[$chat->course
]['chat'])) {
1118 $htmlarray[$chat->course
]['chat'] = $str;
1120 $htmlarray[$chat->course
]['chat'] .= $str;
1128 * Implementation of the function for printing the form elements that control
1129 * whether the course reset functionality affects the chat.
1131 * @param object $mform form passed by reference
1133 function chat_reset_course_form_definition(&$mform) {
1134 $mform->addElement('header', 'chatheader', get_string('modulenameplural', 'chat'));
1135 $mform->addElement('advcheckbox', 'reset_chat', get_string('removemessages', 'chat'));
1139 * Course reset form defaults.
1141 * @param object $course
1144 function chat_reset_course_form_defaults($course) {
1145 return array('reset_chat' => 1);
1149 * Actual implementation of the reset course functionality, delete all the
1150 * chat messages for course $data->courseid.
1154 * @param object $data the data submitted from the reset course.
1155 * @return array status array
1157 function chat_reset_userdata($data) {
1160 $componentstr = get_string('modulenameplural', 'chat');
1163 if (!empty($data->reset_chat
)) {
1164 $chatessql = "SELECT ch.id
1167 $params = array($data->courseid
);
1169 $DB->delete_records_select('chat_messages', "chatid IN ($chatessql)", $params);
1170 $DB->delete_records_select('chat_messages_current', "chatid IN ($chatessql)", $params);
1171 $DB->delete_records_select('chat_users', "chatid IN ($chatessql)", $params);
1172 $status[] = array('component' => $componentstr, 'item' => get_string('removemessages', 'chat'), 'error' => false);
1175 // Updating dates - shift may be negative too.
1176 if ($data->timeshift
) {
1177 shift_course_mod_dates('chat', array('chattime'), $data->timeshift
, $data->courseid
);
1178 $status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
1185 * Returns all other caps used in module
1189 function chat_get_extra_capabilities() {
1190 return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');
1195 * @param string $feature FEATURE_xx constant for requested feature
1196 * @return mixed True if module supports feature, null if doesn't know
1198 function chat_supports($feature) {
1200 case FEATURE_GROUPS
:
1202 case FEATURE_GROUPINGS
:
1204 case FEATURE_MOD_INTRO
:
1206 case FEATURE_BACKUP_MOODLE2
:
1208 case FEATURE_COMPLETION_TRACKS_VIEWS
:
1210 case FEATURE_GRADE_HAS_GRADE
:
1212 case FEATURE_GRADE_OUTCOMES
:
1214 case FEATURE_SHOW_DESCRIPTION
:
1221 function chat_extend_navigation($navigation, $course, $module, $cm) {
1224 $currentgroup = groups_get_activity_group($cm, true);
1226 if (has_capability('mod/chat:chat', context_module
::instance($cm->id
))) {
1227 $strenterchat = get_string('enterchat', 'chat');
1229 $target = $CFG->wwwroot
.'/mod/chat/';
1230 $params = array('id' => $cm->instance
);
1232 if ($currentgroup) {
1233 $params['groupid'] = $currentgroup;
1238 $url = new moodle_url($target.'gui_'.$CFG->chat_method
.'/index.php', $params);
1239 $action = new popup_action('click', $url, 'chat'.$course->id
.$cm->instance
.$currentgroup,
1240 array('height' => 500, 'width' => 700));
1241 $links[] = new action_link($url, $strenterchat, $action);
1243 $url = new moodle_url($target.'gui_basic/index.php', $params);
1244 $action = new popup_action('click', $url, 'chat'.$course->id
.$cm->instance
.$currentgroup,
1245 array('height' => 500, 'width' => 700));
1246 $links[] = new action_link($url, get_string('noframesjs', 'message'), $action);
1248 foreach ($links as $link) {
1249 $navigation->add($link->text
, $link, navigation_node
::TYPE_SETTING
, null , null, new pix_icon('i/group' , ''));
1253 $chatusers = chat_get_users($cm->instance
, $currentgroup, $cm->groupingid
);
1254 if (is_array($chatusers) && count($chatusers) > 0) {
1255 $users = $navigation->add(get_string('currentusers', 'chat'));
1256 foreach ($chatusers as $chatuser) {
1257 $userlink = new moodle_url('/user/view.php', array('id' => $chatuser->id
, 'course' => $course->id
));
1258 $users->add(fullname($chatuser).' '.format_time(time() - $chatuser->lastmessageping
),
1259 $userlink, navigation_node
::TYPE_USER
, null, null, new pix_icon('i/user', ''));
1265 * Adds module specific settings to the settings block
1267 * @param settings_navigation $settings The settings navigation object
1268 * @param navigation_node $chatnode The node to add module settings to
1270 function chat_extend_settings_navigation(settings_navigation
$settings, navigation_node
$chatnode) {
1271 global $DB, $PAGE, $USER;
1272 $chat = $DB->get_record("chat", array("id" => $PAGE->cm
->instance
));
1274 if ($chat->chattime
&& $chat->schedule
) {
1275 $nextsessionnode = $chatnode->add(get_string('nextsession', 'chat').
1276 ': '.userdate($chat->chattime
).
1277 ' ('.usertimezone($USER->timezone
));
1278 $nextsessionnode->add_class('note');
1281 $currentgroup = groups_get_activity_group($PAGE->cm
, true);
1282 if ($currentgroup) {
1283 $groupselect = " AND groupid = '$currentgroup'";
1288 if ($chat->studentlogs ||
has_capability('mod/chat:readlog', $PAGE->cm
->context
)) {
1289 if ($DB->get_records_select('chat_messages', "chatid = ? $groupselect", array($chat->id
))) {
1290 $chatnode->add(get_string('viewreport', 'chat'), new moodle_url('/mod/chat/report.php', array('id' => $PAGE->cm
->id
)));
1296 * user logout event handler
1298 * @param \core\event\user_loggedout $event The event.
1301 function chat_user_logout(\core\event\user_loggedout
$event) {
1303 $DB->delete_records('chat_users', array('userid' => $event->objectid
));
1307 * Return a list of page types
1308 * @param string $pagetype current page type
1309 * @param stdClass $parentcontext Block's parent context
1310 * @param stdClass $currentcontext Current context of block
1312 function chat_page_type_list($pagetype, $parentcontext, $currentcontext) {
1313 $modulepagetype = array('mod-chat-*' => get_string('page-mod-chat-x', 'chat'));
1314 return $modulepagetype;