Esempio n. 1
0
 /**
  * Запускаем отрисовку виджета
  *
  * @return void
  */
 public function run()
 {
     // Авторизован ли пользователь
     if (Yii::app()->user->isAuthenticated() === false) {
         return;
     }
     if (($user = Yii::app()->user->getProfile()) === null) {
         return;
     }
     $poll = Poll::model()->active()->with(array('questions' => array('scopes' => 'active'), 'questions.variants' => array('scopes' => 'active')))->findByPK((int) $this->model_id);
     if ($poll === null) {
         return;
     }
     // Подгружаем результаты пользователя
     $userId = $user->id;
     if (Yii::app()->user->isSuperUser() && Yii::app()->user->getState('adminUserId') !== null) {
         // Для администратора показываем результаты опроса пользователя в случае подмены
         $viewedUser = User::model()->findByPK((int) Yii::app()->user->getState('adminUserId'));
         if ($viewedUser) {
             $userId = $viewedUser->id;
         }
     }
     $result = Result::model()->with('answers')->find('t.user_id = :user_id AND t.poll_id = :poll_id', array(':user_id' => $userId, ':poll_id' => $poll->id));
     $this->render($this->view, array('model' => $poll, 'result' => $result));
 }
 public function loadModel($id)
 {
     if (($model = Poll::model()->findByPk($id)) === null) {
         throw new CHttpException(404, 'Страница не найдена');
     }
     return $model;
 }
Esempio n. 3
0
 public function run()
 {
     $this->htmlOptions['style'] = 'width: ' . $this->width . 'px; height: ' . $this->height . 'px;';
     $this->htmlOptions['class'] = "poll";
     $poll = Poll::model()->findByPK($this->poll_id);
     $this->render('index', array('poll' => $poll));
 }
Esempio n. 4
0
 public function init()
 {
     if (Yii::app()->hasModule('poll')) {
         $this->_poll = Poll::model()->rand()->active()->find();
         $this->title = $this->_poll->title;
         parent::init();
     }
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 /**
  * Initializes the portlet.
  */
 public function init()
 {
     $assets = Yii::app()->assetManager->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../assets');
     $clientScript = Yii::app()->clientScript;
     $clientScript->registerCssFile($assets . '/poll.css');
     $this->_poll = $this->poll_id == 0 ? Poll::model()->latest()->find() : Poll::model()->findByPk($this->poll_id);
     if ($this->_poll) {
         $this->title = $this->_poll->title;
     }
     parent::init();
 }
Esempio n. 7
0
 /**
  * Initializes the portlet.
  */
 public function init()
 {
     // Set the poll module (also kicks in the CSS via the module init)
     $this->_module = Yii::app()->getModule('poll');
     $this->attachBehavior('pollBehavior', 'poll.behaviors.PollBehavior');
     $this->_poll = $this->poll_id == 0 ? Poll::model()->with('choices', 'votes', 'totalVotes')->latest()->find() : Poll::model()->with('choices', 'votes', 'totalVotes')->findByPk($this->poll_id);
     if ($this->_poll) {
         $this->title = $this->_poll->title;
     }
     parent::init();
 }
Esempio n. 8
0
 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);
     }
 }
Esempio n. 9
0
 public function actionResult()
 {
     if (isset($_GET['id'])) {
         $poll = Poll::model()->findByPk($_GET['id']);
         echo "<div class='poll-results'>";
         foreach ($poll->getChoice($poll->id) as $choice) {
             echo CHtml::openTag("div", array("class" => "result"));
             echo CHtml::openTag("div", array("class" => "label"));
             echo $choice->name;
             echo CHtml::closeTag("div");
             echo CHtml::openTag("div", array("class" => "bar"));
             echo "<div class='fill' style='width: " . $choice->getVote($choice->id) . "%;'></div>";
             echo CHtml::closeTag("div");
             echo CHtml::openTag("div", array("class" => "totals"));
             echo "<span class='percent'>" . $choice->getVote($choice->id) . "%</span>";
             echo CHtml::closeTag("div");
             echo CHtml::closeTag("div");
         }
         echo "</div>";
     }
 }
Esempio n. 10
0
 /**
  * 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 = Poll::model()->with('choices', 'votes')->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
Esempio n. 11
0
 public function actionSave($id)
 {
     // Получаем куки для опроса
     /*
             define('COOKIE_NAME', 'poll_genplanmos_ru');
             $isCookie = isset(Yii::app()->request->cookies[COOKIE_NAME]->value);
             
             $pollCookie = $isCookie ? 
            Yii::app()->request->cookies[COOKIE_NAME] :  
            new CHttpCookie(COOKIE_NAME, ''); 
            
             $pollCookieValue = $isCookie? CJSON::decode($pollCookie->value) : array();
     * 
     */
     // Авторизован ли пользователь
     if (Yii::app()->user->isAuthenticated() === false) {
         $this->redirect(Yii::app()->user->loginUrl);
     }
     if (($user = Yii::app()->user->getProfile()) === null) {
         Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('UserModule.user', 'User not found.'));
         Yii::app()->user->logout();
         $this->redirect((array) '/user/account/login');
     }
     $poll = Poll::model()->active()->with(array('questions' => array('scopes' => 'active')))->findByPK((int) $id);
     if (!$poll) {
         throw new CHttpException(404, Yii::t('PollModule.poll', 'Страница не найдена'));
     }
     // Поиск результата
     $result = Result::model()->find('t.user_id = :user_id AND t.poll_id = :poll_id', array(':user_id' => $user->id, ':poll_id' => $poll->id));
     if ($result !== null) {
         throw new CHttpException(403, Yii::t('PollModule.poll', 'Вы уже проходили данный опрос'));
     }
     $result = new Result();
     $result->user_id = $user->id;
     $result->poll_id = $poll->id;
     // Обработка результата
     if (($data = Yii::app()->getRequest()->getPost('question')) !== null) {
         // Формирование списка ответов
         $answers = array();
         foreach ($poll->questions as $question) {
             $formValue = isset($data[$question->id]) ? $data[$question->id] : null;
             switch ($question->type) {
                 case Question::TYPE_VARIANT:
                     $answer = new Answer();
                     $answer->question_id = $question->id;
                     $answer->variant_id = $formValue !== null ? (int) $formValue : $formValue;
                     $answers[] = $answer;
                     break;
                 case Question::TYPE_MULTIPLE:
                     // Просматриваем полученные ответы
                     if (!isset($formValue) || !is_array($formValue)) {
                         break;
                     }
                     foreach ($formValue as $value) {
                         $answer = new Answer();
                         $answer->question_id = $question->id;
                         $answer->variant_id = (int) $value;
                         $answers[] = $answer;
                     }
                     break;
                 default:
                     $answer = new Answer();
                     $answer->question_id = $question->id;
                     $answer->value = $formValue !== null ? $formValue : '';
                     $answers[] = $answer;
                     break;
             }
             $result->answers = $answers;
         }
         if ($result->validate()) {
             $result->save();
             Yii::app()->ajax->success(array('html' => $this->widget('application.modules.poll.widgets.PollWidget', array('model_id' => $poll->id), true)));
         } else {
             Yii::app()->ajax->failure(array('message' => '', 'errors' => $result->getErrors()));
         }
     }
 }
Esempio n. 12
0
 public function loadModel($id)
 {
     $model = Poll::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404);
     }
     return $model;
 }
Esempio n. 13
0
 public function getPollList()
 {
     $models = Poll::model()->findAll();
     return CHtml::listData($models, 'id', 'title');
 }
Esempio n. 14
0
 public function getPollList()
 {
     return CHtml::listData(Poll::model()->findAll(array('select' => 'id, title')), 'id', 'title');
 }