198 lines
8.9 KiB
PHP
198 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Attendance;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\UserCache;
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Subject;
|
|
use App\Classes;
|
|
use App\Room;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UserClassesController extends Controller
|
|
{
|
|
public function index($groupBy='subject_id')
|
|
{
|
|
$user_id = Auth::id();
|
|
$subjects = Subject::where('user_id', $user_id)->get();
|
|
$subjects_ids = $subjects->pluck('id')->toArray();
|
|
$classes = Classes::whereIn('subject_id', $subjects_ids)->orderBy('created_at','DESC')->get();
|
|
foreach($classes as $classes_item) {
|
|
$current_date = date('Y-m-d H:i:s');
|
|
$classes_date = $classes_item->created_at;
|
|
$hours_difference = checkHoursDifference($classes_date, $current_date);
|
|
if ($hours_difference > 1.5 ) {
|
|
$classes_item->classes_code = null;
|
|
$classes_item->save();
|
|
$classes_item->refresh();
|
|
}
|
|
}
|
|
$classes_grouped = $classes->groupBy($groupBy);
|
|
$defaultDate = date("Y-m-d");
|
|
return view('user.user_classes', ['classes' => $classes, 'classes_grouped' => $classes_grouped, 'subjects' => $subjects, 'grouped_by' => $groupBy, 'defaultDate' => $defaultDate]);
|
|
}
|
|
|
|
public function add_classes(Request $request)
|
|
{
|
|
$subject_id = $request->input('subject_id');
|
|
$date = $request->input('date');
|
|
$test_mode = $request->input('test_mode');
|
|
$classes_id = Classes::create([
|
|
'subject_id' => $subject_id,
|
|
'date' => $date,
|
|
'test_mode' => $test_mode
|
|
])->id;
|
|
return redirect(route('user_start_classes', ['classes_id' => $classes_id]));
|
|
}
|
|
|
|
public function delete_classes($classes_id)
|
|
{
|
|
Classes::find($classes_id)->delete();
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function edit_classes($classes_id)
|
|
{
|
|
Classes::find($classes_id);
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function start_classes($classes_id)
|
|
{
|
|
$classes = Classes::find($classes_id);
|
|
$classes_code = $classes->classes_code;
|
|
if(!$classes_code){
|
|
$classes_code = generateRandomString(10);
|
|
$classes->classes_code = $classes_code;
|
|
$classes->save();
|
|
$classes->refresh();
|
|
}
|
|
|
|
$userCache = new UserCache();
|
|
$cache = $userCache->get($classes_code);
|
|
if($cache) {
|
|
$student_id_number = $cache->index;
|
|
$student_name = $cache->name;
|
|
$student_surname = $cache->surname;
|
|
$classes = DB::table("classes")->where("classes_code", $cache->classcode)->first();
|
|
$attendances = Attendance::where('classes_id', $classes->id)->get();
|
|
$seat_numbers = $attendances->pluck('seat_number')->toArray();
|
|
$subject = Subject::find($classes->subject_id);
|
|
|
|
if($classes->test_mode) {
|
|
$room_capacity = Room::find($subject->room_id)->capacity;
|
|
$random_seat = rand(1, $room_capacity);
|
|
while(in_array($random_seat, $seat_numbers)) {
|
|
$random_seat = rand(1, $room_capacity);
|
|
}
|
|
Attendance::create([
|
|
'classes_id' => $classes->id,
|
|
'student_id_number' => $student_id_number,
|
|
'student_name' => $student_name,
|
|
'student_surname' => $student_surname,
|
|
'seat_number' => $random_seat
|
|
]);
|
|
return view('map.summary_map', ['student_name' => $student_name, 'student_surname' => $student_surname, 'seat_number' => $random_seat, 'student_id_number' => $student_id_number, 'classes_id' => $classes->id]);
|
|
}
|
|
|
|
$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;
|
|
}
|
|
$userCache->delete($cache->classcode);
|
|
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]);
|
|
}
|
|
|
|
return view('map.start_map', ['classes_code' => $classes_code, 'classes' => $classes]);
|
|
}
|
|
|
|
public function start_classes_verified(Request $request)
|
|
{
|
|
$student_id_number = $request->input('student_id_number');
|
|
$student_name = $request->input('student_name');
|
|
$student_surname = $request->input('student_surname');
|
|
$classes = Classes::find($request->get('classes_id'));
|
|
$attendances = Attendance::where('classes_id', $classes->id)->get();
|
|
$seat_numbers = $attendances->pluck('seat_number')->toArray();
|
|
$subject = Subject::find($classes->subject_id);
|
|
if($classes->test_mode) {
|
|
$room_capacity = Room::find($subject->room_id)->capacity;
|
|
$random_seat = rand(1, $room_capacity);
|
|
while(in_array($random_seat, $seat_numbers)) {
|
|
$random_seat = rand(1, $room_capacity);
|
|
}
|
|
Attendance::create([
|
|
'classes_id' => $classes->id,
|
|
'student_id_number' => $student_id_number,
|
|
'student_name' => $student_name,
|
|
'student_surname' => $student_surname,
|
|
'seat_number' => $random_seat
|
|
]);
|
|
return view('map.summary_map', ['student_name' => $student_name, 'student_surname' => $student_surname, 'seat_number' => $random_seat, 'student_id_number' => $student_id_number, 'classes_id' => $classes->id]);
|
|
}
|
|
$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;
|
|
}
|
|
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]);
|
|
}
|
|
|
|
public function save_classes_data(Request $request)
|
|
{
|
|
$classes_id = $request->input('classes_id');
|
|
$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
|
|
]);
|
|
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]);
|
|
}
|
|
|
|
public function preview_classes($classes_id, $orderBy='student_surname', $orderDirection='ASC')
|
|
{
|
|
if(!$classes_id == 0) {
|
|
$classes = Classes::find($classes_id);
|
|
$attendances = Attendance::where('classes_id', $classes->id)->orderBy($orderBy,$orderDirection)->get();
|
|
$seat_numbers = $attendances->pluck('seat_number')->toArray();
|
|
$subject = Subject::find($classes->subject_id);
|
|
$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;
|
|
}
|
|
}
|
|
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]);
|
|
}
|
|
}
|