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 // +----------------------------------------------------------------------+
 
  31 // Definitions for chart types.
 
  32 define('CHART_PROJECTS', 1);
 
  33 define('CHART_TASKS', 2);
 
  34 define('CHART_CLIENTS', 3);
 
  36 // Class ttChartHelper is a helper class for charts.
 
  39   // getTotals - returns total times by project or activity for a given user in a specified period.
 
  40   static function getTotals($user_id, $ch_type, $cl_date, $cl_period = null) {
 
  43     if (isset($cl_period) && isset($cl_date)) {
 
  45         case INTERVAL_THIS_DAY:
 
  46           $period = new Period(INTERVAL_THIS_DAY, new DateAndTime(DB_DATEFORMAT, $cl_date));
 
  49         case INTERVAL_THIS_WEEK:
 
  50           $period = new Period(INTERVAL_THIS_WEEK, new DateAndTime(DB_DATEFORMAT, $cl_date));
 
  53         case INTERVAL_THIS_MONTH:
 
  54           $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime(DB_DATEFORMAT, $cl_date));
 
  57         case INTERVAL_THIS_YEAR:
 
  58           $period = new Period(INTERVAL_THIS_YEAR, new DateAndTime(DB_DATEFORMAT, $cl_date));
 
  64     $mdb2 = getConnection();
 
  67     if ($period != null) {
 
  68       $q_period = " and date >= '".$period->getBeginDate(DB_DATEFORMAT)."' and date <= '".$period->getEndDate(DB_DATEFORMAT)."'";
 
  70     if (CHART_PROJECTS == $ch_type) {
 
  72       $sql = "select p.name as name, sum(time_to_sec(l.duration)) as time from tt_log l
 
  73         inner join tt_projects p on (p.id = l.project_id)
 
  74         where l.status = 1 and l.duration > 0 and l.user_id = $user_id $q_period group by l.project_id";
 
  75     } elseif (CHART_TASKS == $ch_type) {
 
  77       $sql = "select t.name as name, sum(time_to_sec(l.duration)) as time from tt_log l
 
  78         inner join tt_tasks t on (t.id = l.task_id)
 
  79         where l.status = 1 and l.duration > 0 and l.user_id = $user_id $q_period group by l.task_id";
 
  80     } elseif (CHART_CLIENTS == $ch_type) {
 
  82       $sql = "select coalesce(c.name, 'NULL') as name, sum(time_to_sec(l.duration)) as time from tt_log l
 
  83         left join tt_clients c on (c.id = l.client_id)
 
  84         where l.status = 1 and l.duration > 0 and l.user_id = $user_id $q_period group by l.client_id";         
 
  87     $res = $mdb2->query($sql);
 
  88     if (!is_a($res, 'PEAR_Error')) {
 
  89       while ($val = $res->fetchRow()) {
 
  90         $result[] = array('name'=>$val['name'],'time'=>$val['time']); // name  - project name, time - total for project in seconds.
 
  94     // Get total time. We'll need it calculate percentages (for labels to the right of diagram).
 
  96     foreach ($result as $one_val) {
 
  97       $total += $one_val['time'];
 
  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'] .= ' ('.sec_to_time_fmt_hm($one_val['time']).' - '.$percent.')';
 
 105     // Note: the remaining code here is needed to display labels on the side of a diagram.
 
 106     // We print lables ourselves (not using libchart.php) because quality of libchart labels is not good. 
 
 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.
 
 112     // Add color to array items. This is used in labels on the side of a chart.
 
 123       array(142, 165, 250),
 
 124       array(162, 254, 239),
 
 125       array(137, 240, 166),
 
 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]);