Files
HotLunches/activity_log.php
2026-10-19 15:22:52 -07:00

327 lines
12 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /?error_message=" . urlencode(getLanguageText('ERRORMSG-SESSION-EXPIRED')));
exit();
}
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator');
if (checkPermission($AllowedUserTypes) != 1) {
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED') . ": $action";
header("Location: /?error_message=" . urlencode($error_message));
exit();
}
if (! db_tableExists("activity_log")) {
return;
}
//
// Process all actions to list and edit records
//
$action = "activity-log";
$CurrentTable = "activity_log";
$FormFile = "";
$TitleText = getLanguageText('TEXT-ACTIVITY-LOG');
$OrderBy = "activity_datetime desc";
// Start the template and output buffering
startTemplate();
$templatePageTitle = $TitleText;
$headerArgs = "";
unset($user_num);
unset($start_date);
unset($end_date);
if (array_key_exists('user_num',$_GET)) $user_num = intval($_GET['user_num']);
if (array_key_exists('user_num',$_POST)) $user_num = intval($_POST['user_num']);
if (array_key_exists('start_date',$_GET)) $start_date = db_escape_string($_GET['start_date']);
if (array_key_exists('start_date',$_POST)) $start_date = db_escape_string($_POST['start_date']);
if (array_key_exists('end_date',$_GET)) $end_date = db_escape_string($_GET['end_date']);
if (array_key_exists('end_date',$_POST)) $end_date = db_escape_string($_POST['end_date']);
if (array_key_exists('selected_userid',$_GET)) $selected_userid = db_escape_string($_GET['selected_userid']);
if (array_key_exists('selected_userid',$_POST)) $selected_userid = db_escape_string($_POST['selected_userid']);
if (array_key_exists('selected_ip',$_GET)) $selected_ip = db_escape_string($_GET['selected_ip']);
if (array_key_exists('selected_ip',$_POST)) $selected_ip = db_escape_string($_POST['selected_ip']);
if (array_key_exists('selected_activity',$_GET)) $selected_activity = db_escape_string($_GET['selected_activity']);
if (array_key_exists('selected_activity',$_POST)) $selected_activity = db_escape_string($_POST['selected_activity']);
if (array_key_exists('search_text',$_GET)) $search_text = db_escape_string($_GET['search_text']);
if (array_key_exists('search_text',$_POST)) $search_text = db_escape_string($_POST['search_text']);
if (array_key_exists('selected_person',$_GET)) $selected_person = db_escape_string($_GET['selected_person']);
if (array_key_exists('selected_person',$_POST)) $selected_person = db_escape_string($_POST['selected_person']);
if ($_SESSION['UserInfo']['type'] == 'Parent') {
$user_num = intval($_SESSION['UserInfo']['record_num']);
$naction = 'list';
}
$school_num = $school_record['record_num'];
$message_width = 400;
include "form.messages.php";
// Process naction parameters
if (array_key_exists('naction',$_GET) && strlen($_GET['naction']) > 0) $naction = $_GET['naction'];
if (isset($naction) && $naction == 'view-details') include "activity_log.details.php";
//
// Parse any parameters
//
$where = "where school_num = $school_num ";
// Handle selected date ranges
if (!isset($start_date)) $start_date = date("Y-m-d 00:00:00",time());
if (!isset($end_date)) $end_date = date("Y-m-d 23:59:59",time());
$start_date = date("Y-m-d 00:00:00",strtotime($start_date));
if ($end_date < substr($start_date,0,10)) {
$end_date = substr($start_date,0,10);
$error_message = getLanguageText('ERRORMSG-DATE-RANGE-ERROR');
}
$end_date = date("Y-m-d 23:59:59",strtotime($end_date));
$sdate = substr($start_date,0,10);
$edate = substr($end_date,0,10);
$stime = strtotime($start_date);
$etime = strtotime($end_date);
$where .= "and activity_time between $stime and $etime ";
// View only a selected userid
if (isset($selected_userid) && strlen($selected_userid) > 0) {
$s_userid = db_escape_string($selected_userid);
$where .= "and userid = '$s_userid' ";
}
// View only a selected ip
if (isset($selected_ip) && strlen($selected_ip) > 0) {
$s_ip = db_escape_string($selected_ip);
$where .= "and user_ip = '$s_ip' ";
}
// View only a selected activity
if (isset($selected_activity) && strlen($selected_activity) > 0) {
$s_activity = db_escape_string($selected_activity);
$where .= "and activity = '$s_activity' ";
}
// Search text processing
if (isset($search_text) && strlen($search_text) > 0) {
$s_text = db_escape_string($search_text);
$where .= "and information LIKE '%$s_text%' ";
}
// Selected person processing
if (isset($selected_person) && strlen($selected_person) > 0) {
$s_person = db_escape_string($selected_person);
$where .= "and information LIKE '%$s_person%' ";
}
if ($_SESSION['UserInfo']['type'] != 'SuperUser') {
$where .= "and userid != 'superuser' ";
}
if (!isset($search_text)) $search_text = '';
if (!isset($selected_userid)) $selected_userid = '';
if (!isset($selected_ip)) $selected_ip = '';
if (!isset($selected_activity)) $selected_activity = '';
?>
<br>
<form method='post' name='activitylog' action="/">
<table width='550' cellpadding='5' class='content-table'>
<tr height='5'>
<td align='left' colspan='2'><img src='images/spacer.gif' height='5'></td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-START-DATE'); ?> :: </td>
<td align='left'><input type='text' name='start_date' id='start_date' value="<?php echo($sdate) ?>" size='15' class='date-field'></td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-END-DATE'); ?> :: </td>
<td align='left'><input type='text' name='end_date' id='end_date' value="<?php echo($edate) ?>" size='15' class='date-field'></td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-USER-ID'); ?> :: </td>
<td align='left'>
<select name='selected_userid'>
<option value=''><?php displayLanguageText('TEXT-VIEW-ALL'); ?></option>
<?php
// List userid's from login_log
$results = db_query("select distinct userid from login_log where school_num = $school_num order by userid");
while ($row = db_fetch_assoc($results)) {
$userid = $row['userid'];
$selected = "";
if (isset($selected_userid) && $selected_userid == $userid) $selected = " selected='selected'";
echo(" <option value=\"$userid\"$selected>$userid</option>\n");
}
?>
</select>
</td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-IP-ADDRESS'); ?> :: </td>
<td align='left'>
<select name='selected_ip'>
<option value=''><?php displayLanguageText('TEXT-VIEW-ALL'); ?></option>
<?php
// List IP's from login_log
$results = db_query("select distinct user_ip from activity_log where school_num = $school_num order by user_ip");
while ($row = db_fetch_assoc($results)) {
$user_ip = $row['user_ip'];
$selected = "";
if (isset($selected_ip) && $selected_ip == $user_ip) $selected = " selected='selected'";
echo(" <option value='$user_ip'$selected>$user_ip</option>\n");
}
?>
</select>
</td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-ACTIVITY'); ?> :: </td>
<td align='left'>
<select name='selected_activity'>
<option value=''><?php displayLanguageText('TEXT-VIEW-ALL'); ?></option>
<?php
// List activities from activity_log
$results = db_query("select distinct activity from activity_log where school_num = $school_num order by activity");
$ppDisplayed = false;
while ($row = db_fetch_assoc($results)) {
$activity = $row['activity'];
$selected = "";
if (isset($selected_activity) && $selected_activity == $activity) $selected = " selected='selected'";
echo(" <option value='$activity'$selected>$activity</option>\n");
}
?>
</select>
</td>
</tr>
<tr>
<td align='right'><?php displayLanguageText('TEXT-SEARCH-TEXT'); ?> :: </td>
<td align='left'><input type='input' name='search_text' size='20' value="<?php echo($search_text) ?>"></td>
<tr>
<td align='left'>&nbsp;</td>
<td align='left'><a href='#' class='btn btn--primary btn--xsmall' onClick='document.activitylog.submit();'><?php displayLanguageText('TEXT-VIEW'); ?></a></td>
</tr>
</table>
<input type='hidden' name='action' value='activity-log'>
</form>
<center>
<?php
$search_text = urlencode($search_text);
//
// Only show superuser activity to superuser
//
if ($_SESSION['UserInfo']['type'] != 'SuperUser') $where .= "and userid != 'superuser' ";
//
// List all requested records
//
$columnHeaders = array(getLanguageText('TEXT-USER-ID'), getLanguageText('TEXT-ACTIVITY-TIME'), getLanguageText('TEXT-IP-ADDRESS'), getLanguageText('TEXT-ACTIVITY'));
$query = "SELECT userid,activity_datetime,user_ip,activity_time,activity,information " .
"FROM activity_log $where order by $OrderBy";
if (isset($prev_query) && strlen($prev_query)) $query = $prev_query;
$results = db_query($query);
$data = array();
$otipSpans = "";
$otipSpanIDs = array();
while ($row = db_fetch_assoc($results)) {
$activity = $row['activity'];
$information = str_replace("\n"," ",$row['information']);
$oTip = "";
$otext = "";
$ntext = "";
$ttext = "";
switch ($activity) {
case 'orders - completed':
case 'orders - insert':
case 'orders - update':
if (str_match("ordered_by_num",$information)) {
$ordered_by = preg_replace("/.*ordered_by_num => /","",$information);
$ordered_by = trim(preg_replace("/ .*/","",$ordered_by));
$otext = sprintf($otipROW2,getLanguageText('TEXT-ORDERED-BY').': ',$ordered_by);
}
if (str_match("ordered_by_name",$information)) {
$name = preg_replace("/.*ordered_by_name => /","",$information);
$name = preg_replace("/\+\+\+.*/","",$name);
$ntext = sprintf($otipROW2,getLanguageText('TEXT-NAME').': ',$name);
}
if (str_match("new_last_name",$information)) {
$lname = preg_replace("/.*new_last_name => /","",$information);
$lname = preg_replace("/\+\+\+.*/","",$lname);
$fname = preg_replace("/.*new_first_name => /","",$information);
$fname = preg_replace("/\+\+\+.*/","",$fname);
$name = "$fname $lname";
$ntext = sprintf($otipROW2,getLanguageText('TEXT-NAME').': ',$name);
}
if (str_match("ordertotal",$information)) {
$ordertotal = preg_replace("/.*ordertotal => /","",$information);
$ordertotal = preg_replace("/ .*/","",$ordertotal);
$ttext = sprintf($otipROW2,getLanguageText('TEXT-TOTAL').': ',$ordertotal);
}
if (strlen($ntext) == 0 && isset($ordered_by) && $ordered_by > 0) {
$name = db_getVal("SELECT concat(first_name,' ',last_name) FROM people WHERE record_num = $ordered_by");
$ntext = sprintf($otipROW2,getLanguageText('TEXT-NAME').': ',$name);
}
if (!isset($ordered_by)) $ordered_by = '';
$span_id = "act_" . $row['activity_time'] . "_" . $ordered_by;
$otipSpans .= "<span id='$span_id' class='hidden'>" . $otipTABLE . $otext . $ntext . $ttext;
$otipSpans .= "</table></span>\n";
$oTip = "onmouseover=\"TagToTip('$span_id')\" onmouseout=\"UnTip()\"";
break;
}
$data[] = array("<a href='?action=activity-log&selected_userid=" . urlencode($row['userid']) . "&start_date=$start_date&end_date=$end_date'>" . $row['userid'] . "</a>",
$row['activity_datetime'],
$row['user_ip'],
"<a href='?action=activity-log&naction=view-details&userid=" . urlencode($row['userid']) . "&activity_time=" . $row['activity_time'] .
"&start_date=$start_date&end_date=$end_date&prev_query=" . urlencode($query) . "' $oTip>" . $activity . "</a>");
}
listDataCustom($data,$columnHeaders,$action,array(),'N');
if (count($data)) {
echo("<br>\n<p style='text-align: right'>" .
"<a href='?action=activity-log-report&start_date=" . urlencode($start_date) .
"&end_date=" . urlencode($end_date) .
"&search_text=$search_text" .
"&selected_userid=$selected_userid" .
"&selected_ip=$selected_ip" .
"&selected_activity=$selected_activity' class='btn btn--primary btn--xsmall'>" .
getLanguageText('TEXT-PRINT') . "</a>&nbsp;&nbsp;&nbsp;</p>\n");
}
endTemplate();
?>