Add shift assignment tool

master
Skylar Ittner 7 years ago
parent a3368546f2
commit 3c71ed88c9

@ -96,6 +96,58 @@ switch ($VARS['action']) {
} else {
returnToSender("no_permission");
}
case "assignshift":
if (!account_has_permission($_SESSION['username'], "QWIKCLOCK_MANAGE")) {
returnToSender("no_permission");
}
if (!$database->has('shifts', ['shiftid' => $VARS['shift']])) {
returnToSender("invalid_shiftid");
}
$already_assigned = $database->select('assigned_shifts', 'uid', ['shiftid' => $VARS['shift']]);
require_once __DIR__ . "/lib/userinfo.php";
$managedusers = getManagedUsernames($_SESSION['uid']);
foreach ($VARS['users'] as $u) {
if (!account_has_permission($_SESSION['username'], "ADMIN")) {
if (!in_array($u, $managedusers)) {
returnToSender("you_arent_my_supervisor", htmlentities($u));
}
}
if (!user_exists($u)) {
returnToSender("user_not_exists", htmlentities($u));
}
$uid = getUserByUsername($u)['uid'];
$database->insert('assigned_shifts', ['uid' => $uid, 'shiftid' => $VARS['shift']]);
$already_assigned = array_diff($already_assigned, [$uid]); // Remove user from old list
}
foreach ($already_assigned as $uid) {
$database->delete('assigned_shifts', ["AND" => ['uid' => $uid, 'shiftid' => $VARS['shift']]]);
}
returnToSender("shift_assigned");
break;
case "autocomplete_user":
header("Content-Type: application/json");
$client = new GuzzleHttp\Client();
$response = $client
->request('POST', PORTAL_API, [
'form_params' => [
'key' => PORTAL_KEY,
'action' => "usersearch",
'search' => $VARS['q']
]
]);
if ($response->getStatusCode() != 200) {
exit("[]");
}
$resp = json_decode($response->getBody(), TRUE);
if ($resp['status'] == "OK") {
exit(json_encode($resp['result']));
} else {
exit("[]");
}
break;
case "signout":
session_destroy();
header('Location: index.php');

@ -66,5 +66,12 @@ define("STRINGS", [
"shift saved" => "Shift saved.",
"invalid time format" => "Invalid time format. Please use HH:MM or HH:MM AM.",
"shift name used" => "The shift name you gave is already in use. Use a different name or edit the existing shift.",
"invalid shiftid" => "Invalid shift ID."
"invalid shiftid" => "Invalid shift ID.",
"you are not the manager of user" => "You are not the manager of {arg}.",
"assign shift" => "Assign Shift",
"shift" => "Shift",
"people" => "People",
"type to add a person" => "Start typing to add a person",
"add" => "Add",
"choose a shift" => "Choose a shift"
]);

@ -52,5 +52,13 @@ define("MESSAGES", [
"invalid_shiftid" => [
"string" => "invalid shiftid",
"type" => "danger"
],
"you_arent_my_supervisor" => [
"string" => "you are not the manager of user",
"type" => "danger"
],
"shift_assigned" => [
"string" => "shift assigned",
"type" => "success"
]
]);

@ -113,6 +113,36 @@ function getManagedUIDs($manageruid) {
sendError("Login server error: " . $response->getBody());
}
$resp = json_decode($response->getBody(), TRUE);
if ($resp['status'] == "OK") {
return $resp['employees'];
} else {
return [];
}
}
/**
* Get an array of username the given UID is a manager of.
* @param int $manageruid The UID of the manager to find employees for.
* @return [int]
*/
function getManagedUsernames($manageruid) {
$client = new GuzzleHttp\Client();
$response = $client
->request('POST', PORTAL_API, [
'form_params' => [
'key' => PORTAL_KEY,
'action' => "getmanaged",
'uid' => $manageruid,
'get' => "username"
]
]);
if ($response->getStatusCode() > 299) {
sendError("Login server error: " . $response->getBody());
}
$resp = json_decode($response->getBody(), TRUE);
if ($resp['status'] == "OK") {
return $resp['employees'];

@ -51,4 +51,16 @@ define("PAGES", [
"static/js/addshift.js"
]
],
"assignshift" => [
"title" => "assign shift",
"navbar" => false,
"styles" => [
"static/css/easy-autocomplete.min.css"
],
"scripts" => [
"static/js/jquery.easy-autocomplete.min.js",
"static/js/jquery.color-2.1.2.min.js",
"static/js/assignshift.js"
]
],
]);

@ -0,0 +1,88 @@
<?php
require_once __DIR__ . '/../required.php';
redirectifnotloggedin();
$shifts = $database->select('shifts', [
"shiftid",
"shiftname",
"start",
"end"]
);
$assigned = [];
require_once __DIR__ . "/../lib/userinfo.php";
$shift = false;
if ($VARS['shift'] && $database->has('shifts', ['shiftid' => $VARS['shift']])) {
$shift = $VARS['shift'];
$uids = $database->select('assigned_shifts', 'uid', ['shiftid' => $shift]);
foreach ($uids as $uid) {
$assigned[] = getUserByID($uid)['username'];
}
}
?>
<form role="form" action="action.php" method="POST">
<div class="panel panel-blue">
<div class="panel-heading">
<h3 class="panel-title">
<i class="fa fa-calendar-o"></i> <?php lang("assign shift"); ?>
</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="form-group">
<label for="shift"><i class="fa fa-font"></i> <?php lang("shift"); ?></label><br />
<select name="shift" required="required" class="form-control" id="shift-select">
<option value="" selected><?php lang("choose a shift"); ?></option>
<?php
foreach ($shifts as $s) {
$str = $s['shiftname'] . " (" . date(TIME_FORMAT, strtotime($s['start'])) . " - " . date(TIME_FORMAT, strtotime($s['end'])) . ")";
$val = $s['shiftid'];
?>
<option value="<?php echo $val; ?>"<?php if ($val === $shift) { ?> selected<?php } ?>><?php echo $str; ?></option>
<?php
}
?>
</select>
</div>
</div>
<?php
if ($shift !== false) {
?>
<div class="col-xs-12 col-md-6">
<label for="people-box"><i class="fa fa-user"></i> <?php lang("people"); ?></label><br />
<div class="row">
<div class="col-xs-8 col-sm-10 col-md-9 col-lg-10">
<input type="text" id="people-box" class="form-control" placeholder="<?php lang("type to add a person") ?>" />
</div>
<div class="col-xs-4 col-sm-2 col-md-3 col-lg-2">
<button class="btn btn-default" type="button" id="addpersonbtn"><i class="fa fa-plus"></i> <?php lang("add") ?></button>
</div>
</div>
<div class="list-group" id="peoplelist">
<?php
foreach ($assigned as $user) {
?>
<div class="list-group-item" data-user="<?php echo $user; ?>">
<?php echo $user; ?> <div onclick="removePerson('<?php echo $user; ?>')" class="btn btn-danger btn-sm pull-right"><i class="fa fa-trash-o"></i></div><input type="hidden" name="users[]" value="<?php echo $user; ?>" />
</div>
<?php
}
?>
</div>
</div>
<?php
}
?>
</div>
</div>
<input type="hidden" name="action" value="assignshift" />
<input type="hidden" name="source" value="shifts" />
<div class="panel-footer">
<button type="submit" class="btn btn-success"><i class="fa fa-floppy-o"></i> <?php lang("save"); ?></button>
</div>
</div>
</form>

@ -21,6 +21,7 @@ if (account_has_permission($_SESSION['username'], "QWIKCLOCK_MANAGE")) {
?>
<div class="btn-group" style="margin-bottom: 10px;">
<a href="app.php?page=editshift" class="btn btn-success"><i class="fa fa-calendar-plus-o"></i> <?php lang("new shift"); ?></a>
<a href="app.php?page=assignshift" class="btn btn-info"><i class="fa fa-calendar-check-o"></i> <?php lang("assign shift"); ?></a>
</div>
<?php
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,76 @@
var options = {
url: "action.php",
ajaxSettings: {
dataType: "json",
method: "GET",
data: {
action: "autocomplete_user"
}
},
preparePostData: function (data) {
data.q = $("#people-box").val();
return data;
},
getValue: function (element) {
return element.username;
},
template: {
type: "custom",
method: function (value, item) {
return item.name + " <i class=\"small\">" + item.username + "</i>";
}
},
list: {
onClickEvent: function () {
var value = $("#people-box").getSelectedItemData().username;
addPerson(value);
}
}
};
$("#people-box").easyAutocomplete(options);
$("#people-box").keyup(function (event) {
if (event.keyCode == 13) {
$("#addpersonbtn").click();
event.preventDefault();
return false;
}
});
$("#people-box").keydown(function (event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
$("#addpersonbtn").click(function () {
addPerson($("#people-box").val());
});
function addPerson(p) {
p = String.trim(p);
if (p == "") {
return false;
}
if ($("#peoplelist div[data-user=" + p + "]").length) {
$("#peoplelist .list-group-item[data-user=" + p + "]").animate({
backgroundColor: "#ff0000",
}, 500, "linear", function () {
$("#peoplelist .list-group-item[data-user=" + p + "]").animate({
backgroundColor: "#ffffff",
}, 500);
});
return false;
}
$('#peoplelist').append("<div class=\"list-group-item\" data-user=\"" + p + "\">" + p + "<div onclick=\"removePerson('" + p + "')\" class=\"btn btn-danger btn-sm pull-right\"><i class=\"fa fa-trash-o\"></i></div><input type=\"hidden\" name=\"users[]\" value=\"" + p + "\" /></div>");
$("#people-box").val("");
}
function removePerson(p) {
$("#peoplelist div[data-user=" + p + "]").remove();
}
$('#shift-select').on('change', function(){
document.location.href = "app.php?page=assignshift&shift=" + $(this).val();
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save