|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.mypage |
|
4
|
|
|
* @author Nemein Oy http://www.nemein.com/ |
|
5
|
|
|
* @copyright Nemein Oy http://www.nemein.com/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* org.openpsa.mypage "now working on" handler |
|
11
|
|
|
* |
|
12
|
|
|
* @package org.openpsa.mypage |
|
13
|
|
|
*/ |
|
14
|
|
|
class org_openpsa_mypage_workingon |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Time person started working on the task |
|
18
|
|
|
*/ |
|
19
|
|
|
public $start = 0; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Time spent working on the task, in seconds |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $time = 0; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Task being worked on |
|
28
|
|
|
*/ |
|
29
|
|
|
public $task; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The description for the current hour report |
|
33
|
|
|
*/ |
|
34
|
|
|
public $description = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Person working on the task |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $person; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* If hour report is invoiceable |
|
43
|
|
|
*/ |
|
44
|
|
|
public $invoiceable = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param midcom_db_person $person Person to handle "now working on" for. By default current user |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function __construct(midcom_db_person $person = null) |
|
50
|
|
|
{ |
|
51
|
4 |
|
$this->person = $person ?? midcom::get()->auth->user->get_storage(); |
|
52
|
|
|
|
|
53
|
|
|
// Figure out what the person is working on |
|
54
|
4 |
|
if ($workingon = $this->person->get_parameter('org.openpsa.mypage', 'workingon')) { |
|
55
|
|
|
$workingon = json_decode($workingon); |
|
56
|
|
|
$task_time = strtotime($workingon->start . " GMT"); |
|
57
|
|
|
|
|
58
|
|
|
// Set the protected vars |
|
59
|
|
|
$this->task = new org_openpsa_projects_task_dba($workingon->task); |
|
60
|
|
|
$this->time = time() - $task_time; |
|
61
|
|
|
$this->start = $task_time; |
|
62
|
|
|
$this->description = $workingon->description; |
|
63
|
|
|
$this->invoiceable = $workingon->invoiceable; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set a task the user works on. If user was previously working on something else hours will be reported automatically. |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function set(string $task_guid) : bool |
|
71
|
|
|
{ |
|
72
|
1 |
|
$description = trim($_POST['description']); |
|
73
|
1 |
|
$invoiceable = isset($_POST['invoiceable']) && $_POST['invoiceable'] == 'true'; |
|
74
|
1 |
|
midcom::get()->auth->request_sudo('org.openpsa.mypage'); |
|
75
|
1 |
|
if ($this->task) { |
|
76
|
|
|
// We were previously working on another task. Report hours |
|
77
|
|
|
if (!$description) { |
|
78
|
|
|
// Generate a message |
|
79
|
|
|
$l10n = midcom::get()->i18n->get_l10n('org.openpsa.mypage'); |
|
80
|
|
|
$formatter = $l10n->get_formatter(); |
|
81
|
|
|
$description = sprintf($l10n->get('worked from %s to %s'), $formatter->time($this->start), $formatter->time()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Do the actual report |
|
85
|
|
|
$this->_report_hours($description, $invoiceable); |
|
86
|
|
|
} |
|
87
|
1 |
|
if (!$task_guid) { |
|
88
|
|
|
// We won't be working on anything from now on. Delete existing parameter |
|
89
|
1 |
|
$stat = $this->person->delete_parameter('org.openpsa.mypage', 'workingon'); |
|
90
|
|
|
} else { |
|
91
|
|
|
// Mark the new task work session as started |
|
92
|
|
|
$workingon = [ |
|
93
|
|
|
'task' => $task_guid, |
|
94
|
|
|
'description' => $description, |
|
95
|
|
|
'invoiceable' => $invoiceable, |
|
96
|
|
|
'start' => gmdate('Y-m-d H:i:s', time()) |
|
97
|
|
|
]; |
|
98
|
|
|
$stat = $this->person->set_parameter('org.openpsa.mypage', 'workingon', json_encode($workingon)); |
|
99
|
|
|
} |
|
100
|
1 |
|
midcom::get()->auth->drop_sudo(); |
|
101
|
1 |
|
return $stat; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Report hours based on time used |
|
106
|
|
|
*/ |
|
107
|
|
|
private function _report_hours(string $description, bool $invoiceable) |
|
108
|
|
|
{ |
|
109
|
|
|
$hour_report = new org_openpsa_expenses_hour_report_dba(); |
|
110
|
|
|
$hour_report->invoiceable = $invoiceable; |
|
111
|
|
|
$hour_report->date = $this->start; |
|
112
|
|
|
$hour_report->person = $this->person->id; |
|
113
|
|
|
$hour_report->task = $this->task->id; |
|
114
|
|
|
$hour_report->description = $description; |
|
115
|
|
|
$hour_report->hours = $this->time / 3600; |
|
116
|
|
|
//apply minimum_time_slot |
|
117
|
|
|
$hour_report->modify_hours_by_time_slot(); |
|
118
|
|
|
|
|
119
|
|
|
if (!$hour_report->create()) { |
|
120
|
|
|
midcom::get()->uimessages->add(midcom::get()->i18n->get_string('org.openpsa.mypage', 'org.openpsa.mypage'), sprintf(midcom::get()->i18n->get_string('reporting %f hours to task %s failed, reason %s', 'org.openpsa.mypage'), $hour_report->hours, $this->task->title, midcom_connection::get_error_string()), 'error'); |
|
121
|
|
|
} else { |
|
122
|
|
|
midcom::get()->uimessages->add(midcom::get()->i18n->get_string('org.openpsa.mypage', 'org.openpsa.mypage'), sprintf(midcom::get()->i18n->get_string('successfully reported %f hours to task %s', 'org.openpsa.mypage'), $hour_report->hours, $this->task->title)); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|