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 // Note: This script uses Lichart PHP library and requires GD 2.0.1 or later.
31 require_once('initialize.php');
33 import('DateAndTime');
34 import('ttChartHelper');
35 import('ttUserConfig');
37 import('ttUserHelper');
38 import('ttTeamHelper');
41 if (!(ttAccessAllowed('view_own_charts') || ttAccessAllowed('view_charts'))) {
42 header('Location: access_denied.php');
45 if (!$user->isPluginEnabled('ch')) {
46 header('Location: feature_disabled.php');
49 if ($user->behalf_id && (!$user->can('view_charts') || !$user->checkBehalfId())) {
50 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
53 if (!$user->behalf_id && !$user->can('view_own_charts') && !$user->adjustBehalfId()) {
54 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to view on behalf.
57 if ($request->isPost() && $request->getParameter('user')) {
58 if (!$user->isUserValid($request->getParameter('user'))) {
59 header('Location: access_denied.php'); // Wrong user id on post.
63 // End of access checks.
65 // Determine user for which we display this page.
66 $userChanged = $request->getParameter('user_changed');
67 if ($request->isPost() && $userChanged) {
68 $user_id = $request->getParameter('user');
69 $user->setOnBehalfUser($user_id);
71 $user_id = $user->getUser();
72 // Handle a situation for no users in on behalf group.
73 if ($user->behalfGroup && $user_id == $user->id)
77 $uc = new ttUserConfig();
78 $tracking_mode = $user->getTrackingMode();
80 // Initialize and store date in session.
81 $cl_date = $request->getParameter('date', @$_SESSION['date']);
83 $now = new DateAndTime(DB_DATEFORMAT);
84 $cl_date = $now->toString(DB_DATEFORMAT);
86 $_SESSION['date'] = $cl_date;
88 if ($request->isPost()) {
89 $cl_interval = $request->getParameter('interval');
90 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
91 $_SESSION['chart_interval'] = $cl_interval;
92 $uc->setValue(SYSC_CHART_INTERVAL, $cl_interval);
94 $cl_type = $request->getParameter('type');
95 if (!$cl_type) $cl_type = ttChartHelper::adjustType($cl_type);
96 $_SESSION['chart_type'] = $cl_type;
97 $uc->setValue(SYSC_CHART_TYPE, $cl_type);
99 // Initialize chart interval.
100 $cl_interval = $_SESSION['chart_interval'];
101 if (!$cl_interval) $cl_interval = $uc->getValue(SYSC_CHART_INTERVAL);
102 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
103 $_SESSION['chart_interval'] = $cl_interval;
105 // Initialize chart type.
106 $cl_type = $_SESSION['chart_type'];
107 if (!$cl_type) $cl_type = $uc->getValue(SYSC_CHART_TYPE);
108 $cl_type = ttChartHelper::adjustType($cl_type);
109 $_SESSION['chart_type'] = $cl_type;
112 // Elements of chartForm.
113 $chart_form = new Form('chartForm');
115 // User dropdown. Changes the user "on behalf" of whom we are working.
116 if ($user->can('view_charts')) {
117 $rank = $user->getMaxRankForGroup($user->getGroup());
118 if ($user->can('view_own_charts'))
119 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
121 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
122 $user_list = $user->getUsers($options);
123 if (count($user_list) >= 1) {
124 $chart_form->addInput(array('type'=>'combobox',
125 'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
129 'datakeys'=>array('id','name'),
131 $chart_form->addInput(array('type'=>'hidden','name'=>'user_changed'));
132 $smarty->assign('user_dropdown', 1);
136 // Chart interval options.
137 $intervals = array();
138 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
139 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
140 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
141 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
142 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
144 // Chart interval dropdown.
145 $chart_form->addInput(array('type' => 'combobox',
146 'onchange' => 'this.form.submit();',
147 'name' => 'interval',
148 'value' => $cl_interval,
152 // Chart type options.
153 $chart_selector = (MODE_PROJECTS_AND_TASKS == $tracking_mode || $user->isPluginEnabled('cl'));
154 if ($chart_selector) {
156 if (MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode)
157 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
158 if (MODE_PROJECTS_AND_TASKS == $tracking_mode)
159 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
160 if ($user->isPluginEnabled('cl'))
161 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
163 // Add chart type dropdown.
164 $chart_form->addInput(array('type' => 'combobox',
165 'onchange' => 'this.form.submit();',
173 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
175 // Get data for our chart.
176 $totals = ttChartHelper::getTotals($user_id, $cl_type, $cl_date, $cl_interval);
177 $smarty->assign('totals', $totals);
179 // Prepare chart for drawing.
181 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
182 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
183 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
184 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
185 * is also passed to charts.tpl Smarty template.
187 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
188 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
190 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
191 * without title, logo, and labels.
193 $chart = new PieChartEx(300, 300);
194 $data_set = new XYDataSet();
195 foreach($totals as $total) {
196 $data_set->addPoint(new Point( $total['name'], $total['time']));
198 $chart->setDataSet($data_set);
200 // Prepare a file name.
201 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
202 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
203 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
204 $file_name = $img_dir.$file_name; // Full file name.
206 // Clean up the file system from older images.
207 $img_files = glob($img_dir.'chart_*.png');
208 foreach($img_files as $file) {
209 // If the create time of file is older than 1 minute, delete it.
210 if (filemtime($file) < (time() - 60)) {
215 // Write chart image to file system.
216 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
217 // At this point libchart usage is complete and we have chart image on disk.
219 $smarty->assign('img_file_name', $img_ref);
220 $smarty->assign('chart_selector', $chart_selector);
221 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
222 $smarty->assign('title', $i18n->get('title.charts'));
223 $smarty->assign('content_page_name', 'charts.tpl');
224 $smarty->display('index.tpl');