Răsfoiți Sursa

Adds support for sending out test result emails to customers and/or locations.

Alan Lam 8 ani în urmă
părinte
comite
fe0e7e546a
5 a modificat fișierele cu 119 adăugiri și 9 ștergeri
  1. 24 2
      controllers/test.php
  2. 7 4
      controllers/test_pdf.php
  3. 4 0
      engine/email.php
  4. 46 0
      engine/test.php
  5. 38 3
      views/test_verify_form.php

+ 24 - 2
controllers/test.php

@@ -11,6 +11,7 @@ $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
 
 $location = $test->getLocation();
 $project = $test->getProject();
+$customer = $project->getCustomer();
 $tester = $test->getTester();
 $template = $project->getTemplate();
 
@@ -276,7 +277,6 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 			// Accept test as completed
 			if($accept) {
 				$test->status = Test::STATUS_COMPLETED;
-				// TODO: Send email to venue/HQ/tester
 			}
 
 			// Reject test
@@ -304,6 +304,28 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 			if($accept) {
 				$tester->score++;
 				$tester->commit();
+
+				// Save PDF
+				$pdf = $test->getPDF();
+				$pdf_string = $pdf->Output("S");
+
+				$directory = $test->getDirectory();
+				$pdf_path = $directory . "/test.pdf";
+
+				$success = file_put_contents($pdf_path, $pdf_string);
+
+				// TODO: Send email to venue/HQ/tester
+				$email_customer = http::value("email_customer");
+				$email_location = http::value("email_location");
+
+				if($email_customer && $customer->email_after_test && $customer->email) {
+					$test->sendResultEmail($customer->email);
+				}
+
+				if($email_location && $customer->email_locations_after_test && $location->email) {
+					$test->sendResultEmail($location->email);
+				}
+
 			}
 
 			Core::addAlert("success", "Test verified successfully.");
@@ -318,7 +340,7 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 	}
 
 	View::includeOnce("header", compact("page_title"));
-	View::include("test_verify_form", compact("test", "tester", "location", "project", "template", "fields", "values", "tester_message"));
+	View::include("test_verify_form", compact("test", "tester", "location", "project", "customer", "template", "fields", "values", "tester_message"));
 	View::includeOnce("footer");
 	return;
 }

+ 7 - 4
controllers/test_pdf.php

@@ -1,8 +1,12 @@
 <?php
 
+// Controller may be included directly and $pdf can be accessed.
+
 $test = Test::get($id);
 if(!$test) throw new Exception("Test not found.");
 
+if(!isset($output)) $output = true;
+
 $location = $test->getLocation();
 $tester = $test->getTester();
 
@@ -382,10 +386,9 @@ $pdf->SetFont("Arial", "", $small_font_size);
 $pdf->MultiCell(95, 4, $values["think_21_poster_notes"] ?? "N/A", 0, 1);
 $pdf->Line($pdf->GetX(), $pdf->GetY(), $pdf->GetX() + 92.5, $pdf->GetY());
 
-
-
 // Output
-
-$pdf->Output();
+if($output) {
+	$pdf->Output();
+}
 
 ?>

+ 4 - 0
engine/email.php

@@ -58,6 +58,10 @@ class Email {
 		return true;
 	}
 
+	public function addAttachment($path, $name = "", $encoding = "base64", $type = "", $disposition = "attachment") {
+		return $this->instance->addAttachment($path, $name, $encoding, $type, $disposition);
+	}
+
 	public function setFrom($email, $name="") {
 		return $this->instance->setFrom($email, $name);
 	}

+ 46 - 0
engine/test.php

@@ -116,6 +116,45 @@ class Test extends Model {
 		$mail->send();
 	}
 
+	public function sendResultEmail($to_address) {
+
+		// Subject Format: {pass-text} - {stage-number} - Check Policy visit to: {internal-id} : {name} : {city} ({dd/mm/yyyy} {hh:mm})
+
+		$location = $this->getLocation();
+
+		$pdf_path = $this->getDirectory() . "/test.pdf";
+
+		// Content
+		$name = htmlspecialchars($this->first_name);
+		$link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
+
+		$pass_text = strtoupper($this->getPassText());
+		$stage = $this->challenge_stage;
+
+		$location_parts = [$location->internal_id, $location->name, $location->city];
+		$location_string = implode(" : ", array_filter($location_parts));
+
+		$unix_start = strtotime($this->dts_start . " UTC");
+		$date_time = date("d/m/Y H:i", $unix_start);
+
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addLeadParagraph("Congratulations!<br />A test has been completed at your location.");
+		$builder->addParagraph("Please see attached PDF and do not hesitate to contact us if you have any questions.");
+		$content = $builder->build();
+
+		// Send
+		$mail = new Email;
+		$mail->setHtmlContent($content);
+		$mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
+		$mail->addAddress($to_address);
+		$mail->addAttachment($pdf_path);
+
+		$mail->setSubject("$pass_text - $stage - Check Policy visit to: $location_string $date_time");
+
+		$mail->send();
+	}
+
 	public function getChallengeStageText() {
 		return Test::STAGES[$this->challenge_stage];
 	}
@@ -210,6 +249,13 @@ class Test extends Model {
 		return $this->pass ? "Pass" : "Fail";
 	}
 
+	public function getPDF() {
+		$id = $this->id;
+		$output = false;
+		require ROOT_DIR . "/controllers/test_pdf.php";
+		return $pdf;
+	}
+
 	public function assignTester($tester) {
 		$this->tester_id = $tester->id;
 		$this->dts_assigned = Util::getDateTime();

+ 38 - 3
views/test_verify_form.php

@@ -282,6 +282,32 @@ function render_card_list_group($details) {
 
 						<?php
 						if($test->status == Test::STATUS_PENDING_VERIFICATION) {
+							?>
+							<?php
+							if($customer->email) {
+								?>
+								<div class="row mb-1">
+									<div class="col-sm offset-sm-4">
+										<div class="form-check">
+											<input class="form-check-input" type="checkbox" name="email_customer" id="email-customer" value="1" <?=Form::checked($customer->email_after_test)?> />
+											<label class="form-check-label" for="email-customer">Email Headquarters (<?=htmlspecialchars($customer->email)?>)</label>
+										</div>
+									</div>
+								</div>
+								<?php
+							}
+							if($location->email) {
+								?>
+								<div class="row mb-1">
+									<div class="col-sm offset-sm-4">
+										<div class="form-check">
+											<input class="form-check-input" type="checkbox" name="email_location" id="email-location" value="1" <?=Form::checked($customer->email_locations_after_test)?> />
+											<label class="form-check-label" for="email-location">Email Venue (<?=htmlspecialchars($location->email)?>)</label>
+										</div>
+									</div>
+								</div>
+								<?php
+							}
 							?>
 							<div class="row mb-1">
 								<label class="col-sm-4 col-form-label" for="tester-message">Message to Tester (if rejecting)</label>
@@ -289,13 +315,22 @@ function render_card_list_group($details) {
 									<textarea class="form-control" name="tester_message" id="tester-message" placeholder="Enter message to tester" value="<?=htmlspecialchars($tester_message)?>" rows="4"></textarea>
 								</div>
 							</div>
-							<button class="btn btn-success" type="submit" name="accept" value="1">Accept</button>
-							<button class="btn btn-danger" type="submit" name="reject" value="1">Reject</button>
+
+							<div class="row mb-1">
+								<div class="col-sm offset-sm-4">
+									<button class="btn btn-success" type="submit" name="accept" value="1">Accept</button>
+									<button class="btn btn-danger" type="submit" name="reject" value="1">Reject</button>
+								</div>
+							</div>
 							<?php
 						}
 						else {
 							?>
-							<button class="btn btn-primary" type="submit">Submit</button>
+							<div class="row mb-1">
+								<div class="col-sm offset-sm-4">
+									<button class="btn btn-primary" type="submit">Submit</button>
+								</div>
+							</div>
 							<?php
 						}
 						?>