Fix: Исправления для вопросов
✅ Ответы с картинками без текста теперь сохраняются ✅ TinyMCE инициализируется после DOMContentLoaded ✅ Валидация: хотя бы текст ИЛИ картинка в ответе ✅ Обновлены store и update методы Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
1c94812f7d
commit
ffcdd86712
|
|
@ -53,6 +53,20 @@ class QuestionController extends Controller
|
||||||
'ordering_items' => 'nullable|array',
|
'ordering_items' => 'nullable|array',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Проверка что есть хотя бы текст или картинка в ответах
|
||||||
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
|
$hasValidAnswer = false;
|
||||||
|
foreach ($validated['answers'] as $answer) {
|
||||||
|
if (!empty($answer['text']) || !empty($answer['image'])) {
|
||||||
|
$hasValidAnswer = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$hasValidAnswer) {
|
||||||
|
return back()->withErrors(['answers' => 'Добавьте хотя бы один ответ (текст или картинку)'])->withInput();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DB::transaction(function () use ($test, $validated) {
|
DB::transaction(function () use ($test, $validated) {
|
||||||
$question = $test->questions()->create([
|
$question = $test->questions()->create([
|
||||||
'type' => $validated['type'],
|
'type' => $validated['type'],
|
||||||
|
|
@ -66,7 +80,7 @@ class QuestionController extends Controller
|
||||||
// Ответы для multiple_choice
|
// Ответы для multiple_choice
|
||||||
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
foreach ($validated['answers'] as $answer) {
|
foreach ($validated['answers'] as $answer) {
|
||||||
if (!empty($answer['text'])) {
|
if (!empty($answer['text']) || !empty($answer['image'])) {
|
||||||
$imagePath = null;
|
$imagePath = null;
|
||||||
if (!empty($answer['image'])) {
|
if (!empty($answer['image'])) {
|
||||||
$imagePath = $answer['image']->store('questions/answers', 'public');
|
$imagePath = $answer['image']->store('questions/answers', 'public');
|
||||||
|
|
@ -145,6 +159,20 @@ class QuestionController extends Controller
|
||||||
'ordering_items' => 'nullable|array',
|
'ordering_items' => 'nullable|array',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Проверка что есть хотя бы текст или картинка в ответах
|
||||||
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
|
$hasValidAnswer = false;
|
||||||
|
foreach ($validated['answers'] as $answer) {
|
||||||
|
if (!empty($answer['text']) || !empty($answer['image'])) {
|
||||||
|
$hasValidAnswer = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$hasValidAnswer) {
|
||||||
|
return back()->withErrors(['answers' => 'Добавьте хотя бы один ответ (текст или картинку)'])->withInput();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DB::transaction(function () use ($question, $validated) {
|
DB::transaction(function () use ($question, $validated) {
|
||||||
$question->update([
|
$question->update([
|
||||||
'type' => $validated['type'],
|
'type' => $validated['type'],
|
||||||
|
|
@ -159,7 +187,7 @@ class QuestionController extends Controller
|
||||||
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
if ($validated['type'] === 'multiple_choice' && !empty($validated['answers'])) {
|
||||||
$question->answers()->delete();
|
$question->answers()->delete();
|
||||||
foreach ($validated['answers'] as $answer) {
|
foreach ($validated['answers'] as $answer) {
|
||||||
if (!empty($answer['text'])) {
|
if (!empty($answer['text']) || !empty($answer['image'])) {
|
||||||
$imagePath = null;
|
$imagePath = null;
|
||||||
if (!empty($answer['image'])) {
|
if (!empty($answer['image'])) {
|
||||||
$imagePath = $answer['image']->store('questions/answers', 'public');
|
$imagePath = $answer['image']->store('questions/answers', 'public');
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,8 @@
|
||||||
<!-- TinyMCE -->
|
<!-- TinyMCE -->
|
||||||
<script src="https://cdn.tiny.mce.com/1/tinymce/7/tinymce.min.js" referrerpolicy="origin"></script>
|
<script src="https://cdn.tiny.mce.com/1/tinymce/7/tinymce.min.js" referrerpolicy="origin"></script>
|
||||||
<script>
|
<script>
|
||||||
// Инициализация TinyMCE
|
// Инициализация TinyMCE после загрузки DOM
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
tinymce.init({
|
tinymce.init({
|
||||||
selector: '#questionText, #questionExplanation',
|
selector: '#questionText, #questionExplanation',
|
||||||
height: 300,
|
height: 300,
|
||||||
|
|
@ -123,6 +124,7 @@ tinymce.init({
|
||||||
input.click();
|
input.click();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function updateQuestionType() {
|
function updateQuestionType() {
|
||||||
const type = document.getElementById('typeSelect').value;
|
const type = document.getElementById('typeSelect').value;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue