Browse Source

Add more fields to user export

Alan Lam 4 years ago
parent
commit
c46e1ef546
2 changed files with 27 additions and 7 deletions
  1. 18 7
      controllers/users_export.php
  2. 9 0
      engine/user.php

+ 18 - 7
controllers/users_export.php

@@ -1,4 +1,8 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var string $type
+ */
 
 switch($type) {
 
@@ -6,9 +10,9 @@ switch($type) {
 		header("Content-Type: text/csv; charset=utf-8");
 		http::setDownloadHeaders("email_addresses.csv");
 
-		$handle = fopen("php://output", "w");
+		$handle = fopen("php://output", "wb");
 
-		fputcsv($handle, ["first_name", "last_name", "email"]);
+		fputcsv($handle, ["id", "first_name", "last_name", "email", "date_of_birth", "date_last_test", "archived"]);
 
 		$nvp = [
 			"email" => (new Constraint("IS_NOT_NULL")),
@@ -16,12 +20,21 @@ switch($type) {
 		];
 
 		$level = http::value("level", "int");
-		if($level) $nvp["level"] = $level;
+		if($level) {
+			$nvp["level"] = $level;
+		}
 
 		$users = User::query($nvp);
 
 		foreach($users as $user) {
-			fputcsv($handle, [$user->first_name, $user->last_name, $user->email]);
+			$dt_start = $user->getDateTimeLastTest();
+
+			$date_last_test = null;
+			if($dt_start) {
+				$date_last_test = $dt_start->setTimezone(TIMEZONE)->format("d/m/Y");
+			}
+
+			fputcsv($handle, [$user->id, $user->first_name, $user->last_name, $user->email, $user->date_of_birth, $date_last_test, $user->archived]);
 		}
 
 		break;
@@ -29,5 +42,3 @@ switch($type) {
 	default:
 		throw new Exception("Invalid type.");
 }
-
-?>

+ 9 - 0
engine/user.php

@@ -773,4 +773,13 @@ class User extends Model {
 		return $this->archived;
 	}
 
+	/**
+	 * @return CarbonImmutable|null
+	 * @throws Exception
+	 */
+	public function getDateTimeLastTest(): ?CarbonImmutable {
+		$test = $this->getCompletedTests()[0] ?? null;
+		return $test ? $test->getDateTimeStart() : null;
+	}
+
 }