/** * Responds to request to GET /answer/delete/{question_id} */ public function getAnswerDelete($answer_id) { $answer = Answer::find($answer_id); $quiz_id = $answer->question->quiz_id; $answer->delete(); return redirect('/edit/' . $quiz_id); }
public function delete_item($a_id) { $a = Answer::find($a_id); $this->authorize('qna-edit', $a); $a->delete(); return redirect("qs/{$a->q_id}"); }
public function storeQuiz(Request $request, $lecture_id, $subject_id, $assignment_id) { $point = 0; $answers = Input::get('answers'); $request->user()->assignments()->attach(array($assignment_id)); $request->user()->answers()->attach($answers); foreach ($answers as $answer_id) { $answer = Answer::find($answer_id); if ($answer->correct) { $point++; } } DB::update('UPDATE (users_assignments) SET point=? WHERE user_id=? AND assignment_id=?', [$point, $request->user()->id, $assignment_id]); return redirect('/lectures/' . $lecture_id . '/assignments'); }
public function storeQuiz(Request $request, $lecture_id, $subject_id, $topic_id, $knowledgeunit_id) { $point = 0; $answers = Input::get('answers'); $request->user()->knowledgeunits()->attach(array($knowledgeunit_id)); $request->user()->answers()->attach($answers); foreach ($answers as $answer_id) { $answer = Answer::find($answer_id); if ($answer->correct) { $point++; } } DB::update('UPDATE (users_knowledgeunits) SET point=? WHERE user_id=? AND knowledgeunit_id=?', [$point, $request->user()->id, $knowledgeunit_id]); return redirect('/lectures/' . $lecture_id . '/subjects/' . $subject_id . '/topics/' . $topic_id . '/knowledgeunits/' . $knowledgeunit_id . '/quiz'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $test = Test::create($request->all()); $score = 0; $quiz = Quiz::find($request->get('quiz_id')); foreach ($quiz->questions as $question) { if ($request->has($question->id)) { $result = Result::create(['quiz_id' => $request->get('quiz_id'), 'user_id' => $request->get('user_id'), 'test_id' => $test->id, 'question_id' => $question->id, 'answer_id' => $request->get($question->id)]); $answer = Answer::find($request->get($question->id)); if ($answer->correct) { $score += 1; } } } $test->score = $score; $test->save(); return redirect()->route('tests.result', $test); }
protected function _saveTest($data) { $isNewEntry = !array_key_exists('id', $data); if ($isNewEntry) { $test = new Test(); $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first(); $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1; $test->order = $next_course_order; } else { $test = Test::find($data['id']); $submitted_questions = []; foreach ($data['questions'] as $qkey => $qdata) { if (array_key_exists('id', $qdata)) { $submitted_questions[] = $qdata['id']; } } foreach ($test->questions as $question) { if (!in_array($question->id, $submitted_questions)) { $question->delete(); } } if ($test->course->id != $data['course']['id']) { $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first(); $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1; $test->order = $next_course_order; } } $test->course_id = $data['course']['id']; $test->title = $data['title']; $test->description = $data['description']; $test->autodiscard = $data['autodiscard']; $test->save(); /////////////////////// $isNewPage = !array_key_exists('id', $data['page']); $pageBody = trim($data['page']['body']); $page = false; if ($isNewPage) { $page = new Page(); } else { $page = Page::find($data['page']['id']); } if ($page) { $page->test_id = $test->id; $page->body = $pageBody; $page->hidden = false; if (strlen(trim(strip_tags($pageBody, '<img>'))) == 0) { $page->hidden = true; } $page->save(); } ///////////////////////// foreach ($data['questions'] as $qkey => $question_data) { if (!array_key_exists('id', $question_data)) { $question = new Question(); } else { $question = Question::find($question_data['id']); $submitted_answers = []; foreach ($question_data['answers'] as $adata) { if (array_key_exists('id', $adata)) { $submitted_answers[] = $adata['id']; } } foreach ($question->answers as $akey => &$answer) { if (!in_array($answer->id, $submitted_answers) || $question_data['type'] == "TEXT" && $akey > 0 || $question_data['type'] == "TEXTAREA") { $answer->delete(); } } } $question->test_id = $test->id; $question->type = $question_data['type']; $question->title = $question_data['title']; $question->subtitle = $question_data['subtitle']; $question->order = $qkey + 1; $question->data = array_key_exists('data', $question_data) ? $question_data['data'] : '{}'; switch ($question->type) { case 'MULTITEXT': if (!property_exists($question->data, 'multitext_required') || is_null($question->data->multitext_required) || $question->data->multitext_required > count($question_data['answers'])) { $question->data->multitext_required = count($question_data['answers']); } break; } $question->save(); /////////////////////////// foreach ($question_data['answers'] as $akey => $answer_data) { if (!array_key_exists('id', $answer_data)) { $answer = new Answer(); } else { $answer = Answer::find($answer_data['id']); } if (!@$answer) { $answer = new Answer(); } $answer->question_id = $question->id; $answer->text = $answer_data['text']; if ($question->type == "CHOICE") { $answer->is_correct = intval($question_data['correct_answer']) == $akey; } else { $answer->is_correct = @$answer_data['is_correct'] ? true : false; } switch ($question->type) { case 'TEXT': case 'MULTITEXT': $answer->is_correct = true; break; } if (array_key_exists('error_margin', $answer_data)) { $answer->error_margin = $answer_data['error_margin']; } else { $answer->error_margin = 10; } $answer->save(); } } return $test->id; }
public function correctAnswer($id, $correct) { $answer = Answer::find($id); $answer->correct = $correct; $answer->corrected = 1; $answer->save(); return back(); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $answer = Answer::find($id); if ($answer == null) { return response()->json(['message' => 'Answer not found']); } if ($answer->currentUserIsOwner()) { $answer->delete(); return response()->json(['success' => true, 'message' => "Answer Deleted Successfully."]); } else { return response()->json(['success' => false, 'message' => "Answer was not deleted."]); } }
public function show($id) { $answer = Answer::find($id); return view('questions.answers.show')->with(['answer' => $answer]); }
/** * Responds to requests to POST /quizzes/{id} */ public function postQuizzesResult($id, Request $request) { // delete zero grade for validation $grade = Grade::where('quiz_id', $id)->where('user_id', Auth::user()->id)->first(); $grade->taken = FALSE; $grade->save(); $request->all(); // get quiz for validation $quiz = Quiz::find($id); // custom message $messages = array('required' => 'You must answer all questions!'); // make sure each question that is part of the quiz is in the request foreach ($quiz->question as $question) { $this->validate($request, ['answer_for_question.' . $question->id => 'required'], $messages); } // Grades quiz $correct_answers = 0; foreach ($request['answer_for_question'] as $answer) { $correct = Answer::find($answer); if ($correct->correct) { $correct_answers++; } } $score = $correct_answers / $quiz->numberOfQuestions() * 100; // store grade in grades_table $grade = Grade::where('quiz_id', $id)->where('user_id', Auth::user()->id)->first(); $grade->grade = $score; $grade->taken = TRUE; $grade->save(); return view('quiz.result')->with('quiz', $quiz)->with('score', $score); }
public function destroy(Request $request, $lecture_id, $subject_id, $topic_id, $knowledgeunit_id, $question_id, $answer_id) { $answer = Answer::find($answer_id); $answer->delete(); return redirect('/lectures/' . $lecture_id . '/subjects/' . $subject_id . '/topics/' . $topic_id . '/knowledgeunits/' . $knowledgeunit_id . '/questions/' . $question_id . '/answers'); }