Kimai_Database_Mysql::get_time_activities PHP Method

get_time_activities() public method

returns list of time summary attached to activity ID's within specific timeframe as array
Author: sl
public get_time_activities ( integer $start, integer $end, array $users = null, array $customers = null, array $projects = null, array $activities = null ) : array
$start integer start time in unix seconds
$end integer end time in unix seconds
$users array filter for only this ID of a user
$customers array filter for only this ID of a customer
$projects array filter for only this ID of a project
$activities array
return array
    public function get_time_activities($start, $end, $users = null, $customers = null, $projects = null, $activities = null)
    {
        $start = MySQL::SQLValue($start, MySQL::SQLVALUE_NUMBER);
        $end = MySQL::SQLValue($end, MySQL::SQLVALUE_NUMBER);
        $p = $this->kga['server_prefix'];
        $whereClauses = $this->timeSheet_whereClausesFromFilters($users, $customers, $projects, $activities);
        $whereClauses[] = "{$p}activities.trash = 0";
        if ($start) {
            $whereClauses[] = "end > {$start}";
        }
        if ($end) {
            $whereClauses[] = "start < {$end}";
        }
        $query = "SELECT start, end, activityID, (end - start) / 3600 * rate AS costs, fixedRate\n          FROM {$p}timeSheet\n          LEFT JOIN {$p}activities USING(activityID)\n          LEFT JOIN {$p}projects USING(projectID)\n          LEFT JOIN {$p}customers USING(customerID) " . (count($whereClauses) > 0 ? " WHERE " : " ") . implode(" AND ", $whereClauses);
        $result = $this->conn->Query($query);
        if (!$result) {
            $this->logLastError('get_time_activities');
            return array();
        }
        $rows = $this->conn->RecordsArray(MYSQLI_ASSOC);
        if (!$rows) {
            return array();
        }
        $arr = array();
        $consideredStart = 0;
        $consideredEnd = 0;
        foreach ($rows as $row) {
            if ($row['start'] <= $start && $row['end'] < $end) {
                $consideredStart = $start;
                $consideredEnd = $row['end'];
            } elseif ($row['start'] <= $start && $row['end'] >= $end) {
                $consideredStart = $start;
                $consideredEnd = $end;
            } elseif ($row['start'] > $start && $row['end'] < $end) {
                $consideredStart = $row['start'];
                $consideredEnd = $row['end'];
            } elseif ($row['start'] > $start && $row['end'] >= $end) {
                $consideredStart = $row['start'];
                $consideredEnd = $end;
            }
            $costs = (double) $row['costs'];
            $fixedRate = (double) $row['fixedRate'];
            if (isset($arr[$row['activityID']])) {
                $arr[$row['activityID']]['time'] += (int) ($consideredEnd - $consideredStart);
                if ($fixedRate > 0) {
                    $arr[$row['activityID']]['costs'] += $fixedRate;
                } else {
                    $arr[$row['activityID']]['costs'] += $costs;
                }
            } else {
                $arr[$row['activityID']]['time'] = (int) ($consideredEnd - $consideredStart);
                if ($fixedRate > 0) {
                    $arr[$row['activityID']]['costs'] = $fixedRate;
                } else {
                    $arr[$row['activityID']]['costs'] = $costs;
                }
            }
        }
        return $arr;
    }
Kimai_Database_Mysql