-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAnswerSheet.php
117 lines (104 loc) · 3.4 KB
/
AnswerSheet.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
<?php
namespace App\Models\AnonymousQuestions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\Semester;
use App\Models\Question;
use App\Models\QuestionOption;
use App\Models\AnonymousQuestions\LongAnswer;
/**
* Groups a collegist's answers to anonymous questions
* of the evaluation form.
*/
class AnswerSheet extends Model
{
use HasFactory;
/**
* For better anonymity,
* this model should not be timestamped.
*
* @var bool
*/
public $timestamps = false;
protected $fillable = [
'year_of_acceptance'
];
/**
* The semester in which the form was filled.
*/
public function semester(): BelongsTo
{
return $this->belongsTo(Semester::class);
}
/**
* The answer options chosen by the filler.
*/
public function chosenOptions(): BelongsToMany
{
return $this->belongsToMany(QuestionOption::class);
}
/**
* The long answers given by the filler.
*/
public function longAnswers(): HasMany
{
return $this->hasMany(LongAnswer::class);
}
/**
* Create an answer sheet for the given user,
* with their anonymous data.
* The default semester is the current one.
*/
public static function createForUser(User $user, Semester $semester = null): AnswerSheet
{
if (is_null($semester)) {
$semester = Semester::current();
}
return $semester->answerSheets()->create([
'year_of_acceptance' => $user->educationalInformation->year_of_acceptance
]);
}
/**
* Create an answer sheet for the current user `user()`,
* with their anonymous data.
* The default semester is the current one.
*/
public static function createForCurrentUser(Semester $semester = null): AnswerSheet
{
return self::createForUser(user(), $semester);
}
/**
* Returns an array with the data associated with the sheet:
* year of acceptance and then textual answers to the questions
* in order of their ids.
* (For multiple-choice questions, the separators between the choices are commas.)
* The id of the sheet is omitted for better pseudonomity.
*/
public function toArray(): array
{
$row = [
$this->semester->tag,
$this->year_of_acceptance
];
foreach ($this->semester->questions()->orderBy('id')->get() as $question) {
if ($question->question_type == Question::TEXT_ANSWER) {
$row[] = $this->longAnswers()
->where('question_id', $question->id)
->first()->text ?? '';
} elseif ($question->isMultipleChoice()) {
$row[] = $this->chosenOptions()
->where('question_id', $question->id)
->pluck('title')->implode('/');
} else {
$row[] = $this->chosenOptions()
->where('question_id', $question->id)
->first()->title ?? '';
}
}
return $row;
}
}