Пример #1
0
 public function submitTest($testName)
 {
     $test = Test::where("name", "=", $testName)->first();
     $user = null;
     if (Session::has('athleteEmail')) {
         $email = Session::get('athleteEmail');
         Session::forget('athleteEmail');
         $user = User::where("email", "=", $email)->first();
     } else {
         $user = Auth::user();
     }
     if ($user != null && $test != null) {
         $userAnsweredTest = new UserAnsweredTest();
         $userAnsweredTest->idUser = $user->idUser;
         $userAnsweredTest->idTest = $test->idTest;
         $userAnsweredTest->idProfileAtMoment = $user->idLastProfile;
         $userAnsweredTest->save();
         $questions = $test->questions->sortBy("number");
         // Respuestas
         foreach (Input::except('_token') as $input => $answer) {
             $splited = explode("-", $input);
             $questionNumber = end($splited);
             //                echo $questionNumber;
             $question = $questions->filter(function ($value) use($questionNumber) {
                 return $value->number == $questionNumber;
             })->first();
             $userAnswer = new UserAnswer();
             $userAnswer->idQuestion = $question->idQuestion;
             $userAnswer->idTestAnswer = $answer;
             $userAnsweredTest->userAnswers()->save($userAnswer);
         }
         return Redirect::to('test/' . $testName)->with(array('testSuccessMessage' => 'Gracias, el cuestionario se ha registrado exitosamente!'));
     } else {
         return Redirect::to('test/' . $testName)->with(array('testErrorMessage' => 'Lo sentimos, ha ocurrido un error al registrar el cuestionario.'));
     }
 }
Пример #2
0
 private function getFilteredTests($city, $testId, $startDate, $endDate, $sportId, $genderId)
 {
     $test = Test::where('idTest', '=', $testId)->with('scales.ranges')->first();
     // Filtrado (Ciudad)
     $query = UserAnsweredTest::whereHas('profile', function ($query) use($city) {
         $query->where('idCity', '=', $city);
     });
     // Filtrado (Test)
     $query->whereHas('test', function ($query) use($test) {
         $query->where('idTest', '=', $test->idTest);
     });
     // Filtrado (Fecha)
     $query->whereBetween('created_at', array($startDate, $endDate));
     // Filtrado (Deporte)
     if ($sportId != -1) {
         $query->whereHas('profile', function ($query) use($sportId) {
             $query->where('idSport', '=', $sportId);
         });
     }
     // Filtrado (Género)
     if ($genderId != -1) {
         $query->whereHas('user', function ($query) use($genderId) {
             $query->where('idGender', '=', $genderId);
         });
     }
     $answeredTests = $query->with('profile.city', 'user.gender', 'test.scales.ranges', 'userAnswers.question.scale', 'userAnswers.testAnswer')->get();
     return array('test' => $test, 'answeredTests' => $answeredTests);
 }