middleware for classes starting created

This commit is contained in:
s416422 2019-12-04 16:55:59 +01:00
parent 027f788596
commit 0d1b25871b
16 changed files with 176 additions and 7 deletions

View File

@ -11,4 +11,8 @@ class AdminPanelController extends Controller
return view('layouts.adminpanel');
}
public function test_connection()
{
return view('admin.admin_testconnection');
}
}

View File

@ -2,11 +2,14 @@
namespace App\Http\Controllers\User;
use App\Attendance;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Subject;
use App\Classes;
use App\Room;
use Illuminate\Support\Facades\Auth;
class UserClassesController extends Controller
@ -43,4 +46,33 @@ class UserClassesController extends Controller
Classes::find($classes_id);
return redirect(route('user_classes'));
}
public function start_classes($classes_id)
{
$classes_code = generateRandomString(10);
session([
'CLASSES_CODE' => $classes_code,
'CLASSES_ID' => $classes_id
]);
return view('user.user_classes_start', ['verified' => false, 'classes_code' => $classes_code]);
}
public function start_classes_verified(Request $request)
{
$classes = Classes::find($request->get('classes_id'));
$room = Room::find(Subject::find($classes->subject_id)->room_id)->name;
$student_id_number = $request->input('student_id_number');
$student_name = $request->input('student_name');
$student_surname = $request->input('student_surname');
$seat_number = $request->input('seat_number');
Attendance::create([
'classes_id' => $classes->id,
'student_id_number' => $student_id_number,
'student_name' => $student_name,
'student_surname' => $student_surname,
'seat_number' => $seat_number,
]);
$attendances = Attendance::where('classes_id', $classes->id)->get();
return view('user.user_classes_start', ['verified' => true, 'room' => $room, 'attendances' => $attendances]);
}
}

View File

@ -61,6 +61,7 @@ class Kernel extends HttpKernel
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'classesCode' => \App\Http\Middleware\CheckClassesCode::class,
];
/**

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Middleware;
use App\Classes;
use App\Subject;
use Illuminate\Support\Facades\Auth;
use Closure;
class CheckClassesCode
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$classes_code_from_request = $request->input('classes_code');
$classes_code_from_session = session()->get('CLASSES_CODE');
$classes_id = session()->get('CLASSES_ID');
$classes = Classes::find($classes_id);
if(!$classes) {
return redirect('home');
} else {
if ($classes_code_from_request != $classes_code_from_session) {
return redirect('home');
} else {
$user_id = Subject::where('id', $classes->subject_id)->first()->user_id;
if (!$user_id || $user_id != Auth::id()) {
return redirect('home');
}
}
}
$request->attributes->add(['classes_id' => $classes->id]);
return $next($request);
}
}

View File

@ -19,6 +19,6 @@ class VerifyCsrfToken extends Middleware
* @var array
*/
protected $except = [
//
'user/classes/start'
];
}

10
app/helpers.php Normal file
View File

@ -0,0 +1,10 @@
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

View File

@ -38,6 +38,11 @@
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
},
"autoload-dev": {

View File

@ -14,11 +14,13 @@ class ClassesTableSeeder extends Seeder
DB::table('classes')->insert([
'subject_id' => 1,
'date' => '2019-12-02',
'code' => generateRandomString(10)
]);
DB::table('classes')->insert([
'subject_id' => 2,
'date' => '2019-12-02',
'code' => generateRandomString(10)
]);
}
}

View File

@ -0,0 +1,28 @@
@extends('layouts.adminpanel')
@section('title') Admin Panel - Users @endsection
@section('admin_content')
<div class="row justify-content-center">
<div class="col-md-12">
<form method="POST" action="{{ route('user_start_classes_verified') }}">
<label for="classes_code"> Kod: </label>
<input type="text" name="classes_code" id="classes_code">
<label for="student_id_number"> Student ID: </label>
<input type="text" name="student_id_number" id="student_id_number">
<label for="student_name"> Student name: </label>
<input type="text" name="student_name" id="student_name">
<label for="student_surname"> Student surname: </label>
<input type="text" name="student_surname" id="student_surname">
<label for="seat_number"> Seat number: </label>
<input type="text" name="seat_number" id="seat_number">
<button type="submit"> Test connection </button>
</form>
</div>
</div>
@endsection

View File

@ -16,9 +16,9 @@
@endif
</div>
<div class="card-body">
<a href="{{ route('user_subjects', ['weekday']) }}" class="btn btn-primary"> My subjects </a>
<a href="{{ route('user_classes', ['subject_id']) }}" class="btn btn-primary"> My classes </a>
<a href="{{ route('user_attendances', ['classes_id']) }}" class="btn btn-primary"> Attendance </a>
<a href="{{ route('user_subjects') }}" class="btn btn-primary"> My subjects </a>
<a href="{{ route('user_classes') }}" class="btn btn-primary"> My classes </a>
<a href="{{ route('user_attendances') }}" class="btn btn-primary"> Attendance </a>
</div>
<div class="card-body">
@yield('user_content')

View File

@ -24,6 +24,7 @@
<a href="{{ route('admin_rooms') }}" class="btn btn-primary"> Rooms </a>
<a href="{{ route('admin_classes') }}" class="btn btn-primary"> Classes </a>
<a href="{{ route('admin_attendances') }}" class="btn btn-primary"> Attendances </a>
<a href="{{ route('admin_test_connection') }}" class="btn btn-primary"> Test connection </a>
</div>
<div class="card-body">
@yield('admin_content')

View File

@ -7,7 +7,7 @@
<div class="col-md-12">
<div class="card-body">
<h4 class="card-header"> Add new attendance record </h4>
<form method="POST" action="{{ route('user_add_attendance', [Auth::user()->id]) }}" class="col-md-12">
<form method="POST" action="{{ route('user_add_attendance') }}" class="col-md-12">
@csrf
<div class="form-group row">

View File

@ -7,7 +7,7 @@
<div class="col-md-12">
<div class="card-body">
<h4 class="card-header"> Add new classes </h4>
<form method="POST" action="{{ route('user_add_classes', [Auth::user()->id]) }}">
<form method="POST" action="{{ route('user_add_classes') }}">
@csrf
<div class="form-group row">
@ -78,12 +78,16 @@
<th> Date </th>
<th></th>
<th></th>
<th></th>
</tr>
@foreach ($classes_list as $classes_item)
<tr>
<td> {{ App\Subject::find($classes_item->subject_id)->name }}, {{ App\Subject::find($classes_item->subject_id)->type }},
{{ App\Subject::find($classes_item->subject_id)->weekday }} {{ App\Subject::find($classes_item->subject_id)->time }}</td>
<td> {{ $classes_item->date }} </td>
<td>
<a href="{{ route('user_start_classes', [$classes_item->id]) }}" name="start-classes-btn" class="btn btn-warning"> Rozpocznij zapisy </a>
</td>
<td>
<a href="{{ route('user_delete_classes', [$classes_item->id]) }}" name="delete-classes-btn" class="btn btn-danger"> Delete </a>
</td>

View File

@ -0,0 +1,36 @@
@extends('home')
@section('title') My Panel - Classes @endsection
@section('user_content')
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card-body">
@if(!$verified)
<p>
Wprowadź poniższy kod do programu:
</p>
<p> {{ $classes_code }}</p>
@else
<p> Połączono prawidłowo.</p>
<p> Zajęcia odbywają się w sali {{ $room }}.</p>
@if($attendances)
<p> Aktualna liczba studentów w sali: {{ $attendances->count() }}</p>
<br><br>
@foreach($attendances as $attendance)
<p> Student ID number: {{ $attendance->student_id_number }}</p>
<p> Student name: {{ $attendance->student_name }}</p>
<p> Student surname: {{ $attendance->student_surname }}</p>
<p> Seat number: {{ $attendance->seat_number }}</p>
<br><br>
@endforeach
@else
<p> Aktualna liczba studentów w sali: 0</p>
<br><br>
@endif
@endif
</div>
</div>
</div>
@endsection

View File

@ -7,7 +7,7 @@
<div class="col-md-12">
<div class="card-body">
<h4 class="card-header"> Add new subject </h4>
<form method="POST" action="{{ route('user_add_subject', [Auth::user()->id]) }}">
<form method="POST" action="{{ route('user_add_subject') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

View File

@ -25,6 +25,7 @@ Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function()
Route::get('/rooms', 'AdminRoomsController@index')->name('admin_rooms');
Route::get('/classes', 'AdminClassesController@index')->name('admin_classes');
Route::get('/attendances', 'AdminAttendancesController@index')->name('admin_attendances');
Route::get('/test', 'AdminPanelController@test_connection')->name('admin_test_connection');
Route::group(array('prefix' => 'add'), function() {
Route::post('/user', 'AdminUsersController@add_user')->name('admin_add_user');
@ -54,6 +55,9 @@ Route::group(array('prefix' => 'user', 'namespace' => 'User'), function() { //TO
Route::get('/classes/{groupBy?}', 'UserClassesController@index')->name('user_classes');
Route::get('/attendance/{groupBy?}', 'UserAttendancesController@index')->name('user_attendances');
Route::get('/classes/start/{classes_id}', 'UserClassesController@start_classes')->name('user_start_classes');
Route::post('/classes/start', 'UserClassesController@start_classes_verified')->name('user_start_classes_verified')->middleware('classesCode');
Route::group(array('prefix' => 'add'), function() {
Route::post('/subject', 'UserSubjectsController@add_subject')->name('user_add_subject');
Route::post('/classes', 'UserClassesController@add_classes')->name('user_add_classes');
@ -71,6 +75,7 @@ Route::group(array('prefix' => 'user', 'namespace' => 'User'), function() { //TO
Route::get('/classes/{classes_id}/{groupBy?}', 'UserClassesController@edit_classes')->name('user_edit_classes');
Route::get('/attendance/{attendance_id}/{groupBy?}', 'UserAttendancesController@edit_attendance')->name('user_edit_attendance');
});
});