/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $results = ExamResult::select('exam_results.exam_id', 'exam_results.student_id', 'exam_results.filename', 'exam_results.id')->leftJoin('exam_result_answers as era', 'era.exam_result_id', '=', 'exam_results.id')->where('exam_results.exam_id', '>=', 459)->where('exam_results.exam_id', '!=', 490)->whereNull('era.id')->get();
     $this->comment(PHP_EOL . $results->count() . PHP_EOL);
     $files = [];
     $updated = 0;
     foreach ($results as $result) {
         $log_files = File::glob(env('ANSWERS_FILES_DIR') . '/*_' . $result->student_id . '_' . $result->exam_id . '*.json');
         // dd($log_files);
         if (empty($log_files)) {
             $this->comment(PHP_EOL . count($log_files) . PHP_EOL);
             continue;
         }
         $numberofanswers = count($log_files);
         if ($numberofanswers >= 2) {
             $this->comment(PHP_EOL . $result->id . PHP_EOL);
             $max_answered = -1;
             $files = [];
             $right_file = '';
             foreach ($log_files as $key => $answer_file) {
                 $files[$key]['total'] = 0;
                 $basename = basename($answer_file);
                 $decoded = json_decode(file_get_contents($answer_file), true);
                 if (empty($decoded)) {
                     continue;
                 }
                 foreach ($decoded['questions'] as $q) {
                     $files[$key]['total'] = $files[$key]['total'] + (!empty($q['answer']) ? 1 : 0);
                 }
                 if ($files[$key]['total'] > $max_answered) {
                     $max_answered = $files[$key]['total'];
                     $files[$key]['name'] = $basename;
                     $right_file = $basename;
                 }
             }
             $this->comment(PHP_EOL . $right_file . PHP_EOL);
             $where = ['id' => $result->id, 'student_id' => $result->student_id, 'exam_id' => $result->exam_id];
             ExamResult::where($where)->update(['filename' => $right_file]);
             $updated++;
         }
     }
     $this->comment(PHP_EOL . 'updated : ' . $updated . PHP_EOL);
 }
 public function do_re_enter(Request $request)
 {
     $students = [$request->input('students')];
     // $students = [10073];
     foreach ($students as $student_id) {
         $exam = Exam::select('exams.type', 'exams.start_at', 'exams.finish_at', 'exams.name', 'exams.id')->join('subject_subjects as subsub', 'subsub.id', '=', 'exams.subject_id')->join('student_subjects as stusub', function ($j) use($student_id) {
             $j->on('stusub.subject_id', '=', 'subsub.id')->where('stusub.student_id', '=', $student_id)->where('stusub.state', '=', 'study');
         })->where(function ($query) use($request, $student_id) {
             $query->orWhereIn('exams.type', ['midterm', 'remidterm', 'activity'])->orWhereRaw('exams.id IN (SELECT ce.exam_id FROM classrooms_exam as ce
                             JOIN classrooms as c ON c.id = ce.classroom_id
                             JOIN classroom_students as cs ON cs.classroom_id = c.id
                                 AND cs.student_id = ' . $student_id . '
                             WHERE exam_id = exams.id GROUP BY ce.id)');
             if ($request->has('finalExam') == 'true') {
                 $query->orWhereIn('exams.type', ['final', 'summer', 'refinal']);
             }
         })->where('exams.semester_id', semester()->id)->where('finish_at', '>=', date('Y-m-d H:i:s'))->groupBy('exams.id')->orderBy('exams.start_at')->with(['questions' => function ($w) {
             $w->select('questionbank_questions.id', 'questionbank_questions.question', 'questionbank_questions.type');
             if ($this->randomize_questions) {
                 $w->orderByRaw('RAND()');
             } else {
                 $w->orderBy('questionbank_questions.type', 'DESC');
             }
         }, 'questions.choices' => function ($w) {
             $w->select('questionbank_choices.id', 'questionbank_choices.question_id', 'questionbank_choices.choice', 'questionbank_choices.istrue');
         }])->first();
         dd($exam);
         ExamResult::where('exam_id', $exam->id)->where('student_id', $student_id)->update(['exit_at' => '']);
     }
     return redirect()->route('exams.extends.reenter')->with('success', 'تمت العملية بنجاح');
 }