Jelajahi Sumber

Add support for customer user level

Alan Lam 8 tahun lalu
induk
melakukan
2aa1f9ad03

+ 16 - 13
controllers/account.php

@@ -2,6 +2,8 @@
 
 $page_title = "Account";
 
+$user = Core::$user;
+
 Core::addBreadcrumb("/account", $page_title);
 
 $users_pending_verification = 0;
@@ -10,28 +12,29 @@ $tests_remaining = 0;
 
 $icons = [];
 
-if(Core::$user->level >= User::LEVEL_ADMIN) {
+if($user->level >= User::LEVEL_ADMIN) {
 
 	$icons[] = new AccountIcon("Customers", "/account/customers", "warehouse");
 	$icons[] = new AccountIcon("Map Overview", "/account/map-overview", "map-marked");
-	$icons[] = new AccountIcon((Core::$user->level == User::LEVEL_SUPERADMIN ? "Users" : "Testers"), "/account/users", "users");
+	$icons[] = new AccountIcon(($user->level == User::LEVEL_SUPERADMIN ? "Users" : "Testers"), "/account/users", "users");
 
 	$users = User::query(["level" => User::LEVEL_UNVERIFIED]);
 	$tests = Test::query(["status" => Test::STATUS_PENDING_VERIFICATION]);
 
 	$users_pending_verification = count($users);
 	$tests_pending_verification = count($tests);
-}
 
-if(Core::$user->level >= User::LEVEL_CUSTOMER) {
-	$icons[] = new AccountIcon("Reports", "/account/reports", "chart-pie");
 	$icons[] = new AccountIcon("Projects", "/account/projects", "list");
 	$icons[] = new AccountIcon("Locations", "/account/locations", "map-marker");
 	$icons[] = new AccountIcon("Templates", "/account/templates", "file-alt");
 }
 
-if(Core::$user->level >= User::LEVEL_TESTER) {
-	$tests = Core::$user->getPendingTests();
+if($user->level >= User::LEVEL_CUSTOMER) {
+	$icons[] = new AccountIcon("Reports", "/account/reports", "chart-pie");
+}
+
+if($user->isTester() || $user->isAdmin()) {
+	$tests = $user->getPendingTests();
 	$tests_remaining = count($tests);
 
 	$tests_icon = new AccountIcon("Tests", "/account/tests", "clipboard-check");
@@ -41,16 +44,16 @@ if(Core::$user->level >= User::LEVEL_TESTER) {
 	$icons[] = new AccountIcon("Practice", "/account/practice", "chalkboard-teacher");
 }
 
-if(Core::$user->level == User::LEVEL_TESTER) {
+if($user->level == User::LEVEL_TESTER) {
 	$icons[] = new AccountIcon("Addresses", "/account/addresses", "address-book");
 	$icons[] = new AccountIcon("Tests (Completed)", "/account/tests/completed", "check");
 }
 
-if(Core::$user->level == User::LEVEL_REGISTERED) {
+if($user->level == User::LEVEL_REGISTERED) {
 	$icons[] = new AccountIcon("Submit ID", "/account/submit-id", "id-card");
 }
 
-if(!Core::$user->terms_accepted) {
+if(!$user->terms_accepted) {
 	$icons = [];
 }
 
@@ -62,10 +65,10 @@ usort($icons, function($a, $b) {
 
 // Actions
 $actions = [];
-if(Core::$user->level == User::LEVEL_DEACTIVATED) {
+if($user->level == User::LEVEL_DEACTIVATED) {
 	$actions[] = new AccountIcon("Reactivate Account", "/account/reactivate", "user-check");
 }
-else if(Core::$user->level == User::LEVEL_TESTER) {
+else if($user->level == User::LEVEL_TESTER) {
 	$actions[] = new AccountIcon("Deactivate Account...", "/account/deactivate", "user-times");
 }
 
@@ -85,7 +88,7 @@ if($tests_pending_verification) {
 	$to_do[] = $icon;
 }
 
-$referrals = Core::$user->getReferrals();
+$referrals = $user->getReferrals();
 
 View::includeOnce("header", compact("page_title"));
 View::include("account", compact("icons", "actions", "to_do", "referrals"));

+ 7 - 1
controllers/reports.php

@@ -29,6 +29,13 @@ $all_projects = Project::query([
 	"customer_id" => $customer->id
 ]);
 
+if(Core::$user->isCustomer()) {
+	// Filter to only projects that have past the deadline
+	$all_projects = array_filter($all_projects, function($project) {
+		return $project->isPastDeadline();
+	});
+}
+
 $data = [];
 $region_data = [];
 $area_manager_data = [];
@@ -84,7 +91,6 @@ if($submit) {
 		User::load(array_column($tests, "tester_id"));
 		$locations = Location::load(array_column($tests, "location_id"));
 
-
 		$regions = array_filter(array_column($locations, "region"));
 		$area_managers = array_filter(array_column($locations, "area_manager"));
 

+ 19 - 1
controllers/user_edit.php

@@ -8,6 +8,11 @@ if($user->level > Core::$user->level) $form_exception->addError("You do not have
 
 $page_title = "Edit User";
 
+$user_customer_link = UserCustomer::queryOne(["user_id" => $user->id]);
+if(!$user_customer_link) $user_customer_link = new UserCustomer;
+
+$customers = Customer::query([], ["sort" => "name"]);
+
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/users", "Users");
 Core::addBreadcrumb("/account/users/$id", $user->getDisplayName());
@@ -42,6 +47,19 @@ if($submit) {
 		$user->activated = http::value("activated", "int");
 		$user->can_claim = http::value("can_claim", "int");
 
+		$user_customer_link->delete();
+
+		$customer_id = http::value("customer_id", "int");
+		if($customer_id) {
+			$customer = Customer::get($customer_id);
+			if(!$customer) $form_exception->addError("Customer is invalid.");
+
+			$user_customer_link = new UserCustomer;
+			$user_customer_link->user_id = $user->id;
+			$user_customer_link->customer_id = $customer->id;
+			$user_customer_link->commit();
+		}
+
 		if($password && !User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
 		if(!isset(User::LEVELS[$user->level])) $form_exception->addError("User level is invalid.");
 		if($user->level > Core::$user->level) $form_exception->addError("You cannot elevate this user's level higher than your own.");
@@ -69,7 +87,7 @@ if($submit) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("user_edit_form", compact("user"));
+View::include("user_edit_form", compact("user", "customers", "user_customer_link"));
 View::includeOnce("footer");
 
 ?>

+ 5 - 0
engine/project.php

@@ -78,6 +78,11 @@ class Project extends Model {
 		}
 	}
 
+	public function isPastDeadline() {
+		$deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
+		return time() > $deadline_unix;
+	}
+
 }
 
 ?>

+ 12 - 0
engine/user.php

@@ -258,6 +258,18 @@ class User extends Model {
 		}
 	}
 
+	public function isAdmin() {
+		return ($this->level == User::LEVEL_ADMIN || $this->level == User::LEVEL_SUPERADMIN);
+	}
+
+	public function isCustomer() {
+		return $this->level == User::LEVEL_CUSTOMER;
+	}
+
+	public function isTester() {
+		return $this->level == User::LEVEL_TESTER;
+	}
+
 	public function getGenderText() {
 
 		if($this->gender == "M") return "Male";

+ 51 - 51
views/reports.php

@@ -173,63 +173,63 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 	</form>
 
 	<?php
-	if($locations_missing_region) {
-		?>
-		<div class="alert alert-warning d-print-none">
-			There are some locations that are missing "region" information:
-			<ul class="mb-0">
-			<?php
-			foreach($locations_missing_region as $location) {
-				$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
-				?>
-				<li>
-					<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
-					<?php
-					if($location->internal_id) {
-						?>
-						<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
-						<?php
-					}
-					?>
-				</li>
-				<?php
-			}
+	if(Core::$user->isAdmin()) {
+		if($locations_missing_region) {
 			?>
-			</ul>
-		</div>
-		<?php
-	}
-	?>
-
-	<?php
-	if($locations_missing_area_manager) {
-		?>
-		<div class="alert alert-warning d-print-none">
-			There are some locations that are missing "area manager" information:
-			<ul class="mb-0">
-			<?php
-			foreach($locations_missing_area_manager as $location) {
-				$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
-				?>
-				<li>
-					<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
-					<?php
-					if($location->internal_id) {
-						?>
-						<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
-						<?php
-					}
-					?>
-				</li>
+			<div class="alert alert-warning d-print-none">
+				There are some locations that are missing "region" information:
+				<ul class="mb-0">
 				<?php
-			}
+				foreach($locations_missing_region as $location) {
+					$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
+					?>
+					<li>
+						<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
+						<?php
+						if($location->internal_id) {
+							?>
+							<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
+							<?php
+						}
+						?>
+					</li>
+					<?php
+				}
+				?>
+				</ul>
+			</div>
+			<?php
+		}
+		if($locations_missing_area_manager) {
 			?>
-			</ul>
-		</div>
-		<?php
+			<div class="alert alert-warning d-print-none">
+				There are some locations that are missing "area manager" information:
+				<ul class="mb-0">
+				<?php
+				foreach($locations_missing_area_manager as $location) {
+					$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
+					?>
+					<li>
+						<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
+						<?php
+						if($location->internal_id) {
+							?>
+							<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
+							<?php
+						}
+						?>
+					</li>
+					<?php
+				}
+				?>
+				</ul>
+			</div>
+			<?php
+		}
 	}
 	?>
 
+
 	<?php
 	if($charts) {
 		?>

+ 27 - 0
views/user_edit_form.php

@@ -34,6 +34,32 @@ var userId = <?=$user->id?>;
 						</select>
 					</div>
 				</div>
+
+				<?php
+
+				if($user->isCustomer()) {
+					?>
+					<div class="row mb-1">
+						<label class="col-sm-4 col-form-label"  for="customer-id">Customer</label>
+						<div class="col-sm">
+							<select class="form-control" name="customer_id" id="customer-id" />
+								<option value="">Unassigned</option>
+								<?php
+								foreach($customers as $customer) {
+									$selected = ($customer->id == $user_customer_link->customer_id) ? "selected" : "";
+									?>
+									<option value="<?=htmlspecialchars($customer->id)?>" <?=$selected?>><?=htmlspecialchars($customer->name)?></option>
+									<?php
+								}
+								?>
+							</select>
+						</div>
+					</div>
+					<?php
+				}
+
+				?>
+
 				<div class="row mb-1">
 					<div class="col-sm offset-sm-4">
 						<div class="form-check">
@@ -207,6 +233,7 @@ var userId = <?=$user->id?>;
 					<?php
 				}
 				?>
+
 			</div>
 		</div>
 		<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Submit</button>