Esempio n. 1
0
 /**
  * Returns the data used by the weekly report.
  *
  * @param string $usr_id The ID of the user this report is for.
  * @param int $prj_id The project id
  * @param string|DateTime $start The start date of this report.
  * @param string|DateTime $end The end date of this report.
  * @param array $options extra options for report:
  * - $separate_closed If closed issues should be separated from other issues.
  * - $ignore_statuses If issue status changes should be ignored in report.
  * - $separate_not_assigned_to_user Separate Issues Not Assigned to User
  * - $show_per_issue Add time spent on issue to issues
  * - $separate_no_time Separate No time spent issues
  * @return array An array of data containing all the elements of the weekly report.
  */
 public static function getWeeklyReport($usr_id, $prj_id, $start, $end, $options = array())
 {
     // figure out timezone
     $user_prefs = Prefs::get($usr_id);
     $tz = $user_prefs['timezone'];
     // if start or end is string, convert assume min and max date are specified
     if (!$start instanceof DateTime) {
         $start = Date_Helper::getDateTime($start, $tz)->setTime(0, 0, 0);
     }
     if (!$end instanceof DateTime) {
         $end = Date_Helper::getDateTime($end, $tz)->setTime(23, 59, 59);
     }
     $start_ts = Date_Helper::getSqlDateTime($start);
     $end_ts = Date_Helper::getSqlDateTime($end);
     $time_tracking = Time_Tracking::getSummaryByUser($usr_id, $prj_id, $start_ts, $end_ts);
     // replace spaces in index with _ and calculate total time
     $total_time = 0;
     foreach ($time_tracking as $category => $data) {
         unset($time_tracking[$category]);
         $time_tracking[str_replace(' ', '_', $category)] = $data;
         $total_time += $data['total_time'];
     }
     // get count of issues assigned in week of report.
     $stmt = 'SELECT
                 COUNT(*)
              FROM
                 {{%issue}},
                 {{%issue_user}},
                 {{%status}}
              WHERE
                 iss_id = isu_iss_id AND
                 iss_sta_id = sta_id AND
                 isu_usr_id = ? AND
                 iss_prj_id = ? AND
                 isu_assigned_date BETWEEN ? AND ?';
     $params = array($usr_id, Auth::getCurrentProject(), $start_ts, $end_ts);
     try {
         $newly_assigned = DB_Helper::getInstance()->getOne($stmt, $params);
     } catch (DbException $e) {
         $newly_assigned = null;
     }
     $email_count = array('associated' => Support::getSentEmailCountByUser($usr_id, $start_ts, $end_ts, true), 'other' => Support::getSentEmailCountByUser($usr_id, $start_ts, $end_ts, false));
     $htt_exclude = array();
     if (!empty($options['ignore_statuses'])) {
         $htt_exclude[] = 'status_changed';
         $htt_exclude[] = 'status_auto_changed';
         $htt_exclude[] = 'remote_status_change';
     }
     $issue_list = History::getTouchedIssuesByUser($usr_id, $prj_id, $start_ts, $end_ts, $htt_exclude);
     $issues = array('no_time' => array(), 'not_mine' => array(), 'closed' => array(), 'other' => array());
     // organize issues into categories
     if ($issue_list) {
         if (!empty($options['show_per_issue']) || !empty($options['separate_no_time'])) {
             Time_Tracking::fillTimeSpentByIssueAndTime($issue_list, $usr_id, $start_ts, $end_ts);
         }
         foreach ($issue_list as $row) {
             if (!empty($row['iss_customer_id']) && CRM::hasCustomerIntegration($row['iss_prj_id'])) {
                 $row['customer_name'] = CRM::getCustomerName($row['iss_prj_id'], $row['iss_customer_id']);
             } else {
                 $row['customer_name'] = null;
             }
             if (!empty($options['separate_closed']) && $row['sta_is_closed'] == 1) {
                 $issues['closed'][] = $row;
             } elseif (!empty($options['separate_not_assigned_to_user']) && !Issue::isAssignedToUser($row['iss_id'], $usr_id)) {
                 $issues['not_mine'][] = $row;
             } elseif (!empty($options['separate_no_time']) && empty($row['it_spent'])) {
                 $issues['no_time'][] = $row;
             } else {
                 $issues['other'][] = $row;
             }
         }
         $sort_function = function ($a, $b) {
             return strcasecmp($a['customer_name'], $b['customer_name']);
         };
         usort($issues['closed'], $sort_function);
         usort($issues['other'], $sort_function);
     }
     return array('start' => $start_ts, 'end' => $end_ts, 'user' => User::getDetails($usr_id), 'group_name' => Group::getName(User::getGroupID($usr_id)), 'issues' => $issues, 'status_counts' => History::getTouchedIssueCountByStatus($usr_id, $prj_id, $start_ts, $end_ts), 'new_assigned_count' => $newly_assigned, 'time_tracking' => $time_tracking, 'email_count' => $email_count, 'phone_count' => Phone_Support::getCountByUser($usr_id, $start_ts, $end_ts), 'note_count' => Note::getCountByUser($usr_id, $start_ts, $end_ts), 'total_time' => Misc::getFormattedTime($total_time, false));
 }