public function index($student_id, Request $request) { if (!$student_id) { return redirect()->rotue('students.students.index'); } $grades = StudentGrade::where('student_grades.student_id', $student_id)->select('student_grades.*', 'exams.id as exam_id', 'exams.name as exam_name')->with('semester', 'subject')->leftJoin("exams", function ($j) { $j->on('exams.id', '=', 'student_grades.ref_value')->where('student_grades.ref_key', '=', 'exam'); })->groupBy('student_grades.id')->get(); $student = Student::findOrFail($student_id); if ($request->has('partial')) { return view('students::grades._list', compact('grades', 'student')); } return view('students::grades.index', compact('grades', 'student')); }
/** * Display a listing of the resource.student_subjects * @return Response */ public function index(Request $request) { $statistics = StudentGrade::where('student_grades.semester_id', semester()->id)->select('subject_id', 'student_id', DB::raw('SUM(value) as sgrade'))->with(['subject', 'student' => function ($query) { $query->whereHas('subjects', function ($q) { $q->where('semester_id', semester()->id); $q->where('state', 'study'); }); }])->groupBy('student_id', 'subject_id')->orderBy('student_id'); if (request('mark_from')) { $statistics->having('sgrade', '>=', request('mark_from')); } if (request('mark_to')) { $statistics->having('sgrade', '<', request('mark_to')); } $statistics = $statistics->get()->groupBy('student_id')->get(); // dd($statistics); return view('exams::reports.index', compact('statistics')); }
/** * 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 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); }
/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); // echo 'in'; if (StudentSubject::count()) { // return; } // $user_subjects = DB::connection('old') // ->table('user_subject') // // ->where('us_userid', 148) // ->get(); // echo DB::connection('old')->table('user_subject')->count(); // exit; // $this->call("OthersTableSeeder"); DB::statement('SET FOREIGN_KEY_CHECKS=0;'); StudentSubject::where('student_id', '!=', 10000)->delete(); // StudentSubject::truncate(); StudentGrade::where('student_id', '!=', 10000)->delete(); // StudentGrade::truncate(); $all_user_subjects = DB::connection('old')->table('user_subject')->leftJoin('users', 'users.user_id', '=', 'user_subject.us_userid')->select('users.user_academyid', 'us_state', 'us_subid', 'us_userid', 'us_semid', 'us_result')->get(); // echo 'got it'; $new_departments = $new_department = Department::select('ad.id', 'ay.id AS ay_id', 'as.code')->join('academystructure_terms as at', 'at.id', '=', 'ad.term_id')->join('academystructure_years as ay', function ($j) { $j->on('at.year_id', '=', 'ay.id'); // ->where('ay.id', '=',$academy['academy_year']); })->join('academystructure_specialties as as', function ($j) { $j->on('as.id', '=', 'ad.spec_id'); // ->where('as.code', '=', $dep_sepcialty_code); })->from('academystructure_departments as ad')->get()->toArray(); $all_old_academies = $academy = DB::connection('old')->table('sub_academy')->select('academy_department', 'academy_year', 'academy_term')->select('academy.*', 'sub_academy.sub_acad_subid')->join('academy', 'sub_acad_academyid', '=', 'academy_id')->get(); $all_old_academies = array_map(function ($a) { return (array) $a; }, $all_old_academies); $student_subjects = []; $student_grades = []; // var_dump(count($all_user_subjects)); foreach ($all_user_subjects as $user_subject) { $student_subject = []; $student_grade = []; $state = $user_subject->us_state == 'sucess' ? 'success' : $user_subject->us_state; $state = $state == 'edu' ? 'study' : $state; $state = $state == 'equ' ? 'equal' : $state; if ($user_subject->us_semid == 9) { $academy = array_filter($all_old_academies, function ($a) use($user_subject) { return $user_subject->user_academyid == $a['academy_id']; }); } else { $academy = array_filter($all_old_academies, function ($a) use($user_subject) { return $user_subject->us_subid == $a['sub_acad_subid']; }); } $academy = current($academy); if (empty($academy)) { continue; } $dep_sepcialty_code = 'D'; if ($academy['academy_department'] == 'اصول الفقة') { $dep_sepcialty_code = 'O'; } elseif ($academy['academy_department'] == 'الدرسات الاسلامية') { $dep_sepcialty_code = 'I'; } $new_departments_filtered = array_filter($new_departments, function ($d) use($academy, $dep_sepcialty_code) { $exist = $d['ay_id'] == $academy['academy_year'] && $d['code'] == $dep_sepcialty_code; if ($exist) { // var_dump($d); } return $exist; }); if ($academy['academy_term'] == 2) { $new_department = array_pop($new_departments_filtered); } else { $new_department = array_shift($new_departments_filtered); } $student_subject['student_id'] = $user_subject->us_userid; $student_subject['subject_id'] = $user_subject->us_subid; $student_subject['semester_id'] = $user_subject->us_semid; $student_subject['state'] = $state; $student_subject['academystructure_department_id'] = $new_department['id']; $student_grade['student_id'] = $user_subject->us_userid; $student_grade['subject_id'] = $user_subject->us_subid; $student_grade['semester_id'] = $user_subject->us_semid; $student_grade['notes'] = "تم جلبها من النظام القديم"; $student_grade['value'] = $user_subject->us_result; $student_grade['created_at'] = new DateTime(); $student_grade['updated_at'] = new DateTime(); if ($user_subject->us_result >= 0) { $student_grades[] = $student_grade; } $student_subjects[] = $student_subject; } foreach (array_chunk($student_subjects, 1000) as $chunk) { DB::table('student_subjects')->insert($chunk); } foreach (array_chunk($student_grades, 1000) as $chunk) { DB::table('student_grades')->insert($chunk); } }
public function storeresult(Request $request) { $value = $request->value; $student_id = $request->student_id; $subject_id = $request->subject_id; $exam_id = $request->exam_id; $question_id = $request->question_id; $exam_type = $request->exam_type; StudentGrade::where('student_id', $student_id)->where(['ref_key' => 'examessay', 'ref_value' => $exam_id])->delete(); DB::table('exam_result_answers')->where('exam_result_id', '=', $exam_id)->where('question_id', '=', $question_id)->delete(); $data = ['student_id' => $student_id, 'value' => $value, 'ref_value' => $exam_id, 'ref_key' => 'examessay', 'subject_id' => $subject_id, 'semester_id' => Semester()->id, 'notes' => 'درجة المقالى']; StudentGrade::create($data); DB::table('exam_result_answers')->insert(['exam_result_id' => $exam_id, 'question_id' => $question_id, 'degree' => $value, 'answer' => 'essay']); return redirect()->route('teachers.profile.students', [$subject_id, $exam_type])->with('success', 'تم حفظ الدرجه بنجاح'); }