/** * 为帖子投票 * Enter description here ... * @param unknown_type $postid * @param unknown_type $value */ public function actionAnswer($answerid, $value = 0) { //$vote = new PostVote; $vote = AnswerVote::model()->findByAttributes(array('userId' => Yii::app()->user->id, 'answerid' => $answerid)); $answer = Answer::model()->with('question')->findByPk($answerid); if ($vote && $vote->value == $value) { //再点击赞同(反对),即取消第一次投的赞同(反对) $result = $vote->delete(); } else { //赞同(反对)被第一次点击 $vote or $vote = new AnswerVote(); $vote->answerid = $answerid; $vote->value = $value; $vote->userId = Yii::app()->user->id; $vote->addTime = time(); $result = $vote->save(); //发送系统通知给回答主人 if ($answer->userId != $vote->userId) { $notice = new Notice(); $notice->type = 'vote_answer'; $notice->setData(array('voteId' => $vote->getPrimaryKey())); $notice->userId = $answer->userId; $notice->save(); } } if ($result) { // $question = Question::model()->findByPk($answer->question) //修改答案投票次数统计 $answer->voteupNum = $answer->voteupCount; $answer->count_votedown = $answer->votedownCount; $answer->save(); //修改问题投票次数统计 $answer->question->updateVoteCount(); //修改用户被赞数量 $user = UserInfo::model()->findByPk($answer->userId); $user->answerVoteupNum = $user->getAnswerVoteupCount(); $user->save(); $score = $answer->voteupCount - $answer->votedownCount; $this->renderPartial('result', array('score' => $score, 'voteupers' => $answer->voteupers)); } }
public function getAnswerVoteupCount() { $sql = "select v.* from ew_answer_vote v left join ew_answer a on v.answerid=a.id where a.userId={$this->id} and v.value>0"; $vs = AnswerVote::model()->findAllBySql($sql); return count($vs); }
<?php /* @var $this NoticeController */ /* @var $data Notice */ $vote = AnswerVote::model()->with('answer.question')->findByPk($data['voteid']); if (!$vote) { return false; } if ($vote) { $question = $vote->answer->question; ?> 你对 <?php echo CHtml::link($question->title, array('question/view', 'id' => $question->id)); ?> 的回答新增加一个<em><?php echo $vote->value > 0 ? "<span style=\"color:green\">赞同票</span>" : "<span style=\"color:red\">反对票</span>"; ?> </em> <?php }