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 actionAjax() { $pollVote = new PollVote(); if (isset($_POST['choice'])) { $choice = PollChoice::model()->findByPk($_POST['choice']); $choice->votes = $choice->votes + 1; $pollVote->poll_id = $choice->poll_id; $pollVote->choice_id = $choice->id; $pollVote->user_id = Yii::app()->user->id; $pollVote->ip_address = $_SERVER[HTTP_X_FORWARDED_FOR]; $pollVote->time = date("Y.m.j "); $pollVote->save(); $choice->save(); } }
public function actionView($id) { Yii::app()->clientScript->scriptMap = array('jquery.js' => false); $model = Poll::model()->findByPk($id); if ($model) { $fn = new CPollHelper($model); $params = array('model' => $model); if (isset($_POST['PortletPollVote_choice_id'])) { foreach ($_POST['PortletPollVote_choice_id'] as $ids) { $userVote = new PollVote(); $userVote->choice_id = $ids; $userVote->poll_id = $model->id; if ($userVote->validate()) { $userVote->save(false, false); } else { die('err'); } } } $userVote = $fn->loadVote(); if (Yii::app()->settings->get('poll', 'is_force') && $model->userCanVote()) { if (Yii::app()->request->isAjaxRequest) { $this->widget('ext.uniform.UniformWidget', array('theme' => 'default')); $userVote->addError('choise_id', 'Тыкни ты уже кудато!!'); $view = 'poll.widgets.random.views.vote'; } else { $view = 'vote'; } // Convert choices to form options list $choices = array(); foreach ($model->choices as $choice) { $choices[$choice->id] = Html::encode($choice->name); } $params['choices'] = $choices; } else { if (Yii::app()->request->isAjaxRequest) { $view = 'poll.widgets.random.views.view'; } else { $view = 'view'; } $userChoice = $fn->loadChoice($userVote); $params += array('userVote' => $userVote, 'userChoice' => $userChoice); } $this->render($view, $params, false, true); } }
/** * Vote on a poll. * If vote is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to vote on */ public function actionVote($id) { $model = $this->loadModel($id); $vote = new PollVote(); if (!$model->userCanVote()) { $this->redirect(array('view', 'id' => $model->id)); } if (isset($_POST['PollVote'])) { $vote->attributes = $_POST['PollVote']; $vote->poll_id = $model->id; if ($vote->save()) { $this->redirect(array('view', 'id' => $model->id)); } } // Convert choices to form options list $choices = array(); foreach ($model->choices as $choice) { $choices[$choice->id] = CHtml::encode($choice->label); } $this->render('vote', array('model' => $model, 'vote' => $vote, 'choices' => $choices)); }
public function actionAjax() { $pollVote = new PollVote(); if (isset($_POST['choice'])) { $choice = PollChoice::model()->findByPk($_POST['choice']); $choice->votes = $choice->votes + 1; $pollVote->poll_id = $choice->poll_id; $pollVote->choice_id = $choice->id; $pollVote->user_id = Yii::app()->user->id; $pollVote->ip_address = Yii::app()->request->userHostAddress; $pollVote->time = date("Y.m.j "); $pollVote->save(); $choice->save(); } }
/** * Show the form for editing the specified resource. * GET /api/apipoll/{id}/edit * * @param int $id * @return Response */ public function postCastVote() { $this->googleAnalytics('/polls/cast-vote/'); $rules = array('poll_id' => 'Required', 'option_id' => 'Required'); $v = Validator::make(Input::all(), $rules); if ($v->passes()) { $ip = Puskice::getIP(); $vote = PollVote::where('poll_id', '=', Input::get("poll_id"))->where('ip_address', '=', $ip)->first(); if ($vote != null) { return Response::json(array('status' => 'fail', 'text' => __("Већ сте гласали на овој анкети. Хвала :)"))); } $vote = new PollVote(); $vote->poll_id = strip_tags(Input::get("poll_id")); $vote->option_id = strip_tags(Input::get("option_id")); $vote->ip_address = $ip; $vote->save(); $option = PollOption::find(Input::get("option_id")); $poll = Poll::find($option->poll_id); if ($poll->published == 1) { $option->vote_count = $option->vote_count + 1; $option->save(); return Response::json(array('status' => 'success', 'text' => __("Хвала што сте гласали"))); } return Response::json(array('status' => 'fail', 'text' => __("Хвала што покушавате да хакујете анкету :)"))); } else { return Response::json(array('status' => 'fail', 'text' => __("Десила се грешка"))); } }