2019-12-01 23:36:12 +01:00
< ? php
namespace App\Http\Controllers\User ;
2019-12-04 16:55:59 +01:00
use App\Attendance ;
2019-12-01 23:36:12 +01:00
use App\Http\Controllers\Controller ;
2019-12-16 15:45:28 +01:00
use App\Http\Controllers\UserCache ;
2019-12-01 23:36:12 +01:00
use Illuminate\Http\Request ;
use App\Subject ;
use App\Classes ;
2019-12-09 12:17:23 +01:00
use App\Room ;
2019-12-04 16:55:59 +01:00
2019-12-04 10:21:58 +01:00
use Illuminate\Support\Facades\Auth ;
2020-03-01 23:30:37 +01:00
use Illuminate\Support\Facades\Validator ;
use Illuminate\Validation\Rule ;
2019-12-01 23:36:12 +01:00
2020-01-18 18:15:35 +01:00
class UserClassesController extends Controller {
2020-03-01 23:30:37 +01:00
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 );
}
2020-01-18 18:15:35 +01:00
public function index ( $groupBy = 'subject_id' ) {
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 ();
2020-01-18 18:15:35 +01:00
$classes = Classes :: whereIn ( 'subject_id' , $subjects_ids ) -> orderBy ( 'created_at' , 'DESC' ) -> get ();
foreach ( $classes as $classes_item ) {
2019-12-07 22:16:05 +01:00
$current_date = date ( 'Y-m-d H:i:s' );
2019-12-13 23:28:16 +01:00
$classes_date = $classes_item -> created_at ;
2019-12-07 22:16:05 +01:00
$hours_difference = checkHoursDifference ( $classes_date , $current_date );
2020-01-18 18:15:35 +01:00
if ( $hours_difference > 1.5 ) {
2019-12-07 22:16:05 +01:00
$classes_item -> classes_code = null ;
$classes_item -> save ();
$classes_item -> refresh ();
}
}
2019-12-01 23:36:12 +01:00
$classes_grouped = $classes -> groupBy ( $groupBy );
2019-12-06 22:34:25 +01:00
$defaultDate = date ( " Y-m-d " );
return view ( 'user.user_classes' , [ 'classes' => $classes , 'classes_grouped' => $classes_grouped , 'subjects' => $subjects , 'grouped_by' => $groupBy , 'defaultDate' => $defaultDate ]);
2019-12-01 23:36:12 +01:00
}
2020-01-18 18:15:35 +01:00
public function add_classes ( Request $request ) {
2020-03-01 23:30:37 +01:00
$validator = $this -> validator ( $request -> all ());
if ( $validator -> fails ()) {
return redirect () -> back ()
-> withErrors ( $validator )
-> withInput ();
}
2019-12-01 23:36:12 +01:00
$subject_id = $request -> input ( 'subject_id' );
$date = $request -> input ( 'date' );
2020-01-17 17:10:54 +01:00
$mode = $request -> input ( 'mode' );
2020-01-18 18:15:35 +01:00
$classes_id = Classes :: create ([
2019-12-01 23:36:12 +01:00
'subject_id' => $subject_id ,
2019-12-20 15:46:32 +01:00
'date' => $date ,
2020-01-17 17:10:54 +01:00
'mode' => $mode
2019-12-08 00:00:52 +01:00
]) -> id ;
2019-12-06 22:34:25 +01:00
return redirect ( route ( 'user_start_classes' , [ 'classes_id' => $classes_id ]));
2019-12-01 23:36:12 +01:00
}
2020-01-18 18:15:35 +01:00
public function delete_classes ( $classes_id ) {
2019-12-01 23:36:12 +01:00
Classes :: find ( $classes_id ) -> delete ();
2019-12-19 00:10:39 +01:00
return redirect () -> back ();
2019-12-01 23:36:12 +01:00
}
2020-01-18 18:15:35 +01:00
public function edit_classes ( $classes_id ) {
2019-12-01 23:36:12 +01:00
Classes :: find ( $classes_id );
2019-12-19 00:10:39 +01:00
return redirect () -> back ();
2019-12-01 23:36:12 +01:00
}
2019-12-04 16:55:59 +01:00
2020-01-18 18:15:35 +01:00
public function start_classes ( $classes_id ) {
2019-12-06 22:34:25 +01:00
$classes = Classes :: find ( $classes_id );
2019-12-07 19:49:42 +01:00
$classes_code = $classes -> classes_code ;
2020-01-18 18:15:35 +01:00
if ( ! $classes_code ) {
2019-12-07 19:49:42 +01:00
$classes_code = generateRandomString ( 10 );
$classes -> classes_code = $classes_code ;
$classes -> save ();
2019-12-07 22:16:05 +01:00
$classes -> refresh ();
2019-12-07 19:49:42 +01:00
}
2019-12-16 15:45:28 +01:00
2019-12-07 19:49:42 +01:00
return view ( 'map.start_map' , [ 'classes_code' => $classes_code , 'classes' => $classes ]);
2019-12-04 16:55:59 +01:00
}
2020-01-18 18:15:35 +01:00
public function start_classes_verified ( Request $request ) {
2020-03-01 23:30:37 +01:00
$validator = $this -> student_validator ( $request -> all ());
if ( $validator -> fails ()) {
return redirect () -> back ()
-> withErrors ( $validator )
-> withInput ();
}
2019-12-07 19:49:42 +01:00
$student_id_number = $request -> input ( 'student_id_number' );
$student_name = $request -> input ( 'student_name' );
$student_surname = $request -> input ( 'student_surname' );
2019-12-04 16:55:59 +01:00
$classes = Classes :: find ( $request -> get ( 'classes_id' ));
2020-01-17 23:29:28 +01:00
$subject = Subject :: find ( $classes -> subject_id );
$room_capacity = Room :: find ( $subject -> room_id ) -> capacity ;
$attendances = Attendance :: where ( 'classes_id' , $classes -> id ) -> get ();
$student_ids = $attendances -> pluck ( 'student_id_number' ) -> toArray ();
$seat_numbers = $attendances -> pluck ( 'seat_number' ) -> toArray ();
2020-01-18 18:15:35 +01:00
if ( in_array ( $student_id_number , $student_ids )) { // if student attendance record already exists
2020-01-17 23:29:28 +01:00
$student_seat_number = Attendance :: where ( 'student_id_number' , $student_id_number ) -> first () -> seat_number ;
$warning = 'Ten numer indeksu został już wcześniej zapisany na te zajęcia.' ;
return view ( 'map.summary_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => $student_seat_number , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'mode' => $classes -> mode , 'warning' => $warning ]);
2019-12-19 15:23:49 +01:00
} else {
2020-01-17 23:29:28 +01:00
if ( count ( $seat_numbers ) >= $room_capacity ) { // if there are no more free seats in room, add student without assigning seat number (regardless of the classes mode)
Attendance :: create ([
'classes_id' => $classes -> id ,
'student_id_number' => $student_id_number ,
'student_name' => $student_name ,
'student_surname' => $student_surname ,
'seat_number' => null
]);
$warning = " Zostałeś zapisany na listę obecności, ale bez przyznanego miejsca. " ;
return view ( 'map.summary_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => null , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'mode' => $classes -> mode , 'warning' => $warning ]);
2020-01-17 17:10:54 +01:00
} else {
2020-01-17 23:29:28 +01:00
if ( $classes -> mode == 'quick' ) {
Attendance :: create ([
'classes_id' => $classes -> id ,
'student_id_number' => $student_id_number ,
'student_name' => $student_name ,
'student_surname' => $student_surname ,
'seat_number' => null
]);
return view ( 'map.summary_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => null , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'mode' => $classes -> mode , 'warning' => null ]);
} else {
// prepare seat map
$room_arrangement = Room :: find ( $subject -> room_id ) -> arrangement ;
if ( ! $room_arrangement ) {
$room_arrangement = Room :: where ( 'name' , 'Inna sala' ) -> first () -> arrangement ;
}
$room_rows = count ( explode ( " ++ " , $room_arrangement ));
if ( $room_rows > 4 ) { //max 4 rows on one page
$multi_parts = true ;
$parts_number = ceil ( $room_rows / 4 );
} else {
$multi_parts = false ;
$parts_number = 1 ;
}
if ( $classes -> mode == 'test' ) {
$random_seat = rand ( 1 , $room_capacity );
while ( in_array ( $random_seat , $seat_numbers )) {
$random_seat = rand ( 1 , $room_capacity );
}
return view ( 'map.seat_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => $random_seat , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'seat_numbers' => $seat_numbers , 'room_arrangement' => $room_arrangement , 'multi_parts' => $multi_parts , 'parts_number' => $parts_number , 'mode' => 'test' , 'random_seat' => $random_seat ]);
} else {
return view ( 'map.seat_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'seat_numbers' => $seat_numbers , 'room_arrangement' => $room_arrangement , 'multi_parts' => $multi_parts , 'parts_number' => $parts_number , 'mode' => null ]);
}
2020-01-17 17:10:54 +01:00
}
}
2019-12-19 15:23:49 +01:00
}
2019-12-07 19:49:42 +01:00
}
2020-01-18 18:15:35 +01:00
public function save_classes_data ( Request $request ) {
2019-12-07 19:49:42 +01:00
$classes_id = $request -> input ( 'classes_id' );
2019-12-04 16:55:59 +01:00
$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' );
2020-01-20 12:05:33 +01:00
$classes = Classes :: find ( $request -> get ( 'classes_id' ));
$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
$student_seat_number = Attendance :: where ( 'student_id_number' , $student_id_number ) -> first () -> seat_number ;
$warning = 'Ten numer indeksu został już wcześniej zapisany na te zajęcia.' ;
return view ( 'map.summary_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => $student_seat_number , 'student_id_number' => $student_id_number , 'classes_id' => $classes -> id , 'mode' => $classes -> mode , 'warning' => $warning ]);
} else {
Attendance :: create ([
'classes_id' => $classes_id ,
'student_id_number' => $student_id_number ,
'student_name' => $student_name ,
'student_surname' => $student_surname ,
'seat_number' => $seat_number
]);
return view ( 'map.summary_map' , [ 'student_name' => $student_name , 'student_surname' => $student_surname , 'seat_number' => $seat_number , 'student_id_number' => $student_id_number , 'classes_id' => $classes_id , 'mode' => null , 'warning' => null ]);
}
2019-12-04 16:55:59 +01:00
}
2019-12-13 23:28:16 +01:00
2020-01-18 18:15:35 +01:00
public function preview_classes ( $classes_id , $orderBy = 'student_surname' , $orderDirection = 'ASC' ) {
if ( ! $classes_id == 0 ) {
2019-12-13 23:28:16 +01:00
$classes = Classes :: find ( $classes_id );
2020-01-18 18:15:35 +01:00
$attendances = Attendance :: where ( 'classes_id' , $classes -> id ) -> orderBy ( $orderBy , $orderDirection ) -> get ();
2019-12-14 22:03:41 +01:00
$seat_numbers = $attendances -> pluck ( 'seat_number' ) -> toArray ();
2019-12-13 23:28:16 +01:00
$subject = Subject :: find ( $classes -> subject_id );
$room_arrangement = Room :: find ( $subject -> room_id ) -> arrangement ;
2020-01-18 18:15:35 +01:00
if ( ! $room_arrangement ) {
2019-12-13 23:28:16 +01:00
$room_arrangement = Room :: where ( 'name' , 'Inna sala' ) -> first () -> arrangement ;
}
2019-12-20 23:21:31 +01:00
$room_rows = count ( explode ( " ++ " , $room_arrangement ));
2020-01-18 18:15:35 +01:00
if ( $room_rows > 4 ) { //max 4 rows on one page
2019-12-20 23:21:31 +01:00
$multi_parts = true ;
2020-01-18 18:15:35 +01:00
$parts_number = ceil ( $room_rows / 4 );
2019-12-19 15:23:49 +01:00
} else {
2019-12-20 23:21:31 +01:00
$multi_parts = false ;
2019-12-19 15:23:49 +01:00
}
2020-01-18 18:15:35 +01:00
return view ( 'user.user_preview_classes' , [ 'classes_id' => $classes_id , 'room_arrangement' => $room_arrangement , 'attendances' => $attendances , 'seat_numbers' => $seat_numbers , 'orderBy' => $orderBy , 'orderDirection' => $orderDirection , 'multi_parts' => $multi_parts , 'parts_number' => $parts_number ]);
2020-01-17 17:10:54 +01:00
2019-12-13 23:28:16 +01:00
}
}
2019-12-01 23:36:12 +01:00
}