/** * Helper function to check an answer. Side effect: stores the answer in the database. * @param $answer - the answer to check * @return ['fromWhence' => '', 'correct' => true|false] */ protected function checkAnswer(\App\Answer $answer) { if ($this->lastFlashcard->cardtype == 'fill') { $correct = (int) (strtoupper($answer->getAnswer()) == strtoupper($this->lastFlashcard->back)); } else { $correct = (int) ($answer->getAnswer() == $this->lastFlashcard->back); } $this->lastFlashcard->sessions()->save($this->session, ['interaction' => $this->type . 'ed', 'correct' => $correct]); if ($correct && $answer->getFromWhence() == \App\Answer::MC) { //remove card from usuable ones. $this->remainingFlashcards = $this->remainingFlashcards->reject(function ($flashcard) { return $flashcard->id == $this->lastFlashcard->id; }); //add card to interacted $this->interactedFlashcards->push($this->lastFlashcard); $query = DB::table('flashcardables')->where('flashcardable_type', 'App\\User')->where('flashcardable_id', $this->user->id)->where('flashcard_id', $this->lastFlashcard->id)->get(); if (!count($query)) { $this->user->flashcards()->save($this->lastFlashcard, ['num_correct' => 1, 'last_review_time' => \Carbon\Carbon::now()]); } else { $this->user->flashcards()->where('flashcard_id', $this->lastFlashcard->id)->increment('num_correct'); } $this->numCorrect++; return ['fromWhence' => \App\Answer::MC, 'correct' => true]; } else { if (!$correct && $answer->getFromWhence() == \App\Answer::MC) { $query = DB::table('flashcardables')->where('flashcardable_type', 'App\\User')->where('flashcardable_id', $this->user->id)->where('flashcard_id', $this->lastFlashcard->id)->get(); if (!count($query)) { $this->user->flashcards()->save($this->lastFlashcard, ['num_incorrect' => 1, 'last_review_time' => \Carbon\Carbon::now()]); } else { $this->user->flashcards()->where('flashcard_id', $this->lastFlashcard->id)->increment('num_incorrect'); } $this->numIncorrect++; return ['fromWhence' => \App\Answer::MC, 'correct' => false]; } else { return ['fromWhence' => 'card', 'correct' => true]; } } }