/** * 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"; }