project_customer_csv.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. $field_names = [
  3. "Test Result",
  4. "Location Name", "Address", "Region", "Internal ID", "Area Manager",
  5. "Date",
  6. ];
  7. foreach($fields as $field) {
  8. $customer_export = $field->customerExport ?? true;
  9. if(!$customer_export) continue;
  10. $field_names[] = $field->label;
  11. }
  12. fputcsv($handle, $field_names);
  13. foreach($tests as $test) {
  14. $project = $test->getProject();
  15. $location = $test->getLocation();
  16. $tester = $test->getTester();
  17. $values = $test->getValues(true);
  18. $test_date = "";
  19. $test_start = "";
  20. $test_end = "";
  21. $test_duration = "";
  22. $result = "Pending";
  23. $age = "";
  24. $gender = "";
  25. $travel_costs = "";
  26. $output = [];
  27. if(!is_null($test->pass)) {
  28. $result = $test->pass ? "Pass" : "Fail";
  29. $test_start_dt = new DateTime($test->dts_start);
  30. $test_end_dt = new DateTime($test->dts_end);
  31. $test_duration_diff = $test_start_dt->diff($test_end_dt, true);
  32. $test_date = $test_start_dt->format("d/m/Y");
  33. $test_start = $test_start_dt->format("H:i:s");
  34. $test_end = $test_end_dt->format("H:i:s");
  35. $test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
  36. if($tester) {
  37. $tester_dob_dt = new DateTime($tester->date_of_birth);
  38. $dob_diff = $test_start_dt->diff($tester_dob_dt, true);
  39. $age = $dob_diff->y;
  40. $gender = $tester->gender;
  41. $travel_costs = $test->test_travel_costs;
  42. }
  43. }
  44. $output[] = $result;
  45. $output[] = $location->name;
  46. $output[] = $location->getDisplayAddress();
  47. $output[] = $location->region;
  48. $output[] = $location->internal_id;
  49. $output[] = $location->area_manager;
  50. $output[] = $test_date;
  51. foreach($fields as $field) {
  52. $customer_export = $field->customerExport ?? true;
  53. if(!$customer_export) continue;
  54. $field_name = $field->name;
  55. $value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
  56. if(!empty($field->displayStrings)) {
  57. $index = array_search($value, $field->values);
  58. $value = $field->displayStrings[$index];
  59. }
  60. $output[] = $value;
  61. }
  62. fputcsv($handle, $output);
  63. }
  64. ?>