diff --git a/app/controllers/Reports.php b/app/controllers/Reports.php index 8f92841..50efc4d 100644 --- a/app/controllers/Reports.php +++ b/app/controllers/Reports.php @@ -1,10 +1,13 @@ $survey->title, "user" => User::info(), @@ -25,7 +28,7 @@ public function watch(int $surveyId) "surveyId" => $surveyId ]; - if(!$survey->anonymous) + if (!$survey->anonymous) $args["participators"] = Survey::participators($surveyId); $this->view("main", "reports", lang("reports"), $args); @@ -35,11 +38,11 @@ public function watch(int $surveyId) public function reset(int $surveyId) { $survey = Survey::existsByUserId(User::id(), "id", $surveyId); - if(!$survey) + if (!$survey) redirect(); $count = Survey::reset($surveyId); - if($count) + if ($count) success(refresh: true); getDataError(); @@ -49,34 +52,57 @@ public function reset(int $surveyId) public function csv(int $surveyId) { $survey = Survey::existsByUserId(User::id(), "id", $surveyId); - if(!$survey) + if (!$survey) redirect(); + ob_clean(); + header('Pragma: private'); header('Cache-control: private, must-revalidate'); - header('Content-type: text/csv'); + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $csvFileName = preg_replace('/[^A-Za-z0-9_-]/', '', str_replace(' ', '-', $survey->title)); - header('Content-Disposition: attachment; filename=' . $csvFileName . '.csv'); + header('Content-Disposition: attachment; filename=' . $csvFileName . '.xlsx'); + header('Cache-Control: max-age=0'); + $file = fopen('php://output', 'w'); - $fp = fopen('php://output', 'w'); + ################################################## - $report = Survey::report($survey); + $questionData = json_decode($survey->data); + $questions = array_map(fn($v) => strip_tags(trim($v->title)), $questionData); - #values - foreach($report as $title => $data) - { - foreach($data["answers"] as $answerTitle => $participateCount) - { - $answerData[] = $answerTitle; - $valueData[] = $participateCount; + $data = [ + array_merge(['Sicil No', 'Ad Soyad'], $questions) + ]; + + ################# + $participators = Survey::participators($surveyId); + + foreach ($participators as $key => $participator) { + if(!$participator->done) + continue; + + $inline = [ + $participator->id, + $participator->fullname + ]; + + + $answerData = json_decode($participator->data); + + foreach ($answerData as $questionKey => $answerKey) { + $question = current(array_filter($questionData, fn($val) => $val->slug == $questionKey)); + + $inline[] = strip_tags(trim($question->type == "textarea" ? $answerKey : $question->answers[$answerKey])); } - fputcsv($fp, [$title]); - fputcsv($fp, $answerData); - fputcsv($fp, $valueData); + $data[] = $inline; } - fclose($fp); - exit; + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->fromArray($data); + + $writer = new Xlsx($spreadsheet); + $writer->save('php://output'); } -} \ No newline at end of file +} diff --git a/app/models/Survey.php b/app/models/Survey.php index 0d693fc..1eaba7c 100644 --- a/app/models/Survey.php +++ b/app/models/Survey.php @@ -1,4 +1,5 @@ from("answers") ->join("personals", "personals.id = answers.personalId") - ->where("answers.surveyId", value:$surveyId) + ->where("answers.surveyId", value: $surveyId) ->where("answers.done", value: 1) ->results(); } @@ -76,7 +77,7 @@ public static function participateCount(int $userId) ->first(); } - public static function report($survey) + public static function generateReportData($survey) { $questionData = json_decode($survey->data); $answerData = Database::get() @@ -176,6 +177,13 @@ public static function report($survey) } } + return $generatedData; + } + + public static function report($survey) + { + $generatedData = self::generateReportData($survey); + $result = []; $group0Data = current($generatedData); @@ -204,7 +212,6 @@ public static function report($survey) } $result[$title]["total"] = $totalCount; - } else { $emptyCount = $fillCount = 0; foreach ($question as $answerK => $answerV) { @@ -214,7 +221,6 @@ public static function report($survey) $fillCount++; $result[$title]["list-json"][] = $answerV; } - } $result[$title]["answers"] = [ @@ -228,7 +234,6 @@ public static function report($survey) } $result[$title]["total"] = $totalCount; - } return $result; diff --git a/app/views/reports.php b/app/views/reports.php index 72c8b3d..8d8cf64 100644 --- a/app/views/reports.php +++ b/app/views/reports.php @@ -65,9 +65,9 @@
- Sonuçları CSV olarak dışarı aktar + Sonuçları excel formatında dışarı aktar