/**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function getsheet()
 {
     $inputs = Input::all();
     $input = (object) $inputs;
     $subjects = Subject::select('name')->where('class', $input->class)->orderby('code', 'asc')->get();
     if (count($subjects) < 1) {
         return Redirect::to('/tabulation')->withInput(Input::all())->with("error", "There are not subjects for this class!");
     }
     $students = Student::select('regiNo', 'firstName', 'middleName', 'lastName')->where('class', $input->class)->where('section', $input->section)->where('session', trim($input->session))->where('shift', $input->shift)->get();
     if (count($students) < 1) {
         return Redirect::to('/tabulation')->withInput(Input::all())->with("error", "There are not student for this class!");
     }
     $merit = DB::table('MeritList')->select('regiNo', 'grade', 'point', 'totalNo')->where('exam', $input->exam)->where('class', $input->class)->where('session', trim($input->session))->orderBy('point', 'DESC')->orderBy('totalNo', 'DESC')->get();
     if (count($merit) < 1) {
         return Redirect::to('/tabulation')->withInput(Input::all())->with("error", "Marks not submit or result not generate for this exam!");
     }
     foreach ($students as $student) {
         $marks = Marks::select('written', 'mcq', 'practical', 'ca', 'total', 'grade', 'point')->where('regiNo', $student->regiNo)->where('exam', $input->exam)->orderby('subject', 'asc')->get();
         if (count($marks) < 1) {
             return Redirect::to('/tabulation')->withInput(Input::all())->with("error", "Marks not submited yet!");
         }
         /*$marks = DB::table('Marks')
           ->join('MeritList', 'Marks.regiNo', '=', 'MeritList.regiNo')
           ->select('Marks.written','Marks.mcq', 'Marks.practical', 'Marks.ca', 'Marks.total', 'Marks.grade', 'Marks.point', 'MeritList.totalNo', 'MeritList.grade as tgrade','MeritList.point as tpoint')
           ->where('Marks.regiNo',$student->regiNo)
           ->where('Marks.exam', '=',$input->exam)
           ->orderby('Marks.subject','asc')
           ->get();*/
         $meritdata = new Meritdata();
         $position = 0;
         foreach ($merit as $m) {
             $position++;
             if ($m->regiNo === $student->regiNo) {
                 $meritdata->regiNo = $m->regiNo;
                 $meritdata->point = $m->point;
                 $meritdata->grade = $m->grade;
                 $meritdata->position = $position;
                 $meritdata->totalNo = $m->totalNo;
                 break;
             }
         }
         $student->marks = $marks;
         $student->meritdata = $meritdata;
     }
     $cl = ClassModel::Select('name')->where('code', $input->class)->first();
     $input->class = $cl->name;
     $fileName = $input->class . '-' . $input->section . '-' . $input->session . '-' . $input->exam;
     // return $students;
     Excel::create($fileName, function ($excel) use($input, $subjects, $students) {
         $excel->sheet('New sheet', function ($sheet) use($input, $subjects, $students) {
             $sheet->loadView('app.excel', compact('subjects', 'input', 'students'));
         });
     })->download('xlsx');
 }
 public function postgenerate()
 {
     $rules = ['class' => 'required', 'exam' => 'required', 'session' => 'required'];
     $validator = \Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('/result/generate')->withErrors($validator)->withInput();
     } else {
         $subjects = Subject::select('name', 'code', 'type', 'subgroup')->where('class', '=', Input::get('class'))->get();
         $marksubmit = Marks::select('subject')->where('class', '=', Input::get('class'))->where('session', trim(Input::get('session')))->where('exam', Input::get('exam'))->distinct()->get();
         if (count($subjects) == count($marksubmit)) {
             $fourthsubjectCode = "";
             foreach ($subjects as $subject) {
                 if ($subject->type === "Electives") {
                     $fourthsubjectCode = $subject->code;
                 }
             }
             $students = Student::select('regiNo')->where('class', '=', Input::get('class'))->where('session', '=', trim(Input::get('session')))->get();
             if (count($students) != 0) {
                 $gparules = GPA::select('gpa', 'grade', 'markfrom')->get();
                 $foobar = array();
                 foreach ($students as $student) {
                     $marks = Marks::select('subject', 'grade', 'point', 'total')->where('regiNo', '=', $student->regiNo)->where('exam', '=', Input::get('exam'))->get();
                     if (count($marks) != 0) {
                         $totalpoint = 0;
                         $totalmarks = 0;
                         $subcounter = 0;
                         $banglamark = 0;
                         $englishmark = 0;
                         $isfail = false;
                         foreach ($marks as $mark) {
                             if ($this->getSubGroup($subjects, $mark->subject) === "Bangla") {
                                 $banglamark += $mark->total;
                             } else {
                                 if ($this->getSubGroup($subjects, $mark->subject) === "English") {
                                     $englishmark += $mark->total;
                                 } else {
                                     if ($mark->subject === $fourthsubjectCode) {
                                         if ($mark->point >= 2.0) {
                                             $totalmarks += $mark->total;
                                             $totalpoint += $mark->point - 2;
                                         } else {
                                             $totalmarks += $mark->total;
                                         }
                                         $subcounter--;
                                     } else {
                                         $totalmarks += $mark->total;
                                         $totalpoint += $mark->point;
                                     }
                                 }
                             }
                             $subcounter++;
                             if ($mark->subject !== $fourthsubjectCode && $mark->grade === "F") {
                                 $isfail = true;
                             }
                         }
                         if ($banglamark > 0) {
                             $blmarks = floor($banglamark / 2);
                             $totalmarks += $banglamark;
                             $totalpoint += $this->pointCalculator($blmarks, $gparules);
                             $subcounter--;
                         }
                         if ($englishmark > 0) {
                             $enmarks = floor($englishmark / 2);
                             $totalmarks += $englishmark;
                             $totalpoint += $this->pointCalculator($enmarks, $gparules);
                             $subcounter--;
                         }
                         $grandPoint = $totalpoint / $subcounter;
                         if ($isfail) {
                             $grandGrade = $this->gradnGradeCal(0.0, $gparules);
                         } else {
                             $grandGrade = $this->gradnGradeCal($grandPoint, $gparules);
                         }
                         $merit = new MeritList();
                         $merit->class = Input::get('class');
                         $merit->session = trim(Input::get('session'));
                         $merit->exam = Input::get('exam');
                         $merit->regiNo = $student->regiNo;
                         $merit->totalNo = $totalmarks;
                         $merit->point = $grandPoint;
                         $merit->grade = $grandGrade;
                         $merit->save();
                     } else {
                         return Redirect::to('/result/generate')->withInput()->with("noresult", "This examintaion marks not submited yet!!");
                     }
                 }
             } else {
                 return Redirect::to('/result/generate')->withInput()->with("noresult", "There is no students in this class!!");
             }
             return Redirect::to('/result/generate')->with("success", "Result Generate and Publish Successfull.");
         } else {
             return Redirect::to('/result/generate')->withInput()->with("noresult", "All subjects marks not submited yet!!");
         }
     }
 }