kinda new to OOP, and I wanna know if i'm heading into the right direction. Also I should mention I'm not using AJAX
AnswerVoteManager.php
<?php
require '../config.php';
interface voting
{
public function voted();
public function redirect();
public function vote();
public function deleteVote();
public function addVote();
public function subtractVote();
public function updateVote();
public function insertVote();
public function getCurrentvote();
}
class AnswerVoteManager
{
private $dbh;
protected $answerID;
protected $title;
protected $voter;
protected $vote_type;
/**
* @return mixed
*/
public function getAnswerID()
{
return $this->answerID;
}
/**
* @param mixed $answerID
*/
public function setAnswerID($answerID)
{
$this->answerID = $answerID;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getVoter()
{
return $this->voter;
}
/**
* @param mixed $voter
*/
public function setVoter($voter)
{
$this->voter = $voter;
}
/**
* @return mixed
*/
public function getVoteType()
{
return $this->vote_type;
}
/**
* @param mixed $vote_type
*/
public function setVoteType($vote_type)
{
$this->vote_type = $vote_type;
}
public function __construct($dbh)
{
$this->dbh = $dbh;
}
public function getCurrentvote($answerid,$username)
{
$sql = "SELECT vote_type from answer_votes WHERE answerid=:answerid and username=:username";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':answerid',$answerid);
$stmt->bindValue(':username',$username);
$stmt->execute();
$type = (string) $stmt->fetchColumn();
if(empty($type))
{
return null;
} else {
return (string) $type;
}
}
private function voted()
{
$sql = "SELECT vote_type from answer_votes where answerID=:answerid and username=:username";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':answerid',$this->answerID);
$stmt->bindValue(':username',$this->voter);
$stmt->execute();
$exists = $stmt->rowCount();
if($exists)
{
return true;
} else {
return false;
}
}
private function redirect()
{
header("Location:" . $_SERVER['REQUEST_URI']);
exit();
}
private function updateVote()
{
$sql = "UPDATE answer_votes set vote_type=:vote_type";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':vote_type',$this->getVoteType());
$stmt->execute();
$this->redirect();
}
private function addVote()
{
$sql = "UPDATE question_answers set count = question_answers.count + 1 WHERE id=:id";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':id',$this->answerID);
$stmt->execute();
}
private function subtractVote()
{
$sql = "UPDATE question_answers set count = question_answers.count - 1 WHERE id=:id";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':id',$this->answerID);
$stmt->execute();
}
private function deleteVote()
{
$sql = "DELETE FROM answer_votes where answerID=:answerid and username=:username LIMIT 1";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':answerid',$this->getAnswerID());
$stmt->bindValue(':username',$this->voter);
$stmt->execute();
$this->subtractVote();
$this->redirect();
}
private function insertVote()
{
$sql = "INSERT into answer_votes (vote_type,answerID,username,questiontitle) values (:vote_type,:answerid,:username,:questiontitle)";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':vote_type',$this->getVoteType());
$stmt->bindValue(':answerid',$this->getAnswerID(),PDO::PARAM_INT);
$stmt->bindValue(':username',$this->getVoter());
$stmt->bindValue(':questiontitle',$this->getTitle());
$stmt->execute();
$this->addVote();
$this->redirect();
}
public function vote()
{
if($this->vote_type == 'Upvote' && $this->voted()) {
$this->deleteVote();
} else {
$this->insertVote();
}
}
}
QuestionVoteManager.php
require '../config.php';
class QuestionVoteManager implements voting
{
protected $voter;
protected $vote_type;
/**
* @var $qtitle (questiontitle)
*/
protected $qtitle;
/**
* @return mixed
*/
public function getVoter()
{
return $this->voter;
}
/**
* @param mixed $voter most likely a string.
*/
public function setVoter($voter)
{
$this->voter = $voter;
}
/**
* @return mixed
*/
public function getVoteType()
{
return $this->vote_type;
}
/**
* @param mixed $vote_type
*/
public function setVoteType($vote_type)
{
$this->vote_type = $vote_type;
}
/**
* @return mixed
*/
public function getQtitle()
{
return $this->qtitle;
}
/**
* @param mixed $qtitle
*/
public function setQtitle($qtitle)
{
$this->qtitle = $qtitle;
}
public function __construct($dbh)
{
$this->dbh = $dbh;
}
public function voted()
{
$sql = "SELECT vote_type from questions_vote WHERE questiontitle=:questiontitle";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':questiontitle',$this->getQtitle());
$stmt->execute();
$exists = $stmt->rowCount();
if($exists)
{
return true;
} else {
return false;
}
}
public function redirect()
{
header("Location:" . $_SERVER['REQUEST_URI']);
exit();
}
public function deleteVote()
{
$sql = "DELETE FROM questions_vote WHERE username=:username and questiontitle=:questiontitle";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':username',$this->getVoter());
$stmt->bindValue(':questiontitle',$this->getQtitle());
$stmt->execute();
$this->subtractVote();
$this->redirect();
}
public function addVote()
{
$sql = "UPDATE questions set count = count + 1 WHERE title=:title";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':title',$this->getQtitle());
$stmt->execute();
}
public function subtractVote()
{
$sql = "UPDATE questions set count = count - 1 WHERE title=:title";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':title',$this->getQtitle());
$stmt->execute();
}
public function updateVote()
{
// TODO: Implement updateVote() method.
}
public function insertVote()
{
$sql = "INSERT INTO questions_vote (vote_type,questiontitle,username) values (:vote_type,:questiontitle,:username)";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':vote_type',$this->getVoteType());
$stmt->bindValue(':questiontitle',$this->getQtitle());
$stmt->bindValue(':username',$this->getVoter());
$stmt->execute();
$this->addVote();
$this->redirect();
}
public function getCurrentvote()
{
$sql = "SELECT vote_type from questions_vote WHERE questiontitle=:questiontitle and username=:username";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':questiontitle',$this->getQtitle());
$stmt->bindValue(':username',$this->getVoter());
$stmt->execute();
$vote_type = (string) $stmt->fetchColumn();
return $vote_type;
}
public function vote()
{
if($this->getVoteType() == 'Upvote' && $this->voted())
{
$this->deleteVote();
} else {
$this->insertVote();
}
}
final public function getVotes($title)
{
$sql = "SELECT count from questions where title=:title";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':title',$title);
$stmt->execute();
$count = $stmt->fetchColumn();
// MIGHT CHANGE THIS IN THE FUTURE WHEN WE ADD DOWNVOTING..
if($count < 0)
{
exit();
}
return (integer) $count;
}
}