示例#1
0
 /**
  * Receive answer from test and format next question and test
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $user = Auth::user();
     $question = Question::with('skill')->with('difficulty')->with('skill.level')->with('skill.track')->find($request->question_id);
     // log
     $question->tests()->updateExistingPivot($request->test_id, ['answered' => TRUE], false);
     $user->tested_questions()->attach($request->question_id, ['correct' => $correct = $question->correct_answer == $request->answer]);
     // initialize
     $error_limit = Config::get('mathtest.error_test');
     $success_limit = Config::get('mathtest.success_test');
     $difficulty = $question->difficulty_id;
     $skill = $question->skill;
     $level = $skill->level;
     $track = $skill->track;
     $test = Test::find($request->test_id);
     $maxile = 0;
     // track if user has done questions of the same skill and difficulty
     $test_record = $test->questions()->selectRaw('question_test.question_id as id')->lists('id');
     $total_correct = $user->numberCorrect()->where('questions.difficulty_id', '=', $difficulty)->where('questions.skill_id', '=', $skill->id)->take(max($error_limit, $success_limit))->first()->total_correct;
     //        dd($total_correct);
     //       dd($question->correct_answer == $request->answer);
     $new_question = new Question();
     //        return $track->users()->get();
     if ($correct) {
         //if answer is correct
         //         dd($correct);
         if ($total_correct < $success_limit - 1) {
             //cleared this difficulty
             if ($new_question = Question::similar($difficulty, $skill->id)->whereNotIn('id', $test_record)->first()) {
                 //                    dd($correct);
             }
         } else {
             $user->track_results()->attach($question->track, ['difficulty_id' => $question->difficulty_id, 'skill_id' => $question->skill_id, 'level_id' => $question->skill->level->id, 'track_id' => $question->skill->track->id, 'maxile' => intval($question->skill->level->starting_maxile_level + 100 * ($difficulty / Difficulty::max('difficulty')) * ($skill->skill / Skill::whereLevelId($level->id)->max('skill')))]);
             if ($difficulty < Difficulty::max('difficulty')) {
                 $new_question = Question::harder($difficulty, $skill->id)->whereNotIn('id', $test_record)->first();
             } elseif ($skill->skill < Skill::whereTrackId($track->id)->whereLevelId($level->id)->max('skill')) {
                 $new_question = Question::whereNotIn('id', $test_record)->upskill($skill, $track->id, $level->id)->first();
             } elseif ($level->level < Level::max('level')) {
                 $new_question = Question::whereNotIn('id', $test_record)->whereSkillId(Skill::orderBy('skill', 'asc')->first()->id)->first();
             } else {
                 return ['msg' => 'You have reached the maximum level and difficulty for all skills in this track.'];
             }
         }
         // if answer is wrong
     } elseif ($difficulty > Difficulty::min('difficulty')) {
         $new_question = Question::easier($difficulty, $skill->id)->whereNotIn('id', $test_record)->first();
     } elseif ($skill->skill > Skill::whereTrackId($track->id)->whereLevelId($level->id)->min('skill')) {
         $new_question = Question::whereNotIn('id', $test_record)->downskill($skill, $track->id, $level->id)->first();
     } elseif ($level->level > Level::min('level')) {
         $new_question = Question::whereNotIn('id', $test_record)->whereSkillId(Skill::orderBy('skill', 'desc')->first()->id)->first();
     } else {
         return ['msg' => 'You have reached the minimum level and difficulty for all skills in this track.'];
     }
     //        dd($new_question);
     if (isset($new_question) and $new_question->id != null) {
         $new_question->tests()->attach($request->test_id, ['answered' => FALSE]);
         return $this->formatQuiz($new_question, $request->test_id);
     } else {
         return ['result' => Track::join('track_user', 'id', '=', 'track_id')->where('track_user.user_id', '=', $user->id)->select('tracks.track')->selectRaw('max(track_user.maxile) as max')->groupBy('tracks.track')->get()];
     }
 }