Skip to content

Commit

Permalink
add excel report system
Browse files Browse the repository at this point in the history
  • Loading branch information
SDClowen committed Aug 13, 2024
1 parent ecfd8a4 commit 650b6c4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 36 deletions.
72 changes: 49 additions & 23 deletions app/controllers/Reports.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace App\Controllers;

use App\Models\User;
use App\Models\Survey;
use Core\{Controller, Request};
use Core\{Controller, Request, Database};
use Core\Attributes\route;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Reports extends Controller
{
Expand All @@ -16,7 +19,7 @@ public function watch(int $surveyId)
redirect();

$result = Survey::report($survey);

$args = [
"surveyTitle" => $survey->title,
"user" => User::info(),
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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');
}
}
}
15 changes: 10 additions & 5 deletions app/models/Survey.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Models;

use Core\{Model, Database};
Expand Down Expand Up @@ -62,7 +63,7 @@ public static function participators(int $surveyId)
return Database::get()
->from("answers")
->join("personals", "personals.id = answers.personalId")
->where("answers.surveyId", value:$surveyId)
->where("answers.surveyId", value: $surveyId)
->where("answers.done", value: 1)
->results();
}
Expand All @@ -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()
Expand Down Expand Up @@ -176,6 +177,13 @@ public static function report($survey)
}
}

return $generatedData;
}

public static function report($survey)
{
$generatedData = self::generateReportData($survey);

$result = [];

$group0Data = current($generatedData);
Expand Down Expand Up @@ -204,7 +212,6 @@ public static function report($survey)
}

$result[$title]["total"] = $totalCount;

} else {
$emptyCount = $fillCount = 0;
foreach ($question as $answerK => $answerV) {
Expand All @@ -214,7 +221,6 @@ public static function report($survey)
$fillCount++;
$result[$title]["list-json"][] = $answerV;
}

}

$result[$title]["answers"] = [
Expand All @@ -228,7 +234,6 @@ public static function report($survey)
}

$result[$title]["total"] = $totalCount;

}

return $result;
Expand Down
4 changes: 2 additions & 2 deletions app/views/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
</g>
</svg>
<div class="ms-2 text-sm font-medium text-gray-900 dark:text-gray-300">
<div>CSV</div>
<div>EXCEL</div>
<p id="helper-radio-text-4" class="text-xs font-normal text-gray-500 dark:text-gray-300">
Sonuçları CSV olarak dışarı aktar
Sonuçları excel formatında dışarı aktar
</p>
</div>
</a>
Expand Down
4 changes: 2 additions & 2 deletions app/views/survey-crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ class="rounded-md bg-blue-600 dark:bg-blue-800 px-8 py-2 text-sm font-semibold t
plugins: [
'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
'anchor', 'searchreplace', 'visualblocks', 'fullscreen',
'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount'
'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount', 'iframe'
],
toolbar: 'undo redo | blocks | bold italic link | forecolor backcolor | image media | ' +
toolbar: 'undo redo | blocks | bold italic link | forecolor backcolor | insertfile image media | ' +
'alignleft aligncenter alignright alignjustify | ' +
'bullist numlist outdent indent | removeformat | help | code',
setup: function (editor) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"require": {
"sdclowen/sdphp": "dev-master",
"verot/class.upload.php": "^2.1"
"verot/class.upload.php": "^2.1",
"phpoffice/phpspreadsheet": "^2.2"
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
require_once "vendor/autoload.php";

$elapsedTime = \Core\App::weakup(__DIR__);
stackMessages("Page loaded in: $elapsedTime s", true);
#stackMessages("Page loaded in: $elapsedTime s", true);
4 changes: 2 additions & 2 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ $(function () {
plugins: [
'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
'anchor', 'searchreplace', 'visualblocks', 'fullscreen',
'insertdatetime', 'media', 'table'
'insertdatetime', 'media', 'table', 'iframe'
],
toolbar: 'blocks | bold italic underline link | forecolor backcolor | image media | ' +
toolbar: 'blocks | bold italic underline link | forecolor backcolor | insertfile image media | ' +
'alignleft aligncenter alignright alignjustify | ' +
'bullist numlist outdent indent | removeformat preview',
setup: function (editor) {
Expand Down

0 comments on commit 650b6c4

Please sign in to comment.