Game-Jaming/Backend/Controllers/Polls/GetWinner.php

44 lines
1.3 KiB
PHP
Raw Normal View History

2021-03-11 12:21:16 +00:00
<?php
require_once "../../../bootstrap.php";
require_once "../Admin/Admin.php";
use Backend\Models\Group;
use Backend\Models\Vote;
2021-03-12 13:05:31 +00:00
use \Illuminate\Support\Collection;
2021-03-11 12:21:16 +00:00
2021-03-23 12:39:44 +00:00
if(isAdmin()){
if(isset($_GET['gameJamId'])) {
$groups = Group::where('game_jam_id', $_GET['gameJamId'])->get();
if($groups) {
2021-03-12 13:05:31 +00:00
$winningGroups = new Collection();
2021-03-23 12:39:44 +00:00
$i = -1;
foreach ($groups as $group) {
$x = Vote::where('group_id', $group->id)->count();
if ($x > $i) {
$winningGroups = new Collection();
$winningGroups->push($group);
2021-03-12 13:05:31 +00:00
2021-03-23 12:39:44 +00:00
$i = $x;
} elseif ($i === $x) {
$winningGroups->push($group);
}
}
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept");
header('Content-Type: application/json;charset=UTF-8');
echo json_encode(array('data' => $winningGroups->jsonSerialize()));
2021-03-11 12:21:16 +00:00
}
2021-03-23 12:39:44 +00:00
else{
http_response_code(400);
echo json_encode(["message" => "game jam not found"]);
2021-03-11 12:21:16 +00:00
}
2021-03-23 12:39:44 +00:00
}else{
http_response_code(400);
2021-03-11 12:21:16 +00:00
}
2021-03-19 08:18:19 +00:00
2021-03-19 12:25:27 +00:00
}else{
2021-03-23 12:39:44 +00:00
http_response_code(401);
echo json_encode(["message" => "not admin"]);
2021-03-11 12:21:16 +00:00
}