Exemplo n.º 1
0
 public function viewSearch(Request $request)
 {
     $input = $request->all();
     $questions = Question::where('question', 'LIKE', '%' . $input['search'] . '%')->orWhere('answer', 'LIKE', '%' . $input['search'] . '%')->select('question', 'answer')->get();
     $specialquestions = Specialquestion::where('question', 'LIKE', '%' . $input['search'] . '%')->orWhere('answer', 'LIKE', '%' . $input['search'] . '%')->select('question', 'answer')->get();
     return view('admin/search')->with(['questions' => $questions, 'specialquestions' => $specialquestions, 'string' => $input['search']]);
 }
Exemplo n.º 2
0
 public function viewPastSpecialQuiz($id)
 {
     $specialquiz = Specialquiz::find($id);
     $users = User::all();
     if (!count($specialquiz)) {
         return redirect()->route('home');
     }
     $year = $specialquiz->year;
     $month = $specialquiz->month;
     $day = $specialquiz->day;
     if ($year < date('Y')) {
         //
     } else {
         if ($year == date('Y') && $month < date('n')) {
             ///
         } else {
             if ($year == date('Y') && $month == date('n') && $day < date('j')) {
                 //
             } else {
                 if (!Auth::user()->isAdmin()) {
                     return redirect()->route('home');
                 }
             }
         }
     }
     $questions = Specialquestion::where('quiz_id', $id)->get();
     $filetype = [];
     $answers = [];
     $ranking = [];
     $i = 1;
     foreach ($questions as $key => $question) {
         $filetype[$i] = pathinfo($question->filename, PATHINFO_EXTENSION);
         $answers[$i] = '';
         $i++;
     }
     $user_id = Auth::user()->id;
     $submitted = false;
     $i = 1;
     foreach ($questions as $key => $question) {
         $allAnswers = Specialanswer::where('question_id', $question['id'])->get();
         $submittedAnswers = $allAnswers->whereLoose('submitted', 1);
         $correctAnswers = $submittedAnswers->whereLoose('correct', 1);
         $correctedAnswers = $submittedAnswers->whereLoose('corrected', 1);
         $answer = $allAnswers->whereLoose('question_id', $question['id'])->first();
         $answer->bonus = intval(100 - count($correctAnswers) / count($submittedAnswers) * 100);
         $questions[$key]->percentage = round(count($correctAnswers) / count($submittedAnswers) * 100, 2);
         if ($correctedAnswers == $submittedAnswers) {
             foreach ($submittedAnswers as $key => $submittedAnswer) {
                 if (!isset($ranking[$submittedAnswer->user_id])) {
                     $ranking[$submittedAnswer->user_id] = [];
                     $ranking[$submittedAnswer->user_id]['total'] = 0;
                 }
                 if ($submittedAnswer->correct) {
                     if ($submittedAnswer->banker) {
                         $ranking[$submittedAnswer->user_id][$i]['points'] = 20 + $answer->bonus;
                         $ranking[$submittedAnswer->user_id][$i]['banker'] = 'bonus';
                     } else {
                         $ranking[$submittedAnswer->user_id][$i]['points'] = 20;
                         $ranking[$submittedAnswer->user_id][$i]['banker'] = false;
                     }
                 } else {
                     if ($submittedAnswer->banker) {
                         $ranking[$submittedAnswer->user_id][$i]['points'] = -20;
                         $ranking[$submittedAnswer->user_id][$i]['banker'] = 'wrong';
                     } else {
                         $ranking[$submittedAnswer->user_id][$i]['points'] = 0;
                         $ranking[$submittedAnswer->user_id][$i]['banker'] = false;
                     }
                 }
                 $ranking[$submittedAnswer->user_id]['total'] += $ranking[$submittedAnswer->user_id][$i]['points'];
             }
         } else {
             $ranking = null;
         }
         $userAnswer = $allAnswers->whereLoose('user_id', $user_id)->whereLoose('submitted', 1)->first();
         if (count($userAnswer)) {
             $answers[$i] = $userAnswer;
             if ($userAnswer->submitted) {
                 $submitted = true;
             }
         } else {
             $answers[$i] = null;
         }
         $i++;
     }
     if ($ranking) {
         foreach ($ranking as $key => $player) {
             $ranking[$key]['id'] = $users->find($key)->id;
             $ranking[$key]['fullName'] = $users->find($key)->fullName();
             $ranking[$key]['avatar'] = $users->find($key)->avatar;
             $ranking[$key]['email'] = $users->find($key)->email;
         }
         $ranking = array_values(array_sort($ranking, function ($value) {
             return 1 / ($value['total'] + 250);
         }));
         foreach ($ranking as $key => $player) {
             if ($key > 0 && $ranking[$key]['total'] == $ranking[$key - 1]['total']) {
                 $ranking[$key]['place'] = $ranking[$key - 1]['place'];
             } else {
                 $ranking[$key]['place'] = $key + 1;
             }
         }
     }
     return view('specialquiz_answered')->with(['quiz' => $specialquiz, 'questions' => $questions, 'answers' => $answers, 'filetype' => $filetype, 'year' => $year, 'month' => $month, 'day' => $day, 'ranking' => $ranking, 'users' => $users]);
 }