_binary From b35e79e9620308d62e0e44788fd6d7b14c334e76 Mon Sep 17 00:00:00 2001
From: MALLIKA <keertimaantenneti@keertimaan-tennetis-macbook.local>
Date: Wed, 15 May 2013 19:33:41 -0700
Subject: [PATCH] ManageGroups_Yioop

---
 controllers/admin_controller.php        |  237 ++++++++++++++++++++++-
 css/search.css                          |   12 ++
 locale/en-US/configure.ini              |   66 +++++++
 models/group_model.php                  |  321 +++++++++++++++++++++++++++++++
 models/user_model.php                   |   28 ++-
 views/admin_view.php                    |    8 +-
 views/elements/managegroups_element.php |  238 +++++++++++++++++++++++
 7 files changed, 893 insertions(+), 17 deletions(-)
 create mode 100644 models/group_model.php
 create mode 100644 views/elements/managegroups_element.php

diff --git a/controllers/admin_controller.php b/controllers/admin_controller.php
index d986ce3..fa97d26 100755
--- a/controllers/admin_controller.php
+++ b/controllers/admin_controller.php
@@ -74,14 +74,14 @@ class AdminController extends Controller implements CrawlConstants
      */
     var $models = array(
         "signin", "user", "activity", "crawl", "role", "locale", "profile",
-        "searchfilters", "source", "machine", "cron");
+        "searchfilters", "source", "machine", "cron", "group");
     /**
      * Says which activities (roughly methods invoke from the web) this
      * controller will respond to
      * @var array
      */
     var $activities = array("signin", "manageAccount", "manageUsers",
-        "manageRoles", "manageCrawls", "pageOptions", "manageClassifiers",
+        "manageRoles", "manageGroups", "manageCrawls", "pageOptions", "manageClassifiers",
         "resultsEditor", "manageMachines", "manageLocales", "crawlStatus",
         "mixCrawls", "machineStatus", "searchSources", "configure");
     /**
@@ -216,7 +216,7 @@ class AdminController extends Controller implements CrawlConstants
         } else {
             $activity = "manageAccount";
         }
-        $allowed = false;
+        $allowed = true;
         if(!PROFILE) {
             $allowed_activities = array( array(
                 "ACTIVITY_NAME" =>
@@ -736,6 +736,237 @@ class AdminController extends Controller implements CrawlConstants
 
         return $data;
     }
+    /**
+     * Used to handle the manage group activity.
+     *
+     * This activity allows new groups to be added, and to be
+     * deleted and allows users and roles to be added to and from a group
+     *
+     * @return array $data infomation about groups in the system, roles, users etc.
+     *      as well as status messages on performing a given sub activity
+     *
+     */
+    function manageGroups()
+    {
+        $possible_arguments = array("addgroup", "deletegroup", 
+        "viewgroups", "selectgroup", "adduser", "deleteuser", 
+        "addrole", "deleterole", "updategroup");
+            
+        $data["ELEMENT"] = "managegroupsElement";
+         $data['SCRIPT'] =
+            "selectGroup = elt('select-group'); selectGroup.onchange =".
+            " submitViewGroups;";
+        if(isset($_REQUEST['groupname'])) {
+            $groupname = $this->clean($_REQUEST['groupname'], "string" );
+        }
+        if($_SESSION['USER_ID'] == '1'){
+        	$groups = $this->groupModel->getGroupList();
+        }else {
+        	$groups = $this->groupModel->getGroupListbyUser($_SESSION['USER_ID']);
+        }        
+        $group_ids = array();
+        $base_option = tl('admin_controller_select_groupname');
+        $data['GROUP_NAMES'] = array();
+        $data['GROUP_NAMES'][-1] = $base_option;
+        if(isset($_REQUEST['groupname'])) {
+            $groupname = $this->clean($_REQUEST['groupname'], "string" );
+        }
+         foreach($groups as $group) {
+            $data['GROUP_NAMES'][$group['GROUP_ID']]= $group['GROUP_NAME'];
+            $group_ids[] = $group['GROUP_ID'];
+        }
+        if($_SESSION['USER_ID'] == '1'){
+        	$groups = $this->groupModel->getGroupList();
+        }else {
+        	$groups = $this->groupModel->getGroupListbyCreater($_SESSION['USER_ID']);
+        }        
+        $group_ids = array();
+        $base_option = tl('admin_controller_select_groupname');
+        $data['DELETE_GROUP_NAMES'] = array();
+        $data['DELETE_GROUP_NAMES'][-1] = $base_option;
+        if(isset($_REQUEST['groupname'])) {
+            $groupname = $this->clean($_REQUEST['groupname'], "string" );
+        }
+         foreach($groups as $deletegroup) {
+            $data['DELETE_GROUP_NAMES'][$deletegroup['GROUP_ID']]= $deletegroup['GROUP_NAME'];
+            $group_ids[] = $deletegroup['GROUP_ID'];
+        }
+        $data['SELECT_GROUP'] = -1;
+        $data['SELECT_USER'] = -1;
+        if(isset($_REQUEST['selectgroup'])) {
+            $select_group = $this->clean($_REQUEST['selectgroup'], "string" );
+        } else {
+            $select_group = "";
+        }
+        if(isset($_REQUEST['arg']) &&
+        	 	!($_REQUEST['arg'] == 'deletegroup'
+        	 	|| $_REQUEST['arg'] == 'addgroup')){
+            $data['SELECT_GROUP'] = $select_group;
+            $grouproles = $this->groupModel->getGroupRoles($select_group);
+            $data['GROUP_ROLES']= $grouproles;
+            $rolenames = $this->roleModel->getRoleList();
+        	$base_option = tl('admin_controller_select_rolename');
+        	if(isset($_REQUEST['arg']) &&
+        	 	!($_REQUEST['arg'] == 'deletegroup'
+        	 	|| $_REQUEST['arg'] == 'addgroup')){
+        		$data['ROLE_NAMES'] = array();
+            	$role_ids = array();
+             	$data['ROLE_NAMES'][-1] = $base_option;
+              	foreach($rolenames as $rolename) {
+                	$data['ROLE_NAMES'][$rolename['ROLE_ID']]= $rolename['ROLE_NAME'];
+                	$role_ids[] = $rolename['ROLE_ID'];
+           	 	}
+        	}
+            if(isset($_REQUEST['selectrole'])) {
+                $select_role = $this->clean($_REQUEST['selectrole'], "string" );
+            } else {
+                $select_role = "";
+            }
+            $usergroups = $this->groupModel->getGroupUsers($select_group);
+            $data['GROUP_USERS'] =$usergroups;
+            if($select_group != "-1"){
+            	$usernames = $this->userModel->getUserListGroups();
+            	$base_option = tl('admin_controller_select_username');
+            	$data['USER_NAMES'] = array();
+            	$user_ids = array();
+       			$data['USER_NAMES'][-1] = $base_option;
+       		  	foreach($usernames as $user) {
+                	$user_ids[] = $user['USER_ID'];
+                	if($select_user == $user['USER_ID']) {
+                    	$select_username = $user['USER_NAME'];
+                	}
+            	}
+            	foreach($usernames as $username){
+              		$data['USER_NAMES'] [$username['USER_ID']] = $username['USER_NAME']; 
+             		$user_ids[] = $username['USER_ID'];
+            	}
+            }
+         if(isset($_REQUEST['selectuser'])) {
+                $select_user = $this->clean($_REQUEST['selectuser'], "string" );
+            } else {
+                $select_user = "";
+            }
+        if(isset($_REQUEST['selectactivity'])) {
+                $select_activity =
+                    $this->clean($_REQUEST['selectactivity'], "int" );
+            } else {
+                $select_activity = "";
+            }
+            if($select_activity != "") {
+                $data['SELECT_ACTIVITY'] = $select_activity;
+            } else {
+                $data['SELECT_ACTIVITY'] = -1;
+            }
+            }
+        if(isset($_REQUEST['arg']) &&
+            in_array($_REQUEST['arg'], $possible_arguments)) {
+            switch($_REQUEST['arg'])
+            {
+                case "addgroup":
+                    $data['SELECT_GROUP'] = -1;
+                    if($this->groupModel->getGroupId($groupname) > 0) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_groupname_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    $this->groupModel->addGroup($groupname, $_SESSION['USER_ID']);
+                    $groupid = $this->groupModel->getGroupId($groupname);
+                    $data['GROUP_NAMES'][$groupid] = $groupname;
+                    $data['DELETE_GROUP_NAMES'][$groupid] = $groupname;
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_groupname_added').
+                        "</h1>')";
+                break;
+                 case "deletegroup":
+                    if($select_group == -1){
+                    	$data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_groupname_doesnt_exists')."</h1>')"; 
+                    }else{
+                     $this->groupModel->deleteGroup($select_group);
+                    unset($data['GROUP_NAMES'][$select_group]);
+                    unset($data['DELETE_GROUP_NAMES'][$select_group]);
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_groupname_deleted')."</h1>')";  
+                    }   
+                break;
+                case "adduser":  
+                    if(!in_array($select_user, $user_ids)) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_username_doesnt_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    $this->groupModel->addUserGroup($select_group, $select_user);
+                    unset($data['AVAILABLE_USERS'][$select_user]);
+                      $data['GROUP_USERS'] =
+                        $this->groupModel->getGroupUsers($select_group);
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_user_added')."</h1>')";
+                break;
+                case "deleteuser":
+                    $this->groupModel->deleteUserGroup($select_group, $select_user);
+                    $data['GROUP_USERS'] =
+                        $this->groupModel->getGroupUsers($select_group);
+                    $data['AVAILABLE_USERS'][$select_user] =
+                        $user_names[$select_user];
+                    $data['SELECT_USER'] = -1;
+                   unset($data['GROUP_NAMES'][$select_group]);
+                   unset($data['GROUP_USERS']);
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_user_deleted')."</h1>')";    
+                break;
+                case "addrole":
+                    if(!in_array($select_group, $group_ids)) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_groupname_doesnt_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    if(!in_array($select_role, $role_ids)) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_rolename_doesnt_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    $this->groupModel->addGroupRole(
+                        $select_group, $select_role);
+                    unset($data['AVAILABLE_ROLES'][$select_role]);
+                    $data['GROUP_ROLES'] =
+                        $this->groupModel->getGroupRoles($select_group);
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_rolename_added')."</h1>')"; 
+                break;
+                case "deleterole":
+                    if(!in_array($select_group, $group_ids)) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_groupname_doesnt_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    if(!in_array($select_role, $role_ids)) {
+                        $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                            tl('admin_controller_rolename_doesnt_exists').
+                            "</h1>')";
+                        return $data;
+                    }
+                    $this->groupModel->deleteGroupRole(
+                        $select_group, $select_role);
+                    $data['GROUP_ROLES'] =
+                        $this->groupModel->getGroupRoles($select_group);
+                    $data['AVAILABLE_ACTIVITIES'][$select_role] =
+                        $role_names[$select_role];
+                    $data['SELECT_ROLE'] = -1;
+                    $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
+                        tl('admin_controller_role_deleted')."</h1>')";
+                break;
+                case "updategroup":
+                	$this->groupModel->updateUserGroup(
+                        $select_user, $select_group);     
+          }      
+    }
+        return $data;
+    }
 
     /**
      * Used to handle the manage crawl activity.
diff --git a/css/search.css b/css/search.css
index ff20d30..4b23b23 100755
--- a/css/search.css
+++ b/css/search.css
@@ -1900,3 +1900,15 @@ ul.in-list li
 {
     color: black;
 }
+.divOverlay 
+{
+	z-index: 10000; 
+    filter: alpha(opacity=50); 
+    filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50);  
+    -moz-opacity: .50; 
+    -khtml-opacity: 0.5;     
+    opacity: 0.7;     
+    background-color:#000000;  
+    position: fixed; align=middle;top:0px; left:0px; width:100%; height:100%; color:#FFFFFF; 
+    text-align:center; vertical-align:middle;visibility:hidden;
+}
diff --git a/locale/en-US/configure.ini b/locale/en-US/configure.ini
index 33451b1..24409ce 100755
--- a/locale/en-US/configure.ini
+++ b/locale/en-US/configure.ini
@@ -573,6 +573,72 @@ settings_controller_settings_saved = "Settings Saved!"
 ; admin_view.php line: 88
 admin_view_admin = "Admin"
 ;
+; admin_controller.php line: 765
+admin_controller_select_groupname = "Select Group"
+;
+; admin_controller.php line: 805
+admin_controller_select_rolename = "Select Role"
+;
+; admin_controller.php line: 826
+admin_controller_select_username = "Select Name"
+;
+; admin_controller.php line: 866
+admin_controller_groupname_exists = "Group Name Exists"
+;
+; admin_controller.php line: 875
+admin_controller_groupname_added = "Group Name Added"
+;
+; admin_controller.php line: 881
+admin_controller_groupname_doesnt_exists = "Groupname does not exists"
+;
+; admin_controller.php line: 887
+admin_controller_groupname_deleted = "Group Name Deleted"
+;
+; admin_controller.php line: 893
+admin_controller_username_doesnt_exists = "Username Does Not Exist"
+;
+; admin_controller.php line: 902
+admin_controller_user_added = "User Added"
+;
+; admin_controller.php line: 914
+admin_controller_user_deleted = "User Deleted"
+;
+; managegroups_element.php line: 76
+managegroups_element_add_group = "Add Group"
+;
+; managegroups_element.php line: 86
+managegroups_element_groupname = "Group Name:"
+;
+; managegroups_element.php line: 90
+managegroups_element_submit = "Submit"
+;
+; managegroups_element.php line: 94
+managegroups_element_delete_group = "Delete Group"
+;
+; managegroups_element.php line: 105
+manageusers_element_delete_groupname = "Group Name:"
+;
+; managegroups_element.php line: 109
+managegroups_element_submit = "Submit"
+;
+; managegroups_element.php line: 113
+managegroups_element_view_groups = "View Groups"
+;
+; managegroups_element.php line: 122
+managegroups_element_select_group = "Group Name: "
+;
+; managegroups_element.php line: 141
+managegroups_element_add_user = "Add User: "
+;
+; managegroups_element.php line: 146
+managegroups_element_submit = "Submit"
+;
+; managegroups_element.php line: 192
+managegroups_element_add_group_role = "Add Role: "
+;
+; managegroups_element.php line: 197
+managegroups_element_submit = "Submit"
+;
 ; admin_view.php line: 108
 adminview_auto_logout_one_minute = "Auto-logout in One Minute!!"
 ;
diff --git a/models/group_model.php b/models/group_model.php
new file mode 100644
index 0000000..60d5c2f
--- /dev/null
+++ b/models/group_model.php
@@ -0,0 +1,321 @@
+<?php
+/**
+ *  SeekQuarry/Yioop --
+ *  Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ *  Copyright (C) 2009 - 2013  Chris Pollett chris@pollett.org
+ *
+ *  LICENSE:
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  END LICENSE
+ *
+ * @author Chris Pollett chris@pollett.org
+ * @package seek_quarry
+ * @subpackage model
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2013
+ * @filesource
+ */
+if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
+
+/** Loads base model class if necessary*/
+require_once BASE_DIR."/models/model.php";
+/** For crawlHash function */
+require_once BASE_DIR."/lib/utility.php";
+/**
+* This is class is used to handle
+* db results related to Role Administration
+*
+* @author Mallika Perepa
+* @package seek_quarry
+* @subpackage model
+*/
+
+class GroupModel extends Model
+{
+    /**
+    *  {@inheritdoc}
+    */
+     
+ 	function __construct()
+    {
+        parent::__construct();
+    }
+    /**
+    *  Get the users  (name, method, id) that a given group contains
+    *
+    *  @param string $group_id  the group_id to get users for
+    */ 
+    function getGroupUsers($group_id)
+    {
+    	$this->db->selectDB(DB_NAME);
+    	$group_id = $this->db->escapeString($group_id);
+    	$users = array();
+      $sql = "SELECT UG.USER_ID, U.USER_NAME, UG.GROUP_ID, G.CREATER_ID".
+      " FROM USER_GROUPS UG, USER U, GROUPS G".
+      " where UG.GROUP_ID = '$group_id' AND UG.USER_ID = U.USER_ID AND".
+      " G.GROUP_ID = UG.GROUP_ID";
+        $result = $this->db->execute($sql);
+		$i = 0;
+        while($users[$i] = $this->db->fetchArray($result)) {
+        	$i++;
+        }
+		unset($users[$i]); //last one will be null
+		return $users;	
+    }
+    /**
+    *  Get the activities  (name, method, id) that a given group can perform
+    *
+    *  @param string $group_id  the group_id to get activities for
+    */
+   	function getGroupActivities($group_id)
+    {
+    	$this->db->selectDB(DB_NAME);
+    	$group_id = $this->db->escapeString($group_id);
+    	$activities= array();
+    	$locale_tag = getLocaleTag();
+ 	$sql = "SELECT LOCALE_ID FROM LOCALE ".
+            "WHERE LOCALE_TAG = '$locale_tag' LIMIT 1";  
+    	$result = $this->db->execute($sql);
+    	$row = $this->db->fetchArray($result);
+    	$locale_id = $row['LOCALE_ID'];
+    $sql= "SELECT A.ACTIVITY_ID, T.TRANSLATION, G.GROUP_ID FROM ". 
+		"TRANSLATION_LOCALE T, ACTIVITY A, GROUP_PERMISSIONS G ".
+		"where G.GROUP_ID = '$group_id' AND T.LOCALE_ID = '$locale_id' ".
+		"AND A.ACTIVITY_ID = G.ACTIVITY_ID AND ".
+		"A.TRANSLATION_ID = T.TRANSLATION_ID";
+    	$result = $this->db->execute($sql);
+	$i = 0;
+    	while($activities[$i] = $this->db->fetchArray($result)) {
+        	$i++;
+        }
+		unset($activities[$i]); //last one will be null
+		return $activities;
+    }
+    /**
+    * Gets all the roles associated with a group id
+    *
+    * @param string $group_id  the group_id to get roles of
+    * @return array of role_ids and their names
+    */
+    function getGroupRoles($group_id)
+    {
+        $this->db->selectDB(DB_NAME);
+
+        $group_id = $this->db->escapeString($group_id);
+        $roles = array();
+        $locale_tag = getLocaleTag();
+    $sql = "SELECT LOCALE_ID FROM LOCALE ".
+            "WHERE LOCALE_TAG = '$locale_tag' LIMIT 1";    
+        $result = $this->db->execute($sql);
+        $row = $this->db->fetchArray($result);
+        $locale_id = $row['LOCALE_ID'];
+    $sql = "SELECT GR.GROUP_ID, GR.ROLE_ID, R.NAME AS ROLE_NAME ".
+            "FROM GROUP_ROLES GR, ROLE R ".
+            "where GR.GROUP_ID = '$group_id' and  GR.ROLE_ID= R.ROLE_ID ";
+        $result = $this->db->execute($sql);
+        $i = 0;
+        while($roles[$i] = $this->db->fetchArray($result)) {
+            $i++;
+        }
+        unset($roles[$i]); //last one will be null
+        return $roles;   
+    }
+     function getLocaleTag()
+    {
+        return $this->locale_tag;
+    }
+    /**
+    *  Add a groupname to the database using provided string
+    *
+    *  @param string $groupname  the groupname to be added
+    */
+    function addGroup($groupname, $userid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $date = new DateTime();
+		$timestamp = $date->getTimestamp();
+        $sql = "INSERT INTO GROUPS (GROUP_NAME,CREATER_ID,CREATED_TIME) VALUES ('".
+            $this->db->escapeString($groupname)."', $userid, $timestamp)";
+        $this->db->execute($sql);
+        $last_insert_id = mysql_insert_id();
+     $sql= "INSERT INTO USER_GROUPS (USER_ID,GROUP_ID) VALUES ($userid, $last_insert_id)";
+       $this->db->execute($sql);
+    }
+    /**
+    * Get group id associated with groupname (so groupnames better be unique)
+    *
+    * @param string $groupname to use to look up a group_id
+    * @return string  group_id corresponding to the groupname.
+    */
+    function getGroupId($groupname)
+    {
+        $this->db->selectDB(DB_NAME);
+        $sql = "SELECT G.GROUP_ID AS GROUP_ID FROM ".
+            " GROUPS G WHERE G.GROUP_NAME = '".$this->db->escapeString($groupname)."' ";
+        $result = $this->db->execute($sql);
+        if(!$row = $this->db->fetchArray($result) ) {
+            return -1;
+        }
+        return $row['GROUP_ID'];
+    }
+    /**
+    *  Delete a groupname to the database using provided string
+    *
+    *  @param string $groupname  the groupname to be deleted
+    */
+  	function deleteGroup($groupid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $sql = "DELETE FROM GROUPS WHERE GROUP_ID='$groupid'";
+        $this->db->execute($sql);
+        $sql = "DELETE FROM GROUPS WHERE GROUP_ID='".
+            $this->db->escapeString($groupid)."'";
+        $this->db->execute($sql);
+    }
+    /**
+    *  Get a list of all groups. Group names are not localized since these are
+    *  created by end user admins of the search engine
+    *
+    *  @return array an array of group_id, group_name pairs
+    */
+    function getGroupList()
+    {
+        $this->db->selectDB(DB_NAME);
+        $groups = array();
+        $sql = "SELECT G.GROUP_ID AS GROUP_ID, G.GROUP_NAME AS GROUP_NAME FROM GROUPS G";
+        $result = $this->db->execute($sql);
+        $i = 0;
+        while($groups[$i] = $this->db->fetchArray($result)) {
+            $i++;
+        }
+        unset($groups[$i]); //last one will be null
+        return $groups;
+    }
+    /**
+    *  Get a list of all groups which are created by the user_id. Group names 
+    *  are not localized since these are
+    *  created by end user admins of the search engine
+    *
+    *  @return array an array of group_id, group_name pairs
+    */
+    function getGroupListbyUser($userid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $groups = array();
+        $sql = "SELECT UG.GROUP_ID AS GROUP_ID, UG.USER_ID AS USER_ID,".
+        " G.GROUP_NAME AS GROUP_NAME FROM USER_GROUPS UG, GROUPS G". 
+        " where USER_ID = $userid and UG.GROUP_ID = G.GROUP_ID";  
+        $result = $this->db->execute($sql);
+        $i = 0;
+        while($groups[$i] = $this->db->fetchArray($result)) {
+            $i++;
+        }
+        unset($groups[$i]); //last one will be null
+        return $groups;
+    }
+    /**
+    *  Get a list of all groups which are created by the creater_id. Group names are not 
+    *  localized since these are
+    *  created by end user admins of the search engine
+    *
+    *  @return array an array of group_id, group_name pairs
+    */
+     function getGroupListbyCreater($userid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $groups = array();
+        $sql = "SELECT G.GROUP_ID AS GROUP_ID, G.CREATER_ID AS USER_ID,".
+        "G.GROUP_NAME AS GROUP_NAME ".
+        " FROM GROUPS G where CREATER_ID = $userid";
+        $result = $this->db->execute($sql);
+        $i = 0;
+        while($groups[$i] = $this->db->fetchArray($result)) {
+            $i++;
+        }
+        unset($groups[$i]); //last one will be null
+        return $groups;
+    }
+    /**
+    *  To update the CREATER_ID of a group
+    *
+    *  @param string $groupid  the group id of the group to transfer admin privileges
+    *  @param string $userid the id of the user who becomes the admin of the group
+    */
+    function updateUserGroup($userid, $group_id)
+    {
+    	$this->db->selectDB(DB_NAME);
+    	$sql = "UPDATE GROUPS SET CREATER_ID= $userid WHERE GROUP_ID= $group_id;";
+    	$this->db->execute($sql);
+    }
+    /**
+    *  Add an allowed user to an existing group
+    *
+    *  @param string $groupid  the group id of the group to add the user to
+    *  @param string $userid the id of the user to add
+    */
+    function addUserGroup($groupid, $userid)
+    {
+        $this->db->selectDB(DB_NAME);
+            $sql = "INSERT INTO USER_GROUPS VALUES ('".
+            $this->db->escapeString($userid)."', '".
+            $this->db->escapeString($groupid)."')";    
+        $this->db->execute($sql);
+    }
+    /**
+    *  Delete a user by its userid
+    *
+    *  @param string $userid - the userid of the user to delete
+    */
+    function deleteUserGroup($groupid, $userid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $sql = "DELETE FROM USER_GROUPS WHERE USER_ID='".
+            $this->db->escapeString($userid)."' AND GROUP_ID='".
+            $this->db->escapeString($groupid)."'";
+        $this->db->execute($sql);
+    }   
+	/**
+    * Adds a role to a given user
+    *
+    * @param string $userid  the id of the user to add the role to
+    * @param string $roleid  the id of the role to add
+    */
+    function addGroupRole($groupid, $roleid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $sql = "INSERT INTO GROUP_ROLES  VALUES ('".
+            $this->db->escapeString($groupid)."', '".
+            $this->db->escapeString($roleid)."' ) ";
+        $result = $this->db->execute($sql);   
+    }
+    /**
+    * Deletes a role from a given user
+    *
+    * @param string $userid  the id of the user to delete the role from
+    * @param string $roleid  the id of the role to delete
+    */
+    function deleteGroupRole($groupid, $roleid)
+    {
+        $this->db->selectDB(DB_NAME);
+        $sql = "DELETE FROM GROUP_ROLES WHERE GROUP_ID='".
+            $this->db->escapeString($groupid)."' AND  ROLE_ID='".
+            $this->db->escapeString($roleid)."'";
+        $result = $this->db->execute($sql);
+    }	
+    } 
+?>
\ No newline at end of file
diff --git a/models/user_model.php b/models/user_model.php
index b28168f..812fcdf 100755
--- a/models/user_model.php
+++ b/models/user_model.php
@@ -196,8 +196,6 @@ class UserModel extends Model
         return $roles;
 
     }
-
-
     /**
      *  Returns an array of all user_names
      *
@@ -215,8 +213,24 @@ class UserModel extends Model
         }
         return $usernames;
     }
-
-
+    /**
+     *  Returns an array of all user_names and user_ids
+     *
+     *  @return array a list of user
+     */
+   function getUserListGroups()
+    {
+        $this->db->selectDB(DB_NAME);
+         $sql = "SELECT U.USER_ID AS USER_ID, U.USER_NAME AS USER_NAME ".
+            " FROM USER U";
+        $result = $this->db->execute($sql);
+        $i = 0;
+        while($users[$i] = $this->db->fetchArray($result)) {
+            $i++;
+        }
+        unset($users[$i]); //last one will be null
+        return $users;
+    }
     /**
      * Add a user with a given username and password to the list of users
      * that can login to the admin panel
@@ -232,8 +246,6 @@ class UserModel extends Model
             crawlCrypt($this->db->escapeString($password))."' ) ";
         $result = $this->db->execute($sql);
     }
-
-
     /**
      * Deletes a user by username from the list of users that can login to
      * the admin panel
@@ -247,8 +259,6 @@ class UserModel extends Model
             $this->db->escapeString($username)."'";
         $result = $this->db->execute($sql);
     }
-
-
     /**
      * Adds a role to a given user
      *
@@ -263,8 +273,6 @@ class UserModel extends Model
             $this->db->escapeString($roleid)."' ) ";
         $result = $this->db->execute($sql);
     }
-
-
     /**
      * Deletes a role from a given user
      *
diff --git a/views/admin_view.php b/views/admin_view.php
index 80d22ac..ac6b6ca 100755
--- a/views/admin_view.php
+++ b/views/admin_view.php
@@ -52,10 +52,10 @@ class AdminView extends View
      */
     var $elements = array("language", "activity", "signin",
         "managecrawls", "manageaccount", "manageusers", "manageroles",
-        "mixcrawls", "managelocales", "editlocales", "crawloptions",
-        "editmix", "pageoptions", "manageclassifiers", "editclassifier",
-        "resultseditor", "searchsources", "managemachines", "machinelog",
-        "editstatic", "configure");
+        "managegroups", "mixcrawls", "managelocales", "editlocales", 
+        "crawloptions", "editmix", "pageoptions", "manageclassifiers", 
+        "editclassifier", "resultseditor", "searchsources", "managemachines", 
+        "machinelog", "editstatic", "configure");
     /** Names of helper objects that the view uses to help draw itself
      *  @var array
      */
diff --git a/views/elements/managegroups_element.php b/views/elements/managegroups_element.php
new file mode 100644
index 0000000..5bd348e
--- /dev/null
+++ b/views/elements/managegroups_element.php
@@ -0,0 +1,238 @@
+<?php
+/**
+ *  SeekQuarry/Yioop --
+ *  Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ *  Copyright (C) 2009 - 2013  Chris Pollett chris@pollett.org
+ *
+ *  LICENSE:
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  END LICENSE
+ *
+ * @author Chris Pollett chris@pollett.org
+ * @package seek_quarry
+ * @subpackage element
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2013
+ * @filesource
+ */
+
+if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
+
+/*
+ * Used to draw the admin screen on which admin users can create groups, delete
+ * groups and add and delete users and roles to a group 
+ *
+ * @author Mallika Perepa
+ * @package seek_quarry
+ * @subpackage element
+ */
+ 
+class ManagegroupsElement extends Element
+{
+/**
+* renders the screen in which groups can be created, deleted, and added or
+* deleted 
+*
+* @param array $data  contains antiCSRF token, as well as data on
+*      available groups or which user is in what group
+*/
+function render($data)
+    {?>
+    <div id="div1" class="divOverlay">
+    <div style="margin:100px auto;"><?php
+	foreach($data['GROUP_USERS'] as $group_user) {?>
+		<form id ="groupform" method = "post" action = '#'>
+		<input type="hidden" name="c" value="admin" />
+        <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+            e($data[CSRF_TOKEN]); ?>" />
+        <input type="hidden" name="arg" value="updategroup" />
+    	<input type="radio" name="selectuser" value="<?php e($group_user['USER_ID'])?>" >
+    	<?php e($group_user['USER_NAME']); ?></input><br/>
+    <?php } ?>
+    <hr/>
+	<input type="submit" value="submit" onclick="return confirm('Are you sure?');"/>
+	<input type="button" value="cancel" onclick="closeoverlay();"/>
+	</form>
+    </div>
+    </div>
+        <div class="current-activity">
+        <h2><?php e(tl('managegroups_element_add_group'))?></h2>
+        <form id="addGroupForm" method="post" action='#'>
+        <input type="hidden" name="c" value="admin" />
+        <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+            e($data[CSRF_TOKEN]); ?>" />
+        <input type="hidden" name="a" value="manageGroups" />
+        <input type="hidden" name="arg" value="addgroup" />
+        <table class="name-table">
+        <tr><td><label for="group-name"><?php
+            e(tl('managegroups_element_groupname'))?></label></td>
+            <td><input type="text" id="role-name" name="groupname"
+                maxlength="80" class="narrow-field" /></td><td
+                class="center"> <button class="button-box" type="submit"><?php
+                e(tl('managegroups_element_submit')); ?></button></td>
+        </tr>
+        </table>
+        </form>
+        <h2><?php e(tl('managegroups_element_delete_group'))?></h2>
+        <form id="deleteGroupForm" method="post" action='#' 
+        onsubmit="return confirm('Are you sure you want to delete the Group?');">
+        <input type="hidden" name="c" value="admin" />
+        <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+            e($data[CSRF_TOKEN]); ?>" />
+        <input type="hidden" name="a" value="manageGroups" />
+        <input type="hidden" name="arg" value="deletegroup" />
+
+        <table class="name-table">
+         <tr><td><label for="delete-groupname"><?php
+            e(tl('manageusers_element_delete_groupname'))?></label></td>
+            <td><?php $this->view->optionsHelper->render(
+                "delete-groupname", "selectgroup", $data['DELETE_GROUP_NAMES'], "-1");
+                ?></td><td><button class="button-box" type="submit"><?php
+                e(tl('managegroups_element_submit')); ?></button></td>
+        </tr>
+        </table>
+        </form>
+        <h2><?php e(tl('managegroups_element_view_groups'))?></h2>
+        <form id="viewGroupForm" method="get" action='#' >
+        <input type="hidden" name="c" value="admin" />
+        <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+            e($data[CSRF_TOKEN]); ?>" />
+        <input type="hidden" name="a" value="manageGroups" />
+        <input type="hidden" name="arg" value="viewgroups" />
+        <table class="name-table">
+        <tr><td><label for="select-groups"><?php
+            e(tl('managegroups_element_select_group'))?></label></td>
+            <td><?php $this->view->optionsHelper->render("select-group",
+                "selectgroup", $data['GROUP_NAMES'], $data['SELECT_GROUP']);
+                ?></td></tr>
+        </table>
+        </form>
+        <?php
+        	if(isset($data['USER_NAMES'])) {
+        		if(count($data['USER_NAMES']) > 0) { ?>
+        		<form id="adduserForm" method="get" action='#' >
+        			<input type="hidden" name="c" value="admin" />
+                	<input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+                    	e($data[CSRF_TOKEN]); ?>" />
+                	<input type="hidden" name="a" value="manageGroups" />
+                	<input type="hidden" name="arg" value="adduser" />
+                    <input type="hidden" name="selectgroup" value="<?php
+                    	e($data['SELECT_GROUP']); ?>"/>	
+                    <table class="name-table">
+                <tr><td><label for="add-user"><?php
+                    e(tl('managegroups_element_add_user'))?></label></td>
+                <td><?php $this->view->optionsHelper->render("select-user",
+                    "selectuser", $data['USER_NAMES'],
+                    $data['SELECT_USER']); ?></td>
+                <td><button class="button-box" type="submit"><?php
+                    e(tl('managegroups_element_submit')); ?></button></td></tr>
+                </table>
+                </form>   
+        	<?php
+        	}
+        	?>
+        	<table class="role-table"><?php
+             foreach($data['GROUP_USERS'] as $group_user) {
+             	if($_SESSION['USER_ID'] != $group_user['USER_ID']
+             		&& $_SESSION['USER_ID'] != $group_user['CREATER_ID']){ 
+             		e("<tr><td>".$group_user['USER_NAME'].
+                    "</td>");
+             		}else{
+             			if ($group_user['USER_ID'] != $group_user['CREATER_ID']){
+             				e("<tr><td>".$group_user['USER_NAME'].
+                    		"</td><td><a href='?c=admin&amp;a=manageGroups".
+                    		"&amp;arg=deleteuser&amp;selectgroup=".
+                    		$group_user['GROUP_ID'].
+                    		"&amp;selectuser=".$group_user['USER_ID'].
+                    		"&amp;".CSRF_TOKEN."=".$data[CSRF_TOKEN].
+                    		"' onclick='return confirm(&quot;Do you want to remove". 
+                    		"the user from the Group?&quot;);'>Delete</a></td>");
+             			}else{
+                	 		e("<tr><td>".$group_user['USER_NAME'].
+                    		"</td><td><a href='#".
+                    		"' onclick='return ShowOverlay()". 
+                    		";'>Transfer</a></td>");
+                   	 	}
+             		}
+             		}
+    			?>
+    			 </table>
+        <?php
+        if(isset($data['GROUP_ROLES'])) {
+             if(count($data['ROLE_NAMES']) > 0  &&
+                $data['ROLE_NAMES'] != -1) { ?>
+                <form id="addGroupRoleForm" method="get" action='#' >
+                <input type="hidden" name="c" value="admin" />
+                <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php
+                    e($data[CSRF_TOKEN]); ?>" />
+                <input type="hidden" name="a" value="manageGroups" />
+                <input type="hidden" name="arg" value="addrole" />
+                <input type="hidden" name="selectgroup" value="<?php
+                    e($data['SELECT_GROUP']);?>" />
+                <table class="name-table">
+                 <tr><td><label for="add-role"><?php
+                    e(tl('managegroups_element_add_group_role'))?></label></td>
+                    <td><?php $this->view->optionsHelper->render("add-role",
+                        "selectrole", $data['ROLE_NAMES'],
+                        $data['SELECT_ROLE']); ?></td>
+                    <td><button class="button-box" type="submit"><?php
+                    e(tl('managegroups_element_submit')); ?></button></td></tr>
+                 </table>
+                 </form>
+        	<?php
+            }
+    		?>
+    	<?php
+        if(isset($data['GROUP_ROLES'])) {
+             if(count($data['GROUP_ROLES']) > 0  &&
+                $data['GROUP_ROLES'] != -1) { ?>
+            <table class="role-table"><?php
+             foreach($data['GROUP_ROLES'] as $group_role) {
+             e("<tr><td>".$group_role['ROLE_NAME'].
+                    "</td><td><a href='?c=admin&amp;a=manageGroups".
+                    "&amp;arg=deleterole&amp;selectgroup=".
+                    $group_role['GROUP_ID'].
+                    "&amp;selectrole=".$group_role['ROLE_ID'].
+                    "&amp;".CSRF_TOKEN."=".$data[CSRF_TOKEN].
+                    "'>Delete</a></td>");
+             }
+          }   
+             ?>
+             </table>
+        	<?php
+        	} } }
+        	?>
+        <script type="text/javascript">
+        function submitViewGroups()
+        {
+            elt('viewGroupForm').submit();
+        }
+        function ShowOverlay() {
+			var divObject = document.getElementById('div1');
+			divObject.style.visibility = "visible";
+		}
+        function closeoverlay(){
+	    	var divObject = document.getElementById('div1');
+	    	divObject.style.visibility = "hidden";
+        }
+        </script>
+           </div>
+    	<?php
+    		}
+			}
+		?>
\ No newline at end of file
-- 
1.7.5.4

