101 lines
2.1 KiB
PHP
101 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Activity Log Helper Functions
|
|
*
|
|
* Provides reusable functions for activity logging operations
|
|
* throughout the application.
|
|
*/
|
|
|
|
/**
|
|
* Get the current activity user ID from session
|
|
*
|
|
* @return int|null The logged-in user ID or null if guest
|
|
*/
|
|
function getActivityUserId(): ?int
|
|
{
|
|
$userId = session()->get('id');
|
|
return $userId ? (int) $userId : null;
|
|
}
|
|
|
|
/**
|
|
* Get the current activity user type/role from session
|
|
*
|
|
* @return string The user role (admin, doctor, patient) or 'guest'
|
|
*/
|
|
function getActivityUserType(): string
|
|
{
|
|
return session()->get('role') ?: 'guest';
|
|
}
|
|
|
|
/**
|
|
* Get the current page/path being accessed
|
|
*
|
|
* @return string The current request path
|
|
*/
|
|
function getActivityPage(): string
|
|
{
|
|
$request = service('request');
|
|
return $request->getPath();
|
|
}
|
|
|
|
/**
|
|
* Get the client IP address
|
|
*
|
|
* @return string|null The client IP address or null if unavailable
|
|
*/
|
|
function getActivityIP(): ?string
|
|
{
|
|
$request = service('request');
|
|
if (method_exists($request, 'getIPAddress')) {
|
|
return $request->getIPAddress();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get all activity metadata at once
|
|
*
|
|
* Useful for logging operations that need complete activity context
|
|
*
|
|
* @return array Array containing user_id, user_type, page, and ip
|
|
*/
|
|
function getActivityMetadata(): array
|
|
{
|
|
return [
|
|
'user_id' => getActivityUserId(),
|
|
'user_type' => getActivityUserType(),
|
|
'page' => getActivityPage(),
|
|
'ip' => getActivityIP(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Check if current user is authenticated
|
|
*
|
|
* @return bool True if user is logged in, false otherwise
|
|
*/
|
|
function isActivityUserAuthenticated(): bool
|
|
{
|
|
return getActivityUserId() !== null;
|
|
}
|
|
|
|
/**
|
|
* Get formatted user identifier for logging
|
|
*
|
|
* Returns a string like "User #123 (admin)" or "Guest"
|
|
*
|
|
* @return string Formatted user identifier
|
|
*/
|
|
function getFormattedActivityUser(): string
|
|
{
|
|
$userId = getActivityUserId();
|
|
$userType = getActivityUserType();
|
|
|
|
if ($userId === null) {
|
|
return 'Guest';
|
|
}
|
|
|
|
return "User #{$userId} ({$userType})";
|
|
}
|