/** * 用户投票 * * @param int $userid * @param int $pollid * @param array $option * @return bool || PwError */ public function doVote($userid, $pollid, $option) { $userid = intval($userid); $pollid = intval($pollid); if (!$userid || !$pollid) { return new PwError('VOTE:fail'); } if (empty($option) || !is_array($option)) { return new PwError('VOTE:not.select.option'); } $poll = $this->_getPollDs()->getPoll($pollid); if (!$poll) { return new PwError('VOTE:thread.not.exist'); } $isVoted = $this->_getPollVoterDs()->isVoted($userid, $pollid); if ($isVoted) { return new PwError('VOTE:is.voted'); } $voteTimeslimit = $poll['option_limit'] ? $poll['option_limit'] : 1; if (count($option) > $voteTimeslimit) { return new PwError('VOTE:most.times.limit', array('{mosttimes}' => $voteTimeslimit)); } Wind::import('SRV:poll.dm.PwPollOptionDm'); foreach ($option as $optionid) { $this->_getPollVoterDs()->add($userid, $pollid, $optionid); $pollOptionDm = new PwPollOptionDm($optionid); $pollOptionDm->addVotedNum(1); $this->_getPollOptionDs()->update($pollOptionDm); } //更新该投票人数 $voterNum = $this->_getPollVoterDs()->countUser($pollid); Wind::import('SRV:poll.dm.PwPollDm'); $pollDm = new PwPollDm($pollid); /* @var $pollDm PwPollDm */ $pollDm->setVoterNum($voterNum); $this->_getPollDs()->updatePoll($pollDm); return true; }