forked from dotproject/dotProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocateduserhours.php
More file actions
481 lines (414 loc) · 19.3 KB
/
allocateduserhours.php
File metadata and controls
481 lines (414 loc) · 19.3 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
if (!defined('DP_BASE_DIR')) {
die('You should not access this file directly.');
}
$coarseness = (int)dPgetParam($_POST, 'coarseness', 1);
$do_report = dPgetParam($_POST, 'do_report', 0);
$hideNonWd = (int)dPgetParam($_POST, 'hideNonWd', 0);
$log_start_date = dPgetCleanParam($_POST, 'log_start_date', 0);
$log_end_date = dPgetCleanParam($_POST, 'log_end_date', 0);
$use_assigned_percentage = (int)dPgetParam($_POST, 'use_assigned_percentage', 0);
$user_id = (int)dPgetParam($_POST, 'user_id', $AppUI->user_id);
// create Date objects from the datetime fields
$start_date = intval($log_start_date) ? new CDate($log_start_date) : new CDate(date('Y-m-01'));
$end_date = intval($log_end_date) ? new CDate($log_end_date) : new CDate();
$end_date->setTime(23, 59, 59);
?>
<script language="javascript">
var calendarField = '';
function popCalendar(field) {
calendarField = field;
idate = eval('document.editFrm.log_' + field + '.value');
window.open('index.php?m=public&a=calendar&dialog=1&callback=setCalendar&date=' + idate, 'calwin', 'width=250, height=220, scrollbars=no, status=no');
}
/**
* @param string Input date in the format YYYYMMDD
* @param string Formatted date
*/
function setCalendar(idate, fdate) {
fld_date = eval('document.editFrm.log_' + calendarField);
fld_fdate = eval('document.editFrm.' + calendarField);
fld_date.value = idate;
fld_fdate.value = fdate;
}
</script>
<form name="editFrm" action="index.php?m=projects&a=reports" method="post">
<input type="hidden" name="project_id" value="<?php echo $project_id;?>" />
<input type="hidden" name="report_category" value="<?php echo $report_category;?>" />
<input type="hidden" name="report_type" value="<?php echo $report_type;?>" />
<table cellspacing="0" cellpadding="4" border="0" width="100%" class="std">
<tr>
<td nowrap="nowrap"><?php echo $AppUI->_('For period');?>:
<input type="hidden" name="log_start_date" value="<?php echo $start_date->format(FMT_TIMESTAMP_DATE);?>" />
<input type="text" name="start_date" value="<?php echo $start_date->format($df);?>" class="text" disabled="disabled" />
<a href="#" onclick="javascript:popCalendar('start_date')">
<img src="./images/calendar.gif" width="24" height="12" alt="<?php echo $AppUI->_('Calendar');?>" border="0" />
</a>
</td>
<td nowrap="nowrap"><?php echo $AppUI->_('to');?>
<input type="hidden" name="log_end_date" value="<?php echo $end_date ? $end_date->format(FMT_TIMESTAMP_DATE) : '';?>" />
<input type="text" name="end_date" value="<?php echo $end_date ? $end_date->format($df) : '';?>" class="text" disabled="disabled" />
<a href="#" onclick="javascript:popCalendar('end_date')">
<img src="./images/calendar.gif" width="24" height="12" alt="<?php echo $AppUI->_('Calendar');?>" border="0" />
</a>
</td>
<td nowrap='nowrap'>
<input type="radio" name="coarseness" value="1" <?php if ($coarseness == 1) echo 'checked' ?> />
<?php echo $AppUI->_('Days');?>
<input type="radio" name="coarseness" value="7" <?php if ($coarseness == 7) echo 'checked' ?> />
<?php echo $AppUI->_('Weeks');?>
</td>
<td nowrap='nowrap'>
<?php
echo $AppUI->_('Tasks created by');
echo ' ';
echo getUsersCombo($user_id);
?>
</td>
</tr>
<tr>
<td nowrap="nowrap" colspan="3" align="center">
<input type="checkbox" name="log_all_projects" id="log_all_projects" value="1" <?php if ($log_all_projects) echo 'checked="checked"' ?> />
<label for="log_all_projects"><?php echo $AppUI->_('Log All Projects');?></label>
<input type="checkbox" name="use_assigned_percentage" id="use_assigned_percentage" value="1" <?php if ($use_assigned_percentage) echo 'checked="checked"' ?> />
<labe for="use_assigned_percentage"><?php echo $AppUI->_('Use assigned percentage');?></label>
<input type="checkbox" name="hideNonWd" id="hideNonWd" value="1" <?php if ($hideNonWd) echo 'checked="checked"' ?> />
<label for="hideNonWd"><?php echo $AppUI->_('Hide non-working days');?></label>
</td>
<td align="left" width="50%" nowrap="nowrap">
<input class="button" type="submit" name="do_report" value="<?php echo $AppUI->_('submit');?>" />
</td>
</tr>
</table>
</form>
<?php
if ($do_report) {
// Let's figure out which users we have
$q = new DBQuery;
$q->addTable('users', 'u');
$q->addQuery('u.user_id, u.user_username, contact_first_name, contact_last_name');
$q->addJoin('contacts', 'c', 'u.user_contact = contact_id');
$user_list = $q->loadHashList('user_id');
$q->clear();
$q = new DBQuery;
$q->addTable('tasks', 't');
$q->addTable('user_tasks', 'ut');
$q->addTable('projects', 'p');
$q->addQuery('t.*, ut.*, p.project_name');
$q->addWhere('(task_start_date BETWEEN "' . $start_date->format(FMT_DATETIME_MYSQL)
. '" AND "' . $end_date->format(FMT_DATETIME_MYSQL)
. '" OR task_end_date BETWEEN "' . $start_date->format(FMT_DATETIME_MYSQL)
. '" AND "' . $end_date->format(FMT_DATETIME_MYSQL)
. '" OR (task_start_date <= "' . $start_date->format(FMT_DATETIME_MYSQL)
. '" AND task_end_date >= "' . $end_date->format(FMT_DATETIME_MYSQL) . '"))');
$q->addWhere('task_end_date IS NOT NULL');
$q->addWhere("task_end_date != '0000-00-00 00:00:00'");
$q->addWhere('task_start_date IS NOT NULL');
$q->addWhere("task_start_date != '0000-00-00 00:00:00'");
$q->addWhere("task_dynamic != '1'");
$q->addWhere("task_milestone = '0'");
$q->addWhere('task_duration > 0');
$q->addWhere('t.task_project = p.project_id');
$q->addWhere('t.task_id = ut.task_id');
if ($user_id) {
$q->addWhere("t.task_owner = '$user_id'");
}
if ($project_id != 0) {
$q->addWhere("t.task_project='$project_id'");
}
$proj = new CProject;
$proj->setAllowedSQL($AppUI->user_id, $q);
$obj = new CTask;
$obj->setAllowedSQL($AppUI->user_id, $q);
$task_list_hash = $q->loadHashList('task_id');
$q->clear();
$task_list = array();
$fetched_projects = array();
foreach ($task_list_hash as $task_id => $task_data) {
$task = new CTask();
$task->bind($task_data);
$task_list[] = $task;
$fetched_projects[$task->task_project] = $task_data['project_name'];
}
$user_usage = array();
$task_dates = array();
$actual_date = $start_date;
$days_header = ''; // we will save days title here
$user_tasks_counted_in = array();
$user_names = array();
if (count($task_list) == 0) {
echo '<p>' . $AppUI->_('No data available') .'</p>';
} else {
foreach ($task_list as $task) {
$task_start_date = new CDate($task->task_start_date);
$task_end_date = new CDate($task->task_end_date);
$day_difference = $task_end_date->dateDiff($task_start_date);
$actual_date = $task_start_date;
$users = $task->getAssignedUsers();
if ($coarseness == 1) {
userUsageDays();
} else if ($coarseness == 7) {
userUsageWeeks();
}
}
if ($coarseness == 1) {
showDays();
} else if ($coarseness == 7) {
showWeeks();
}
?>
<center><table class="std">
<?php echo $table_header . $table_rows; ?>
</table>
<table width="100%"><tr><td align="center">
<?php
echo '<h4>' . $AppUI->_('Total capacity for shown users') . '</h4>';
echo $AppUI->_('Allocated hours').': '.number_format($allocated_hours_sum,2).'<br />';
echo $AppUI->_('Total capacity').': '.number_format($total_hours_capacity,2).'<br />';
echo $AppUI->_('Percentage used').': '. (($total_hours_capacity > 0) ? number_format($allocated_hours_sum/$total_hours_capacity,2)*100 : 0) .'%<br />';
?>
</td>
<td align="center">
<?php
echo '<h4>' . $AppUI->_('Total capacity for all users') . '</h4>';
echo $AppUI->_('Allocated hours').': '.number_format($allocated_hours_sum,2).'<br />';
echo $AppUI->_('Total capacity').': '.number_format($total_hours_capacity_all,2).'<br />';
echo $AppUI->_('Percentage used').': '.(($total_hours_capacity_all > 0) ? number_format($allocated_hours_sum/$total_hours_capacity_all,2)*100 : 0).'%<br />';
}
?>
</td>
</tr>
</table>
</center>
<?php
foreach ($user_tasks_counted_in as $user_id => $project_information) {
echo '<b>'.$user_names[$user_id].'</b><br /><blockquote>';
echo '<table width="50%" border="1" class="std">';
foreach ($project_information as $project_id => $task_information) {
echo '<tr><th colspan="3"><span style="font-weight:bold; font-size:110%">'.$fetched_projects[$project_id].'</span></th></tr>';
$project_total = 0;
foreach ($task_information as $task_id => $hours_assigned) {
echo '<tr><td> </td><td>'.$task_list_hash[$task_id]['task_name'].'</td><td>'.round($hours_assigned,2).' hrs</td></tr>';
$project_total += round($hours_assigned,2);
}
echo '<tr><td colspan="2" align="right"><b>'.$AppUI->_('Total assigned').'</b></td><td><b>' . $project_total . ' hrs</b></td></tr>';
}
echo '</table></blockquote>';
}
}
function userUsageWeeks() {
global $task_start_date, $task_end_date, $day_difference, $hours_added, $actual_date;
global $users, $user_data, $user_usage,$use_assigned_percentage, $user_tasks_counted_in, $task;
global $start_date, $end_date, $AppUI;
$task_duration_per_week = $task->getTaskDurationPerWeek($use_assigned_percentage);
$AppUI->setBaseLocale();
$ted = new CDate(Date_Calc::endOfWeek($task_end_date->day,$task_end_date->month,$task_end_date->year));
$tsd = new CDate(Date_Calc::beginOfWeek($task_start_date->day,$task_start_date->month,$task_start_date->year));
$ed = new CDate(Date_Calc::endOfWeek($end_date->day,$end_date->month,$end_date->year));
$sd = new CDate(Date_Calc::beginOfWeek($start_date->day,$start_date->month,$start_date->year));
setlocale(LC_ALL, $AppUI->user_lang);
$week_difference = $end_date->workingDaysInSpan($start_date)/count(explode(",",dPgetConfig("cal_working_days")));
$actual_date = $start_date;
for ($i = 0; $i<=$week_difference; $i++) {
if (!$actual_date->before($tsd) && !$actual_date->after($ted)) {
$AppUI->setBaseLocale();
$awoy = $actual_date->year.Date_Calc::weekOfYear($actual_date->day,$actual_date->month,$actual_date->year);
setlocale(LC_ALL, $AppUI->user_lang);
foreach ($users as $user_id => $user_data) {
if (!isset($user_usage[$user_id][$awoy])) {
$user_usage[$user_id][$awoy] = 0;
}
$percentage_assigned = $use_assigned_percentage ? ($user_data['perc_assignment']/100) : 1;
$hours_added = $task_duration_per_week * $percentage_assigned;
$user_usage[$user_id][$awoy] += $hours_added;
if ($user_usage[$user_id][$awoy] < 0.005) {
// We want to show at least 0.01 even when the assigned time is very small
// so we know that at that time the user has a running task
$user_usage[$user_id][$awoy] += 0.006;
$hours_added += 0.006;
}
// Let's register the tasks counted in for calculation
if (!array_key_exists($user_id, $user_tasks_counted_in)) {
$user_tasks_counted_in[$user_id] = array();
}
if (!array_key_exists($task->task_project, $user_tasks_counted_in[$user_id])) {
$user_tasks_counted_in[$user_id][$task->task_project] = array();
}
if (!array_key_exists($task->task_id, $user_tasks_counted_in[$user_id][$task->task_project])) {
$user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] = 0;
}
// We add it up
$user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] += $hours_added;
}
}
$actual_date->addSeconds(168*3600); // + one week
}
}
function showWeeks() {
global $allocated_hours_sum, $end_date, $start_date, $AppUI, $user_list, $user_names;
global $user_usage, $hideNonWd, $table_header, $table_rows, $df, $working_days_count;
global $total_hours_capacity, $total_hours_capacity_all;
$working_days_count = 0;
$allocated_hours_sum = 0;
$AppUI->setBaseLocale();
$ed = new CDate(Date_Calc::endOfWeek($end_date->day,$end_date->month,$end_date->year));
$sd = new CDate(Date_Calc::beginOfWeek($start_date->day,$start_date->month,$start_date->year));
setlocale(LC_ALL, $AppUI->user_lang);
$week_difference = ceil($ed->workingDaysInSpan($sd)/count(explode(',',dPgetConfig('cal_working_days'))));
$actual_date = $sd;
$table_header = '<tr><th>'.$AppUI->_('User').'</th>';
for ($i=0; $i<$week_difference; $i++) {
$actual_date->addSeconds(168*3600); // + one week
$working_days_count = $working_days_count + count(explode(',',dPgetConfig('cal_working_days')));
}
$table_header .= '<th nowrap="nowrap" colspan="2">'.$AppUI->_('Allocated').'</th></tr>';
$table_rows = '';
foreach ($user_list as $user_id => $user_data) {
@$user_names[$user_id] = $user_data['user_username'];
if (isset($user_usage[$user_id])) {
$table_rows .= '<tr><td nowrap="nowrap">('.$user_data['user_username'].') '.$user_data['contact_first_name'].' '.$user_data['contact_last_name'].'</td>';
$actual_date = $sd;
/*
for ($i=0; $i<$week_difference; $i++) {
setlocale(LC_ALL, 'en_AU'.(($locale_char_set)? ('.' . $locale_char_set) : '.utf8'));
$awoy = $actual_date->year.Date_Calc::weekOfYear($actual_date->day,$actual_date->month,$actual_date->year);
setlocale(LC_ALL, $AppUI->user_lang);
$table_rows .= '<td align="right">';
if (isset($user_usage[$user_id][$awoy])) {
$hours = number_format($user_usage[$user_id][$awoy],2);
$table_rows .= $hours;
$percentage_used = round(($hours/(dPgetConfig('daily_working_hours')*count(explode(',',dPgetConfig('cal_working_days')))))*100);
$bar_color = 'blue';
if ($percentage_used > 100) {
$bar_color = 'red';
$percentage_used = 100;
}
$table_rows .= '<div style="height:2px;width:' . $percentage_used . '%; background-color:' . $bar_color . '"> </div>';
} else {
$table_rows .= ' ';
}
$table_rows .= '</td>';
$actual_date->addSeconds(168*3600); // + one week
}
*/
$array_sum = array_sum($user_usage[$user_id]);
$average_user_usage = number_format(($array_sum/($week_difference * count(explode(',',dPgetConfig('cal_working_days')))*dPgetConfig('daily_working_hours')))*100, 2);
$allocated_hours_sum += $array_sum;
$bar_color = 'blue';
if ($average_user_usage > 100) {
$bar_color = 'red';
$average_user_usage = 100;
}
$table_rows .= '<td><div align="left">'.round($array_sum, 2).' '.$AppUI->_('hours').'</td> <td align="right"> ' . $average_user_usage;
$table_rows .= '%</div>';
$table_rows .= '<div align="left" style="height:2px;width:' . $average_user_usage . '%; background-color:' . $bar_color . '"> </div></td>';
$table_rows .= '</tr>';
}
}
/*
$total_hours_capacity = $week_difference * count(explode(',',dPgetConfig('cal_working_days'))) * dPgetConfig('daily_working_hours') * count($user_usage);
$total_hours_capacity_all = $week_difference * count(explode(',',dPgetConfig('cal_working_days'))) *dPgetConfig('daily_working_hours') * count($user_list);
*/
$total_hours_capacity = $working_days_count/2*dPgetConfig('daily_working_hours')*count($user_usage);
$total_hours_capacity_all = $working_days_count/2*dPgetConfig('daily_working_hours')*count($user_list);
}
function userUsageDays() {
global $task_start_date, $task_end_date, $day_difference, $hours_added, $actual_date, $users;
global $user_data, $user_usage,$use_assigned_percentage, $user_tasks_counted_in, $task;
global $start_date, $end_date;
$task_duration_per_day = $task->getTaskDurationPerDay($use_assigned_percentage);
for ($i = 0; $i<=$day_difference; $i++) {
if (!$actual_date->before($start_date) && !$actual_date->after($end_date)
&& $actual_date->isWorkingDay()) {
foreach ($users as $user_id => $user_data) {
if (!isset($user_usage[$user_id][$actual_date->format('%Y%m%d')])) {
$user_usage[$user_id][$actual_date->format('%Y%m%d')] = 0;
}
$percentage_assigned = $use_assigned_percentage ? ($user_data['perc_assignment']/100) : 1;
$hours_added = $task_duration_per_day * $percentage_assigned;
$user_usage[$user_id][$actual_date->format('%Y%m%d')] += $hours_added;
if ($user_usage[$user_id][$actual_date->format('%Y%m%d')] < 0.005) {
// We want to show at least 0.01 even when the assigned time is very small
// so we know that at that time the user has a running task
$user_usage[$user_id][$actual_date->format('%Y%m%d')] += 0.006;
$hours_added += 0.006;
}
// Let's register the tasks counted in for calculation
if (!array_key_exists($user_id, $user_tasks_counted_in)) {
$user_tasks_counted_in[$user_id] = array();
}
if (!array_key_exists($task->task_project, $user_tasks_counted_in[$user_id])) {
$user_tasks_counted_in[$user_id][$task->task_project] = array();
}
if (!array_key_exists($task->task_id, $user_tasks_counted_in[$user_id][$task->task_project])) {
$user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] = 0;
}
// We add it up
$user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] += $hours_added;
}
}
$actual_date->addDays(1);
}
}
function showDays() {
global $allocated_hours_sum, $end_date, $start_date, $AppUI, $user_list, $user_names;
global $user_usage, $hideNonWd, $table_header, $table_rows, $df, $working_days_count;
global $total_hours_capacity, $total_hours_capacity_all;
$days_difference = $end_date->dateDiff($start_date);
$actual_date = $start_date;
$working_days_count = 0;
$allocated_hours_sum = 0;
$table_header = '<tr><th>'.$AppUI->_('User').'</th>';
for ($i=0; $i<=$days_difference; $i++) {
if ($actual_date->isWorkingDay()) {
$working_days_count++;
}
$actual_date->addDays(1);
}
$table_header .= '<th nowrap="nowrap" colspan="2">'.$AppUI->_('Allocated').'</th></tr>';
$table_rows = '';
foreach ($user_list as $user_id => $user_data) {
@$user_names[$user_id] = $user_data['user_username'];
if (isset($user_usage[$user_id])) {
$table_rows .= '<tr><td nowrap="nowrap">('.$user_data['user_username'].') '.$user_data['contact_first_name'].' '.$user_data['contact_last_name'].'</td>';
$actual_date = $start_date;
/*
for ($i=0; $i<=$days_difference; $i++) {
if (($actual_date->isWorkingDay()) || (!$actual_date->isWorkingDay() && !$hideNonWd)) {
$table_rows .= '<td>';
if (isset($user_usage[$user_id][$actual_date->format('%Y%m%d')])) {
$hours = number_format($user_usage[$user_id][$actual_date->format('%Y%m%d')],2);
$table_rows .= $hours;
$percentage_used = round($hours/dPgetConfig('daily_working_hours')*100);
$bar_color = 'blue';
if ($percentage_used > 100) {
$bar_color = 'red';
$percentage_used = 100;
}
$table_rows .= '<div style="height:2px;width:' . $percentage_used . '%; background-color:' . $bar_color . '"> </div>';
} else {
$table_rows .= ' ';
}
$table_rows .= '</td>';
}
$actual_date->addDays(1);
}
*/
$array_sum = array_sum($user_usage[$user_id]);
$average_user_usage = number_format(($array_sum/($working_days_count*dPgetConfig('daily_working_hours')))*100, 2);
$allocated_hours_sum += $array_sum;
$bar_color = 'blue';
if ($average_user_usage > 100) {
$bar_color = 'red';
$average_user_usage = 100;
}
$table_rows .= '<td><div align="left">'.round($array_sum, 2).' '.$AppUI->_('hours').'</td> <td align="right"> '.$average_user_usage ;
$table_rows .= '%</div>';
$table_rows .= '<div align="left" style="height:2px;width:' . $average_user_usage . '%; background-color:' . $bar_color . '"> </div></td>';
$table_rows .= '</tr>';
}
}
$total_hours_capacity = $working_days_count*dPgetConfig('daily_working_hours')*count($user_usage);
$total_hours_capacity_all = $working_days_count*dPgetConfig('daily_working_hours')*count($user_list);
}
?>