libdir.'/pagelib.php'); if (!isset($CFG->chat_refresh_room)) { set_config("chat_refresh_room", 5); } if (!isset($CFG->chat_refresh_userlist)) { set_config("chat_refresh_userlist", 10); } if (!isset($CFG->chat_old_ping)) { set_config("chat_old_ping", 35); } if (!isset($CFG->chat_method)) { set_config("chat_method", "header_js"); } if (!isset($CFG->chat_serverhost)) { set_config("chat_serverhost", $_SERVER['HTTP_HOST']); } if (!isset($CFG->chat_serverip)) { set_config("chat_serverip", $_SERVER['SERVER_ADDR']); } if (!isset($CFG->chat_serverport)) { set_config("chat_serverport", 9111); } if (!isset($CFG->chat_servermax)) { set_config("chat_servermax", 100); } // The HTML head for the message window to start with ( is used to get some browsers starting with output $CHAT_HTMLHEAD = "
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; // The HTML head for the message window to start with (with js scrolling) $CHAT_HTMLHEAD_JS = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; // The HTML code for standard empty pages (e.g. if a user was kicked out) $CHAT_HTMLHEAD_OUT = "wwwroot/mod/chat/view.php?c=$chat->id\">$chat->name
";
}
$current = $chatuser->chatid;
}
$fullname = fullname($chatuser);
echo " - $fullname
";
}
echo "
";
return true;
}
function chat_cron () {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
chat_update_chat_times();
chat_delete_old_users();
/// Delete old messages
if ($chats = get_records("chat")) {
foreach ($chats as $chat) {
if ($chat->keepdays) {
$timeold = time() - ($chat->keepdays * 24 * 3600);
delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
}
}
}
return true;
}
function chat_get_participants($chatid, $groupid=0) {
//Returns the users with data in one chat
//(users with records in chat_messages, students)
global $CFG;
if ($groupid) {
$groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
} else {
$groupselect = "";
}
//Get students
$students = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}chat_messages c
WHERE c.chatid = '$chatid' $groupselect
AND u.id = c.userid");
//Return students array (it contains an array of unique users)
return ($students);
}
function chat_refresh_events($courseid = 0) {
// This standard function will check all instances of this module
// and make sure there are up-to-date events created for each of them.
// If courseid = 0, then every chat event in the site is checked, else
// only chat events belonging to the course specified are checked.
// This function is used, in its new format, by restore_refresh_events()
if ($courseid) {
if (! $chats = get_records("chat", "course", $courseid)) {
return true;
}
} else {
if (! $chats = get_records("chat")) {
return true;
}
}
$moduleid = get_field('modules', 'id', 'name', 'chat');
foreach ($chats as $chat) {
$event = NULL;
$event->name = addslashes($chat->name);
$event->description = addslashes($chat->intro);
$event->timestart = $chat->chattime;
if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
update_event($event);
} else {
$event->courseid = $chat->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'chat';
$event->instance = $chat->id;
$event->eventtype = $chat->schedule;
$event->timeduration = 0;
$event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
add_event($event);
}
}
return true;
}
function chat_force_language($lang) {
/// This function prepares moodle to operate in given language
/// usable when $nomoodlecookie = true;
/// BEWARE: there must be no $course, $USER or $SESSION
global $CFG;
if(!empty($CFG->courselang)) {
unset($CFG->courselang);
}
if(!empty($CFG->locale)) {
unset($CFG->locale);
}
$CFG->lang = $lang;
moodle_setlocale();
}
//////////////////////////////////////////////////////////////////////
/// Functions that require some SQL
function chat_get_users($chatid, $groupid=0) {
global $CFG;
if ($groupid) {
$groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
} else {
$groupselect = "";
}
return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
FROM {$CFG->prefix}chat_users c,
{$CFG->prefix}user u
WHERE c.chatid = '$chatid'
AND u.id = c.userid $groupselect
ORDER BY c.firstping ASC");
}
function chat_get_latest_message($chatid, $groupid=0) {
/// Efficient way to extract just the latest message
/// Uses ADOdb directly instead of get_record_sql()
/// because the LIMIT command causes problems with
/// the developer debugging in there.
global $db, $CFG;
if ($groupid) {
$groupselect = " AND (groupid='$groupid' OR groupid='0')";
} else {
$groupselect = "";
}
if (!$rs = $db->Execute("SELECT *
FROM {$CFG->prefix}chat_messages
WHERE chatid = '$chatid' $groupselect
ORDER BY timestamp DESC LIMIT 1")) {
return false;
}
if ($rs->RecordCount() == 1) {
return (object)$rs->fields;
} else {
return false; // Found no records
}
}
//////////////////////////////////////////////////////////////////////
// login if not already logged in
function chat_login_user($chatid, $version, $groupid, $course) {
global $USER;
if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
$chatuser->version = $version;
$chatuser->ip = $USER->lastIP;
$chatuser->lastping = time();
$chatuser->lang = current_language();
if (($chatuser->course != $course->id)
or ($chatuser->userid != $USER->id)) {
return false;
}
if (!update_record('chat_users', $chatuser)) {
return false;
}
} else {
$chatuser->chatid = $chatid;
$chatuser->userid = $USER->id;
$chatuser->groupid = $groupid;
$chatuser->version = $version;
$chatuser->ip = $USER->lastIP;
$chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
$chatuser->sid = random_string(32);
$chatuser->course = $course->id; //caching - needed for current_language too
$chatuser->lang = current_language(); //caching - to resource intensive to find out later
if (!insert_record('chat_users', $chatuser)) {
return false;
}
if ($version == 'sockets') {
// do not send 'enter' message, chatd will do it
} else {
$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $groupid;
$message->message = 'enter';
$message->system = 1;
$message->timestamp = time();
if (!insert_record('chat_messages', $message)) {
error('Could not insert a chat message!');
}
}
}
return $chatuser->sid;
}
function chat_delete_old_users() {
// Delete the old and in the way
global $CFG;
$timeold = time() - $CFG->chat_old_ping;
$query = "lastping < '$timeold'";
if ($oldusers = get_records_select('chat_users', $query) ) {
delete_records_select('chat_users', $query);
foreach ($oldusers as $olduser) {
$message->chatid = $olduser->chatid;
$message->userid = $olduser->userid;
$message->groupid = $olduser->groupid;
$message->message = 'exit';
$message->system = 1;
$message->timestamp = time();
if (!insert_record('chat_messages', $message)) {
error('Could not insert a chat message!');
}
}
}
}
function chat_update_chat_times($chatid=0) {
/// Updates chat records so that the next chat time is correct
$timenow = time();
if ($chatid) {
if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
return;
}
} else {
if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
return;
}
}
foreach ($chats as $chat) {
unset($chat->name);
unset($chat->intro);
switch ($chat->schedule) {
case 1: // Single event - turn off schedule and disable
$chat->chattime = 0;
$chat->schedule = 0;
break;
case 2: // Repeat daily
$chat->chattime += 24 * 3600;
break;
case 3: // Repeat weekly
$chat->chattime += 7 * 24 * 3600;
break;
}
update_record("chat", $chat);
$event = NULL; // Update calendar too
if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
$event->timestart = $chat->chattime;
update_event($event);
}
}
}
function chat_format_message_manually($message, $courseid, $sender, $currentuser) {
global $CFG;
$output = New stdClass;
$output->beep = false; // by default
$output->refreshusers = false; // by default
// Get some additional info
// But before that :-) let's override get_user_timezone() for this call... messy stuff...
$tz = ($currentuser->timezone == 99) ? $CFG->timezone : $currentuser->timezone;
$message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
$message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
if ($courseid) {
$message->picture = "wwwroot/user/view.php?id=$sender->id&course=$courseid\">$message->picture";
}
// Start processing the message
if(!empty($message->system)) {
// System event
$output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
$output->html = '
'.$message->picture.' | '; $output->html .= ''.$output->text.' |
$message->picture | "; $output->html .= "$outinfo"; if ($outmain) { $output->html .= ": $outmain"; } $output->html .= " |