@@ -475,15 +475,15 @@ private function buildSurveyBuilderHeaders($flat_fields = array(), $flat_fields_
475475
476476 foreach ($ q ->Values () as $ v )
477477 {
478- $ header = sprintf ('%s - %s ' ,$ name , $ v ->Value );
478+ $ header = self :: functionPrepareValueForCSV ( sprintf ('%s - %s ' , $ name , $ v ->Value ) );
479479 $ template_1 [$ header ] = null ;
480480 }
481481 }
482482 else {
483483 if (strpos ($ name ,'Country ' ) !== false ) {
484484 $ template_1 ['Continent ' ] = null ;
485485 }
486- $ template_1 [$ name ] = null ;
486+ $ template_1 [self :: functionPrepareValueForCSV ( $ name) ] = null ;
487487 }
488488
489489 }
@@ -502,41 +502,39 @@ private function buildSurveyBuilderHeaders($flat_fields = array(), $flat_fields_
502502ORDER BY SS.`Order`, Q.`Order`;
503503SQL ;
504504
505- $ res = DB ::query ($ entity_survey_header_query );
506- $ template_2 = array ( 'DeploymentID ' => null ) ;
505+ $ res = DB ::query ($ entity_survey_header_query );
506+ $ template_2 = [ 'DeploymentID ' => null ] ;
507507 foreach ($ res as $ row )
508508 {
509509 $ name = $ row ['Name ' ];
510- if (in_array ($ name , $ flat_fields_entity ))
511- {
512- $ q = SurveyMultiValueQuestionTemplate::get ()->byID (intval ($ row ['QuestionID ' ]));
513- if (is_null ($ q )) continue ;
510+ $ q = SurveyQuestionTemplate::get ()->byID (intval ($ row ['QuestionID ' ]));
511+ if (is_null ($ q )) continue ;
514512
515- foreach ($ q-> Values () as $ v )
516- {
517- $ header = sprintf ('%s - %s ' ,$ name , $ v ->Value );
513+ if ($ q instanceof SurveyDoubleEntryTableQuestionTemplate){
514+ foreach ( $ q -> Rows () as $ r ) {
515+ $ header = self :: functionPrepareValueForCSV ( sprintf ('%s - %s ' , $ name , $ r ->Value ) );
518516 $ template_2 [$ header ] = null ;
519517 }
520518 }
521- else
522- {
523- if ($ row ['ClassName ' ] === 'SurveyRadioButtonMatrixTemplateQuestion ' )
524- {
525- $ q = SurveyRadioButtonMatrixTemplateQuestion::get ()->byID (intval ($ row ['QuestionID ' ]));
526- if (is_null ($ q )) continue ;
527- foreach ($ q ->Rows () as $ r )
528- {
529- $ header = sprintf ('%s - %s ' ,$ name , $ r ->Value );
530- $ template_2 [$ header ] = null ;
531- }
519+ else if ($ q instanceof SurveyMultiValueQuestionTemplate && in_array ($ name , $ flat_fields_entity )){
520+ foreach ($ q ->Values () as $ v ){
521+ $ header = self ::functionPrepareValueForCSV (sprintf ('%s - %s ' , $ name , $ v ->Value ));
522+ $ template_2 [$ header ] = null ;
532523 }
533- else
534- $ template_2 [$ name ] = null ;
535524 }
525+ else
526+ $ template_2 [$ name ] = null ;
536527 }
537528
538529 return array ($ template_1 , $ template_2 );
530+ }
539531
532+ /**
533+ * @param string $value
534+ * @return string
535+ */
536+ private static function functionPrepareValueForCSV ($ value ){
537+ return str_replace (", " ,"- " , $ value );
540538 }
541539
542540 private function getDeploymentsData ($ survey_id )
@@ -630,14 +628,15 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
630628 {
631629 $ res = $ this ->ExportSurveyResultsDataSurveyBuilder ();
632630 $ survey_id = 0 ;
633- $ file_data = array () ;
631+ $ file_data = [] ;
634632 list ($ header_template1 , $ header_template2 ) = $ this ->buildSurveyBuilderHeaders ($ flat_fields , $ flat_fields_entity );
635633
636- $ line = $ header_template1 ;
634+ $ line = $ header_template1 ;
637635 list ($ rows , $ columns ) = self ::getRowsAndColumns ();
638636
639637 foreach ($ res as $ row )
640638 {
639+
641640 if ($ survey_id !== intval ($ row ['SurveyID ' ]))
642641 {
643642 // reset
@@ -646,7 +645,7 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
646645 $ res2 = $ this ->getDeploymentsData ($ survey_id );
647646 $ line2 = $ header_template2 ;
648647 $ entity_survey_id = 0 ;
649- $ entities_surveys_set = array () ;
648+ $ entities_surveys_set = [] ;
650649
651650 foreach ($ res2 as $ row2 )
652651 {
@@ -656,7 +655,7 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
656655
657656 if ($ entity_survey_id > 0 )
658657 {
659- array_push ( $ entities_surveys_set, $ line2) ;
658+ $ entities_surveys_set[] = $ line2 ;
660659 }
661660
662661 $ line2 = $ header_template2 ;
@@ -673,7 +672,7 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
673672
674673 if ($ class === 'SurveyRadioButtonMatrixTemplateQuestion ' )
675674 {
676- $ tuples = explode (', ' , $ answer );
675+ $ tuples = explode (', ' , $ answer );
677676 $ translation = '' ;
678677 foreach ($ tuples as $ t )
679678 {
@@ -687,8 +686,8 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
687686 {
688687 continue ;
689688 }
690- $ c = $ columns [$ c ];
691- $ translation .= sprintf ("%s:%s " ,$ r , $ c ). ', ' ;
689+ $ c = $ columns [$ c ];
690+ $ translation .= self :: functionPrepareValueForCSV ( sprintf ("%s:%s " , $ r , $ c) ). ', ' ;
692691 }
693692 $ answer = trim ($ translation ,', ' );
694693 }
@@ -701,35 +700,41 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
701700 $ q = SurveyRankingQuestionTemplate::get ()->byID ($ question_id );
702701 if (!is_null ($ q ))
703702 {
704- $ values = $ q ->Values ()->sort ('Order ' , 'ASC ' );
705- $ options = array () ;
703+ $ values = $ q ->Values ()->sort ('Order ' , 'ASC ' );
704+ $ options = [] ;
706705 foreach ($ values as $ v )
707706 {
708- array_push ( $ options, $ v ->Value ) ;
707+ $ options[] = $ v ->Value ;
709708 }
709+
710710 $ answers = explode ('| ' , $ row ['Answer ' ]);
711711 foreach ($ options as $ o )
712712 {
713713 $ index = array_search ($ o , $ answers );
714- $ line [sprintf ("%s - %s " , $ question , $ o )] = $ index === false ? '0 ' : ($ index + 1 );;
714+ $ key = sef::functionPrepareValueForCSV (sprintf ("%s - %s " , $ question , $ o ));
715+ if (array_key_exists ($ key , $ line2 ))
716+ $ line [$ key ] = $ index === false ? '0 ' : ($ index + 1 );;
715717 }
716718 }
717719 }
718720 else
719721 {
720722 if ($ class === 'SurveyRadioButtonMatrixTemplateQuestion ' )
721723 {
722- $ answer = preg_split ('/,/ ' ,$ answer );
723- foreach ($ answer as $ a )
724+ foreach (explode (', ' , $ answer ) as $ tuple )
724725 {
725- $ a = preg_split ('/:/ ' ,$ a );
726- if ( count ($ a ) !== 2 ) continue ;
727- $ line2 [sprintf ("%s - %s " ,$ question ,$ a [0 ])] = $ a [1 ];
726+ $ elements = explode (': ' , $ tuple );
727+ if (count ($ elements ) !== 2 ) continue ;
728+ $ key = self ::functionPrepareValueForCSV (sprintf ("%s - %s " , $ question , $ elements [0 ]));
729+ if (array_key_exists ($ key , $ line2 ))
730+ $ line2 [$ key ] = $ elements [1 ];
728731 }
729732 } else {
730733 $ answers = explode ('| ' , $ answer );
731734 foreach ($ answers as $ a ) {
732- $ line2 [sprintf ("%s - %s " , $ question , $ a )] = '1 ' ;
735+ $ key = self ::functionPrepareValueForCSV (sprintf ("%s - %s " , $ question , $ a ));
736+ if (array_key_exists ($ key , $ line2 ))
737+ $ line2 [$ key ] = '1 ' ;
733738 }
734739 }
735740 }
@@ -738,30 +743,32 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
738743 {
739744 if ($ class === 'SurveyRadioButtonMatrixTemplateQuestion ' )
740745 {
741- $ answer = preg_split ( ' /,/ ' , $ answer );
742- foreach ($ answer as $ a )
746+
747+ foreach (explode ( ' , ' , $ answer) as $ tuple )
743748 {
744- $ a = preg_split ('/:/ ' ,$ a );
745- if ( count ($ a ) !== 2 ) continue ;
746- $ line2 [sprintf ("%s - %s " ,$ question ,$ a [0 ])] = $ a [1 ];
749+ $ elements = explode (': ' , $ tuple );
750+ if ( count ($ elements ) !== 2 ) continue ;
751+ $ key = self ::functionPrepareValueForCSV (sprintf ("%s - %s " , $ question , $ elements [0 ]));
752+ if (array_key_exists ($ key , $ line2 ))
753+ $ line2 [$ key ] = $ elements [1 ];
747754 }
748755 }
749756 else
750- $ line2 [$ question ] = $ answer ;
757+ $ line2 [self :: functionPrepareValueForCSV ( $ question) ] = $ answer ;
751758 }
752759 }
753760
754761 if (isset ($ line2 ['DeploymentID ' ]) && intval ($ line2 ['DeploymentID ' ]) > 0 )
755- array_push ( $ entities_surveys_set, $ line2) ;
762+ $ entities_surveys_set[] = $ line2 ;
756763
757764 if (count ($ entities_surveys_set ) === 0 )
758765 {
759- array_push ( $ entities_surveys_set, $ header_template2) ;
766+ $ entities_surveys_set[] = $ header_template2 ;
760767 }
761768
762769 foreach ($ entities_surveys_set as $ line2 )
763770 {
764- array_push ( $ file_data, array_merge ($ line , $ line2) );
771+ $ file_data[] = array_merge ($ line , $ line2 );
765772 }
766773
767774 }
@@ -797,15 +804,15 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
797804 foreach ($ options as $ o )
798805 {
799806 $ index = array_search ($ o , $ answers );
800- $ line [sprintf ("%s - %s " , $ question , $ o )] = $ index === false ? '0 ' : ($ index + 1 );;
807+ $ line [self :: functionPrepareValueForCSV ( sprintf ("%s - %s " , $ question , $ o) )] = $ index === false ? '0 ' : ($ index + 1 );;
801808 }
802809 }
803810 }
804811 else
805812 {
806813 $ answers = explode ('| ' , $ row ['Answer ' ]);
807814 foreach ($ answers as $ a ) {
808- $ line [sprintf ("%s - %s " , $ question , $ a )] = 1 ;
815+ $ line [self :: functionPrepareValueForCSV ( sprintf ("%s - %s " , $ question , $ a) )] = 1 ;
809816 }
810817 }
811818 }
@@ -821,7 +828,7 @@ private function getSurveyBuilderExportData($flat_fields = array(), $flat_fields
821828 }
822829
823830 if (isset ($ line ['SurveyID ' ]) && intval ($ line ['SurveyID ' ]) > 0 )
824- array_push ( $ file_data, array_merge ($ line , $ header_template2) );
831+ $ file_data[] = array_merge ($ line , $ header_template2 );
825832
826833 return $ file_data ;
827834 }
0 commit comments