예제 #1
0
 /**
  * compute averages and positions for students in class
  * @param  int $class_id class id
  * @return route    redirect to route
  */
 public function compute_result($class_id)
 {
     //get all students in this class
     $students = Student::where('class_id', $class_id)->get();
     //get the number of subjects for this class
     $number_of_subjects = count(studentClass::find($class_id)->subjects);
     $result_table = 'class_results_' . \Session::get('current_session') . '_' . \Session::get('current_term');
     $positions_table = 'class_positions_' . \Session::get('current_session') . '_' . \Session::get('current_term');
     //array to hold all students total scores
     $results = [];
     foreach ($students as $student) {
         $total_score = 0;
         $offered_subjects = Helper::get_offered_subjects($class_id, $student->id);
         foreach ($offered_subjects as $subject_id) {
             $subject = \DB::table($result_table)->where(['class_id' => $class_id, 'student_id' => $student->id, 'subject_id' => $subject_id])->first();
             $total_score += $subject->subject_total;
         }
         $results[$student->id] = $total_score;
     }
     $positions = Helper::calculate_position($results);
     foreach ($positions as $student_id => $position) {
         $total_score = 0;
         $offered_subjects = Helper::get_offered_subjects($class_id, $student_id);
         foreach ($offered_subjects as $subject_id) {
             $subject = \DB::table($result_table)->where(['class_id' => $class_id, 'student_id' => $student_id, 'subject_id' => $subject_id])->first();
             $total_score += $subject->subject_total;
         }
         $average = floatVal($total_score) / floatVal(count($offered_subjects));
         try {
             \DB::table($positions_table)->where(['class_id' => $class_id, 'student_id' => $student_id])->update(['total' => $total_score, 'average' => $average, 'position' => $position, 'status_id' => 6]);
         } catch (\Illuminate\Database\QueryException $e) {
             $errorCode = $e->errorInfo[1];
             // if($errorCode == 1050){
             //     session()->flash('flash_message', 'Result table for chosen session and term already exists.');
             //     return \Redirect::back()->withInput();
             // }
         }
     }
     return redirect()->route('results.result_sheet', array($class_id));
 }