forked from eberhardt/moodle-block_coursefeedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportlib.php
175 lines (153 loc) · 3.94 KB
/
exportlib.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Export functions
*
* @package block
* @subpackage coursefeedback
* @copyright 2011-2014 onwards Jan Eberhardt (@ innoCampus, TU Berlin)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
require_once($CFG->dirroot . "/blocks/coursefeedback/lib.php");
class feedbackexport
{
protected $course = 0;
protected $filetypes = array("csv");
private $content = "";
private $format;
public function __construct($course = 0, $seperator = "\t")
{
global $DB;
if($DB->record_exists("course", array("id" => $course)))
$this->course = $course;
else
{
print_error("courseidnotfound", "error");
exit(0);
}
}
public function get_filetypes()
{
return $this->filetypes;
}
public function init_format($format)
{
if(in_array($format, $this->get_filetypes()))
{
$exportformatclass = "exportformat_" . $format;
$this->format = new $exportformatclass();
return true;
}
else
return false;
}
public function create_file($lang)
{
global $CFG, $DB;
if(!isset($this->format))
{
print_error("format not initialized", "block_coursefeedback");
}
else
{
$answers = block_coursefeedback_get_answers($this->course);
$this->reset();
$this->content = $this->format->build($answers, $lang);
}
}
public function get_content()
{
return $this->content;
}
public function reset()
{
$this->content = "";
}
}
/**
* @author Jan Eberhardt
* Generell format class. Doesn"t contain very much so far, but should provide basics.
*/
abstract class exportformat
{
private $type = "unknown";
public final function get_type()
{
return $this->type;
}
public abstract function build($arg1);
}
/**
* @author Jan Eberhardt
* CSV export class
*/
class exportformat_csv extends exportformat
{
public $seperator;
public $newline;
public $quotes;
/**
* Set CSV options.
*
* TODO Choosable values.
*/
public function __construct()
{
$this->type = "csv";
$this->seperator = ";";
$this->newline = "\n";
$this->quotes = "\"";
}
/**
* (non-PHPdoc)
* @see exportformat::build()
*/
public function build($answers, $lang = null)
{
global $DB;
$config = get_config("block_coursefeedback");
$content = $this->quote(get_string("download_thead_questions", "block_coursefeedback"))
. $this->seperator
. $this->quote(get_string("table_html_abstain", "block_coursefeedback"));
for ($i = 1; $i < 7; $i++)
$content .= $this->seperator . $i;
$content .= $this->newline;
$lang = block_coursefeedback_find_language($lang);
foreach ($answers as $questionid => $values)
{
$conditions = array("coursefeedbackid" => $config->active_feedback,
"language" => $lang,
"questionid" => $questionid);
if($question = $DB->get_field("block_coursefeedback_questns", "question", $conditions));
{
$question = $this->quote(format_text(trim($question, " \""), FORMAT_PLAIN)) . $this->quotes;
$content .= $question . $this->seperator . join($this->seperator, $values) . $this->newline;
}
}
return $content;
}
/**
* Quotes a field value.
*
* @param string $str
* @return string
*/
private function quote($str)
{
return $this->quotes . $str . $this->quotes;
}
}