Пример #1
0
 public function vote($voteObj)
 {
     $userId = parent::getUserId();
     if ($userId == false) {
         return "Invalid AuthToken";
     }
     // Get the current vote
     $sth = $this->db->prepare("SELECT * FROM Votes WHERE UserId = :userId AND ShareId = :shareId LIMIT 1");
     $success = $sth->execute(array(":userId" => $userId, ":shareId" => $voteObj["shareId"]));
     $result = $sth->fetch();
     if ($success && $result != false) {
         $up = 0;
         $down = 0;
         if ($voteObj["voteType"] == "down") {
             // Change to a down vote
             $down = 1;
         } else {
             if ($voteObj["voteType"] == "up") {
                 $up = 1;
             }
         }
         $sthUpdateVote = $this->db->prepare("UPDATE Votes SET UpVote = :up, DownVote = :down WHERE UserId = :userId AND ShareId = :shareId LIMIT 1");
         $sthUpdateVote->execute(array(":up" => $up, ":down" => $down, ":userId" => $userId, "shareId" => $voteObj["shareId"]));
         //print_r($sthUpdateVote->errorInfo());
     } else {
         $up = 0;
         $down = 0;
         // Submit a new vote
         if ($voteObj["voteType"] == "up") {
             $up = 1;
         } else {
             if ($voteObj["voteType"] == "down") {
                 $down = 1;
             }
         }
         $sthNewVote = $this->db->prepare("INSERT INTO Votes (VoteId, UserId, ShareId, UpVote, DownVote, Flag)\n\t\t\t\t\t\t\t\tVALUES ('', :userId, :shareId, :up, :down, 0)");
         $sthNewVote->execute(array(":userId" => $userId, ":shareId" => $voteObj["shareId"], ":up" => $up, ":down" => $down));
     }
     // Update the rank after every vote
     $this->_calculateRank($voteObj["shareId"]);
     // Record the action
     $notifications = new Notifications();
     $notifications->recordAction($userId, $voteObj["voteType"], $voteObj["shareId"]);
     $response = $this->defaultResponseObj;
     $response["Success"] = true;
     $response["Message"] = "Vote recorded";
     return $response;
 }