Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16701 | Rev 16749 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16701 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\FastSurveyMapper;
15
use LeadersLinked\Form\FastSurvey\FastSurveyForm;
16
use LeadersLinked\Model\FastSurvey;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
18
use LeadersLinked\Model\Feed;
19
use LeadersLinked\Mapper\CompanyUserMapper;
20
use LeadersLinked\Mapper\FeedMapper;
16747 efrain 21
use LeadersLinked\Mapper\QueryMapper;
22
use LeadersLinked\Mapper\UserMapper;
23
use PhpOffice\PhpSpreadsheet\IOFactory;
24
use PhpOffice\PhpSpreadsheet\Spreadsheet;
25
use LeadersLinked\Mapper\FastSurveyVoteMapper;
16701 efrain 26
 
27
class FastSurveyController extends AbstractActionController {
28
 
29
    /**
30
     *
31
     * @var AdapterInterface
32
     */
33
    private $adapter;
34
 
35
    /**
36
     *
37
     * @var AbstractAdapter
38
     */
39
    private $cache;
40
 
41
    /**
42
     *
43
     * @var  LoggerInterface
44
     */
45
    private $logger;
46
 
47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
52
 
53
    /**
54
     *
55
     * @param AdapterInterface $adapter
56
     * @param AbstractAdapter $cache
57
     * @param LoggerInterface $logger
58
     * @param array $config
59
     */
60
    public function __construct($adapter, $cache, $logger, $config) {
61
        $this->adapter = $adapter;
62
        $this->cache = $cache;
63
        $this->logger = $logger;
64
        $this->config = $config;
65
    }
66
 
67
    public function indexAction()
68
    {
69
        $currentUserPlugin = $this->plugin('currentUserPlugin');
70
        $currentUser = $currentUserPlugin->getUser();
71
        $currentCompany = $currentUserPlugin->getCompany();
72
 
73
        $request = $this->getRequest();
74
 
75
        $request = $this->getRequest();
76
        if($request->isGet()) {
77
 
78
 
79
            $headers  = $request->getHeaders();
80
 
81
            $isJson = false;
82
            if($headers->has('Accept')) {
83
                $accept = $headers->get('Accept');
84
 
85
                $prioritized = $accept->getPrioritized();
86
 
87
                foreach($prioritized as $key => $value) {
88
                    $raw = trim($value->getRaw());
89
 
90
                    if(!$isJson) {
91
                        $isJson = strpos($raw, 'json');
92
                    }
93
 
94
                }
95
            }
96
 
97
 
98
            if($isJson) {
99
 
100
 
101
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
102
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/edit');
103
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/delete');
16747 efrain 104
                $allowChart = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/chart');
16701 efrain 105
                $allowDownload = $acl->isAllowed($currentUser->usertype_id, 'fast-survey/download');
106
 
107
 
108
 
109
                $search = $this->params()->fromQuery('search');
110
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
111
 
112
                $page               = intval($this->params()->fromQuery('start', 1), 10);
113
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
114
                $order =  $this->params()->fromQuery('order', []);
115
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
116
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
117
 
118
                $fields =  ['added_on'];
119
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'added_on';
120
 
121
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
122
                    $order_direction = 'DESC';
123
                }
124
 
125
                $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
126
 
127
                $paginator = $fastSurveyMapper->fetchAllDataTable($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
128
 
129
 
130
                $items = [];
131
                $records = $paginator->getCurrentItems();
132
                foreach($records as $record)
133
                {
134
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
135
                    $added_on = $dt->format('d/m/Y h:i a');
136
 
137
                    $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->expire_on);
138
                    $expire_on = $dt->format('d/m/Y h:i a');
139
 
140
                    $votes = $record->votes1
141
                        + $record->votes2
142
                        + $record->votes3
143
                        + $record->votes4
144
                        + $record->votes5;
145
 
146
                    switch($record->status)
147
                    {
148
                        case FastSurvey::STATUS_ACTIVE :
149
                            $status = 'LABEL_ACTIVE';
150
                            break;
151
 
152
                        default :
153
                            $status = 'LABEL_INACTIVE';
154
                            break;
155
 
156
                    }
157
 
158
 
159
                    $item = [
160
                        'question' => $record->question,
161
                        'details' => [
162
                            'added_on' => $added_on,
163
                            'expire_on' => $expire_on,
164
                        ],
165
                        'votes' => $votes,
166
                        'actions' => [
167
                            'link_edit' => $allowEdit && !$votes ?   $this->url()->fromRoute('fast-survey/edit', ['id' => $record->uuid ]) : '',
168
                            'link_delete' => $allowDelete && !$votes ? $this->url()->fromRoute('fast-survey/delete', ['id' => $record->uuid ]) : '',
16747 efrain 169
                            'link_chart' => $allowChart ? $this->url()->fromRoute('fast-survey/chart', ['id' => $record->uuid ]) : '',
16701 efrain 170
                            'link_download' => $allowDownload ? $this->url()->fromRoute('fast-survey/download', ['id' => $record->uuid ]) : '',
171
                        ],
172
 
173
                    ];
174
 
175
                    array_push($items, $item);
176
                }
177
 
178
                return new JsonModel([
179
                    'success' => true,
180
                    'data' => [
181
                        'items' => $items,
182
                        'total' => $paginator->getTotalItemCount(),
183
                    ]
184
                ]);
185
            } else  {
186
                $form = new FastSurveyForm();
187
 
188
                $this->layout()->setTemplate('layout/layout-backend');
189
                $viewModel = new ViewModel();
190
                $viewModel->setTemplate('leaders-linked/fast-survey/index.phtml');
191
                $viewModel->setVariables([
192
                    'form' => $form
193
                ]);
194
                return $viewModel ;
195
            }
196
 
197
        } else {
198
            return new JsonModel([
199
                'success' => false,
200
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
201
            ]);;
202
        }
203
    }
204
 
205
    public function addAction()
206
    {
207
        $currentUserPlugin = $this->plugin('currentUserPlugin');
208
        $currentUser = $currentUserPlugin->getUser();
209
 
210
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
211
        $currentNetwork = $currentNetworkPlugin->getNetwork();
212
 
213
        $currentCompany = $currentUserPlugin->getCompany();
214
 
215
        $request = $this->getRequest();
216
 
217
 
218
        if ($request->isPost()) {
219
 
220
 
221
            $form = new FastSurveyForm();
222
            $dataPost = $request->getPost()->toArray();
223
 
224
            $form->setData($dataPost);
225
            if ($form->isValid()) {
226
 
227
                $dataPost = (array) $form->getData();
228
 
229
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
230
                $companyUser = $companyUserMapper->fetchOwnerByCompanyId($currentCompany->id);
231
 
232
 
233
 
234
 
235
                $hydrator = new ObjectPropertyHydrator();
236
                $fastSurvey = new FastSurvey();
237
                $hydrator->hydrate($dataPost, $fastSurvey);
238
 
239
                $fastSurvey->network_id = $currentNetwork->id;
240
                $fastSurvey->company_id = $currentCompany->id;
241
                $fastSurvey->user_id = $companyUser->id;
242
                $fastSurvey->status = FastSurvey::STATUS_ACTIVE;
243
 
244
                $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
245
                $result = $fastSurveyMapper->insert($fastSurvey);
246
                if ($result) {
247
                    $feed = new Feed();
248
                    $feed->company_id = $currentCompany->id;
249
                    $feed->network_id = $currentNetwork->id;
250
                    $feed->user_id = $companyUser->id;
251
                    $feed->fast_survey_id = $fastSurvey->id;
252
                    $feed->type = Feed::TYPE_COMPANY;
253
                    $feed->file_type = Feed::FILE_TYPE_FAST_SURVEY;
254
                    $feed->posted_or_shared = Feed::POSTED;
255
                    $feed->status = Feed::STATUS_PUBLISHED;
256
                    $feed->title = '';
257
                    $feed->description = '';
258
                    $feed->total_comments   = 0;
259
                    $feed->total_shared     = 0;
260
 
261
                    $feed->shared_with      = Feed::SHARE_WITH_CONNECTIONS;
262
 
263
                    $feedMapper = FeedMapper::getInstance($this->adapter);
264
                    $feedMapper->insert($feed);
265
 
266
                    $response = [
267
                        'success' => true,
268
                        'data' => 'LABEL_RECORD_ADDED',
269
                    ];
270
 
271
                    $this->logger->info('Se agrego la encuesta rápida : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
272
 
273
                } else {
274
                    $response = [
275
                        'success' => false,
276
                        'data' => $fastSurveyMapper->getError()
277
                    ];
278
                }
279
 
280
                return new JsonModel($response);
281
            } else {
282
                $messages = [];
283
                $form_messages = (array) $form->getMessages();
284
                foreach ($form_messages as $fieldname => $field_messages) {
285
 
286
                    $messages[$fieldname] = array_values($field_messages);
287
                }
288
 
289
                return new JsonModel([
290
                    'success' => false,
291
                    'data' => $messages
292
                ]);
293
            }
294
        } else {
295
            $data = [
296
                'success' => false,
297
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
298
            ];
299
 
300
            return new JsonModel($data);
301
        }
302
 
303
        return new JsonModel($data);
304
    }
305
 
306
    public function editAction()
307
    {
308
        $currentUserPlugin = $this->plugin('currentUserPlugin');
309
        $currentUser = $currentUserPlugin->getUser();
310
        $currentCompany = $currentUserPlugin->getCompany();
311
 
312
        $request = $this->getRequest();
313
        $id = $this->params()->fromRoute('id');
314
 
315
 
316
        if (!$id) {
317
            $data = [
318
                'success' => false,
319
                'data' => 'ERROR_INVALID_PARAMETER'
320
            ];
321
 
322
            return new JsonModel($data);
323
        }
324
 
325
        $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
326
        $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
327
        if (!$fastSurvey) {
328
            $data = [
329
                'success' => false,
330
                'data' => 'ERROR_RECORD_NOT_FOUND'
331
            ];
332
 
333
            return new JsonModel($data);
334
        }
335
 
336
        if($fastSurvey->company_id != $currentCompany->id) {
337
            $response = [
338
                'success' => false,
339
                'data' =>  'ERROR_UNAUTHORIZED'
340
            ];
341
 
342
            return new JsonModel($response);
343
        }
344
 
345
        if($fastSurvey->votes1 > 0 || $fastSurvey->votes2 > 0|| $fastSurvey->votes3 > 0|| $fastSurvey->votes4 > 0|| $fastSurvey->votes5 > 0) {
346
            $response = [
347
                'success' => false,
348
                'data' =>  'ERROR_FAST_SURVEY_THERE_ARE_VOTES'
349
            ];
350
 
351
            return new JsonModel($response);
352
        }
353
 
354
        if ($request->isPost()) {
355
            $form = new FastSurveyForm();
356
            $dataPost = $request->getPost()->toArray();
357
 
358
            $form->setData($dataPost);
359
 
360
            if ($form->isValid()) {
361
                $dataPost = (array) $form->getData();
362
 
363
                $hydrator = new ObjectPropertyHydrator();
364
                $hydrator->hydrate($dataPost, $fastSurvey);
365
 
366
                $result = $fastSurveyMapper->update($fastSurvey);
367
                if ($result) {
368
                    $this->logger->info('Se actualizo la encuesta : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
369
 
370
                    $data = [
371
                        'success' => true,
372
                        'data' => 'LABEL_RECORD_UPDATED'
373
                    ];
374
                } else {
375
                    $data = [
376
                        'success' => false,
377
                        'data' => $fastSurveyMapper->getError()
378
                    ];
379
                }
380
 
381
                return new JsonModel($data);
382
            } else {
383
                $messages = [];
384
                $form_messages = (array) $form->getMessages();
385
                foreach ($form_messages as $fieldname => $field_messages) {
386
                    $messages[$fieldname] = array_values($field_messages);
387
                }
388
 
389
                return new JsonModel([
390
                    'success' => false,
391
                    'data' => $messages
392
                ]);
393
            }
394
        } else if ($request->isGet()) {
395
            $hydrator = new ObjectPropertyHydrator();
396
 
397
            $data = [
398
                'success' => true,
399
                'data' => [
400
                    'question' => $fastSurvey->question,
401
                    'number_of_answers' => $fastSurvey->number_of_answers,
402
                    'answer1' => $fastSurvey->answer1,
403
                    'answer2' => $fastSurvey->answer2,
404
                    'answer3' => $fastSurvey->answer3,
405
                    'answer4' => $fastSurvey->answer4,
406
                    'answer5' => $fastSurvey->answer5,
407
                    'duration_days' => $fastSurvey->duration_days,
408
                    'duration_hours' => $fastSurvey->duration_hours,
409
                    'duration_minutes' => $fastSurvey->duration_minutes,
410
                    'status' => $fastSurvey->status,
411
                    'privacy' => $fastSurvey->privacy,
412
                    'result_type' => $fastSurvey->result_type,
413
                ]
414
            ];
415
 
416
            return new JsonModel($data);
417
        } else {
418
            $data = [
419
                'success' => false,
420
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
421
            ];
422
 
423
            return new JsonModel($data);
424
        }
425
 
426
        return new JsonModel($data);
427
    }
428
 
429
    public function deleteAction()
430
    {
431
        $currentUserPlugin = $this->plugin('currentUserPlugin');
432
        $currentUser = $currentUserPlugin->getUser();
433
        $currentCompany = $currentUserPlugin->getCompany();
434
 
435
        $request = $this->getRequest();
436
        if ($request->isPost()) {
437
 
438
            $id = $this->params()->fromRoute('id');
439
            if (!$id) {
440
                $response = [
441
                    'success' => false,
442
                    'data' => 'ERROR_INVALID_PARAMETER'
443
                ];
444
 
445
                return new JsonModel($response);
446
            }
447
 
448
 
449
 
450
            $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
451
            $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
452
            if (!$fastSurvey) {
453
                $data = [
454
                    'success' => false,
455
                    'data' => 'ERROR_RECORD_NOT_FOUND'
456
                ];
457
 
458
                return new JsonModel($data);
459
            }
460
 
461
            if($fastSurvey->company_id != $currentCompany->id) {
462
                $response = [
463
                    'success' => false,
464
                    'data' =>  'ERROR_UNAUTHORIZED'
465
                ];
466
 
467
                return new JsonModel($response);
468
            }
469
 
470
            if($fastSurvey->votes1 > 0 || $fastSurvey->votes2 > 0|| $fastSurvey->votes3 > 0|| $fastSurvey->votes4 > 0|| $fastSurvey->votes5 > 0) {
471
                $response = [
472
                    'success' => false,
473
                    'data' =>  'ERROR_FAST_SURVEY_THERE_ARE_VOTES'
474
                ];
475
 
476
                return new JsonModel($response);
477
            }
478
 
479
 
480
            $feedMapper = FeedMapper::getInstance($this->adapter);
481
            $feedMapper->deleteByFastSurvey($fastSurvey);
482
 
483
            $result = $fastSurveyMapper->delete($fastSurvey);
484
            if ($result) {
485
 
486
 
487
 
488
                $this->logger->info('Se borro la encuesta : ' . $fastSurvey->question, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
489
 
490
                $response = [
491
                    'success' => true,
492
                    'data' => 'LABEL_RECORD_DELETED'
493
                ];
494
            } else {
495
 
496
                $response = [
497
                    'success' => false,
498
                    'data' => $fastSurveyMapper->getError()
499
                ];
500
 
501
            }
502
        } else {
503
            $response = [
504
                'success' => false,
505
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
506
            ];
507
 
508
        }
509
 
510
        return new JsonModel($response);
511
    }
512
 
513
 
16747 efrain 514
    public function chartAction()
515
    {
516
        $currentUserPlugin = $this->plugin('currentUserPlugin');
517
        $currentUser = $currentUserPlugin->getUser();
518
        $currentCompany = $currentUserPlugin->getCompany();
519
 
520
 
521
        $id = $this->params()->fromRoute('id');
522
 
523
 
524
        if (!$id) {
525
            $data = [
526
                'success' => false,
527
                'data' => 'ERROR_INVALID_PARAMETER'
528
            ];
529
 
530
            return new JsonModel($data);
531
        }
532
 
533
        $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
534
        $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
535
        if (!$fastSurvey) {
536
            $data = [
537
                'success' => false,
538
                'data' => 'ERROR_RECORD_NOT_FOUND'
539
            ];
540
 
541
            return new JsonModel($data);
542
        }
543
 
544
        if($fastSurvey->company_id != $currentCompany->id) {
545
            $response = [
546
                'success' => false,
547
                'data' =>  'ERROR_UNAUTHORIZED'
548
            ];
549
 
550
            return new JsonModel($response);
551
        }
552
 
553
 
554
        $request = $this->getRequest();
555
        if ($request->isGet()) {
556
            $data = [
557
                'question'  => $fastSurvey->question,
558
                'labels'    => [$fastSurvey->answer1, $fastSurvey->answer2],
559
                'values'    => [$fastSurvey->votes1, $fastSurvey->votes2],
560
            ];
561
 
562
            if($fastSurvey->number_of_answers >= 3) {
563
                array_push($data['labels'], $fastSurvey->answer3);
564
                array_push($data['values'], $fastSurvey->votes3);
565
            }  else if($fastSurvey->number_of_answers >= 4) {
566
                array_push($data['labels'], $fastSurvey->answer4);
567
                array_push($data['values'], $fastSurvey->votes4);
568
            }  else if($fastSurvey->number_of_answers == 5) {
569
                array_push($data['labels'], $fastSurvey->answer5);
570
                array_push($data['values'], $fastSurvey->votes5);
571
            }
572
 
573
            return new JsonModel([
574
                'success' => true,
575
                'data' => $data,
576
            ]);
577
 
578
        } else {
579
            return new JsonModel([
580
                'success' => false,
581
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
582
            ]);
583
        }
584
    }
16701 efrain 585
 
16747 efrain 586
    public function downloadAction()
587
    {
588
        $currentUserPlugin = $this->plugin('currentUserPlugin');
589
        $currentUser = $currentUserPlugin->getUser();
590
        $currentCompany = $currentUserPlugin->getCompany();
591
 
592
 
593
        $id = $this->params()->fromRoute('id');
594
 
595
 
596
        if (!$id) {
597
            $data = [
598
                'success' => false,
599
                'data' => 'ERROR_INVALID_PARAMETER'
600
            ];
601
 
602
            return new JsonModel($data);
603
        }
604
 
605
        $fastSurveyMapper = FastSurveyMapper::getInstance($this->adapter);
606
        $fastSurvey = $fastSurveyMapper->fetchOneByUuid($id);
607
        if (!$fastSurvey) {
608
            $data = [
609
                'success' => false,
610
                'data' => 'ERROR_RECORD_NOT_FOUND'
611
            ];
612
 
613
            return new JsonModel($data);
614
        }
615
 
616
        if($fastSurvey->company_id != $currentCompany->id) {
617
            $response = [
618
                'success' => false,
619
                'data' =>  'ERROR_UNAUTHORIZED'
620
            ];
621
 
622
            return new JsonModel($response);
623
        }
624
 
625
 
626
        $request = $this->getRequest();
627
        if ($request->isGet()) {
628
 
629
 
630
 
631
 
632
            $spreadsheet = new Spreadsheet();
633
            $spreadsheet->getProperties()->setTitle($fastSurvey->question);
634
 
635
 
636
            $spreadsheet->setActiveSheetIndex(0);
637
            $spreadsheet->getActiveSheet()->SetCellValue('A1', 'TOTAL DE VOTOS');
638
 
639
            if($fastSurvey->number_of_answers >= 3) {
640
                $spreadsheet->getActiveSheet()->mergeCells('A1:G1');
641
            }  else if($fastSurvey->number_of_answers >= 4) {
642
                $spreadsheet->getActiveSheet()->mergeCells('A1:H1');
643
            }  else if($fastSurvey->number_of_answers == 5) {
644
                $spreadsheet->getActiveSheet()->mergeCells('A1:I1');
645
            }
646
 
647
            $spreadsheet->getActiveSheet()->SetCellValue('A2', $fastSurvey->answer1);
648
            $spreadsheet->getActiveSheet()->SetCellValue('A3', $fastSurvey->votes1);
649
 
650
 
651
            $spreadsheet->getActiveSheet()->SetCellValue('B2', $fastSurvey->answer2);
652
            $spreadsheet->getActiveSheet()->SetCellValue('B3', $fastSurvey->votes2);
653
 
654
            if($fastSurvey->number_of_answers >= 3) {
655
                $spreadsheet->getActiveSheet()->SetCellValue('C2', $fastSurvey->answer3);
656
                $spreadsheet->getActiveSheet()->SetCellValue('C3', $fastSurvey->votes3);
657
 
658
 
659
            }  else if($fastSurvey->number_of_answers >= 4) {
660
 
661
                $spreadsheet->getActiveSheet()->SetCellValue('D2', $fastSurvey->answer4);
662
                $spreadsheet->getActiveSheet()->SetCellValue('D3', $fastSurvey->votes4);
663
 
664
            }  else if($fastSurvey->number_of_answers == 5) {
665
 
666
                $spreadsheet->getActiveSheet()->SetCellValue('E2', $fastSurvey->answer5);
667
                $spreadsheet->getActiveSheet()->SetCellValue('E3', $fastSurvey->votes5);
668
            }
669
 
670
            $spreadsheet->getActiveSheet()->SetCellValue('A5', 'DETALLE DE LOS USUARIOS');
671
 
672
            if($fastSurvey->number_of_answers >= 3) {
673
                $spreadsheet->getActiveSheet()->mergeCells('A5:G5');
674
            }  else if($fastSurvey->number_of_answers >= 4) {
675
                $spreadsheet->getActiveSheet()->mergeCells('A5:H5');
676
            }  else if($fastSurvey->number_of_answers == 5) {
677
                $spreadsheet->getActiveSheet()->mergeCells('A5:I5');
678
            }
679
 
680
 
681
            $spreadsheet->getActiveSheet()->SetCellValue('A6', 'Fecha');
682
            $spreadsheet->getActiveSheet()->SetCellValue('B6', 'Nombre');
683
            $spreadsheet->getActiveSheet()->SetCellValue('C6', 'Apellido');
684
            $spreadsheet->getActiveSheet()->SetCellValue('D6', 'Correo electrónico');
685
            $spreadsheet->getActiveSheet()->SetCellValue('E6', $fastSurvey->answer1);
686
            $spreadsheet->getActiveSheet()->SetCellValue('F6', $fastSurvey->answer2);
687
            if($fastSurvey->number_of_answers >= 3) {
688
                $spreadsheet->getActiveSheet()->SetCellValue('G6', $fastSurvey->answer3);
689
            }  else if($fastSurvey->number_of_answers >= 4) {
690
                $spreadsheet->getActiveSheet()->SetCellValue('H6', $fastSurvey->answer4);
691
            }  else if($fastSurvey->number_of_answers == 5) {
692
                $spreadsheet->getActiveSheet()->SetCellValue('I6', $fastSurvey->answer5);
693
            }
694
 
695
            /*
696
             select u.first_name , u.last_name, u.email , v.vote, v.added_on from tbl_fast_survey_votes as v
697
inner join tbl_users as u on v.user_id = u.id
698
where v.fast_survey_id = 5
699
order by v.added_on
700
             */
701
 
702
            $queryMapper = QueryMapper::getInstance($this->adapter);
703
            $select = $queryMapper->getSql()->select();
704
            $select->columns(['vote', 'added_on']);
705
            $select->from(['v' => FastSurveyVoteMapper::_TABLE]);
706
            $select->join(['u' => UserMapper::_TABLE], 'v.user_id = u.id', ['first_name', 'last_name', 'email']);
707
            $select->where->equalTo('v.fast_survey_id', $fastSurvey->id);
708
            $select->order('added_on');
709
 
710
 
711
            $i = 7;
712
            $records = $queryMapper->fetchAll($select);
713
            foreach($records as $record)
714
            {
715
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record['added_on']);
716
 
717
                $spreadsheet->getActiveSheet()->SetCellValue('A' . $i, $dt->format('d/m/Y H:i a'));
718
                $spreadsheet->getActiveSheet()->SetCellValue('B' . $i, $record['first_name']);
719
                $spreadsheet->getActiveSheet()->SetCellValue('C' . $i, $record['last_name']);
720
                $spreadsheet->getActiveSheet()->SetCellValue('D' . $i, $record['email']);
721
 
722
                switch($record['vote'])
723
                {
724
                    case 1 :
725
                        $spreadsheet->getActiveSheet()->SetCellValue('E' . $i, 'X');
726
                        break;
727
 
728
                    case 2 :
729
                        $spreadsheet->getActiveSheet()->SetCellValue('F' . $i, 'X');
730
                        break;
731
 
732
                    case 3 :
733
                        $spreadsheet->getActiveSheet()->SetCellValue('G' . $i, 'X');
734
                        break;
735
 
736
                    case 4 :
737
                        $spreadsheet->getActiveSheet()->SetCellValue('H' . $i, 'X');
738
                        break;
739
 
740
                    case 5 :
741
                        $spreadsheet->getActiveSheet()->SetCellValue('I' . $i, 'X');
742
                        break;
743
 
744
                }
745
 
746
                $i++;
747
            }
748
 
749
            $fileName = Functions::normalizeString($fastSurvey->question) . '_'  . date('d-m-Y-h-i-a', time()) . '.xls';
750
            $tempFilename = tempnam(sys_get_temp_dir(), 'reporte_pulso_diario_' . time());
751
 
752
            $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
753
            $writer->save($tempFilename);
754
 
755
            $content = file_get_contents($tempFilename);
756
            @unlink($tempFilename);
757
 
758
            return new JsonModel([
759
                'success' => true,
760
                'data' => [
761
                    'content' => base64_encode($content),
762
                    'basename' => $fileName
763
 
764
                ]
765
            ]);
766
        } else {
767
            return new JsonModel([
768
                'success' => false,
769
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
770
            ]);
771
        }
772
    }
16701 efrain 773
 
774
 
775
 
776
}