Commit
… an Array / JSON with responses
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
class JsonWriter extends Writer | ||
{ | ||
private $output; | ||
/** | ||
* The open filehandle | ||
*/ | ||
private $file = null; | ||
/** | ||
* first don't need seperator | ||
*/ | ||
protected $havePrev = false; | ||
|
||
function __construct() | ||
{ | ||
$this->output = ''; | ||
} | ||
|
||
public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions) | ||
{ | ||
parent::init($survey, $sLanguageCode, $oOptions); | ||
$sStartOutput='{'.json_encode("responses").': ['; | ||
if ($oOptions->output=='display') { | ||
header("Content-type: application/json"); | ||
echo $sStartOutput; | ||
} elseif ($oOptions->output == 'file') { | ||
$this->file = fopen($this->filename, 'w'); | ||
fwrite($this->file, $sStartOutput); | ||
} | ||
|
||
} | ||
|
||
protected function outputRecord($headers, $values, FormattingOptions $oOptions) | ||
{ | ||
$aJson[$values[0]]=array_combine ($headers,$values); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
$sJson=json_encode($aJson); | ||
Yii::log($this->havePrev, 'info', 'info'); | ||
if($this->havePrev){ | ||
$sJson=','.$sJson; | ||
|
||
} | ||
$this->havePrev=true; | ||
if ($oOptions->output=='display') | ||
{ | ||
echo $sJson; | ||
$this->output = ''; | ||
} elseif ($oOptions->output == 'file') { | ||
$this->output .= $sJson; | ||
fwrite($this->file, $this->output); | ||
$this->output=''; | ||
} | ||
|
||
} | ||
|
||
public function close() | ||
{ | ||
$sEndOutput=']}'; | ||
if (!$this->file) | ||
{ | ||
echo $sEndOutput; | ||
} | ||
else | ||
{ | ||
$this->output .= $sEndOutput; | ||
fwrite($this->file, $this->output); | ||
fclose($this->file); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,8 @@ function exportSurvey($iSurveyId, $sLanguageCode, $sExportPlugin, FormattingOpti | |
'xls' => '', | ||
'pdf' => '', | ||
'html' => '', | ||
'csv' => '' | ||
'csv' => '', | ||
'json' => '' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
SamMousa
via email
Contributor
|
||
); | ||
$event = new PluginEvent('listExportPlugins'); | ||
$oPluginManager = App()->getPluginManager(); | ||
|
@@ -114,6 +115,9 @@ function exportSurvey($iSurveyId, $sLanguageCode, $sExportPlugin, FormattingOpti | |
case "html": | ||
$writer = new HtmlWriter(); | ||
break; | ||
case "json": | ||
$writer = new JsonWriter(); | ||
break; | ||
case "csv": | ||
default: | ||
$writer = new CsvWriter(); | ||
|
@@ -146,4 +150,4 @@ function exportSurvey($iSurveyId, $sLanguageCode, $sExportPlugin, FormattingOpti | |
return $result; | ||
} | ||
} | ||
} | ||
} |
Don't know id anser id os OK, maybe best then index (0,1,2, ...)