Esempio n. 1
0
 function testCsvReader_TwoLines()
 {
     $filename = dirname(__FILE__) . '/data/writer-one-line.csv';
     $csv = new CsvWriter($filename);
     $csv->addLine(array('One', 'Two words', 'One "quoted"', 'Single "quote'));
     $csv->addLine(array('Line Number', '"Two "" is here"', 'Is', 'It fine?'));
     $csv->close();
     $file = fopen(dirname(__FILE__) . '/data/writer-one-line.csv', 'r');
     $string1 = utf8_encode(fgets($file));
     $string2 = utf8_encode(fgets($file));
     fclose($file);
     @unlink($filename);
     $expected1 = 'One,Two words,"One ""quoted""","Single ""quote"' . "\r\n";
     $expected2 = 'Line Number,"""Two """" is here""",Is,It fine?' . "\r\n";
     $this->assertEqual($string1, $expected1);
     $this->assertEqual($string2, $expected2);
 }
 /**
  * Root function for any export results action
  *
  * @param mixed $iSurveyId
  * @param mixed $sLanguageCode
  * @param csv|doc|pdf|xls $sExportPlugin Type of export
  * @param FormattingOptions $oOptions
  * @param string $sFilter 
  */
 function exportSurvey($iSurveyId, $sLanguageCode, $sExportPlugin, FormattingOptions $oOptions, $sFilter = '')
 {
     //Do some input validation.
     if (empty($iSurveyId)) {
         safeDie('A survey ID must be supplied.');
     }
     if (empty($sLanguageCode)) {
         safeDie('A language code must be supplied.');
     }
     if (empty($oOptions)) {
         safeDie('Formatting options must be supplied.');
     }
     if (empty($oOptions->selectedColumns)) {
         safeDie('At least one column must be selected for export.');
     }
     //echo $oOptions->toString().PHP_EOL;
     $writer = null;
     $iSurveyId = sanitize_int($iSurveyId);
     if ($oOptions->output == 'display') {
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Pragma: public");
     }
     switch ($sExportPlugin) {
         case "doc":
             $writer = new DocWriter();
             break;
         case "xls":
             $writer = new ExcelWriter();
             break;
         case "pdf":
             $writer = new PdfWriter();
             break;
         case "csv":
         default:
             $writer = new CsvWriter();
             break;
     }
     $surveyDao = new SurveyDao();
     $survey = $surveyDao->loadSurveyById($iSurveyId);
     $writer->init($survey, $sLanguageCode, $oOptions);
     $iBatchSize = 100;
     $iCurrentRecord = $oOptions->responseMinRecord - 1;
     $bMoreRecords = true;
     $first = true;
     while ($bMoreRecords) {
         $iExported = $surveyDao->loadSurveyResults($survey, $iBatchSize, $iCurrentRecord, $oOptions->responseMaxRecord, $sFilter);
         $iCurrentRecord += $iExported;
         $writer->write($survey, $sLanguageCode, $oOptions, $first);
         $first = false;
         $bMoreRecords = $iExported == $iBatchSize;
     }
     $result = $writer->close();
     if ($oOptions->output == 'file') {
         return $writer->filename;
     } else {
         return $result;
     }
 }