From 437eaa1fbb94bac2eda10a677f7faca1fd658d89 Mon Sep 17 00:00:00 2001 From: Josh North Date: Wed, 18 Feb 2015 05:13:53 -0500 Subject: [PATCH] optimization, begin refactor, basic reports --- config.inc.php.example | 3 +- includes/menu.inc.php | 1 + index.php | 60 +++++++++++++---------- profile.php | 80 +++++++++++++++--------------- punchlog.php | 102 ++++++++++++++++++--------------------- reports.php | 107 ++++++++++++++++++++++++++++++----------- 6 files changed, 204 insertions(+), 149 deletions(-) diff --git a/config.inc.php.example b/config.inc.php.example index 6366a67..1d6e20c 100755 --- a/config.inc.php.example +++ b/config.inc.php.example @@ -6,7 +6,7 @@ $yaptc_appname = 'Timecard System'; // Program name to display in title bar $yaptc_company = 'Widgets, Inc.'; // Your company name $sql = new PDO('mysql:host=localhost;dbname=your_database;', 'your_user', 'your_password'); // Database connection string - + $adminmessage = ''; // Message will display on all pages! //********** NO NEED TO EDIT PAST HERE **********// @@ -17,4 +17,3 @@ $yaptc_lib = $yaptc_dirpath . 'lib/'; $yaptc_libweb = $yaptc_webpath . 'lib/'; ?> - diff --git a/includes/menu.inc.php b/includes/menu.inc.php index 1582fe4..3aa7b12 100755 --- a/includes/menu.inc.php +++ b/includes/menu.inc.php @@ -40,6 +40,7 @@

+

" . $adminmessage . "

"; } ?>
diff --git a/index.php b/index.php index fb8a7d0..a863a83 100755 --- a/index.php +++ b/index.php @@ -17,25 +17,31 @@ else { //********** BEGIN CONTENT **********// $userid = $_SESSION['user_id']; + +// This is to get the current user status - in or out - and the notes and times associated for use in the form $result = $sql->prepare("SELECT punches.id as punchid, users.id as user, punches.intime as intime, punches.outtime as outtime, punches.notes as notes FROM punches INNER JOIN users ON punches.userid = users.id WHERE users.id = $userid ORDER BY punches.id DESC LIMIT 1"); $result->execute(); $last = $result->fetchObject(); + +// Let's build the page - this is the header with current status echo "

Current Status

"; -if(!isset($last->user)) -{ -echo "

You do not appear to have any punches on record.

"; -$status = "Out"; -} -else -{ -if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; } +if(!isset($last->user)) { + echo "

You do not appear to have any punches on record.

"; + $status = "Out"; + } else { + if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; } echo "

You have been Punched $status since " . date('g:i a \o\n M jS, Y', strtotime($statustime)) . ".

"; } echo "

Quick Punch

"; + echo "

Clicking the button below will immediately enter a new punch for you depending on your current status. Any notes you enter will be attached to the punch for your administrator to review.

"; echo "
"; echo "
"; +if (isset($notes)) { echo ""; +} else { +echo ""; +} echo "
"; if ($status=="In") { echo ""; @@ -46,32 +52,34 @@ echo "
"; echo "
"; diff --git a/profile.php b/profile.php index bc87da1..09d7247 100755 --- a/profile.php +++ b/profile.php @@ -4,7 +4,6 @@ require_once("config.inc.php"); $yaptc_pagename = "Profile"; require_once($yaptc_inc . "header.inc.php"); require_once($yaptc_inc . "menu.inc.php"); -//********** BEGIN CONTENT **********// // Is user logged in? If not, they shouldn't be here - kill all variables and redirect to login... if (!isset($_SESSION['user_id']) || !isset($_SESSION['signature']) || !isset($_SESSION['loggedIn']) || $_SESSION['loggedIn'] != true || $_SESSION['signature'] != md5($_SESSION['user_id'] . $_SERVER['HTTP_USER_AGENT'])) { @@ -16,43 +15,46 @@ echo "

You are not logged in!!!

"; } else { -echo "

Profile Information

"; -echo "

You may make changes to your user profile below if you wish. Updates will take effect immediately on pressing \"Save\".

"; - $query = "SELECT users.id, users.password, users.created, users.username, users.firstname, users.lastname, users.email, usertypes.typename AS usertype - FROM users, usertypes - WHERE users.id = :id"; - $stmt = $sql->prepare($query); - $stmt->execute(array(':id' => $_SESSION['user_id'])); - $user = $stmt->fetchObject(); -echo "
"; -echo "
"; -echo "
"; -echo ""; -echo "username\" readonly>"; -echo "
"; -echo "
"; -echo ""; -echo "created\" readonly>"; -echo "
"; -echo "
"; -echo ""; -echo "usertype\" readonly>"; -echo "
"; -echo "
"; -echo ""; -echo "firstname\">"; -echo "
"; -echo "
"; -echo ""; -echo "lastname\">"; -echo "
"; -echo "
"; -echo ""; -echo "email\">"; -echo "
"; -echo "
"; -echo ""; -echo "
"; +//********** BEGIN CONTENT **********// + +$query = "SELECT users.id, users.password, users.created, users.username, users.firstname, users.lastname, users.email, usertypes.typename AS usertype FROM users, usertypes WHERE users.id = :id"; + $stmt = $sql->prepare($query); + $stmt->execute(array(':id' => $_SESSION['user_id'])); + $user = $stmt->fetchObject(); +?> +

Profile Information

+

You may make changes to your user profile below if you wish. Updates will take effect immediately on pressing "Save".

+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +prepare($query); @@ -67,10 +69,10 @@ exit; } echo "
"; echo ""; - } //********** END CONTENT **********// +} require_once($yaptc_inc . "footer.inc.php"); ?> diff --git a/punchlog.php b/punchlog.php index 77ff74c..aec760c 100755 --- a/punchlog.php +++ b/punchlog.php @@ -17,48 +17,34 @@ else { //********** BEGIN CONTENT **********// $userid = $_SESSION['user_id']; -$nowarray = explode("-", date("Y-m-d-H-i")); +$timenow = date('Y-m-d H:i'); + +// This is to get the current user status - in or out - and the notes and times associated for use in the form $result = $sql->prepare("SELECT punches.id as punchid, users.id as user, punches.intime as intime, punches.outtime as outtime, punches.notes as notes FROM punches INNER JOIN users ON punches.userid = users.id WHERE users.id = $userid ORDER BY punches.id DESC LIMIT 1"); $result->execute(); $last = $result->fetchObject(); + +// Let's build the page - this is the header with current status echo "

Advanced Punch

"; -if(!isset($last->user)) -{ -$status = "Out"; -} -else -{ -if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; } +if(!isset($last->user)) { + echo "

You do not appear to have any punches on record.

"; + $status = "Out"; + } else { + if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; } +echo "

You have been Punched $status since " . date('g:i a \o\n M jS, Y', strtotime($statustime)) . ".

"; } + echo "

Use this form to enter a specific time on your punch. NOTE: changing the time from the current time will cause a flag on your log for the administrator to review, so we suggest you enter a reason why in the notes field (i.e. forgot punch, working from home, system down, etc).

"; echo "
"; echo "
"; -echo "
"; -echo "
"; -echo ""; -echo ""; -echo "
"; -echo "
"; -echo ""; -echo ""; -echo "
"; -echo "
"; -echo ""; -echo ""; -echo "
"; -echo "
"; -echo ""; -echo ""; -echo "
"; -echo "
"; -echo ""; -echo ""; -echo "
"; -echo "
"; +echo ""; +echo ""; echo ""; +if (isset($notes)) { echo ""; -echo "
"; -echo "
"; +} else { +echo ""; +} echo "
"; if ($status=="In") { echo ""; @@ -68,39 +54,47 @@ echo ""; } echo "
"; - if (!empty($_POST)) { - if (!empty($_POST['notes'])) { -$p_notes = $_POST['notes']; -} else { -$p_notes = ""; -} -$p_punchtime = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'] . " " . $_POST['hour'] . ":" . $_POST['minute'] . ":00"; + + + +// If the posted variables are not empty, we must be trying to insert a new punch. Use the form values to insert new record +if (!empty($_POST)) { + +// Is the notes field set? If so, use, otherwise set to null +if (isset($_POST['notes'])) { if (!empty($_POST['notes'])) { $p_notes = $_POST['notes']; } else { $p_notes = NULL; } } else { $p_notes = NULL; } + +// Is the punch time field set? If so, use, otherwise set to now +if (isset($_POST['punchtime'])) { + if (!empty($_POST['punchtime'])) { $p_punchtime = $_POST['punchtime'] . ':00'; $p_modified = "1"; } else { $p_punchtime = $timenow . ':00'; $p_modified = "0"; } +} else { $p_punchtime = $timenow . ':00'; $p_modified = "0"; } + +// Is the user currently punched in? If so, insert the punch out record, otherwise, insert a new punch in if ($status=="In") { -$query = "UPDATE punches SET outtime = :p_punchtime, notes = :p_notes WHERE id = :p_punchid"; - $stmt = $sql->prepare($query); -$stmt->execute(array( + $query = "UPDATE punches SET outtime = :p_punchtime, notes = :p_notes, modified = :p_modified WHERE id = :p_punchid"; + $stmt = $sql->prepare($query); + $stmt->execute(array( ':p_punchid' => $punchid, ':p_notes' => $p_notes, - ':p_punchtime' => $p_punchtime + ':p_punchtime' => $p_punchtime, + ':p_modified' => $p_modified, )); } else { -$query = "INSERT INTO punches (userid, notes, intime) VALUES (:p_userid, :p_notes, :p_punchtime)"; - $stmt = $sql->prepare($query); -$stmt->execute(array( - ':p_userid' => $_SESSION['user_id'], + $query = "INSERT INTO punches (userid, notes, intime, modified) VALUES (:p_userid, :p_notes, :p_punchtime, :p_modified)"; + $stmt = $sql->prepare($query); + $stmt->execute(array( + ':p_userid' => $_SESSION['user_id'], ':p_notes' => $p_notes, - ':p_punchtime' => $p_punchtime + ':p_punchtime' => $p_punchtime, + ':p_modified' => $p_modified, )); -} - - - - + } +// And then send user back to this page to see the updates header('Location: '.$_SERVER['PHP_SELF']); -exit; } + +// Close out the form... echo "
"; echo "
"; diff --git a/reports.php b/reports.php index bc8ed1f..7b2fe8d 100755 --- a/reports.php +++ b/reports.php @@ -16,18 +16,21 @@ echo "

You are not logged in!!!

"; else { //********** BEGIN CONTENT **********// + echo "

Punch History

"; -echo "

Below is your company punch history. You can use the form boxes to narrow down the results as needed, by date, user, or a combination.

"; +echo "

Below is your company punch history. The below drop-down can be used to select pre-configured reports. Other reports are currently being written.

"; echo "
"; echo "
"; echo "
"; -echo "
"; -echo " "; -echo " "; +if (isset($_POST['reporttype'])) { echo "";} +else { echo "";} +echo " "; +echo " "; echo " "; echo "
"; echo "
"; @@ -35,18 +38,15 @@ echo "
"; echo "
"; - - -// tag order to query depending on drop-down -if ($_POST['order'] == "Newest to Oldest") { - $order="ORDER BY punches.id DESC"; } - else { - $order="ORDER BY punches.id"; } - -// actual query +if (isset($_POST['reporttype'])) { +if ($_POST['reporttype'] == "Hours per week per user") { $query = "SELECT +YEAR(punches.intime) AS g_year, +WEEK(punches.intime) AS g_week, +SUM(TIME_TO_SEC(TIMEDIFF(punches.outtime, punches.intime))/3600) AS punchhours, punches.id as punchid, users.id as user, + users.username as username, users.firstname as firstname, users.lastname as lastname, punches.intime as intime, @@ -54,34 +54,85 @@ $query = "SELECT punches.notes as notes, punches.modified as modified FROM punches - INNER JOIN users ON punches.userid = users.id $order"; - - $stmt = $sql->prepare($query); +INNER JOIN users ON punches.userid = users.id +GROUP BY g_year, g_week, users.username;"; +$stmt = $sql->prepare($query); $stmt->execute(); - $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); //set up table header and open table echo ''; echo ''; echo ''; -echo ''; -echo ''; -echo ''; -echo ''; +echo ''; +echo ''; +echo ''; echo ''; -echo ''; -echo ''; echo ''; echo ''; echo ''; // $rows is an array containing all records... -foreach ($rows as $row) - echo ""; - +foreach ($rows as $row) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; +} echo ''; echo '
First NameLast NameTime InTime OutYearWeek#UsernameHoursFlagNotes
" . $row['firstname'] . "" . $row['lastname'] . "" . $row['intime'] . "" . $row['outtime'] . "" . $row['hours'] . "" . $row['flag'] . "" . $row['notes'] . "
" . $row['g_year'] . "" . $row['g_week'] . "" . $row['username'] . "" . $row['punchhours'] . "
'; +} +elseif ($_POST['reporttype'] == "Hours per month per user") { +$query = "SELECT +YEAR(punches.intime) AS g_year, +MONTH(punches.intime) AS g_month, +SUM(TIME_TO_SEC(TIMEDIFF(punches.outtime, punches.intime))/3600) AS punchhours, + punches.id as punchid, + users.id as user, + users.username as username, + users.firstname as firstname, + users.lastname as lastname, + punches.intime as intime, + punches.outtime as outtime, + punches.notes as notes, + punches.modified as modified + FROM punches +INNER JOIN users ON punches.userid = users.id +GROUP BY g_year, g_month, users.username;"; +$stmt = $sql->prepare($query); +$stmt->execute(); +$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + +//set up table header and open table +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; +echo ''; + +// $rows is an array containing all records... +foreach ($rows as $row) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; +} +echo ''; +echo '
YearMonthUsernameHours
" . $row['g_year'] . "" . $row['g_month'] . "" . $row['username'] . "" . $row['punchhours'] . "
'; +} +else { + echo "no query"; +} +} else { echo "no query"; } //********** END CONTENT **********// }