-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportTripleSSyntaxWriter.php
182 lines (164 loc) · 6.76 KB
/
exportTripleSSyntaxWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* exportTripleSSyntaxWriter part of exportTripleS Plugin for LimeSurvey
* Writer for the plugin
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2014-2023 Denis Chenu <http://sondages.pro>
* @license AGPL v3
* @version 3.1.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
Yii::import('application.helpers.admin.export.*');
class exportTripleSSyntaxWriter extends Writer
{
const APPNAME = 'exportTripleS';
const VERSION = '2.0';
private $output;
private $separator;
private $hasOutputHeader;
public $pluginSettings = array();
private $position = 0;
private $ident = 1;
protected $customFieldmap = array();
protected $iSurveyId;
protected $sSurveyTitle;
protected $sLanguageCode;
function __construct($settings)
{
$this->output = '';
$this->separator = ',';
$this->hasOutputHeader = false;
$basedir = dirname(__FILE__); // this will give you the / directory
if (intval(App()->getConfig('versionnumber')) < 4) {
Yii::setPathOfAlias('exportTripleS', $basedir . DIRECTORY_SEPARATOR . 'legacy');
} else {
Yii::setPathOfAlias('exportTripleS', $basedir);
}
foreach ($settings as $name => $value) {
$this->pluginSettings[$name] = $value;
}
}
public function init(\SurveyObj $oSurvey, $sLanguageCode, \FormattingOptions $oOptions)
{
parent::init($oSurvey, $sLanguageCode, $oOptions);
$this->iSurveyId = $oSurvey->id;
// Fix surveyls_title : oSurvey is for default lang
if ($sLanguageCode != $oSurvey->info['language']) {
$aAvailableLanguage = explode(" ", $oSurvey->info['additional_languages']);
if (!in_array($sLanguageCode, $aAvailableLanguage)) {
$sLanguageCode = $oSurvey->info['language'];
}
}
$this->sLanguageCode = $sLanguageCode;
$oLanguageSurvey = SurveyLanguageSetting::model()->find("surveyls_survey_id=:sid and surveyls_language=:language", array(":sid" => $oSurvey->id,":language" => $sLanguageCode));
if ($oLanguageSurvey) {
$sSurveyTitle = $oLanguageSurvey->surveyls_title;
} else {
$sSurveyTitle = $oSurvey->info['surveyls_title'];
}
$this->sSurveyTitle = self::filterText($sSurveyTitle);
$now = date("Ymd-His");
$oOptions->headingFormat = "full"; // force to use own code
$oOptions->answerFormat = "short"; // force to use own code
Yii::import('exportTripleS.tripleSHelper');
$tripleSfunction = new tripleSHelper($this->pluginSettings);
$tripleSfunction->iSurveyId = $this->iSurveyId;
$tripleSfunction->sLanguageCode = $this->sLanguageCode;
$this->customFieldmap = $tripleSfunction->createTripleSFieldmap($oSurvey, $sLanguageCode, $oOptions);
if ($oOptions->output == 'display') {
if (!$this->pluginSettings['debugMode']) {
header("Content-Disposition: attachment; filename=survey_{$oSurvey->id}_{$now}_triples.sss");
}
header("Content-type: text/xml; charset=UTF-8");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
$this->handle = fopen('php://output', 'w');
} elseif ($oOptions->output == 'file') {
$this->handle = fopen($this->filename, 'w');
}
}
protected function out($content)
{
fwrite($this->handle, $content . "\n");
}
protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
}
public function close()
{
if ($this->pluginSettings['XMLversion'] > 1.2) {
$aAttributes = array(
'version' => '2.0',
'languages' => $this->languageCode,
'modes' => 'interview',
);
$aRecordAttributes = array(
'ident' => 'D',
'format' => 'fixed',
'skip' => "0",
);
} else {
$aAttributes = array(
'version' => '1.2',
'languages' => $this->languageCode,
);
$aRecordAttributes = array(
'ident' => 'D',
);
}
$sss = array(
'@attributes' => $aAttributes,
'date' => date("Y-m-d"),
'time' => date("H:i:s"),
'origin' => 'LimeSurvey ' . App()->getConfig('versionnumber') . " - build:" . App()->getConfig('buildnumber') . " - " . self::APPNAME . " " . self::VERSION,
//'user'=>'', // Get owner name ?
'survey' => array(
'name' => 'sid' . $this->iSurveyId,
'title' => $this->sSurveyTitle,
'record' => array(
'@attributes' => $aRecordAttributes,
'variable' => array(
// Fill by $this->customFieldmap
),
)
),
);
foreach ($this->customFieldmap as $key => $aTripleSarray) {
foreach ($aTripleSarray as $aTripleS) {
if (isset($aTripleS['@attributes']['type']) && $this->pluginSettings['debugMode'] < 4) {
// Unset invalid variable in XML
if ($this->pluginSettings['debugMode'] < 2) {
unset($aTripleS['datasize']);
unset($aTripleS['info']);
} elseif ($this->pluginSettings['debugMode'] <= 4) {
unset($aTripleS['info']['fieldInfo']);
}
$sss['survey']['record']['variable'][] = $aTripleS;
}
}
//~ $sss['survey']['record']['variable']=array_merge($sss['survey']['record']['variable'],$aTripleSarray);
}
Yii::import('exportTripleS.third_party.Array2XML');
$xml = Array2XML::createXML('sss', $sss);
$this->out($xml->saveXML());
fclose($this->handle);
}
private static function filterText($string)
{
if (version_compare(substr(PCRE_VERSION, 0, strpos(PCRE_VERSION, ' ')), '7.0') > -1) {
return preg_replace(array('~\R~u'), array(' '), strip_tags(trim($string)));
}
return preg_replace("/[\n\r]/", " ", strip_tags(trim($string)));
}
}