Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 14215 Rev 15150
Línea 130... Línea 130...
130
                        'name' => $record->name,
130
                        'name' => $record->name,
131
                        'form' => $surveyForm->name,
131
                        'form' => $surveyForm->name,
132
                        'status' => $record->status,
132
                        'status' => $record->status,
133
                        'actions' => [
133
                        'actions' => [
134
                            'link_report_all' => $this->url()->fromRoute('survey/report/all', ['survey_id' => $record->uuid]),
134
                            'link_report_all' => $this->url()->fromRoute('survey/report/all', ['survey_id' => $record->uuid]),
135
                            'link_report_csv' => $this->url()->fromRoute('survey/report/csv', ['survey_id' => $record->uuid]),
135
                            'link_report_excel' => $this->url()->fromRoute('survey/report/excel', ['survey_id' => $record->uuid]),
136
                            'link_overview' => $this->url()->fromRoute('survey/report/overview', ['survey_id' => $record->uuid])
136
                            'link_overview' => $this->url()->fromRoute('survey/report/overview', ['survey_id' => $record->uuid])
137
                        ]
137
                        ]
138
                    ];
138
                    ];
Línea 139... Línea 139...
139
 
139
 
Línea 320... Línea 320...
320
            return new JsonModel($data);
320
            return new JsonModel($data);
321
        }
321
        }
Línea 322... Línea 322...
322
 
322
 
323
 
-
 
324
        if ($request->isGet()) {
323
 
325
            $hydrator = new ObjectPropertyHydrator();
-
 
326
 
324
        if ($request->isGet()) {
327
            //get form data           
325
     
Línea 328... Línea 326...
328
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
326
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
Línea 329... Línea 327...
329
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
327
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
330
 
328
 
Línea 331... Línea 329...
331
            if ($surveyForm) {
329
            if ($surveyForm) {
332
 
330
 
333
                return $this->renderPDF($surveyForm, $surveyTests, $survey);
331
                return $this->renderPDF($currentCompany, $surveyForm, $surveyTests, $survey);
Línea 351... Línea 349...
351
 
349
 
352
        return new JsonModel($data);
350
        return new JsonModel($data);
Línea 353... Línea 351...
353
    }
351
    }
-
 
352
 
Línea 354... Línea -...
354
 
-
 
-
 
353
    
Línea -... Línea 354...
-
 
354
    public function renderPDF($company, $surveyForm, $surveyTests, $survey) {
-
 
355
 
-
 
356
 
-
 
357
      
-
 
358
        
-
 
359
        
-
 
360
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
-
 
361
        
-
 
362
        $header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
-
 
363
        if(empty($header) || !file_exists($header)) {
-
 
364
            $header = $this->config['leaderslinked.images_default.company_pdf_header'];
-
 
365
        }
-
 
366
        
-
 
367
        $footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
-
 
368
        if(empty($footer) || !file_exists($footer)) {
-
 
369
            $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
-
 
370
        }
-
 
371
        
-
 
372
        
-
 
373
        //Generate New PDF
-
 
374
        $pdf = new SurveyReport();
-
 
375
        
-
 
376
        
355
    
377
        $pdf->header = $header;
356
    public function renderPDF($surveyForm, $surveyTests, $survey) {
378
        $pdf->footer = $footer;
357
 
379
        
358
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $survey->uuid;
380
        
359
        
381
        
Línea 360... Línea 382...
360
        
382
        $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $survey->uuid;
361
        if(file_exists($target_path)) {
383
        if(!file_exists($target_path)) {
362
            Functions::deleteFiles($target_path);
384
            mkdir($target_path, 0755, true);
363
        } else {
385
        } else {
Línea 364... Línea 386...
364
            @mkdir($target_path, 0755, true);
386
            Functions::deleteFiles($target_path);
365
        }
387
        }
366
        
388
        
367
        // Set Data
389
        // Set Data
368
        $headerFormName = utf8_decode($surveyForm->name);
390
        $headerFormName = $this->cleanStringToPdf($surveyForm->name);
369
        $headerSurveyName = utf8_decode('Informe de Encuesta: ' . trim($survey->name) . ' al ' . date("m-d-Y H:i:s", strtotime($survey->added_on)));
391
        $headerSurveyName = $this->cleanStringToPdf('Informe de Encuesta: ' . trim($survey->name) . ' al ' . date("m-d-Y H:i:s", strtotime($survey->added_on)));
-
 
392
        $sections = json_decode($surveyForm->content, true);
-
 
393
 
-
 
394
        $allTests = array_map(
Línea 370... Línea 395...
370
        $sections = json_decode($surveyForm->content, true);
395
            function($response) {
Línea 371... Línea 396...
371
 
396
                return json_decode($response->content, true);
372
        $allTests = array_map(
397
            },
Línea 421... Línea 446...
421
 
446
 
422
                }
447
                }
423
            }
448
            }
Línea 424... Línea -...
424
        }
-
 
425
 
-
 
Línea 426... Línea 449...
426
        //Generate New PDF
449
        }
427
        $pdf = new SurveyReport();
450
 
Línea 428... Línea 451...
428
 
451
 
429
        $pdf->AliasNbPages();
452
        $pdf->AliasNbPages();
Línea 430... Línea 453...
430
        $pdf->AddPage();
453
        $pdf->AddPage();
Línea 431... Línea 454...
431
 
454
 
-
 
455
        // Set header secundary
-
 
456
        $pdf->customHeader($headerFormName, $headerSurveyName);
432
        // Set header secundary
457
 
433
        $pdf->customHeader($headerFormName, $headerSurveyName);
458
        $countSection = 0;
434
 
459
 
435
        $countSection = 0;
460
        for ($i = 0, $max = count($sections);  $i < $max ; $i++) {
436
 
461
            $headerUserName = '';
Línea 448... Línea 473...
448
                        case 'simple':
473
                        case 'simple':
449
                            $labels = [];
474
                            $labels = [];
450
                            $values = [];
475
                            $values = [];
Línea 451... Línea 476...
451
 
476
 
452
                            foreach($question['options'] as $option) {
477
                            foreach($question['options'] as $option) {
-
 
478
                                $label = $this->cleanStringToPdf($option['text']) . "\n(%.1f%%)";
-
 
479
                                $value = $averages[$question['slug_question']][$option['slug_option']];
-
 
480
                                if(empty($value)) {
-
 
481
                                    continue;
-
 
482
                                }
-
 
483
                                
453
                                $labels []= strip_tags($option['text']) . "\n(%.1f%%)";
484
                                array_push($labels, $label);
454
 
485
                                
455
                                $values []= $averages[$question['slug_question']][$option['slug_option']];
486
                                array_push($values, $value);
Línea 456... Línea 487...
456
                            }
487
                            }
457
 
488
 
458
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
489
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
459
                            $pdf->Cell(0,10,strip_tags(trim(str_replace(' ', ' ', html_entity_decode($question['text'])))),0,1);
490
                            $pdf->Cell(0,10, $this->cleanStringToPdf($question['text'], false),0,1);
460
                            $pdf->pieChart($labels, $values, '', $filename);
491
                            $pdf->pieChart($labels, $values, '', $filename);
461
                            $pdf->SetFont('Arial', '', 12);
492
                            $pdf->SetFont('Arial', '', 12);
Línea 462... Línea 493...
462
                            $pdf->Image($filename, 45, $pdf->getY(), 120);
493
                            $pdf->Image($filename, 10, $pdf->getY(), 190);
Línea 463... Línea 494...
463
                            $pdf->setY($pdf->getY() + 90);
494
                            $pdf->setY($pdf->getY() + 90);
464
                           
495
                           
465
 
496
 
Línea 466... Línea 497...
466
                            break;
497
                            break;
-
 
498
 
467
 
499
                        case 'multiple':
-
 
500
                            $labels = [];
-
 
501
                            $values = [];
-
 
502
 
-
 
503
                            foreach($question['options'] as $option) 
-
 
504
                            {
-
 
505
                                $label = $this->cleanStringToPdf($option['text']) . "\n(%.1f%%)";
Línea 468... Línea 506...
468
                        case 'multiple':
506
                                $value = $averages[$question['slug_question']][$option['slug_option']];
469
                            $labels = [];
507
                                if(empty($value)) {
Línea 470... Línea 508...
470
                            $values = [];
508
                                    continue;
471
 
509
                                }
472
                            foreach($question['options'] as $option) {
510
                                
473
                                $labels []= strip_tags($option['text']);
511
                                array_push($labels, $label);
474
 
512
 
475
                                $values []= $averages[$question['slug_question']][$option['slug_option']];
513
                                array_push($values, $value);
Línea 476... Línea 514...
476
                            }
514
                            }
477
 
515
 
478
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
516
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
Línea 491... Línea 529...
491
        }
529
        }
Línea 492... Línea 530...
492
 
530
 
493
        return $pdf->Output();
531
        return $pdf->Output();
Línea 494... Línea 532...
494
    }
532
    }
495
 
533
 
496
    public function csvAction() {
534
    public function excelAction() {
497
        $request = $this->getRequest();
535
        $request = $this->getRequest();
498
        $currentUserPlugin = $this->plugin('currentUserPlugin');
536
        $currentUserPlugin = $this->plugin('currentUserPlugin');
Línea 528... Línea 566...
528
            return new JsonModel($data);
566
            return new JsonModel($data);
529
        }
567
        }
Línea 530... Línea 568...
530
 
568
 
531
 
-
 
532
        if ($request->isGet()) {
569
 
533
            $hydrator = new ObjectPropertyHydrator();
-
 
534
 
570
        if ($request->isGet()) {
535
            //get form data           
571
         
Línea 536... Línea 572...
536
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
572
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
Línea 537... Línea 573...
537
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
573
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
-
 
574
 
-
 
575
            if ($surveyForm) {
-
 
576
 
-
 
577
                $filename = $this->excelRender($surveyForm, $surveyTests, $survey);
-
 
578
                $content = file_get_contents($filename);
-
 
579
                
-
 
580
                
-
 
581
                $response = $this->getResponse();
-
 
582
                $headers = $response->getHeaders();
-
 
583
                $headers->clearHeaders();
-
 
584
 
-
 
585
                
-
 
586
                $headers->addHeaderLine('Content-Description: File Transfer');
-
 
587
                $headers->addHeaderLine('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
-
 
588
                $headers->addHeaderLine('Content-Disposition: attachment; filename='.$survey->uuid.'.xls');
-
 
589
                $headers->addHeaderLine('Content-Transfer-Encoding: binary');
-
 
590
                $headers->addHeaderLine('Expires: 0');
-
 
591
                $headers->addHeaderLine('Cache-Control: must-revalidate');
-
 
592
                $headers->addHeaderLine('Pragma: public');
-
 
593
                
-
 
594
               
-
 
595
                $response->setStatusCode(200);
538
 
596
                $response->setContent($content);
Línea 539... Línea 597...
539
            if ($surveyForm) {
597
                
540
 
598
                
541
                return $this->csvRender($surveyForm, $surveyTests, $survey);
599
                return $response;
542
            } else {
600
            } else {
Línea 543... Línea 601...
543
 
601
 
544
                $data = [
602
                $data = [
545
                    'success' => false,
603
                    'success' => false,
Línea 559... Línea 617...
559
 
617
 
560
        return new JsonModel($data);
618
        return new JsonModel($data);
Línea 561... Línea 619...
561
    }
619
    }
562
 
620
 
563
 
621
 
Línea 564... Línea 622...
564
    public function csvRender($surveyForm, $surveyTests, $survey)
622
    public function excelRender($surveyForm, $surveyTests, $survey)
565
    {
623
    {
Línea 631... Línea 689...
631
                $question = $section['questions'][$j];
689
                $question = $section['questions'][$j];
Línea 632... Línea 690...
632
 
690
 
633
                if (!in_array($question['type'], ['simple', 'multiple'])){
691
                if (!in_array($question['type'], ['simple', 'multiple'])){
634
                    continue;
692
                    continue;
-
 
693
                }
-
 
694
                
635
                }
695
                $row++;
Línea 636... Línea 696...
636
                $sheet->setCellValueByColumnAndRow(1, $row++, strip_tags($question['text']));
696
                $sheet->setCellValue('A' . $row , $this->cleanStringToExcel($question['text']));
637
 
697
 
638
                switch ($question['type']) {
698
                switch ($question['type']) {
639
                    case 'simple':
-
 
640
                        for($k = 0; $k < count($question['options']); $k++) {
-
 
641
                            $option = $question['options'][$k];
-
 
642
                            $sheet->setCellValueByColumnAndRow(1, $row, strip_tags($option['text']));
699
                    case 'simple':
-
 
700
                        for($k = 0; $k < count($question['options']); $k++) {
-
 
701
                            $row++;
-
 
702
                            
-
 
703
                            $option = $question['options'][$k];
-
 
704
 
-
 
705
                            
-
 
706
                            $sheet->setCellValue('B' . $row , $this->cleanStringToExcel($option['text']));
643
                            $sheet->setCellValueByColumnAndRow(2, $row, round($averages[$question['slug_question']][$option['slug_option']], 2) . '%');
707
                            $sheet->setCellValue('C' . $row , round($averages[$question['slug_question']][$option['slug_option']], 2) . '%');
Línea 644... Línea 708...
644
                            $row++;
708
                            
Línea 645... Línea 709...
645
                        }
709
                        }
646
 
710
 
647
                        break;
-
 
648
 
-
 
649
                    case 'multiple':
-
 
-
 
711
                        break;
650
                        for($k = 0; $k < count($question['options']); $k++) {
712
 
-
 
713
                    case 'multiple':
-
 
714
                        for($k = 0; $k < count($question['options']); $k++) {
-
 
715
                            
-
 
716
                            $row++;
651
                            $option = $question['options'][$k];
717
                            $option = $question['options'][$k];
Línea 652... Línea 718...
652
                            $sheet->setCellValueByColumnAndRow(1, $row, strip_tags($option['text']));
718
                            $sheet->setCellValue('B' . $row , $this->cleanStringToExcel($option['text']));
653
                            $sheet->setCellValueByColumnAndRow(2, $row,$averages[$question['slug_question']][$option['slug_option']]);
719
                            $sheet->setCellValue('C' . $row , round($averages[$question['slug_question']][$option['slug_option']]));
654
                            $row++;
720
                            
Línea 655... Línea 721...
655
                        }
721
                        }
Línea 656... Línea -...
656
 
-
 
-
 
722
 
-
 
723
                        break;
657
                        break;
724
                }
658
                }
725
            }
659
            }
726
 
660
 
727
            
-
 
728
        }
661
            
729
 
662
        }
-
 
663
 
730
       
664
        header('Content-Description: File Transfer');
731
        
-
 
732
        $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $survey->uuid;
-
 
733
        if(!file_exists($target_path)) {
-
 
734
            mkdir($target_path, 0755, true);    
-
 
735
        }
-
 
736
        
-
 
737
        $filename = $target_path  . DIRECTORY_SEPARATOR . $survey->uuid . '.xls'; 
Línea -... Línea 738...
-
 
738
        
-
 
739
        $writer = new Xlsx($spreadsheet);
-
 
740
        $writer->save($filename);
-
 
741
        
-
 
742
        return $filename;
-
 
743
        
-
 
744
    }
665
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
745
    
-
 
746
 
-
 
747
    
-
 
748
    private function cleanStringToPdf($s, $mbConvert = true)
-
 
749
    {
-
 
750
        
-
 
751
        $s = html_entity_decode($s);
666
        header('Content-Disposition: attachment; filename=report.xls');
752
       
667
        header('Content-Transfer-Encoding: binary');
753
        
668
        header('Expires: 0');
754
        if($mbConvert) {
-
 
755
            $detect = mb_detect_encoding($s);
-
 
756
            if(strtoupper($detect) != 'UTF8') {
-
 
757
                
-
 
758
                $s = mb_convert_encoding($s, 'UTF8', $detect);
-
 
759
                
-
 
760
            }
-
 
761
        } else {
-
 
762
            $s = utf8_decode($s);
-
 
763
        }
-
 
764
        
669
        header('Cache-Control: must-revalidate');
765
        
670
        header('Pragma: public');
766
        return strip_tags($s);
671
 
767
    }