Browse Source

Implement JSON output

master
Les De Ridder 8 months ago
parent
commit
4ee2ffd30b

+ 56
- 15
app/Http/Controllers/PollController.php View File

@@ -7,6 +7,7 @@ use Carbon\Carbon;
7 7
 use Validator;
8 8
 use DB;
9 9
 use Cache;
10
+use Config;
10 11
 
11 12
 use App\Poll;
12 13
 use App\PollVote;
@@ -16,7 +17,11 @@ class PollController extends Controller
16 17
 {
17 18
     public function __invoke(Request $request)
18 19
     {
19
-        return view('create_poll');
20
+        if($request->format() == 'json') {
21
+            return response()->json(['timezone' => Config::get('app.timezone')]);
22
+        } else {
23
+            return view('create_poll');
24
+        }
20 25
     }
21 26
 
22 27
     public function create(Request $request)
@@ -76,14 +81,19 @@ class PollController extends Controller
76 81
         $new = $request->session()->pull('new', false);
77 82
 
78 83
         if($request->format() == 'json') {
79
-            if($new) {
80
-
81
-            } else {
82
-
84
+            $data = [
85
+                'id' => $poll->id,
86
+                'new' => $new,
87
+                'question' => $poll->question,
88
+                'options' => $poll->options->map(function($o) { return $o->makeHidden('poll_id'); }),
89
+                'multipleAnswersAllowed' => $poll->allow_multiple_answers
90
+            ];
91
+
92
+            if($new && $poll->duplicate_vote_checking == 'codes') {
93
+                $data['votingUrls'] = $poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); });
83 94
             }
84 95
 
85
-            //TODO: Implement JSON output
86
-            return null;
96
+            return response()->json($data);
87 97
         } else {
88 98
             return view('view_poll')
89 99
                 ->with('poll', $poll)
@@ -100,10 +110,33 @@ class PollController extends Controller
100 110
 
101 111
         $this->createPieChart($poll);
102 112
 
103
-        return view('view_poll_results')
104
-            ->with('poll', $poll)
105
-            ->with('voted', $voted)
106
-            ->with('alreadyClosed', $alreadyClosed);
113
+        if($request->format() == 'json') {
114
+            $data = [
115
+                'id' => $poll->id,
116
+                'voted' => $voted,
117
+                'alreadyClosed' => $alreadyClosed,
118
+                'resultsVisible' => $poll->results_visible
119
+            ];
120
+
121
+            if($poll->results_visible) {
122
+                $data['results'] = $poll->options->map(function($o) {
123
+                    $array = $o->makeHidden('poll')->makeHidden('poll_id')->append('vote_count')->toArray();
124
+
125
+                    //I really shouldn't have to do this...
126
+                    $array['voteCount'] = $array['vote_count'];
127
+                    unset($array['vote_count']);
128
+
129
+                    return $array;
130
+                });
131
+            }
132
+
133
+            return response()->json($data);
134
+        } else {
135
+            return view('view_poll_results')
136
+                ->with('poll', $poll)
137
+                ->with('voted', $voted)
138
+                ->with('alreadyClosed', $alreadyClosed);
139
+        }
107 140
     }
108 141
 
109 142
     private static function imageToDataUri($image)
@@ -149,14 +182,14 @@ class PollController extends Controller
149 182
         $colourSquareUris = [];
150 183
 
151 184
         $startDegrees = 0;
152
-        $sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); });
153
-        $nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; })->values();
185
+        $sortedOptions = $poll->options->sortByDesc(function($option) { return $option->vote_count; });
186
+        $nonZeroOptions = $sortedOptions->filter(function($option) { return $option->vote_count > 0; })->values();
154 187
         debug($nonZeroOptions);
155 188
         for($i = 0; $i < $nonZeroOptions->count(); $i++) {
156 189
             $option = $nonZeroOptions[$i];
157 190
 
158 191
             //TODO: Fix gaps
159
-            $degrees = round($poll->votes->where('poll_option_id', $option->id)->count() / $voteCount * 360);
192
+            $degrees = round($option->vote_count / $voteCount * 360);
160 193
             $endDegrees = min($startDegrees + $degrees, 360);
161 194
 
162 195
             $c = function($j) use($i, $baseColours, $nonZeroOptions) {
@@ -265,7 +298,15 @@ class PollController extends Controller
265 298
             return redirect()->action('PollController@viewResults', ['poll' => $poll]);
266 299
         }
267 300
 
268
-        return view('edit_poll')->with('poll', $poll)->with('changed', $changed)->with('extraCodes', $extraCodes);
301
+        if($request->format() == 'json') {
302
+            return response()->json([
303
+                "id" => $poll->id,
304
+                "changed" => $changed,
305
+                "extraVotingUrls" => collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })
306
+            ]);
307
+        } else {
308
+            return view('edit_poll')->with('poll', $poll)->with('changed', $changed)->with('extraCodes', $extraCodes);
309
+        }
269 310
     }
270 311
 
271 312
     public function edit(Request $request, Poll $poll)

+ 5
- 0
app/PollOption.php View File

@@ -14,4 +14,9 @@ class PollOption extends Model
14 14
     {
15 15
         return $this->belongsTo('App\Poll');
16 16
     }
17
+
18
+    public function getVoteCountAttribute()
19
+    {
20
+        return $this->poll->votes->where('poll_option_id', $this->id)->count();
21
+    }
17 22
 }

+ 1
- 1
resources/views/edit_poll.blade.php View File

@@ -14,7 +14,7 @@
14 14
                     <span>Your changes have been saved.</span>
15 15
                 @elseif ($extraCodes != null)
16 16
                     <span>Your extra voting URLs have been generated:</span>
17
-                    <textarea class="copyarea" readonly>{{collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n")}}</textarea>
17
+                    <textarea class="copyarea" readonly>{{ collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n") }}</textarea>
18 18
                 @endif
19 19
             </div>
20 20
 

+ 1
- 1
resources/views/view_poll.blade.php View File

@@ -24,7 +24,7 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
24 24
             <div class="some-top-margin">
25 25
                 @if ($poll->duplicate_vote_checking == 'codes')
26 26
                     <span>Voting URLs:</span>
27
-                    <textarea class="copyarea" readonly>{{$poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n")}}</textarea>
27
+                    <textarea class="copyarea" readonly>{{ $poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n") }}</textarea>
28 28
                 @else
29 29
                     <span>Poll URL: <a href="{{ action('PollController@view', ['poll' => $poll]) }}">{{ action('PollController@view', ['poll' => $poll]) }}</a></span>
30 30
                 @endif

+ 2
- 2
resources/views/view_poll_results.blade.php View File

@@ -19,8 +19,8 @@
19 19
 
20 20
     @if ($poll->results_visible)
21 21
         @php
22
-            $sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); });
23
-            $nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; });
22
+            $sortedOptions = $poll->options->sortByDesc(function($option) { return $option->vote_count; });
23
+            $nonZeroOptions = $sortedOptions->filter(function($option) { return $option->vote_count > 0; });
24 24
 
25 25
             $cache = Cache::get($poll->id);
26 26
 

Loading…
Cancel
Save