/**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     //
     parent::boot($router);
     $router->bind('articles', function ($id) {
         $article = Article::findOrFail($id);
         // If the owner return all articles else return only published.
         if (Auth::user() && $article->user_id === Auth::user()->id) {
             $articles = Article::findOrFail($id);
         } else {
             $articles = Article::published()->findOrFail($id);
         }
         return $articles;
     });
     $router->bind('questions', function ($id) {
         return Question::findOrFail($id);
     });
     $router->bind('answers', function ($id) {
         return Answer::findOrFail($id);
     });
     $router->bind('tags', function ($name) {
         return Tag::where('name', $name)->firstOrFail();
     });
     $router->bind('users', function ($id) {
         return User::findOrFail($id);
     });
 }
Esempio n. 2
0
 public function destroy($id)
 {
     $answer = Answer::findOrFail($id);
     if ($answer->votes->contains(auth()->user())) {
         $answer->votes()->detach(auth()->user()->id);
     }
     return redirect()->back();
 }
Esempio n. 3
0
 /**
  * Fetch current user answer's weight
  *
  * @return answer weight on success, otherwise return 0.
  */
 public function weight()
 {
     $answer = Answer::findOrFail($this->answer_id);
     if ($answer) {
         return $answer->weight;
     } else {
         return 0;
     }
 }
Esempio n. 4
0
 public function update(AnswerRequest $request, int $id)
 {
     $answer = Answer::findOrFail($id);
     $this->authorize('edit-answer', $answer);
     $answer->body = $request->get('body');
     $answer->save();
     session()->flash('success', 'La respuesta ha sido editada.');
     return redirect()->route('question.show', $answer->question->id);
 }
Esempio n. 5
0
 public function reply()
 {
     $questionId = Request::get('questionId');
     $chosenAnswerId = Request::get('chosenAnswerId');
     $replyResult = Answer::findOrFail($chosenAnswerId)->is_correct;
     \Auth::user()->replies()->updateOrCreate(['question_id' => $questionId], ['is_correct' => $replyResult]);
     $answers = Question::find($questionId)->answers();
     $correctAnswerId = $answers->where('is_correct', '=', true)->first()->id;
     return response()->json(['correctAnswerId' => $correctAnswerId, 'chosenAnswerId' => $chosenAnswerId, 'replyResult' => $replyResult, 'answers' => $answers->get()]);
 }
Esempio n. 6
0
 public function fieldFormModel($id, array $fields)
 {
     $answer = Answer::findOrFail($id);
     $fieldNames = array_keys(array_except($fields, ['tags']));
     $fields = ['id' => $id];
     foreach ($fieldNames as $field) {
         $fields[$field] = $answer->{$field};
     }
     return $fields;
 }
 /**
  * Store a user's quiz score into storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  Quiz $quiz
  * @return \Illuminate\Http\Response
  */
 public function userQuiz(Request $request, Quiz $quiz)
 {
     //        if(Gate::denies('allow-quiz', $quiz->id)){
     //            redirect()->action('QuizzesController@index');
     //        };
     $answers = collect([]);
     for ($i = 0; $i < $quiz->questions->count(); $i++) {
         if ($request->has('select.' . $i)) {
             $answer = Answer::findOrFail($request->input('select.' . $i));
             $answers = $answers->merge([$answer]);
         }
     }
     $score = $answers->whereLoose('correct', 1)->count();
     $attempt = Auth::user()->hasQuizAttempt($quiz->id) ? Auth::user()->lastQuizTaken($quiz->id)->pivot->attempt + 1 : 1;
     Auth::user()->quizzes()->attach($quiz->id, ['score' => $score, 'attempt' => $attempt]);
     return view('quiz.score', ['answers' => $answers, 'attempt' => $attempt]);
 }
 function answer_handle($game_id, $question_index, PlayRequest $request)
 {
     $current_question_index = $question_index - 1;
     if (Input::get('submit')) {
         $input = Request::all();
         $answer = Answer::findOrFail($input['answer']);
         DB::table('answers')->whereId($answer->id)->increment('count');
         $message = "";
         if ($answer->correct) {
             $message = "Your answer is correct!";
             Session::put('correct_count', Session::get('correct_count') + 1);
         } else {
             $message = "Wrong answer!";
         }
         Session::flash('message', $message);
         return redirect()->action('GamesController@play', [$game_id, $question_index]);
     } elseif (Input::get('check')) {
         $input = Request::all();
         $answer = Answer::findOrFail($input['answer']);
         if (!Session::get('checked_answer')) {
             Session::put('checked_answer', true);
             if ($answer->correct) {
                 $message = "That is the correct answer";
             } else {
                 $message = "That is an incorrect answer";
             }
         } else {
             $message = "You have already used the check answer option";
         }
         Session::flash('message', $message);
         return redirect()->action('GamesController@play', [$game_id, $current_question_index]);
     } elseif (Input::get('fifty')) {
         $message = "fifty fifty";
         if (Session::get('fifty_fifty')) {
             $message = "You've already used your fifty fifty option";
             Session::flash('message', $message);
             return redirect()->action('GamesController@play', [$game_id, $current_question_index]);
         } else {
             Session::put('fifty_fifty', true);
         }
         $game = Game::findOrFail($game_id);
         $question = $game->questions[$current_question_index];
         $i = 0;
         foreach ($question->answers as $answer) {
             if (!$answer->correct && $i < 2) {
                 $question->answers = $question->answers->except($answer->id);
                 $i += 1;
             }
         }
         Session::flash('message', $message);
         $progress = $this->progress($game, $question_index);
         $audience = $this->audience($question);
         return view('games.play')->with(['game' => $game, 'question' => $question, 'next_question_index' => $question_index, 'question_index' => $current_question_index, 'progress' => $progress, 'audience' => $audience]);
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($qId, $id)
 {
     $answer = Answer::findOrFail($id);
     $answer->delete();
     return redirect('admin/questions/' . $qId . '/answers');
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  *
  * @return Response
  */
 public function update($id, Request $request)
 {
     if (!isset($request->qcode)) {
         return redirect('questionnaire')->with('error', 'Questionnaire code not set');
     }
     $guest = Guest::findByQcode($request->qcode);
     if (!isset($guest)) {
         return redirect('questionnaire')->with('error', 'Cannot find that questionnaire code');
     }
     // Check for changes to the guest
     $delta = $this->delta_check($guest, $request);
     $answer = Answer::findOrFail($id);
     $answer->update($request->all() + ['delta' => $delta]);
     Session::flash('message', 'Questionnaire updated!');
     return redirect()->route('answers', ['guest' => $guest, '#view2016']);
 }
Esempio n. 11
0
 public function update(Request $request, $id)
 {
     $question = Question::findOrFail($id);
     $rules = ['name' => 'required|min:5', 'class' => 'required', 'subject_id' => 'required', 'partition_id' => 'required'];
     $attrName = [0 => 'с първият отговор', 1 => 'с вторият отговор', 2 => 'с третият отговор', 3 => 'с четвъртият отговор', 4 => 'с петият отговор', 5 => 'с шестият отговор', 6 => 'с седмият отговор', 7 => 'с осмият отговор'];
     foreach ($request->get('answers') as $key => $value) {
         if ($key < 4) {
             $rules['answers.' . $key . '.id'] = 'required';
         }
         $rules['answers.' . $key . '.name'] = 'required|min:5';
         $messages['answers.' . $key . '.name.required'] = 'Полето ' . $attrName[$key] . ' е задължително.';
         $messages['answers.' . $key . '.name.min'] = 'Полето ' . $attrName[$key] . ' трябва да бъде минимум :min знака.';
     }
     $validator = \Validator::make($request->all(), $rules, $messages);
     if ($validator->fails()) {
         return $validator->errors()->toJSON();
     }
     $input['name'] = $request->get('name');
     $input['class'] = $request->get('class');
     $input['subject_id'] = $request->get('subject_id');
     $input['partition_id'] = $request->get('partition_id');
     $reqCorrect = $request->get('correct');
     if (sizeof($reqCorrect) > 1) {
         $input['type'] = 'multiple';
     } else {
         $input['type'] = 'one';
     }
     $question->fill($input)->save();
     foreach ($request->get('answers') as $key => $value) {
         if (array_key_exists('id', $value)) {
             // Updating answers
             $answer = Answer::findOrFail($value['id']);
             $input['name'] = $value['name'];
             $input['question_id'] = $id;
             $input['correct'] = false;
             if (sizeof($reqCorrect) > 1) {
                 foreach ($reqCorrect as $k => $v) {
                     if ($key == $reqCorrect[$k]) {
                         $input['correct'] = true;
                     }
                 }
             } else {
                 if ($key == $reqCorrect[0]) {
                     $input['correct'] = true;
                 }
             }
             $answer->fill($input)->save();
         } else {
             // Creating answers if they don't exists
             $input['name'] = $value['name'];
             $input['question_id'] = $id;
             $input['correct'] = false;
             $reqCorrect = $request->get('correct');
             if (is_array($reqCorrect)) {
                 foreach ($reqCorrect as $k => $v) {
                     if ($key == $reqCorrect[$k]) {
                         $input['correct'] = true;
                     }
                 }
             } else {
                 if ($key == $reqCorrect) {
                     $input['correct'] = true;
                 }
             }
             $answer->create($input);
         }
     }
     // Deleting answers
     $removedIds = explode(', ', $request->get('removedIds'));
     foreach ($removedIds as $key => $value) {
         $remove_answer[$key] = Answer::where('id', '=', $value)->where('question_id', '=', $id)->delete();
     }
     \Session::flash('flash_message', 'Въпросът беше успешно редактиран!');
     return ['type' => 'redirect', 'url' => url(route('admin.question.index'))];
 }
Esempio n. 12
0
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     //
     $answer = Answer::findOrFail($id)->with(['User'])->get();
     return $answer;
 }
 public function getFeedbackDelAnswer($id)
 {
     $answer = \App\Answer::findOrFail($id);
     $answer->delete();
     return redirect('admin/feedbacks');
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int $id
  * @return Response
  */
 public function destroy($id)
 {
     $answer = Answer::findOrFail($id);
     $answer->delete();
     $message = trans('messages.answer_successfully_removed');
     Flash::info($message);
     return redirect()->route('teacher.units.show', $answer->question->unit);
 }
 public function destroy($id)
 {
     $answer = Answer::findOrFail($id);
     $answer->delete();
     return redirect('questions/answers');
 }