Initial commit
This commit is contained in:
96
app/Http/Controllers/QuestionController.php
Normal file
96
app/Http/Controllers/QuestionController.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Question;
|
||||
use App\Models\Option;
|
||||
use App\Models\Quiz;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class QuestionController extends Controller
|
||||
{
|
||||
public function store(Request $request, Quiz $quiz)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'nullable|string|max:255',
|
||||
'context' => 'nullable|string',
|
||||
'label' => 'required|string',
|
||||
'points' => 'required|integer|min:1',
|
||||
'type' => 'required|in:qcm,open',
|
||||
'options' => 'required_if:type,qcm|array',
|
||||
'options.*.option_text' => 'nullable|required_if:type,qcm|string',
|
||||
'options.*.is_correct' => 'nullable|required_if:type,qcm|boolean',
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($request, $quiz) {
|
||||
$question = Question::create([
|
||||
'quiz_id' => $quiz->id,
|
||||
'title' => $request->title,
|
||||
'context' => $request->context,
|
||||
'label' => $request->label,
|
||||
'points' => $request->points,
|
||||
'type' => $request->type
|
||||
]);
|
||||
|
||||
if ($request->type === 'qcm') {
|
||||
foreach ($request->options as $opt) {
|
||||
Option::create([
|
||||
'question_id' => $question->id,
|
||||
'option_text' => $opt['option_text'],
|
||||
'is_correct' => $opt['is_correct']
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return back()->with('success', 'Question ajoutée avec succès.');
|
||||
}
|
||||
|
||||
public function update(Request $request, Quiz $quiz, Question $question)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'nullable|string|max:255',
|
||||
'context' => 'nullable|string',
|
||||
'label' => 'required|string',
|
||||
'points' => 'required|integer|min:1',
|
||||
'type' => 'required|in:qcm,open',
|
||||
'options' => 'required_if:type,qcm|array',
|
||||
'options.*.option_text' => 'nullable|required_if:type,qcm|string',
|
||||
'options.*.is_correct' => 'nullable|required_if:type,qcm|boolean',
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($request, $question) {
|
||||
$question->update([
|
||||
'title' => $request->title,
|
||||
'context' => $request->context,
|
||||
'label' => $request->label,
|
||||
'points' => $request->points,
|
||||
'type' => $request->type
|
||||
]);
|
||||
|
||||
if ($request->type === 'qcm') {
|
||||
$question->options()->delete();
|
||||
foreach ($request->options as $opt) {
|
||||
Option::create([
|
||||
'question_id' => $question->id,
|
||||
'option_text' => $opt['option_text'],
|
||||
'is_correct' => $opt['is_correct']
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$question->options()->delete();
|
||||
}
|
||||
});
|
||||
|
||||
return back()->with('success', 'Question mise à jour avec succès.');
|
||||
}
|
||||
|
||||
public function destroy(Quiz $quiz, Question $question)
|
||||
{
|
||||
$question->delete();
|
||||
|
||||
return back()->with('success', 'Question supprimée avec succès.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user