IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Session;
class AssignClassController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$teacherclasses = \App\TeacherClass::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idTeacherClass', 'desc');
if ($request->has('idFinancialYear')) {
$teacherclasses = $teacherclasses->where('idFinancialYear', '=', $request->idFinancialYear)->get();
} else {
$teacherclasses = $teacherclasses->where('idFinancialYear', '=', Session::get('idFinancialYear'))->get();
}
$classes = ['' => '--Select--', 'All' => 'All'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$teachers = DB::table('employees')
->join('designations', 'employees.idDesignation', '=', 'designations.idDesignation')
->where('employees.idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('designationName', '=', 'Teacher')
->select(DB::raw("CONCAT_WS('-',firstName,enrollmentNo) as name"), 'idEmployee')
->orderBy('idEmployee', 'desc')->get()
->pluck('name', 'idEmployee')->toArray();
//dd($teachers);
return view('schools.employees.assignclass', compact('classes', 'teachers', 'teacherclasses'));
}
/**
* 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 = [
'idEmployee' => 'required',
'idClass' => 'required',
// 'idSection' => 'required'
];
if(isset($request->sections)){
if ((count($request->sections) == 0) && $request->idClass != 'All') {
$rules += ['idSection' => 'required'];
}
}else if($request->idClass != 'All'){
$rules += ['idSection' => 'required'];
}
$messages = [
'idEmployee.required' => 'Teacher must be selected',
'idClass.required' => 'Class must be selected',
'idSection.required' => 'Section must be selected'
];
$this->validate($request, $rules, $messages);
if ($request->idClass == 'All') {
$classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get();
foreach ($classes as $c1) {
$sections = \App\Section::where('idClass', '=', $c1->idClass)->get();
foreach ($sections as $sec) {
$teacherclass = new \App\TeacherClass();
$teacherclass->fill($request->all());
$teacherclass->idClass = $sec->idClass;
$teacherclass->idSection = $sec->idSection;
$teacherclass->idSchool = Auth::guard('school')->user()->idSchool;
$teacherclass->idFinancialYear = Session::get('idFinancialYear');
$teacherclass->save();
}
}
} else if ($request->has('sections')) {
foreach ($request->sections as $key => $value) {
$teacherclass = new \App\TeacherClass();
$teacherclass->fill($request->all());
$teacherclass->idSection = $key;
$teacherclass->idSchool = Auth::guard('school')->user()->idSchool;
$teacherclass->idFinancialYear = Session::get('idFinancialYear');
$teacherclass->save();
}
} else {
$teacherclass = new \App\TeacherClass();
$teacherclass->fill($request->all());
$teacherclass->idClass = $request->idClass;
$teacherclass->idSchool = Auth::guard('school')->user()->idSchool;
$teacherclass->idFinancialYear = Session::get('idFinancialYear');
$teacherclass->save();
}
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/asignclass');
}
/**
* 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) {
$teacherclasses = \App\TeacherClass::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear', '=', Session::get('idFinancialYear'))
->orderBy('idTeacherClass', 'desc')->get();
$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,' ',lastName,' ',enrollmentNo) AS name"), 'idEmployee')
->where('employees.idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('designationName', '=', 'Teacher')
->orderBy('idEmployee', 'desc')->get()->pluck('name', 'idEmployee')->toArray();
$teacherclass = \App\TeacherClass::where('idTeacherClass', '=', $id)->first();
$section = \App\Section::where('idSection', '=', $teacherclass->idSection)->get()->pluck('sectionName', 'idSection')->toArray();
return view('schools.employees.assignclass', compact('classes', 'section', 'teachers', 'teacherclasses', 'teacherclass'));
}
/**
* 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 = [
'idEmployee' => 'required',
'idClass' => 'required',
// 'idSection' => 'required'
];
$messages = [
'idEmployee.required' => 'Teacher must be selected',
'idClass.required' => 'Class must be selected',
'idSection.required' => 'Section must be selected'
];
$this->validate($request, $rules, $messages);
$teacherclass = \App\TeacherClass::where('idTeacherClass', '=', $id)->first();
$teacherclass->fill($request->all());
$teacherclass->update();
return redirect('school/asignclass');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$teacherclass = \App\TeacherClass::where('idTeacherClass', '=', $id)->first();
$teacherclass->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -