101 lines
4.0 KiB
PHP
101 lines
4.0 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Question;
|
|
use App\Models\Subject;
|
|
use App\Models\AnswerOption;
|
|
use Illuminate\Http\Request;
|
|
class QuestionController extends Controller {
|
|
public function index(Request $r) {
|
|
$subjects = Subject::all();
|
|
$query = Question::with('subject')->latest();
|
|
if ($r->filled('subject')) $query->where('subject_id', $r->subject);
|
|
$questions = $query->paginate(20)->withQueryString();
|
|
return view('admin.questions.index', compact('questions','subjects'));
|
|
}
|
|
public function create() {
|
|
$subjects = Subject::all();
|
|
return view('admin.questions.create', compact('subjects'));
|
|
}
|
|
public function store(Request $r) {
|
|
$r->validate([
|
|
'subject_id' => 'required|exists:subjects,id',
|
|
'question_text' => 'required|string',
|
|
'type' => 'required|in:multiple_choice,number_input',
|
|
'difficulty' => 'required|in:1,2,3',
|
|
'options' => 'required_if:type,multiple_choice|array|min:2',
|
|
'options.*' => 'required|string',
|
|
'correct' => 'required_if:type,multiple_choice|integer',
|
|
'number_answer' => 'required_if:type,number_input',
|
|
]);
|
|
$pts = [1=>5, 2=>10, 3=>20][$r->difficulty];
|
|
$q = Question::create([
|
|
'subject_id' => $r->subject_id,
|
|
'question_text' => $r->question_text,
|
|
'type' => $r->type,
|
|
'difficulty' => $r->difficulty,
|
|
'points_value' => $pts,
|
|
'active' => true,
|
|
]);
|
|
if ($r->type === 'multiple_choice') {
|
|
foreach ($r->options as $i => $text) {
|
|
if (trim($text) === '') continue;
|
|
AnswerOption::create([
|
|
'question_id' => $q->id,
|
|
'text' => $text,
|
|
'is_correct' => ($i == $r->correct),
|
|
'sort_order' => $i,
|
|
]);
|
|
}
|
|
} else {
|
|
AnswerOption::create([
|
|
'question_id' => $q->id,
|
|
'text' => $r->number_answer,
|
|
'is_correct' => true,
|
|
'sort_order' => 0,
|
|
]);
|
|
}
|
|
return redirect()->route('admin.questions.index')->with('success','Frage gespeichert.');
|
|
}
|
|
public function edit(Question $question) {
|
|
$subjects = Subject::all();
|
|
$question->load('answerOptions');
|
|
return view('admin.questions.edit', compact('question','subjects'));
|
|
}
|
|
public function update(Request $r, Question $question) {
|
|
$r->validate([
|
|
'subject_id' => 'required|exists:subjects,id',
|
|
'question_text' => 'required|string',
|
|
'difficulty' => 'required|in:1,2,3',
|
|
'options' => 'array',
|
|
'options.*' => 'string',
|
|
'correct' => 'integer',
|
|
]);
|
|
$pts = [1=>5, 2=>10, 3=>20][$r->difficulty];
|
|
$question->update([
|
|
'subject_id' => $r->subject_id,
|
|
'question_text' => $r->question_text,
|
|
'difficulty' => $r->difficulty,
|
|
'points_value' => $pts,
|
|
'active' => $r->boolean('active', true),
|
|
]);
|
|
if ($r->filled('options')) {
|
|
$question->answerOptions()->delete();
|
|
foreach ($r->options as $i => $text) {
|
|
if (trim($text) === '') continue;
|
|
AnswerOption::create([
|
|
'question_id' => $question->id,
|
|
'text' => $text,
|
|
'is_correct' => ($i == $r->correct),
|
|
'sort_order' => $i,
|
|
]);
|
|
}
|
|
}
|
|
return redirect()->route('admin.questions.index')->with('success','Gespeichert.');
|
|
}
|
|
public function destroy(Question $question) {
|
|
$question->delete();
|
|
return redirect()->route('admin.questions.index')->with('success','Frage gelöscht.');
|
|
}
|
|
}
|