IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Exam/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/Exam/MarksCreateExam.php

<?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 MarksCreateExam extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        if ($request->ajax()) {
            $exams = \App\MarkExams::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('marks_exam.idSchool', Auth::guard('school')->user()->idSchool)
            ->where('marks_exam.idFinancialYear', Session::get('idFinancialYear'));
            $search = $request->input('search.value');
            $start = $request->input('start');
            $length = $request->input('length');
            $totalFiltered = 0;
            $totalData = 0;
            if (isset($search)) {
                $filter = \App\MarkExams::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('marks_exam.idSchool', Auth::guard('school')->user()->idSchool)
                            ->where('marks_exam.idFinancialYear', Session::get('idFinancialYear'))
                            ->where('name', 'LIKE', "%{$search}%")->orderBy('idExam', "DESC")->count();
                $exams = $exams->where('name', 'LIKE', "%{$search}%")
                            ->orderBy('marks_exam.created_at', "DESC")
                            ->skip($start)->take($length)->get();
                $totalFiltered = $filter;
            } else {
                $exams = $exams
                            ->orderBy('marks_exam.created_at', "DESC")->skip($start)->take($length)->get();
                $totalData = \App\MarkExams::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('marks_exam.idSchool', Auth::guard('school')->user()->idSchool)
                                ->where('marks_exam.idFinancialYear', Session::get('idFinancialYear'))->count();
                $totalFiltered = $totalData;
            }


            $data = array();
            $k = 0;
            foreach ($exams as $value) {
                $nestedData = array();
                $k++;
                array_push($nestedData, $k);
                array_push($nestedData, $value->name);
                array_push($nestedData, $value->className);

                if(isset($value->sectionName))
                array_push($nestedData, $value->sectionName);
                else array_push($nestedData, "All");

                $subjectMarks = null;
                foreach(json_decode($value->marks,true) as $points){
                    if($points["pratical"] == "nil"){
                        $points["pratical"] = "NA";
                    }
                    if($subjectMarks == null)
                    $subjectMarks = '<span>'.$points["subject"].'( Theory :'.$points["theory"].'</span> Pratical : '.$points["pratical"].' )<br>';
                    else $subjectMarks .= '<span>'.$points["subject"].'( Theory :'.$points["theory"].'</span> Pratical : '.$points["pratical"].' )<br>';
                }
                array_push($nestedData,$subjectMarks);
                
                $subjects = null;
                $getSubject = \App\Subject::whereIn('idSubject', json_decode($value->subjects,true))->where('idFinancialYear', Session::get('idFinancialYear'))->get()->pluck('subjectName')->toArray();
                foreach($getSubject as $subject){
                    if($subjects == null)
                    $subjects = '<span>'.$subject.'</span><br>';
                    else $subjects .= '<span>'.$subject.'</span><br>';
                }
                array_push($nestedData,  $subjects);

                $marks = null;
                foreach(json_decode($value->grades,true) as $points){
                    if($marks == null)
                    $marks = '<span>'.$points["from"].' marks to '.$points["to"].' marks</span> (Grade '.$points["name"].')<br>';
                    else $marks .= '<span>'.$points["from"].' marks to '.$points["to"].' marks</span> (Grade  '.$points["name"].')<br>';
                }

                array_push($nestedData,  $marks);
                array_push($nestedData, '<a href="https://online-login.online/school/marks/exam-create/' . $value->idExam . '/edit" class="btn btn-raised btn-info waves-effect btn-round">Edit</a><button class="btn btn-raised btn-danger waves-effect btn-round js-sweetalert" data-id="'.$value->idExam.'" data-type="confirm">DELETE</button>');
                array_push($data, $nestedData);
            }

            $json_data = array(
                "draw" => intval($request->input('draw')),
                "recordsTotal" => intval($totalData),
                "recordsFiltered" => intval($totalFiltered),
                "data" => $data
            );
            return json_encode($json_data);
        }  
        $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_create_exam',compact('classes','examtype'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $rules = [
            'examName' => 'required',
            'idClass' => 'required',
            'grades' => 'required',
            'marks' => 'required'
        ];
        $message = [
            'examName.required' => 'Exam Name is required',
            'idClass.required' => 'Class is required',
            'grades.required' => 'Grades is required',
            'marks.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);
        }

        $marks = array();
        foreach($request->marks as $points){
            array_push($marks,$points);
        }
        if(isset($request->idSectionAll)){
            $exam = new \App\MarkExams();
            $exam->idSchool = Auth::guard('school')->user()->idSchool;
            $exam->idType = $request->examName;
            $exam->idClass = $request->idClass;
            if(isset($request->idSubjectAll))
            {
                $subjects = \App\Subject::where('idClass', '=', $request->idClass)->where('idFinancialYear', Session::get('idFinancialYear'))->get()->pluck('idSubject')->toArray();
                $exam->subjects = json_encode($subjects);
            }else{
                $exam->subjects = json_encode($request->idSubject);
            }
            $exam->marks = json_encode($marks);
            $exam->grades = json_encode($grades);
            $exam->idFinancialYear = Session::get('idFinancialYear');
            $exam->save();
        }else{
            foreach ($request->idSection as $key => $value) {
                $exam = new \App\MarkExams();
                $exam->idSchool = Auth::guard('school')->user()->idSchool;
                $exam->idType = $request->examName;
                $exam->idClass = $request->idClass;
                $exam->idSection = $value;
                if(isset($request->idSubjectAll))
                {
                    $subjects = \App\Subject::where('idClass', '=', $request->idClass)->where('idFinancialYear', Session::get('idFinancialYear'))->get()->pluck('idSubject')->toArray();
                    $exam->subjects = json_encode($subjects);
                }else{
                    $exam->subjects = json_encode($request->idSubject);
                }
                $exam->marks = json_encode($marks);
                $exam->grades = json_encode($grades);
                $exam->idFinancialYear = Session::get('idFinancialYear');
                $exam->save();
            }
        }
        
        flash('Exams has been added successfully.');      
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
        return redirect('school/marks/exam-create');
    }

    /**
     * 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)->where('idFinancialYear', Session::get('idFinancialYear'))->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',
            'marks' => 'required'
        ];
        $message = [
            'examName.required' => 'Exam Name is required',
            'idClass.required' => 'Class is required',
            'grades.required' => 'Grades is required',
            'narks.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);
        }
        $marks = array();
        foreach($request->marks as $points){
            array_push($marks,$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)->where('idFinancialYear', Session::get('idFinancialYear'))->get()->pluck('idSubject')->toArray();
            $exam->subjects = json_encode($subjects);
        }else{
            $exam->subjects = json_encode($request->idSubject);
        }
        $exam->marks = json_encode($marks);
        $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']);
    }

}

Copyright © 2021 - 2025 IMMREX7