/**
  * Answer a Question.
  *
  * @param $question
  * @param $value
  * @return Model|Answer
  */
 public function answerQuestion($question, $value)
 {
     $question = Question::resolveSelf($question);
     $answer = $this->getAnswerFromQuestion($question);
     if (!$answer) {
         $answer = $this->createNewAnswer($question, $value);
     } else {
         $answer = $this->updateExistingAnswer($answer, $value);
     }
     $this->touch();
     return $answer;
 }
 /** @test */
 public function can_search_for_specific_question_fuzzy_match()
 {
     $interrogator = new Interrogator();
     $section = $interrogator->createSection('Section 1', [], 'MetricLoop\\Interrogator\\User', 1);
     $section2 = $interrogator->createSection('Section 2', [], 'MetricLoop\\Interrogator\\Client', 1);
     $group = $interrogator->createGroup('Group 1', $section, [], 1);
     $group2 = $interrogator->createGroup('Group 2', $section2, [], 1);
     $interrogator->createQuestion('Question 1', 1, $group, [], [], 1);
     $interrogator->createQuestion('Question 2', 2, $group, [], [], 1);
     $interrogator->createQuestion('Question 3', 3, $group, [], [], 1);
     $interrogator->createQuestion('Question 4', 4, $group, [], [], 1);
     $interrogator->createQuestion('Question 5', 5, $group, [], [], 1);
     $interrogator->createQuestion('Question 6', 6, $group, [], [], 1);
     $interrogator->createQuestion('Question 1', 1, $group2, [], [], 1);
     $interrogator->createQuestion('Question 2', 2, $group2, [], [], 1);
     $interrogator->createQuestion('Question 3', 3, $group2, [], [], 1);
     $interrogator->createQuestion('Question 4', 4, $group2, [], [], 1);
     $interrogator->createQuestion('Question 5', 5, $group2, [], [], 1);
     $interrogator->createQuestion('Question 6', 6, $group2, [], [], 1);
     $user = User::create(['name' => 'Ulysses User', 'email' => '*****@*****.**', 'current_team_id' => 1]);
     $client = Client::create(['name' => 'Carly Client', 'email' => '*****@*****.**', 'current_team_id' => 1]);
     $section = Section::where('class_name', get_class($user))->first();
     foreach ($section->groups as $group) {
         foreach ($group->questions as $question) {
             $user->answerQuestion($question, 'Test Answer');
         }
     }
     $section = Section::where('class_name', get_class($client))->first();
     foreach ($section->groups as $group) {
         foreach ($group->questions as $question) {
             $client->answerQuestion($question, 'Test Answer');
         }
     }
     $term = 'est Answe';
     $question = Question::first();
     $results = $interrogator->searchQuestion($term, get_class($user), $question, 1);
     $this->assertCount(1, $results);
 }
 public function test_can_restore_deleted_question()
 {
     $interrogator = new Interrogator();
     $this->createTestQuestion($interrogator);
     $interrogator->deleteQuestion(1);
     $this->assertFalse(Section::first()->trashed());
     $this->assertFalse(Group::first()->trashed());
     $this->assertTrue(Question::withTrashed()->first()->trashed());
     $interrogator->restoreQuestion(1);
     $this->assertFalse(Section::first()->trashed());
     $this->assertFalse(Group::first()->trashed());
     $this->assertFalse(Question::first()->trashed());
 }
 /**
  * Returns a single Question.
  *
  * @param null $question
  * @return null
  */
 public function getQuestion($question = null)
 {
     return Question::resolveSelf($question);
 }
Exemple #5
0
 /**
  * Resolves Question object regardless of given identifier.
  *
  * @param $question
  * @param bool $withTrashed
  * @return \Illuminate\Database\Eloquent\Collection|Model|null
  * @throws QuestionNotFoundException
  */
 public static function resolveSelf($question, $withTrashed = false)
 {
     if (is_null($question)) {
         return null;
     }
     if (!$question instanceof Question) {
         if (is_numeric($question)) {
             try {
                 if ($withTrashed) {
                     $question = Question::withTrashed()->with('type')->findOrFail($question);
                 } else {
                     $question = Question::with('type')->findOrFail($question);
                 }
             } catch (ModelNotFoundException $e) {
                 throw new QuestionNotFoundException('Question not found with the given ID.');
             }
         } else {
             try {
                 if ($withTrashed) {
                     $question = Question::withTrashed()->whereSlug($question)->with('type')->firstOrFail();
                 } else {
                     $question = Question::whereSlug($question)->with('type')->firstOrFail();
                 }
             } catch (ModelNotFoundException $e) {
                 throw new QuestionNotFoundException('Question not found with the given slug.');
             }
         }
     }
     return $question;
 }