Commit 297fa2a74cb174a62bd9f7826ce1c55dc1c37256
1 parent
b1a970d4
This handles all the group management functions
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@279 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
459 additions
and
0 deletions
lib/administration/GroupManager.inc
0 → 100644
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * $Id: GroupManager.inc | |
| 4 | + * | |
| 5 | + * Performs group administration tasks- this includes create, remove, update | |
| 6 | + * as well as addGroupToUnit and removeGroupFromUnit ..etc | |
| 7 | + * | |
| 8 | + * @version $Revision: 1.0 | |
| 9 | + * @author Mukhtar Dharsey | |
| 10 | + * @package dmslib | |
| 11 | + */ | |
| 12 | + | |
| 13 | +// group management | |
| 14 | + class GroupManager | |
| 15 | + { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * | |
| 19 | + * Adds a new group | |
| 20 | + * | |
| 21 | + * @param name | |
| 22 | + * name of new group | |
| 23 | + * @return boolean | |
| 24 | + * true if the addition was successful, else false. | |
| 25 | + */ | |
| 26 | + | |
| 27 | + function createGroup($name) { | |
| 28 | + global $default; | |
| 29 | + | |
| 30 | + $sql = new Owl_DB; | |
| 31 | + | |
| 32 | + | |
| 33 | + // check that the group name is unique | |
| 34 | + $query = "SELECT name FROM $default->owl_groups_table WHERE name = '" . $name . "'"; | |
| 35 | + $sql->query($query); | |
| 36 | + $rows = $sql->num_rows($sql); | |
| 37 | + | |
| 38 | + if ($rows > 0) | |
| 39 | + { | |
| 40 | + // duplicate username | |
| 41 | + $default->errorMessage = "GroupManager::The Group name " . $name . " is already in use, please choose another one"; | |
| 42 | + $default->log->debug($default->errorMessage); | |
| 43 | + return false; | |
| 44 | + } | |
| 45 | + // insert the user | |
| 46 | + $query = "INSERT INTO $default->owl_groups_table (name) VALUES ('" . $name . "')"; | |
| 47 | + | |
| 48 | + $result = $sql->query($query); | |
| 49 | + | |
| 50 | + if(!'result') | |
| 51 | + { | |
| 52 | + $default->log->debug( "GroupManager::New Group Addition Unsuccessful!<br>"); | |
| 53 | + return false; | |
| 54 | + } | |
| 55 | + else | |
| 56 | + { | |
| 57 | + $default->log->debug ("GroupManager::New Group added Successfully!<br>"); | |
| 58 | + return true; | |
| 59 | + } | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | +} | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Removes a group from the groups table as well as from the groups_units_link table | |
| 67 | + * | |
| 68 | + * @param groupID | |
| 69 | + * The id of the group to be deleted | |
| 70 | + * @return boolean | |
| 71 | + * True if the deletion was successful, else false if not or nonexistant. | |
| 72 | + */ | |
| 73 | + function removeGroup($groupID) | |
| 74 | + { | |
| 75 | + global $default; | |
| 76 | + // create a connection | |
| 77 | + $sql = new Owl_DB; | |
| 78 | + | |
| 79 | + //do validation that userid exists | |
| 80 | + $query = "SELECT * FROM $default->owl_groups_table WHERE id = '" . $groupID ."'"; | |
| 81 | + $result = $sql->query($query); | |
| 82 | + $row = $sql->num_rows($result); | |
| 83 | + | |
| 84 | + // check if result was found..0 if not | |
| 85 | + if($row == 0) | |
| 86 | + { | |
| 87 | + $default->log->debug("GroupManager::Group does not exist in the database<br>"); | |
| 88 | + return false; | |
| 89 | + } | |
| 90 | + | |
| 91 | + //if user id exists delete it from the users table | |
| 92 | + $query = "DELETE FROM $default->owl_groups_table WHERE id = $groupID"; | |
| 93 | + $result = $sql->query($query); | |
| 94 | + | |
| 95 | + if(!'result') | |
| 96 | + { | |
| 97 | + $default->log->debug("GroupManager::Deletion unsuccessful<br>"); | |
| 98 | + return false; | |
| 99 | + } | |
| 100 | + else | |
| 101 | + { | |
| 102 | + $default->log->debug ("GroupManager::Deletion from user table Successful<br>"); | |
| 103 | + //check if belongs to group | |
| 104 | + | |
| 105 | + $result= $this->removeGroupFromUnit($groupID); | |
| 106 | + return true; | |
| 107 | + } | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Updates groups details ..ie rename the group | |
| 115 | + * | |
| 116 | + * @param groupID | |
| 117 | + * the group which is to be renamed | |
| 118 | + * @param name | |
| 119 | + * new name of the group | |
| 120 | + * @return boolean | |
| 121 | + * true if the addition was successful, else false. | |
| 122 | + */ | |
| 123 | + function updateGroup($groupID, $name) | |
| 124 | + { | |
| 125 | + global $default; | |
| 126 | + // create a connection | |
| 127 | + $sql = new Owl_DB; | |
| 128 | + | |
| 129 | + //do validation that userid exists | |
| 130 | + $query = "SELECT * FROM $default->owl_groups_table WHERE id = $groupID"; | |
| 131 | + $result = $sql->query($query); | |
| 132 | + $row = $sql->num_rows($result); | |
| 133 | + | |
| 134 | + //if row = 0 ...then no entry was found..so return false | |
| 135 | + if($row == 0) | |
| 136 | + { | |
| 137 | + $default->log->debug("GroupManager::Group does not exist in the database<br>"); | |
| 138 | + return false; | |
| 139 | + } | |
| 140 | + | |
| 141 | + //if user id exists update all info into the users table | |
| 142 | + $query = "UPDATE $default->owl_groups_table SET name = '" . $name . "' WHERE id = $groupID " ; | |
| 143 | + | |
| 144 | + $result = $sql->query($query); | |
| 145 | + | |
| 146 | + | |
| 147 | + // error checking to see if success | |
| 148 | + if(!'result') | |
| 149 | + { | |
| 150 | + $default->log->debug("GroupManager::Not Updated"); | |
| 151 | + return false; | |
| 152 | + } | |
| 153 | + else | |
| 154 | + { | |
| 155 | + $default->log->debug("GroupManager::Update Successful<br>"); | |
| 156 | + return true; | |
| 157 | + } | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Returns an array of all the groups | |
| 162 | + * | |
| 163 | + * @return array | |
| 164 | + * An array of groups | |
| 165 | + */ | |
| 166 | + function listGroups() | |
| 167 | + { | |
| 168 | + | |
| 169 | + global $default; | |
| 170 | + $groups = array (); | |
| 171 | + $i = 0; | |
| 172 | + // create a connection | |
| 173 | + $sql = new Owl_DB; | |
| 174 | + | |
| 175 | + //Get list of all the usernames | |
| 176 | + $query = "SELECT name FROM $default->owl_groups_table"; | |
| 177 | + $result = $sql->query($query); | |
| 178 | + $row = $sql->num_rows($result); | |
| 179 | + | |
| 180 | + //error checking | |
| 181 | + if($row == 0) | |
| 182 | + { | |
| 183 | + $default->log->debug("GroupManager::No users in table"); | |
| 184 | + return false; | |
| 185 | + } | |
| 186 | + | |
| 187 | + //store in array | |
| 188 | + while($sql->next_record()) | |
| 189 | + { | |
| 190 | + $groups["$i"] = array("name" => $sql->f("name")); | |
| 191 | + $i++; | |
| 192 | + } | |
| 193 | + | |
| 194 | + //return an array of the groups | |
| 195 | + return $groups; | |
| 196 | + | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * | |
| 201 | + * Adds a group to a unit NB: Group belongs to 1 unit only | |
| 202 | + * | |
| 203 | + * @param groupID | |
| 204 | + * The ID of the group to add the user to | |
| 205 | + * @param unitID | |
| 206 | + * The Id of the unit to add to | |
| 207 | + * @return boolean | |
| 208 | + * True if the addition was successful, else false if not or nonexistant. | |
| 209 | + */ | |
| 210 | + function addGroupToUnit($groupID,$unitID) | |
| 211 | + { | |
| 212 | + global $default; | |
| 213 | + // create a connection | |
| 214 | + $sql = new Owl_DB; | |
| 215 | + | |
| 216 | + //do validation that groupid exists | |
| 217 | + $query = "SELECT * FROM $default->owl_groups_units_table WHERE group_id = $groupID"; | |
| 218 | + $result = $sql->query($query); | |
| 219 | + $row = $sql->num_rows($result); | |
| 220 | + | |
| 221 | + // if it does exist return false..already there | |
| 222 | + if($row >= 1) | |
| 223 | + { | |
| 224 | + $default->log->debug("GroupManager::Group already belongs to a unit<br>"); | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + | |
| 228 | + //add user to the table | |
| 229 | + $query = "INSERT INTO $default->owl_groups_units_table (group_id,unit_id) VALUES($groupID,$unitID)"; | |
| 230 | + $result = $sql->query($query); | |
| 231 | + | |
| 232 | + if(!'result') | |
| 233 | + { | |
| 234 | + $default->log->debug("GroupManager::Insertion into groups_units table unsuccessful<br>"); | |
| 235 | + return false; | |
| 236 | + } | |
| 237 | + else | |
| 238 | + { | |
| 239 | + $default->log->debug("GroupManager::Insertion into groups_units table Successful<br>"); | |
| 240 | + return true; | |
| 241 | + } | |
| 242 | + | |
| 243 | + } | |
| 244 | + | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Removes a group from a unit NB: Group can only belong to 1 unit | |
| 248 | + * | |
| 249 | + * @param groupID | |
| 250 | + * The ID of the group be removed from a unit | |
| 251 | + | |
| 252 | + * @return boolean | |
| 253 | + * True if the deletion was successful, else false if not or nonexistant. | |
| 254 | + */ | |
| 255 | + function removeGroupFromUnit($groupID) | |
| 256 | + { | |
| 257 | + global $default; | |
| 258 | + // create a connection | |
| 259 | + $sql = new Owl_DB; | |
| 260 | + | |
| 261 | + //do validation that groupid exists | |
| 262 | + $query = "SELECT * FROM $default->owl_groups_units_table WHERE group_id = $groupID"; | |
| 263 | + $result = $sql->query($query); | |
| 264 | + $row = $sql->num_rows($result); | |
| 265 | + | |
| 266 | + // check if result was found..0 if not | |
| 267 | + if($row == 0) | |
| 268 | + { | |
| 269 | + $default->log->debug("GroupManager::Group does not exist in the database<br>"); | |
| 270 | + return false; | |
| 271 | + } | |
| 272 | + | |
| 273 | + //if group id exists delete it from the users table | |
| 274 | + $query = "DELETE FROM $default->owl_groups_units_table WHERE group_id = $groupID"; | |
| 275 | + $result = $sql->query($query); | |
| 276 | + | |
| 277 | + if(!'result') | |
| 278 | + { | |
| 279 | + $default->log->debug ("GroupManager::Deletion unsuccessful<br>"); | |
| 280 | + return false; | |
| 281 | + } | |
| 282 | + else | |
| 283 | + { | |
| 284 | + $default->log->debug("GroupManager::Deletion from user_group_link table Successful<br>"); | |
| 285 | + return true; | |
| 286 | + } | |
| 287 | + | |
| 288 | + } | |
| 289 | + /* | |
| 290 | + * | |
| 291 | + * gets the id of a group using its name | |
| 292 | + * | |
| 293 | + * @param $groupName | |
| 294 | + * The GroupName for which we want its ID | |
| 295 | + * @return Integer | |
| 296 | + * The groupname's Id | |
| 297 | + */ | |
| 298 | + | |
| 299 | + function getGroupID($groupName) | |
| 300 | + { | |
| 301 | + global $default; | |
| 302 | + | |
| 303 | + $sql = new Owl_DB; | |
| 304 | + | |
| 305 | + // check that group exists if it does'nt return false | |
| 306 | + $query = "SELECT id FROM $default->owl_groups_table WHERE name = '" . $groupName . "'"; | |
| 307 | + $sql->query($query); | |
| 308 | + $rows = $sql->num_rows($sql); | |
| 309 | + // go into record set | |
| 310 | + $sql->next_record(); | |
| 311 | + | |
| 312 | + // store the id in a variable | |
| 313 | + $id = $sql->f("id"); | |
| 314 | + | |
| 315 | + // if no entry..username does not exist | |
| 316 | + if ($rows == 0) | |
| 317 | + { | |
| 318 | + // duplicate username | |
| 319 | + $default->errorMessage = "GroupManager::The Group " . $groupName . " does not exist<br>"; | |
| 320 | + $default->log->debug($default->errorMessage); | |
| 321 | + return false; | |
| 322 | + } | |
| 323 | + else | |
| 324 | + { | |
| 325 | + return $id; | |
| 326 | + } | |
| 327 | + } | |
| 328 | + | |
| 329 | + /* | |
| 330 | + * Function getGroupName($groupID) | |
| 331 | + * | |
| 332 | + * gets the id of a user using their username | |
| 333 | + * | |
| 334 | + * @param $username | |
| 335 | + * The username for which we want its ID | |
| 336 | + * @return char | |
| 337 | + * name of group | |
| 338 | + */ | |
| 339 | + | |
| 340 | + function getGroupName($groupID) | |
| 341 | + { | |
| 342 | + global $default; | |
| 343 | + | |
| 344 | + $sql = new Owl_DB; | |
| 345 | + | |
| 346 | + | |
| 347 | + // check that username exists if it does'nt return false | |
| 348 | + $query = "SELECT name FROM $default->owl_groups_table WHERE id = '" . $groupID . "'"; | |
| 349 | + $sql->query($query); | |
| 350 | + $rows = $sql->num_rows($sql); | |
| 351 | + // go into record set | |
| 352 | + $sql->next_record(); | |
| 353 | + | |
| 354 | + // store the id in a variable | |
| 355 | + $name = $sql->f("name"); | |
| 356 | + | |
| 357 | + // if no entry..group name does'nt exist | |
| 358 | + if ($rows == 0) | |
| 359 | + { | |
| 360 | + // duplicate username | |
| 361 | + $default->errorMessage = "GroupManager::The group does not exist<br>"; | |
| 362 | + $default->log->debug($default->errorMessage); | |
| 363 | + return false; | |
| 364 | + } | |
| 365 | + else | |
| 366 | + { | |
| 367 | + return $name; | |
| 368 | + } | |
| 369 | + } | |
| 370 | + | |
| 371 | + /* | |
| 372 | + * | |
| 373 | + * Gets the unit that the group belongs to | |
| 374 | + * | |
| 375 | + * @param $groupID | |
| 376 | + * The ID of the group | |
| 377 | + * @return Array | |
| 378 | + * array of unitID and name of the unit | |
| 379 | + */ | |
| 380 | + function getUnit($groupID) | |
| 381 | + { | |
| 382 | + global $default; | |
| 383 | + $unitinfo = array(); | |
| 384 | + $sql = new Owl_DB; | |
| 385 | + //$groupName = new GroupManager; | |
| 386 | + | |
| 387 | + | |
| 388 | + // check that group exists if it does'nt return false | |
| 389 | + $query = "SELECT unit_id FROM $default->owl_groups_units_table WHERE group_id = '" . $groupID . "'"; | |
| 390 | + $sql->query($query); | |
| 391 | + $rows = $sql->num_rows($sql); | |
| 392 | + | |
| 393 | + // if no entry..group does not belong to a unit | |
| 394 | + if ($rows == 0) | |
| 395 | + { | |
| 396 | + // duplicate username | |
| 397 | + $default->errorMessage = "GroupManager::The group does not belong to a unit<br>"; | |
| 398 | + $default->log->debug($default->errorMessage); | |
| 399 | + return false; | |
| 400 | + } | |
| 401 | + | |
| 402 | + | |
| 403 | + while($sql->next_record()) | |
| 404 | + { | |
| 405 | + $unitinfo[1] = array("id" => $sql->f("unit_id"), | |
| 406 | + "name" => $this->getUnitName($sql->f("unit_id")) // TODO change this to unit obj | |
| 407 | + ); | |
| 408 | + | |
| 409 | + } | |
| 410 | + | |
| 411 | + return $unitinfo; | |
| 412 | + } | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + /* | |
| 417 | + * | |
| 418 | + * gets the name of a unit using their username | |
| 419 | + * | |
| 420 | + * @param unitID | |
| 421 | + * The id of the unit who's name we want | |
| 422 | + * @return char | |
| 423 | + * name of unit | |
| 424 | + */ | |
| 425 | + | |
| 426 | + function getUnitName($unitID) | |
| 427 | + { | |
| 428 | + global $default; | |
| 429 | + | |
| 430 | + $sql = new Owl_DB; | |
| 431 | + | |
| 432 | + | |
| 433 | + // check that username exists if it does'nt return false | |
| 434 | + $query = "SELECT name FROM $default->owl_units_table WHERE id = '" . $unitID . "'"; | |
| 435 | + $sql->query($query); | |
| 436 | + $rows = $sql->num_rows($sql); | |
| 437 | + // go into record set | |
| 438 | + $sql->next_record(); | |
| 439 | + | |
| 440 | + // store the id in a variable | |
| 441 | + $name = $sql->f("name"); | |
| 442 | + | |
| 443 | + // if no entry..unit name does'nt exist | |
| 444 | + if ($rows == 0) | |
| 445 | + { | |
| 446 | + // duplicate username | |
| 447 | + $default->errorMessage = "GroupManager::The unit does not exist<br>"; | |
| 448 | + $default->log->debug($default->errorMessage); | |
| 449 | + return false; | |
| 450 | + } | |
| 451 | + else | |
| 452 | + { | |
| 453 | + return $name; | |
| 454 | + } | |
| 455 | + } | |
| 456 | + | |
| 457 | + } | |
| 458 | + | |
| 459 | +?> | |
| 0 | 460 | \ No newline at end of file | ... | ... |