/** * Returns the new score of the Challenge * @return Int */ public function addVote($idUser, $typeVote) { //Upgrade owner of the Truth if necessary $type = $this->idTruth === null ? 'Dare' : 'Truth'; User::userRankUpgrade($this->idUserTo, 1, $type); //We add the vote up or down if ($typeVote == 'up') { $this->voteUp += 1; } else { $this->voteDown += 1; } //We save $this->save(); //We add the vote to the table VotingDetail $votingDetail = new VotingDetail(); $votingDetail->idUser = $idUser; $votingDetail->idChallenge = $this->idChallenge; $votingDetail->voteDate = date('Y-m-d, H:i:s'); $votingDetail->voteType = $typeVote == 'up' ? 1 : 0; $votingDetail->save(); return $this->voteUp - $this->voteDown; }
/** * Returns the new score of the Truth * @return Int */ public function addVote($idUser, $typeVote) { //Upgrade owner of the Truth if necessary User::userRankUpgrade($this->idUser, 1, 'Truth'); //We add the vote up or down if ($typeVote == 'up') { $this->voteUp += 1; } else { $this->voteDown += 1; } //We save $this->save(); //We add the vote to the table VotingDetail $votingDetail = new VotingDetail(); $votingDetail->idUser = $idUser; $votingDetail->idTruth = $this->idTruth; $votingDetail->voteDate = date('Y-m-d, H:i:s'); //VoteType = 1 -> + 1 / VoteType = 0 -> -1 $votingDetail->voteType = $typeVote == 'up' ? 1 : 0; $votingDetail->save(); return $this->voteUp - $this->voteDown; }
/** * Returns the Truth score of the User $idUser related to the votes of his/her submitted ideas * @return array() */ public function getScoreVoteIdeas($type = null) { if ($type == null) { return getScoreVoteIdeas('truth') + getScoreVoteIdeas('dare'); } else { //Prepare Query $criteria = new CDbCriteria(); $criteria->group = " {$type}.idUser "; $criteria->select = " SUM(CASE WHEN t.voteDate >= :minDateSubmitWeek THEN (CASE t.voteType WHEN 1 THEN 1 ELSE -1 END) END) AS scoreWeek, "; $criteria->select .= " SUM(CASE WHEN t.voteDate >= :minDateSubmitMonth THEN (CASE t.voteType WHEN 1 THEN 1 ELSE -1 END) END) AS scoreMonth, "; $criteria->select .= " SUM(CASE WHEN t.voteDate >= :minDateSubmitYear THEN (CASE t.voteType WHEN 1 THEN 1 ELSE -1 END) END) AS scoreYear, "; $criteria->select .= " SUM(CASE t.voteType WHEN 1 THEN 1 ELSE -1 END) AS score "; $criteria->with = array($type); $criteria->addCondition(" {$type}.idUser = :idUser "); //Bind Parameters $criteria->params = array(':idUser' => $this->idUser); $criteria->params[':minDateSubmitWeek'] = MyFunctions::getFirstDayWeek(); $criteria->params[':minDateSubmitMonth'] = MyFunctions::getFirstDayMonth(); $criteria->params[':minDateSubmitYear'] = MyFunctions::getFirstDayYear(); //Execute Query $result = VotingDetail::model()->find($criteria); //Fetch results $scoreTotal = $result['score'] === null ? 0 : $result->score; $scoreWeek = $result['scoreWeek'] === null ? 0 : $result->scoreWeek; $scoreMonth = $result['scoreMonth'] === null ? 0 : $result->scoreMonth; $scoreYear = $result['scoreYear'] === null ? 0 : $result->scoreYear; return array('total' => $scoreTotal, 'week' => $scoreWeek, 'month' => $scoreMonth, 'year' => $scoreYear); } }
public function actionDeleteChallenge() { if (isset($_POST['idChallenge'])) { $challenge = Challenge::model()->findByPk($_POST['idChallenge']); if ($challenge->idUserTo == Yii::app()->user->getId()) { //We change the status to decline $challenge->status = 2; $challenge->save(); //We delete the associated votes $votingDetails = VotingDetail::model()->deleteAllByAttributes(array('idChallenge' => $_POST['idChallenge'])); echo "SUCCESS"; } } }
public function actionVoteChallenge() { if (isset($_POST['idChallenge']) && isset($_POST['vote'])) { if (!VotingDetail::model()->exists("idUser = :idUser AND idChallenge = :idChallenge", array(':idUser' => Yii::app()->user->getId(), ":idChallenge" => $_POST["idChallenge"]))) { $model = Challenge::model()->findByPk($_POST["idChallenge"]); $vote = $model->addVote(Yii::app()->user->getId(), $_POST['vote']); echo $vote > 0 ? "+{$vote}" : $vote; } else { echo "Already Voted!"; } return; } return "ERROR"; }