IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;
use App\Http\SendNotificationApi;
use Session;
class HomeworkController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$today_date = Carbon::today()->format('Y-m-d');
if ($request->has('fromDate') && $request->fromDate != null) {
$from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
}
if ($request->has('toDate') && $request->toDate != null) {
$to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
}
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$homeworks = \App\Homework::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))
->orderBy('idHomework', 'desc');
if ($request->toDate != null && $request->fromDate != null) {
$homeworks = $homeworks->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"])->paginate('50');
} else if ($request->has('fromDate') && $request->fromDate != null) {
$homeworks = $homeworks->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"])->paginate('50');
} else if ($request->has('toDate') && $request->toDate != null) {
$homeworks = $homeworks->whereDate('created_at', '<=', $to_date)->get();
} else {
$homeworks = $homeworks->paginate('50');
}
return view('schools.homework.index', compact('classes', 'homeworks'));
}
/**
* 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 = [
'idClass' => 'required',
'subject' => 'required|max:120',
'homework' => 'required|max:1000',
// 'homeworkFile' => 'mimes:doc,jpg,png,jpeg,pdf,docx|max:25000'
];
if ($request->has('schedule')) {
$rules += ['publishDate' => 'required'];
}
if (count($request->sections) == 0) {
$rules += ['idSection' => 'required'];
}
$messages = [
'idClass.required' => 'Class must be selected.',
'idSection.required' => 'Section must be selected.'
];
$this->Validate($request, $rules, $messages);
if (!empty($request->students)) {
foreach ($request->students as $key => $value) {
$std = \App\AdmEntry::where('idStudent', '=', $value)->first();
$homework = new \App\Homework();
$homework->fill($request->all());
$homework->idStudent = $value;
$homework->idSection = $std->idSection;
$homework->idSchool = Auth::guard('school')->user()->idSchool;
$homework->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$homework->isPublished = 'N';
} else {
$homework->publishDate = today_date();
$homework->isPublished = 'Y';
}
DB::beginTransaction();
$homework->save();
if ($request->hasFile('homeworkFile')) {
$hw = 'homework_' . $homework->idHomework . '.' . $request->file('homeworkFile')->getClientOriginalExtension();
$request->file('homeworkFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/homeworks/', $hw);
$homework->homeworkFile = $hw;
}
$homework->total_student = 1;
$homework->update();
DB::commit();
if($homework->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Homework : ".$request->homework);
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idStudent', $value)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Homework : ".$request->homework);
}
}
} else {
foreach ($request->sections as $key1 => $value1) {
$homework = new \App\Homework();
$homework->fill($request->all());
$homework->idSection = $value1;
$homework->idSchool = Auth::guard('school')->user()->idSchool;
$homework->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$homework->isPublished = 'N';
} else {
$homework->publishDate = today_date();
$homework->isPublished = 'Y';
}
DB::beginTransaction();
$homework->save();
if ($request->hasFile('homeworkFile')) {
$hw = 'homework_' . $homework->idHomework . '.' . $request->file('homeworkFile')->getClientOriginalExtension();
$request->file('homeworkFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/homeworks/', $hw);
$homework->homeworkFile = $hw;
}
$homework->update();
$totalStudents = DB::table('students')
->where('students.idSection', $value1)
->where('students.idClass', $request->idClass)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->count();
$homework->total_student = $totalStudents;
$homework->update();
DB::commit();
if($homework->isPublished == 'Y'){
$reg_ids=DB::table('students')
->join('parents', 'students.father_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idSection', $value1)
->where('students.idClass', $request->idClass)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Homework : ".$request->homework);
$reg_ids=DB::table('students')
->join('parents', 'students.mother_mobile', '=', 'parents.mobile')
->select('parents.idFirebase')
->where('students.idSection', $value1)
->where('students.idClass', $request->idClass)
->where('students.idFinancialYear', Session::get('idFinancialYear'))
->where('students.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('idFirebase')->toArray();
SendNotificationApi::sendNotification( $reg_ids,"Homework : ".$request->homework);
}
}
}
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/homeworks');
}
/**
* 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) {
$classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
$homeworks = \App\Homework::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))
->orderBy('idHomework', 'desc')->paginate('50');
$homework = \App\Homework::findOrFail($id);
$section = \App\Section::where('idSection', '=', $homework->idSection)->get()->pluck('sectionName', 'idSection')->toArray();
$student = \App\AdmEntry::where('idStudent', '=', $homework->idStudent)->get()->pluck('name', 'idStudent')->toArray();
return view('schools.homework.index', compact('classes', 'homeworks', 'homework', 'section', 'student'));
}
/**
* 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 = [
'idClass' => 'required',
'subject' => 'required|max:120',
'homework' => 'required|max:500',
// 'homeworkFile' => 'required|mimes:doc,jpg,png,jpeg,pdf,docx|max:25000'
];
if (empty($request->sections)) {
$rules += ['idSection' => 'required'];
}
$messages = [
'idClass.required' => 'Class must be selected.',
'idSection.required' => 'Section must be selected.'
];
$this->Validate($request, $rules, $messages);
$homework = \App\Homework::findOrFail($id);
$homework->fill($request->all());
if ($request->hasFile('homeworkFile')) {
$hw = 'homework_' . $homework->idHomework . '.' . $request->file('homeworkFile')->getClientOriginalExtension();
$request->file('homeworkFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/homeworks/', $hw);
$homework->homeworkFile = $hw;
}
$homework->update();
return redirect('school/homeworks');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$homework = \App\Homework::findOrFail($id);
$homework->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
public function viewHomeworkDoc($id) {
$homework = \App\Homework::findOrFail($id);
$path = storage_path('app/public/schools/' . $homework->idSchool . '/homeworks/' . $homework->homeworkFile);
return response()->file($path);
}
}
Copyright © 2021 -