Skip to content

Commit

Permalink
Preliminary support for groups in Chat.
Browse files Browse the repository at this point in the history
It's not well tested yet - I'm hoping to do some testing on moodle.org
with some real people and get this refined enough for the Beta release
later today.
  • Loading branch information
moodler committed Feb 24, 2004
1 parent 4c17698 commit 84a2fdd
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 60 deletions.
5 changes: 5 additions & 0 deletions mod/chat/db/mysql.php
Expand Up @@ -23,6 +23,11 @@ function chat_upgrade($oldversion) {
table_column("chat", "", "schedule", "integer", "4", "", "0", "not null", "studentlogs");
}

if ($oldversion < 2004022300) {
table_column("chat_messages", "", "groupid", "integer", "10", "unsigned", "0", "not null", "userid");
table_column("chat_users", "", "groupid", "integer", "10", "unsigned", "0", "not null", "userid");
}

return true;
}

Expand Down
2 changes: 2 additions & 0 deletions mod/chat/db/mysql.sql
Expand Up @@ -24,6 +24,7 @@ CREATE TABLE `prefix_chat_messages` (
`id` int(10) unsigned NOT NULL auto_increment,
`chatid` int(10) NOT NULL default '0',
`userid` int(10) NOT NULL default '0',
`groupid` int(10) NOT NULL default '0',
`system` int(1) unsigned NOT NULL default '0',
`message` text NOT NULL,
`timestamp` int(10) unsigned NOT NULL default '0',
Expand All @@ -40,6 +41,7 @@ CREATE TABLE `prefix_chat_users` (
`id` int(10) unsigned NOT NULL auto_increment,
`chatid` int(11) NOT NULL default '0',
`userid` int(11) NOT NULL default '0',
`groupid` int(11) NOT NULL default '0',
`version` varchar(16) NOT NULL default '',
`ip` varchar(15) NOT NULL default '',
`firstping` int(10) unsigned NOT NULL default '0',
Expand Down
6 changes: 6 additions & 0 deletions mod/chat/db/postgres7.php
Expand Up @@ -6,6 +6,12 @@ function chat_upgrade($oldversion) {

global $CFG;

if ($oldversion < 2004022300) {
table_column("chat_messages", "", "groupid", "integer", "10", "unsigned", "0", "not null", "userid");
table_column("chat_users", "", "groupid", "integer", "10", "unsigned", "0", "not null", "userid");
}


return true;
}

Expand Down
2 changes: 2 additions & 0 deletions mod/chat/db/postgres7.sql
Expand Up @@ -24,6 +24,7 @@ CREATE TABLE prefix_chat_messages (
id SERIAL,
chatid integer NOT NULL default '0',
userid integer NOT NULL default '0',
groupid integer NOT NULL default '0',
system integer NOT NULL default '0',
message text NOT NULL,
timestamp integer NOT NULL default '0',
Expand All @@ -39,6 +40,7 @@ CREATE TABLE prefix_chat_users (
id SERIAL,
chatid integer NOT NULL default '0',
userid integer NOT NULL default '0',
groupid integer NOT NULL default '0',
version varchar(16) NOT NULL default '',
ip varchar(15) NOT NULL default '',
firstping integer NOT NULL default '0',
Expand Down
4 changes: 3 additions & 1 deletion mod/chat/gui_header_js/chatinput.php
Expand Up @@ -4,6 +4,7 @@
require("../lib.php");

require_variable($chat_sid);
optional_variable($groupid);

if (!$chatuser = get_record("chat_users", "sid", $chat_sid)) {
echo "Not logged in!";
Expand Down Expand Up @@ -51,7 +52,8 @@ function empty_field_and_submit() {

<form action="../insert.php" method="GET" target="empty" name="fdummy"
OnSubmit="return empty_field_and_submit()">
<input type="hidden" name="chat_sid" value="<?php echo $chat_sid; ?>">
<input type="hidden" name="chat_sid" value="<?php echo $chat_sid ?>">
<input type="hidden" name="groupid" value="<?php echo $groupid ?>">
<input type="hidden" name="chat_version" value="header_js">
<input type="hidden" name="chat_message">
</form>
Expand Down
70 changes: 46 additions & 24 deletions mod/chat/gui_header_js/index.php
@@ -1,49 +1,71 @@
<?php

require_once('../../../config.php');
require_once('../lib.php');
require_once('../../../config.php');
require_once('../lib.php');

require_variable($id);
require_variable($id);
optional_variable($groupid);

if (!$chat = get_record("chat", "id", $id)) {
error("Could not find that chat room!");
}
if (!$chat = get_record("chat", "id", $id)) {
error("Could not find that chat room!");
}

if (!$course = get_record("course", "id", $chat->course)) {
error("Could not find the course this belongs to!");
}

require_login($course->id);

if (isguest()) {
error("Guest does not have access to chat rooms");
}

if (!$course = get_record("course", "id", $chat->course)) {
error("Could not find the course this belongs to!");
}
/// Check to see if groups are being used here
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
if ($currentgroup = get_and_set_current_group($course->id, $groupmode, $groupid)) {
if (!$group = get_record('groups', 'id', $currentgroup)) {
error("That group (id $currentgroup) doesn't exist!");
}
$groupname = ': '.$group->name;
} else {
$groupname = ': '.get_string('allparticipants');
}
} else {
$currentgroup = false;
$groupname = '';
}

require_login($course->id);
if (!$chat_sid = chat_login_user($chat->id, "header_js")) {
error("Could not log in to chat room!!");
}

if (isguest()) {
error("Guest does not have access to chat rooms");
}

if (!$chat_sid = chat_login_user($chat->id, "header_js")) {
error("Could not log in to chat room!!");
}

$strchat = get_string("modulename", "chat");
if ($currentgroup !== false) {
$params = "chat_enter=true&chat_sid=$chat_sid&groupid=$currentgroup";
} else {
$params = "chat_enter=true&chat_sid=$chat_sid";
}

$strchat = get_string("modulename", "chat");


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>
<?php echo "$strchat: $course->shortname: $chat->name" ?>
<?php echo "$strchat: $course->shortname: $chat->name$groupname" ?>
</title>
</head>
<frameset cols="*,200" border="5" framespacing="no" frameborder="yes" marginwidth="2" marginheight="1">
<frameset rows="0,0,*,40" border="0" framespacing="no" frameborder="no" marginwidth="2" marginheight="1">
<frame src="../empty.php" NAME="empty" scrolling="no" marginwidth="0" marginheight="0">
<frame src="jsupdate.php?chat_sid=<?php echo $chat_sid; ?>&chat_enter=true" scrolling="no" marginwidth="0" marginheight="0">
<frame src="jsupdate.php?<?php echo $params ?>" scrolling="no" marginwidth="0" marginheight="0">
<frame src="chatmsg.php" NAME="msg" scrolling="auto" marginwidth="2" marginheight="1">
<frame src="chatinput.php?chat_sid=<?php echo $chat_sid; ?>" name="input" scrolling="no" marginwidth="2" marginheight="1">
<frame src="chatinput.php?<?php echo $params ?>" name="input" scrolling="no" marginwidth="2" marginheight="1">
</frameset>
<frame src="../users.php?chat_sid=<?php echo $chat_sid; ?>&chat_enter=true" name="users" scrolling="auto" marginwidth="5" marginheight="5">
<frame src="../users.php?<?php echo $params ?>" name="users" scrolling="auto" marginwidth="5" marginheight="5">
</frameset>
<noframes>
Sorry, this version of ARSC needs a browser that understands framesets. We have a Lynx friendly version too.
Sorry, this version of Moodle Chat needs a browser that handles frames.
</noframes>
</html>
12 changes: 7 additions & 5 deletions mod/chat/gui_header_js/jsupdate.php
Expand Up @@ -3,6 +3,9 @@
require("../../../config.php");
require("../lib.php");

$groupid = empty($_GET['groupid']) ? 0 : $_GET['groupid'];
$groupselect = $groupid ? " AND (groupid='$groupid' OR groupid='0') " : "";

if (!$chatuser = get_record("chat_users", "sid", $chat_sid)) {
echo "Not logged in!";
die;
Expand All @@ -14,8 +17,7 @@

require_login($chat->course);


if ($message = chat_get_latest_message($chatuser->chatid)) {
if ($message = chat_get_latest_message($chatuser->chatid, $groupid)) {
$chat_newlasttime = $message->timestamp;
} else {
$chat_newlasttime = 0;
Expand All @@ -31,7 +33,7 @@
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: text/html");
header("Refresh: $CFG->chat_refresh_room; URL=jsupdate.php?chat_sid=".$chat_sid."&chat_lasttime=".$chat_newlasttime);
header("Refresh: $CFG->chat_refresh_room; URL=jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&groupid=$groupid");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
Expand All @@ -44,8 +46,8 @@

if ($chat_lasttime) {
if ($messages = get_records_select("chat_messages",
"chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime'",
"timestamp ASC")) {
"chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect",
"timestamp ASC")) {
foreach ($messages as $message) {
$formatmessage = chat_format_message($message, $chat->course);
if ($formatmessage->beep) {
Expand Down
12 changes: 10 additions & 2 deletions mod/chat/insert.php
Expand Up @@ -6,6 +6,7 @@
require_variable($chat_sid);
require_variable($chat_version);
require_variable($chat_message);
optional_variable($groupid);

if (!$chatuser = get_record("chat_users", "sid", $chat_sid)) {
echo "Not logged in!";
Expand All @@ -17,6 +18,12 @@
}

require_login($chat->course);

if ($groupid) {
if (!isteacheredit($course->id) and !ismember($groupid)) {
error("You can't chat here!");
}
}

/// Clean up the message

Expand All @@ -28,6 +35,7 @@

$message->chatid = $chatuser->chatid;
$message->userid = $chatuser->userid;
$message->groupid = $groupid;
$message->message = $chat_message;
$message->timestamp = time();

Expand All @@ -42,10 +50,10 @@
/// Go back to the other page

if ($chat_version == "header" OR $chat_version == "box") {
redirect("../gui_$chat_version/chatinput.php?chat_sid=$chat_sid");
redirect("../gui_$chat_version/chatinput.php?chat_sid=$chat_sid&groupid=$groupid");

} else if ($chat_version == "text") {
redirect("../gui_$chat_version/index.php?chat_sid=$chat_sid&chat_lastid=$chat_lastid");
redirect("../gui_$chat_version/index.php?chat_sid=$chat_sid&chat_lastid=$chat_lastid&groupid=$groupid");

} else {
redirect("empty.php");
Expand Down
33 changes: 26 additions & 7 deletions mod/chat/lib.php
Expand Up @@ -165,18 +165,24 @@ function chat_cron () {
return true;
}

function chat_get_participants($chatid) {
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.*
FROM {$CFG->prefix}user u,
{$CFG->prefix}chat_messages c
WHERE c.chatid = '$chatid' and
u.id = c.userid");
WHERE c.chatid = '$chatid' $groupselect
AND u.id = c.userid");

//Return students array (it contains an array of unique users)
return ($students);
Expand All @@ -185,30 +191,42 @@ function chat_get_participants($chatid) {
//////////////////////////////////////////////////////////////////////
/// Functions that require some SQL

function chat_get_users($chatid) {
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 u.id, u.firstname, u.lastname, u.picture, c.lastmessageping
FROM {$CFG->prefix}chat_users c,
{$CFG->prefix}user u
WHERE c.chatid = '$chatid'
AND u.id = c.userid
AND u.id = c.userid $groupselect
GROUP BY u.id
ORDER BY c.firstping ASC");
}

function chat_get_latest_message($chatid) {
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'
WHERE chatid = '$chatid' $groupselect
ORDER BY timestamp DESC LIMIT 1")) {
return false;
}
Expand Down Expand Up @@ -251,6 +269,7 @@ function chat_delete_old_users() {
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();
Expand Down

0 comments on commit 84a2fdd

Please sign in to comment.