Beispiel #1
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);
 }