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