IMMREX7

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

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use PDF;
use Session;

class TimetableController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $teachers = [''=>'--Select--']+ DB::table('employees')
                        ->join('designations', 'employees.idDesignation', '=', 'designations.idDesignation')
                        ->select(DB::raw("CONCAT(firstName,' ',enrollmentNo) AS name"),'idEmployee')
                        ->where('employees.idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->where('designationName', '=', 'Teacher')
                        ->orderBy('idEmployee', 'desc')->get()->pluck('name', 'idEmployee')->toArray();
        $subjects = ['' => '--Select Subject--'] + \App\Subject::where('idClass', '=', $request->idClass)
                        ->get()
                        ->pluck('subjectName', 'idSubject')->toArray();
        $periods = \App\Period::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                ->where('idClass', '=', $request->idClass)
                ->where('idSection', '=', $request->idSection);
        $timetables = DB::table('timetable')
                ->join('periods', 'timetable.idPeriod', '=', 'periods.idPeriod')
                ->select('timetable.idPeriod', 'idWeekday', 'idEmployee', 'idSubject', 'periodName', 'fromTime', 'toTime', 'isLunchBreak')
                ->groupBy('timetable.idPeriod')
                ->where('periods.idClass', '=', $request->idClass)
                ->where('periods.idSection', '=', $request->idSection)
                ->get()->pluck('periodName')->toArray();
//        dd($timetable);
        if (count($timetables) > 0) {
            $periods = $periods->whereNotIn('periodName',$timetables)->get();
            $timetable = DB::table('timetable')
                ->join('periods', 'timetable.idPeriod', '=', 'periods.idPeriod')
                ->select('timetable.idPeriod', 'idWeekday', 'idEmployee', 'idSubject', 'periodName', 'fromTime', 'toTime', 'isLunchBreak')
                ->groupBy('timetable.idPeriod')
                ->where('periods.idClass', '=', $request->idClass)
                ->where('periods.idSection', '=', $request->idSection)
                ->get();
            return view('schools.timetable.edit_timetable', compact('classes', 'periods', 'teachers', 'subjects','timetable'));
        } else {
            $periods = $periods->get();
            return view('schools.timetable.add_timetable', compact('classes', 'periods', 'teachers', 'subjects'));
        }
    }

    /**
     * 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) {
//        dd($request->all());
//        $rules = [];
//        foreach ($request->periods as $key => $value) {
//            foreach ($value as $key1 => $value1) {
//                if ($value1['idSubject'] == null) {
//                    $rules['periods.' . $key1 . '.idSubject'] = 'required';
//                }
//                if ($value1['idEmployee'] == null) {
//                    $rules['periods.' . $key1 . '.idEmployee'] = 'required';
//                }
//            }
//        }
//        $messages = [
//            'periods.*idSubject.required' => 'Subject must be Selected.',
//            'periods.*idEmployee.required' => 'Teacher must be Selected.',
//        ];
//        $this->validate($request, $rules, $messages);
        foreach ($request->periods as $key => $value) {
            foreach ($value as $key1 => $value1) {
                $timetable_exist = \App\Timetable::where('idPeriod', '=', $key1)->where('idWeekday', '=', $key)->first();
                if ($timetable_exist) {
                    if ($value1['idEmployee'] != null && $value1['idSubject'] != null) {
                        $timetable_exist->idEmployee = $value1['idEmployee'];
                        $timetable_exist->idSubject = $value1['idSubject'];
                    } else {
                        $timetable_exist->idEmployee = null;
                        $timetable_exist->idSubject = null;
                    }
                    $timetable_exist->update();
                } else {
                    $timetable = new \App\Timetable();
                    $timetable->fill($request->all());
                    $timetable->idPeriod = $key1;
                    if ($value1['idEmployee'] != null && $value1['idSubject'] != null) {
                        $timetable->idEmployee = $value1['idEmployee'];
                        $timetable->idSubject = $value1['idSubject'];
                    } else {
                        $timetable->idEmployee = null;
                        $timetable->idSubject = null;
                    }
                    $timetable->idWeekday = $value1['idWeekday'];
                    $timetable->idSchool = Auth::guard('school')->user()->idSchool;
                    $timetable->save();
                }
            }
        }
        flash('Timetable has been saved successfully !!');
        return redirect('school/timetable');
    }

    /**
     * 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) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $period = \App\Timetable::findOrfail($id);
        $period->delete();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

    public function viewTimetable(Request $request) {
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $periods = \App\Period::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->where('idClass', '=', $request->idClass)
                        ->where('idSection', '=', $request->idSection)->get();
        return view('schools.timetable.view_timetable', compact('classes', 'periods'));
    }
    
    public function printTimetable($cls,$sec) {
        $class = \App\ClassM::where('idClass','=',$cls)->first();
        $section = \App\Section::where('idSection','=',$sec)->first();
        $school = \App\School::where('idSchool','=',Auth::guard('school')->user()->idSchool)->first();
        $periods = \App\Period::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->where('idClass', '=', $cls)
                        ->where('idSection', '=', $sec)->get();
        $pdf = PDF::loadView('schools.timetable.print_timetable', ['margin_top' => 20], compact('school','class','section', 'periods', 'teachers', 'subjects'));
        return $pdf->stream('timetable.pdf');
     //   return view('', compact('school','classes', 'periods', 'teachers', 'subjects'));
    }

}

Copyright © 2021 - 2025 IMMREX7