Here is a simple Snippet that allows web users to join or leave web groups.
In the configuration section you must specify which web groups are allowed to be joined/left.
Only basic functionality is provided; you are welcome to expand it!
//
// WebGroupJoin v0.1 for MODx
// (Tested on MODx 0.9.1)
//
// By Luca Allulli, February 2006
// License: GNU GPL
//
//Begin configuration
//Web groups that web users are allowed to join/leave
$webUserGroups=array("Teachers", "Students");
//Message strings
$submitButton="Subscribe";
$successful="Successfully subscribed.";
//End configuration
$output="";
$uid=$modx->getLoginUserID();
if(!isset($uid)) return "";
$sql = 'SELECT webgroup FROM'.$modx->getFullTableName("web_groups").' WHERE webuser='.$uid.';';
$joinedGroups=$modx->db->getColumn("webgroup", $sql);
$sql = "SELECT id, name FROM".$modx->getFullTableName("webgroup_names")." WHERE name IN ('".implode("','",$webUserGroups)."');";
$rs=$modx->db->query($sql);
while($row=$modx->fetchRow($rs)) {
$id=$row['id'];
$webGroupName[$id]=$row['name'];
$webGroupJoined[$id]=in_array($id, $joinedGroups);
}
if(!isset($webGroupName)) return "";
if(!isset($_POST['posted'])) {
//Ask user for input
$output.='<form action="index.php?id='.$modx->documentIdentifier.'" method="post">';
$output.='<input type="hidden" name="posted" value="yes" />';
foreach($webGroupName as $id => $name) {
$output.='<input type="checkbox" name="group'.$id.'" ';
if($webGroupJoined[$id])
$output.='checked="checked" ';
$output.='" />'.$webGroupName[$id].'<br />';
}
$output.='<input type="submit" value="'.$submitButton.'" /></form>'."\n";
} else {
//Process input
foreach($webGroupJoined as $id => $joined) {
if(isset($_POST['group'.$id]) && !$joined) {
$sql = 'INSERT INTO '.$modx->getFullTableName("web_groups").' (id, webgroup, webuser) VALUES (NULL, '.$id.', '.$uid.');';
$rs=$modx->db->query($sql);
}
if(!isset($_POST['group'.$id]) && $joined) {
$sql = 'DELETE FROM '.$modx->getFullTableName("web_groups").' WHERE webgroup='.$id.' AND webuser='.$uid.';';
$rs=$modx->db->query($sql);
}
}
$output.=$successful;
}
return $output;