public function createNewTest(Request $request)
 {
     $user = Auth::User();
     //get all information of a new Test.
     //返回一个成功的信息回去
     $name = $request->input('name');
     $semester = $request->input('semester');
     $type = $request->input('type');
     $date = $request->input('date');
     $schools_id = 1;
     $newExamination = new Examination();
     $newExamination->name = $name;
     $newExamination->semester = $semester;
     $newExamination->type = $type;
     $newExamination->date = $date;
     $newExamination->description = $request->input('description');
     $newExamination->schools_id = $schools_id;
     $newExamination->save();
     //将这个新的考试信息存储到session中
     //     $request->session()->put('newExamination', $newExamination);
     $checkedIds = $request->input('add');
     $sections = DB::table('sections')->whereIn('id', $checkedIds)->get();
     $allStudentsAllSections = [];
     $i = 0;
     //已经创建了一个新的examination,也已经知道有多少个班级会被记录,因此可以为每个班级的每个学生创建此次考试的transcription.
     foreach ($sections as $section) {
         $allStudentsAllSections[$i]['sectionGrade'] = $section->grade;
         $allStudentsAllSections[$i]['sectionOrder'] = $section->order;
         $allStudentIdsEachSection = User::getStudentBySection($section->id);
         $allStudentsEachSection = DB::table('users')->whereIn('id', $allStudentIdsEachSection)->get();
         $allStudentsAllSections[$i]['students'] = $allStudentsEachSection;
         $i++;
     }
     //在examinationsInfos表中将sectionId, examinationsId, 和当前users_id生成一新的数据.
     foreach ($sections as $section) {
         $examinationsInfos = new Examinationsinfo();
         $examinationsInfos->users_id = $user->id;
         $examinationsInfos->examinations_id = $newExamination->id;
         $examinationsInfos->sections_id = $section->id;
         $examinationsInfos->save();
     }
     //        Session::put('url.intended', URL::previous());
     //        Session::put('url.intended', URL::previous());  // using the Facade
     //        session()->put('url.intended', URL::previous()); // using the L5 helper
     //        dd($allStudentsAllSections);
     /*Session::flash('backUrl', Request::fullUrl());*/
     //        在这里将"sections","allStudentsAllSections","newExamination"都放到session中,
     //以便在教师填写填写完学生的考试成绩时, 在取出, 返回到allSectionsEachTest.blade.php页面
     Session::put('newExamination', $newExamination);
     Session::put('allStudentsAllSections', $allStudentsAllSections);
     //        $data = Session::all();
     //        dd($data);
     return view('scores.allSectionsEachTest', compact('user', 'sections', 'allStudentsAllSections', 'newExamination'));
     /*        foreach($checkedIds as $checkedId){
     
                 $teachingSections = TeachingSection::where('sections_id', '=', $checkedId)->get();
     
                 $studentsIds = DB::table('teaching_sections')->where('sections_id', '=', $checkedId)->lists('users_id');
                 $students = DB::table('users')->where('role', '=', '1')->whereIn('id', $studentsIds)->get();
                 $section = Section::find($checkedId);
     
                 return view('scores.logStudentsTestResult', compact('students', 'section', 'newExamination'));*/
     //            foreach ($teachingSections as $teachingSection) {
     ////                这里得到的是一个collection of users
     //                $students = User::where('id', '=', $teachingSection->users_id)
     //                    ->where('role', '=', '1')->get();
     //                $section = Section::find($teachingSection->sections_id);
     //
     ////            return view('scores.logStudentsTestResult', compact('students', 'section', 'newExamination'));
     //
     //            }
 }
 public function checkSectionsScores()
 {
     $user = Auth::User();
     //        find all section which the teacher records
     $allExaminations = Examinationsinfo::where('users_id', '=', $user->id)->get();
     //        find all section in an examination
     /*        $allExaminationsIds = DB::table('examinationsinfos')
               ->where('users_id', '=', $user->id)->get();*/
     $allExaminationsInfosUnique = Examinationsinfo::where('users_id', '=', $user->id)->get()->unique('examinations_id');
     //        dd($allExaminationsIds);
     //        $allExaminationsIdUnique = $allExaminationsIds->unique('examinations_id');
     //        foreach($allSectionsEachExaminations as $allSectionsEachExamnation){
     //            $allSectionsEachExamination =
     //
     //        }
     //        $allExaminationsAllSections = [];
     //        dd($allExaminationsIdUnique);
     $allExaminationsAllSectionsArray = array();
     $i = 0;
     foreach ($allExaminationsInfosUnique as $examinationInfoUnique) {
         $examination = Examination::find($examinationInfoUnique->examinations_id);
         $allExaminationsAllSectionsArray[$i]['examName'] = $examination->name;
         if ($examination->semester == 1) {
             $allExaminationsAllSectionsArray[$i]['examSemester'] = '第一学期';
         } elseif ($examination->semester == 2) {
             $allExaminationsAllSectionsArray[$i]['examSemester'] = '第二学期';
         } elseif ($examination->semester == 3) {
             $allExaminationsAllSectionsArray[$i]['examSemester'] = '小学期';
         } else {
             $allExaminationsAllSectionsArray[$i]['examSemester'] = '其他';
         }
         if ($examination->type == 1) {
             $allExaminationsAllSectionsArray[$i]['examType'] = '期末考试';
         } elseif ($examination->type == 2) {
             $allExaminationsAllSectionsArray[$i]['examType'] = '期中考试';
         } elseif ($examination->type == 3) {
             $allExaminationsAllSectionsArray[$i]['examType'] = '月考';
         } else {
             $allExaminationsAllSectionsArray[$i]['examType'] = '普通考试';
         }
         //            $allExaminationsAllSectionsArray[$i]['examSemester'] =$examination->semester;
         $allExaminationsAllSectionsArray[$i]['examDate'] = $examination->date;
         //            $allExaminationsAllSectionsArray[$i]['examType'] =$examination->type;
         $allExaminationsAllSectionsArray[$i]['examId'] = $examination->id;
         $allSectionsIdsThisExamination = DB::table('examinationsinfos')->where('examinations_id', '=', $examinationInfoUnique->examinations_id)->lists('sections_id');
         $allSectionsThisExamination = DB::table('sections')->whereIn('id', $allSectionsIdsThisExamination)->get();
         //            dd($allSectionsThisExamination);
         $j = 0;
         $allSections = array();
         foreach ($allSectionsThisExamination as $sectionThisExamination) {
             $allSections[$j]['sectionTotal'] = $sectionThisExamination->total;
             $allSections[$j]['sectionGrade'] = $sectionThisExamination->grade;
             $allSections[$j]['sectionOrder'] = $sectionThisExamination->order;
             $allSections[$j]['sectionId'] = $sectionThisExamination->id;
             /*                $allExaminationsAllSectionsArray[$i][$j]['sectionGrade']=$sectionThisExamination->grade;
                             $allExaminationsAllSectionsArray[$i][$j]['sectionOrder']=$sectionThisExamination->order;*/
             /* $allExaminationsAllSectionsArray[$i][$j]= $sectionThisExamination->toArray();*/
             $j++;
         }
         $allExaminationsAllSectionsArray[$i]['sectionsThisExamiantion'] = $allSections;
         $i++;
     }
     //        dd($allExaminationsAllSectionsArray);
     return view('scores.checkSectionsScores', compact('allExaminationsAllSectionsArray'));
 }
 public function forecastSectionFuturePerformance($sectionId)
 {
     //       get all examinations info about this classes
     $section = Section::find($sectionId);
     //        得到该班级的最近3场考试的平均数据
     $allExamsIdsThisSection = Examinationsinfo::where('Sections_id', '=', $sectionId)->lists('examinations_id');
     $allExamsThisSection = Examination::whereIn('id', $allExamsIdsThisSection)->get();
     //        得到该班级的所有学生
     $allStudensIdsThisSection = User::getStudentBySection($sectionId);
     $allExamsAveData = array();
     $examCount = 0;
     foreach ($allExamsThisSection as $exam) {
         //            i用于标记某场考试该班级有多少学生参加了此次考试
         $i = 0;
         //            找到所有的transcripts, 具备两个要求, 第一个examinations_id 和sections_id
         $allTranscriptsThisSectionEachExam = DB::table('transcripts')->where('examinations_id', '=', $exam->id)->whereIn('users_id', $allStudensIdsThisSection)->get();
         $allExamsAveData[$examCount]['examName'] = $exam->name;
         $allExamsAveData[$examCount]['examDate'] = $exam->date;
         //            开始时为该班级的每次考试的语文平均成绩为0
         $allExamsAveData[$examCount]['chineseSectionAve'] = 0;
         $allExamsAveData[$examCount]['mathSectionAve'] = 0;
         $allExamsAveData[$examCount]['englishSectionAve'] = 0;
         $allExamsAveData[$examCount]['physicsSectionAve'] = 0;
         $allExamsAveData[$examCount]['biologySectionAve'] = 0;
         $allExamsAveData[$examCount]['chemistrySectionAve'] = 0;
         $allExamsAveData[$examCount]['historySectionAve'] = 0;
         $allExamsAveData[$examCount]['politicsSectionAve'] = 0;
         $allExamsAveData[$examCount]['geographySectionAve'] = 0;
         $allExamsAveData[$examCount]['totalSectionAve'] = 0;
         foreach ($allTranscriptsThisSectionEachExam as $transcriptThisSectionEachExam) {
             //                find out all subjects of this student in this examination
             $subjects = Subject::where('transcripts_id', '=', $transcriptThisSectionEachExam->id)->get();
             $allExamsAveData[$examCount]['totalSectionAve'] += $transcriptThisSectionEachExam->total;
             foreach ($subjects as $subject) {
                 $subjectType = $subject->type;
                 if ($subjectType == 1) {
                     $allExamsAveData[$examCount]['chineseSectionAve'] += $subject->score;
                 } elseif ($subjectType == 2) {
                     $allExamsAveData[$examCount]['mathSectionAve'] += $subject->score;
                 } elseif ($subjectType == 3) {
                     $allExamsAveData[$examCount]['englishSectionAve'] += $subject->score;
                 } elseif ($subjectType == 4) {
                     $allExamsAveData[$examCount]['physicsSectionAve'] += $subject->score;
                 } elseif ($subjectType == 5) {
                     $allExamsAveData[$examCount]['biologySectionAve'] += $subject->score;
                 } elseif ($subjectType == 6) {
                     $allExamsAveData[$examCount]['chemistrySectionAve'] += $subject->score;
                 } elseif ($subjectType == 7) {
                     $allExamsAveData[$examCount]['historySectionAve'] += $subject->score;
                 } elseif ($subjectType == 8) {
                     $allExamsAveData[$examCount]['politicsSectionAve'] += $subject->score;
                 } else {
                     $allExamsAveData[$examCount]['geographySectionAve'] += $subject->score;
                 }
             }
             //                一张transcript 意味这一个学生参加了这次考试
             $i++;
         }
         $allExamsAveData[$examCount]['chineseSectionAve'] /= $i;
         $allExamsAveData[$examCount]['mathSectionAve'] /= $i;
         $allExamsAveData[$examCount]['englishSectionAve'] /= $i;
         $allExamsAveData[$examCount]['physicsSectionAve'] /= $i;
         $allExamsAveData[$examCount]['biologySectionAve'] /= $i;
         $allExamsAveData[$examCount]['chemistrySectionAve'] /= $i;
         $allExamsAveData[$examCount]['historySectionAve'] /= $i;
         $allExamsAveData[$examCount]['politicsSectionAve'] /= $i;
         $allExamsAveData[$examCount]['geographySectionAve'] /= $i;
         $allExamsAveData[$examCount]['totalSectionAve'] /= $i;
         $examCount++;
     }
     //        得到所有考试的所有学生的每科平均分.
     $wholeAveData = array();
     $wholeAveData['wholeChineseAve'] = 0;
     $wholeAveData['wholeMathAve'] = 0;
     $wholeAveData['wholeEnglishAve'] = 0;
     $wholeAveData['wholePhysicsAve'] = 0;
     $wholeAveData['wholeBiologyAve'] = 0;
     $wholeAveData['wholeChemistryAve'] = 0;
     $wholeAveData['wholeHistoryAve'] = 0;
     $wholeAveData['wholePoliticsAve'] = 0;
     $wholeAveData['wholeGeographyAve'] = 0;
     $wholeAveData['wholeTotalAve'] = 0;
     foreach ($allExamsAveData as $eachExamAveData) {
         $wholeAveData['wholeChineseAve'] += $eachExamAveData['chineseSectionAve'];
         $wholeAveData['wholeMathAve'] += $eachExamAveData['mathSectionAve'];
         $wholeAveData['wholeEnglishAve'] += $eachExamAveData['englishSectionAve'];
         $wholeAveData['wholePhysicsAve'] += $eachExamAveData['physicsSectionAve'];
         $wholeAveData['wholeBiologyAve'] += $eachExamAveData['biologySectionAve'];
         $wholeAveData['wholeChemistryAve'] += $eachExamAveData['chemistrySectionAve'];
         $wholeAveData['wholeHistoryAve'] += $eachExamAveData['historySectionAve'];
         $wholeAveData['wholePoliticsAve'] += $eachExamAveData['politicsSectionAve'];
         $wholeAveData['wholeGeographyAve'] += $eachExamAveData['geographySectionAve'];
         $wholeAveData['wholeTotalAve'] += $eachExamAveData['totalSectionAve'];
     }
     $wholeAveData['wholeChineseAve'] /= $examCount;
     $wholeAveData['wholeMathAve'] /= $examCount;
     $wholeAveData['wholeEnglishAve'] /= $examCount;
     $wholeAveData['wholePhysicsAve'] /= $examCount;
     $wholeAveData['wholeBiologyAve'] /= $examCount;
     $wholeAveData['wholeChemistryAve'] /= $examCount;
     $wholeAveData['wholeHistoryAve'] /= $examCount;
     $wholeAveData['wholePoliticsAve'] /= $examCount;
     $wholeAveData['wholeGeographyAve'] /= $examCount;
     $wholeAveData['wholeTotalAve'] /= $examCount;
     //        end得到所有考试的所有学生的每科平均分.
     //        对班级未来的各科考试预测
     $wholeForecastData = array();
     $wholeForecastData['forecastChinese'] = 102.3;
     $wholeForecastData['forecastMathAve'] = 122.2;
     $wholeForecastData['forecastEnglish'] = 77.09999999999999;
     $wholeForecastData['forecastPhysics'] = 72.09999999999999;
     $wholeForecastData['forecastBiology'] = 84.2;
     $wholeForecastData['forecastChemistry'] = 70;
     $wholeForecastData['forecastHistory'] = 63.9;
     $wholeForecastData['forecastPolitics'] = 73;
     $wholeForecastData['forecastGeography'] = 84.90000000000001;
     $wholeForecastData['forecastTotal'] = 749.7;
     //        使用预测算法对班级的下次考试预测, 并更新$wholeForecastData的所有值
     //        dd($wholeForecastData);
     //        dd($wholeAveData);
     //        dd($allExamsAveData);
     //        将数据重新格式化成highcharts能够直接识别的形式.
     $wholeAveDataArray = [$wholeAveData['wholeChineseAve'], $wholeAveData['wholeMathAve'], $wholeAveData['wholeEnglishAve'], $wholeAveData['wholePhysicsAve'], $wholeAveData['wholeBiologyAve'], $wholeAveData['wholeChemistryAve'], $wholeAveData['wholeHistoryAve'], $wholeAveData['wholePoliticsAve'], $wholeAveData['wholeGeographyAve']];
     $wholeForecastDataArray = [$wholeForecastData['forecastChinese'], $wholeForecastData['forecastMathAve'], $wholeForecastData['forecastEnglish'], $wholeForecastData['forecastPhysics'], $wholeForecastData['forecastBiology'], $wholeForecastData['forecastChemistry'], $wholeForecastData['forecastHistory'], $wholeForecastData['forecastPolitics'], $wholeForecastData['forecastGeography']];
     //        $averageTotalAndRankingArray = [[$studentAllPreExamsAve['totalAve'],$studentAllPreExamsAve['rankingAve'],35]];
     //        $forecastTotalAndRankingArray = [[$forecastNextExam['total'],$forecastNextExam['ranking'],40]];
     //
     //        dd($wholeAveDataArray);
     //        dd($wholeForecastDataArray);
     return view('forecast.forecastSectionFuturePerformance', compact('section', 'wholeAveDataArray', 'wholeForecastDataArray', 'allExamsAveData', 'wholeForecastData'));
 }