forked from KETSE/casebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tasks.php
217 lines (181 loc) · 5.57 KB
/
Tasks.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
<?php
namespace CB;
use CB\L;
use CB\User;
use CB\DataModel as DM;
class Tasks
{
/**
* update dates of a event or task (startDate, endDate)
* @param object $p params object containing id and dates
* @return json response
*/
public function updateDates($p)
{
$rez = array('success' => true);
if (!Security::canManageTask($p['id'])) {
throw new \Exception(L\get('Access_denied'), 1);
}
$obj = Objects::getCachedObject($p['id']);
$data = $obj->getData();
$data['cdate'] = $p['date_start'];
if (empty($p['date_end'])) {
unset($data['data']['due_date']);
unset($data['data']['due_time']);
} else {
$data['data']['due_date'] = $p['date_end'];
if (substr($p['date_end'], 12, 5) == '00:00') {
unset($data['data']['due_time']);
} else {
$data['data']['due_time'] = $p['date_end'];
}
}
$obj->update($data);
$this->afterUpdate($p['id']);
return $rez;
}
/**
* set complete or incomplete status for a task responsible user
* @param array $p params
*/
public function setUserStatus($p)
{
$rez = array(
'success' => true
,'id' => $p['id']
);
$obj = Objects::getCachedObject($p['id']);
$data = $obj->getData();
if ((User::getId() != $data['cid']) && !Security::isAdmin()) {
throw new \Exception(L\get('Access_denied'));
}
if ($obj->getUserStatus($p['user_id']) == Objects\Task::$USERSTATUS_NONE) {
throw new \Exception(L\get('Wrong_id'));
}
$status = ($p['status'] == 1)
? Objects\Task::$USERSTATUS_DONE
: Objects\Task::$USERSTATUS_ONGOING;
$obj->setUserStatus($status, $p['user_id']);
// $obj->updateSysData();
$this->afterUpdate($p['id']);
return $rez;
}
/**
* task completion method for currently authenticated user
*
* @param array $p {
* int $id task id
* }
* @return array json responce
*/
public function complete($p)
{
/* check if current user can manage this task */
if (!Security::canManageTask($p['id'])) {
throw new \Exception(L\get('Access_denied'));
}
$obj = Objects::getCachedObject($p['id']);
if ($obj->getUserStatus() != Objects\Task::$USERSTATUS_ONGOING) {
throw new \Exception(L\get('Task_already_completed'));
}
$obj->setUserStatus(Objects\Task::$USERSTATUS_DONE);
// $obj->updateSysData();
$this->afterUpdate($p['id']);
return array('success' => true);
}
/**
* method for marking task as closed
* @param int $id task id
* @return json response
*/
public function close($id)
{
return $this->changeStatus($id, Objects\Task::$STATUS_CLOSED);
}
/**
* reopen a task
* @param int $id
* @return json response
*/
public function reopen($id)
{
return $this->changeStatus($id, Objects\Task::$STATUS_ACTIVE);
}
/**
* change status for a task
* @param int $status
* @param int $id
* @return json response
*/
protected function changeStatus($id, $status)
{
$obj = Objects::getCachedObject($id);
$data = $obj->getData();
//status change for task is allowed only for owner or admin
if (!$obj->isOwner() && !Security::isAdmin()) {
return array(
'success' => false
,'msg' => L\get('No_access_for_this_action')
);
}
switch ($status) {
case Objects\Task::$STATUS_ACTIVE:
$obj->setActive();
break;
case Objects\Task::$STATUS_CLOSED:
$obj->setClosed();
break;
default:
return array(
'success' => false
,'id' => $id
);
}
$this->afterUpdate($id);
return array(
'success' => true
,'id' => $id
);
}
/**
* method called after a task have been updated
* used now to update solr and cases date
* @param int $taskId
* @return void
*/
protected function afterUpdate($taskId)
{
Objects::updateCaseUpdateInfo($taskId);
$solr = new Solr\Client();
$solr->updateTree(array('id' => $taskId));
}
/**
* set the flags for actions that could be made to the tasks by a specific or current user
* @param reference $object_records
* @return void
*/
public static function setTaskActionFlags(&$taskData, $userId = false)
{
$p = array(&$taskData);
static::setTasksActionFlags($p, $userId);
}
/**
* set the flags for actions that could be made to the tasks by a specific or current user
* @param reference $object_records
* @return void
*/
public static function setTasksActionFlags(&$tasksDataArray, $userId = false)
{
$userId = $userId; // dummy codacy assignment
$taskTemplates = DM\Templates::getIdsByType('task');
foreach ($tasksDataArray as &$d) {
if ((!in_array(@$d['template_id'], $taskTemplates)) ||
empty($d['status'])
) {
continue;
}
$task = Objects::getCachedObject($d['id']);
$d['can'] = $task->getActionFlags();
}
}
}