-
Notifications
You must be signed in to change notification settings - Fork 4
/
data.php
229 lines (188 loc) · 5.21 KB
/
data.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
<?php
/**
* Enumeration-Class to describe the type of the data.
*/
abstract class DataType
{
const COIN = 0;
const QUESTION = 1;
const PARTICLESYSTEM = 2;
}
/**
* Abstract class to hold abstract data information.
*
* It implements the JsonSerializable-interface.
*/
abstract class Data implements JsonSerializable {
protected $id;
protected $type;
abstract public function jsonSerialize();
abstract public function toJson();
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
/**
* Get the next available question id on the file-system.
*
* The id-range is [0 - MAX_INT].
* This function does not fill id-gaps, it looks for the highest existing id
* and takes the next.
*
* @return int - the next available id.
*/
protected function get_new_id() {
if (!file_exists("questions/")) {
return 0;
}
$files = array();
$iterator = new FilesystemIterator("questions/", FilesystemIterator::SKIP_DOTS);
while($iterator->valid()) {
array_push($files, $iterator->getFileName());
$iterator->next();
}
if (sizeof($files) == 0) {
return 0;
}
natsort($files);
$lastFile = end($files);
$name = explode(".", $lastFile);
return intval($name[0]) + 1;
}
/**
* Returns the directory-path, in which the files of this data-type will be stored.
*
* @return string - The directory-path.
*/
protected abstract function getPath();
/**
* Saves the data-object to a file.
*
* The content of the file will be the JSON, describing the content of the
* data-object. The filename will be <id>.json.
*
* For this function to work, php needs to have write-access on the
* servers file-system folder.
*
* @return void
*/
public function saveToFile() {
// open questions file
if (!file_exists($this->getPath())) {
mkdir($this->getPath(), 0777, true);
}
if ($this->getId() === "") {
$this->setId($this->get_new_id());
}
$fileName = $this->getPath() . $this->getId() . ".json";
// overwrite file with write access
$dataFile = fopen($fileName, "w");
// write new questions
fwrite($dataFile, $this->toJson());
// close file
fclose($dataFile);
}
}
/**
* Concrete question-data class.
*/
class Question extends Data {
private $question;
private $answers;
private $correctAnswer;
/**
* Constructor for the question class.
*
* @param int $id The question id.
* @param string $question The question-text.
* @param [string] $answers An array of possible multiple-choice answers.
* @param int $correctAnswer The index of the correct answer in the answers-array.
*
* @return Question
*/
public function __construct($id, $question, $answers, $correctAnswer) {
$this->id = $id;
$this->question = $question;
$this->answers = $answers;
$this->correctAnswer = $correctAnswer;
$this->type = DataType::QUESTION;
}
public function jsonSerialize() {
return get_object_vars($this);
}
public function toJson() {
return json_encode($this);
}
protected function getPath() {
return "questions/";
}
}
/**
* Concrete coin-data class.
*/
class Coin extends Data {
/**
* Constructor for the coin class.
*
* The coin-id is logically linked to the question-id.
* The question with the equivalent id "unlocks" the respective coin in the
* FH Wedel QR-Schnitzeljagd application.
*
* @param int $id The coin id.
*
* @return Coin
*/
public function __construct($id) {
$this->id = $id;
$this->type = DataType::COIN;
}
public function jsonSerialize() {
return get_object_vars($this);
}
public function toJson() {
return json_encode($this);
}
protected function getPath() {
return "coins/";
}
}
class ParticleSystem extends Data {
private $startColor;
private $endColor;
public function __construct($id, $startColor, $endColor) {
$this->id = $id;
$this->startColor = $startColor;
$this->endColor = $endColor;
$this->type = DataType::PARTICLESYSTEM;
}
public function jsonSerialize() {
return get_object_vars($this);
}
public function toJson() {
return json_encode($this);
}
protected function getPath() {
return "particle/";
}
protected function get_new_id() {
if (!file_exists("particle/")) {
return 0;
}
$files = array();
$iterator = new FilesystemIterator("particle/", FilesystemIterator::SKIP_DOTS);
while($iterator->valid()) {
array_push($files, $iterator->getFileName());
$iterator->next();
}
if (sizeof($files) == 0) {
return 0;
}
natsort($files);
$lastFile = end($files);
$name = explode(".", $lastFile);
return intval($name[0]) + 1;
}
}
?>