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); } }
/** * 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 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(); }
/** * 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 actionClear($id) { // $model = $this->loadModel($id); PollChoice::model()->updateAll(array('votes' => 0), 'poll_id="' . $id . '"'); PollVote::model()->deleteAll("poll_id ='" . $id . "'"); }
<?php /** * Created by PhpStorm. * User: Jakub * Date: 16/02/2016 * Time: 21:52 */ class PollVote extends Model { public $pollvoteid; public $pollid; public $polloptionid; public $dateline; public $userid; public $username; public $avatar; } PollVote::init('pollvotes', 'pollvoteid');
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(); } }
/** * @return void */ public function show() { if (!TwitterAuth::isLogged()) { ?> Sólo los miembros pueden ver esta página. <a href="<?=HTMLResponse::getRoute()?>?authenticate=1"> Inicia sesión. </a><br> <? return; } else { if (!Team::isMember()) { HTMLResponse::exitWithRoute('/votaciones/'); } $answer = PollVote::findOne('userid = ? and pollid = ?', [TwitterAuth::getUserId(), $this->poll->pollid]); $options = PollOption::find('pollid = ? order by polloptionid asc', [$this->poll->pollid]); if (!$answer && strlen($hash = HTMLResponse::fromGET('hash', ''))) { $optionid = HTMLResponse::fromGET('vote'); foreach($options as $index => $option) { if ($option->polloptionid == $optionid && $option->getHash() == $hash) { $answer = PollVote::create(); $answer->userid = TwitterAuth::getUserId(); $answer->dateline = time(); $answer->avatar = TwitterAuth::getAvatar(); $answer->pollid = $this->poll->pollid; $answer->polloptionid = $option->polloptionid; $answer->username = TwitterAuth::getUserName(); $answer->save(); } } } $answers = Model::groupBy(PollVote::find('pollid = ?', [$this->poll->pollid]), 'polloptionid'); $hasAnswered = !!$answer; ?><div style="text-align:left; margin: 0 auto" class="inblock"> <table style="width:640px"> <thead> <tr> <td>Lista de opciones</td> </tr> </thead> <? foreach($options as $index => $option) { ?> <tr><td class="row" style="text-align: left"> <div style="height: 6px"></div> <div class="inblock middle" style="width:320px"> <b>Opción <?=$index+1?></b>: <?= htmlentities($option->title) ?> </div> <div class="inblock middle"> <div class="moreless inblock middle" style="width: 150px"> <a href="javascript:void(0)" onclick="$(this).closest('.row').find('.moreless').toggle(); $(this).closest('.row').find('.onmore').slideDown(500);">+ Mostrar más</a> </div> <div class="moreless inblock middle" style="width: 150px; display: none"> <a href="javascript:void(0)" onclick="$(this).closest('.row').find('.moreless').toggle(); $(this).closest('.row').find('.onmore').slideUp(400);">- Mostrar menos</a> </div> </div> <div class="inblock middle"> <? if (!$hasAnswered) { ?> <a href="<?=HTMLResponse::getRoute()?>?vote=<?=$option->polloptionid?>&hash=<?=$option->getHash()?>" onclick="return confirm('¿Votas <?=htmlentities($option->title)?>?')"> Votar esta opción </a> <? } else if ($answer->polloptionid == $option->polloptionid) { ?> <i>Votaste esta opción</i> <? } ?> </div> <div class="onmore" style="display: none; padding: 12px"> <?= $option->description ?> </div> <div style="height: 6px"></div> <? if (!$hasAnswered) { ?><i>Vota primero para ver los resultados</i><? } else { $optionAnswers = $answers[$option->polloptionid]; ?> Votado por: <?= $optionAnswers ? '<b>'.implode(', ', Model::pluck($optionAnswers, 'username')).'</b> ('.count($optionAnswers).' votos)' : '<i>Nadie</i>'; ?> <? } ?> <div style="height: 6px"></div> </td></tr> <? } ?></table></div><br><br><? } }
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; ?>">
/** * 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' => __("Десила се грешка"))); } }