-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.inc.php
executable file
·457 lines (377 loc) · 13.1 KB
/
stat.inc.php
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?PHP
/*
*******************************************************************************
* Chronologist - web-based time tracking database
* Copyright (C) 2003 by Sylvain LAFRASSE.
*******************************************************************************
* LICENSE:
* This file is part of Chronologist.
*
* Chronologist is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Chronologist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chronologist; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************
* FILE NAME : stat.inc.php
* DESCRIPTION : Statistics related functions.
* AUTHORS : Sylvain LAFRASSE.
*******************************************************************************
*/
require_once("design.inc.php");
function MyWeek($week,$year)
{
// Get the first day of the year
$FirstDay = mktime(0, 0, 1, 1, 1, $year);
// Get the numeric representation of the day of the week (0 for Sunday through 6 for Saturday)
$Day = date("w", $FirstDay);
// If the first day is a monday
$FirstMonday = $FirstDay;
// If the first day is NOT a monday
if ($Day != 1)
{
// If the first day is a sunday
if ($Day == 0)
{
$FirstMonday = $FirstDay + (24 * 60 * 60);
}
else
{
$FirstMonday = $FirstDay + ((8 - $Day) * (24 * 60 * 60));
}
}
// Get the ISO-8601 week number of the first day of the year (weeks starting on Monday)
$FirstDayWeekNumber = date("W", $FirstDay);
$NthMonday = $FirstMonday + (($week - 1 - $FirstDayWeekNumber) * ( 7 * (24 * 60 * 60)));
// $NthMonday = $FirstMonday + (($week - 1) * ( 7 * (24 * 60 * 60)));
$NthSunday = $NthMonday + (6 * (24 * 60 * 60));
$Period = array();
$Period['start'] = $NthMonday;
$Period['end'] = $NthSunday;
return $Period;
}
function get_lundi_dimanche_from_week($week,$year)
{
$Period = array();
if (strftime("%W" ,mktime(0, 0, 0, 01, 01, $year)) == 1)
{
$mon_mktime = mktime(0, 0, 0, 01, (01 + (($week - 1) * 7)), $year);
}
else
{
$mon_mktime = mktime(0, 0, 0, 01, (01 + ($week*7)), $year);
}
if (date("w", $mon_mktime) > 1)
{
$decalage = ((date("w", $mon_mktime) - 1) * 60 * 60 * 24);
}
// $lundi = $mon_mktime - $decalage;
// $dimanche = $lundi + (6 * 60 * 60 * 24);
$Period['start'] = $mon_mktime - $decalage;
$Period['end'] = $Period['start'] + (6 * 60 * 60 * 24);
// return array(date("D - d/m/Y", $lundi) ,date("D - d/m/Y", $dimanche));
// return array($lundi, $dimanche);
return $Period;
}
/*
function get_weeks($year, $group_by_month = false)
{
$firstday_ts = mktime(1,0,0,1,1,$year);
$firstday = date("w", $firstday_ts);
if($group_by_month === false)
{
$weeks[] = date('Y-m-d',$firstday_ts);
}
else
{
$m = 'Jan'; //start month
$weeks[$m][] = date('Y-m-d',$firstday_ts);
}
$first = $firstday_ts;
if($firstday > 0)
{
$x = 8 - $firstday;
$first = mktime(1,0,0,1,$x,$year);
if($group_by_month === false)
{
$weeks[] = date('Y-m-d',$first);
}
else
{
$weeks[$m][] = date("Y-m-d", $first);
}
}
$first = strtotime("+7 day",$first);
for($i = 0; $year == date("Y", $first); $i++, $first = strtotime("+7 day",$first))
{
if($group_by_month === false)
{
$weeks[] = date("Y-m-d", $first);
}
else
{
if($m != date("M",$first))
{
$m = date("M",$first);
}
$weeks[$m][] = date("Y-m-d", $first);
}
}
return $weeks;
}
*/
/*
//function get_week_boundaries($int_time)
function get_week_boundaries($Week, $Year)
{
// Get the first day of the year.
$int_time = mktime(0, 0, 1, 1, 1, $Year);
// first: find monday 0:00
$weekdayid = date("w", $int_time);
// sunday must have w=7, not 0. otherwise sunday will count to the next week *
if ($weekdayid == 0) $weekdayid = 7;
$dayid = date("j", $int_time);
$monthid = date("n", $int_time);
$yearid = date("Y", $int_time);
$beginofday = mktime(0, 0, 0, $monthid, $dayid, $yearid);
$beginofweek = $beginofday - (($weekdayid-1) * 86400); //86400 == seconds of one day (24 hours)
//now add the value of one week and call it the end of the week
//NOTE: End of week is Sunday, 23:59:59. I think you could also use Monday 00:00:00 but I though that'd suck
$endofweek = ($beginofweek + 7 * 86400) - 1;
$week["begin"] = $beginofweek;
$week["end"] = $endofweek;
$week["pov"] = $int_time;
return $week;
}
*/
/*
// Return any week beginning and ending full date
function GetWeekPeriod($Week, $Year)
{
// Get the first day of the year.
$int_time = mktime(0, 0, 1, 1, 1, $Year);
// first: find monday 0:00
$weekdayid = date("w", $int_time);
// sunday must have w=7, not 0. otherwise sunday will count to the next week *
if ($weekdayid == 0) $weekdayid = 7;
$dayid = date("j", $int_time);
$monthid = date("n", $int_time);
$yearid = date("Y", $int_time);
$beginofday = mktime(0, 0, 0, $monthid, $dayid, $yearid);
$beginofweek = $beginofday - (($weekdayid-1) * 86400); //86400 == seconds of one day (24 hours)
//now add the value of one week and call it the end of the week
//NOTE: End of week is Sunday, 23:59:59. I think you could also use Monday 00:00:00 but I though that'd suck
$endofweek = ($beginofweek + 7 * 86400) - 1;
$week["begin"] = $beginofweek;
$week["end"] = $endofweek;
$week["pov"] = $int_time;
return $week;
$Period = array();
// If you are a Catholic you should use $x = 0 considering Sunday as the first day of the week.
// If you are an Orthodox you should use $x = 1 considering Sunday as the seventh day of the week
$x = 1;
// $Period['start'] = mktime(0, 0, 0, 1, 1 + ($Week*7), $Year);
// $Period['end'] = mktime(0, 0, 0, 1, (($Week+1)*7), $Year);
$Period['start'] = mktime(0, 0, 0, 1, 1 + $Week*7 + $x - 6, $Year);
$Period['end'] = mktime(0, 0, 0, 1, 1 + $Week*7 + $x , $Year);
return $Period;
}
*/
// Return any week beginning and ending full date
// Thanx to "leonardo at kreativ dot ro" from "http://fr.php.net/date"
function GetWeekPeriod($Week, $Year)
{
// TODO: correct the bug that make it workng ONLY in 2005 !!!
$Period = array();
// If you are a Catholic you should use $x = 0 considering Sunday as the first day of the week.
// If you are an Orthodox you should use $x = 1 considering Sunday as the seventh day of the week
$x = 1;
// $Period['start'] = mktime(0, 0, 0, 1, 1 + ($Week*7), $Year);
// $Period['end'] = mktime(0, 0, 0, 1, (($Week+1)*7), $Year);
$Period['start'] = mktime(0, 0, 0, 1, 1 + $Week*7 + $x - 6, $Year);
$Period['end'] = mktime(0, 0, 0, 1, 1 + $Week*7 + $x , $Year);
return $Period;
}
// Return any month beginning and ending full date
function GetMonthPeriod($Month, $Year)
{
$Period = array();
$Period['start'] = mktime( 0, 0, 1, $Month, 1 , $Year);
$Period['end'] = mktime(23, 59, 59, $Month, date("t", $Period['start']), $Year);
return $Period;
}
function GetProjectTime($UID, $PID, $AfterUnixTimeStamp = 0, $BeforeUnixTimeStamp = 0)
{
$TotalTime = 0;
// Gets the user projects duration from the database
$SQL = "SELECT SUM(duration) as length
FROM `tasks`
WHERE pid = '$PID'
";
// If the user is not administrator
if ($UID != 0)
{
$SQL .= "AND uid = '$UID'
";
}
// Select all the tasks after this timestamp
if ($AfterUnixTimeStamp != 0)
{
$SQL .= "AND beginning > FROM_UNIXTIME('$AfterUnixTimeStamp')
";
}
// Select all the tasks before this timestamp
if ($BeforeUnixTimeStamp != 0)
{
$SQL .= "AND beginning < FROM_UNIXTIME('$BeforeUnixTimeStamp')
";
}
global $Connection;
$Result = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
$Row = mysqli_fetch_array($Result);
$TotalTime = $Row['length'];
mysqli_free_result($Result);
return $TotalTime;
}
function GetSubProjectsCumulativeTime($UID, $PPID, $AfterUnixTimeStamp = 0, $BeforeUnixTimeStamp = 0)
{
$TotalTime = 0;
// Gets all the subprojects of the given project from the database
$SQL = "SELECT pid
FROM projects
WHERE ppid = '$PPID'
";
global $Connection;
$Result = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
// Puts all the sub-projects of the current sub-project in the 'to-be-returned' array
while ($Row = mysqli_fetch_array($Result))
{
// Get the current project ID
$PID = $Row['pid'];
// Gets the user projects duration from the database
$SQL = "SELECT SUM(duration) as length
FROM `tasks`
WHERE pid = '$PID'
";
// If the user is not administrator
if ($UID != 0)
{
$SQL .= "AND uid = '$UID'
";
}
// Select all the tasks after this timestamp
if ($AfterUnixTimeStamp != 0)
{
$SQL .= "AND beginning > FROM_UNIXTIME('$AfterUnixTimeStamp')
";
}
// Select all the tasks before this timestamp
if ($BeforeUnixTimeStamp != 0)
{
$SQL .= "AND beginning < FROM_UNIXTIME('$BeforeUnixTimeStamp')
";
}
$TimeResult = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
$TimeRow = mysqli_fetch_array($TimeResult);
$TotalTime += $TimeRow['length'];
// Recursively do the same thing for all the sub-projects of the current project
$TotalTime += GetSubProjectsCumulativeTime($UID, $PID, $AfterUnixTimeStamp, $BeforeUnixTimeStamp);
}
mysqli_free_result($Result);
return $TotalTime;
}
function GetActivityTime($UID, $AID, $AfterUnixTimeStamp = 0, $BeforeUnixTimeStamp = 0)
{
$TotalTime = 0;
// Gets the user activity duration from the database
$SQL = "SELECT SUM(duration) as length
FROM `tasks`
WHERE aid = '$AID'
";
// If the user is not administrator
if ($UID != 0)
{
$SQL .= "AND uid = '$UID'
";
}
// Select all the tasks after this timestamp
if ($AfterUnixTimeStamp != 0)
{
$SQL .= "AND beginning > FROM_UNIXTIME('$AfterUnixTimeStamp')
";
}
// Select all the tasks before this timestamp
if ($BeforeUnixTimeStamp != 0)
{
$SQL .= "AND beginning < FROM_UNIXTIME('$BeforeUnixTimeStamp')
";
}
global $Connection;
$Result = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
$Row = mysqli_fetch_array($Result);
$TotalTime = $Row['length'];
mysqli_free_result($Result);
return $TotalTime;
}
function GetSubActivitiesCumulativeTime($UID, $PAID, $AfterUnixTimeStamp = 0, $BeforeUnixTimeStamp = 0)
{
$TotalTime = 0;
// Gets all the sub-activites of the given activity from the database
$SQL = "SELECT aid
FROM activities
WHERE paid = '$PAID'
";
global $Connection;
$Result = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
// Puts all the sub-activities of the current sub-activity in the 'to-be-returned' array
while ($Row = mysqli_fetch_array($Result))
{
// Get the current actitivty ID
$AID = $Row['aid'];
// Gets the user activitiess duration from the database
$SQL = "SELECT SUM(duration) as length
FROM `tasks`
WHERE aid = '$AID'
";
// If the user is not administrator
if ($UID != 0)
{
$SQL .= "AND uid = '$UID'
";
}
// Select all the tasks after this timestamp
if ($AfterUnixTimeStamp != 0)
{
$SQL .= "AND beginning > FROM_UNIXTIME('$AfterUnixTimeStamp')
";
}
// Select all the tasks before this timestamp
if ($BeforeUnixTimeStamp != 0)
{
$SQL .= "AND beginning < FROM_UNIXTIME('$BeforeUnixTimeStamp')
";
}
$TimeResult = mysqli_query($Connection, $SQL)
or die("Could not execute the '$SQL' request.");
$TimeRow = mysqli_fetch_array($TimeResult);
$TotalTime += $TimeRow['length'];
// Recursively do the same thing for all the sub-activities of the current activity
$TotalTime += GetSubActivitiesCumulativeTime($UID, $AID, $AfterUnixTimeStamp, $BeforeUnixTimeStamp);
}
mysqli_free_result($Result);
return $TotalTime;
}
?>