public function store(Question $question, choiceRequest $req, Choice $choiceModel) { $countchoice = 0; $msgdanger = 'لا تستطيع اضافة اجابة للسؤال المقالي'; if ($question->type == 'essay') { return redirect()->back()->with('error', $msgdanger); } if ($question->type == 'true_false') { $countchoice = $choiceModel->where('question_id', $question->id)->count(); $msgerrorTrueFalse = 'لا تستطيع اضافة اكثر من اجابتين للسؤال الصح والخطاء'; if ($countchoice > 1) { return redirect()->back()->with('error', $msgerrorTrueFalse); } } if ($question->type != 'multiple_choice' and $req->input('istrue') == 1) { $choice = $choiceModel->where('question_id', $question->id); $choice->update(['istrue' => 0]); } $choice = $choiceModel->fill($req->all()); $choice->save(); $message = 'تم اضافة الاجابة بنجاح'; if (request('submit') == 'save' and $countchoice < 1) { return redirect()->back()->with('success', $message); } else { return redirect()->route('choice.index', array('qid' => $question->id))->with('success', $message); } }
public function updatequestion(Question $question, QuestionRequest $request, $id, $activity_id) { $question = $question->findOrFail($id); $choices = $request->input('choices', 0); $choices_ids = (array) $request->input('choices_ids', 0); if (!$question->fill($request->all())->save()) { return redirect()->back()->with('error', 'وقع مشكل اثناء تحديث السؤال'); } if ($question->type == 'essay') { Choice::where('question_id', $question->id)->delete(); } else { Choice::where('question_id', $question->id)->whereNotIn('id', $choices_ids)->delete(); } if (is_array($choices)) { foreach ($choices as $key => $choice) { if (array_key_exists($key, $choices_ids)) { $id = $choices_ids[$key]; } else { $id = 0; } //dd($request); if ($question->type == 'multiple_choice') { $istrue = (int) $request->input('choices_correct_' . ($id > 0 ? $id : "new_" . $key)); } if ($question->type != 'multiple_choice') { $istrue = $request->input('choices_correct_new_00') == $id ? 1 : 0; } if ($id > 0) { Choice::where('id', $id)->update(['choice' => $choice, 'istrue' => $istrue]); } else { Choice::create(['choice' => $choice, 'question_id' => $question->id, 'istrue' => $istrue]); } } } $lesson = Lesson::findOrFail($question->lesson_id); $message = 'تم تعديل السؤال بنجاح'; if (request('submit') == 'save') { //return redirect()->route('choice.create',array('id'=>$id))->with('success' ,$message); return redirect()->back()->with('success', $message); } else { return redirect()->route('teachers.profile.questions', array('id' => $activity_id))->with('success', $message); } }