IMMREX7
<?php
namespace App\Http\Controllers\School\Exam;
use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Auth;
use Session;
use Carbon\Carbon;
class AddMarksController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$isSectionAll = 'N';
$isSubjectAll = 'N';
$isStudentAll = 'N';
$examtype = ['' => '--Select--'] + \App\MarkExamType::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', Session::get('idFinancialYear'))
->orderBy('idType')->get()->pluck('name', 'idType')->toArray();
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
return view('schools.exams.marks_add',compact('classes','examtype','isSectionAll','isSubjectAll','isStudentAll'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request) {
$type = \App\MarkExams::where('marks_exam.idType', '=', $request->examName)->where('idClass', '=', $request->idClass)->first();
if($type == null || $request->idSubject == null || $request->idSection == null || $request->idStudent == null){
flash('All fields are compulsary');
return redirect('school/marks/add-marks');
}
//dd($request->all());
$subjects = \App\Subject::where('idClass', '=', $request->idClass)->where('idFinancialYear', Session::get('idFinancialYear'))->get()->pluck('idSubject','subjectName')->toArray();
$sections = \App\Section::where('idClass', '=', $request->idClass)->get()->pluck('idSection','sectionName')->toArray();
$totalStudents = \App\AdmEntry::whereIn('idSection', $request->idSection)->where('idFinancialYear', Session::get('idFinancialYear'))->get();
$students = $totalStudents->pluck('idStudent', 'name')->toArray();
$examtype = ['' => '--Select--'] + \App\MarkExamType::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', Session::get('idFinancialYear'))
->orderBy('idType')->get()->pluck('name', 'idType')->toArray();
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$isSectionAll = 'N';
if(isset($request->idSectionAll)) $isSectionAll = 'Y';
$isSubjectAll = 'N';
if(isset($request->idSubjectAll)) $isSubjectAll = 'Y';
$isStudentAll = 'N';
if(isset($request->idStudentAll)) $isStudentAll = 'Y';
$idStudent = $request->idStudent;
$idSubject = $request->idSubject;
$idSections = $request->idSection;
$idResult = $request->type;
$marks = array();
foreach(json_decode($type->marks,true) as $points){
$marks[$points["id"]] = $points;
}
return view('schools.exams.marks_add',compact('idResult','classes','examtype','subjects','sections','students','type','isSectionAll','isSubjectAll','isStudentAll','idStudent','idSubject','idSections','totalStudents','marks'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$rules = [
'idType' => 'required',
'idExam' => 'required',
];
$message = [
'idType.required' => 'Exam Grades is required',
'idExam.required' => 'Exam Name is required',
];
$this->Validate($request, $rules, $message);
$type = \App\MarkExams::where('marks_exam.idType', '=', $request->idType)->first();
foreach($request->data as $key => $value){
foreach($value as $idSubject => $fields){
$student = \App\AdmEntry::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idStudent', $fields['students'])->where('idFinancialYear', Session::get('idFinancialYear'))->first();
if(\App\ExamSheet::where('idStudent',$student->idStudent)->where('idSubject',$idSubject)->where('idExam',$request->idType)->first() == null)
$marks = new \App\ExamSheet();
else $marks = \App\ExamSheet::where('idStudent',$student->idStudent)->where('idSubject',$idSubject)->where('idExam',$request->idType)->first();
$marks->idSchool = $student->idSchool;
$marks->idFinancialYear = $student->idFinancialYear;
$marks->idClass = $student->idClass;
$marks->idSection = $student->idSection;
$marks->idGradeRule = $request->idExam;
$marks->attendance = $fields['status'];
$marks->idExam = $request->idType;
$marks->idStudent = $student->idStudent;
$marks->published_date = Carbon::now()->format('Y-m-d');
$marks->idSubject =$idSubject;
$marks->marks = $fields['theory'];
$marks->pratical = $fields['pratical'];
$marks->grade = $fields['grade'];
if(\App\ExamSheet::where('idStudent',$student->idStudent)->where('idSubject',$idSubject)->where('idExam',$request->idType)->first() == null){
foreach(json_decode($type->grades,true) as $grade){
if($grade['name'] == $fields['grade'])
$marks->grade_range = $grade['from'].'-'.$grade['to'];
}
$marks->total = $fields['total'];
$marks->save();
}else{
foreach(json_decode($type->grades,true) as $grade){
if($grade['name'] == $fields['grade'])
$marks->grade_range = $grade['from'].'-'.$grade['to'];
}
$marks->total = $fields['total'];
$marks->update();
}
}
}
flash('Marks saved successfully');
return redirect('school/marks/add-marks');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$type = \App\MarkExams::select('marks_exam_type.*','marks_exam.*','className','sectionName')->join('marks_exam_type', 'marks_exam.idType', '=', 'marks_exam_type.idType')
->join('classes', 'marks_exam.idClass', '=', 'classes.idClass')
->leftJoin('sections', 'marks_exam.idSection', '=', 'sections.idSection')
->where('idExam', '=', $id)->first();
if($type == null){
return redirect('school/marks/exam-create');
}
$examtype = \App\MarkExamType::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', Session::get('idFinancialYear'))
->orderBy('idType')->get()->pluck('name', 'idType')->toArray();
$classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$subject = \App\Subject::where('idClass', '=', $type->idClass)->get()->pluck('idSubject','subjectName')->toArray();
$section = \App\Section::where('idClass', '=', $type->idClass)->get()->pluck('sectionName','idSection')->toArray();
//dd($section);
return view('schools.exams.marks_create_exam', compact('type','examtype','classes','subject','section'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$rules = [
'examName' => 'required',
'idClass' => 'required',
'grades' => 'required',
'theory' => 'required'
];
$message = [
'examName.required' => 'Exam Name is required',
'idClass.required' => 'Class is required',
'grades.required' => 'Grades is required',
'theory.required' => 'Marks are required'
];
$this->Validate($request, $rules, $message);
$grades = array();
foreach($request->grades as $points){
if($points["from"] > $points["to"] ){
if ($request->ajax()) {
return response()->json(['success' => "Grade marks error from is greater than to in grade ".$points[
"name"]."."], 200, ['app-status' => 'success']);
}
}else
array_push($grades,$points);
}
$this->Validate($request, $rules, $message);
$exam = \App\MarkExams::where('idExam', '=', $id)->first();
$exam->idType = $request->examName;
$exam->idClass = $request->idClass;
$exam->idSection = $request->idSection;
if(isset($request->idSubjectAll))
{
$subjects = \App\Subject::where('idClass', '=', $request->idClass)->get()->pluck('idSubject')->toArray();
$exam->subjects = json_encode($subjects);
}else{
$exam->subjects = json_encode($request->idSubject);
}
$exam->theory = $request->theory;
if($request->praticals != "nil")
$exam->pratical = $request->pratical;
$exam->grades = json_encode($grades);
$exam->update();
return redirect('school/marks/exam-create');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$type = \App\MarkExams::where('idExam', '=', $id)->first();
$type->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
public function reports(Request $request){
$classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
if($request->get('idSection') != null){
$sections_ids = $request->get('idSection');
$students = \App\AdmEntry::select('students.firstName', 'students.middleName', 'students.lastName', 'students.idStudent', 'students.ecNo', 'students.idClass', 'students.idSection','idFinancialYear','idSchool')->where('students.idClass', $request->get('idClass'))->whereIn('students.idSection', $sections_ids)->where('students.idFinancialYear', Session::get('idFinancialYear'))->orderBy('ecNo')->where('students.isActive', 'Y')->get();
}else if($request->get('idClass') != null){
$students = \App\AdmEntry::select('students.firstName', 'students.middleName', 'students.lastName', 'students.idStudent', 'students.ecNo', 'students.ecNoidClass', 'students.idSection','idFinancialYear','idSchool')->where('students.idClass', $request->get('idClass'))->where('students.idFinancialYear', Session::get('idFinancialYear'))->where('students.isActive', 'Y')->orderBy('ecNo')->get();
}else {
$students = [];
}
if(count($students) == 0)
return view('schools.exams.reports', compact('classes','students'));
if($request->get('idSubject') != null){
$subject_ids = $request->get('idSubject');
$subjects = \App\Subject::where('idClass', '=', $request->get('idClass'))->whereIn('idSubject', $subject_ids)->where('idFinancialYear','=', Session::get('idFinancialYear'))->get()->pluck('subjectName', 'idSubject')->toArray();
}else{
flash('No subject selected');
return redirect('school/marks/report-marks');
}
if($request->get('idExam') != null) {
$idExam = $request->get('idExam');
$type = \App\MarkExams::where('marks_exam.idType', '=', $idExam)->first();
$totalArr = array();
$praticalArr = array();
$totalMarks = json_decode($type->marks,true);
foreach($totalMarks as $total){
if($total["pratical"] == "nil")
{
$totalArr[$total["id"]] = $total["theory"];
$praticalArr[$total["id"]] = "NA";
}
else
$totalArr[$total["id"]] = $total["theory"] + $total["pratical"];
}
}
return view('schools.exams.reports', compact('classes','students','idExam','subjects','type','totalArr','praticalArr'));
}
public function fetchReport($id,Request $request){
$student = \App\AdmEntry::where('idStudent', '=', $request->get('student'))->first();
$exam = \App\MarkExamType::where('idType', $id)->orderBy('idType')->first();
$type = \App\MarkExams::where('marks_exam.idType', '=', $id)->first();
$result = \App\ExamSheet::join('subjects', 'exam_marksheet.idSubject', '=', 'subjects.idSubject')->where('exam_marksheet.idStudent',$student->idStudent)->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->where('idExam',$id)->get();
$examByPerformance = \App\ExamSheet::join('marks_exam_type', 'exam_marksheet.idExam', '=', 'marks_exam_type.idType')->select(DB::raw('SUM(total) as marks'),'name')->where('exam_marksheet.idStudent',$student->idStudent)->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->groupBy('idExam')->get();
$top = \App\ExamSheet::select(DB::raw('SUM(total) as total'),'idStudent as id')->whereNotNull('exam_marksheet.total')->where('exam_marksheet.idClass',$student->idClass)->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->where('idExam',$id)->groupBy('idStudent')->orderBy('total','DESC')->first();
$lowest = \App\ExamSheet::select(DB::raw('SUM(total) as total'),'idStudent as id')->where('exam_marksheet.idClass',$student->idClass)->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->where('idExam',$id)->groupBy('idStudent')->orderBy('total','ASC')->first();
$topper = \App\ExamSheet::join('subjects', 'exam_marksheet.idSubject', '=', 'subjects.idSubject')->join('students', 'exam_marksheet.idStudent', '=', 'students.idStudent')->join('classes', 'students.idClass', '=', 'classes.idClass')
->join('sections', 'students.idSection', '=', 'sections.idSection')->where('exam_marksheet.idClass',$student->idClass)
->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->where('exam_marksheet.idStudent',$top->id)->where('idExam',$id)->first();
$topperResult = \App\ExamSheet::join('subjects', 'exam_marksheet.idSubject', '=', 'subjects.idSubject')->where('exam_marksheet.idStudent',$top->id)->where('exam_marksheet.idFinancialYear', $student->idFinancialYear)->where('idExam',$id)->get();
$mine = $this->prepareResult($result,$type);
$top = $this->prepareResult($topperResult,$type);
if($lowest->total == null) $lowestScore = 0 ;
else $lowestScore = $lowest->total;
return view('schools.exams.exam_result', compact('examByPerformance','topper','mine','top','exam','lowestScore'));
}
private function prepareResult($result,$type){
$totalMarks = json_decode($type->marks,true);
$gradesRange = json_decode($type->grades,true);
$resultArr = array();
$overMinelTotal = 0;
$overTotal = 0;
foreach($result as $marks){
$temp = array(
"subject" => $marks->subjectName,
"full_marks" => 0,
"status" => $marks->attendance,
"theory" => $marks->marks,
"practical" => $marks->pratical,
"total" => $marks->marks + $marks->pratical,
"grade" => $marks->grade,
"percentage" => ""
);
$overMinelTotal = $overMinelTotal + $temp["total"];
foreach($totalMarks as $total){
if($total["id"] == $marks->idSubject)
{
if($total["pratical"] == "nil")
{
$temp["full_marks"] = $total["theory"];
$temp["practical"] = "NA";
}
else
$temp["full_marks"] = $total["theory"] + $total["pratical"];
$overTotal = $overTotal + $temp["full_marks"];
if($temp["full_marks"] != 0 )
{
$temp["percentage"] = ($temp["total"] / $temp["full_marks"]) * 100;
$temp["percentage"] = number_format((float)$temp["percentage"], 2, '.', '');
}
}
}
if($marks->marks != null){
foreach($gradesRange as $range){
$total = $temp["total"];
if($range["from"] <= $total && $total <= $range["to"] ){
$temp["grade"] = $range["name"];
}
}
}else{
foreach($gradesRange as $range){
$total = $temp["total"];
if($temp["grade"] == $range["name"] ){
$temp["total"] = "";
$temp["full_marks"] ="";
$temp["practical"] = "";
$temp["percentage"] = "";
$temp["percentage"] = "";
$temp["theory"] = "";
}
}
}
array_push($resultArr,$temp);
}
$grade = '';
foreach($gradesRange as $range){
if($overTotal > 0){
$total = ($overMinelTotal / $overTotal) * 100;
if($range["from"] <= $total && $total <= $range["to"] ){
$grade = $range["name"];
}
}
}
return array(
"result" => $resultArr,
"total" => $overMinelTotal,
"totalMarks" => $overTotal,
"grade" => $grade,
);
}
}
Copyright © 2021 -