2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 * DAO for "user_groups" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class UserGroupsDAO extends DAO {
27 * Create a new user group
31 * @return user id, if successful
32 * false and add error into global var $msg, if unsuccessful
35 public function Create($title, $description)
37 global $addslashes, $msg;
39 $missing_fields = array();
42 $title = $addslashes(trim($title));
44 /* login name check */
47 $missing_fields[] = _AT('title');
52 $missing_fields = implode(', ', $missing_fields);
53 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
56 if (!$msg->containsErrors())
58 /* insert into the db */
59 $sql = "INSERT INTO ".TABLE_PREFIX."user_groups
69 if (!$this->execute($sql))
71 $msg->addError('DB_NOT_UPDATED');
76 return mysql_insert_id();
86 * Update an existing user group
88 * @param user_group_id
91 * @return user id, if successful
92 * false and add error into global var $msg, if unsuccessful
95 public function Update($user_group_id, $title, $description)
97 global $addslashes, $msg;
99 $missing_fields = array();
102 $title = $addslashes(trim($title));
104 /* login name check */
107 $missing_fields[] = _AT('title');
112 $missing_fields = implode(', ', $missing_fields);
113 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
116 if (!$msg->containsErrors())
118 /* insert into the db */
119 $sql = "UPDATE ".TABLE_PREFIX."user_groups
120 SET title = '".$title."',
121 description = '".$description."',
123 WHERE user_group_id = ".$user_group_id;
125 return $this->execute($sql);
130 * Update an existing user group record
132 * @param userGroupID: user group ID
133 * fieldName: the name of the table field to update
134 * fieldValue: the value to update
135 * @return true if successful
136 * error message array if failed; false if update db failed
137 * @author Cindy Qi Li
139 public function UpdateField($userGroupID, $fieldName, $fieldValue)
143 // check if the required fields are filled
144 if ($fieldName == 'title' && $fieldValue == '') return array(_AT('TR_ERROR_EMPTY_FIELD'));
146 $sql = "UPDATE ".TABLE_PREFIX."user_groups
147 SET ".$fieldName."='".$addslashes($fieldValue)."'
148 WHERE user_group_id = ".$userGroupID;
150 return $this->execute($sql);
154 * delete user group by given user id
156 * @param user group id
157 * @return true / false
158 * @author Cindy Qi Li
160 public function Delete($userGroupID)
162 // delete user_group_privilege
163 include_once(TR_INCLUDE_PATH.'classes/DAO/UserGroupPrivilegeDAO.class.php');
165 $userGroupPrivilegeDAO = new UserGroupPrivilegeDAO();
166 $userGroupPrivilegeDAO->DeleteByUserGroupID($userGroupID);
168 // delete user_groups
169 $sql = 'DELETE FROM '.TABLE_PREFIX.'user_groups WHERE user_group_id = '.$userGroupID;
171 return $this->execute($sql);
175 * Return all user groups' information
179 * @author Cindy Qi Li
181 public function getAll()
183 $sql = 'SELECT * FROM '.TABLE_PREFIX.'user_groups ORDER BY title';
184 return $this->execute($sql);
188 * Return user information by given user id
190 * @param user group id
192 * @author Cindy Qi Li
194 public function getUserGroupByID($user_group_id)
196 $sql = 'SELECT * FROM '.TABLE_PREFIX.'user_groups WHERE user_group_id='.$user_group_id;
197 if ($rows = $this->execute($sql))