/
Writer.php
432 lines (393 loc) · 15.1 KB
/
Writer.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* Contains functions and properties that are common to all writers.
* All extending classes must implement the internalWrite(...) method and
* have access to functionality as described below:
*
* TODO Write more docs here
*/
abstract class Writer implements IWriter
{
protected $sLanguageCode;
protected $translator;
public $filename;
protected function translate($key, $sLanguageCode)
{
return $this->translator->translate($key, $sLanguageCode);
}
/**
* An initialization method that implementing classes can override to gain access
* to any information about the survey, language, or formatting options they
* may need for setup.
*
* @param Survey $survey
* @param mixed $sLanguageCode
* @param FormattingOptions $oOptions
*/
public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions)
{
$this->languageCode = $sLanguageCode;
$this->translator = new Translator();
if ($oOptions->output == 'file') {
$sRandomFileName=Yii::app()->getConfig("tempdir") . DIRECTORY_SEPARATOR . randomChars(40);
$this->filename = $sRandomFileName;
}
}
/**
* Returns an abbreviated heading for the survey's question that matches
* the $fieldName parameter (or false if a match is not found).
*
* @param Survey $survey
* @param string $fieldName
* @return string
*/
public function getAbbreviatedHeading(SurveyObj $survey, $fieldName)
{
$question = $survey->fieldMap[$fieldName];
if ($question)
{
$heading = $question['question'];
$heading = $this->stripTagsFull($heading);
$heading = mb_substr($heading, 0, 15).'.. ';
$aid = $survey->fieldMap[$fieldName]['aid'];
if (!empty($aid))
{
$heading .= '['.$this->stripTagsFull($aid).']';
}
return $heading;
}
return false;
}
/**
* Returns a full heading for the question that matches the $fieldName.
* False is returned if no matching question is found.
*
* @param Survey $survey
* @param FormattingOptions $oOptions
* @param string $fieldName
* @return string (or false)
*/
public function getFullHeading(SurveyObj $survey, FormattingOptions $oOptions, $fieldName)
{
$question = $survey->fieldMap[$fieldName];
$heading = $question['question'];
$heading = $this->stripTagsFull($heading);
$heading.=$this->getFullFieldSubHeading($survey, $oOptions, $fieldName);
return $heading;
}
public function getCodeHeading(SurveyObj $survey, FormattingOptions $oOptions, $fieldName)
{
$question = $survey->fieldMap[$fieldName];
$heading = $question['title'];
$heading = $this->stripTagsFull($heading);
$heading.=$this->getCodeFieldSubHeading($survey, $oOptions, $fieldName);
return $heading;
}
public function getCodeFieldSubHeading(SurveyObj $survey, FormattingOptions $oOptions, $fieldName)
{
$field = $survey->fieldMap[$fieldName];
$answerCode = $field['aid'];
$questionId = $field['qid'];
$fieldType = $field['type'];
$subHeading = '';
switch ($fieldType)
{
case 'R':
$subHeading .= ' ['.$this->translate('Ranking', $this->languageCode).' '.
$answerCode.']';
break;
case 'L':
case '!':
if ($answerCode == 'other')
{
$subHeading .= ' '.$this->getOtherSubHeading();
}
break;
case 'O':
if ($answerCode == 'comment')
{
$subHeading .= ' '.$this->getCommentSubHeading();
}
break;
case 'M':
case 'P':
//This section creates differing output from the old code base, but I do think
//that it is more correct than the old code.
$isOther = ($answerCode == 'other');
$isComment = (mb_substr($answerCode, -7, 7) == 'comment');
if ($isComment)
{
$isOther = (mb_substr($answerCode, 0, -7) == 'other');
}
if ($isOther)
{
$subHeading .= ' '.$this->getOtherSubHeading();
}
else
{
$subHeading .= ' ['.$answerCode.']';
}
break;
case ':':
case ';':
list($scaleZeroTitle, $scaleOneTitle) = explode('_', $answerCode);
$subHeading .= ' ['.$scaleZeroTitle.']['.$scaleOneTitle.']';
break;
case '1':
$answerScale = substr($fieldName, -1) + 1;
$subQuestions = $survey->getSubQuestionArrays($questionId);
foreach ($subQuestions as $question)
{
if ($question['title'] == $answerCode && $question['scale_id'] == 0)
{
$subHeading = ' ['.flattenText($question['title'], true,true).'][Scale '.$answerScale.']';
}
}
break;
default:
if (!empty($answerCode))
{
$subHeading .= ' ['.$answerCode.']';
}
}
//rtrim the result since it could be an empty string ' ' which
//should be removed.
return rtrim($subHeading);
}
public function getFullFieldSubHeading(SurveyObj $survey, FormattingOptions $oOptions, $fieldName)
{
$field = $survey->fieldMap[$fieldName];
$answerCode = $field['aid'];
$questionId = $field['qid'];
$fieldType = $field['type'];
$subHeading = '';
switch ($fieldType)
{
case 'R':
$subHeading .= ' ['.$this->translate('Ranking', $this->languageCode).' '.
$answerCode.']';
break;
case 'L':
case '!':
if ($answerCode == 'other')
{
$subHeading .= ' '.$this->getOtherSubHeading();
}
break;
case 'O':
if ($answerCode == 'comment')
{
$subHeading .= ' '.$this->getCommentSubHeading();
}
break;
case 'M':
case 'P':
//This section creates differing output from the old code base, but I do think
//that it is more correct than the old code.
$isOther = ($answerCode == 'other');
$isComment = (mb_substr($answerCode, -7, 7) == 'comment');
if ($isComment)
{
$isOther = (mb_substr($answerCode, 0, -7) == 'other');
}
if ($isOther)
{
$subHeading .= ' '.$this->getOtherSubHeading();
}
else
{
$sqs = $survey->getSubQuestionArrays($questionId);
foreach ($sqs as $sq)
{
if (!$isComment && $sq['title'] == $answerCode)
{
$value = $sq['question'];
}
}
if (!empty($value))
{
$subHeading .= ' ['.$this->stripTagsFull($value).']';
}
}
if (isset($isComment) && $isComment == true)
{
$subHeading .= ' '.$this->getCommentSubHeading();
$comment = false;
}
break;
case ':':
case ';':
$subHeading .= ' ['.$this->stripTagsFull($field['subquestion1']).']['.$this->stripTagsFull($field['subquestion2']).']';
break;
case '1':
$answerScale = substr($fieldName, -1) + 1;
$subQuestions = $survey->getSubQuestionArrays($questionId);
foreach ($subQuestions as $question)
{
if ($question['title'] == $answerCode && $question['scale_id'] == 0)
{
$subHeading = ' ['.flattenText($question['question'], true,true).'][Scale '.$answerScale.']';
}
}
break;
default:
$subQuestion = null;
$subQuestions = $survey->getSubQuestionArrays($questionId);
foreach ($subQuestions as $question)
{
if ($question['title'] == $answerCode)
{
$subQuestion = $question;
}
}
if (!empty($subQuestion) && !empty($subQuestion['question']))
{
$subHeading .= ' ['.$this->stripTagsFull($subQuestion['question']).']';
}
}
//rtrim the result since it could be an empty string ' ' which
//should be removed.
return rtrim($subHeading);
}
private function getOtherSubHeading()
{
return '['.$this->translate('Other', $this->languageCode).']';
}
private function getCommentSubHeading()
{
return '- comment';
}
/**
* Performs a transformation of the response value based on the value, the
* type of field the value is a response for, and the FormattingOptions.
* All transforms should be processed during the execution of this function!
*
* The final step in the transform is to apply a stripTagsFull on the $value.
* This occurs for ALL values whether or not any other transform is applied.
*
* @param string $value
* @param string $fieldType
* @param FormattingOptions $oOptions
* @return string
*/
protected function transformResponseValue($value, $fieldType, FormattingOptions $oOptions)
{
//The following if block handles transforms of Ys and Ns.
if (($oOptions->convertN || $oOptions->convertY) &&
isset($fieldType) &&
($fieldType == 'M' || $fieldType == 'P' || $fieldType == 'Y'))
{
if ($value == 'N' && $oOptions->convertN)
{
//echo "Transforming 'N' to ".$oOptions->nValue.PHP_EOL;
return $oOptions->nValue;
}
else if ($value == 'Y' && $oOptions->convertY)
{
//echo "Transforming 'Y' to ".$oOptions->yValue.PHP_EOL;
return $oOptions->yValue;
}
}
//This spot should only be reached if no transformation occurs.
return $value;
}
/**
* This method is made final to prevent extending code from circumventing the
* initialization process that must take place prior to any of the translation
* infrastructure to work.
*
* The inialization process is dependent upon the survey being passed into the
* write function and so must be performed when the method is called and not
* prior to (such as in a constructor).
*
* All extending classes must implement the internalWrite function which is
* the code that is called after all initialization is completed.
*
* @param Survey $survey
* @param string $sLanguageCode
* @param FormattingOptions $oOptions
* @param boolean $bOutputHeaders Set if header should be given back
*/
final public function write(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions, $bOutputHeaders=true)
{
//Output the survey.
$headers = array();
if ($bOutputHeaders)
{
foreach ($oOptions->selectedColumns as $column)
{
//Survey question field, $column value is a field name from the getFieldMap function.
switch ($oOptions->headingFormat)
{
case 'abbreviated':
$value = $this->getAbbreviatedHeading($survey, $column);
break;
case 'full':
$value = $this->getFullHeading($survey, $oOptions, $column);
break;
default:
case 'code':
$value = viewHelper::getFieldCode($survey->fieldMap[$column]);
break;
}
if ($oOptions->headerSpacesToUnderscores)
{
$value = str_replace(' ', '_', $value);
}
//$this->output.=$this->csvEscape($value).$this->separator;
$headers[] = $value;
}
}
//Output the results.
$sFile='';
foreach($survey->responses as $response)
{
$elementArray = array();
foreach ($oOptions->selectedColumns as $column)
{
$value = $response[$column];
if (isset($survey->fieldMap[$column]) && $survey->fieldMap[$column]['type']!='answer_time' && $survey->fieldMap[$column]['type']!='page_time' && $survey->fieldMap[$column]['type']!='interview_time')
{
switch ($oOptions->answerFormat) {
case 'long':
$elementArray[] = $this->transformResponseValue($survey->getFullAnswer($column, $value, $this->translator, $this->languageCode), $survey->fieldMap[$column]['type'], $oOptions);
break;
default:
case 'short':
$elementArray[] = $this->transformResponseValue($value,
$survey->fieldMap[$column]['type'], $oOptions);
break;
}
}
else //Token table value
{
$elementArray[]=$value;
}
}
if ($oOptions->output=='display')
{
$this->outputRecord($headers, $elementArray, $oOptions);
} else {
$sFile.=$this->outputRecord($headers, $elementArray, $oOptions);
}
}
return $sFile;
}
protected function stripTagsFull($string)
{
$string=str_replace('-oth-','',$string);
return flattenText($string,false,true,'UTF-8',false);
}
/**
* This method will be called once for every row of data that needs to be
* output.
*
* Implementations must use the data from these method calls to construct
* proper output for their output type and the given FormattingOptions.
*
* @param array $headers
* @param array $values
* @param FormattingOptions $oOptions
*/
abstract protected function outputRecord($headers, $values, FormattingOptions $oOptions);
}