-
Notifications
You must be signed in to change notification settings - Fork 15
Implement Single Transferable Voting #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b522630
48aef5a
3930701
81d93c1
cffda85
e48317f
41c52f2
915f9d5
6524b7e
f484e7f
a17ab1d
ffedd65
d201b33
69180e5
517c6e5
5f3c9d3
c7c90f2
df32ac9
a55a1d4
a12d814
7b8068f
56eddfc
09fc13d
81dfd94
67b2927
8865886
cba3f1f
af63219
d30078e
8f37ae7
0768886
39be459
1dcd34f
519362f
096869b
c8067e9
2941e63
3bcc077
17774e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 or general assembly polls. | ||
*/ | ||
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')); | ||
}); | ||
} | ||
} | ||
Comment on lines
+28
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential reference to undefined - $validatedData = $request->validate([
- ...
- ]);
- ...
- if (count($options) == 0) {
- $validator->after(function ($validator) {
- ...
- });
- }
+ $validator = Validator::make($request->all(), [
+ ...
+ ]);
+ $validatedData = $validator->validate();
+ if (count($options) == 0) {
+ $validator->after(function ($validator) {
+ ...
+ });
+ }
|
||
$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"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably irrrelevant with
scopeBindings()
as that ensurers the question is the child of the specified general assembly.