5205 |
efrain |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Controller;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
6849 |
efrain |
7 |
|
5205 |
efrain |
8 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
9 |
use Laminas\Log\LoggerInterface;
|
|
|
10 |
use Laminas\View\Model\JsonModel;
|
|
|
11 |
use LeadersLinked\Mapper\CompanyMapper;
|
|
|
12 |
use LeadersLinked\Mapper\DailyPulseEmojiMapper;
|
|
|
13 |
use LeadersLinked\Mapper\DailyPulseRecordMapper;
|
|
|
14 |
use LeadersLinked\Mapper\EngagementUserMapper;
|
|
|
15 |
use LeadersLinked\Model\DailyPulseRecord;
|
|
|
16 |
use LeadersLinked\Model\DailyPulseEmoji;
|
|
|
17 |
use LeadersLinked\Mapper\EngagementMapper;
|
|
|
18 |
use LeadersLinked\Model\EngagementUser;
|
|
|
19 |
use LeadersLinked\Mapper\EngagementRecordMapper;
|
|
|
20 |
use LeadersLinked\Model\EngagementRecord;
|
|
|
21 |
|
|
|
22 |
class DailyPulseController extends AbstractActionController
|
|
|
23 |
{
|
|
|
24 |
/**
|
|
|
25 |
*
|
|
|
26 |
* @var AdapterInterface
|
|
|
27 |
*/
|
|
|
28 |
private $adapter;
|
6849 |
efrain |
29 |
|
5205 |
efrain |
30 |
/**
|
|
|
31 |
*
|
|
|
32 |
* @var LoggerInterface
|
|
|
33 |
*/
|
|
|
34 |
private $logger;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
*
|
|
|
38 |
* @var array
|
|
|
39 |
*/
|
|
|
40 |
private $config;
|
6849 |
efrain |
41 |
|
5205 |
efrain |
42 |
/**
|
|
|
43 |
*
|
|
|
44 |
* @param AdapterInterface $adapter
|
|
|
45 |
* @param LoggerInterface $logger
|
|
|
46 |
* @param array $config
|
|
|
47 |
*/
|
6849 |
efrain |
48 |
public function __construct($adapter, $logger, $config)
|
5205 |
efrain |
49 |
{
|
|
|
50 |
$this->adapter = $adapter;
|
|
|
51 |
$this->logger = $logger;
|
|
|
52 |
$this->config = $config;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function indexAction()
|
|
|
56 |
{
|
|
|
57 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
58 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
59 |
|
|
|
60 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
61 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
62 |
|
|
|
63 |
$companyMapper = CompanyMapper::getInstance($this->adapter);
|
|
|
64 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
$request = $this->getRequest();
|
|
|
68 |
if($request->isGet()) {
|
|
|
69 |
|
|
|
70 |
$emojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
$engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
|
|
|
75 |
$engagementUser = $engagementUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
|
|
|
76 |
|
|
|
77 |
if($engagementUser) {
|
|
|
78 |
$points = $engagementUser->points;
|
|
|
79 |
} else {
|
|
|
80 |
$points = 0;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$dailyRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
|
|
|
84 |
|
|
|
85 |
$howAreYouFeelRecord = $dailyRecordMapper->fetchOneCurrentByUserIdAndType($currentUser->id, DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL);
|
|
|
86 |
$climateOnYourOrganization = $dailyRecordMapper->fetchOneCurrentByUserIdAndType($currentUser->id, DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION);
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
$emojis_how_are_you_feel = [];
|
|
|
90 |
$emojis_climate_on_your_organization = [];
|
|
|
91 |
|
|
|
92 |
$records = $emojiMapper->fetchAllActiveByCompanyId($company->id);
|
|
|
93 |
foreach($records as $record)
|
|
|
94 |
{
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
if($record->type == DailyPulseEmoji::TYPE_HOW_ARE_YOU_FEEL) {
|
|
|
100 |
|
|
|
101 |
if($howAreYouFeelRecord) {
|
|
|
102 |
$link_save = '';
|
|
|
103 |
if($howAreYouFeelRecord->emoji_id != $record->id) {
|
|
|
104 |
continue;
|
|
|
105 |
}
|
|
|
106 |
} else {
|
|
|
107 |
$link_save = $this->url()->fromRoute('daily-pulse/how_are_you_feel', ['id' => $record->uuid]);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
array_push($emojis_how_are_you_feel, [
|
|
|
111 |
'id' => $record->uuid,
|
|
|
112 |
'link_save' => $link_save,
|
|
|
113 |
'image' => $this->url()->fromRoute('storage', ['code' => $record->uuid, 'type' => 'daily-pulse', 'filename' => $record->image]),
|
|
|
114 |
]);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if($record->type == DailyPulseEmoji::TYPE_CLIMATE_ON_YOUR_ORGANIZATION) {
|
|
|
118 |
|
|
|
119 |
if($climateOnYourOrganization) {
|
|
|
120 |
$link_save = '';
|
|
|
121 |
if($climateOnYourOrganization->emoji_id != $record->id) {
|
|
|
122 |
continue;
|
|
|
123 |
}
|
|
|
124 |
} else {
|
|
|
125 |
$link_save = $this->url()->fromRoute('daily-pulse/climate_on_your_organization', ['id' => $record->uuid]);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
array_push($emojis_climate_on_your_organization, [
|
|
|
129 |
'id' => $record->uuid,
|
|
|
130 |
'link_save' => $link_save,
|
|
|
131 |
'image' => $this->url()->fromRoute('storage', ['code' => $record->uuid, 'type' => 'daily-pulse', 'filename' => $record->image]),
|
|
|
132 |
]);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
$data = [
|
|
|
141 |
'success' => true,
|
|
|
142 |
'data' => [
|
|
|
143 |
'points' => $points,
|
|
|
144 |
'emojis_how_are_you_feel' => $emojis_how_are_you_feel,
|
|
|
145 |
'emojis_climate_on_your_organization' => $emojis_climate_on_your_organization,
|
|
|
146 |
|
|
|
147 |
]
|
|
|
148 |
];
|
|
|
149 |
|
|
|
150 |
return new JsonModel($data);
|
|
|
151 |
|
|
|
152 |
} else {
|
|
|
153 |
$data = [
|
|
|
154 |
'success' => false,
|
|
|
155 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
156 |
];
|
|
|
157 |
|
|
|
158 |
return new JsonModel($data);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return new JsonModel($data);
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
public function howAreYouFeelAction()
|
|
|
168 |
{
|
|
|
169 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
170 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
171 |
|
|
|
172 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
173 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
174 |
|
|
|
175 |
$companyMapper = CompanyMapper::getInstance($this->adapter);
|
|
|
176 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
|
|
|
177 |
|
|
|
178 |
$id = $this->params()->fromRoute('id');
|
|
|
179 |
$emojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
|
|
|
180 |
$emoji = $emojiMapper->fetchOneByUuid($id);
|
|
|
181 |
|
|
|
182 |
if(!$emoji) {
|
|
|
183 |
|
|
|
184 |
$data = [
|
|
|
185 |
'success' => false,
|
|
|
186 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_NOT_FOUND'
|
|
|
187 |
];
|
|
|
188 |
|
|
|
189 |
return new JsonModel($data);
|
|
|
190 |
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
if($emoji->status == DailyPulseEmoji::STATUS_INACTIVE) {
|
|
|
194 |
$data = [
|
|
|
195 |
'success' => false,
|
|
|
196 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_IS_INACTIVE'
|
|
|
197 |
];
|
|
|
198 |
|
|
|
199 |
return new JsonModel($data);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
if($emoji->type != DailyPulseEmoji::TYPE_HOW_ARE_YOU_FEEL) {
|
|
|
203 |
$data = [
|
|
|
204 |
'success' => false,
|
|
|
205 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_IS_WRONG_TYPE'
|
|
|
206 |
];
|
|
|
207 |
|
|
|
208 |
return new JsonModel($data);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
if($emoji->company_id != $company->id) {
|
|
|
213 |
$data = [
|
|
|
214 |
'success' => false,
|
|
|
215 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
216 |
];
|
|
|
217 |
|
|
|
218 |
return new JsonModel($data);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$request = $this->getRequest();
|
|
|
222 |
if($request->isPost()) {
|
|
|
223 |
|
|
|
224 |
$dailyPulseRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
|
|
|
225 |
$dailyPulse = $dailyPulseRecordMapper->fetchOneCurrentByUserIdAndType($currentUser->id, DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL);
|
|
|
226 |
|
|
|
227 |
if($dailyPulse) {
|
|
|
228 |
|
|
|
229 |
$data = [
|
|
|
230 |
'success' => false,
|
|
|
231 |
'data' => 'ERROR_DAILY_PULSE_RECORD_USER_ALREADY_FOUND'
|
|
|
232 |
];
|
|
|
233 |
|
|
|
234 |
} else {
|
|
|
235 |
$engagementMapper = EngagementMapper::getInstance($this->adapter);
|
|
|
236 |
$engagement = $engagementMapper->fetchOneByCompanyId($company->id);
|
|
|
237 |
|
|
|
238 |
$dailyPulse = new DailyPulseRecord();
|
|
|
239 |
$dailyPulse->company_id = $company->id;
|
|
|
240 |
$dailyPulse->user_id = $currentUser->id;
|
|
|
241 |
$dailyPulse->emoji_id = $emoji->id;
|
|
|
242 |
$dailyPulse->type = DailyPulseRecord::TYPE_HOW_ARE_YOU_FEEL;
|
|
|
243 |
$dailyPulse->engagement = $engagement ? $engagement->daily_pulse_how_are_you_feel : 0;
|
|
|
244 |
$dailyPulse->points = $emoji->points;
|
|
|
245 |
|
|
|
246 |
$result = $dailyPulseRecordMapper->insert($dailyPulse);
|
|
|
247 |
if($result) {
|
6001 |
efrain |
248 |
|
|
|
249 |
if($engagement) {
|
|
|
250 |
$engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
|
|
|
251 |
$engagementUser = $engagementUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
|
|
|
252 |
|
|
|
253 |
if($engagementUser) {
|
|
|
254 |
$engagementUser->points = $engagementUser->points + ($engagement ? $engagement->daily_pulse_how_are_you_feel : 0);
|
|
|
255 |
$engagementUserMapper->update( $engagementUser );
|
|
|
256 |
} else {
|
|
|
257 |
$engagementUser = new EngagementUser();
|
|
|
258 |
$engagementUser->company_id = $company->id;
|
|
|
259 |
$engagementUser->user_id = $currentUser->id;
|
|
|
260 |
$engagementUser->points = $engagement ? $engagement->daily_pulse_how_are_you_feel : 0;
|
|
|
261 |
$engagementUserMapper->insert( $engagementUser );
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
$engagementRecord = new EngagementRecord();
|
|
|
265 |
$engagementRecord->company_id = $company->id;
|
|
|
266 |
$engagementRecord->user_id = $currentUser->id;
|
|
|
267 |
$engagementRecord->points = $engagement->daily_pulse_how_are_you_feel;
|
|
|
268 |
$engagementRecord->type = EngagementRecord::TYPE_DAILY_PULSE_HOW_ARE_YOU_FEEL;
|
|
|
269 |
|
|
|
270 |
$engagementRecordMapper = EngagementRecordMapper::getInstance($this->adapter);
|
|
|
271 |
$engagementRecordMapper->insert($engagementRecord);
|
5205 |
efrain |
272 |
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
$data = [
|
|
|
276 |
'success' => true,
|
|
|
277 |
'data' => 'LABEL_DAILY_PULSE_EMOJI_ADDED'
|
|
|
278 |
];
|
|
|
279 |
} else {
|
|
|
280 |
$data = [
|
|
|
281 |
'success' => false,
|
|
|
282 |
'data' => $dailyPulseRecordMapper->getError()
|
|
|
283 |
];
|
|
|
284 |
}
|
|
|
285 |
}
|
|
|
286 |
} else {
|
|
|
287 |
$data = [
|
|
|
288 |
'success' => false,
|
|
|
289 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
290 |
];
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
return new JsonModel($data);
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
public function climateOnYourOrganizationAction()
|
|
|
297 |
{
|
|
|
298 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
299 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
300 |
|
|
|
301 |
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
|
|
|
302 |
$currentNetwork = $currentNetworkPlugin->getNetwork();
|
|
|
303 |
|
|
|
304 |
$companyMapper = CompanyMapper::getInstance($this->adapter);
|
|
|
305 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
|
|
|
306 |
|
|
|
307 |
$id = $this->params()->fromRoute('id');
|
|
|
308 |
$emojiMapper = DailyPulseEmojiMapper::getInstance($this->adapter);
|
|
|
309 |
$emoji = $emojiMapper->fetchOneByUuid($id);
|
|
|
310 |
|
|
|
311 |
if(!$emoji) {
|
|
|
312 |
|
|
|
313 |
$data = [
|
|
|
314 |
'success' => false,
|
|
|
315 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_NOT_FOUND'
|
|
|
316 |
];
|
|
|
317 |
|
|
|
318 |
return new JsonModel($data);
|
|
|
319 |
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
if($emoji->status == DailyPulseEmoji::STATUS_INACTIVE) {
|
|
|
323 |
$data = [
|
|
|
324 |
'success' => false,
|
|
|
325 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_IS_INACTIVE'
|
|
|
326 |
];
|
|
|
327 |
|
|
|
328 |
return new JsonModel($data);
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
if($emoji->type != DailyPulseEmoji::TYPE_CLIMATE_ON_YOUR_ORGANIZATION) {
|
|
|
332 |
$data = [
|
|
|
333 |
'success' => false,
|
|
|
334 |
'data' => 'ERROR_DAILY_PULSE_EMOJI_IS_WRONG_TYPE'
|
|
|
335 |
];
|
|
|
336 |
|
|
|
337 |
return new JsonModel($data);
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
if($emoji->company_id != $company->id) {
|
|
|
342 |
$data = [
|
|
|
343 |
'success' => false,
|
|
|
344 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
345 |
];
|
|
|
346 |
|
|
|
347 |
return new JsonModel($data);
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
$request = $this->getRequest();
|
|
|
351 |
if($request->isPost()) {
|
|
|
352 |
$dailyPulseRecordMapper = DailyPulseRecordMapper::getInstance($this->adapter);
|
|
|
353 |
$dailyPulse = $dailyPulseRecordMapper->fetchOneCurrentByUserIdAndType($currentUser->id, DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION);
|
|
|
354 |
|
|
|
355 |
if($dailyPulse) {
|
|
|
356 |
$data = [
|
|
|
357 |
'success' => false,
|
|
|
358 |
'data' => 'ERROR_DAILY_PULSE_RECORD_USER_ALREADY_FOUND'
|
|
|
359 |
];
|
|
|
360 |
|
|
|
361 |
} else {
|
|
|
362 |
$engagementMapper = EngagementMapper::getInstance($this->adapter);
|
|
|
363 |
$engagement = $engagementMapper->fetchOneByCompanyId($company->id);
|
|
|
364 |
|
|
|
365 |
$dailyPulse = new DailyPulseRecord();
|
|
|
366 |
$dailyPulse->company_id = $company->id;
|
|
|
367 |
$dailyPulse->user_id = $currentUser->id;
|
|
|
368 |
$dailyPulse->emoji_id = $emoji->id;
|
|
|
369 |
$dailyPulse->type = DailyPulseRecord::TYPE_CLIMATE_ON_YOUR_ORGANIZATION;
|
|
|
370 |
$dailyPulse->engagement = $engagement ? $engagement->daily_pulse_climate_on_your_organization : 0;
|
|
|
371 |
$dailyPulse->points = $emoji->points;
|
|
|
372 |
|
|
|
373 |
$result = $dailyPulseRecordMapper->insert($dailyPulse);
|
|
|
374 |
if($result) {
|
|
|
375 |
|
6002 |
efrain |
376 |
if($engagement) {
|
|
|
377 |
$engagementUserMapper = EngagementUserMapper::getInstance($this->adapter);
|
|
|
378 |
$engagementUser = $engagementUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
|
|
|
379 |
|
|
|
380 |
if($engagementUser) {
|
|
|
381 |
$engagementUser->points = $engagementUser->points + ($engagement ? $engagement->daily_pulse_climate_on_your_organization : 0);
|
|
|
382 |
$engagementUserMapper->update( $engagementUser );
|
|
|
383 |
|
|
|
384 |
} else {
|
|
|
385 |
$engagementUser = new EngagementUser();
|
|
|
386 |
$engagementUser->company_id = $company->id;
|
|
|
387 |
$engagementUser->user_id = $currentUser->id;
|
|
|
388 |
$engagementUser->points = $engagement ? $engagement->daily_pulse_climate_on_your_organization : 0;
|
|
|
389 |
$engagementUserMapper->insert( $engagementUser );
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
$engagementRecord = new EngagementRecord();
|
|
|
393 |
$engagementRecord->company_id = $company->id;
|
|
|
394 |
$engagementRecord->user_id = $currentUser->id;
|
|
|
395 |
$engagementRecord->points = $engagement->daily_pulse_climate_on_your_organization;
|
|
|
396 |
$engagementRecord->type = EngagementRecord::TYPE_DAILY_PULSE_CLIMATE_ON_YOUR_ORGANIZATION;
|
|
|
397 |
|
|
|
398 |
$engagementRecordMapper = EngagementRecordMapper::getInstance($this->adapter);
|
|
|
399 |
$engagementRecordMapper->insert($engagementRecord);
|
5205 |
efrain |
400 |
}
|
|
|
401 |
|
|
|
402 |
$data = [
|
|
|
403 |
'success' => true,
|
|
|
404 |
'data' => 'LABEL_DAILY_PULSE_EMOJI_ADDED'
|
|
|
405 |
];
|
|
|
406 |
} else {
|
|
|
407 |
$data = [
|
|
|
408 |
'success' => false,
|
|
|
409 |
'data' => $dailyPulseRecordMapper->getError()
|
|
|
410 |
];
|
|
|
411 |
}
|
|
|
412 |
}
|
|
|
413 |
} else {
|
|
|
414 |
$data = [
|
|
|
415 |
'success' => false,
|
|
|
416 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
417 |
];
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
return new JsonModel($data);
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
|
|
|
424 |
|
|
|
425 |
|
|
|
426 |
}
|