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 if ($user->can('view_own_charts'))
118 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
120 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
121 $user_list = $user->getUsers($options);
122 if (count($user_list) >= 1) {
123 $chart_form->addInput(array('type'=>'combobox',
124 'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
128 'datakeys'=>array('id','name'),
130 $chart_form->addInput(array('type'=>'hidden','name'=>'user_changed'));
131 $smarty->assign('user_dropdown', 1);
135 // Chart interval options.
136 $intervals = array();
137 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
138 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
139 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
140 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
141 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
143 // Chart interval dropdown.
144 $chart_form->addInput(array('type' => 'combobox',
145 'onchange' => 'this.form.submit();',
146 'name' => 'interval',
147 'value' => $cl_interval,
151 // Chart type options.
152 $chart_selector = (MODE_PROJECTS_AND_TASKS == $tracking_mode || $user->isPluginEnabled('cl'));
153 if ($chart_selector) {
155 if (MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode)
156 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
157 if (MODE_PROJECTS_AND_TASKS == $tracking_mode)
158 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
159 if ($user->isPluginEnabled('cl'))
160 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
162 // Add chart type dropdown.
163 $chart_form->addInput(array('type' => 'combobox',
164 'onchange' => 'this.form.submit();',
172 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
174 // Get data for our chart.
175 $totals = ttChartHelper::getTotals($user_id, $cl_type, $cl_date, $cl_interval);
176 $smarty->assign('totals', $totals);
178 // Prepare chart for drawing.
180 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
181 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
182 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
183 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
184 * is also passed to charts.tpl Smarty template.
186 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
187 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
189 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
190 * without title, logo, and labels.
192 $chart = new PieChartEx(300, 300);
193 $data_set = new XYDataSet();
194 foreach($totals as $total) {
195 $data_set->addPoint(new Point( $total['name'], $total['time']));
197 $chart->setDataSet($data_set);
199 // Prepare a file name.
200 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
201 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
202 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
203 $file_name = $img_dir.$file_name; // Full file name.
205 // Clean up the file system from older images.
206 $img_files = glob($img_dir.'chart_*.png');
207 foreach($img_files as $file) {
208 // If the create time of file is older than 1 minute, delete it.
209 if (filemtime($file) < (time() - 60)) {
214 // Write chart image to file system.
215 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
216 // At this point libchart usage is complete and we have chart image on disk.
218 $smarty->assign('img_file_name', $img_ref);
219 $smarty->assign('chart_selector', $chart_selector);
220 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
221 $smarty->assign('title', $i18n->get('title.charts'));
222 $smarty->assign('content_page_name', 'charts.tpl');
223 $smarty->display('index.tpl');