UserCache fix + style + websocket server fix

This commit is contained in:
barwie2@st.amu.edu.pl 2020-01-18 18:15:35 +01:00
parent 36ad10c8c8
commit 8d488a87f8
4 changed files with 54 additions and 151 deletions

View File

@ -14,10 +14,8 @@ use App\Room;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class UserClassesController extends Controller
{
public function index($groupBy='subject_id')
{
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();
@ -37,8 +35,7 @@ class UserClassesController extends Controller
return view('user.user_classes', ['classes' => $classes, 'classes_grouped' => $classes_grouped, 'subjects' => $subjects, 'grouped_by' => $groupBy, 'defaultDate' => $defaultDate]);
}
public function add_classes(Request $request)
{
public function add_classes(Request $request) {
$subject_id = $request->input('subject_id');
$date = $request->input('date');
$mode = $request->input('mode');
@ -50,20 +47,17 @@ class UserClassesController extends Controller
return redirect(route('user_start_classes', ['classes_id' => $classes_id]));
}
public function delete_classes($classes_id)
{
public function delete_classes($classes_id) {
Classes::find($classes_id)->delete();
return redirect()->back();
}
public function edit_classes($classes_id)
{
public function edit_classes($classes_id) {
Classes::find($classes_id);
return redirect()->back();
}
public function start_classes($classes_id)
{
public function start_classes($classes_id) {
$classes = Classes::find($classes_id);
$classes_code = $classes->classes_code;
if (!$classes_code) {
@ -73,52 +67,10 @@ class UserClassesController extends Controller
$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)
{
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');
@ -183,8 +135,7 @@ class UserClassesController extends Controller
}
}
public function save_classes_data(Request $request)
{
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');
@ -200,8 +151,7 @@ class UserClassesController extends Controller
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]);
}
public function preview_classes($classes_id, $orderBy='student_surname', $orderDirection='ASC')
{
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();

View File

@ -1,41 +0,0 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserCache extends Controller
{
public function delete($classes_code) {
DB::table("usercache")->where("classcode", $classes_code)->delete();
}
public function get($classes_code) {
$object = DB::table("usercache")->where("classcode", $classes_code)->first();
return $object;
}
public function add(Request $request) {
$classes_code = $request->classes_code;
$classes = DB::table("classes")->where("classes_code", $classes_code)->first();
if(is_null($classes)) {
return response()->json(['message' => 'Nie ma takich zajęć'], 400);
} else {
$cache = DB::table("usercache")->where("classcode", $classes_code)->first();
if(!is_null($cache)) {
return response()->json(['message' => 'Użytkownik w cache'], 500);
}
}
DB::table("usercache")->insert([
"name" => $request->student_name,
"surname" => $request->student_surname,
"classcode" => $request->classes_code,
"index" => $request->student_index
]);
return response()->json(['message' => 'ADDED'], 200);
}
}

View File

@ -1,11 +1,13 @@
const WebSocket = require('ws');
var http = require('http');
const wss = new WebSocket.Server({ port: 8888 });
var websocket_port = 8888;
const wss = new WebSocket.Server({port: websocket_port});
console.log("Websocket server running on port " + websocket_port);
var lookup = [];
wss.on('connection', function connection(ws) {
console.log(ws.protocol);
lookup[ws.protocol] = ws;
});
@ -14,7 +16,7 @@ var server = http.createServer ( function(request,response){
response.writeHead(200, {"Content-Type": "text\plain"});
request.setEncoding('utf8');
if(request.method == "POST") {
if (request.method === "POST") {
var body = '';
request.on('data', function (chunk) {
body += chunk;
@ -33,16 +35,11 @@ response.writeHead(200,{"Content-Type":"text\plain"});
lookup[body.classes_code].send(JSON.stringify(send_array));
});
}
else
{
else {
response.end("Undefined request .");
}
});
/*wss.on('connection', function connection(ws) {
console.log(ws.protocol);
lookup[ws.protocol] = ws;
});*/
server.listen(8889);
console.log("Server running on port 8889");
var http_port = 8889;
server.listen(http_port);
console.log("HTTP server running on port " + http_port);

View File

@ -17,9 +17,6 @@ Route::get('/', function () {
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/add/cache', 'UserCache@add');
Route::post('/delete/cache', 'UserCache@delete');
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function()
{