project_csv.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. $project = Project::get($id);
  3. if(!$project) throw new Exception("Project cannot be found.");
  4. if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
  5. header("Content-Type: text/csv");
  6. header("Content-Disposition: attachment; filename=\"export.csv\"");
  7. header("Cache-Control: private, max-age=0, must-revalidate");
  8. header("Pragma: public");
  9. $template = $project->getTemplate();
  10. $object = json_decode($template->json);
  11. $fields_1 = $object->beforeTest;
  12. $fields_2 = $object->duringTest;
  13. $fields = array_merge($fields_1, $fields_2);
  14. $fields = array_filter($fields, function($field) {
  15. if(!isset($field->name)) return false;
  16. if($field->type == "file") return false;
  17. return true;
  18. });
  19. $tests = Test::query([
  20. "project_id" => $id,
  21. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  22. ]);
  23. $locations = Location::load(array_column($tests, "location_id"));
  24. $testers = User::load(array_column($tests, "tester_id"));
  25. $handle = fopen("php://output", "w");
  26. $field_names = [
  27. "Project ID", "Project Name",
  28. "Template Name",
  29. "Test ID", "Test Status", "Test Result",
  30. "Location Name", "Legal Entity", "Address", "Email", "Phone", "Region", "Internal ID", "Area Manager",
  31. "Tester Age", "Tester Gender",
  32. "Date", "Start Time", "End Time", "Duration",
  33. ];
  34. foreach($fields as $field) {
  35. $field_names[] = $field->label;
  36. }
  37. fputcsv($handle, $field_names);
  38. foreach($tests as $test) {
  39. $location = $test->getLocation();
  40. $tester = $test->getTester();
  41. $values = $test->getValues(true);
  42. $test_date = "";
  43. $test_start = "";
  44. $test_end = "";
  45. $test_duration = "";
  46. $result = "Pending";
  47. $age = "";
  48. $gender = "";
  49. $output = [];
  50. $output[] = $id;
  51. $output[] = $project->name;
  52. $output[] = $template->name;
  53. $output[] = $test->id;
  54. $output[] = $test->getStatusText();
  55. if(!is_null($test->pass)) {
  56. $result = $test->pass ? "Pass" : "Fail";
  57. $test_start_dt = new DateTime($test->dts_start);
  58. $test_end_dt = new DateTime($test->dts_end);
  59. $test_duration_diff = $test_start_dt->diff($test_end_dt, true);
  60. $test_date = $test_start_dt->format("d/m/Y");
  61. $test_start = $test_start_dt->format("H:i:s");
  62. $test_end = $test_end_dt->format("H:i:s");
  63. $test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
  64. if($tester) {
  65. $tester_dob_dt = new DateTime($tester->date_of_birth);
  66. $dob_diff = $test_start_dt->diff($tester_dob_dt, true);
  67. $age = $dob_diff->y;
  68. $gender = $tester->gender;
  69. }
  70. }
  71. $output[] = $result;
  72. $output[] = $location->name;
  73. $output[] = $location->legal_entity_name;
  74. $output[] = $location->getDisplayAddress();
  75. $output[] = $location->email;
  76. $output[] = $location->phone;
  77. $output[] = $location->region;
  78. $output[] = $location->internal_id;
  79. $output[] = $location->area_manager;
  80. $output[] = $age;
  81. $output[] = $gender;
  82. $output[] = $test_date;
  83. $output[] = $test_start;
  84. $output[] = $test_end;
  85. $output[] = $test_duration;
  86. foreach($fields as $field) {
  87. $field_name = $field->name;
  88. $value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
  89. if(!empty($field->displayStrings)) {
  90. $index = array_search($value, $field->values);
  91. $value = $field->displayStrings[$index];
  92. }
  93. $output[] = $value;
  94. }
  95. fputcsv($handle, $output);
  96. }
  97. fclose($handle);
  98. ?>