Răsfoiți Sursa

Added pagination to tests page for admins.

Alan Lam 8 ani în urmă
părinte
comite
6fdf26ec73
6 a modificat fișierele cu 140 adăugiri și 8 ștergeri
  1. 27 4
      controllers/tests.php
  2. 3 0
      engine/core.php
  3. 8 0
      public/js/app.js
  4. 38 0
      views/modal_page_jump.php
  5. 38 0
      views/pagination.php
  6. 26 4
      views/test_list.php

+ 27 - 4
controllers/tests.php

@@ -7,6 +7,13 @@ Core::addBreadcrumb("/account/tests", $page_title);
 
 $user = Core::$user;
 
+// Pagination
+$page_size = 25;
+
+$page = http::value("page", "int");
+if($page <= 1) $page = 1;
+Core::$page = $page;
+
 // Admin / Super Admin
 if($user->level >= User::LEVEL_ADMIN) {
 
@@ -25,6 +32,16 @@ if($user->level >= User::LEVEL_ADMIN) {
 	if(!$filter_count) $filter_count = "";
 
 	$tests = Test::query($nvp, ["sort" => "status"]);
+	$test_count = count($tests);
+
+	$page_count = ceil($test_count / $page_size);
+	if($page_count <= 1) $page_count = 1;
+
+	Core::$page_count = $page_count;
+
+	$offset = ($page - 1) * $page_size;
+
+	$tests = array_slice($tests, $offset, $page_size);
 
 	$testers = User::load(array_column($tests, "tester_id"));
 	$projects = Project::load(array_column($tests, "project_id"));
@@ -33,9 +50,14 @@ if($user->level >= User::LEVEL_ADMIN) {
 		return $a->name <=> $b->name;
 	});
 
+	$item_number_first = $offset + 1;
+	$item_number_last = $offset + $page_size;
+	if($item_number_last > $test_count) $item_number_last = $test_count;
+
+	$pagination_string = "Showing $item_number_first-$item_number_last of $test_count tests";
+
 	View::includeOnce("header", compact("page_title"));
-	View::include("test_list", compact("tests", "projects", "project_id", "location_id", "test_id", "filter_count", "status"));
-	View::includeOnce("footer");
+	View::include("test_list", compact("tests", "test_count", "pagination_string", "projects", "project_id", "location_id", "test_id", "filter_count", "status"));
 }
 
 // HQ / Venue (view completed tests)
@@ -45,7 +67,6 @@ else if($user->level >= User::LEVEL_VENUE) {
 
 	View::includeOnce("header", compact("page_title"));
 	View::include("test_list_completed", compact("tests"));
-	View::includeOnce("footer");
 }
 
 // Tester
@@ -55,7 +76,9 @@ else if($user->level == User::LEVEL_TESTER) {
 
 	View::includeOnce("header", compact("page_title"));
 	View::include("test_list_assigned", compact("tests"));
-	View::includeOnce("footer");
 }
 
+View::includeOnce("modal_page_jump");
+View::includeOnce("footer");
+
 ?>

+ 3 - 0
engine/core.php

@@ -11,6 +11,9 @@ class Core {
 	public static $show_alerts = true;
 	public static $show_footer_content = true;
 
+	public static $page = 1;
+	public static $page_count = 1;
+
 	public static $scripts = [];
 
 	public static function addAlert($class, $message) {

+ 8 - 0
public/js/app.js

@@ -1,5 +1,13 @@
 $(function() {
 	$('[data-toggle="tooltip"]').tooltip();
+
+	$("#page-jump-button").click(function(event) {
+		let urlTemplate = $(this).data("url-template");
+		let page = $("#page-jump-input").val();
+		let url = urlTemplate.replace("PAGE", page);
+		window.location = url;
+	});
+
 });
 
 var maps = [];

+ 38 - 0
views/modal_page_jump.php

@@ -0,0 +1,38 @@
+<?php
+
+$uri = $_SERVER["REQUEST_URI"];
+list($uri) = explode("?", $uri);
+
+$query = $_GET;
+unset($query["path"]);
+$query["page"] = "PAGE";
+
+$url = $uri . "?" . http_build_query($query);
+
+?>
+<div class="modal fade" id="modal-page-jump" tabindex="-1">
+	<div class="modal-dialog">
+		<div class="modal-content">
+			<div class="modal-header">
+				<h5 class="modal-title"><strong>Jump to page</strong></h5>
+				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
+					<span aria-hidden="true">&times;</span>
+				</button>
+
+			</div>
+			<div class="modal-body">
+				<div class="form-group">
+					<div class="input-group">
+						<input class="form-control" type="number" id="page-jump-input" min="1" max="<?=Core::$page_count?>" value="<?=Core::$page?>" placeholder="Enter page number" required />
+						<span class="input-group-append">
+							<button type="button" class="btn btn-primary" id="page-jump-button" data-url-template="<?=$url?>">Jump</button>
+						</span>
+					</div>
+					<small class="form-text text-muted">
+						Range is 1 to <?=Core::$page_count?>.
+					</small>
+				</div>
+			</div>
+		</div>
+	</div>
+</div>

+ 38 - 0
views/pagination.php

@@ -0,0 +1,38 @@
+<?php
+
+$page = Core::$page;
+$page_count = Core::$page_count;
+
+$uri = $_SERVER["REQUEST_URI"];
+$parts = explode("?", $uri);
+
+$path = $parts[0];
+$query_string = $parts[1] ?? "";
+parse_str($query_string, $nvp);
+
+if($page_count == 1) return;
+
+$previous_disabled = ($page <= 1) ? "disabled" : "";
+$next_disabled = ($page >= $page_count) ? "disabled" : "";
+
+$previous_url = $path . "?" . http_build_query(array_merge($nvp, ["page" => $page - 1]));
+$next_url = $path . "?" . http_build_query(array_merge($nvp, ["page" => $page + 1]));
+
+?>
+<nav aria-label="Page navigation">
+	<ul class="pagination">
+		<li class="page-item <?=$previous_disabled?>">
+			<a class="page-link" href="<?=htmlspecialchars($previous_url)?>">
+				<span aria-hidden="true">&laquo;</span>
+				<span class="sr-only">Previous</span>
+			</a>
+		</li>
+		<li class="page-item"><a class="page-link" href="#" data-toggle="modal" data-target="#modal-page-jump">Page <?=number_format($page)?> of <?=number_format($page_count)?></a></li>
+		<li class="page-item <?=$next_disabled?>">
+			<a class="page-link" href="<?=htmlspecialchars($next_url)?>">
+				<span aria-hidden="true">&raquo;</span>
+				<span class="sr-only">Next</span>
+			</a>
+		</li>
+	</ul>
+</nav>

+ 26 - 4
views/test_list.php

@@ -6,7 +6,9 @@ $filters_collapse = $filter_count ? "collapse show" : "collapse";
 
 	<div class="row">
 		<div class="col">
-			<h2>Tests</h2>
+			<h2>
+				Tests
+			</h2>
 		</div>
 		<div class="col-auto">
 			<a class="btn btn-outline-primary" href="#filters" data-toggle="collapse">
@@ -87,11 +89,19 @@ $filters_collapse = $filter_count ? "collapse show" : "collapse";
 	else {
 		?>
 
-		<div class="alert alert-info">
-			Showing <?=number_format(count($tests))?> test(s).
+		<div class="row">
+			<div class="col-md text-center text-md-left align-self-md-center mb-3">
+				<?=htmlspecialchars($pagination_string)?>
+			</div>
+			<div class="col-md-auto d-flex justify-content-center">
+				<?php
+				View::include("pagination");
+				?>
+			</div>
 		</div>
 
-		<div class="table-responsive">
+
+		<div class="table-responsive mb-3">
 
 			<table class="table table-sm table-hover" style="min-width: 1024px;">
 				<thead>
@@ -174,6 +184,18 @@ $filters_collapse = $filter_count ? "collapse show" : "collapse";
 				</tbody>
 			</table>
 		</div>
+
+
+		<div class="row">
+			<div class="col-md text-center text-md-left align-self-md-center mb-3">
+				<?=htmlspecialchars($pagination_string)?>
+			</div>
+			<div class="col-md-auto d-flex justify-content-center">
+				<?php
+				View::include("pagination");
+				?>
+			</div>
+		</div>
 		<?php
 	}