public function index($exam_id) { $exam = Exam::with('subject')->findOrFail($exam_id); $exam_questions = ExamQuestion::with(['question' => function ($query) { $query->orderByRaw("case\n when questionbank_questions.type = 'true_false' then 'a'\n when questionbank_questions.type = 'single_choice' then 'b'\n when questionbank_questions.type = 'multiple_choice' then 'c'\n when questionbank_questions.type = 'essay' then 'd'\n end ASC"); }])->select('era.id as has_been_corrected', 'exam_questions.exam_id', 'exam_questions.question_id', 'exam_questions.devide_grade')->has('question')->join('exams as e', 'e.id', '=', 'exam_questions.exam_id')->leftJoin('exam_results as er', 'er.exam_id', '=', 'e.id')->leftJoin('exam_result_answers as era', 'era.exam_result_id', '=', 'er.id')->where('exam_questions.exam_id', $exam->id)->groupBy('exam_questions.id')->get(); $list_questions = Question::select('questionbank_questions.*')->selectRaw('CONCAT(( CASE questionbank_questions.type WHEN "essay" THEN "مقالي" WHEN "true_false" THEN "صح أو خطأ" WHEN "single_choice" THEN "إختياري إجابة واحدة" WHEN "multiple_choice" THEN "إختياري متعدد" WHEN "essay" THEN "مقالي" END), " --- ", questionbank_questions.question) as full_question')->join('subject_lessons as sl', 'sl.id', '=', 'questionbank_questions.lesson_id')->join('subject_subjects as ss', function ($join) use($exam) { $join->on('ss.id', '=', 'sl.subject_subject_id')->where('ss.id', '=', $exam->subject_id); })->groupBy('questionbank_questions.id')->whereNotIn('questionbank_questions.id', $exam_questions->pluck('question_id'))->pluck('full_question', 'id')->toArray(); return view('exams::questions.index', compact('exam_questions', 'list_questions', 'exam')); }
public function subjectexam(Request $request) { $subjects = Subject::lists('name', 'id')->toArray(); $semesters = Semester::select(\DB::raw('CONCAT(academycycle_semesters.name , "-", academycycle_years.name) as name , academycycle_semesters.id as id'))->join('academycycle_years', 'academycycle_years.id', '=', 'academycycle_semesters.academycycle_year_id')->pluck('name', 'id')->toArray(); $year_term = Year::join('academystructure_terms', 'academystructure_years.id', '=', 'academystructure_terms.year_id')->join('academystructure_departments', 'academystructure_terms.id', '=', 'academystructure_departments.term_id')->select(\DB::raw('CONCAT(academystructure_years.name, "-", academystructure_terms.name) as name, GROUP_CONCAT(DISTINCT(academystructure_departments.subject_ids)) as sid'))->groupBy('academystructure_terms.name', 'academystructure_years.name')->get()->toArray(); $year_term_options; foreach ($year_term as $key => &$aa) { $a = implode(',', array_unique(array_merge(json_decode(str_replace('],[', ',', $aa['sid']), TRUE)))); $year_term_options[$a] = $aa['name']; } $spec_id = Specialty::join('academystructure_departments', 'academystructure_departments.spec_id', '=', 'academystructure_specialties.id')->select(\DB::raw('academystructure_specialties.name as name, GROUP_CONCAT(DISTINCT(academystructure_departments.id)) as sid'))->groupBy('academystructure_departments.spec_id')->pluck('name', 'sid')->toArray(); $types = config('exams.types'); $filter_semester_id = 0; if (request('exam_semester')) { $filter_semester_id = request('exam_semester'); } else { $filter_semester_id = semester()->id; } if (request('spec_id')) { $search_spec_id = explode(',', request('spec_id')); $statistics = Exam::with(['subject', 'results' => function ($q) use($search_spec_id) { $q->whereHas('student', function ($query) use($search_spec_id) { $query->whereIn('students.academystructure_department_id', $search_spec_id); }); }])->where('active', 1)->where('semester_id', $filter_semester_id)->where('finish_at', '=<', date("Y/m/d H:i:s")); $subject_students = Subject::select('id')->withCount(['students' => function ($q) use($filter_semester_id, $search_spec_id) { $q->where('semester_id', $filter_semester_id); $q->where('students.state', 'active'); $q->where('student_subjects.state', 'study'); $q->whereIn('students.academystructure_department_id', $search_spec_id); }])->pluck('students_count', 'id')->toArray(); } else { $statistics = Exam::with(['subject', 'results'])->where('active', 1)->where('semester_id', $filter_semester_id)->where('finish_at', '<', date("Y/m/d H:i:s")); $subject_students = Subject::select('id')->withCount(['students' => function ($q) use($filter_semester_id) { $q->where('semester_id', $filter_semester_id); $q->where('students.state', 'active'); $q->where('student_subjects.state', 'study'); }])->pluck('students_count', 'id')->toArray(); } if (request('exam_year_term')) { $statistics->whereIn('subject_id', explode(',', request('exam_year_term'))); } if (request('exam_subject')) { $statistics->where('subject_id', request('exam_subject')); } if (request('exam_type')) { $statistics->where('type', request('exam_type')); } $statistics = $statistics->get(); return view('exams::reports.subjectexam', compact('subjects', 'semesters', 'year_term_options', 'types', 'statistics', 'subject_students', 'spec_id')); }
/** * 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); }
public function print_exam($exam_id, $with_answer = false) { $exam = Exam::with('subject')->findOrFail($exam_id); /*\DB::listen(function ($query) { var_dump($query->sql); // $query->bindings // $query->time });*/ $questions = ExamQuestion::with(['question', 'question.choices', 'question.lesson'])->where('exam_id', $exam_id)->select("exam_questions.*")->join("questionbank_questions AS qbq", "qbq.id", '=', "exam_questions.question_id")->orderByRaw(\DB::raw("FIELD(qbq.type, 'true_false' , 'single_choice' , 'multiple_choice' , 'essay ' )"))->get(); $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetFont('traditionalarabic', '', 18, '', 'false'); $pdf->setRTL('enable', true); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(true); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->startPageGroup(); $pdf->AddPage(); if ($with_answer) { if (user()->can('printwithanswer.exams.exams')) { $pdf->writeHTML(view('exams::exams.print', compact('exam', 'questions'))->render()); } } else { if (user()->can('printwithoutanswer.exams.exams')) { $pdf->writeHTML(view('exams::exams.printwithoutanswer', compact('exam', 'questions'))->render()); } } $filename = $exam->id . '.pdf'; ob_clean(); $pdf->output($filename, 'D'); return redirect()->back()->with('success', trans('exams::exams.download_success')); }
public function destroy($exam_id) { $exam = Exam::with('results')->inCurrentSemester()->findOrFail($exam_id); if (!$exam->results->isEmpty()) { return redirect()->route('teachers.profile.activitiessubject', $subject_id)->with('error', trans('exams::exams.cant_delete_with_results')); } if (!$exam->delete()) { return redirect()->back()->with('error', trans('exams::exams.delete_error')); } return redirect()->route('teachers.profile.activitiessubject')->with('success', trans('exams::exams.delete_success')); }
public function store(Request $request) { $error = 0; if (!$request->hasFile('answer')) { $error = 1; $message = 'لم تقم برفع أي ملف'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } $file = $request->file('answer'); $exam_file = json_decode(file_get_contents($file->getRealPath())); if (empty($exam_file->exam_id)) { $error = 1; $message = 'الملف الذي رفعته غير صحيح'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } if (empty($exam_file->questions) or !is_array($exam_file->questions)) { $error = 1; $message = 'لا توجد إجابات بالاختبار الذي رفعته'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } $exam_id = $exam_file->exam_id; $student_id = $exam_file->student_id; if (!($exam = Exam::with('questions', 'questions.choices')->find($exam_id))) { $error = 1; $message = 'الاختبار الذي تحاول رفع إجابته غير موجود'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } $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 = round($this->activity_points / $exam->questions->count(), 2); } // 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[] = $result_answer; } StudentGrade::where('student_id', $student_id)->where(['ref_key' => 'exam', 'ref_value' => $exam->id])->delete(); $result->answers()->delete(); foreach ($result_answers as $a) { $result->answers()->create($a); } foreach ($exam->questions as $question) { 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)) { $grade += round($grade_per_question, 2); } 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)) { $grade += round($grade_per_question, 2); } break; case 'multiple_choice': $multiple_answer = array_first($answers, function ($key, $answer) use($question) { return $answer->question_id == $question->id; }); if ($multiple_answer) { $all_answers = $multiple_answer ? explode(",", $multiple_answer->answer) : 0; if (count($all_answers) == $question->choices->count()) { $grade += "0.00"; } else { $true_answers = $question->choices->filter(function ($question) { return $question->istrue; }); if ($true_answers) { $true_answers_ids = $true_answers->pluck('id')->toArray(); $intersect = array_intersect($all_answers, $true_answers_ids); $multichoice_grade = count($intersect) * ($grade_per_question / count($true_answers_ids)); $grade += round($multichoice_grade, 2); } } } break; } } if ($exam->type == 'activity' && $grade > $this->activity_points) { $grade = $this->activity_points; } $data = ['student_id' => $student_id, 'value' => $grade, 'ref_value' => $exam->id, 'ref_key' => 'exam', 'subject_id' => $exam->subject_id, 'semester_id' => Semester()->id, 'notes' => 'درجة الأنشطة']; StudentGrade::create($data); $message = 'تم رفع الإجابة بنجاح'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } else { $error = 1; $message = 'لم يتم حفظ الإجابة!! حدث خلل.'; return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); } return response()->json(compact('error', 'message'), 200, [], JSON_NUMERIC_CHECK); }