/
DocWriter.php
102 lines (94 loc) · 2.9 KB
/
DocWriter.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
<?php
class DocWriter extends Writer
{
private $output;
private $separator;
private $isBeginning;
/**
* The open filehandle
*/
private $file = null;
public function __construct()
{
$this->separator = "\t";
$this->output = '';
$this->isBeginning = true;
}
public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions)
{
parent::init($survey, $sLanguageCode, $oOptions);
if ($oOptions->output=='display')
{
header("Content-Disposition: attachment; filename=results-survey".$survey->id.".doc");
header("Content-type: application/vnd.ms-word");
}
$sOutput = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
table {
border-collapse:collapse;
}
td, th {
border:solid black 1.0pt;
}
th {
background: #c0c0c0;
}
</style>';
if ($oOptions->output=='display'){
echo $sOutput;
} elseif ($oOptions->output == 'file') {
$this->file = fopen($this->filename, 'w');
$this->output = $sOutput;
}
}
/**
* @param array $headers
* @param array $values
* @param FormattingOptions $oOptions
*/
protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
if ($oOptions->answerFormat == 'short')
{
//No headers at all, only output values.
$this->output .= implode($this->separator, $values).PHP_EOL;
}
elseif ($oOptions->answerFormat == 'long')
{
//Output each record, one per page, with a header preceding every value.
if ($this->isBeginning)
{
$this->isBeginning = false;
}
else
{
$this->output .= "<br clear='all' style='page-break-before:always'>";
}
$this->output .= "<table><tr><th colspan='2'>".$this->translate('New Record', $this->languageCode)."</td></tr>".PHP_EOL;
$counter = 0;
foreach ($headers as $header)
{
$this->output .= "<tr><td>".$header."</td><td>".$values[$counter]."</td></tr>".PHP_EOL;
$counter++;
}
$this->output .= "</table>".PHP_EOL;
}
else
{
safeDie('An invalid answer format was selected. Only \'short\' and \'long\' are valid.');
}
if ($oOptions->output=='display'){
echo $this->output;
$this->output='';
} elseif ($oOptions->output == 'file') {
fwrite($this->file, $this->output);
$this->output='';
}
}
public function close()
{
if (!is_null($this->file)) {
fclose($this->file);
}
}
}