+
+ // get - gets details of a role identified by its id.
+ static function get($id)
+ {
+ global $user;
+
+ $mdb2 = getConnection();
+
+ $sql = "select id, name, description, rank, rights, status from tt_roles
+ where id = $id and team_id = $user->team_id and (status = 0 or status = 1)";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['id'] != '') {
+ return $val;
+ } else
+ return false;
+ }
+ return false;
+ }
+
+ // The getRoleByName looks up a role by name.
+ static function getRoleByName($role_name) {
+
+ $mdb2 = getConnection();
+ global $user;
+
+ $sql = "select id from tt_roles where team_id = $user->team_id and name = ".
+ $mdb2->quote($role_name)." and (status = 1 or status = 0)";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['id'])
+ return $val;
+ }
+ return false;
+ }
+
+ // The getTopManagerRoleID obtains an ID for top manager role.
+ static function getTopManagerRoleID() {
+ $mdb2 = getConnection();
+
+ $sql = "select id from tt_roles where team_id = 0 and rank = 512";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['id'])
+ return $val['id'];
+ }
+ return false;
+ }
+
+ // The getLegacyRole obtains a legacy role value for a role_id.
+ // This is a temporary function to allow usage of both old and new roles
+ // while new role code is being written and deployed.
+ static function getLegacyRole($role_id) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $sql = "select rank from tt_roles where team_id = $user->team_id and id = $role_id";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['rank']) {
+ $rank = $val['rank'];
+ if ($rank >= ROLE_MANAGER)
+ return ROLE_MANAGER;
+ else if ($rank >= ROLE_COMANAGER)
+ return ROLE_COMANAGER;
+ else if ($rank >= ROLE_CLIENT)
+ return ROLE_CLIENT;
+ else
+ return ROLE_USER;
+ }
+ }
+ return false;
+ }
+
+ // isClientRole determines if the role is a "client" role.
+ // This simply means the role has no "track_own_time" right.
+ static function isClientRole($role_id) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $sql = "select rights from tt_roles where team_id = $user->team_id and id = $role_id";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['rights']) {
+ return !in_array('track_own_time', explode(',', $val['rights']));
+ }
+ }
+ return false;
+ }
+
+ // getRoleByRank looks up a role by its rank.
+ static function getRoleByRank($rank) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $rank = (int) $rank; // Cast to int just in case for better security.
+
+ $sql = "select id from tt_roles where team_id = $user->team_id and rank = $rank and (status = 1 or status = 0)";
+ $res = $mdb2->query($sql);
+
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['id'])
+ return $val['id'];
+ }
+ return false;
+ }
+
+ // update function updates a role in the database.
+ static function update($fields) {
+ global $user;
+ $mdb2 = getConnection();
+
+ $id = (int)$fields['id'];
+ if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
+ if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
+ if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
+ if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
+ $parts = trim($name_part.$descr_part.$status_part.$rights_part, ',');
+ $sql = "update tt_roles set $parts where id = $id and team_id = $user->team_id";
+ $affected = $mdb2->exec($sql);
+ return (!is_a($affected, 'PEAR_Error'));
+ }
+
+ // delete - marks the role as deleted.
+ static function delete($role_id) {
+ global $user;
+
+ $mdb2 = getConnection();
+
+ // Mark the task as deleted.
+ $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
+ $affected = $mdb2->exec($sql);
+ return (!is_a($affected, 'PEAR_Error'));
+ }
+