Location: PHPKode > projects > Web Task Manager with Email/SMS Voting > Calendar/cal_groups.php
<?php

include ('cal_header.inc.php');
include ("cal_utils.php") ;


calHeaderFunc();

function viewgroups()
{
echo "<br /><hleft>View subscribed groups</hleft><br />";

 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $userid=$row->user_id;


$query = "select * from groupusers inner join groups on groupusers.groupid = groups.groupid where groupusers.userid = ".$userid;
	
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);

	if($rows==0)
	{
		echo "<br>No groups have been subscribed to.<br>";
	}
	
	while ($row = mysql_fetch_object($result))
	{
		echo "<br>GroupName: ".$row->GroupName;
		echo " <a href=cal_groups.php?op=remove&amp;group=".$row->GroupID.">Remove</a>";
	}
	
	echo "<br><br>";

}

function remove()
{
 $group = $_GET['group'];

 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $userid=$row->user_id;
 
 $query = "select GroupName from groups where groupid = ".$group;
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $groupname = $row->GroupName;

echo "REMOVE GROUP";

$query = "delete from groupusers where userid = ".$userid." and groupid = ".$group;

mysql_query($query) or die('Error, delete query failed');

echo "<br>Group ".$groupname." has been removed";

}

function addgroup()
{
	echo "<br /><hleft>Subscribe to a group</hleft><br /><br />";
  
	$query = "select *, count(groups.groupid) as grpcount, groups.groupid as grpid from groups left join groupusers on groupusers.groupid = groups.groupid group by groups.groupid";
	
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);

	if($rows==0)
	{
		echo "<br>No groups are available.<br>";
	}
	else
	{
        $rows=0;
		echo "<table class=mes><tr><td width = 30% class=mes>Group Name</td>";
		echo "<td width = 60% class=mes>Description</td><td width = 10% class=mes># Users</td></tr>";
		while ($row = mysql_fetch_object($result))
		{
              $rows++;
                     $rows=$rows%2;
			echo "<tr><td class=mes".$rows."><a href = cal_groups.php?op=sub&amp;group=".$row->grpid.">".$row->GroupName."</a></td>";
			echo "<td  class=mes".$rows.">".$row->Description."</td>";
			//if usergroupid is null, it has joined on nothing and thus there are no users in the group (count will show as 1 in results, changed to 0 for display)
			if (!isset($row->UserGroupID))
				echo "<td  class=\"mes".$rows."\">0</td>";
			else
				echo "<td  class=mes".$rows."><a href = cal_groups.php?op=users&amp;group=".$row->grpid.">".$row->grpcount."</a></td></tr>";
		}
		echo "</table><br>";
	}
}

function subscribe()
{
	$group = $_GET['group'];

	$query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);
	$row = mysql_fetch_object($result);
	$userid=$row->user_id;
	
	$query = "select GroupName from groups where groupid = ".$group;
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);
	$row = mysql_fetch_object($result);
	$groupname = $row->GroupName;
	
	$query = "select * from groupusers where userid = ".$userid." and groupid = ".$group;
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);
	
	//check to see if user is in group attempting to be added to or not  if no results retuned, user is not in group and may be added
	if ($rows==0)
	{
		$query = "INSERT INTO groupusers (GroupID, UserID) VALUES (".$group.", ".$userid.")";
		
		echo "<br /><div class=headerc>user joined group ".$groupname."</div><br />";

		mysql_query($query) or die('Error, insert query failed');
		$query = "FLUSH PRIVILEGES";
		mysql_query($query) or die('Error, insert query failed');
	}
	else
	{
		echo "<br>Group has already been joined for this user";
	}
	
}


function creategroup()
{
	//for now, only public groups, to be extended to private groups with invitations, etc.
	echo "<div class=titlefont>CREATE A GROUP</div><br/><form name=evfrm action=cal_groups.php?op=add method=post><div class=normalfont>
			Group Name<br/><br>
			<input type=text name=groupname size=70 value='' ><br/>
			Group Description<br/>
			<textarea name=description cols=70 rows=7></textarea><br/>
			<br/><input type=submit value=\"Create Group\">
			&nbsp;&nbsp;<input type=button value=\"Cancel\" onclick='Javascript:window.history.go(-1);'>
			<br/><br><br></div>
			";
}


function add()
{
	$name = $_POST['groupname'];
	$desc = $_POST['description'];
	
	$query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
	$result = mysql_query($query);
	$rows = mysql_num_rows($result);
	$row = mysql_fetch_object($result);
	$userid=$row->user_id;
	
	echo "<br>Group Created:<br>Name: ".$name."<br>description: ".$desc;
	
	$query = "INSERT INTO groups (GroupName, TypeID, UserID, Description) VALUES ('".$name."', 0, ".$userid.", '".$desc."')";

mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
	
}

function viewusers()
{
	$group = $_GET['group'];
	
		$query = "select GroupName from groups where GroupID = ".$group;
	$result = mysql_query($query);
	$row = mysql_fetch_object($result);
	
	echo "<br>Users of Group: ".$row->GroupName."<br>";
	
	$query = "select * from groupusers inner join calendar_users on groupusers.userid = calendar_users.user_id where groupid = ".$group;
	$result = mysql_query($query);
	
	while ($row = mysql_fetch_object($result))
	{
		echo "<br>".$row->username;
	}
	
	echo "<br><br>";
	
}


// message functions
switch ($op){
    
    case "viewgroups":{
	 viewgroups();
    break;
    }

	case "addgroup":{
	 addgroup();
    break;
    }
	
	case "creategroup":{
	 creategroup();
    break;
    }
	
	case "remove":{
	 remove();
    break;
    }
	
	case "add":{
	 add();
	 break;
	 }
	 
	case "sub":{
	 subscribe();
	 break;
	 }
	 
	case "users":{
	 viewusers();
	 break;
	 }
	 
	
    // default: 
    default:{
	viewgroups();
      break;
	}
}

include ('cal_functions.inc.php');
include ('cal_footer.inc.php');

?>
Return current item: Web Task Manager with Email/SMS Voting