-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathQuestionController.php
89 lines (85 loc) · 3.65 KB
/
QuestionController.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
<?php
namespace App\Http\Controllers\StudentsCouncil;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Validation\Rule;
use App\Models\AnonymousQuestions\AnswerSheet;
use App\Models\Semester;
use App\Models\Question;
use App\Models\QuestionOption;
use App\Models\GeneralAssemblies\GeneralAssembly;
use App\Utils\HasPeriodicEvent;
use App\Exports\UsersSheets\AnonymousQuestionsExport;
/**
* Controls actions related to anonymous questions.
*/
class QuestionController extends Controller
{
/**
* Saves a new question.
*/
protected function createQuestion(Request $request, Semester|GeneralAssembly $parent = null): Question
{
$validatedData = $request->validate([
'title' => 'required|string',
'question_type' => [
'required',
Rule::in(Question::QUESTION_TYPES)
],
'max_options' => ['required', 'min:1', Rule::excludeIf($request['question_type'] == QUESTION::TEXT_ANSWER), 'integer'],
'options' => ['required', 'min:1', Rule::excludeIf($request['question_type'] != QUESTION::SELECTION && $request['question_type'] != QUESTION::RANKING), 'array'],
'options.*' => ['required', 'min:1', 'max:255', Rule::excludeIf($request['question_type'] != QUESTION::SELECTION && $request['question_type'] != QUESTION::RANKING), 'string'],
]);
if ($validatedData['question_type'] == Question::SELECTION || $validatedData['question_type'] == Question::RANKING) {
$options = array_filter($validatedData['options'], function ($s) {
return $s != null;
});
if (count($options) == 0) {
$validator->after(function ($validator) {
$validator->errors()->add('options', __('voting.at_least_one_option'));
});
}
}
$question = $parent->questions()->create([
'title' => $validatedData['title'],
'max_options' => isset($validatedData['max_options']) ? $validatedData['max_options'] : null,
'question_type' => $validatedData['question_type'],
]);
if ($validatedData['question_type'] == Question::SELECTION || $validatedData['question_type'] == Question::RANKING) {
foreach ($options as $option) {
$question->options()->create([
'title' => $option,
'votes' => 0
]);
}
}
return $question;
}
protected function saveVoteForQuestion(Question $question, $validatedData, ?AnswerSheet $answerSheet = null)
{
// validation ensures we have answers
// to all of these questions
$answer = $validatedData[$question->formKey()];
if ($question->question_type == Question::TEXT_ANSWER ||
$question->question_type == Question::RANKING) {
$question->storeAnswers(user(), $answer, $answerSheet);
} elseif ($question->question_type == Question::SELECTION) {
if ($question->isMultipleChoice()) {
$options = array_map(
function (int $id) {return QuestionOption::find($id);},
$answer
);
$question->storeAnswers(user(), $options, $answerSheet);
} else {
$option = QuestionOption::find($answer);
$question->storeAnswers(user(), $option, $answerSheet);
}
} else {
throw new \Exception("Unknown question type");
}
}
}