| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:26:55 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-11-29 16:08:56 +00:00
										 |  |  |  * Logs listings | 
					
						
							| 
									
										
										
										
											2024-11-27 14:26:55 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This page ("logs") retrieves and displays logs for a specified user within a time range. | 
					
						
							|  |  |  |  * It supports pagination and filtering, and generates a widget to display the logs. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 08:45:31 +00:00
										 |  |  | // Get any new messages
 | 
					
						
							|  |  |  | include '../app/includes/messages.php'; | 
					
						
							|  |  |  | include '../app/includes/messages-show.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 19:31:07 +00:00
										 |  |  | // Check for rights; user or system
 | 
					
						
							|  |  |  | if (($userObject->hasRight($user_id, 'superuser') || | 
					
						
							|  |  |  |       $userObject->hasRight($user_id, 'view app logs'))) { | 
					
						
							|  |  |  |     $scope = 'system'; | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     $scope = 'user'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  | // specify time range
 | 
					
						
							|  |  |  | include '../app/helpers/time_range.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 16:08:03 +00:00
										 |  |  | // pagination variables
 | 
					
						
							|  |  |  | $items_per_page = 15; | 
					
						
							|  |  |  | $browse_page = $_REQUEST['p'] ?? 1; | 
					
						
							|  |  |  | $browse_page = (int)$browse_page; | 
					
						
							|  |  |  | $offset = ($browse_page -1) * $items_per_page; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  | // prepare the result
 | 
					
						
							| 
									
										
										
										
											2024-09-16 16:08:03 +00:00
										 |  |  | $search = $logObject->readLog($user_id, $scope, $offset, $items_per_page); | 
					
						
							|  |  |  | $search_all = $logObject->readLog($user_id, $scope); | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if (!empty($search)) { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:08:03 +00:00
										 |  |  |     // we get total items and number of pages
 | 
					
						
							|  |  |  |     $item_count = count($search_all); | 
					
						
							|  |  |  |     $page_count = ceil($item_count / $items_per_page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  |     $logs = array(); | 
					
						
							|  |  |  |     $logs['records'] = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach ($search as $item) { | 
					
						
							| 
									
										
										
										
											2024-09-16 16:08:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // when we show only user's logs, omit user_id column
 | 
					
						
							|  |  |  |         if ($scope === 'user') { | 
					
						
							|  |  |  |             $log_record = array( | 
					
						
							|  |  |  |                 // assign title to the field in the array record
 | 
					
						
							|  |  |  |                 'time'		=> $item['time'], | 
					
						
							|  |  |  |                 'log message'	=> $item['message'] | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $log_record = array( | 
					
						
							|  |  |  |                 // assign title to the field in the array record
 | 
					
						
							|  |  |  |                 'userID'	=> $item['user_id'], | 
					
						
							|  |  |  |                 'time'		=> $item['time'], | 
					
						
							|  |  |  |                 'log message'	=> $item['message'] | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  |         // populate the result array
 | 
					
						
							|  |  |  |         array_push($logs['records'], $log_record); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // prepare the widget
 | 
					
						
							|  |  |  | $widget['full'] = false; | 
					
						
							|  |  |  | $widget['collapsible'] = false; | 
					
						
							|  |  |  | $widget['name'] = 'Logs'; | 
					
						
							|  |  |  | $username = $userObject->getUserDetails($user_id)[0]['username']; | 
					
						
							|  |  |  | $widget['title'] = "Log events for user \"$username\"";
 | 
					
						
							|  |  |  | $widget['filter'] = true; | 
					
						
							| 
									
										
										
										
											2024-09-16 16:08:03 +00:00
										 |  |  | if (!empty($logs['records'])) { | 
					
						
							| 
									
										
										
										
											2024-09-16 14:09:37 +00:00
										 |  |  |     $widget['full'] = true; | 
					
						
							|  |  |  |     $widget['table_headers'] = array_keys($logs['records'][0]); | 
					
						
							|  |  |  |     $widget['table_records'] = $logs['records']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | $widget['pagination'] = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // display the widget
 | 
					
						
							|  |  |  | include '../app/templates/logs-list.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |