Location: PHPKode > projects > CMS Admin > www/modules/managegroups.php
<?php
$module = new ManageGroups();
$mod = 'managegroups';
require_once dirname(__FILE__).'/../classes/course.php';
require_once dirname(__FILE__).'/../classes/group.php';
require_once dirname(__FILE__).'/../include/login.php';
require_once dirname(__FILE__).'/../layout/layout.php';

class ManageGroups
{
	private $course;

	function main(User $user, Permissions $perm, $course)
	{
		$this->course = $course;
		$c = new Course($course);

		if($user->userLevel <= $perm->permissions['modifyGroups'] && $c->isUserInCourse($user->userID, $course) ||
			$user->userLevel == ADMIN) {
			Layout::header('Manage Groups');
			Layout::sidebar($user, $this->course);
			echo '<div id="main">';
			if(isset($_GET['action'])) {
				switch($_GET['action']) {
				case 'add':
					$this->addGroup();
					break;
				case 'modify':
					$this->modifyGroup();
					break;
				case 'delete':
					$this->deleteGroup();
					break;
				case 'studentupdate':
					$this->studentUpdate();
					break;
				}
			} else if(isset($_GET['add'])) {
				$this->printForm(new Group(0), 'add');
			} else if(isset($_GET['group'])) {
				$this->modifyGroupForm();
			} else {
				$this->menu();
			}
			echo "</div>\n";
			Layout::footer();
		} else {
			echo 'insufficient privledges';
		}
	}


	public function modifyGroupForm()
	{
		$g = new Group($_GET['group']);

		echo '<div id="linklist" class="linklist"><h5><a href="managegroups.php?c='.$this->course.'&amp;action=delete&amp;group=' . $g->groupID . '">Delete This Group</a></h5></div>'."\n";
		$action = 'modify&amp;group=' . $g->groupID;
		$this->printForm($g, $action);

		$a = new Users($this->course);
        $data = $a->ListUsers('LastName');



	echo <<< END
<div id="linklist" class="linklist"><h5>Students</h5>
<form method="post" action="managegroups.php?c=$this->course&amp;action=studentupdate&amp;groupID=$g->groupID">
<ul>
END;

		$c = new Course($this->course);

        for ($i=0; $i<$data->numRows(); ++$i)
        {
			$rowData = $data->fetchRow();
			$u = new User($rowData['UserID']);

			if($u->userLevel == STUDENT && $c->isUserInCourse($u->userID)) {

				echo '<li><input type="checkbox" name="u' . $rowData['UserID'] . '"';

				if(isset($g->users[$rowData['UserID']]))
			            echo ' checked="checked"';
			
				echo ' />' . $u->firstName . ' ' . $u->lastName . ' (' . $u->email . ')</li>'."\n";
			}
        }
		
		echo <<< END
</ul>
<ul>
<h5><li><label>&nbsp;</label><input type="submit" value="Update Users" /></li></h5>
</ul>
</form>
</div>
END;





	}

	public function addGroup()
	{
		$a = new Users($this->course);
		/*
         * XXX HACK
         * we need a userID that corresponds directly to the groupID,
		 * to abuse in the assignments module
		 *
		 */

		$groupID = $a->RegisterUser('_______', 'GROUP', date('U').'@argus.group',
                            '1', '9', 1, 1);
		$cm = new CourseManager();
		$cm->addUserToCourse($this->course, $groupID);
		$g = new GroupManager($this->course);

		echo '<div id="message" class="message">';

		if($g->AddGroup($groupID, $_POST['groupName'])) {
			echo '<h5>Group successfully added.</h5></div>';
			$this->menu();
	   } else {
			echo "<ul>\n";
			foreach($a->error as $error)
				echo "<li>$error</li>\n";
			echo "</ul>\n";
			echo '</div>';

		}

	}


	public function modifyGroup()
	{
		$g = new Group($_GET['group']);

		echo '<div id="message" class="message">';

		$g->groupName = htmlspecialchars($_POST['groupName']);

		if($g->update()) {

			echo '<h5>Group successfully updated.</h5></div>';
			$this->menu();
		} else {

			echo "<ul>\n";
			foreach($a->error as $error)
				echo "<li>$error</li>\n";
			echo "</ul>\n";
			echo '</div>';

			$u = new User(0);
			$u->userID = $_GET['user'];
			$u->firstName = htmlspecialchars($_POST['firstName']);
			$u->lastName = htmlspecialchars($_POST['lastName']);
			$u->email = htmlspecialchars($_POST['email']);
			$u->userLevel = htmlspecialchars($_POST['userLevel']);
			$action = 'modify&amp;user='. $u->userID;

			$this->printForm($u, $action);
		}	
	}   



	public function studentUpdate()
	{
		$gm = new GroupManager($this->course);

		$a = new Group($_GET['groupID']);

      	$a = new Users($this->course);
        $data = $a->ListUsers('LastName');

        for ($i=0; $i<$data->numRows(); ++$i)
        {
            $rowData = $data->fetchRow();
			$u = $rowData['UserID'];
			$gm->removeUserFromGroup($_GET['groupID'], $u);
			if(isset($_POST["u$u"])) {
				$gm->addUserToGroup($_GET['groupID'], $u);
			}
        }

		echo '<div id="message" class="message">';

		echo '<h5>Group successfully modified.</h5></div>';
		$this->menu();

	}



	public function deleteGroup()
	{
		// more error checking would be good, but not critical since the delete would just silently fail
		$a = new GroupManager($this->course);
		$a->RemoveGroup($_GET['group']);
		$b = new Users($this->course);
		$b->DeleteUser($_GET['group']);
		echo '<div id="message" class="message"><h5>Group successfully removed.</h5></div>';
		$this->menu();

	}


	public function menu()
	{
		$a = new GroupManager($this->course);

		echo '<div id="linklist" class="linklist"><h5><a href="managegroups.php?c='.$this->course.'&amp;add">Add Group</a></h5></div>';
		echo '<div id="linklist"  class="linklist">';
		echo '<h5>Modify Group</h5>';

		$orderBy='LastName';
		echo '<ul>';
		if(isset($_GET['order']))
		$orderBy = $_GET['order'];
		$data = $a->ListGroups('GroupID');
		for ($i=0; $i<$data->numRows(); ++$i)
		{
			$rowData = $data->fetchRow();
			echo '<li><a href="managegroups.php?c='.$this->course.'&amp;group=' . $rowData['GroupID'] .'">';
			echo $rowData['GroupName'];

			$g = new Group($rowData['GroupID']);
			echo '</a><ul id="sub" class="sub">';
			foreach ($g->users as $user)
			{
				$u = new User($user);
				echo '<li>';
                echo $u->firstName .' '. $u->lastName .' - ('. $u->email . ')';
				echo '</a></li>'."\n";
			}

			echo '</ul></li>';
		}
		echo '</ul>';
	}


	private function printForm(Group $g, $action)
	{
		$title = ($action == 'add') ? 'Add Group' : 'Modify Group';
		
		$selected_1 = ($u->userLevel == 1) ? 'selected="selected"' : '';
		$selected_2 = ($u->userLevel == 2) ? 'selected="selected"' : '';
		$selected_3 = ($u->userLevel == 3) ? 'selected="selected"' : '';
		$selected_0 = ($u->userLevel == 0) ? 'selected="selected"' : '';

		echo <<< END
<div id="linklist" class="linklist"><h5>$title</h5>
<form method="post" action="managegroups.php?c=$this->course&amp;action=$action">
<ul>
<li><label>Group Name</label><input type="text" name="groupName" value="$g->groupName"/></li>
<li><label>&nbsp;</label>   <input type="submit" value="$title" /></li>
</ul>
</form>
</div>
END;
	}

}
?>
Return current item: CMS Admin