Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 6883 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 6883 Rev 6908
Línea 286... Línea 286...
286
        }
286
        }
Línea 287... Línea 287...
287
 
287
 
288
        return new JsonModel($data);
288
        return new JsonModel($data);
Línea 289... Línea 289...
289
    }
289
    }
290
 
290
 
291
    public function reportAction() {
291
    public function deleteAction() {
292
        $request = $this->getRequest();
292
        $request = $this->getRequest();
293
        $currentUserPlugin = $this->plugin('currentUserPlugin');
293
        $currentUserPlugin = $this->plugin('currentUserPlugin');
294
        $currentCompany = $currentUserPlugin->getCompany();
294
        $currentCompany = $currentUserPlugin->getCompany();
295
        $currentUser = $currentUserPlugin->getUser();
295
        $currentUser = $currentUserPlugin->getUser();
296
 
296
        try{
Línea 297... Línea -...
297
        $request = $this->getRequest();
-
 
298
        $uuid = $this->params()->fromRoute('uuid');
-
 
299
        
297
        $request = $this->getRequest();
300
 
298
        $id = $this->params()->fromRoute('id');
301
 
299
        
302
        if (!$uuid) {
300
        if (!$id) {
303
            $data = [
301
            $data = [
Línea 304... Línea 302...
304
                'success' => false,
302
                'success' => false,
305
                'data' => 'ERROR_INVALID_PARAMETER'
303
                'data' => 'ERROR_INVALID_PARAMETER'
306
            ];
-
 
307
 
-
 
308
            return new JsonModel($data);
-
 
309
        }
-
 
310
       
-
 
311
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
-
 
312
        $surveyTests = $surveyTestMapper->fetchOneByuuid($uuid);
-
 
Línea -... Línea 304...
-
 
304
            ];
-
 
305
 
313
 
306
            return new JsonModel($data);
314
        
307
        }
315
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
308
 
316
        $survey = $surveyMapper->fetchOne($surveyTests->survey_id);
309
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
317
 
310
        $survey = $surveyTestMapper->fetchOne($id);
Línea 318... Línea 311...
318
        if (!$surveyTests) {
311
        if (!$survey) {
319
            $data = [
312
            $data = [
Línea -... Línea 313...
-
 
313
                'success' => false,
-
 
314
                'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
315
            ];
-
 
316
 
-
 
317
            return new JsonModel($data);
-
 
318
        }
Línea 320... Línea 319...
320
                'success' => false,
319
 
321
                'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
322
            ];
-
 
323
 
-
 
324
            return new JsonModel($data);
-
 
325
        }
-
 
Línea -... Línea 320...
-
 
320
        if ($survey->company_id != $currentCompany->id) {
326
 
321
            return new JsonModel([
-
 
322
                'success' => false,
Línea -... Línea 323...
-
 
323
                'data' => 'ERROR_UNAUTHORIZED'
-
 
324
            ]);
327
 
325
        }
-
 
326
 
328
        if ($request->isGet()) {
327
        if ($request->isPost()) {
Línea 329... Línea 328...
329
            $hydrator = new ObjectPropertyHydrator();
328
 
330
 
329
            $result = $surveyTestMapper->delete($survey->id);
331
            //get form data           
330
            if ($result) {
332
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
331
               // $this->logger->info('Se borro el formulario ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
Línea 333... Línea 332...
333
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
332
 
334
 
333
                $data = [
335
            if ($surveyForm) {
334
                    'success' => true,
Línea 352... Línea 351...
352
 
351
 
353
            return new JsonModel($data);
352
            return new JsonModel($data);
Línea 354... Línea 353...
354
        }
353
        }
-
 
354
 
-
 
355
        return new JsonModel($data);
-
 
356
        } catch (\Throwable $e) {
-
 
357
            $e->getMessage();
-
 
358
            return new JsonModel([
-
 
359
                'success' => false,
-
 
360
                'data' => $e
-
 
361
            ]);
355
 
362
        }
Línea 356... Línea -...
356
        return new JsonModel($data);
-
 
357
    }
-
 
358
 
363
        
359
 
364
    }
360
 
365
 
361
    public function deleteAction() {
366
    public function reportAction() {
362
        $request = $this->getRequest();
367
        $request = $this->getRequest();
363
        $currentUserPlugin = $this->plugin('currentUserPlugin');
368
        $currentUserPlugin = $this->plugin('currentUserPlugin');
364
        $currentCompany = $currentUserPlugin->getCompany();
369
        $currentCompany = $currentUserPlugin->getCompany();
365
        $currentUser = $currentUserPlugin->getUser();
370
        $currentUser = $currentUserPlugin->getUser();
Línea -... Línea 371...
-
 
371
 
-
 
372
        $request = $this->getRequest();
366
        try{
373
        $uuid = $this->params()->fromRoute('uuid');
367
        $request = $this->getRequest();
374
        
368
        $id = $this->params()->fromRoute('id');
375
 
369
        
376
 
370
        if (!$id) {
377
        if (!$uuid) {
Línea 371... Línea 378...
371
            $data = [
378
            $data = [
372
                'success' => false,
379
                'success' => false,
373
                'data' => 'ERROR_INVALID_PARAMETER'
380
                'data' => 'ERROR_INVALID_PARAMETER'
374
            ];
381
            ];
375
 
382
 
-
 
383
            return new JsonModel($data);
-
 
384
        }
-
 
385
       
-
 
386
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
-
 
387
        $surveyTests = $surveyTestMapper->fetchOneByuuid($uuid);
376
            return new JsonModel($data);
388
 
377
        }
389
        
378
 
390
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
379
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
391
        $survey = $surveyMapper->fetchOne($surveyTests->survey_id);
380
        $survey = $surveyTestMapper->fetchOne($id);
392
 
Línea 381... Línea 393...
381
        if (!$survey) {
393
        if (!$surveyTests) {
382
            $data = [
394
            $data = [
Línea 383... Línea -...
383
                'success' => false,
-
 
384
                'data' => 'ERROR_RECORD_NOT_FOUND'
-
 
385
            ];
-
 
386
 
-
 
387
            return new JsonModel($data);
-
 
388
        }
-
 
Línea 389... Línea 395...
389
 
395
                'success' => false,
-
 
396
                'data' => 'ERROR_RECORD_NOT_FOUND'
Línea 390... Línea -...
390
        if ($survey->company_id != $currentCompany->id) {
-
 
391
            return new JsonModel([
397
            ];
392
                'success' => false,
398
 
-
 
399
            return new JsonModel($data);
Línea 393... Línea 400...
393
                'data' => 'ERROR_UNAUTHORIZED'
400
        }
394
            ]);
-
 
-
 
401
 
395
        }
402
 
396
 
-
 
397
        if ($request->isPost()) {
403
        if ($request->isGet()) {
Línea 398... Línea 404...
398
 
404
            $hydrator = new ObjectPropertyHydrator();
399
            $result = $surveyTestMapper->delete($survey->id);
405
 
400
            if ($result) {
406
            //get form data           
401
               // $this->logger->info('Se borro el formulario ' . $survey->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
407
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
Línea 402... Línea 408...
402
 
408
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
403
                $data = [
409
 
404
                    'success' => true,
410
            if ($surveyForm) {
Línea 421... Línea 427...
421
 
427
 
422
            return new JsonModel($data);
428
            return new JsonModel($data);
Línea 423... Línea 429...
423
        }
429
        }
-
 
430
 
-
 
431
        return new JsonModel($data);
424
 
432
    }
-
 
433
 
425
        return new JsonModel($data);
434
    public function renderPDF($surveyForm, $surveyTests, $survey) {
-
 
435
 
-
 
436
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $survey->uuid;
426
        } catch (\Throwable $e) {
437
        
427
            $e->getMessage();
438
        
428
            return new JsonModel([
439
        if(file_exists($target_path)) {
429
                'success' => false,
440
            Functions::deleteFiles($target_path);
430
                'data' => $e
441
        } else {
Línea -... Línea 442...
-
 
442
            @mkdir($target_path, 0755, true);
-
 
443
        }
-
 
444
        
-
 
445
        // Set Data
-
 
446
        $headerFormName = utf8_decode($surveyForm->name);
-
 
447
        $headerSurveyName = utf8_decode('Informe de Encuesta: ' . trim($survey->name) . ' al ' . date("m-d-Y H:i:s", strtotime($survey->added_on)));
-
 
448
        $sections = json_decode($surveyForm->content, true);
-
 
449
 
-
 
450
        $allTests = array_map(
-
 
451
            function($response) {
-
 
452
                return json_decode($response->content, true);
-
 
453
            },
-
 
454
            $surveyTests,
-
 
455
        );
-
 
456
 
-
 
457
        $averages = [];
-
 
458
 
-
 
459
        foreach($sections as $section) {
-
 
460
            foreach($section['questions'] as $question) {
-
 
461
                switch ($question['type']) {
-
 
462
                    case 'multiple':
-
 
463
                        $totals = [];
-
 
464
 
-
 
465
                        foreach($question['options'] as $option) {
-
 
466
                            $totals[$option['slug_option']] = 0;
-
 
467
                        }
-
 
468
 
-
 
469
                        foreach($question['options'] as $option) {
-
 
470
                            $totals[$option['slug_option']] = count(array_filter(
-
 
471
                                $allTests,
-
 
472
                                function ($test) use($option, $question) {
-
 
473
                                    return in_array($option['slug_option'], $test[$question['slug_question']]);
-
 
474
                                }
-
 
475
                            ));
-
 
476
                        }
-
 
477
 
-
 
478
                        $averages[$question['slug_question']] = $totals;
-
 
479
 
-
 
480
                        break;
-
 
481
                   
-
 
482
                    case 'simple':
-
 
483
                        $totals = [];
-
 
484
 
-
 
485
                        foreach($question['options'] as $option) {
-
 
486
                            $totals[$option['slug_option']] = 0;
-
 
487
                        }
-
 
488
 
-
 
489
                        foreach ($allTests as $test) {
-
 
490
                            $totals[$test[$question['slug_question']]]++;
-
 
491
                        }
-
 
492
 
-
 
493
                        foreach($totals as $slug => $amount) {
-
 
494
                            $totals[$slug] = ($amount / count($allTests)) * 100;
-
 
495
                        }
-
 
496
 
-
 
497
                        $averages[$question['slug_question']] = $totals;
-
 
498
                    break;
-
 
499
 
-
 
500
                    case 'open':
-
 
501
                        $totals = [];
-
 
502
 
-
 
503
                        foreach($question['options'] as $option) {
-
 
504
                            $totals[$option['slug_option']] = 0;
-
 
505
                        }
-
 
506
 
-
 
507
                        foreach ($allTests as $test) {
-
 
508
                            $totals[$test[$question['slug_question']]]++;
-
 
509
                        }
-
 
510
 
-
 
511
                        foreach($totals as $slug => $amount) {
-
 
512
                            $totals[$slug] = ($amount / count($allTests)) * 100;
-
 
513
                        }
-
 
514
 
-
 
515
                        $averages[$question['slug_question']] = $totals;
-
 
516
                    break;
-
 
517
 
-
 
518
                }
-
 
519
            }
-
 
520
        }
-
 
521
 
-
 
522
        //Generate New PDF
-
 
523
        $pdf = new SurveyReport();
-
 
524
 
-
 
525
        $pdf->AliasNbPages();
-
 
526
        $pdf->AddPage();
-
 
527
 
-
 
528
        // Set header secundary
-
 
529
        $pdf->customHeader($headerFormName, $headerSurveyName);
-
 
530
 
-
 
531
        $countSection = 0;
-
 
532
 
-
 
533
        for ($i = 0; $i < count($sections); $i++) {
-
 
534
            if ($countSection > 1) {
-
 
535
                $countSection = 0;
-
 
536
                $pdf->AddPage();
-
 
537
                $pdf->customHeader($headerFormName, $headerUserName);
-
 
538
            }
-
 
539
            $section = $sections[$i];
-
 
540
        
-
 
541
            foreach ($section['questions'] as $question) {
-
 
542
                    
-
 
543
                    switch ($question['type']) {
-
 
544
                        case 'simple':
-
 
545
                            $labels = [];
-
 
546
                            $values = [];
-
 
547
 
-
 
548
                            foreach($question['options'] as $option) {
-
 
549
                                $labels []= strip_tags($option['text']) . "\n(%.1f%%)";
-
 
550
 
-
 
551
                                $values []= $averages[$question['slug_question']][$option['slug_option']];
-
 
552
                            }
-
 
553
 
-
 
554
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
-
 
555
                            $pdf->Cell(0,10,strip_tags(trim(str_replace(' ', ' ', html_entity_decode($question['text'])))),0,1);
-
 
556
                            $pdf->pieChart($labels, $values, '', $filename);
-
 
557
                            $pdf->SetFont('Arial', '', 12);
-
 
558
                            $pdf->Image($filename, 45, $pdf->getY(), 120);
-
 
559
                            $pdf->setY($pdf->getY() + 90);
-
 
560
 
-
 
561
                            break;
-
 
562
 
-
 
563
                        case 'multiple':
-
 
564
                            $labels = [];
-
 
565
                            $values = [];
-
 
566
 
-
 
567
                            foreach($question['options'] as $option) {
-
 
568
                                $labels []= strip_tags($option['text']);
-
 
569
 
-
 
570
                                $values []= $averages[$question['slug_question']][$option['slug_option']];
-
 
571
                            }
-
 
572
 
-
 
573
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
-
 
574
                            $pdf->Cell(0,10,strip_tags($question['text']),0,1);
-
 
575
                            $pdf->barChart($labels, $values, '', $filename);
-
 
576
                            $pdf->SetFont('Arial', '', 12);
-
 
577
                            $pdf->Image($filename, 12, $pdf->getY());
-
 
578
                            $pdf->setY($pdf->getY() + (count($values) * 13) + 10);
-
 
579
 
-
 
580
                            break;
-
 
581
                    }
-
 
582
                $countSection++;   
-
 
583
            }
-
 
584
 
-
 
585
            
431
            ]);
586
        }
Línea -... Línea 587...
-
 
587
 
432
        }
588
        return $pdf->Output();