/**
  * 配列をCSV形式で書き出す
  * @param array $arr 書き込むCSVの配列
  * @access public
  * @see CsvWriter::write()
  */
 function write($arr)
 {
     static $b = false;
     if (!$b) {
         // 初回の書き込みの場合
         $b = true;
         $headers = array();
         foreach ($this->format as $key => $params) {
             $headers[] = $params['name'];
         }
         parent::write($headers);
     }
     $csv = array();
     foreach ($this->format as $key => $params) {
         $csv[$key] = $this->filter($arr[$key], $params);
     }
     parent::write($csv);
 }
Exemplo n.º 2
0
 /**
  * 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;
     }
 }