atcheck/app/Http/Controllers/User/UserAttendancesController.php

89 lines
3.0 KiB
PHP
Raw Normal View History

2019-12-01 23:36:12 +01:00
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
2019-12-04 10:21:58 +01:00
use Illuminate\Support\Facades\Auth;
use App\Exports\AttendanceExportView;
use App\Exports\AttendanceGroupedExportView;
use Maatwebsite\Excel\Facades\Excel;
2019-12-01 23:36:12 +01:00
use App\Subject;
use App\Classes;
use App\Attendance;
class UserAttendancesController extends Controller
{
2019-12-04 10:21:58 +01:00
public function index($groupBy='classes_id')
2019-12-01 23:36:12 +01:00
{
2019-12-04 10:21:58 +01:00
$user_id = Auth::id();
2019-12-01 23:36:12 +01:00
$subjects = Subject::where('user_id', $user_id)->get();
$subjects_ids = $subjects->pluck('id')->toArray();
2019-12-13 23:28:16 +01:00
$classes = Classes::whereIn('subject_id', $subjects_ids)->orderBy('created_at','DESC')->get();
2019-12-01 23:36:12 +01:00
$classes_ids = $classes->pluck('id')->toArray();
$attendances = Attendance::whereIn('classes_id', $classes_ids)->get();
$attendances_grouped = $attendances->groupBy($groupBy);
return view('user.user_attendances', ['attendances' => $attendances, 'attendances_grouped' => $attendances_grouped, 'classes' => $classes, 'grouped_by' => $groupBy]);
}
2019-12-04 10:21:58 +01:00
public function add_attendance(Request $request)
2019-12-01 23:36:12 +01:00
{
$classes_id = $request->input('classes_id');
$student_id_number = $request->input('student_id');
$student_name = $request->input('student_name');
$student_surname = $request->input('student_surname');
$seat_number = $request->input('seat_number');
$note = $request->input('note');
2019-12-01 23:36:12 +01:00
Attendance::create([
'classes_id' => $classes_id,
'student_id_number' => $student_id_number,
'student_name' => $student_name,
'student_surname' => $student_surname,
'seat_number' => $seat_number,
'notes' => $note
2019-12-01 23:36:12 +01:00
]);
return redirect()->back();
2019-12-01 23:36:12 +01:00
}
2019-12-04 10:21:58 +01:00
public function delete_attendance($attendance_id)
2019-12-01 23:36:12 +01:00
{
Attendance::find($attendance_id)->delete();
return redirect()->back();
2019-12-01 23:36:12 +01:00
}
2019-12-04 10:21:58 +01:00
public function edit_attendance($attendance_id)
2019-12-01 23:36:12 +01:00
{
Attendance::find($attendance_id);
return redirect()->back();
2019-12-01 23:36:12 +01:00
}
public function export($classes_id)
{
$classes_date = Classes::find($classes_id)->date;
return Excel::download(new AttendanceExportView($classes_id), "classes-attendance-{$classes_date}.xlsx");
}
public function export_grouped($groupBy)
{
$today_date = date('Y-m-d');
$groupByLabel = str_replace("_", "-", $groupBy);
if($groupBy == 'classes_id') {
$groupByLabel = 'classes_name';
}
return Excel::download(new AttendanceGroupedExportView($groupBy), "all-attendance-grouped-by-{$groupByLabel}-{$today_date}.xlsx");
}
public function add_attendance_note(Request $request)
{
$attendance_id = $request->input('attendance_id');
$note = $request->input('note_content');
$attendance = Attendance::find($attendance_id);
$attendance->notes = $note;
$attendance->save();
$attendance->refresh();
return redirect()->back();
}
2019-12-01 23:36:12 +01:00
}