diff --git a/app/Controllers/Api/Images/Rate.php b/app/Controllers/Api/Images/Rate.php index b30fc0e..3d65efd 100644 --- a/app/Controllers/Api/Images/Rate.php +++ b/app/Controllers/Api/Images/Rate.php @@ -3,7 +3,7 @@ namespace App\Controllers\Api\Images; use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF}; -use App\Models\{User, Vote}; +use App\Models\{User, Vote, VoteContest}; class Rate { @@ -14,28 +14,29 @@ class Rate $photoId = $_GET['pid']; $voteType = (int) $_GET['vote']; $contest = (isset($_GET['action']) && $_GET['action'] === 'vote-konk') ? 1 : 0; + $contestId = $_GET['cid']; if ($contest === 1) { - if (Vote::photoContest($userId, $photoId) === -1) { + if (VoteContest::photo($userId, $photoId, $contestId) === -1) { DB::query( - 'INSERT INTO photos_rates (id, user_id, photo_id, type, contest) VALUES (NULL, :id, :pid, :type, 1)', - [':id' => $userId, ':pid' => $photoId, ':type' => $voteType] + 'INSERT INTO photos_rates_contest (id, user_id, photo_id, type, contest_id) VALUES (NULL, :id, :pid, :type, :cid)', + [':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId] ); - if (Vote::photoContest($userId, $photoId) != $voteType) { + if (VoteContest::photo($userId, $photoId, $contestId) != $voteType) { DB::query( - 'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest=1', - [':id' => $userId, ':pid' => $photoId, ':type' => Vote::photo($userId, $photoId)] + 'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest_id=:cid', + [':id' => $userId, ':pid' => $photoId, ':type' => VoteContest::photo($userId, $photoId, $contestId), ':cid'=>$contestId] ); } - } elseif (Vote::photoContest($userId, $photoId) === $voteType) { + } elseif (VoteContest::photo($userId, $photoId, $contestId) === $voteType) { DB::query( - 'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND contest=1', - [':id' => $userId, ':pid' => $photoId] + 'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid', + [':id' => $userId, ':pid' => $photoId, ':cid'=>$contestId] ); } else { DB::query( - 'UPDATE photos_rates SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest=1', - [':id' => $userId, ':pid' => $photoId, ':type' => $voteType] + 'UPDATE photos_rates_contest SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid', + [':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId] ); } } else { @@ -77,7 +78,13 @@ class Rate } $currentVote = Vote::photo($userId, $photoId); - $contCurrentVote = Vote::photoContest($userId, $photoId); + $contCurrentVote = VoteContest::photo($userId, $photoId, $contestId); + + if ($contest === 0) { + $count = Vote::count($photoId); + } else { + $count = VoteContest::count($photoId, $contestId); + } $result = [ 'buttons' => [ 'negbtn' => $currentVote === 0, @@ -86,7 +93,7 @@ class Rate 'posbtn_contest' => $contCurrentVote === 1, ], 'errors' => '', - 'rating' => Vote::count($photoId), + 'rating' => $count, 'votes' => [ 1 => $formattedVotesPos, 0 => $formattedVotesNeg diff --git a/app/Models/VoteContest.php b/app/Models/VoteContest.php new file mode 100644 index 0000000..29b2b08 --- /dev/null +++ b/app/Models/VoteContest.php @@ -0,0 +1,37 @@ + $user_id, ':pid' => $pid, ':id'=>$cid)); + if (!empty($result)) { + $type = $result[0]['type']; + if ($type < 0) { + $type = -1; + } + return $type; + } else { + return -1; + } + + } + + public static function count($pid, $cid) { + $result = DB::query('SELECT * FROM photos_rates_contest WHERE photo_id=:pid AND contest_id=:id', array(':pid' => $pid, ':id'=>$cid)); + $votes = 0; + foreach ($result as $r) { + if ($r['type'] === 1) { + $votes++; + } else { + $votes--; + } + } + return $votes; + } + +} +?>