<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>View participant groups</title>
<style type="text/css" media="screen">@import "convener.css";</style>
</head>
<body>
<div id="wrapper">
<div id="menu"><?php include('menu.php');?>
</div>
<div id="content">
<?php
// This manages participant groups: show groups, delete groups, show group members, remove group members and rename group.
// Adding members to groups is handled by addmembers.php.
require('../config.php');
require('../dbfunc.php');
$conveners_id = 1; // now for testing, set by authentication when it's ready
if ($_POST['removemembers']) { // check if "Remove members" was pressed
$id = escape_smart($_POST['g']);
if ($_POST['del']) foreach ($_POST['del'] as $value) {
mysql_query("DELETE FROM groups_have_participants WHERE participants_id = '$value' AND groups_id = '$id' LIMIT 1", $link)
OR die("Could not remove participant (id: $value) from group (id: $id):" . mysql_error());
}
}
if ($_GET['g']) { // group was selected with GET
$id = escape_smart($_GET['g']);
$result = mysql_query("SELECT name FROM groups WHERE groups_id = $id", $link);
$row = mysql_fetch_row($result);
echo '<h2>Members of "' . $row[0] . '"</h2>';
$result = mysql_query("SELECT * FROM groups_have_participants LEFT JOIN participants ON groups_have_participants.participants_id =
participants.participants_id WHERE groups_id = $id ORDER BY lastname, firstname", $link);
if (mysql_num_rows($result) == 0) {
echo 'This group has no members!';
} else {
echo '<form action="viewgroup.php?g=' . $id . '" method="post"><table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td><input type="checkbox" name="del[]" value="' . $row['participants_id'] . '" /></td>
<td class="leftalign">' . $row['lastname'] . ', ' . $row['firstname'] . '</td><td class="leftalign">' . $row['email'] . '</td></tr>';
}
echo '</table>';
}
echo '<p><input type="submit" name="removemembers" value="Remove selected" /><input type="hidden" name="g" value="' . $id . '" />
</p></form>
<form action="addmembers.php" method="post"><input type="submit" name="addmembers" value="Add members" />
<input type="hidden" name="g" value="' . $id . '" /></form>
<form action="#" method="post"><input type="submit" name="rename" value="Rename group" /></p></form>
<input type="hidden" name="g" value="' . $id . '" /></form>
<form action="viewgroup.php" method="post"><input type="submit" name="deletegroup" value="Delete group" />
<input type="hidden" name="g" value="' . $id . '" /></form>';
} elseif ($_POST['addgroup']) { // display form to add new group
echo '<h2>Create new participant group</h2><form action="viewgroup.php" method="post">
<p><input type="text" name="name" size="25" maxlenght="45" /> Name for the group</p>
<p><input type="checkbox" name="public" value="0" /> Public group (not yet implemented)</p>
<input type="submit" name="creategroup" value="Create group"></form>';
} elseif ($_POST['creategroup']) { // insert new group into database
$name = escape_smart($_POST['name']);
// --- check if group name already exists
$result = mysql_query("SELECT name FROM groups WHERE name = '$name' AND conveners_id = '$conveners_id'", $link);
if (mysql_num_rows($result) != 0) die ('<p>You already have participant group named "' . $name . '". Please select different name.</p>');
mysql_query("INSERT INTO groups (conveners_id, name, public) VALUES ('$conveners_id', '$name', '0')", $link)
OR die("Could not create participant group: " . mysql_error());
echo "<h2>Added participant group \"$name\"</h2><p>The participant group was succesfully created. You can now add members to it.</p>
<p><a href=\"viewgroup.php\">Back to group listing.</a></p>";
} elseif ($_POST['deletegroup']) {
$id = escape_smart($_POST['g']);
$result = mysql_query("SELECT name FROM groups WHERE groups_id = '$id'", $link);
$row = mysql_fetch_row($result);
// --- delete group from table groups + referenced data from groups_have_participants, do not touch table participants
mysql_query("DELETE FROM groups WHERE groups_id = '$id' LIMIT 1", $link) OR die(MYSQL_ERROR());
echo "<h2>Group \"$row[0]\" deleted!</h2><p>The group was deleted from the database. All the individual contacts are
still available to use for new groups.</p>";
} else { // display summary of the groups owned by this convener (+ public groups?)
$result = mysql_query("SELECT groups_id, name, public FROM groups WHERE conveners_id = $conveners_id", $link);
echo '<h2>Participant groups</h2>';
if (mysql_num_rows($result) == 0) {
echo '<p class="notice">No groups found!</p>';
} else {
echo '<p>You are the owner of following groups:</p><form action="viewgroup.php" method="post"><table><tr class="tblh">
<td>Name</td><td>Members</td><td>Public</td></tr>';
while ($row = mysql_fetch_assoc($result)) {
$result_members = mysql_query("SELECT COUNT(*) FROM groups_have_participants WHERE groups_id = '" . $row['groups_id']
. "'", $link);
$members = mysql_fetch_row($result_members);
($row['public'] == 0 ? $public = 'No' : $public = 'Yes');
echo '<tr><td><a href="viewgroup.php?g=' . $row['groups_id'] . '">' . $row['name'] . '</a></td><td>'
. $members[0] . '</td><td>' . $public . '</td></tr>';
}
}
echo '</table><form action="viewgroup.php" method="post"><p><input type="submit" name="addgroup" value="Add new group" /></p></form>';
}
?>
</div>
</div>
</body>
</html>