Skip to content

Commit

Permalink
reservering aanvraag toegevoegd
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Nov 11, 2019
1 parent e3ea20c commit 6ca0528
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 51 deletions.
35 changes: 35 additions & 0 deletions import/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ public function getDeviceCart($cartId) {
return $cart;
}

public function isReserved($cartId, $date, $hour) {
$result = $this->runQuery("SELECT * FROM damstede.cartreservations WHERE cart_id='".intval($cartId)."' AND DATE(date)=STR_TO_DATE('".$this->makeSafe($date)."', '%Y-%m-%d') AND hour='".intval($hour)."' LIMIT 1");
if ($result != false) {
if (mysqli_num_rows($result) > 0) {
return $this->formatReservation(mysqli_fetch_assoc($result));
}
else {
return false;
}
}
else {
return false;
}
}

public function reserveCart($cartId, $date, $hour, $location, $user, $teacher) {
if ($this->isReserved($cartId, $date, $hour) == false) {
$result = $this->runQuery("INSERT INTO damstede.cartreservations (cart_id, date, hour, location, user, teacher) VALUES ('".intval($cartId)."', '".date("Y-m-d", strtotime($date))."', '".intval($hour)."', '".$this->makeSafe($location)."', '".$this->makeSafe($user)."', '".$this->makeSafe($teacher)."')");
if ($result != false) {
if (mysqli_affected_rows($this->connection) > 0) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}

private function formatReservation($res) {
$res["id"] = intval($res["id"]);
$res["cart_id"] = intval($res["cart_id"]);
Expand Down
95 changes: 95 additions & 0 deletions import/reserve-cb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?PHP
header('Content-Type: text/html; charset=utf-8');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$data = array();
$data["type"] = "error";
$data["message"] = "Onbekende error";
$data["data"] = array();

function returnError($msg) {
global $data;
$data["type"] = "error";
$data["message"] = $msg;
$data["data"] = array();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
die();
}

function returnWarning($msg) {
global $data;
$data["type"] = "warning";
$data["message"] = $msg;
$data["data"] = array();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
die();
}

function returnData($msg, $stuff) {
global $data;
$data["type"] = "success";
$data["message"] = $msg;
$data["data"] = $stuff;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
die();
}

session_start();

if (!isset($_SESSION["zermelo_access_token"]) || empty($_SESSION["zermelo_access_token"])) {
returnError("Je bent niet (meer) ingelogd! Log opnieuw in.");
}
else {
// ERROR HANDLING START

if (!isset($_POST["date"]) || empty($_POST["date"])) {
returnError("Datum is niet ingevuld. Vul een datum in.");
}

if (strtotime($_POST["date"]) < strtotime(date("today"))) {
returnError("Datum is in het verleden... Je kunt alleen voor vandaag of in de toekomst reserveren!");
}

if (!isset($_POST["hour"]) || empty($_POST["hour"])) {
returnError("Lesuur is niet aangegeven. Kies een lesuur in het dropdown-menu.");
}

if (intval($_POST["hour"]) < 1 || intval($_POST["hour"]) > 9) {
returnError("Ongeldig lesuur! Lesuur mag minimaal 1 en maximaal 9 zijn.");
}

if (!isset($_POST["location"]) || empty($_POST["location"])) {
returnError("Geen lokaal ingevuld. Vul het lokaal in waar de kar zal worden gebruikt.");
}

if (!isset($_POST["cart"]) || empty($_POST["cart"])) {
returnError("Geen apparaatkar geselecteerd. Kies een kar om te reserveren uit het dropdown-menu.");
}

require_once("db.php");
$damstedeDB = new DamstedeDB();

if (empty($damstedeDB->getDeviceCart($_POST["cart"]))) {
returnError("Apparaatkar ".intval($_POST["cart"])." bestaat niet.");
}

if ($damstedeDB->isReserved($_POST["cart"], $_POST["date"], $_POST["hour"])) {
returnError("Deze kar is al gereserveerd voor dit uur. Probeer een andere kar uit het dropdown-menu.");
}

// ERROR HANDLING END

$reserved = $damstedeDB->reserveCart($_POST["cart"], $_POST["date"], $_POST["hour"], $_POST["location"], $_SESSION["user"]["code"], $_POST["teacher"]);
if ($reserved != false) {
returnData("Reservering geplaatst", null);
}
else {
returnError("Kon geen reservering plaatsen. Probeer het later opnieuw.");
}
}
?>
26 changes: 25 additions & 1 deletion import/schedule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var schedule = {
defaults: null,
carts: [],
dates: ["", "", "", "", ""],
currentlyLoaded: [0, 0],

init: function() {
return new Promise(function(resolve, reject) {
Expand All @@ -25,35 +27,49 @@ var schedule = {
},

clear: function() {
schedule.dates = ["", "", "", "", ""];
schedule.currentlyLoaded = [0, 0];
document.getElementsByClassName("schedule")[0].innerHTML = schedule["defaults"];
},

reload: function() {
schedule.get(schedule.currentlyLoaded[0], schedule.currentlyLoaded[1]).then(function(reservations) {
schedule.load(reservations);
});
},

get: function(year, week) {
document.getElementById('loading').style.display = 'table';
return new Promise(function(resolve, reject) {
var dateReq = new XMLHttpRequest();
dateReq.open('POST', 'import/weekdates.php?year='+year+'&week='+week);
dateReq.onload = function() {
schedule.clear();
schedule.currentlyLoaded = [year, week];
var dayDates = document.getElementsByClassName("daydate");
var dates = dateReq.responseText.split(" ");
for (var i = 0; i < 5; i++) {
dates[i] = dates[i].split("-");
dayDates[i].innerHTML = dates[i][2]+'-'+dates[i][1];
}
schedule["dates"] = dates;
var request = new XMLHttpRequest();
request.open('POST', 'import/dschedule.php?year='+year+'&week='+week);
request.onload = function() {
try {
var response = JSON.parse(request.responseText);
console.log(response);
if (response["type"] == "success") {
document.getElementById('loading').style.display = 'none';
resolve(response["data"]);
}
else {
document.getElementById('loading').style.display = 'none';
reject(response["message"]);
}
}
catch(e) {
document.getElementById('loading').style.display = 'none';
reject(e);
}
};
Expand All @@ -77,16 +93,19 @@ var schedule = {

load: function(reservations) {
console.log(reservations);
document.getElementById('loading').style.display = 'table';
var reservationCount = reservations.length;
for (var i = 0; i < reservationCount; i++) {
document.getElementById("week-hour-"+reservations[i]["hour"]).children[reservations[i]["day"]+1].appendChild(schedule.createResElem(reservations[i]));
}
schedule.fillInResAddBtns();
document.getElementById('loading').style.display = 'none';
},

createResElem: function(res) {
var resElem = document.createElement("div");
resElem.className = "reservation";
resElem.setAttribute("title", 'Reservering voor kar ' + res["cart_id"] + ' (' + schedule.carts[res["cart_id"]]["dev_type"] + ') op ' + new Date(Date.parse(res["date"])).toLocaleDateString() + ', het ' + res["hour"] + 'e uur');
if (res["cancelled"]) {
resElem.className += " cancelled";
}
Expand All @@ -103,12 +122,17 @@ var schedule = {
resAddBtn.className = "reservation add-btn";
resAddBtn.innerHTML = "+";
resAddBtn.setAttribute("title", "Kar reserveren");
resAddBtn.setAttribute("onclick", "showAction('reservationadder');");

var lessons = document.getElementsByClassName("lesson");
var lessonCount = lessons.length;
for (var i = 0; i < lessonCount; i++) {
if (lessons[i].className.indexOf("break") < 0) {
lessons[i].appendChild(resAddBtn.cloneNode(true));
var lessonDay = parseInt(lessons[i].getAttribute("data-lesson").split("-")[0]);
var lessonHour = parseInt(lessons[i].getAttribute("data-lesson").split("-")[1]);
var clonedBtn = resAddBtn.cloneNode(true);
clonedBtn.setAttribute("onclick", "showAction('reservationadder'); setUpReservationAdder('"+schedule.dates[lessonDay-1].join("-")+"', "+lessonHour+");");
lessons[i].appendChild(clonedBtn);
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions opmaak.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ header #pageoptions > *:hover {
padding-right: 32px;
}

#content.nopadding {
padding: 0px 0px !important;
}

.action {
display: table;
position: fixed;
Expand Down Expand Up @@ -256,6 +260,7 @@ header #pageoptions > *:hover {
color: #EDEDED;
font-size: 15px;
transition: 0.15s;
outline: 0;
}

.actionbuttons .button.extra, button.extra {
Expand Down Expand Up @@ -359,17 +364,22 @@ select {
margin-top: 4px;
margin-bottom: 4px;
cursor: default;
background-color: #444444;
background: #444444;
background-color: #333333;
color: #EDEDED;
font-family: robotolight, Roboto, roboto, Arial, sans-serif;
font-size: 14px;
border: solid #333333 1px;
border-radius: 3px;
transition: 0.15s;
outline: 0;
}

select:focus, select:hover, select:active {
select:focus, select:hover {
background-color: #444444;
transition: 0.05s;
}

select:active {
background-color: #555555;
outline: 0 !important;
transition: 0.05s;
Expand Down Expand Up @@ -471,6 +481,7 @@ label {
line-height: 14px;
font-size: 24px;
padding: 0px 6px;
margin: 0px 4px;
opacity: 0;
transition: 0.15s;
pointer-events: none;
Expand All @@ -485,6 +496,7 @@ label {
opacity: 1;
height: initial;
padding: 12px 6px;
margin: 4px 4px;
transition: 0.05s;
pointer-events: initial;
}
Expand Down Expand Up @@ -540,4 +552,10 @@ label {
.location-prefix {
display: none;
}
}

@media screen and (max-width: 650px) {
.actiontable th {
width: 80px;
}
}
Loading

0 comments on commit 6ca0528

Please sign in to comment.