public function analyseStudentPerformance($studentId)
 {
     $student = User::find($studentId);
     $studentTeachingSection = TeachingSection::where('users_id', '=', $studentId)->first();
     //        dd($studentTeachingSection);
     $studentSection = Section::find($studentTeachingSection->sections_id);
     $studentArray = array();
     $studentArray['name'] = $student->name;
     $studentArray['realname'] = $student->realname;
     $studentArray['grade'] = $studentSection->grade;
     $studentArray['order'] = $studentSection->order;
     //        a table show all exminations the student has token
     //        show the different table of different subjects.
     $allTranscripts = Transcript::where('users_id', '=', $studentId)->get();
     //        dd($allTranscripts);
     $studentExaminations = array();
     $i = 0;
     foreach ($allTranscripts as $transcript) {
         /*对该成绩单的学生的每次考试的语文,数学,英语三科的排名*/
         $studentExaminations[$i]['chineseRanking'] = Transcript::getStudentSubjectRanking($transcript, 1);
         $studentExaminations[$i]['mathRanking'] = Transcript::getStudentSubjectRanking($transcript, 2);
         $studentExaminations[$i]['englishRanking'] = Transcript::getStudentSubjectRanking($transcript, 3);
         $examination = Examination::find($transcript->examinations_id);
         $studentExaminations[$i]['examination_name'] = $examination->name;
         $studentExaminations[$i]['examination_semester'] = $examination->semester;
         $studentExaminations[$i]['examination_date'] = $examination->date;
         $studentExaminations[$i]['total'] = $transcript->total;
         $studentExaminations[$i]['rank'] = $transcript->rank;
         $subjects = Subject::where('transcripts_id', '=', $transcript->id)->get();
         foreach ($subjects as $subject) {
             $subjectType = $subject->type;
             if ($subjectType == 1) {
                 $studentExaminations[$i]['chinese'] = $subject->score;
             } elseif ($subjectType == 2) {
                 $studentExaminations[$i]['math'] = $subject->score;
             } elseif ($subjectType == 3) {
                 $studentExaminations[$i]['english'] = $subject->score;
             } elseif ($subjectType == 4) {
                 $studentExaminations[$i]['physics'] = $subject->score;
             } elseif ($subjectType == 5) {
                 $studentExaminations[$i]['biology'] = $subject->score;
             } elseif ($subjectType == 6) {
                 $studentExaminations[$i]['chemistry'] = $subject->score;
             } elseif ($subjectType == 7) {
                 $studentExaminations[$i]['history'] = $subject->score;
             } elseif ($subjectType == 8) {
                 $studentExaminations[$i]['politics'] = $subject->score;
             } else {
                 $studentExaminations[$i]['geography'] = $subject->score;
             }
         }
         $i++;
     }
     //dd($studentExaminations);
     return view('scores.analyseStudentPerformance', compact('studentArray', 'studentExaminations'));
 }
 public function saveStudentSubject(Request $request)
 {
     $transcriptId = $request->input('transcriptId');
     $transcript = Transcript::find($transcriptId);
     $chineseScore = $request->input('chinese');
     $newSubject1 = new Subject();
     $newSubject1->score = $chineseScore;
     $newSubject1->type = 1;
     $newSubject1->transcripts_id = $transcriptId;
     $newSubject1->save();
     $transcript->total += $chineseScore;
     $transcript->save();
     $mathScore = $request->input('math');
     $newSubject2 = new Subject();
     $newSubject2->score = $mathScore;
     $newSubject2->type = 2;
     $newSubject2->transcripts_id = $transcriptId;
     $newSubject2->save();
     $transcript->total += $mathScore;
     $transcript->save();
     $englishScore = $request->input('english');
     $newSubject3 = new Subject();
     $newSubject3->score = $englishScore;
     $newSubject3->type = 3;
     $newSubject3->transcripts_id = $transcriptId;
     $newSubject3->save();
     $transcript->total += $englishScore;
     $transcript->save();
     //        理科三门
     if ($request->has('physics')) {
         $physicsScore = $request->input('physics');
         $newSubject4 = new Subject();
         $newSubject4->score = $physicsScore;
         $newSubject4->type = 4;
         $newSubject4->transcripts_id = $transcriptId;
         $newSubject4->save();
         $transcript->total += $physicsScore;
         $transcript->save();
     }
     if ($request->has('biology')) {
         $biologyScore = $request->input('biology');
         $newSubject4 = new Subject();
         $newSubject4->score = $biologyScore;
         $newSubject4->type = 5;
         $newSubject4->transcripts_id = $transcriptId;
         $newSubject4->save();
         $transcript->total += $biologyScore;
         $transcript->save();
     }
     if ($request->has('chemistry')) {
         $chemistryScore = $request->input('chemistry');
         $newSubject5 = new Subject();
         $newSubject5->score = $chemistryScore;
         $newSubject5->type = 6;
         $newSubject5->transcripts_id = $transcriptId;
         $newSubject5->save();
         $transcript->total += $chemistryScore;
         $transcript->save();
     }
     //        文科三门
     if ($request->has('history')) {
         $historyScore = $request->input('history');
         $newSubject6 = new Subject();
         $newSubject6->score = $historyScore;
         $newSubject6->type = 7;
         $newSubject6->transcripts_id = $transcriptId;
         $newSubject6->save();
         $transcript->total += $historyScore;
         $transcript->save();
     }
     if ($request->has('politics')) {
         $politicsScore = $request->input('politics');
         $newSubject7 = new Subject();
         $newSubject7->score = $politicsScore;
         $newSubject7->type = 8;
         $newSubject7->transcripts_id = $transcriptId;
         $newSubject7->save();
         $transcript->total += $politicsScore;
         $transcript->save();
     }
     if ($request->has('geography')) {
         $geographyScore = $request->input('geography');
         $newSubject8 = new Subject();
         $newSubject8->score = $geographyScore;
         $newSubject8->type = 9;
         $newSubject8->transcripts_id = $transcriptId;
         $newSubject8->save();
         $transcript->total += $geographyScore;
         $transcript->save();
     }
     //        班级排名算法
     $noRanking = 1;
     $previousTranscripts = Transcript::where('examinations_id', '=', $transcript->examinations_id)->get();
     foreach ($previousTranscripts as $previousTranscript) {
         //            第一步: 判断该transcription的主人是否和本学生属于同一个班级
         $previousStudentSectionId = TeachingSection::where('users_id', '=', $previousTranscript->users_id)->select('sections_id')->get();
         $currentStudentSectionId = TeachingSection::where('users_id', '=', $transcript->users_id)->select('sections_id')->get();
         if ($previousStudentSectionId == $currentStudentSectionId) {
             //第二步: 如果他们同一个班级, 并且总分比本学生的总分高的话, 就往后排一名
             if ($previousTranscript->total > $transcript->total) {
                 $noRanking++;
             }
         }
     }
     $transcript->rank = $noRanking;
     $transcript->save();
     //        end班级排名算法
     //        将两个变量从session中取出, 并得到, 然后将这两个变量传送到一个controller的action中, 以便能够返回到之前这次新建考试的所教班级的所有学生页面中
     /*        if (Session::has('newExamination'))
             {
                 $newExamination = Session::get('newExamination');
             }
             if (Session::has('allStudentsAllSections'))
             {
                 $allStudentsAllSections = Session::get('allStudentsAllSections');
             }*/
     return redirect()->action('ExaminationController@returnAllSectionsEachTestPage');
     //        return redirect()->route('profile', [1]);
     //        return Redirect::back();
     //        flash()->overlay('老师你好,已经成功记录该班级的学生成绩', '成功');
     //        return view('dashboard.dashboard');
     //        return redirect('/createNewTest');
     //        return Redirect::intended('/');
     //        return Redirect::action('ExaminationController@createNewTest');
     // YourController@store
     /*        if (Session::has('backUrl')) {
                 Session::keep('backUrl');
             }
     
             return ($url = Session::get('backUrl'))
                 ? Redirect::to($url)
                 : Redirect::view('/dashboard');
     //            : Redirect::route('any.named.route');*/
 }
 public function getStudentsAllInfoEachSection($examinations_id, $sections_id)
 {
     //        get all students in the section
     $studentsIds = User::getStudentBySection($sections_id);
     //
     //        //get all above students transcripts in the examinations.
     //        $transcripts = DB::table('transcripts')->where('examination_id', '=', $examinations_id)
     //            ->whereIn('users_id', '=', $studentsId)->get();
     //        DB::table('transcripts')
     //            ->join('users', 'transcripts.users_id', '=', 'users.id')
     //            ->join('examinations', 'transcripts.examinations_id', '=', 'examinations.id')
     //            ->get();
     //        $students = DB::table('users')->whereIn('id', '=', $studentsIds)->get();
     $section = Section::find($sections_id);
     $examination = Examination::find($examinations_id);
     $examinationArray = array();
     $examinationArray['name'] = $examination->name;
     if ($examination->semester == 1) {
         $examinationArray['semester'] = '第一学期';
     } elseif ($examination->semester == 2) {
         $examinationArray['semester'] = '第二学期';
     } elseif ($examination->semester == 3) {
         $examinationArray['semester'] = '小学期';
     } else {
         $examinationArray['semester'] = '其他';
     }
     if ($examination->type == 1) {
         $examinationArray['type'] = '期末考试';
     } elseif ($examination->type == 2) {
         $examinationArray['type'] = '期中考试';
     } elseif ($examination->type == 3) {
         $examinationArray['type'] = '月考';
     } else {
         $examinationArray['type'] = '普通考试';
     }
     $examinationArray['date'] = $examination->date;
     $arrayStudentScores = array();
     $i = 0;
     //        这个班级的最大总分
     $maxTotalScore = Transcript::getMaxTotalScores($studentsIds);
     //        dd($maxTotalScore);
     foreach ($studentsIds as $studentsId) {
         $student = User::findOrFail($studentsId);
         $arrayStudentScores[$i]['student_id'] = $student->id;
         $arrayStudentScores[$i]['student_name'] = $student->realname;
         $arrayStudentScores[$i]['student_realName'] = $student->realname;
         $transcript = Transcript::where('users_id', '=', $student->id)->where('examinations_id', '=', $examinations_id)->first();
         //            dd($transcript);
         $arrayStudentScores[$i]['total'] = $transcript->total;
         $subjects = Subject::where('transcripts_id', '=', $transcript->id)->get();
         foreach ($subjects as $subject) {
             $subjectType = $subject->type;
             if ($subjectType == 1) {
                 $arrayStudentScores[$i]['chinese'] = $subject->score;
             } elseif ($subjectType == 2) {
                 $arrayStudentScores[$i]['math'] = $subject->score;
             } elseif ($subjectType == 3) {
                 $arrayStudentScores[$i]['english'] = $subject->score;
             } elseif ($subjectType == 4) {
                 $arrayStudentScores[$i]['physics'] = $subject->score;
             } elseif ($subjectType == 5) {
                 $arrayStudentScores[$i]['biology'] = $subject->score;
             } elseif ($subjectType == 6) {
                 $arrayStudentScores[$i]['chemistry'] = $subject->score;
             } elseif ($subjectType == 7) {
                 $arrayStudentScores[$i]['history'] = $subject->score;
             } elseif ($subjectType == 8) {
                 $arrayStudentScores[$i]['politics'] = $subject->score;
             } else {
                 $arrayStudentScores[$i]['geography'] = $subject->score;
             }
         }
         //            得到与最高分的分差
         $arrayStudentScores[$i]['pointDifference'] = $maxTotalScore - $transcript->total;
         $transcript = Transcript::where('users_id', '=', $studentsId)->where('examinations_id', '=', $examinations_id)->first();
         $arrayStudentScores[$i]['ranking'] = $transcript->rank;
         $i++;
     }
     //        dd($arrayStudentScores);
     //        使用ranking进行排序
     /*
      * 下面是对该班成绩的分析*/
     //        该班级的该次考试的总和各科平均分
     $examAverages = array();
     $examAverages['totalAve'] = 0;
     $examAverages['chineseAve'] = 0;
     $examAverages['mathAve'] = 0;
     $examAverages['englishAve'] = 0;
     $examAverages['physicsAve'] = 0;
     $examAverages['biologyAve'] = 0;
     $examAverages['chemistryAve'] = 0;
     $examAverages['historyAve'] = 0;
     $examAverages['politicsAve'] = 0;
     $examAverages['geographyAve'] = 0;
     $count = 0;
     //        该次考试各科成绩中第一名,最后一名,平均分,中位数分析比较
     $bestStudent = $arrayStudentScores[0];
     $worstStudent = $arrayStudentScores[0];
     $middleStudent = $arrayStudentScores[0];
     $length = sizeof($arrayStudentScores);
     $halfLength = round($length / 2);
     //        dd($halfLength);
     foreach ($arrayStudentScores as $studentExamScores) {
         $examAverages['totalAve'] += $studentExamScores['total'];
         $examAverages['chineseAve'] += $studentExamScores['chinese'];
         $examAverages['mathAve'] += $studentExamScores['math'];
         $examAverages['englishAve'] += $studentExamScores['english'];
         $examAverages['physicsAve'] += $studentExamScores['physics'];
         $examAverages['biologyAve'] += $studentExamScores['biology'];
         $examAverages['chemistryAve'] += $studentExamScores['chemistry'];
         $examAverages['historyAve'] += $studentExamScores['history'];
         $examAverages['politicsAve'] += $studentExamScores['politics'];
         $examAverages['geographyAve'] += $studentExamScores['geography'];
         $count++;
         //the best performance student
         if ($studentExamScores['ranking'] < $bestStudent['ranking']) {
             $bestStudent = $studentExamScores;
         }
         //        the worst performance student
         if ($studentExamScores['ranking'] > $worstStudent['ranking']) {
             $worstStudent = $studentExamScores;
         }
         if ($studentExamScores['ranking'] == $halfLength) {
             $middleStudent = $studentExamScores;
         }
     }
     $examAverages['totalAve'] /= $count;
     $examAverages['chineseAve'] /= $count;
     $examAverages['mathAve'] /= $count;
     $examAverages['englishAve'] /= $count;
     $examAverages['physicsAve'] /= $count;
     $examAverages['biologyAve'] /= $count;
     $examAverages['chemistryAve'] /= $count;
     $examAverages['historyAve'] /= $count;
     $examAverages['politicsAve'] /= $count;
     $examAverages['geographyAve'] /= $count;
     //        end该班级的该次考试的总和各科平均分
     //        该次考试各科成绩中第一名,最后一名,平均分,中位数分析比较
     //       the best student
     //        the worst student
     //        the middle student
     //        end该次考试各科成绩中第一名,最后一名,平均分,中位数分析比较
     return view('scores.analyseSectionPerformance', compact('examinationArray', 'arrayStudentScores', 'section', 'examAverages', 'bestStudent', 'worstStudent', 'middleStudent'));
 }
 public function forecastStudentFuturePerformance($studentId)
 {
     $student = User::find($studentId);
     //        这部分工作和CheckStudentController的中 analyseStudentPerformance 方法是一样, code取自该方法
     $studentTeachingSection = TeachingSection::where('users_id', '=', $studentId)->first();
     //        dd($studentTeachingSection);
     $studentSection = Section::find($studentTeachingSection->sections_id);
     $studentArray = array();
     $studentArray['name'] = $student->name;
     $studentArray['realname'] = $student->realname;
     $studentArray['grade'] = $studentSection->grade;
     $studentArray['order'] = $studentSection->order;
     $allTranscripts = Transcript::where('users_id', '=', $studentId)->get();
     //      该学生的全部考试考试的所有各种平均分
     $studentAllPreExamsAve = array();
     $studentAllPreExamsAve['totalAve'] = 0;
     $studentAllPreExamsAve['rankingAve'] = 0;
     $studentAllPreExamsAve['chineseAve'] = 0;
     $studentAllPreExamsAve['mathAve'] = 0;
     $studentAllPreExamsAve['englishAve'] = 0;
     $studentAllPreExamsAve['physicsAve'] = 0;
     $studentAllPreExamsAve['biologyAve'] = 0;
     $studentAllPreExamsAve['chemistryAve'] = 0;
     $studentAllPreExamsAve['historyAve'] = 0;
     $studentAllPreExamsAve['geographyAve'] = 0;
     $studentAllPreExamsAve['politicsAve'] = 0;
     $count = 0;
     $previousTotalAndRankingArray = [];
     foreach ($allTranscripts as $transcript) {
         $examination = Examination::find($transcript->examinations_id);
         $tampTotalAndRankingArray = [floatval($transcript->total), floatval($transcript->rank), 1];
         $previousTotalAndRankingArray[$count] = $tampTotalAndRankingArray;
         /*            $previousTotalAndRankingArray[$count]['total'] = $transcript->total;
                     $previousTotalAndRankingArray[$count]['rank'] = $transcript->rank;
                     $previousTotalAndRankingArray[$count]['ballSize'] = 10;*/
         $studentAllPreExamsAve['totalAve'] += $transcript->total;
         $studentAllPreExamsAve['rankingAve'] += $transcript->rank;
         $subjects = Subject::where('transcripts_id', '=', $transcript->id)->get();
         foreach ($subjects as $subject) {
             $subjectType = $subject->type;
             if ($subjectType == 1) {
                 $studentAllPreExamsAve['chineseAve'] += $subject->score;
             } elseif ($subjectType == 2) {
                 $studentAllPreExamsAve['mathAve'] += $subject->score;
             } elseif ($subjectType == 3) {
                 $studentAllPreExamsAve['englishAve'] += $subject->score;
             } elseif ($subjectType == 4) {
                 $studentAllPreExamsAve['physicsAve'] += $subject->score;
             } elseif ($subjectType == 5) {
                 $studentAllPreExamsAve['biologyAve'] += $subject->score;
             } elseif ($subjectType == 6) {
                 $studentAllPreExamsAve['chemistryAve'] += $subject->score;
             } elseif ($subjectType == 7) {
                 $studentAllPreExamsAve['historyAve'] += $subject->score;
             } elseif ($subjectType == 8) {
                 $studentAllPreExamsAve['politicsAve'] += $subject->score;
             } else {
                 $studentAllPreExamsAve['geographyAve'] += $subject->score;
             }
         }
         $count++;
     }
     $studentAllPreExamsAve['totalAve'] /= $count;
     $studentAllPreExamsAve['rankingAve'] = round($studentAllPreExamsAve['rankingAve'] / $count);
     $studentAllPreExamsAve['chineseAve'] /= $count;
     $studentAllPreExamsAve['mathAve'] /= $count;
     $studentAllPreExamsAve['englishAve'] /= $count;
     $studentAllPreExamsAve['physicsAve'] /= $count;
     $studentAllPreExamsAve['biologyAve'] /= $count;
     $studentAllPreExamsAve['chemistryAve'] /= $count;
     $studentAllPreExamsAve['historyAve'] /= $count;
     $studentAllPreExamsAve['politicsAve'] /= $count;
     $studentAllPreExamsAve['geographyAve'] /= $count;
     $studentAllPreExamsAveData = [$studentAllPreExamsAve['chineseAve'], $studentAllPreExamsAve['mathAve'], $studentAllPreExamsAve['englishAve'], $studentAllPreExamsAve['physicsAve'], $studentAllPreExamsAve['biologyAve'], $studentAllPreExamsAve['chemistryAve'], $studentAllPreExamsAve['historyAve'], $studentAllPreExamsAve['politicsAve'], $studentAllPreExamsAve['geographyAve']];
     //        end该学生的全部考试考试的所有各种平均分
     //        对下次考试的预测
     $forecastNextExam = array();
     //        调用测试算法,对下次考试预测
     $forecastNextExam['total'] = 862;
     $forecastNextExam['ranking'] = 1;
     $forecastNextExam['chinese'] = 108;
     $forecastNextExam['math'] = 117;
     $forecastNextExam['english'] = 77;
     $forecastNextExam['physics'] = 74;
     $forecastNextExam['biology'] = 80;
     $forecastNextExam['chemistry'] = 78;
     $forecastNextExam['history'] = 71;
     $forecastNextExam['politics'] = 82;
     $forecastNextExam['geography'] = 78;
     $forecastNextExamData = [$forecastNextExam['chinese'], $forecastNextExam['math'], $forecastNextExam['english'], $forecastNextExam['physics'], $forecastNextExam['biology'], $forecastNextExam['chemistry'], $forecastNextExam['history'], $forecastNextExam['politics'], $forecastNextExam['geography']];
     //        dd($studentArray);
     //        dd($studentAllPreExamsAve);
     //        dd($forecastNextExam);
     //        对之前所有考试的总分和排名, 平均排名和 未来排名
     $averageTotalAndRankingArray = [[$studentAllPreExamsAve['totalAve'], $studentAllPreExamsAve['rankingAve'], 1]];
     $forecastTotalAndRankingArray = [[$forecastNextExam['total'], $forecastNextExam['ranking'], 1]];
     //        dd($previousTotalAndRankingArray);
     //        dd($averageTotalAndRankingArray);
     //        dd($forecastTotalAndRankingArray);
     return view('forecast.forecastStudentsFuturePerformance', compact('studentArray', 'studentAllPreExamsAveData', 'forecastNextExamData', 'previousTotalAndRankingArray', 'averageTotalAndRankingArray', 'forecastTotalAndRankingArray'));
 }