/**
  * 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 = PollChoice::model()->with('pollVotes')->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);
        }
    }
Exemple #3
0
 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();
 }
Exemple #4
0
 public function checkUniqueLabel($attribute,$params)
 {
   $this->label = MHelper::String()->simple_ucfirst($this->label);
  if(PollChoice::model()->count('LOWER(label)=:label AND org_id=:org_id AND type=:type',
      array(':label'=>MHelper::String()->toLower($this->label),':org_id'=>$this->org_id, ':type'=>$this->type)) > 0)
  { 
  	  $this->addError($attribute, 'Название уже существует');
      return false;
  } else { 
  	  return true;
  }
 }
 public function getVote($id)
 {
     $vote = PollChoice::model()->findByPk($id);
     $cd = Yii::app()->db->createCommand();
     $cd->select('SUM(pch.votes) as sum');
     $cd->from('PollChoice pch');
     $cd->where('poll_id=:id', array(':id' => $vote->poll_id));
     $count = $cd->queryScalar();
     $vote = $vote->votes / $count * 100;
     $vote = round($vote);
     return $vote;
 }
 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();
     }
 }
Exemple #7
0
 /**
  * Before a PollVote is saved.
  */
 public function beforeSave()
 {
     $this->ip_address = $_SERVER['REMOTE_ADDR'];
     $this->timestamp = time();
     $this->user_id = Yii::app()->user->id;
     // Relation may not exist yet so find it normally
     $choice = PollChoice::model()->findByPk($this->choice_id);
     if ($choice) {
         $choice->votes += 1;
         $choice->save();
     } else {
         return FALSE;
     }
     return parent::beforeSave();
 }
Exemple #8
0
	public function run()
	{
		if(!$this->type){
			$choice = PollChoice::model()->open()->find(array('condition'=>'org_id='.$this->org_id, 'order'=>'created_date DESC'));
		
		} else {
			$choice = PollChoice::model()->open()->find(array('condition'=>'org_id='.$this->org_id.' and type='.$this->type, 'order'=>'created_date DESC'));
		
		}
		
		$this->render('poll',array(
			'choice'=>$choice
		));
		
	}
    public function actionIndex() {

    	$this->active_link = 'dashboard';
    	//count users
    	$users = User::model()->count('status != '.User::STATUS_DELETED);
        $objects = Objects::model()->active()->count(array('select'=>'id'));
        $objectsNew = Objects::model()->active()->count(array('select'=>'id','condition'=>'verified = false'));
        $polls = PollChoice::model()->count();
        $photos = ObjectsImages::model()->count();
        $videos = ObjectsHttp::model()->count(array('condition'=>'type='.ObjectsHttp::TYPE_VIDEO));
        $this->render('dashboard',array('users'=>$users,'objects'=>$objects,
            'objectsNew'=>$objectsNew, 'polls'=>$polls,'photos'=>$photos,
            'videos'=>$videos

        ));
    }
Exemple #10
0
  /**
   * Before a PollVote is saved.
   */
  public function beforeSave()
  {
    if(parent::beforeSave()) {
    	if($this->isNewRecord) {
	    	$this->ip_address = Yii::app()->request->userHostAddress;
	    	if(!Yii::app()->user->isGuest)
	    		$this->user_id = Yii::app()->user->id;
	    	// Relation may not exist yet so find it normally
		    $choice = PollChoice::model()->findByPk($this->choice_id);
		    if ($choice) {
		     // $choice->votes += 1;
		      $choice->save();
		    }
		    else {
		      return FALSE;
		    }
		}
	    
	    return true;
    } else
        return false;
  }
Exemple #11
0
  /**
   * Renders the portlet content.
   */
  public function renderContent()
  {
  //  $model = $this->_poll;

  //  if ($model) {
   //   $userVote = $this->loadVote();
   //   $params = array('model' => $model, 'userVote' => $userVote);
  	$params = array();

      // Save a user's vote
      if (isset($_POST['PortletPollVote_choice_id'])) {
        $userVote->choice_id = $_POST['PortletPollVote_choice_id'];
        $userVote->poll_id = $model->id;
        if ($userVote->save()) {
          // Prevent submit on refresh
          $route = Yii::app()->controller->route;
          Yii::app()->controller->redirect(Yii::app()->createUrl($route));
        }
      }

      // Force user to vote if needed
 

    // Convert choices to form options list
    $choices = array();
    $modelChoices = PollChoice::model()->open()->findAll(array('condition'=>'org_id='.$this->org_id.' and type='.$this->type,'order'=>'votes DESC, label'));
 /*   if($modelChoices){
        foreach ($modelChoices as $choice) {
           $userVote = $this->loadVoteChoice($choice);
           $userChoice = $choice;
           if (Yii::app()->getModule('poll')->forceVote && $choice->userCanVote()) {
          		$choices[$choice->id] = array(
          			'label'=>CHtml::encode($choice->label),
          			'userVote'=>$userVote,
          			'userChoice'=>$userChoice
          			);
          	} else {
          		$choices[$choice->id] = array(
          			'label'=>'hhhhhhhhhhhhh',
          			'userVote'=>$userVote,
          			'userChoice'=>$userChoice
          			);
          	}

        }
	}*/

     $params['choices'] = $choices;
  /*   $params += array(
          'userVote' => $userVote,
          'userChoice' => $userChoice,
          'userCanCancel' => $model->userCanCancelVote($userVote),
        );*/

      $this->render('vote', array('modelChoices'=>$modelChoices,'type'=>$this->type,'org_id'=>$this->org_id));
  //  }
  }
 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 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();
     }
 }
Exemple #14
0
 public static function getChoice($id)
 {
     return $models = PollChoice::model()->findAll("poll_id=:poll_id", array(":poll_id" => $id));
 }
Exemple #15
0

<?php 
Yii::app()->tpl->closeWidget();
?>




<?php 
$form = $this->beginWidget('CActiveForm', array('id' => 'poll-form'));
?>
<div class=" grid6">

    <?php 
$currentPoll = PollChoice::model()->findAll(array('condition' => '`t`.`poll_id`=' . $model->id));
?>

    <div class="widget">

        <div class="whead">

            <h6>Выборки</h6>
            <a href="javascript:void(0)" onClick="addInput('#poll-choice','prepend');"><span class="tableOptions icon-plus"></span></a>
            <div class="clear"></div>
        </div>

        <table class="tDefault tMedia">
            <thead>
                <tr>
                    <th>Название</th>