/**
  * 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);
 }
Beispiel #2
0
 public function priorexam(Request $request)
 {
     $statistics = ExamResult::whereHas('Exam', function ($q) {
         $q->whereRaw('exams.start_at > exam_results.enter_at');
     })->with('student', 'exam');
     if (request('student_id')) {
         $statistics->whereHas('student', function ($q) use($request) {
             $q->where('students.id', request('student_id'));
         });
     }
     if (request('exam_subject')) {
         $statistics->whereHas('exam', function ($q) use($request) {
             $q->where('subject_id', request('exam_subject'));
         });
     }
     if (request('exam_type')) {
         $statistics->whereHas('exam', function ($q) use($request) {
             $q->where('type', request('exam_type'));
         });
     }
     $per_page = request('per_page') ? request('per_page') : 100;
     $statistics = $statistics->paginate($per_page);
     $statistics->appends($request->except("page"));
     $subjects = Subject::lists('name', 'id')->toArray();
     return view('exams::reports.priorexam', compact('statistics', 'subjects'));
 }
Beispiel #3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $semester_id = semester()->id;
     $results = ExamResult::leftJoin('exam_result_answers as era', 'era.exam_result_id', '=', 'exam_results.id')->select('exam_results.id', 'exam_results.student_id', 'exam_results.filename')->where('exam_results.filename', '!=', '')->where('exam_results.semester_id', $semester_id)->whereNull('era.id')->groupBy('exam_results.id')->get();
     // $semester_id = semester()->id;
     if (!$results->count()) {
         $this->comment(PHP_EOL . 'All is corrected' . PHP_EOL);
     }
     $this->comment(PHP_EOL . 'Starting Correcting ' . $results->count() . '....' . PHP_EOL);
     foreach ($results as $result) {
         // $this->comment(PHP_EOL.'C'.PHP_EOL);
         try {
             $file = File::get('/var/www/node/rest/exams/' . $result->filename);
         } catch (\Illuminate\Contracts\Filesystem\FileNotFoundException $e) {
             $this->comment(PHP_EOL . 'No Answers File Provided' . PHP_EOL);
             continue;
         }
         $exam_file = json_decode($file);
         if (empty($exam_file->exam_id)) {
             $error = 1;
             $this->comment(PHP_EOL . 'Wrong file no exam_id' . PHP_EOL);
             continue;
         }
         if (empty($exam_file->questions) or !is_array($exam_file->questions)) {
             $this->comment(PHP_EOL . 'No Answers in the file you provided' . PHP_EOL);
             continue;
         }
         $exam_id = $exam_file->exam_id;
         $student_id = $exam_file->student_id;
         if (!($exam = Exam::with(['questions' => function ($query) {
             $query->wherePivot('devide_grade', 0);
         }, 'questions.choices'])->find($exam_id))) {
             $this->comment(PHP_EOL . 'Exam does not exist' . PHP_EOL);
             continue;
         }
         $result_data = ['student_id' => $student_id, 'exam_id' => $exam->id, 'semester_id' => $exam->semester_id];
         $grade_per_question = 0.5;
         if ($exam->type == 'activity') {
             $grade_per_question = $this->activity_points / $exam->questions->count();
         } else {
             if ($exam->type == 'midterm') {
                 $grade_per_question = $this->midterm_points / $exam->questions->filter(function ($question) {
                     return $question->type != 'essay';
                 })->count();
             } else {
                 $this->comment(PHP_EOL . 'We Only correcting activities for now' . PHP_EOL);
                 continue;
             }
         }
         // dd($grade_per_question);
         // if ($result = ExamResult::firstOrCreate($result_data)) {
         $result_answers = [];
         $grade = 0.0;
         $answers = $exam_file->questions;
         foreach ($answers as $answer) {
             $result_answer = [];
             $result_answer['question_id'] = $answer->question_id;
             $result_answer['answer'] = $answer->answer;
             $result_answers[$answer->question_id] = $result_answer;
         }
         StudentGrade::where('student_id', $student_id)->where(['ref_key' => 'exam', 'ref_value' => $exam->id])->delete();
         $result->answers()->delete();
         foreach ($exam->questions as $question) {
             $current_grade = 0;
             switch ($question->type) {
                 case 'true_false':
                     $true_choice = $question->choices->first(function ($key, $choice) {
                         return $choice->istrue;
                     });
                     $is_true = array_where($answers, function ($key, $answer) use($true_choice) {
                         return $answer->answer == $true_choice->id;
                     });
                     // var_dump($true_choice->toArray());
                     if (count($is_true)) {
                         $current_grade = $grade_per_question;
                     }
                     break;
                 case 'single_choice':
                     $true_choice = $question->choices->first(function ($key, $choice) {
                         return $choice->istrue;
                     });
                     $is_true = array_where($answers, function ($key, $answer) use($true_choice) {
                         return $answer->answer == $true_choice->id;
                     });
                     if (count($is_true)) {
                         $current_grade = $grade_per_question;
                     }
                     break;
                 case 'multiple_choice':
                     $question_grade = 0;
                     $multiple_answer = array_first($answers, function ($key, $answer) use($question) {
                         return $answer->question_id == $question->id;
                     });
                     if ($multiple_answer) {
                         $all_answers = $multiple_answer ? array_filter(explode(",", $multiple_answer->answer)) : [];
                         $all_answered_count = count($all_answers);
                         if ($all_answered_count == $question->choices->count() || $all_answered_count == 0) {
                             $current_grade = 0.0;
                         } else {
                             $true_answers = $question->choices->filter(function ($question) {
                                 return $question->istrue;
                             });
                             if ($true_answers) {
                                 $true_answers_ids = $true_answers->pluck('id')->toArray();
                                 $true_answers_count = count($true_answers_ids);
                                 $right_answered_count = count(array_intersect($all_answers, $true_answers_ids));
                                 $wrong_answered_count = $all_answered_count - $right_answered_count;
                                 $grade_per_choice = $true_answers_count > 0 ? $grade_per_question / $true_answers_count : 0;
                                 $multichoice_right_grade = $right_answered_count * $grade_per_choice;
                                 $multichoice_wrong_grade = $wrong_answered_count * $grade_per_choice;
                                 if ($multichoice_wrong_grade <= $multichoice_right_grade) {
                                     $question_grade = $multichoice_right_grade - $multichoice_wrong_grade;
                                 } else {
                                     $question_grade = 0;
                                 }
                                 // if ($question_grade > $grade_per_question) {
                                 // $current_grade = $grade_per_question;
                                 // } else {
                                 $current_grade = $question_grade;
                                 // }
                                 // $current_grade = $multichoice_right_grade;
                             }
                         }
                     }
                     break;
             }
             $result_answers[$question->id]['degree'] = $current_grade;
             $grade += $current_grade;
         }
         foreach ($result_answers as $a) {
             $result->answers()->create($a);
         }
         if ($exam->type == 'activity' && $grade > $this->activity_points) {
             $grade = $this->activity_points;
         }
         $notes = '';
         switch ($exam->type) {
             case 'midterm':
                 $notes = 'درجة اختبار المنتصف الاختيار ' . $exam->name;
                 break;
             case 'activity':
                 $notes = 'درجة النشاط ' . $exam->name;
                 break;
         }
         $search_data = ['student_id' => $student_id, 'ref_value' => $exam->id, 'ref_key' => 'exam', 'subject_id' => $exam->subject_id, 'semester_id' => $semester_id, 'notes' => $notes];
         $update_data = ['value' => round($grade, 2)];
         StudentGrade::updateOrCreate($search_data, $update_data);
     }
     $this->comment(PHP_EOL . 'Correcting is done :)' . PHP_EOL);
 }
Beispiel #4
0
 public function checkInOut(Request $request)
 {
     $result_data = ['student_id' => $this->student->id, 'exam_id' => $request->input('exam_id'), 'semester_id' => $this->semester->id];
     $result = ExamResult::firstOrCreate($result_data);
     if ($request->input('type', 'in') == 'in') {
         $result->enter_at = new DateTime();
     } else {
         $result->exit_at = new DateTime();
     }
     $result->save();
     return response()->json(['error' => false], 200, [], JSON_NUMERIC_CHECK);
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     //Exam
     Exam::created(function ($exam) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_exam', 'reference_id' => $exam->id]);
     });
     Exam::updated(function ($exam) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_exam', 'reference_id' => $exam->id]);
     });
     Exam::deleted(function ($exam) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_exam', 'reference_id' => $exam->id]);
     });
     //ExamCenter
     ExamCenter::created(function ($center) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_center', 'reference_id' => $center->id]);
     });
     ExamCenter::updated(function ($center) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_center', 'reference_id' => $center->id]);
     });
     ExamCenter::deleted(function ($center) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_center', 'reference_id' => $center->id]);
     });
     //ExamCenterPeriod
     ExamCenterPeriod::created(function ($period) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_period', 'reference_id' => $period->id]);
     });
     ExamCenterPeriod::updated(function ($period) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_period', 'reference_id' => $period->id]);
     });
     ExamCenterPeriod::deleted(function ($period) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_period', 'reference_id' => $period->id]);
     });
     //ExamQuestion
     ExamQuestion::created(function ($question) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_question', 'reference_id' => $question->id]);
     });
     ExamQuestion::updated(function ($question) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_question', 'reference_id' => $question->id]);
     });
     ExamQuestion::deleted(function ($question) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_question', 'reference_id' => $question->id]);
     });
     //ExamRecording
     ExamRecording::created(function ($recording) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_recording', 'reference_id' => $recording->id]);
     });
     ExamRecording::updated(function ($recording) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_recording', 'reference_id' => $recording->id]);
     });
     ExamRecording::deleted(function ($recording) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_recording', 'reference_id' => $recording->id]);
     });
     //ExamResult
     ExamResult::created(function ($result) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_result', 'reference_id' => $result->id]);
     });
     ExamResult::updated(function ($result) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_result', 'reference_id' => $result->id]);
     });
     ExamResult::deleted(function ($result) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_result', 'reference_id' => $result->id]);
     });
     //ExamResultAnswer
     ExamResultAnswer::created(function ($answer) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_answer', 'reference_id' => $answer->id]);
     });
     ExamResultAnswer::updated(function ($answer) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_answer', 'reference_id' => $answer->id]);
     });
     ExamResultAnswer::deleted(function ($answer) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_answer', 'reference_id' => $answer->id]);
     });
     //ExamRules
     ExamRules::created(function ($rule) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_rule', 'reference_id' => $rule->id]);
     });
     ExamRules::updated(function ($rule) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_rule', 'reference_id' => $rule->id]);
     });
     ExamRules::deleted(function ($rule) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_rule', 'reference_id' => $rule->id]);
     });
     //ExamExcuse
     ExamExcuse::created(function ($excuse) {
         UserLog::create(['operation' => 'create', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_excuses', 'reference_id' => $excuse->id]);
     });
     ExamExcuse::updated(function ($excuse) {
         UserLog::create(['operation' => 'update', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_excuses', 'reference_id' => $excuse->id]);
     });
     ExamExcuse::deleted(function ($excuse) {
         UserLog::create(['operation' => 'delete', 'user_id' => user() ? user()->id : NULL, 'reference_key' => 'Exams_excuses', 'reference_id' => $excuse->id]);
     });
 }
 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', 'تمت العملية بنجاح');
 }