Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/includes/class.effort.php
blob: 984feebb1f448f60e7e0b3a7dc50b8a77ab02845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php

/**
 * Class effort
 *
 * Task level Effort Tracking functionality.
 */
class effort
{
    const FORMAT_HOURS_COLON_MINUTES = 0; // Default value in database
    const FORMAT_HOURS_SPACE_MINUTES = 1;
    const FORMAT_HOURS_PLAIN = 2;
    const FORMAT_HOURS_ONE_DECIMAL = 3;
    const FORMAT_MINUTES = 4;
    const FORMAT_DAYS_PLAIN = 5;
    const FORMAT_DAYS_ONE_DECIMAL = 6;
    const FORMAT_DAYS_PLAIN_HOURS_PLAIN = 7;
    const FORMAT_DAYS_PLAIN_HOURS_ONE_DECIMAL = 8;
    const FORMAT_DAYS_PLAIN_HOURS_COLON_MINUTES = 9;
    const FORMAT_DAYS_PLAIN_HOURS_SPACE_MINUTES = 10;

    private $_task_id;
    private $_userId;
    public $details;

    /**
     * Class Constructor: Requires the user id and task id as all effort is in context of the task.
     *
     * @param $task_id
     * @param $user_id
     */
    public function __construct($task_id,$user_id)
    {
        $this->_task_id = $task_id;
        $this->_userId = $user_id;
    }

    /**
     * Manually add effort to the effort table for this issue / user.
     *
     * @param $effort_to_add int amount of effort in hh:mm to add to effort table.
     */
    public function addEffort($effort_to_add, $proj)
    {
        global $db;

        # note: third parameter seem useless, not used by EditStringToSeconds().., maybe drop it..
        $effort = self::editStringToSeconds($effort_to_add, $proj->prefs['hours_per_manday'], $proj->prefs['estimated_effort_format']);
        if ($effort === FALSE) {
            Flyspray::show_error(L('invalideffort'));
            return false;
        }

        # quickfix to avoid useless table entries.
        if($effort==0){ 
            Flyspray::show_error(L('zeroeffort'));
            return false;
        } else{
            $db->query('INSERT INTO {effort}
                (task_id, date_added, user_id,start_timestamp,end_timestamp,effort)
                VALUES  ( ?, ?, ?, ?,?,? )',
                array($this->_task_id, time(), $this->_userId,time(),time(),$effort)
            );
            return true;
        }
    }

    /**
     * Starts tracking effort for the current user against the current issue.
     *
     * @return bool Returns Success or Failure of the action.
     */
    public function startTracking()
    {
        global $db;

        //check if the user is already tracking time against this task.
        $result = $db->query('SELECT * FROM {effort} WHERE task_id ='.$this->_task_id.' AND user_id='.$this->_userId.' AND end_timestamp IS NULL;');
        if($db->countRows($result)>0)
        {
            return false;
        }
        else
        {
                $db->query('INSERT INTO  {effort}
                                         (task_id, date_added, user_id,start_timestamp)
                                 VALUES  ( ?, ?, ?, ? )',
                                 array   ($this->_task_id, time(), $this->_userId,time()));

                return true;
        }
    }

    /**
     * Stops tracking the current tracking request and then updates the actual hours field on the table, this
     * is useful as both stops constant calculation from start/end timestamps and provides a quick aggregation
     * method as we only need to deal with one field.
     */
    public function stopTracking()
    {
        global $db;

        $time = time();


        $sql = $db->query('SELECT start_timestamp FROM {effort}  WHERE user_id='.$this->_userId.' AND task_id='.$this->_task_id.' AND end_timestamp IS NULL;');
        $result = $db->fetchRow($sql);
        $start_time = $result[0];
        $seconds = $time - $start_time;
        
        // Round to full minutes upwards.
        $effort = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
 
        $sql = $db->query("UPDATE {effort} SET end_timestamp = ".$time.",effort = ".$effort." WHERE user_id=".$this->_userId." AND task_id=".$this->_task_id." AND end_timestamp IS NULL;");
    }

    /**
     * Removes any outstanding tracking requests for this task for this user.
     */
    public function cancelTracking()
    {
        global $db;
    
        # 2016-07-04: also remove invalid finished 0 effort entries that were accidently possible up to Flyspray 1.0-rc
        $db->query('DELETE FROM {effort}
            WHERE user_id='.$this->_userId.'
            AND task_id='.$this->_task_id.'
            AND (
                end_timestamp IS NULL
                OR (start_timestamp=end_timestamp AND effort=0)
            );'
        );
    }

    public function populateDetails()
    {
        global $db;

        $this->details = $db->query('SELECT * FROM {effort} WHERE task_id ='.$this->_task_id.';');
    }
    
    public static function secondsToString($seconds, $factor, $format) {
        if ($seconds == 0) {
            return '';
        }
        
        $factor = ($factor == 0 ? 86400 : $factor);

        switch ($format) {
            case self::FORMAT_HOURS_COLON_MINUTES:
                $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
                $hours = floor($seconds / 3600);
                $minutes = floor(($seconds - ($hours * 3600)) / 60);
                return sprintf('%01u:%02u', $hours, $minutes);
                break;
            case self::FORMAT_HOURS_SPACE_MINUTES:
                $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
                $hours = floor($seconds / 3600);
                $minutes = floor(($seconds - ($hours * 3600)) / 60);
                if ($hours == 0) {
                    return sprintf('%u %s', $minutes, L('minuteabbrev'));
                } else {
                    return sprintf('%u %s %u %s', $hours, L('hourabbrev'), $minutes, L('minuteabbrev'));
                }
                break;
            case self::FORMAT_HOURS_PLAIN:
                $hours = ceil($seconds / 3600);
                return sprintf('%01u %s', $hours, ($hours == 1 ? L('hoursingular') : L('hourplural')));
                break;
            case self::FORMAT_HOURS_ONE_DECIMAL:
                $hours = round(ceil($seconds * 10 / 3600) / 10, 1);
                return sprintf('%01.1f %s', $hours, ($hours == 1 ? L('hoursingular') : L('hourplural')));
                break;
            case self::FORMAT_MINUTES:
                $minutes = ceil($seconds / 60);
                return sprintf('%01u %s', $minutes, L('minuteabbrev'));
                break;
            case self::FORMAT_DAYS_PLAIN:
                $days = ceil($seconds / $factor);
                return sprintf('%01u %s', $days, ($days == 1 ? L('manday') : L('mandays')));
                break;
            case self::FORMAT_DAYS_ONE_DECIMAL:
                $days = round(ceil($seconds * 10 / $factor) / 10, 1);
                return sprintf('%01.1f %s', $days, ($days == 1 ? L('manday') : L('mandays')));
                break;
            case self::FORMAT_DAYS_PLAIN_HOURS_PLAIN:
                $days = floor($seconds / $factor);
                $hours = ceil(($seconds - ($days * $factor)) / 3600);
                if ($days == 0) {
                    return sprintf('%1u %s', $hours, L('hourabbrev'));
                } else {
                    return sprintf('%u %s %1u %s', $days, L('mandayabbrev'), $hours, L('hourabbrev'));
                }
                break;
            case self::FORMAT_DAYS_PLAIN_HOURS_ONE_DECIMAL:
                $days = floor($seconds / $factor);
                $hours = round(ceil(($seconds - ($days * $factor)) * 10 / 3600) / 10, 1);
                if ($days == 0) {
                    return sprintf('%01.1f %s', $hours, L('hourabbrev'));
                } else {
                    return sprintf('%u %s %01.1f %s', $days, L('mandayabbrev'), $hours, L('hourabbrev'));
                }
                break;
            case self::FORMAT_DAYS_PLAIN_HOURS_COLON_MINUTES:
                $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
                $days = floor($seconds / $factor);
                $hours = floor(($seconds - ($days * $factor)) / 3600);
                $minutes = floor(($seconds - (($days * $factor) + ($hours * 3600))) / 60);
                if ($days == 0) {
                    return sprintf('%01u:%02u', $hours, $minutes);
                } else {
                    return sprintf('%u %s %01u:%02u', $days, L('mandayabbrev'), $hours, $minutes);
                }
                break;
            case self::FORMAT_DAYS_PLAIN_HOURS_SPACE_MINUTES:
                $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
                $days = floor($seconds / $factor);
                $hours = floor(($seconds - ($days * $factor)) / 3600);
                $minutes = floor(($seconds - (($days * $factor) + ($hours * 3600))) / 60);
                if ($days == 0) {
                    return sprintf('%u %s %u %s', $hours, L('hourabbrev'), $minutes, L('minuteabbrev'));
                } else {
                    return sprintf('%u %s %u %s %u %s', $days, L('mandayabbrev'), $hours, L('hourabbrev'), $minutes, L('minuteabbrev'));
                }
                break;
            default:
                $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
                $hours = floor($seconds / 3600);
                $minutes = floor(($seconds - ($hours * 3600)) / 60);
                return sprintf('%01u:%02u', $hours, $minutes);
        }
    }

    public static function secondsToEditString($seconds, $factor, $format) {
        $factor = ($factor == 0 ? 86400 : $factor);

        // Adjust seconds to be evenly dividable by 60, so
        // 3595 -> 3600, floor can be safely used for minutes in formats
        // and the result will be 1:00 instead of 0:60 (if ceil would be used).
        
        $seconds = ($seconds % 60 == 0 ? $seconds : floor($seconds / 60) * 60 + 60);
        
        switch ($format) {
            case self::FORMAT_HOURS_COLON_MINUTES:
            case self::FORMAT_HOURS_SPACE_MINUTES:
            case self::FORMAT_HOURS_PLAIN:
            case self::FORMAT_HOURS_ONE_DECIMAL:
            case self::FORMAT_MINUTES:
                $hours = floor($seconds / 3600);
                $minutes = floor(($seconds - ($hours * 3600)) / 60);
                return sprintf('%01u:%02u', $hours, $minutes);
                break;
            case self::FORMAT_DAYS_PLAIN:
            case self::FORMAT_DAYS_ONE_DECIMAL:
            case self::FORMAT_DAYS_PLAIN_HOURS_PLAIN:
            case self::FORMAT_DAYS_PLAIN_HOURS_ONE_DECIMAL:
            case self::FORMAT_DAYS_PLAIN_HOURS_COLON_MINUTES:
            case self::FORMAT_DAYS_PLAIN_HOURS_SPACE_MINUTES:
                $days = floor($seconds / $factor);
                $hours = floor(($seconds - ($days * $factor)) / 3600);
                $minutes = floor(($seconds - ($hours * 3600)) / 60);
                if ($days == 0) {
                    return sprintf('%01u:%02u', $hours, $minutes);
                } else {
                    return sprintf('%u %02u:%02u', $days, $hours, $minutes);
                }
                break;
            default:
                $hours = floor($seconds / 3600);
                $minutes = floor(($seconds - (($days * $factor) + ($hours * 3600))) / 60);
                return sprintf('%01u:%02u', $hours, $minutes);
        }
    }

    public static function editStringToSeconds($string, $factor, $format) {
        if (!isset($string) || empty($string)) {
            return 0;
        }
        
        $factor = ($factor == 0 ? 86400 : $factor);
        
        $matches = array();
        if (preg_match('/^((\d+)\s)?(\d+)(:(\d{2}))?$/', $string, $matches) !== 1) {
            return FALSE;
        }

        if (!isset($matches[2])) {
            $matches[2] = 0;
        }
            
        if (!isset($matches[5])) {
            $matches[5] = 0;
        } else {
            if ($matches[5] > 59) {
                return FALSE;
            }
        }
            
        $effort = ($matches[2] * $factor) + ($matches[3] * 3600) + ($matches[5] * 60);
        return $effort;
    }
}