diff --git a/app/Http/Controllers/PollController.php b/app/Http/Controllers/PollController.php index 900236a..657a40b 100644 --- a/app/Http/Controllers/PollController.php +++ b/app/Http/Controllers/PollController.php @@ -102,6 +102,18 @@ class PollController extends Controller ->with('voted', $voted); } + private static function imageToDataUri($image) + { + ob_start(); + + imagepng($image); + $dataUri = "data:image/png;base64," . base64_encode(ob_get_contents()); + + ob_end_clean(); + + return $dataUri; + } + private function createPieChart(Poll $poll) { $voteCount = $poll->votes->count(); @@ -122,19 +134,21 @@ class PollController extends Controller $chartWidth = $width - 2 * $padding; $chartHeight = $height - 2 * $padding; + $colourSquareSize = 13; + $pieChart = imagecreatetruecolor($width, $height); - imagefill($pieChart, 0, 0, imagecolorallocate($pieChart, 0xFF, 0xFF, 0xFF)); + $transparent = imagecolorallocatealpha($pieChart, 0xFF, 0xFF, 0xFF, 0x7F); + imagefill($pieChart, 0, 0, $transparent); + imagesavealpha($pieChart, true); imageantialias($pieChart, true); - $primary = imagecolorallocate($pieChart, 0xE8, 0x3F, 0xB8); - - $colours = []; + $colourSquareUris = []; $startDegrees = 0; $sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); }); $nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; })->values(); debug($nonZeroOptions); - for($i = 0; $i < $poll->options->count(); $i++) { + for($i = 0; $i < $nonZeroOptions->count(); $i++) { $option = $nonZeroOptions[$i]; //TODO: Fix gaps @@ -147,27 +161,31 @@ class PollController extends Controller * floor($i / count($baseColours)) / (floor($nonZeroOptions->count() / count($baseColours)) + 1); }; $colour = imagecolorallocate($pieChart, $c(0), $c(1), $c(2)); - $colours[$option->id] = '#' . dechex($c(0) << 16 | $c(1) << 8 | $c(2) << 0); debug([$option->text, [$startDegrees, $endDegrees], [$c(0), $c(1), $c(2)]]); imagefilledarc($pieChart, $width / 2, $height / 2, $chartWidth, $chartHeight, $startDegrees, $endDegrees, $colour, IMG_ARC_PIE); + $colourSquare = imagecreatetruecolor($colourSquareSize, $colourSquareSize); + $colourSquareColour = imagecolorallocate($colourSquare, $c(0), $c(1), $c(2)); + imagefill($colourSquare, 0, 0, $colourSquareColour); + $colourSquareUris[$option->id] = PollController::imageToDataUri($colourSquare); + $startDegrees = $endDegrees; } - debug($colours); + debug($colourSquareUris); $resized = imagecreatetruecolor($width / $supersamplingFactor, $height / $supersamplingFactor); + imagecolortransparent($resized, imagecolorallocatealpha($resized, 0, 0, 0, 0x7F)); + imagealphablending($resized, false); + imagesavealpha($resized, true); imagecopyresampled($resized, $pieChart, 0, 0, 0, 0, $width / $supersamplingFactor, $height / $supersamplingFactor, $width, $height); $pieChart = $resized; - ob_start(); - imagepng($pieChart); - $dataUri = "data:image/png;base64," . base64_encode(ob_get_contents()); - ob_end_clean(); + $dataUri = PollController::imageToDataUri($pieChart); - Cache::put($poll->id, ['vote_count' => $voteCount, 'pie_chart' => $dataUri, 'colours' => $colours], now()->addDays(1)); + Cache::put($poll->id, ['vote_count' => $voteCount, 'pie_chart' => $dataUri, 'colour_squares' => $colourSquareUris], now()->addDays(1)); } public function hasVoted(Request $request, Poll $poll) diff --git a/resources/views/create_poll.blade.php b/resources/views/create_poll.blade.php index 848ec53..b35edad 100644 --- a/resources/views/create_poll.blade.php +++ b/resources/views/create_poll.blade.php @@ -10,6 +10,8 @@
+ Question:
+
@@ -17,6 +19,8 @@
+ Options:
+ @for ($i = 0; $i < 5; $i++)
@@ -30,6 +34,8 @@
@endfor + +

@@ -56,6 +62,8 @@ + +

@@ -80,9 +88,12 @@ codes +
+

+
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index f6de717..a6f9787 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -16,6 +16,7 @@ .some-top-margin { margin-top: 2.0rem; } .some-more-bottom-margin { margin-bottom: 2.0rem; } .inline-block { display: inline-block; } + .text-browser { display: none; } .primary-box { padding: 1rem 1rem 1rem 1rem!important; @@ -118,6 +119,8 @@ @yield('content') +

+ diff --git a/resources/views/view_poll.blade.php b/resources/views/view_poll.blade.php index 1ea1a64..c849f33 100644 --- a/resources/views/view_poll.blade.php +++ b/resources/views/view_poll.blade.php @@ -11,6 +11,8 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
Your poll has been created!
+

+
@if ($poll->duplicate_vote_checking == 'codes') Voting URLs: @@ -24,18 +26,22 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
@if ($hasVoted) - @if (!$new || $poll->duplicate_vote_checking != 'codes') -
- You have already voted on this poll or need a code to vote. -
+ @if (!$new || $poll->duplicate_vote_checking != 'codes') +

- @if ($poll->results_visible) -
- Results -
- @endif +
+ You have already voted on this poll or need a code to vote. +
+ + @if ($poll->results_visible) +
+ Results +
@endif + @endif @else +

+
@csrf diff --git a/resources/views/view_poll_results.blade.php b/resources/views/view_poll_results.blade.php index 2922987..26b92ff 100644 --- a/resources/views/view_poll_results.blade.php +++ b/resources/views/view_poll_results.blade.php @@ -7,16 +7,23 @@
Your vote has been recorded!
+ +

@endif @if($poll->results_visible) - @php ($sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); })) - @php ($nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; })) + @php + $sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); }); + $nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; }); + + $cache = Cache::get($poll->id); + + $total = $cache['vote_count']; + @endphp
- @php ($total = $poll->votes->count()) @foreach ($sortedOptions as $option) @php ($votes = $poll->votes->where('poll_option_id', $option->id)->count()) @@ -24,11 +31,15 @@
{{ $option->text }} + : {{ $votes }} votes
- -
+
+
+ {{ $total == 0 ? 0 : round($votes / $total * 100, 2) }}% of votes +

+
@@ -40,18 +51,23 @@
- @php ($cache = Cache::get($poll->id)) -
- -
- @foreach ($nonZeroOptions as $option) - -
- {{ $option->text }} -
- @endforeach -
+ @if ($total != 0) +
+ + +
+ +

+ + @foreach ($nonZeroOptions as $option) + + {{ $option->text }} + +
+ @endforeach +
+ @endif
@else