Refactoring.
[timetracker.git] / WEB-INF / lib / ttTeamHelper.class.php
index e88492e..0d10a84 100644 (file)
@@ -71,10 +71,11 @@ class ttTeamHelper {
   // The getActiveUsers obtains all active users in a given team.
   static function getActiveUsers($options = null) {
     global $user;
+    global $i18n;
     $mdb2 = getConnection();
 
     if (isset($options['getAllFields']))
-      $sql = "select * from tt_users where team_id = $user->team_id and status = 1 order by upper(name)";
+      $sql = "select u.*, r.name as role_name, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.team_id = $user->team_id and u.status = 1 order by upper(u.name)";
     else
       $sql = "select id, name from tt_users where team_id = $user->team_id and status = 1 order by upper(name)";
     $res = $mdb2->query($sql);
@@ -82,6 +83,9 @@ class ttTeamHelper {
     if (is_a($res, 'PEAR_Error'))
       return false;
     while ($val = $res->fetchRow()) {
+      // Localize top manager role name, as it is not localized in db.
+      if ($val['rank'] == 512)
+        $val['role_name'] = $i18n->get('role.top_manager.label');
       $user_list[] = $val;
     }
 
@@ -99,29 +103,74 @@ class ttTeamHelper {
     return $user_list;
   }
 
-  // The getUsers obtains all active and inactive (but not deleted) users in a given team.
-  static function getUsers() {
+  // The swapRolesWith swaps existing user role with that of another user.
+  static function swapRolesWith($user_id) {
     global $user;
     $mdb2 = getConnection();
 
-    $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by upper(name)";
+    $sql = "select u.id, u.role_id from tt_users u left join tt_roles r on (u.role_id = r.id) where u.id = $user_id and u.team_id = $user->team_id and u.status = 1 and r.rank < $user->rank";
+    $res = $mdb2->query($sql);
+    if (is_a($res, 'PEAR_Error'))
+      return false;
+    $val = $res->fetchRow();
+    if (!$val['id'] || !$val['role_id'])
+      return false;
+
+    // Promote user.
+    $sql = "update tt_users set role_id = $user->role_id where id = $user_id and team_id = $user->team_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
+
+    // Demote self.
+    $role_id = $val['role_id'];
+    $sql = "update tt_users set role_id = $role_id where id = $user->id and team_id = $user->team_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
+
+    return true;
+  }
+
+  // The getUsersForSwap obtains all users a current user can swap roles with.
+  static function getUsersForSwap() {
+    global $user;
+    $mdb2 = getConnection();
+
+    $sql = "select u.id, u.name, r.rank, r.rights from tt_users u left join tt_roles r on (u.role_id = r.id) where u.team_id = $user->team_id and u.status = 1 and r.rank < $user->rank order by upper(u.name)";
     $res = $mdb2->query($sql);
     $user_list = array();
     if (is_a($res, 'PEAR_Error'))
       return false;
     while ($val = $res->fetchRow()) {
+      $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
+      if ($isClient)
+        continue; // Skip adding clients.
       $user_list[] = $val;
     }
 
     return $user_list;
   }
 
+    // The getUsers obtains all active and inactive (but not deleted) users in a given team.
+  static function getUsers() {
+    global $user;
+    $mdb2 = getConnection();
+    $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by upper(name)";
+    $res = $mdb2->query($sql);
+    $user_list = array();
+    if (is_a($res, 'PEAR_Error'))
+      return false;
+    while ($val = $res->fetchRow()) {
+      $user_list[] = $val;
+    }
+    return $user_list;
+  }
+
   // The getInactiveUsers obtains all inactive users in a given team.
   static function getInactiveUsers($team_id, $all_fields = false) {
     $mdb2 = getConnection();
 
     if ($all_fields)
-      $sql = "select * from tt_users where team_id = $team_id and status = 0 order by upper(name)";
+      $sql = "select u.*, r.name as role_name from tt_users u left join tt_roles r on (u.role_id = r.id) where u.team_id = $team_id and u.status = 0 order by upper(u.name)";
     else
       $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by upper(name)";
     $res = $mdb2->query($sql);
@@ -138,7 +187,6 @@ class ttTeamHelper {
   // The getAllUsers obtains all users in a given team.
   static function getAllUsers($team_id, $all_fields = false) {
     $mdb2 = getConnection();
-
     if ($all_fields)
       $sql = "select * from tt_users where team_id = $team_id order by upper(name)";
     else
@@ -263,6 +311,86 @@ class ttTeamHelper {
     return false;
   }
 
+  // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
+  // "Relevant" means that client roles are filtered out if Client plugin is disabled.
+  static function getActiveRolesForUser()
+  {
+    global $user;
+    $result = array();
+    $mdb2 = getConnection();
+
+    $sql = "select id, name, description, rank, rights from tt_roles where team_id = $user->team_id and rank < $user->rank and status = 1 order by rank";
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
+        if ($val['is_client'] && !$user->isPluginEnabled('cl'))
+          continue; // Skip adding a client role.
+        $result[] = $val;
+      }
+    }
+    return $result;
+  }
+
+  // getActiveRoles - returns an array of active roles for team.
+  static function getActiveRoles($team_id)
+  {
+    $result = array();
+    $mdb2 = getConnection();
+
+    $sql = "select id, name, description, rank, rights from tt_roles where team_id = $team_id and status = 1 order by rank";
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
+        $result[] = $val;
+      }
+    }
+    return $result;
+  }
+
+  // getInactiveRoles - returns an array of inactive roles for team.
+  static function getInactiveRoles($team_id)
+  {
+    $result = array();
+    $mdb2 = getConnection();
+
+    $sql = "select id, name, rank, description from tt_roles
+      where team_id = $team_id and status = 0 order by rank";
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $result[] = $val;
+      }
+    }
+    return $result;
+  }
+
+  // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
+  // "Relevant" means that client roles are filtered out if Client plugin is disabled.
+  static function getInactiveRolesForUser()
+  {
+    global $user;
+    $result = array();
+    $mdb2 = getConnection();
+
+    $sql = "select id, name, description, rank, rights from tt_roles where team_id = $user->team_id and rank < $user->rank and status = 0 order by rank";
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
+        if ($val['is_client'] && !$user->isPluginEnabled('cl'))
+          continue; // Skip adding a client role.
+        $result[] = $val;
+      }
+    }
+    return $result;
+  }
+
   // The getActiveClients returns an array of active clients for team.
   static function getActiveClients($team_id, $all_fields = false)
   {
@@ -334,7 +462,7 @@ class ttTeamHelper {
     $result = array();
     $mdb2 = getConnection();
 
-    if (ROLE_CLIENT == $user->role && $user->client_id)
+    if ($user->isClient())
       $client_part = " and i.client_id = $user->client_id";
 
     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
@@ -560,24 +688,6 @@ class ttTeamHelper {
     return false;
   }
 
-  // The getTeams function returns an array of all active teams on the server.
-  static function getTeams() {
-    $result = array();
-    $mdb2 = getConnection();
-
-    $sql =  "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
-    $res = $mdb2->query($sql);
-    $result = array();
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-       $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
-        $result[] = $val;
-      }
-      return $result;
-    }
-    return false;
-  }
-
   // The markDeleted function marks the team and everything in it as deleted.
   static function markDeleted($team_id) {
 
@@ -592,6 +702,11 @@ class ttTeamHelper {
 
     $mdb2 = getConnection();
 
+    // Mark roles deleted.
+    $sql = "update tt_roles set status = NULL where team_id = $team_id";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error')) return false;
+
     // Mark projects deleted.
     $sql = "update tt_projects set status = NULL where team_id = $team_id";
     $affected = $mdb2->exec($sql);
@@ -620,10 +735,10 @@ class ttTeamHelper {
     $result = array();
     $mdb2 = getConnection();
 
-    $role_manager = ROLE_MANAGER;
     $sql = "select t.name as team_name, u.id as manager_id, u.name as manager_name, u.login as manager_login, u.email as manager_email
       from tt_teams t
-      inner join tt_users u on (u.team_id = t.id and u.role = $role_manager)
+      inner join tt_users u on (u.team_id = t.id)
+      inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
       where t.id = $team_id";
 
     $res = $mdb2->query($sql);
@@ -734,6 +849,7 @@ class ttTeamHelper {
   // The update function updates team information.
   static function update($team_id, $fields)
   {
+    global $user;
     $mdb2 = getConnection();
     $name_part = 'name = '.$mdb2->quote($fields['name']);
     $currency_part = '';
@@ -764,10 +880,11 @@ class ttTeamHelper {
     if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
     if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
     if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
+    $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
 
     $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
       $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
-      $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
+      $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part $modified_part where id = $team_id";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error')) return false;
 
@@ -779,10 +896,10 @@ class ttTeamHelper {
     $inactive_teams = array();
     $mdb2 = getConnection();
 
-    // Get all team ids for teams created or modified more than 6 months ago.
+    // Get all team ids for teams created or modified more than 8 months ago.
     // $ts = date('Y-m-d', strtotime('-1 year'));
-    $ts = date('Y-m-d', strtotime('-6 month'));
-    $sql =  "select id from tt_teams where timestamp < '$ts' order by id";
+    $ts = $mdb2->quote(date('Y-m-d', strtotime('-8 month')));
+    $sql =  "select id from tt_teams where created < $ts and (modified is null or modified < $ts) order by id";
     $res = $mdb2->query($sql);
 
     $count = 0;
@@ -818,7 +935,7 @@ class ttTeamHelper {
 
     $count = 0;
     $ts = date('Y-m-d', strtotime('-2 years'));
-    $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
+    $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       if ($val = $res->fetchRow()) {
@@ -833,7 +950,7 @@ class ttTeamHelper {
       // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
       $count_last_year = 0;
       $ts = date('Y-m-d', strtotime('-1 year'));
-      $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
+      $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
       $res = $mdb2->query($sql);
       if (!is_a($res, 'PEAR_Error')) {
         if ($val = $res->fetchRow()) {
@@ -979,7 +1096,7 @@ class ttTeamHelper {
   static function enablePlugin($plugin, $enable = true)
   {
     global $user;
-    if (!$user->canManageTeam())
+    if (!$user->can('manage_features'))
       return false;
 
     $plugin_array = explode(',', $user->plugins);