Просмотр исходного кода

Merge branch 'develop' into develop-performance

Alan Lam 8 лет назад
Родитель
Сommit
6ebff569d4

+ 6 - 1
controllers/account.php

@@ -6,6 +6,7 @@ Core::addBreadcrumb("/account", $page_title);
 
 $users_pending_verification = 0;
 $tests_pending_verification = 0;
+$tests_remaining = 0;
 
 $links = [];
 
@@ -31,6 +32,10 @@ if(Core::$user->level >= User::LEVEL_CUSTOMER) {
 if(Core::$user->level >= User::LEVEL_TESTER) {
 	$links["/account/tests"] = "Tests";
 	$links["/account/practice"] = "Practice";
+
+	$tests = Core::$user->getPendingTests();
+	$tests_remaining = count($tests);
+
 }
 
 if(Core::$user->level == User::LEVEL_TESTER) {
@@ -63,7 +68,7 @@ $actions["/account/delete"] = "Delete Account...";
 $referrals = Core::$user->getReferrals();
 
 View::includeOnce("header", compact("page_title"));
-View::include("account", compact("links", "actions", "referrals", "users_pending_verification", "tests_pending_verification"));
+View::include("account", compact("links", "actions", "referrals", "users_pending_verification", "tests_pending_verification", "tests_remaining"));
 View::includeOnce("footer");
 
 ?>

+ 5 - 6
controllers/address_add.php

@@ -18,15 +18,15 @@ if($submit) {
 		$user_address->user_id = Core::$user->id;
 		$user_address->name = http::value("name", "trim");
 		$user_address->postal_code = http::value("postal_code", "trim");
+		$user_address->country = http::value("country", "trim");
 
 		if(empty($user_address->name)) $form_exception->addError("Name is required.");
-		if(empty($user_address->postal_code)) {
-			$form_exception->addError("Postal code is required.");
-		}
+		if(empty($user_address->postal_code)) $form_exception->addError("Postal code is required.");
+		else if(empty($user_address->country)) $form_exception->addError("Country is required.");
 		else {
-			// Geocode the postal code
+			// Geocode the postal code and country
 			try {
-				$results = Geolocation::geocode($user_address->postal_code);
+				$results = Geolocation::geocode($user_address->postal_code . ", " . $user_address->country);
 
 				$result = $results[0];
 
@@ -36,7 +36,6 @@ if($submit) {
 					if(in_array("route", $types)) $user_address->address_1 = $component->long_name;
 					if(in_array("postal_town", $types)) $user_address->city = $component->long_name;
 					if(in_array("administrative_area_level_1", $types)) $user_address->state = $component->long_name;
-					if(in_array("country", $types)) $user_address->country = $component->long_name;
 				}
 
 				$user_address->latitude = $result->geometry->location->lat;

+ 2 - 0
controllers/address_edit.php

@@ -18,6 +18,7 @@ if($submit) {
 		$user_address->city = http::value("city", "trim");
 		$user_address->county = http::value("county", "trim");
 		$user_address->postal_code = http::value("postal_code", "trim");
+		$user_address->country = http::value("country", "trim");
 		$user_address->max_radius_miles = http::value("max_radius_miles", "float");
 
 		$user_address->latitude = http::value("latitude", "float");
@@ -27,6 +28,7 @@ if($submit) {
 		if(empty($user_address->address_1)) $form_exception->addError("Address line 1 is required.");
 		if(empty($user_address->city)) $form_exception->addError("City is required.");
 		if(empty($user_address->postal_code)) $form_exception->addError("Postal code is required.");
+		if(empty($user_address->country)) $form_exception->addError("Country is required.");
 		if(empty($user_address->max_radius_miles)) $form_exception->addError("Max distance is required.");
 		if(!in_array($user_address->max_radius_miles, UserAddress::RADIUSES)) $form_exception->addError("Max distance is invalid.");
 

+ 3 - 2
controllers/location_add.php

@@ -26,6 +26,7 @@ if($submit) {
 		$location->city = http::value("city", "trim");
 		$location->county = http::value("county", "trim");
 		$location->postal_code = http::value("postal_code", "trim");
+		$location->country = http::value("country", "trim");
 		$location->email = http::value("email", "email");
 		$location->phone = http::value("phone", "trim");
 		$location->opening_hours = http::value("opening_hours", "trim");
@@ -42,17 +43,17 @@ if($submit) {
 		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
 		if(empty($location->city)) $form_exception->addError("City is required.");
 		if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
+		if(empty($location->country)) $form_exception->addError("Country is required.");
 
 		// Geocode
 		try {
-			$results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]));
+			$results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code", "country"]));
 
 			$result = $results[0];
 
 			foreach($result->address_components as $component) {
 				$types = $component->types;
 				if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
-				if(in_array("country", $types)) $location->country = $component->long_name;
 			}
 
 			$location->latitude = $result->geometry->location->lat;

+ 2 - 0
controllers/location_edit.php

@@ -21,6 +21,7 @@ if($submit) {
 		$location->city = http::value("city", "trim");
 		$location->county = http::value("county", "trim");
 		$location->postal_code = http::value("postal_code", "trim");
+		$location->country = http::value("country", "trim");
 		$location->email = http::value("email", "email");
 		$location->phone = http::value("phone", "trim");
 		$location->opening_hours = http::value("opening_hours", "trim");
@@ -42,6 +43,7 @@ if($submit) {
 		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
 		if(empty($location->city)) $form_exception->addError("City is required.");
 		if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
+		if(empty($location->country)) $form_exception->addError("Country is required.");
 
 		if(empty($location->latitude)) $form_exception->addError("Map location is required.");
 		else if(empty($location->longitude)) $form_exception->addError("Map location is required.");

+ 3 - 2
controllers/project_ajax.php

@@ -90,11 +90,11 @@ if($submit) {
 				break;
 			}
 
-
 			// Gets testers near a project
 			case "get-testers": {
 
 				$location_id = http::value("location_id", "int");
+				$include_unverified = http::value("include_unverified", "int");
 				$location = Location::get($location_id);
 				if(!$location) throw new Exception("Location not found.");
 
@@ -157,9 +157,10 @@ if($submit) {
 					$is_current_tester = $user->id == $test->tester_id;
 					$is_deactivated = $user->level == User::LEVEL_DEACTIVATED;
 					$is_registered = $user->level == User::LEVEL_REGISTERED;
-					$is_unverified = $user->level == User::LEVEL_UNVERIFIED;
+					$is_unverified = $user->level <= User::LEVEL_UNVERIFIED;
 
 					if($is_deactivated) continue;
+					if(!$include_unverified && $is_unverified) continue;
 
 					$distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
 					$distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;

+ 4 - 1
controllers/project_csv.php

@@ -38,7 +38,7 @@ $field_names = [
 	"Template Name",
 	"Test ID", "Test Status", "Test Result",
 	"Location Name", "Legal Entity", "Address", "Email", "Phone", "Region", "Internal ID", "Area Manager",
-	"Tester Age", "Tester Gender",
+	"Tester Age", "Tester Gender", "Travel Costs",
 
 	"Date", "Start Time", "End Time", "Duration",
 ];
@@ -64,6 +64,7 @@ foreach($tests as $test) {
 
 	$age = "";
 	$gender = "";
+	$travel_costs = "";
 
 	$output = [];
 	$output[] = $id;
@@ -90,6 +91,7 @@ foreach($tests as $test) {
 			$dob_diff = $test_start_dt->diff($tester_dob_dt, true);
 			$age = $dob_diff->y;
 			$gender = $tester->gender;
+			$travel_costs = $test->test_travel_costs;
 		}
 	}
 
@@ -105,6 +107,7 @@ foreach($tests as $test) {
 	$output[] = $location->area_manager;
 	$output[] = $age;
 	$output[] = $gender;
+	$output[] = $travel_costs;
 	$output[] = $test_date;
 	$output[] = $test_start;
 	$output[] = $test_end;

+ 118 - 0
controllers/project_customer_csv.php

@@ -0,0 +1,118 @@
+<?php
+
+$project = Project::get($id);
+if(!$project) throw new Exception("Project cannot be found.");
+if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
+
+header("Content-Type: text/csv");
+header("Content-Disposition: attachment; filename=\"export.csv\"");
+header("Cache-Control: private, max-age=0, must-revalidate");
+header("Pragma: public");
+
+$template = $project->getTemplate();
+
+$object = json_decode($template->json);
+
+$fields_1 = $object->beforeTest;
+$fields_2 = $object->duringTest;
+
+$fields = array_merge($fields_1, $fields_2);
+$fields = array_filter($fields, function($field) {
+	if(!isset($field->name)) return false;
+	if($field->type == "file") return false;
+	return true;
+});
+
+$tests = Test::query([
+	"project_id" => $id,
+	"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+]);
+
+$locations = Location::load(array_column($tests, "location_id"));
+$testers = User::load(array_column($tests, "tester_id"));
+
+$handle = fopen("php://output", "w");
+
+$field_names = [
+	"Project Name",
+	"Test Status", "Test Result",
+	"Location Name", "Address", "Region", "Internal ID", "Area Manager",
+
+	"Date",
+];
+
+foreach($fields as $field) {
+	$field_names[] = $field->label;
+}
+
+fputcsv($handle, $field_names);
+
+foreach($tests as $test) {
+
+	$location = $test->getLocation();
+	$tester = $test->getTester();
+	$values = $test->getValues(true);
+
+	$test_date = "";
+	$test_start = "";
+	$test_end = "";
+	$test_duration = "";
+
+	$result = "Pending";
+
+	$age = "";
+	$gender = "";
+	$travel_costs = "";
+
+	$output = [];
+	$output[] = $project->name;
+	$output[] = $test->getStatusText();
+
+	if(!is_null($test->pass)) {
+
+		$result = $test->pass ? "Pass" : "Fail";
+
+		$test_start_dt = new DateTime($test->dts_start);
+		$test_end_dt = new DateTime($test->dts_end);
+		$test_duration_diff = $test_start_dt->diff($test_end_dt, true);
+
+		$test_date = $test_start_dt->format("d/m/Y");
+		$test_start = $test_start_dt->format("H:i:s");
+		$test_end = $test_end_dt->format("H:i:s");
+		$test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
+
+		if($tester) {
+			$tester_dob_dt = new DateTime($tester->date_of_birth);
+			$dob_diff = $test_start_dt->diff($tester_dob_dt, true);
+			$age = $dob_diff->y;
+			$gender = $tester->gender;
+			$travel_costs = $test->test_travel_costs;
+		}
+	}
+
+	$output[] = $result;
+	$output[] = $location->name;
+	$output[] = $location->getDisplayAddress();
+	$output[] = $location->region;
+	$output[] = $location->internal_id;
+	$output[] = $location->area_manager;
+	$output[] = $test_date;
+
+	foreach($fields as $field) {
+		$field_name = $field->name;
+		$value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
+
+		if(!empty($field->displayStrings)) {
+			$index = array_search($value, $field->values);
+			$value = $field->displayStrings[$index];
+		}
+
+		$output[] = $value;
+	}
+
+	fputcsv($handle, $output);
+}
+
+fclose($handle);
+
+?>

+ 4 - 2
controllers/register.php

@@ -35,6 +35,7 @@ try {
 		$user->dts_registered = gmdate("Y-m-d H:i:s");
 
 		$user_address->postal_code = http::value("postal_code");
+		$user_address->country = http::value("country");
 
 		// Referrer
 		$referrer_id = http::value("r", "int");
@@ -52,13 +53,14 @@ try {
 		if(empty($user->terms_accepted)) $form_exception->addError("Acceptance of Terms and Conditions is required.");
 
 		if(empty($user_address->postal_code)) $form_exception->addError("Postal code is required.");
+		if(empty($user_address->country)) $form_exception->addError("Country is required.");
 
 		$email_exists = User::query(["email" => $user->email]);
 		if($email_exists) $form_exception->addError("Email address is already registered.");
 
 		// Geocode the postal code
 		try {
-			$results = Geolocation::geocode($user_address->postal_code);
+			$results = Geolocation::geocode($user_address->postal_code . ", " . $user_address->country);
 
 			$result = $results[0];
 
@@ -75,7 +77,7 @@ try {
 			$user_address->longitude = $result->geometry->location->lng;
 		}
 		catch(Exception $e) {
-			$form_exception->addError("Failed to get location from postal code.");
+			$form_exception->addError("Failed to get location from postal code and country.");
 		}
 
 		if($form_exception->hasErrors()) {

+ 3 - 0
controllers/test_invoice.php

@@ -20,6 +20,9 @@ $travel_cost = (float) http::value("travel_cost", "float");
 
 $invoice_total = $fee + $play_spent + $travel_cost;
 
+$test->test_travel_cost = $travel_cost;
+$test->commit();
+
 $address = $tester->getInvoiceAddress();
 if(!$address) throw new Exception("Invoice address not found. Please set an address as the invoice address then try again.");
 if(!$tester->hasValidPaymentMethod()) throw new Exception("No valid payment method found. Please go to the Settings page and make sure all respective fields are filled in and then try again.");

+ 1 - 1
engine/test.php

@@ -4,7 +4,7 @@ class Test extends Model {
 
 	protected static $table = "test";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "test_fee", "test_time", "shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes", "dts_due", "dts_start", "dts_end", "verifier_id", "verifier_notes", "pass", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
+	protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "test_fee", "test_time", "test_travel_cost", "shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes", "dts_due", "dts_start", "dts_end", "verifier_id", "verifier_notes", "pass", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
 
 	const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
 

+ 5 - 2
public/js/app-project.js

@@ -56,7 +56,7 @@ $(function() {
 
 		return true;
 	});
-	$("#testers-max-distance").change(function() {
+	$("#testers-max-distance, #testers-include-unverified").change(function() {
 		if(selectedLocation) {
 			clearTesters();
 			initialiseTesters();
@@ -283,9 +283,12 @@ function initialiseTesters() {
 
 	$("#tester-list-loading-container").text("Loading available testers...");
 
+	let includeUnverified = $("#testers-include-unverified").val();
+
 	$.post("/account/projects/" + projectID + "/get-testers", {
 		submit: $("#submit").val(),
-		location_id: selectedLocation.id
+		location_id: selectedLocation.id,
+		include_unverified: includeUnverified
 	}, function(response) {
 		if(!response.success) return;
 		if(!response.testers || response.testers.length == 0) {

+ 1 - 0
routes.php

@@ -64,6 +64,7 @@ $routes = [
 	"/^account\/projects\/$id_pattern$/" => ["controller" => "project", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/$id_pattern\/edit$/" => ["controller" => "project_edit", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-csv$/" => ["controller" => "project_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/export-customer-csv$/" => ["controller" => "project_customer_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 
 	// Tests

+ 13 - 2
views/account.php

@@ -59,9 +59,9 @@
 		<div class="col-lg mb-3 mb-lg-0">
 
 			<?php
-			$has_to_do = !empty($users_pending_verification) || !empty($tests_pending_verification);
+			$has_to_do = !empty($users_pending_verification) || !empty($tests_pending_verification) || !empty($tests_remaining);
 
-			if($has_to_do && Core::$user->level >= User::LEVEL_ADMIN) {
+			if($has_to_do) {
 				?>
 				<h3>To-Do</h3>
 
@@ -88,6 +88,17 @@
 					</div>
 					<?php
 				}
+				if($tests_remaining) {
+					$s = ($tests_remaining == 1) ? "" : "s";
+					?>
+					<div class="alert alert-info mb-3 d-flex flex-row justify-content-between">
+						<div class="align-self-center">
+							<i class="fal fa-fw fa-info-circle"></i> <?=number_format($tests_remaining)?> test<?=$s?> remaining.
+						</div>
+						<a class="btn btn-primary" href="/account/tests">View</a>
+					</div>
+					<?php
+				}
 			}
 
 			if(Core::$user->level != User::LEVEL_DEACTIVATED && !Core::$user->terms_accepted) {

+ 4 - 0
views/address_add_form.php

@@ -11,6 +11,10 @@
 			<label for="postal-code">Postal Code</label>
 			<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($user_address->postal_code)?>" required />
 		</div>
+		<div class="form-group">
+			<label  for="country">Country</label>
+			<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+		</div>
 		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Add</button>
 	</form>
 </div>

+ 4 - 0
views/address_edit_form.php

@@ -29,6 +29,10 @@
 					<label for="postal-code">Postal Code</label>
 					<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($user_address->postal_code)?>" required />
 				</div>
+				<div class="form-group">
+					<label  for="country">Country</label>
+					<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+				</div>
 			</div>
 			<div class="col-lg">
 				<label>Map</label>

+ 6 - 0
views/location_form.php

@@ -75,6 +75,12 @@ $tabs = [
 								<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($location->postal_code)?>" required />
 							</div>
 						</div>
+						<div class="row mb-1">
+							<label class="col-sm-4 col-form-label" for="country">Country</label>
+							<div class="col-sm">
+								<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($location->country)?>" required />
+							</div>
+						</div>
 
 						<div class="row mb-1">
 							<label class="col-sm-4 col-form-label" for="email">Email Address</label>

+ 14 - 1
views/locations.php

@@ -27,7 +27,20 @@ var customerId = <?=$customer->id?>;
 				}
 				?>
 			</button>
-			<a class="btn btn-outline-success" href="/account/locations/add?customer_id=<?=$customer->id?>">Add</a>
+		</div>
+		<div class="col-auto">
+
+
+			<div class="dropdown">
+				<button class="btn btn-secondary dropdown-toggle" type="button" id="actions-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+					Actions
+				</button>
+				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
+					<a class="dropdown-item" href="/account/locations/add?customer_id=<?=$customer->id?>/edit">Add</a>
+					<a class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export">Export CSV</a>
+				</div>
+			</div>
+
 		</div>
 	</div>
 

+ 10 - 3
views/project.php

@@ -20,6 +20,7 @@ var projectID = <?=$project->id?>;
 				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/edit">Edit</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-csv">Export CSV</a>
+					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-customer-csv">Export Customer CSV</a>
 				</div>
 			</div>
 			<?php
@@ -155,17 +156,23 @@ var projectID = <?=$project->id?>;
 					<div class="input-group mb-3">
 						<div class="input-group-prepend">
 							<span class="input-group-text">
-								Filter
+								<i class="fal fa-filter"></i>
 							</span>
 						</div>
-						<input class="form-control" type="text" placeholder="Enter name or ID" data-filter-target="#testers-tbody" />
+						<input class="form-control" type="text" placeholder="Name / ID" data-filter-target="#testers-tbody" />
 					</div>
 				</div>
+				<div class="col-md">
+					<select class="form-control mb-3" id="testers-include-unverified">
+						<option value="0">Exc. Unverified</option>
+						<option value="1">Inc. Unverified</option>
+					</select>
+				</div>
 				<div class="col-md">
 					<div class="input-group mb-3">
 						<div class="input-group-prepend">
 							<span class="input-group-text">
-								Up to
+								<i class="fal fa-car"></i>
 							</span>
 						</div>
 						<select class="form-control" id="testers-max-distance">

+ 7 - 1
views/register.php

@@ -106,12 +106,18 @@
 						</select>
 					</div>
 				</div>
-				<div class="row mb-3">
+				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label"  for="postal-code">Postal Code</label>
 					<div class="col-sm">
 						<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($user_address->postal_code)?>" required />
 					</div>
 				</div>
+				<div class="row mb-3">
+					<label class="col-sm-4 col-form-label" for="country">Country</label>
+					<div class="col-sm">
+						<input class="form-control" type="text" name="country" id="country" placeholder="Enter country" value="<?=htmlspecialchars($user_address->country)?>" required />
+					</div>
+				</div>
 				<div class="row mb-1">
 					<div class="col-sm offset-sm-4">
 						<label>