2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 import('ttConfigHelper');
 
  30 import('ttGroupHelper');
 
  33   var $login = null;            // User login.
 
  34   var $name = null;             // User name.
 
  35   var $id = null;               // User id.
 
  36   var $org_id = null;           // Organization id.
 
  37   var $group_id = null;         // Group id.
 
  38   var $role_id = null;          // Role id.
 
  39   var $role_name = null;        // Role name.
 
  40   var $rank = null;             // User role rank.
 
  41   var $client_id = null;        // Client id for client user role.
 
  42   var $behalf_id = null;        // User id, on behalf of whom we are working.
 
  43   var $behalf_group_id = null;  // Group id, on behalf of which we are working.
 
  44   var $behalf_name = null;      // User name, on behalf of whom we are working.
 
  45   var $group_name = null;       // Group name.
 
  46   var $behalf_group_name = null;// Group name, on behalf of which we are working.
 
  47   var $email = null;            // User email.
 
  48   var $lang = null;             // Language.
 
  49   var $decimal_mark = null;     // Decimal separator.
 
  50   var $date_format = null;      // Date format.
 
  51   var $time_format = null;      // Time format.
 
  52   var $week_start = 0;          // Week start day.
 
  53   var $show_holidays = 0;       // Whether to show holidays in calendar.
 
  54   var $tracking_mode = 0;       // Tracking mode.
 
  55   var $project_required = 0;    // Whether project selection is required on time entires.
 
  56   var $task_required = 0;       // Whether task selection is required on time entires.
 
  57   var $record_type = 0;         // Record type (duration vs start and finish, or both).
 
  58   var $punch_mode = 0;          // Whether punch mode is enabled for user.
 
  59   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
 
  60   var $future_entries = 0;      // Whether to allow creating future entries.
 
  61   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
 
  62   var $bcc_email = null;        // Bcc email.
 
  63   var $allow_ip = null;         // Specification from where user is allowed access.
 
  64   var $password_complexity = null; // Password complexity example.
 
  65   var $currency = null;         // Currency.
 
  66   var $plugins = null;          // Comma-separated list of enabled plugins.
 
  67   var $config = null;           // Comma-separated list of miscellaneous config options.
 
  68   var $custom_logo = 0;         // Whether to use a custom logo for group.
 
  69   var $lock_spec = null;        // Cron specification for record locking.
 
  70   var $workday_minutes = 480;   // Number of work minutes in a regular day.
 
  71   var $rights = array();        // An array of user rights such as 'track_own_time', etc.
 
  72   var $is_client = false;       // Whether user is a client as determined by missing 'track_own_time' right.
 
  73   var $minutes_in_unit = 15;    // Number of minutes in unit for Work units plugin.
 
  74   var $first_unit_threshold = 0;// Threshold for 1st unit for Work units plugin.
 
  75   var $unit_totals_only = 0;    // Totals only option for the Work units plugin.
 
  78   function __construct($login, $id = null) {
 
  79     if (!$login && !$id) {
 
  80       // nothing to initialize
 
  84     $mdb2 = getConnection();
 
  86     $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id, u.email,
 
  87       g.org_id, g.name as group_name, g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
 
  88       g.tracking_mode, g.project_required, g.task_required, g.record_type,
 
  89       g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
 
  90       FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
 
  94       $sql .= "u.login = ".$mdb2->quote($login);
 
  95     $sql .= " AND u.status = 1";
 
  97     $res = $mdb2->query($sql);
 
  98     if (is_a($res, 'PEAR_Error')) {
 
 102     $val = $res->fetchRow();
 
 103     if ($val['id'] > 0) {
 
 104       $this->login = $val['login'];
 
 105       $this->name = $val['name'];
 
 106       $this->id = $val['id'];
 
 107       $this->org_id = $val['org_id'];
 
 108       $this->group_id = $val['group_id'];
 
 109       $this->role_id = $val['role_id'];
 
 110       $this->role_name = $val['role_name'];
 
 111       $this->rights = explode(',', $val['rights']);
 
 112       $this->is_client = !in_array('track_own_time', $this->rights);
 
 113       $this->rank = $val['rank'];
 
 114       $this->client_id = $val['client_id'];
 
 115       $this->email = $val['email'];
 
 116       $this->lang = $val['lang'];
 
 117       $this->decimal_mark = $val['decimal_mark'];
 
 118       $this->date_format = $val['date_format'];
 
 119       $this->time_format = $val['time_format'];
 
 120       $this->week_start = $val['week_start'];
 
 121       $this->tracking_mode = $val['tracking_mode'];
 
 122       $this->project_required = $val['project_required'];
 
 123       $this->task_required = $val['task_required'];
 
 124       $this->record_type = $val['record_type'];
 
 125       $this->bcc_email = $val['bcc_email'];
 
 126       $this->allow_ip = $val['allow_ip'];
 
 127       $this->password_complexity = $val['password_complexity'];
 
 128       $this->group_name = $val['group_name'];
 
 129       $this->currency = $val['currency'];
 
 130       $this->plugins = $val['plugins'];
 
 131       $this->lock_spec = $val['lock_spec'];
 
 132       $this->workday_minutes = $val['workday_minutes'];
 
 133       $this->custom_logo = $val['custom_logo'];
 
 135       $this->config = $val['config'];
 
 136       $config = new ttConfigHelper($this->config);
 
 137       // Set user config options.
 
 138       $this->show_holidays = $config->getDefinedValue('show_holidays');
 
 139       $this->punch_mode = $config->getDefinedValue('punch_mode');
 
 140       $this->allow_overlap = $config->getDefinedValue('allow_overlap');
 
 141       $this->future_entries = $config->getDefinedValue('future_entries');
 
 142       $this->uncompleted_indicators = $config->getDefinedValue('uncompleted_indicators');
 
 143       if ($this->isPluginEnabled('wu')) {
 
 144         $minutes_in_unit = $config->getIntValue('minutes_in_unit');
 
 145         if ($minutes_in_unit) $this->minutes_in_unit = $minutes_in_unit;
 
 146         $first_unit_threshold = $config->getIntValue('1st_unit_threshold');
 
 147         if ($first_unit_threshold) $this->first_unit_threshold = $first_unit_threshold;
 
 148         $this->unit_totals_only = $config->getDefinedValue('unit_totals_only');
 
 151       // Set "on behalf" id and name (user).
 
 152       if (isset($_SESSION['behalf_id'])) {
 
 153           $this->behalf_id = $_SESSION['behalf_id'];
 
 154           $this->behalf_name = $_SESSION['behalf_name'];
 
 156       // Set "on behalf" id and name (group).
 
 157       if (isset($_SESSION['behalf_group_id'])) {
 
 158           $this->behalf_group_id = $_SESSION['behalf_group_id'];
 
 159           $this->behalf_group_name = $_SESSION['behalf_group_name'];
 
 164   // The getActiveUser returns user id on behalf of whom the current user is operating.
 
 165   function getActiveUser() {
 
 166     return ($this->behalf_id ? $this->behalf_id : $this->id);
 
 169   // The getActiveGroup returns group id on behalf of which the current user is operating.
 
 170   function getActiveGroup() {
 
 171     return ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
 
 174   // can - determines whether user has a right to do something.
 
 175   function can($do_something) {
 
 176     return in_array($do_something, $this->rights);
 
 179   // isClient - determines whether current user is a client.
 
 180   function isClient() {
 
 181     return $this->is_client;
 
 184   // isPluginEnabled checks whether a plugin is enabled for user.
 
 185   function isPluginEnabled($plugin)
 
 187     return in_array($plugin, explode(',', $this->plugins));
 
 190   // getAssignedProjects - returns an array of assigned projects.
 
 191   function getAssignedProjects()
 
 194     $mdb2 = getConnection();
 
 196     $group_id = $this->behalf_group_id ? $this->behalf_group_id : $this->group_id;
 
 197     // Do a query with inner join to get assigned projects.
 
 198     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
 
 199       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
 
 200       where p.group_id = $group_id and p.status = 1 order by p.name";
 
 201     $res = $mdb2->query($sql);
 
 202     if (!is_a($res, 'PEAR_Error')) {
 
 203       while ($val = $res->fetchRow()) {
 
 210   // getAssignedTasks - returns an array of assigned tasks.
 
 211   function getAssignedTasks()
 
 213     // Start with projects;
 
 214     $projects = $this->getAssignedProjects();
 
 215     if (!$projects) return false;
 
 217     // Build an array of task ids.
 
 219     foreach($projects as $project) {
 
 220       $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
 
 221       $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
 
 223     if (!$task_ids) return false;
 
 225     // Get task descriptions.
 
 227     $mdb2 = getConnection();
 
 228     $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
 
 230     $sql = "select id, name, description from tt_tasks".
 
 231       " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
 
 232     $res = $mdb2->query($sql);
 
 233     if (!is_a($res, 'PEAR_Error')) {
 
 234       while ($val = $res->fetchRow()) {
 
 241   // getAssignedClients - returns an array of clients assigned to own projects.
 
 242   function getAssignedClients()
 
 244     // Start with projects;
 
 245     $projects = $this->getAssignedProjects();
 
 246     if (!$projects) return false;
 
 247     $assigned_project_ids = array();
 
 248     foreach($projects as $project) {
 
 249       $assigned_project_ids[] = $project['id'];
 
 252     $mdb2 = getConnection();
 
 254     // Get active clients for group.
 
 256     $sql = "select id, name, address, projects from tt_clients where group_id = $this->group_id and status = 1";
 
 257     $res = $mdb2->query($sql);
 
 258     if (!is_a($res, 'PEAR_Error')) {
 
 259       while ($val = $res->fetchRow()) {
 
 260         $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
 
 261         if (array_intersect($assigned_project_ids, $client_project_ids))
 
 262           $clients[] = $val; // Add client if one of user projects is a client project, too.
 
 268   // isDateLocked checks whether a specifc date is locked for modifications.
 
 269   function isDateLocked($date)
 
 271     if (!$this->isPluginEnabled('lk'))
 
 272       return false; // Locking feature is disabled.
 
 274     if (!$this->lock_spec)
 
 275       return false; // There is no lock specification.
 
 277     if (!$this->behalf_id && $this->can('override_own_date_lock'))
 
 278       return false; // User is working as self and can override own date lock.
 
 280     if ($this->behalf_id && $this->can('override_date_lock'))
 
 281       return false; // User is working on behalf of someone else and can override date lock.
 
 283     require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
 
 284     require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
 
 286     // Calculate the last occurrence of a lock.
 
 287     $last = tdCron::getLastOccurrence($this->lock_spec, time());
 
 288     $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
 
 289     if ($date->before($lockdate))
 
 295   // canOverridePunchMode checks whether a user can override punch mode in a situation.
 
 296   function canOverridePunchMode()
 
 298     if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
 
 299       return false; // User is working as self and cannot override for self.
 
 301     if ($this->behalf_id && !$this->can('override_punch_mode'))
 
 302       return false; // User is working on behalf of someone else and cannot override.
 
 307   // getUsers obtains users in a group, as specififed by options.
 
 308   function getUsers($options) {
 
 310     $mdb2 = getConnection();
 
 312     $skipClients = !isset($options['include_clients']);
 
 313     $includeSelf = isset($options['include_self']);
 
 314     $group_id = isset($options['group_id']) ? $options['group_id'] : $this->group_id;
 
 316     $select_part = 'select u.id, u.name';
 
 317     if (isset($options['include_login'])) $select_part .= ', u.login';
 
 318     if (!isset($options['include_clients'])) $select_part .= ', r.rights';
 
 319     if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
 
 321     $from_part = ' from tt_users u';
 
 324     if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
 
 325         $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
 
 327     $where_part = " where u.org_id = $this->org_id and u.group_id = $group_id";
 
 328     if (isset($options['status']))
 
 329       $where_part .= ' and u.status = '.(int)$options['status'];
 
 331       $where_part .= ' and u.status is not null';
 
 333       $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
 
 335       if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
 
 338     $order_part = " order by upper(u.name)";
 
 340     $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
 
 341     $res = $mdb2->query($sql);
 
 342     $user_list = array();
 
 343     if (is_a($res, 'PEAR_Error'))
 
 346     while ($val = $res->fetchRow()) {
 
 348         $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
 
 350           continue; // Skip adding clients.
 
 355     if (isset($options['self_first'])) {
 
 356       // Put own entry at the front.
 
 357       $cnt = count($user_list);
 
 358       for($i = 0; $i < $cnt; $i++) {
 
 359         if ($user_list[$i]['id'] == $this->id) {
 
 360           $self = $user_list[$i]; // Found self.
 
 361           array_unshift($user_list, $self); // Put own entry at the front.
 
 362           array_splice($user_list, $i+1, 1); // Remove duplicate.
 
 369   // getGroups obtains an array consisting of:
 
 370   // - A parent group (..) of a currently selected group, if available.
 
 371   // - A currently selected group (.) represented by $behalf_group_id.
 
 372   // - All subgroups (only immediate children) of a currently selected group.
 
 373   function getGroups() {
 
 374     $mdb2 = getConnection();
 
 376     $selected_group_id = ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
 
 377     $selected_group_name = ($this->behalf_group_id ? $this->behalf_group_name : $this->group_name);
 
 379     // Start with parent group.
 
 380     if ($selected_group_id != $this->group_id) {
 
 381       // We are in one of subgroups, and a parent exists.
 
 382       // Get parent group info.
 
 383       $sql = "select parent_id from tt_groups where org_id = $this->org_id and id = $selected_group_id and status = 1";
 
 384       $res = $mdb2->query($sql);
 
 385       if (!is_a($res, 'PEAR_Error')) {
 
 386         $val = $res->fetchRow();
 
 387         $parent_id = $val['parent_id'];
 
 389           // Get parent group name.
 
 390           $sql = "select name from tt_groups where org_id = $this->org_id and id = $parent_id and status = 1";
 
 391           $res = $mdb2->query($sql);
 
 392           if (!is_a($res, 'PEAR_Error')) {
 
 393             $val = $res->fetchRow();
 
 394             $groups[] = array('id'=>$parent_id,'name'=>$val['name']);
 
 400     // Add current group.
 
 401     $groups[] = array('id'=>$selected_group_id,'name'=>$selected_group_name);
 
 404     $sql = "select id, name from tt_groups where org_id = $this->org_id and parent_id = $selected_group_id and status = 1";
 
 406     $res = $mdb2->query($sql);
 
 407     if (!is_a($res, 'PEAR_Error')) {
 
 408       while ($val = $res->fetchRow()) {
 
 415   // getSubgroups obtains a list of immediate subgroups.
 
 416   function getSubgroups($group_id = null) {
 
 417     $mdb2 = getConnection();
 
 419     if (!$group_id) $group_id = $this->getActiveGroup();
 
 421     $sql = "select id, name, description from tt_groups where org_id = $this->org_id".
 
 422       " and parent_id = $group_id and status is not null order by upper(name)";
 
 423     $res = $mdb2->query($sql);
 
 424     if (!is_a($res, 'PEAR_Error')) {
 
 425       while ($val = $res->fetchRow()) {
 
 432   // getUser function is used to manage users in group and returns user details.
 
 433   // At the moment, the function is used for user edits and deletes.
 
 434   function getUser($user_id) {
 
 435     if (!$this->can('manage_users')) return false;
 
 437     $mdb2 = getConnection();
 
 439     $sql =  "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.email from tt_users u".
 
 440             " left join tt_roles r on (u.role_id = r.id)".
 
 441             " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
 
 442             " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
 
 443     $res = $mdb2->query($sql);
 
 444     if (!is_a($res, 'PEAR_Error')) {
 
 445       $val = $res->fetchRow();
 
 451   // checkBehalfId checks whether behalf_id is appropriate.
 
 452   // On behalf user must be active and have lower rank if the user is from home group,
 
 454   // - subgroup must ve valid;
 
 455   // - user should be a member of it.
 
 456   function checkBehalfId() {
 
 457     if (!$this->behalf_group_id) {
 
 458       // Checking user from home group.
 
 459       $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
 
 460       $users = $this->getUsers($options);
 
 461       foreach($users as $one_user) {
 
 462         if ($one_user['id'] == $this->behalf_id)
 
 466       // Checking user from a subgroup.
 
 467       $group_id = $this->behalf_group_id;
 
 468       if (!$this->isSubgroupValid($group_id))
 
 471       // So far, so good. Check user now.
 
 472       $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>MAX_RANK);
 
 473       $users = $this->getUsers($options);
 
 474       foreach($users as $one_user) {
 
 475         if ($one_user['id'] == $this->behalf_id)
 
 482   // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
 
 485   // Needed for situations when user does not have do_own_something right.
 
 486   // Example: has view_charts but does not have view_own_charts.
 
 487   // In this case we still allow access to charts, but set behalf_id to someone else.
 
 488   // Another example: working in a subgroup on behalf of someone else.
 
 489   function adjustBehalfId() {
 
 490     $group_id = $this->behalf_group_id ? $this->behalf_group_id : $this->group_id;
 
 491     $rank = $this->getMaxRankForGroup($group_id);
 
 493     // Adjust to first found user in group.
 
 494     $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank);
 
 495     $users = $this->getUsers($options);
 
 496     foreach($users as $one_user) {
 
 497       // Fake loop to access first element.
 
 498       $this->behalf_id = $one_user['id'];
 
 499       $this->behalf_name = $one_user['name'];
 
 500       $_SESSION['behalf_id'] = $this->behalf_id;
 
 501       $_SESSION['behalf_name'] = $this->behalf_name;
 
 507   // updateGroup updates group information with new data.
 
 508   function updateGroup($fields) {
 
 509     if (!($this->can('manage_basic_settings') ||
 
 510       $this->can('manage_advanced_settings') ||
 
 511       $this->can('manage_features'))) return false;
 
 512     // TODO: update the above for subgroup updates.
 
 514     $group_id = $fields['group_id'];
 
 515     if ($group_id && !$this->isGroupValid($group_id)) return false;
 
 517     $mdb2 = getConnection();
 
 518     if (!$group_id) $group_id = $this->getActiveGroup();
 
 520     if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
 
 521     if (isset($fields['description'])) $description_part = ', description = '.$mdb2->quote($fields['description']);
 
 522     if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
 
 523     if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
 
 524     if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
 
 525     if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
 
 526     if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
 
 527     if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
 
 528     if (isset($fields['tracking_mode'])) {
 
 529       $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
 
 530       $project_required_part = ' , project_required = '.(int) $fields['project_required'];
 
 531       $task_required_part = ' , task_required = '.(int) $fields['task_required'];
 
 533     if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
 
 534     if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
 
 535     if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
 
 536     if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
 
 537     if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
 
 538     if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
 
 539     if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
 
 540     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
 
 542     $parts = trim($name_part.$description_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
 
 543       $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
 
 544       $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$workday_minutes_part.$modified_part, ',');
 
 546     $sql = "update tt_groups set $parts where id = $group_id and org_id = $this->org_id";
 
 547     $affected = $mdb2->exec($sql);
 
 548     if (is_a($affected, 'PEAR_Error')) return false;
 
 553   // markUserDeleted marks a user in group as deleted.
 
 554   function markUserDeleted($user_id) {
 
 555     if (!$this->can('manage_users') || $this->id == $user_id)
 
 558     // Make sure we operate on a legit user.
 
 559     $user_details = $this->getUser($user_id);
 
 560     if (!$user_details) return false;
 
 562     $mdb2 = getConnection();
 
 564     // Mark user to project binds as deleted.
 
 565     $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
 
 566     $affected = $mdb2->exec($sql);
 
 567     if (is_a($affected, 'PEAR_Error'))
 
 570     // Mark user favorite reports as deleted.
 
 571     $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
 
 572     $affected = $mdb2->exec($sql);
 
 573     if (is_a($affected, 'PEAR_Error'))
 
 576     // Mark user as deleted.
 
 577     $sql = "update tt_users set status = NULL where id = $user_id and group_id = ".$this->group_id;
 
 578     $affected = $mdb2->exec($sql);
 
 579     if (is_a($affected, 'PEAR_Error'))
 
 585   // enablePlugin either enables or disables a specific plugin for group.
 
 586   function enablePlugin($plugin, $enable = true)
 
 588     if (!$this->can('manage_advanced_settings'))
 
 589       return false; // Note: enablePlugin is currently only used on week_view.php.
 
 590                     // So, it's not really a plugin we are enabling, but rather week view display options.
 
 591                     // Therefore, a check for manage_advanced_settings, not manage_features.
 
 593     $plugin_array = explode(',', $this->plugins);
 
 594     if ($enable && !in_array($plugin, $plugin_array))
 
 595       $plugin_array[] = $plugin; // Add plugin to array.
 
 597     if (!$enable && in_array($plugin, $plugin_array)) {
 
 598       $key = array_search($plugin, $plugin_array);
 
 600         unset($plugin_array[$key]); // Remove plugin from array.
 
 603     $plugins = implode(',', $plugin_array);
 
 604     if ($plugins != $this->plugins) {
 
 605       if (!$this->updateGroup(array('plugins' => $plugins)))
 
 607       $this->plugins = $plugins;
 
 613   // isGroupValid determines if a group is valid for user.
 
 614   function isGroupValid($group_id) {
 
 615     if ($group_id == $this->group_id)
 
 618       return $this->isSubgroupValid($group_id);
 
 621   // isSubgroupValid determines if a subgroup is valid for user.
 
 622   // A subgroup is valid if:
 
 623   //   - user can manage_subgroups;
 
 624   //   - subgroup is either a direct child of user group, or "on the path"
 
 625   //   to it (grand-child, etc.).
 
 626   function isSubgroupValid($subgroup_id) {
 
 627     if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
 
 629     $current_group_id = $subgroup_id;
 
 630     while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
 
 631       if ($parent_group_id == $this->group_id) {
 
 632         return true; // Found it.
 
 634       $current_group_id = $parent_group_id;
 
 639   // getMaxRankForGroup determines effective user rank for a user in a given group.
 
 640   // For home group it is the existing user rank (as per role) minus 1.
 
 641   // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
 
 642   function getMaxRankForGroup($group_id) {
 
 644     $max_rank = 0; // Start safely.
 
 645     if ($this->group_id == $group_id) {
 
 646       $max_rank = $this->rank - 1;
 
 650     if ($this->isSubgroupValid($group_id))
 
 651       $max_rank = MAX_RANK;
 
 656   // getUserPartForHeader constructs a string for user to display on pages header.
 
 657   // It changes with "on behalf" attributes for both user and group.
 
 658   function getUserPartForHeader() {
 
 660     if (!$this->id) return null;
 
 662     $user_part = htmlspecialchars($this->name);
 
 663     $user_part .= ' - '.htmlspecialchars($this->role_name);
 
 664     if ($this->behalf_id) {
 
 665       $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
 
 667     if ($this->behalf_group_id) {
 
 668       $user_part .= ',  <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_group_name).'</span>';
 
 670       if ($this->group_name) // Note: we did not require group names in the past.
 
 671         $user_part .= ', '.$this->group_name;
 
 676   // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
 
 677   function setOnBehalfGroup($group_id) {
 
 679     // Unset things first.
 
 680     $this->behalf_group_id = null;
 
 681     $this->behalf_group_name = null;
 
 682     unset($_SESSION['behalf_group_id']);
 
 683     unset($_SESSION['behalf_group_name']);
 
 685     // Do not do anything if we don't have rights.
 
 686     if (!$this->can('manage_subgroups')) return;
 
 688     // No need to set if the group is our home group.
 
 689     if ($group_id == $this->group_id) return;
 
 691     // No need to set if the subgroup is not valid.
 
 692     if (!$this->isSubgroupValid($group_id)) return;
 
 694     // We are good to set on behalf group.
 
 695     $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
 
 696     $_SESSION['behalf_group_id'] = $group_id;
 
 697     $_SESSION['behalf_group_name'] = $onBehalfGroupName;
 
 698     $this->behalf_group_id = $group_id;
 
 699     $this->behalf_group_name = $onBehalfGroupName;
 
 701     // Question remains whether or not we need to adjust on behalf user.
 
 702     // Adjusting for now. Test it and redesign if necessary.
 
 703     unset($_SESSION['behalf_id']);
 
 704     unset($_SESSION['behalf_name']);
 
 705     $this->adjustBehalfId();