/** * 投票详细 */ private function _voteShow() { $this->_modelVote = $this->_getGlobalData('Model_Vote', 'object'); if ($this->_isPost()) { //投票 $data = $this->_modelVote->vote($_POST); $this->_utilMsg->showMsg($data['msg'], $data['status'], $data['href']); } else { //显示投票详情 $data = $this->_modelVote->findById($_GET['Id']); if ($data) { if ($data['start_time'] > CURRENT_TIME) { $this->_utilMsg->showMsg('投票时间还未开始', -1); } $this->_utilRbac = $this->_getGlobalData('Util_Rbac', 'object'); $userClass = $this->_utilRbac->getUserClass(); $users = $this->_getGlobalData('user'); $data['word_user_id'] = $users[$data['user_id']]['nick_name']; $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']); $data['end_time'] = date('Y-m-d H:i:s', $data['end_time']); $data['content'] = unserialize($data['content']); $data['vote_user'] = unserialize($data['vote_user']); $data['my_status'] = $this->_modelVote->getUserStatus($userClass['_id'], $data['vote_user']); #------如果是马上显示或是已经过了结束时间就可以显示投票结果------# if ($data['is_open'] || $data['end_time'] < CURRENT_TIME) { $result = $data['result'] ? unserialize($data['result']) : array(); $voteResult = array(); foreach ($data['content'] as $key => $option) { $voteResult[$option] = floatval($result[$key]); } $this->_view->assign('voteResult', json_encode($voteResult)); } #------如果是马上显示或是已经过了结束时间就可以显示投票结果------# #------得到详细日志------# $this->_modelVoteLog = $this->_getGlobalData('Model_VoteLog', 'object'); $voteLogList = $this->_modelVoteLog->findByVoteId($data['Id']); foreach ($voteLogList as &$log) { $log['word_user_id'] = $users[$log['user_id']]['nick_name']; } $this->_view->assign('voteLogList', $voteLogList); #------得到详细日志------# $mySource = $data['vote_user'][$userClass['_id']]; $this->_view->assign('mySource', $mySource); $this->_view->assign('data', $data); $this->_view->set_tpl(array('body' => 'ServiceTools/VoteShow.html')); $this->_utilMsg->createNavBar(); $this->_view->display(); } else { $this->_utilMsg->showMsg(false); } } }
/** * 用户投票动作 * @param array $postArr */ public function vote(array $postArr) { if (empty($postArr['Id'])) { return array('status' => -1, 'msg' => '请选择要投票的项目', 'href' => 1); } if (!is_array($postArr['source'])) { return array('status' => -1, 'msg' => '您没有投票', 'href' => 1); } $data = $this->findById($postArr['Id']); if (!$data) { return array('status' => -1, 'msg' => '该投票项目不存在', 'href' => 1); } if ($data['end_time'] < CURRENT_TIME) { return array('status' => -1, 'msg' => '投票已结束', 'href' => 1); } $this->_utilRbac = $this->_getGlobalData('Util_Rbac', 'object'); $userClass = $this->_utilRbac->getUserClass(); $data['vote_user'] = unserialize($data['vote_user']); //获取所有用户的投票 $mySources = $data['vote_user'][$userClass['_id']]; //获取当前用户的投票数 if ($this->getUserStatus($userClass['_id'], $data['vote_user']) != 1) { return array('status' => -1, 'msg' => '您没有权限投票或已经投过票', 'href' => 1); } $voteResult = $data['result'] ? unserialize($data['result']) : array(); //获取当前投票的结果集 $this->_modelVoteLog = $this->_getGlobalData('Model_VoteLog', 'object'); if ($postArr['source'][1] == $postArr['source'][2] || $postArr['source'][1] == $postArr['source'][3] || $postArr['source'][2] == $postArr['source'][3]) { return array('status' => -1, 'msg' => '投票不能投相同的人', 'href' => 1); } foreach ($postArr['source'] as $vote => $option) { //循环将3个投票都投出去 $voteResult[$option] = floatval($voteResult[$option]); $voteResult[$option] += floatval($mySources[$vote]); $voteResult[$option] = sprintf('%.2f', $voteResult[$option]); $voteResult[$option] = strval($voteResult[$option]); $logArr = array('vote_id' => $data['Id'], 'vote_option_id' => $option, 'user_id' => $userClass['_id'], 'source' => floatval($mySources[$vote])); //写日志 $this->_modelVoteLog->add($logArr); } $data['vote_user'][$userClass['_id']] = array_fill(1, count($data['vote_user'][$userClass['_id']]), 0); $updateArr = array(); $updateArr['vote_user'] = serialize($data['vote_user']); $updateArr['result'] = serialize($voteResult); if ($this->update($updateArr, "Id={$data['Id']}")) { return array('status' => 1, 'msg' => '投票成功', 'href' => 1); } else { return array('status' => -1, 'msg' => '投票失败', 'href' => 1); } }