Implement JSON output
This commit is contained in:
parent
3416f731e7
commit
4ee2ffd30b
|
@ -7,6 +7,7 @@ use Carbon\Carbon;
|
||||||
use Validator;
|
use Validator;
|
||||||
use DB;
|
use DB;
|
||||||
use Cache;
|
use Cache;
|
||||||
|
use Config;
|
||||||
|
|
||||||
use App\Poll;
|
use App\Poll;
|
||||||
use App\PollVote;
|
use App\PollVote;
|
||||||
|
@ -16,7 +17,11 @@ class PollController extends Controller
|
||||||
{
|
{
|
||||||
public function __invoke(Request $request)
|
public function __invoke(Request $request)
|
||||||
{
|
{
|
||||||
return view('create_poll');
|
if($request->format() == 'json') {
|
||||||
|
return response()->json(['timezone' => Config::get('app.timezone')]);
|
||||||
|
} else {
|
||||||
|
return view('create_poll');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(Request $request)
|
public function create(Request $request)
|
||||||
|
@ -76,14 +81,19 @@ class PollController extends Controller
|
||||||
$new = $request->session()->pull('new', false);
|
$new = $request->session()->pull('new', false);
|
||||||
|
|
||||||
if($request->format() == 'json') {
|
if($request->format() == 'json') {
|
||||||
if($new) {
|
$data = [
|
||||||
|
'id' => $poll->id,
|
||||||
} else {
|
'new' => $new,
|
||||||
|
'question' => $poll->question,
|
||||||
|
'options' => $poll->options->map(function($o) { return $o->makeHidden('poll_id'); }),
|
||||||
|
'multipleAnswersAllowed' => $poll->allow_multiple_answers
|
||||||
|
];
|
||||||
|
|
||||||
|
if($new && $poll->duplicate_vote_checking == 'codes') {
|
||||||
|
$data['votingUrls'] = $poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); });
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Implement JSON output
|
return response()->json($data);
|
||||||
return null;
|
|
||||||
} else {
|
} else {
|
||||||
return view('view_poll')
|
return view('view_poll')
|
||||||
->with('poll', $poll)
|
->with('poll', $poll)
|
||||||
|
@ -100,10 +110,33 @@ class PollController extends Controller
|
||||||
|
|
||||||
$this->createPieChart($poll);
|
$this->createPieChart($poll);
|
||||||
|
|
||||||
return view('view_poll_results')
|
if($request->format() == 'json') {
|
||||||
->with('poll', $poll)
|
$data = [
|
||||||
->with('voted', $voted)
|
'id' => $poll->id,
|
||||||
->with('alreadyClosed', $alreadyClosed);
|
'voted' => $voted,
|
||||||
|
'alreadyClosed' => $alreadyClosed,
|
||||||
|
'resultsVisible' => $poll->results_visible
|
||||||
|
];
|
||||||
|
|
||||||
|
if($poll->results_visible) {
|
||||||
|
$data['results'] = $poll->options->map(function($o) {
|
||||||
|
$array = $o->makeHidden('poll')->makeHidden('poll_id')->append('vote_count')->toArray();
|
||||||
|
|
||||||
|
//I really shouldn't have to do this...
|
||||||
|
$array['voteCount'] = $array['vote_count'];
|
||||||
|
unset($array['vote_count']);
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json($data);
|
||||||
|
} else {
|
||||||
|
return view('view_poll_results')
|
||||||
|
->with('poll', $poll)
|
||||||
|
->with('voted', $voted)
|
||||||
|
->with('alreadyClosed', $alreadyClosed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function imageToDataUri($image)
|
private static function imageToDataUri($image)
|
||||||
|
@ -149,14 +182,14 @@ class PollController extends Controller
|
||||||
$colourSquareUris = [];
|
$colourSquareUris = [];
|
||||||
|
|
||||||
$startDegrees = 0;
|
$startDegrees = 0;
|
||||||
$sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); });
|
$sortedOptions = $poll->options->sortByDesc(function($option) { return $option->vote_count; });
|
||||||
$nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; })->values();
|
$nonZeroOptions = $sortedOptions->filter(function($option) { return $option->vote_count > 0; })->values();
|
||||||
debug($nonZeroOptions);
|
debug($nonZeroOptions);
|
||||||
for($i = 0; $i < $nonZeroOptions->count(); $i++) {
|
for($i = 0; $i < $nonZeroOptions->count(); $i++) {
|
||||||
$option = $nonZeroOptions[$i];
|
$option = $nonZeroOptions[$i];
|
||||||
|
|
||||||
//TODO: Fix gaps
|
//TODO: Fix gaps
|
||||||
$degrees = round($poll->votes->where('poll_option_id', $option->id)->count() / $voteCount * 360);
|
$degrees = round($option->vote_count / $voteCount * 360);
|
||||||
$endDegrees = min($startDegrees + $degrees, 360);
|
$endDegrees = min($startDegrees + $degrees, 360);
|
||||||
|
|
||||||
$c = function($j) use($i, $baseColours, $nonZeroOptions) {
|
$c = function($j) use($i, $baseColours, $nonZeroOptions) {
|
||||||
|
@ -265,7 +298,15 @@ class PollController extends Controller
|
||||||
return redirect()->action('PollController@viewResults', ['poll' => $poll]);
|
return redirect()->action('PollController@viewResults', ['poll' => $poll]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('edit_poll')->with('poll', $poll)->with('changed', $changed)->with('extraCodes', $extraCodes);
|
if($request->format() == 'json') {
|
||||||
|
return response()->json([
|
||||||
|
"id" => $poll->id,
|
||||||
|
"changed" => $changed,
|
||||||
|
"extraVotingUrls" => collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
return view('edit_poll')->with('poll', $poll)->with('changed', $changed)->with('extraCodes', $extraCodes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit(Request $request, Poll $poll)
|
public function edit(Request $request, Poll $poll)
|
||||||
|
|
|
@ -14,4 +14,9 @@ class PollOption extends Model
|
||||||
{
|
{
|
||||||
return $this->belongsTo('App\Poll');
|
return $this->belongsTo('App\Poll');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getVoteCountAttribute()
|
||||||
|
{
|
||||||
|
return $this->poll->votes->where('poll_option_id', $this->id)->count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<span>Your changes have been saved.</span>
|
<span>Your changes have been saved.</span>
|
||||||
@elseif ($extraCodes != null)
|
@elseif ($extraCodes != null)
|
||||||
<span>Your extra voting URLs have been generated:</span>
|
<span>Your extra voting URLs have been generated:</span>
|
||||||
<textarea class="copyarea" readonly>{{collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n")}}</textarea>
|
<textarea class="copyarea" readonly>{{ collect($extraCodes)->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n") }}</textarea>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ $type = $poll->allow_multiple_answers ? "checkbox" : "radio";
|
||||||
<div class="some-top-margin">
|
<div class="some-top-margin">
|
||||||
@if ($poll->duplicate_vote_checking == 'codes')
|
@if ($poll->duplicate_vote_checking == 'codes')
|
||||||
<span>Voting URLs:</span>
|
<span>Voting URLs:</span>
|
||||||
<textarea class="copyarea" readonly>{{$poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n")}}</textarea>
|
<textarea class="copyarea" readonly>{{ $poll->voting_codes()->get()->map(function($c) use($poll) { return action('PollController@view', ['poll' => $poll, 'code' => $c]); })->implode("\n") }}</textarea>
|
||||||
@else
|
@else
|
||||||
<span>Poll URL: <a href="{{ action('PollController@view', ['poll' => $poll]) }}">{{ action('PollController@view', ['poll' => $poll]) }}</a></span>
|
<span>Poll URL: <a href="{{ action('PollController@view', ['poll' => $poll]) }}">{{ action('PollController@view', ['poll' => $poll]) }}</a></span>
|
||||||
@endif
|
@endif
|
||||||
|
|
|
@ -19,8 +19,8 @@
|
||||||
|
|
||||||
@if ($poll->results_visible)
|
@if ($poll->results_visible)
|
||||||
@php
|
@php
|
||||||
$sortedOptions = $poll->options->sortByDesc(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count(); });
|
$sortedOptions = $poll->options->sortByDesc(function($option) { return $option->vote_count; });
|
||||||
$nonZeroOptions = $sortedOptions->filter(function($option) use($poll) { return $poll->votes->where('poll_option_id', $option->id)->count() > 0; });
|
$nonZeroOptions = $sortedOptions->filter(function($option) { return $option->vote_count > 0; });
|
||||||
|
|
||||||
$cache = Cache::get($poll->id);
|
$cache = Cache::get($poll->id);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue