editing subject fixed, form validators provided

This commit is contained in:
s416422 2020-03-01 23:30:37 +01:00
parent e46e8ce3d2
commit 32b589a95d
12 changed files with 372 additions and 129 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Exports\AttendanceExportView;
use App\Exports\AttendanceGroupedExportView;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use App\Subject;
@ -15,6 +16,26 @@ use App\Attendance;
class UserAttendancesController extends Controller
{
protected function validator(array $data)
{
$messages = [
'student_id.required' => 'Numer indeksu jest wymagany.',
'student_name.required' => 'Imię jest wymagane.',
'student_surname.required' => 'Nazwisko jest wymagane.',
'classes_id.required' => 'Wskazanie zajęć jest wymagane.',
'classes_id.exists' => 'Niepoprawne zajęcia.',
];
$rules = [
'classes_id' => ['required', 'exists:classes,id'],
'student_id' => ['required'],
'student_name' => ['required'],
'student_surname' => ['required'],
];
return Validator::make($data, $rules, $messages);
}
public function index($groupBy='classes_id')
{
$user_id = Auth::id();
@ -29,12 +50,25 @@ class UserAttendancesController extends Controller
public function add_attendance(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$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');
$attendances = Attendance::where('classes_id', $classes_id)->get();
$student_ids = $attendances->pluck('student_id_number')->toArray();
if (in_array($student_id_number, $student_ids)) { // if student attendance record already exists
return redirect()->back()
->withErrors(['Ten numer indeksu został już wcześniej zapisany na te zajęcia.']);
}
Attendance::create([
'classes_id' => $classes_id,
'student_id_number' => $student_id_number,

View File

@ -12,9 +12,45 @@ use App\Classes;
use App\Room;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class UserClassesController extends Controller {
protected function validator(array $data)
{
$messages = [
'subject_id.required' => 'Wskazanie przedmiotu jest wymagane.',
'subject_id.exists' => 'Wybrany przedmiot jest niepoprawny.',
'date.required' => 'Data jest wymagana.',
'date.date' => 'Niepoprawny format daty.',
'mode.in' => 'Tryb zajęć jest niepoprawny.'
];
$rules = [
'subject_id' => ['required', 'exists:subjects,id'],
'date' => ['required', 'date'],
'mode' => Rule::in([null, 'test', 'quick'])
];
return Validator::make($data, $rules, $messages);
}
protected function student_validator(array $data)
{
$messages = [
'student_id_number.required' => 'Numer indeksu jest wymagany.',
'student_name.required' => 'Imię jest wymagane.',
'student_surname.required' => 'Nazwisko jest wymagane.',
];
$rules = [
'student_id_number' => ['required'],
'student_name' => ['required'],
'student_surname' => ['required'],
];
return Validator::make($data, $rules, $messages);
}
public function index($groupBy = 'subject_id') {
$user_id = Auth::id();
$subjects = Subject::where('user_id', $user_id)->get();
@ -36,6 +72,12 @@ class UserClassesController extends Controller {
}
public function add_classes(Request $request) {
$validator = $this->validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$subject_id = $request->input('subject_id');
$date = $request->input('date');
$mode = $request->input('mode');
@ -71,6 +113,12 @@ class UserClassesController extends Controller {
}
public function start_classes_verified(Request $request) {
$validator = $this->student_validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$student_id_number = $request->input('student_id_number');
$student_name = $request->input('student_name');
$student_surname = $request->input('student_surname');

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@ -11,6 +12,38 @@ use App\Room;
class UserSubjectsController extends Controller
{
protected function validator(array $data)
{
$messages = [
'name.required' => 'Nazwa przedmiotu jest wymagana.',
'name.unique' => 'Podana nazwa przedmiotu już istnieje w systemie.',
'room_id.required' => 'Sala jest wymagana.',
'room_id.exists' => 'Niepoprawny numer sali.'
];
$rules = [
'name' => ['required', 'unique:subjects,name'],
'room_id' => ['required', 'exists:rooms,id'],
];
return Validator::make($data, $rules, $messages);
}
protected function edited_validator(array $data)
{
$messages = [
'name_e.required' => 'Nazwa przedmiotu jest wymagana.',
'name_e.unique' => 'Podana nazwa przedmiotu już istnieje w systemie.',
'room_id_e.required' => 'Sala jest wymagana.',
'room_id_e.exists' => 'Niepoprawny numer sali.'
];
$rules = [
'name_e' => ['required', 'unique:subjects,name'],
'room_id_e' => ['required', 'exists:rooms,id'],
];
return Validator::make($data, $rules, $messages);
}
public function index($groupBy = 'weekday') {
$user_id = Auth::id();
$subjects = Subject::where('user_id', $user_id)->orderBy('name','ASC')->get();
@ -27,19 +60,24 @@ class UserSubjectsController extends Controller
}
public function add_subject(Request $request) {
$validator = $this->validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$name = $request->input('name');
// $type = $request->input('type');
$weekday = $request->input('weekday');
$time = $request->input('time');
$room_id = $request->input('room_id');
$user_id_n = Auth::id();
$user_id = Auth::id();
Subject::create([
'name' => $name,
// 'type' => $type,
'weekday'=> $weekday,
'time' => $time,
'room_id' => $room_id,
'user_id' => $user_id_n
'user_id' => $user_id
]);
return redirect()->back();
}
@ -50,9 +88,25 @@ class UserSubjectsController extends Controller
return redirect()->back();
}
public function edit_subject($subject_id)
public function edit_subject(Request $request)
{
Subject::find($subject_id);
$subject_id = $request->input('subject_id');
$subject = Subject::find($subject_id);
if($subject) {
$validator = $this->edited_validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput()
->with('subject_id_redirected', $subject_id);
}
$subject->name = $request->input('name_e');
$subject->weekday = $request->input('weekday_e');
$subject->time = $request->input('time_e');
$subject->room_id = $request->input('room_id_e');
$subject->save();
$subject->refresh();
}
return redirect()->back();
}
}

View File

@ -7,6 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class Room extends Model
{
protected $fillable = [
'name', 'capacity', 'type', 'arrangement'
'name', 'capacity', 'arrangement'
];
}

View File

@ -7,6 +7,6 @@ use Illuminate\Database\Eloquent\Model;
class Subject extends Model
{
protected $fillable = [
'name', 'type', 'weekday', 'time', 'room_id', 'user_id'
'name', 'weekday', 'time', 'room_id', 'user_id'
];
}

View File

@ -15,8 +15,7 @@ class CreateSubjectsTable extends Migration
{
Schema::create('subjects', function (Blueprint $table) {
$table->bigIncrements('id')->unique();
$table->string('name');
// $table->string('type')->nullable();
$table->string('name')->unique();
$table->string('weekday')->nullable();
$table->string('time')->nullable();
$table->integer('room_id');

View File

@ -3,7 +3,6 @@ $(document).ready(function(){
$('.card-reader').toggleClass('open');
});
$('.add-subject-btn').on('click', function () {
$('.add-subject').toggleClass('open');
if ($('.add-subject').first().hasClass('open')) {
@ -36,6 +35,26 @@ $(document).ready(function(){
$(this).find(`#note_content-${id}`).first().val(originalText);
});
$('.edit-subject-btn').on('click', function () {
$(this).parents('tr.editable').next().toggleClass('d-none');
$(this).parents('tr.editable').toggleClass('d-none');
});
$('.edit-subject-btn-cancel').on('click', function () {
$(this).parents('tr.edit-subject-tr').prev().toggleClass('d-none');
$(this).parents('tr.edit-subject-tr').toggleClass('d-none');
});
if($('body').find('.invalid-feedback-alert').length > 0) {
if($('.add-subject').find('.invalid-feedback').length > 0) {
$('.add-subject-btn').first().click();
}
if($('.edit-subject-tr').find('.invalid-feedback').length > 0) {
let subject_id = $('#subject_id_redirected').val();
$(`#tr-editable-${subject_id}`).find('.edit-subject-btn').first().click();
}
}
$('#sort-select').on('change', function() {
$(location).attr('href', $(this).val())
});

View File

@ -14,6 +14,15 @@
<div id="refresh"></div>
<p class="code-p"><b>Kod:</b> <span id="classes_code_input">{{ $classes_code }}</span></p>
<h1 class="main-text main-seat-text">Przyłóż legitymację do czytnika</h1>
@if ($errors->any())
<div class="alert alert-danger invalid-feedback-alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="card-reader">
<form method="POST" action="{{ route('user_start_classes_verified') }}" class="card-reader-form">
<div>

View File

@ -12,12 +12,19 @@
<button type="button" class="btn btn-primary add-attendance-btn" title="Dodaj nowy wpis"> <i class="fa fa-plus"></i> Dodaj nowy wpis </button>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger invalid-feedback-alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="card-body add-attendance">
<h5> Dodaj obecność </h5>
<form method="POST" action="{{ route('user_add_attendance') }}" class="col-md-12">
@csrf
<div class="form-group row">
<label for="classes_id" class="col-md-4 col-form-label text-md-right">{{ __('Zajęcia') }}</label>
@ -84,7 +91,7 @@
<label for="seat_number" class="col-md-4 col-form-label text-md-right">{{ __('Nr miejsca') }}</label>
<div class="col-md-6">
<input id="seat_number" type="number" class="form-control @error('seat_number') is-invalid @enderror" name="seat_number" value="{{ old('seat_number') }}" required autocomplete="seat_number">
<input id="seat_number" type="number" class="form-control @error('seat_number') is-invalid @enderror" name="seat_number" value="{{ old('seat_number') }}" autocomplete="seat_number">
@error('seat_number')
<span class="invalid-feedback" role="alert">

View File

@ -8,10 +8,18 @@
<div class="card-header custom-header">
<h4> Sprawdź obecność </h4>
</div>
@if ($errors->any())
<div class="alert alert-danger invalid-feedback-alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="card-body">
<form method="POST" action="{{ route('user_add_classes') }}" class="classes-form">
@csrf
<div class="form-group row">
<label for="subject_id" class="col-md-4 col-form-label text-md-right">{{ __('Przedmiot') }}</label>
@ -49,11 +57,11 @@
<div class="mode-div">
<div>
<label for="test-mode" class="form-check-label">{{ __('Tryb egzaminu') }}</label>
<input id="test-mode" type="checkbox" class="form-control" name="mode" value="test">
<input id="test-mode" type="checkbox" class="form-control @error('mode') is-invalid @enderror" name="mode" value="test">
</div>
<div>
<label for="quick-mode" class=" form-check-label">{{ __('Tryb szybki') }}</label>
<input id="quick-mode" type="checkbox" class="form-control" name="mode" value="quick">
<input id="quick-mode" type="checkbox" class="form-control @error('mode') is-invalid @enderror" name="mode" value="quick">
</div>
</div>
</div>

View File

@ -9,7 +9,16 @@
<h4> Moje przedmioty <span class="badge badge-secondary" title="Liczba przedmiotów"> {{ $subjects->count() }} </span></h4>
<button type="button" class="btn btn-primary add-subject-btn" title="Dodaj nowy przedmiot"> <i class="fa fa-plus"></i> Dodaj nowy przedmiot </button>
</div>
@if ($errors->any())
<div class="alert alert-danger invalid-feedback-alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
<input type="hidden" id="subject_id_redirected" value="{{ Illuminate\Support\Facades\Session::get('subject_id_redirected') }}">
@endif
<div class="card-body add-subject">
<h5> Dodaj nowy przedmiot </h5>
<form method="POST" action="{{ route('user_add_subject') }}">
@ -33,7 +42,7 @@
<div class="col-md-6">
<select id="weekday" class="form-control @error('weekday') is-invalid @enderror" name="weekday" required>
<select id="weekday" class="form-control @error('weekday') is-invalid @enderror" name="weekday">
<option label="-- wybierz dzień tygodnia -- "></option>
@foreach ($weekdays as $weekday)
@if ($weekday == $defaultWeekday)
@ -57,7 +66,7 @@
<label for="time" class="col-md-4 col-form-label text-md-right">{{ __('Godzina') }}</label>
<div class="col-md-6">
<input id="time" type="time" class="form-control @error('time') is-invalid @enderror" name="time" value="{{ $defaultTime }}" required autocomplete="time">
<input id="time" type="time" class="form-control @error('time') is-invalid @enderror" name="time" value="{{ $defaultTime }}" autocomplete="time">
@error('time')
<span class="invalid-feedback" role="alert">
@ -85,20 +94,6 @@
</div>
</div>
<div class="form-group row">
<label for="user_id" class="col-md-4 col-form-label text-md-right">{{ __('Prowadzący') }}</label>
<div class="col-md-6">
<input id="user_id" type="text" class="@error('user_id') is-invalid @enderror form-control-plaintext" name="user_id" value="{{ Auth::user()->name }} {{ Auth::user()->surname }}" required readonly>
@error('user_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary" title="Dodaj nowy przedmiot">
@ -139,23 +134,90 @@
<th> Nazwa </th>
<th> Dzień tygodnia </th>
<th> Godzina </th>
<th> Prowadzący </th>
<th> Sala </th>
<th></th>
<th></th>
</tr>
@foreach ($subjects_list as $subject)
<tr>
<td> {{ $subject->name }} </td>
<td> {{ $subject->weekday }} </td>
<td> {{ $subject->time }} </td>
<td> {{ App\User::find($subject->user_id)->name }} {{ App\User::find($subject->user_id)->surname }}</td>
<td> {{ App\Room::find($subject->room_id)->name }} </td>
<tr class="editable" id="tr-editable-{{ $subject->id }}">
<td class="editable-name"> {{ $subject->name }} </td>
<td class="editable-weekday"> {{ $subject->weekday }} </td>
<td class="editable-time"> {{ $subject->time }} </td>
<td class="editable-room"> {{ App\Room::find($subject->room_id)->name }} </td>
<td>
<button type="button" name="edit-subject-btn" class="btn btn-secondary edit-subject-btn" title="Edytuj przedmiot"> <i class="fa fa-pencil"></i> Edytuj </button>
</td>
<td>
<button type="button" name="delete-subject-btn" class="btn btn-danger" data-toggle="modal" data-target="#subjectConfirmationModal-{{ $subject->id }}" title="Usuń przedmiot"> <i class="fa fa-trash-o"></i> Usuń </button>
</td>
</tr>
<tr class="edit-subject-tr d-none table-light p-0" id="tr-editing-{{ $subject->id }}">
<td colspan="7" class="p-0 table-light">
<form method="POST" action="{{ route('user_edit_subject') }}">
@csrf
<input type="hidden" name="subject_id" value="{{ $subject->id }}">
<div class="table-responsive">
<table class="table custom-table m-0">
<tr class="table-light">
<td title="Nazwa przedmiotu">
<input type="text" class="form-control @error('name_e') is-invalid @enderror edit-name-input" name="name_e" value="{{ $subject->name }}" autocomplete="name_e" required>
@error('name_e')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</td>
<td title="Dzień tygodnia">
<select class="form-control @error('weekday_e') is-invalid @enderror edit-weekday-input" name="weekday_e">
<option label="-- wybierz -- "></option>
@foreach ($weekdays as $weekday)
@if ($weekday == $subject->weekday)
<option value="{{ $weekday }}" selected="selected">{{ $weekday }}</option>
@else
<option value="{{ $weekday }}">{{ $weekday }}</option>
@endif
@endforeach
</select>
@error('weekday_e')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</td>
<td title="Godzina">
<input type="time_e" class="form-control @error('time_e') is-invalid @enderror edit-time-input" name="time_e" value="{{ $subject->time }}" autocomplete="time_e">
@error('time_e')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</td>
<td title="Sala">
<select class="form-control @error('room_id_e') is-invalid @enderror edit-room-input" name="room_id_e" required>
@foreach ($rooms as $room)
@if ($room->id == $subject->room_id)
<option value="{{ $room->id }}" selected="selected">{{ $room->name }}</option>
@else
<option value="{{ $room->id }}">{{ $room->name }}</option>
@endif
@endforeach
</select>
@error('room_id_e')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</td>
<td>
<a href="{{ route('user_edit_subject', [$subject->id]) }}" name="edit-subject-btn" class="btn btn-secondary" title="Edytuj przedmiot"> <i class="fa fa-pencil"></i> Edytuj </a>
<button type="button" name="edit-subject-btn-cancel" class="btn btn-secondary edit-subject-btn-cancel" title="Anuluj zmiany"> <i class="fa fa-times"></i> Anuluj </button>
</td>
<td>
<button type="submit" name="edit-subject-btn-save" class="btn btn-primary edit-subject-btn-save" title="Zapisz zmiany"> <i class="fa fa-pencil"></i> Zapisz </button>
</td>
</tr>
</table>
</div>
</form>
</td>
</tr>
@include('user.delete_confirmation_subject', [$subject])

View File

@ -18,8 +18,8 @@ Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function()
{
Route::group(['middleware' => 'auth'], function () {
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function() {
Route::get('/', 'AdminPanelController@index')->name('admin');
Route::get('/users', 'AdminUsersController@index')->name('admin_users');
Route::get('/subjects', 'AdminSubjectsController@index')->name('admin_subjects');
@ -49,8 +49,9 @@ Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function()
Route::get('/classes/{classes_id}', 'AdminClassesController@edit_classes')->name('admin_edit_classes');
Route::get('/attendance/{attendance_id}', 'AdminAttendancesController@edit_attendance')->name('admin_edit_attendance');
});
});
Route::group(array('prefix' => 'user', 'namespace' => 'User'), function() { //TODO poprawic {user_id}
});
Route::group(array('prefix' => 'user', 'namespace' => 'User'), function() { //TODO poprawic {user_id}
Route::get('/subjects/{groupBy?}', 'UserSubjectsController@index')->name('user_subjects');
Route::get('/classes/{groupBy?}', 'UserClassesController@index')->name('user_classes');
Route::get('/attendance/{groupBy?}', 'UserAttendancesController@index')->name('user_attendances');
@ -74,10 +75,12 @@ Route::group(array('prefix' => 'user', 'namespace' => 'User'), function() { //TO
});
Route::group(array('prefix' => 'edit'), function() {
Route::get('/subject/{subject_id}/{groupBy?}', 'UserSubjectsController@edit_subject')->name('user_edit_subject');
Route::get('/attendance/{attendance_id}/{groupBy?}', 'UserAttendancesController@edit_attendance')->name('user_edit_attendance');
Route::post('/subject', 'UserSubjectsController@edit_subject')->name('user_edit_subject');
// Route::get('/attendance/{attendance_id}/{groupBy?}', 'UserAttendancesController@edit_attendance')->name('user_edit_attendance');
});
Route::get('/export/attendances/{classes_id}', 'UserAttendancesController@export')->name('user_export');
Route::get('/export/attendances/grouped/{groupBy}', 'UserAttendancesController@export_grouped')->name('user_export_grouped');
});
});