/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = PollVote::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionTovote (){ $id = (int)$_POST['id']; $voter = (int)$_POST['vote']; $ret = array(); // check $ip = MHelper::Ip()->getIp(); $vote = PollVote::model()->find(array('condition'=>'choice_id=:id and ip_address=:ip','params'=>array(':id'=>$id,':ip'=>$ip))); if(!$vote){ $vote = new PollVote; $vote->vote = $voter; $vote->choice_id = $id; $vote->ip_address = $ip; if(!Yii::app()->user->isGuest){ $vote->user_id = Yii::app()->user->id; } if(!$vote->save()){ VarDumper::dump($vote->errors); die(); // Ctrl + X Delete line } $weight = ''; $sql = "SELECT COUNT(*) FROM poll_vote WHERE choice_id={$id} and vote={$voter}"; $numClients = Yii::app()->db->createCommand($sql)->queryScalar(); $review = PollChoice::model()->findByPk($id); if($voter == 1){ $review->yes = $numClients; $diff = $review->yes - $review->no; $sum = $review->yes + $review->no; if($diff>0){ $weight = round(($diff)*100/$sum); } $review->weight = $weight; $review->votes = $diff; $review->save(false,array('yes','weight','votes')); } else { $review->no = $numClients; $diff = $review->yes - $review->no; $sum = $review->yes + $review->no; if($diff>0){ $weight = round(($diff)*100/$sum); } $review->weight = $weight; $review->votes = $diff; $review->save(false,array('no','weight','votes')); } $ret['flag'] = true; $ret['count'] = $numClients; $ret['yes'] = $review->yes; $ret['no'] = $review->no; $ret['type'] = $review->type; $ret['id'] = $review->id; $ret['weight'] = $weight; $ret['weight_text'] = (!empty($weight))?'считают '.$weight.'%':''; echo CJSON::encode($ret); } }
public function afterUninstall() { Yii::app()->settings->clear($this->id); $db = Yii::app()->db; $tablesArray = array(PollChoice::model()->tableName(), PollVote::model()->tableName(), Poll::model()->tableName()); foreach ($tablesArray as $table) { $db->createCommand()->truncateTable($table); $db->createCommand()->dropTable($table); } return parent::afterUninstall(); }
public function actionClear($id) { // $model = $this->loadModel($id); PollChoice::model()->updateAll(array('votes' => 0), 'poll_id="' . $id . '"'); PollVote::model()->deleteAll("poll_id ='" . $id . "'"); }
public function isVote($id) { if (Yii::app()->user->isGuest) { if (PollVote::model()->find("ip_address=:ip_address and poll_id=:poll_id", array(":ip_address" => $this->get_client_ip(), ":poll_id" => $id))) { return true; } else { return false; } } else { if (PollVote::model()->find("user_id=:user_id and poll_id=:poll_id", array(":user_id" => Yii::app()->user->id, ":poll_id" => $id))) { return true; } else { return false; } } }
<?php $vote = PollVote::model()->find(array('condition'=>'choice_id=:id and ip_address=:ip','params'=>array(':id'=>$model->id,':ip'=>$ip))); if($vote){ ?> <div class="vote c-9" id="vote<?php echo $model->id; ?>"> <span class="user_votes"> <span class="user_pro <?php if($vote->vote == 1) { echo 'user_mine';} ?>"><i class="zmdi zmdi-thumb-up"></i></span> <?php $diff = $model->yes - $model->no; if($diff > 0){ echo '<span class="user_n user_num_g">'.$diff.'</span>'; // echo '<span class="user_n user_num_g"></span>'; } else if($diff < 0){ echo '<span class="user_n user_num_r">'.abs($diff).'</span>'; // echo '<span class="user_n user_num_r"></span>'; } else { echo '<span class="user_n"></span>'; } ?> <span class="user_contra <?php if($vote->vote != 1) { echo 'user_mine';} ?>" ><i class="zmdi zmdi-thumb-down"></i></span> </span> </div> <?php } else { ?> <div class="vote c-9 active" id="vote<?php echo $model->id; ?>">