project_csv.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var resource $handle
  4. * @var object[] $fields
  5. * @var Test[] $tests
  6. * @var Template $template
  7. */
  8. $config = $template->getConfiguration();
  9. $field_names = [
  10. "Project ID", "Project Name",
  11. "Template Name",
  12. "Test ID", "Test Status", "Test Void", "Gaming Void", "Alcohol Void", "Test Result",
  13. "Location Name", "Legal Entity", "Address", "Email", "Phone", "Brand", "Region", "Internal ID", "Area Manager",
  14. "Tester Age", "Tester Gender", "Travel Costs",
  15. "Date", "Start Time", "End Time", "Duration",
  16. ];
  17. if($config->hasAlcohol ?? false) {
  18. array_splice($field_names, 6, 0, ["Gaming Void", "Alcohol Void"]);
  19. }
  20. foreach($fields as $field) {
  21. $field_names[] = $field->label;
  22. }
  23. fputcsv($handle, $field_names);
  24. foreach($tests as $test) {
  25. $project = $test->getProject();
  26. $location = $test->getLocation();
  27. $tester = $test->getTester();
  28. $values = $test->getValues(true);
  29. $test_date = "";
  30. $test_start = "";
  31. $test_end = "";
  32. $test_duration = "";
  33. $result = "Pending";
  34. $age = "";
  35. $gender = "";
  36. $travel_costs = "";
  37. if($test->isVoid()) {
  38. $gaming_void = "Y";
  39. $alcohol_void = "Y";
  40. }
  41. else {
  42. $gaming_void = $test->void_primary ? "Y" : "N";
  43. $alcohol_void = $test->void_secondary ? "Y" : "N";
  44. }
  45. $output = [];
  46. $output[] = $project->id;
  47. $output[] = $project->name;
  48. $output[] = $template->name;
  49. $output[] = $test->id;
  50. $output[] = $test->getStatusText();
  51. $output[] = $test->isVoid() ? "Y" : "N";
  52. if($config->hasAlcohol ?? false) {
  53. $output[] = $gaming_void;
  54. $output[] = $alcohol_void;
  55. }
  56. if(!is_null($test->pass)) {
  57. $result = $test->pass ? "Pass" : "Fail";
  58. $test_start_dt = new DateTime($test->dts_start);
  59. $test_end_dt = new DateTime($test->dts_end);
  60. $test_duration_diff = $test_start_dt->diff($test_end_dt, true);
  61. $test_date = $test_start_dt->format("d/m/Y");
  62. $test_start = $test_start_dt->format("H:i:s");
  63. $test_end = $test_end_dt->format("H:i:s");
  64. $test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
  65. if($tester) {
  66. $tester_dob_dt = new DateTime($tester->date_of_birth);
  67. $dob_diff = $test_start_dt->diff($tester_dob_dt, true);
  68. $age = $dob_diff->y;
  69. $gender = $tester->gender;
  70. $travel_costs = $test->test_travel_costs;
  71. }
  72. }
  73. $output[] = $result;
  74. $output[] = $location->name;
  75. $output[] = $location->legal_entity_name;
  76. $output[] = $location->getDisplayAddress();
  77. $output[] = $location->email;
  78. $output[] = $location->phone;
  79. $output[] = $location->brand;
  80. $output[] = $location->region;
  81. $output[] = $location->internal_id;
  82. $output[] = $location->area_manager;
  83. $output[] = $age;
  84. $output[] = $gender;
  85. $output[] = $travel_costs;
  86. $output[] = $test_date;
  87. $output[] = $test_start;
  88. $output[] = $test_end;
  89. $output[] = $test_duration;
  90. foreach($fields as $field) {
  91. $field_name = $field->name;
  92. $value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
  93. if(!empty($field->displayStrings)) {
  94. $index = array_search($value, $field->values);
  95. $value = $field->displayStrings[$index];
  96. }
  97. $output[] = $value;
  98. }
  99. fputcsv($handle, $output);
  100. }