Completed
Push — master ( 1bb378...803221 )
by Florian
04:44
created

Event::categoryWhitelist()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
c 0
b 0
f 0
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the feedback project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Entity;
13
14
use App\Entity\Base\BaseEntity;
15
use App\Entity\Traits\IdTrait;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\ORM\Mapping as ORM;
18
19
/**
20
 * an event determines how the questionnaire looks like.
21
 *
22
 * @ORM\Entity()
23
 * @ORM\HasLifecycleCallbacks
24
 */
25
class Event extends BaseEntity
26
{
27
    use IdTrait;
28
29
    /**
30
     * @var string
31
     *
32
     * @ORM\Column(type="text")
33
     */
34
    private $name;
35
36
    /**
37
     * @var string
38
     *
39
     * @ORM\Column(type="text")
40
     */
41
    private $date;
42
43
    /**
44
     * @var string
45
     *
46
     * @ORM\Column(type="text")
47
     */
48
    private $feedbackStartTime;
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Column(type="text")
54
     */
55
    private $feedbackEndTime;
56
57
    /**
58
     * @var string
59
     *
60
     * @ORM\Column(type="text")
61
     */
62
    private $template;
63
64
    /**
65
     * @var string
66
     *
67
     * @ORM\Column(type="text")
68
     */
69
    private $templateName;
70
71
    /**
72
     * @var bool
73
     *
74
     * @ORM\Column(type="boolean")
75
     */
76
    private $hasLecture = false;
77
78
    /**
79
     * @var bool
80
     *
81
     * @ORM\Column(type="boolean")
82
     */
83
    private $hasExercise = false;
84
85
    /**
86
     * @var bool
87
     *
88
     * @ORM\Column(type="boolean")
89
     */
90
    private $finalTemplateVersionLoaded = false;
91
92
    /**
93
     * @var Participant[]|ArrayCollection
94
     *
95
     * @ORM\OneToMany(targetEntity="App\Entity\Participant", mappedBy="event")
96
     */
97
    private $participants;
98
99
    /**
100
     * @var Semester
101
     *
102
     * @ORM\ManyToOne(targetEntity="App\Entity\Semester", inversedBy="events")
103
     */
104
    private $semester;
105
106
    /**
107
     * Event constructor.
108
     */
109
    public function __construct()
110
    {
111
        $this->participants = new ArrayCollection();
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getName(): string
118
    {
119
        return $this->name;
120
    }
121
122
    /**
123
     * @param string $name
124
     */
125
    public function setName(string $name): void
126
    {
127
        $this->name = $name;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getFeedbackStartTime(): string
134
    {
135
        return $this->feedbackStartTime;
136
    }
137
138
    /**
139
     * @param string $feedbackStartTime
140
     */
141
    public function setFeedbackStartTime(string $feedbackStartTime): void
142
    {
143
        $this->feedbackStartTime = $feedbackStartTime;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getFeedbackEndTime(): string
150
    {
151
        return $this->feedbackEndTime;
152
    }
153
154
    /**
155
     * @param string $feedbackEndTime
156
     */
157
    public function setFeedbackEndTime(string $feedbackEndTime): void
158
    {
159
        $this->feedbackEndTime = $feedbackEndTime;
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getTemplate(): string
166
    {
167
        return $this->template;
168
    }
169
170
    /**
171
     * @param string $template
172
     */
173
    public function setTemplate(string $template): void
174
    {
175
        $this->template = $template;
176
    }
177
178
    /**
179
     * @return bool
180
     */
181
    public function getHasLecture(): bool
182
    {
183
        return $this->hasLecture;
184
    }
185
186
    /**
187
     * @param bool $hasLecture
188
     */
189
    public function setHasLecture(bool $hasLecture): void
190
    {
191
        $this->hasLecture = $hasLecture;
192
    }
193
194
    /**
195
     * @return bool
196
     */
197
    public function getHasExercise(): bool
198
    {
199
        return $this->hasExercise;
200
    }
201
202
    /**
203
     * @param bool $hasExercise
204
     */
205
    public function setHasExercise(bool $hasExercise): void
206
    {
207
        $this->hasExercise = $hasExercise;
208
    }
209
210
    /**
211
     * @return Participant[]|ArrayCollection
212
     */
213
    public function getParticipants()
214
    {
215
        return $this->participants;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getDate(): string
222
    {
223
        return $this->date;
224
    }
225
226
    /**
227
     * @param string $date
228
     */
229
    public function setDate(string $date): void
230
    {
231
        $this->date = $date;
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getTemplateName(): string
238
    {
239
        return $this->templateName;
240
    }
241
242
    /**
243
     * @param string $templateName
244
     */
245
    public function setTemplateName(string $templateName): void
246
    {
247
        $this->templateName = $templateName;
248
    }
249
250
    /**
251
     * @return Semester
252
     */
253
    public function getSemester(): Semester
254
    {
255
        return $this->semester;
256
    }
257
258
    /**
259
     * @param Semester $semester
260
     */
261
    public function setSemester(Semester $semester): void
262
    {
263
        $this->semester = $semester;
264
    }
265
266
    /**
267
     * which categories should be displayed to the user.
268
     *
269
     * @return array
270
     */
271
    public function getCategoryWhitelist()
272
    {
273
        $base = ['event'];
274
        if ($this->getHasExercise()) {
275
            $base[] = 'exercise';
276
        }
277
        if ($this->getHasLecture()) {
278
            $base[] = 'lecture';
279
        }
280
281
        return $base;
282
    }
283
284
    /**
285
     * @return bool
286
     */
287
    public function isFinalTemplateVersionLoaded(): bool
288
    {
289
        return $this->finalTemplateVersionLoaded;
290
    }
291
292
    /**
293
     * @param bool $finalTemplateVersionLoaded
294
     */
295
    public function setFinalTemplateVersionLoaded(bool $finalTemplateVersionLoaded): void
296
    {
297
        $this->finalTemplateVersionLoaded = $finalTemplateVersionLoaded;
298
    }
299
300
    /**
301
     * @return bool
302
     */
303
    public function feedbackHasStarted()
304
    {
305
        $now = new \DateTime();
306
        $today = $now->format('Y-m-d');
307
        $time = $now->format('H:i');
308
309
        return $today > $this->getDate() || ($today === $this->getDate() && $time >= $this->getFeedbackStartTime());
310
    }
311
312
    /**
313
     * @return string
314
     */
315
    public function getTemplateFilePath()
316
    {
317
        return 'templates/' . $this->getTemplateName();
318
    }
319
320
    /**
321
     * @param $publicDir
322
     */
323
    public function loadTemplateIfSafe($publicDir)
324
    {
325
        if ($this->finalTemplateVersionLoaded) {
326
            return;
327
        }
328
329
        if ($this->feedbackHasStarted()) {
330
            $this->finalTemplateVersionLoaded = true;
331
        }
332
333
        $filePath = $publicDir . '/' . $this->getTemplateFilePath();
334
        if (file_exists($filePath)) {
335
            $this->template = file_get_contents($filePath);
336
        }
337
    }
338
339
    /**
340
     * @return Answer[]
341
     */
342
    public function getPublicFeedback()
343
    {
344
        //to preserve privacy, no feedback shown if not enough participants
345
        if ($this->getParticipants()->count() <= 5) {
346
            return [];
347
        }
348
349
        $feedback = [];
350
        foreach ($this->getParticipants() as $participant) {
351
            foreach ($participant->getAnswers() as $answer) {
352
                if (!$answer->isPrivate()) {
353
                    $feedback[] = $answer;
354
                }
355
            }
356
        }
357
358
        return $feedback;
359
    }
360
}
361