Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
timelinetask.php
1<?php
2
4
7
8class TimelineTask implements \JsonSerializable
9{
10 private Task $task;
11 private ?int $userId = null;
12 private ?string $approveType = null;
13
14 public function __construct(Task $task)
15 {
16 $this->task = $task;
17 if ($this->hasApproveType())
18 {
19 $parameters = $this->task->getParameters();
20 $this->approveType = is_string($parameters['ApproveType'] ?? null) ? $parameters['ApproveType'] : 'all';
21 }
22 }
23
24 public function setUserId(int $userId): static
25 {
26 $this->userId = $userId;
27
28 return $this;
29 }
30
31 public function calculateExecutionTime(): ?int
32 {
33 $createdDate = $this->getCreatedDate();
34 if (isset($createdDate))
35 {
36 return
37 $this->task->isCompleted()
38 ? $this->task->getModified()->getTimestamp() - $createdDate->getTimestamp()
39 : (new DateTime())->getTimestamp() - $createdDate->getTimestamp()
40 ;
41 }
42
43 return null;
44 }
45
49 public function getTaskUserIds(): array
50 {
51 $ids = [];
52 foreach ($this->task->getTaskUsers() as $taskUser)
53 {
54 $ids[] = $taskUser->getUserId();
55 }
56
57 return $ids;
58 }
59
60 private function checkViewRights(): bool
61 {
62 return isset($this->userId) && $this->task->hasViewRights($this->userId);
63 }
64
65 public function jsonSerialize(): array
66 {
67 $users = [];
68 if (!$this->checkViewRights())
69 {
70 return [
71 'canView' => false,
72 'status' => $this->task->getStatus(),
73 ];
74 }
75
76 foreach ($this->task->getTaskUsers() as $taskUser)
77 {
78 $userData = [
79 'id' => $taskUser->getUserId(),
80 'status' => $taskUser->getStatus(),
81 ];
82
83 if ($taskUser->hasDateUpdate() && $taskUser->get('DATE_UPDATE') !== null)
84 {
85 $dateUpdate = $taskUser->get('DATE_UPDATE');
86 }
87 else
88 {
89 $dateUpdate = new DateTime();
90 }
91
92 if ($this->getCreatedDate())
93 {
94 $userData['executionTime'] = $dateUpdate->getTimestamp() - $this->getCreatedDate()->getTimestamp();
95 }
96
97 $users[] = $userData;
98 }
99
100 $viewData = [
101 'canView' => true,
102 'id' => $this->task->getId(),
103 'name' => $this->task->getName(),
104 'status' => $this->task->getStatus(),
105 'modified' => $this->task->getModified()->getTimestamp(),
106 'executionTime' => $this->calculateExecutionTime(),
107 'users' => $users,
108 ];
109
110 if ($this->approveType)
111 {
112 $viewData['approveType'] = $this->approveType;
113 }
114
115 return $viewData;
116 }
117
118 private function getCreatedDate(): ?DateTime
119 {
120 return $this->task->hasCreatedDate() ? $this->task->get('CREATED_DATE') : null;
121 }
122
123 public function hasApproveType(): int
124 {
125 return in_array(
126 $this->task->getActivity(),
127 [
128 'ApproveActivity',
129 'ReviewActivity',
130 ],
131 true,
132 );
133 }
134}