Fixed charts for negative hours.
[timetracker.git] / WEB-INF / lib / ttChartHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('Period');
30 import('ttTimeHelper');
31
32 // Definitions for chart types.
33 define('CHART_PROJECTS', 1);
34 define('CHART_TASKS', 2);
35 define('CHART_CLIENTS', 3);
36
37 // Class ttChartHelper is a helper class for charts.
38 class ttChartHelper {
39
40   // getTotals - returns total times by project or task for a given user in a specified period.
41   static function getTotals($user_id, $chart_type, $selected_date, $interval_type) {
42
43     $period = null;
44     switch ($interval_type) {
45       case INTERVAL_THIS_DAY:
46         $period = new Period(INTERVAL_THIS_DAY, new DateAndTime(DB_DATEFORMAT, $selected_date));
47         break;
48  
49       case INTERVAL_THIS_WEEK:
50         $period = new Period(INTERVAL_THIS_WEEK, new DateAndTime(DB_DATEFORMAT, $selected_date));
51         break;
52
53       case INTERVAL_THIS_MONTH:
54         $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime(DB_DATEFORMAT, $selected_date));
55         break;
56
57       case INTERVAL_THIS_YEAR:
58         $period = new Period(INTERVAL_THIS_YEAR, new DateAndTime(DB_DATEFORMAT, $selected_date));
59         break;
60     }
61
62     $result = array();
63     $mdb2 = getConnection();
64
65     $q_period = '';
66     if ($period != null) {
67       $q_period = " and date >= '".$period->getStartDate(DB_DATEFORMAT)."' and date <= '".$period->getEndDate(DB_DATEFORMAT)."'";
68     }
69     if (CHART_PROJECTS == $chart_type) {
70       // Data for projects.
71       $sql = "select p.name as name, sum(time_to_sec(l.duration)) as time from tt_log l
72         left join tt_projects p on (p.id = l.project_id)
73         where l.status = 1 and l.user_id = $user_id $q_period group by l.project_id";
74     } elseif (CHART_TASKS == $chart_type) {
75       // Data for tasks.
76       $sql = "select t.name as name, sum(time_to_sec(l.duration)) as time from tt_log l
77         left join tt_tasks t on (t.id = l.task_id)
78         where l.status = 1 and l.user_id = $user_id $q_period group by l.task_id";
79     } elseif (CHART_CLIENTS == $chart_type) {
80       // Data for clients.
81       $sql = "select c.name as name, sum(time_to_sec(l.duration)) as time from tt_log l
82         left join tt_clients c on (c.id = l.client_id)
83         where l.status = 1 and l.user_id = $user_id $q_period group by l.client_id";
84     }
85
86     $res = $mdb2->query($sql);
87     if (!is_a($res, 'PEAR_Error')) {
88       while ($val = $res->fetchRow()) {
89         if ($val['time'] >= 0) // Only positive totals make sense in pie charts. Skip negatives entirely.
90           $result[] = array('name'=>$val['name'],'time'=>$val['time']); // name - project name, time - total for project in seconds.
91       }
92     }
93
94     // Get total time. We'll need it to calculate percentages (for labels to the right of diagram).
95     $total = 0;
96     foreach ($result as $one_val) {
97       $total += $one_val['time'];
98     }
99     // Add a string representation of time + percentage to names. Example: "Time Tracker (1:15 - 6%)".
100     foreach ($result as &$one_val) {
101       $percent = round(100*$one_val['time']/$total).'%';
102       $one_val['name'] .= ' ('.ttTimeHelper::minutesToDuration($one_val['time'] / 60).' - '.$percent.')';
103     }
104
105     // Note: the remaining code here is needed to display labels on the side of the diagram.
106     // We print labels ourselves (not using libchart.php) because quality of libchart labels is not good.
107
108     // Note: Optimize this sorting and reversing.
109     $result = mu_sort($result, 'time');
110     $result = array_reverse($result); // This is to assign correct colors to labels.
111
112     // Add color to array items. This is used in labels on the side of a chart.
113     $colors = array(
114       array(2, 78, 0),
115       array(148, 170, 36),
116       array(233, 191, 49),
117       array(240, 127, 41),
118       array(243, 63, 34),
119       array(190, 71, 47),
120       array(135, 81, 60),
121       array(128, 78, 162),
122       array(121, 75, 255),
123       array(142, 165, 250),
124       array(162, 254, 239),
125       array(137, 240, 166),
126       array(104, 221, 71),
127       array(98, 174, 35),
128       array(93, 129, 1)
129     );
130     for ($i = 0; $i < count($result); $i++) {
131       $color = $colors[$i%count($colors)];
132       $result[$i]['color_html'] = sprintf('#%02x%02x%02x', $color[0], $color[1], $color[2]);
133     }
134
135     return $result;
136   }
137
138   // adjustType - adjust chart type to something that is available for a group.
139   static function adjustType($requested_type) {
140     global $user;
141     $tracking_mode = $user->getTrackingMode();
142     $client_option = $user->isPluginEnabled('cl');
143
144     // We have 3 possible options for chart type: projects, tasks, or clients.
145     // Deal with each one individually.
146
147     if ($requested_type == CHART_PROJECTS) {
148       if ($tracking_mode == MODE_PROJECTS || $tracking_mode == MODE_PROJECTS_AND_TASKS)
149         return CHART_PROJECTS;
150       else if ($client_option)
151         return CHART_CLIENTS;
152     }
153
154     if ($requested_type == CHART_TASKS) {
155       if ($tracking_mode == MODE_PROJECTS_AND_TASKS)
156         return CHART_TASKS;
157       if ($tracking_mode == MODE_PROJECTS)
158         return CHART_PROJECTS;
159       else if ($client_option)
160         return CHART_CLIENTS;
161     }
162
163     if ($requested_type == CHART_CLIENTS) {
164       if ($client_option)
165         return CHART_CLIENTS;
166       else if ($tracking_mode == MODE_PROJECTS || ($tracking_mode == MODE_PROJECTS_AND_TASKS))
167         return CHART_PROJECTS;
168     }
169
170     return  CHART_PROJECTS;
171   }
172 }