Ejemplo n.º 1
0
 /**
  * Редактирование голосования
  */
 public function action_edit()
 {
     if (!($vote = \Model_Vote::find(1))) {
         \Session::set_flash('error', 'Нет записи в БД. Обратитесь к разработчику.');
         \Response::redirect('admin/articles');
     }
     $vote->answers = json_decode($vote->answers_json);
     $val = \Model_Vote::validate('edit');
     if ($val->run()) {
         $vote->question = \Input::post('question');
         $vote->enable = \Input::post('enable');
         // Варианты ответов
         $answers = array();
         for ($i = 1; $i <= 10; $i++) {
             if (\Input::post('reset') == 1) {
                 $answers[] = array('answer' => \Input::post('answer_' . $i), 'count' => 0);
             } else {
                 $answers[] = array('answer' => \Input::post('answer_' . $i), 'count' => $vote->answers[$i - 1]->count);
             }
         }
         $vote->answers_json = json_encode($answers);
         // Hash для кукисов
         if (\Input::post('reset') == 1) {
             $vote->hash = md5(date('Y-m-d H:i:s'));
         }
         if ($vote->save()) {
             \Session::set_flash('success', 'Голосование обновлено.');
             \Response::redirect('admin/votes/edit');
         } else {
             \Session::set_flash('error', 'Could not update Vote #' . $id);
         }
     } else {
         if (\Input::method() == 'POST') {
             $vote->question = $val->validated('question');
             $vote->answers_json = $val->validated('answers_json');
             $vote->hash = $val->validated('hash');
             $vote->enable = $val->validated('enable');
             \Session::set_flash('error', $val->error());
         }
         $this->template->set_global('vote', $vote, false);
     }
     $this->template->content = \View::forge('votes/edit');
 }
Ejemplo n.º 2
0
 /**
  * 增加新的投票
  */
 private function _voteAdd()
 {
     if ($this->_isPost()) {
         $this->_modelVote = $this->_getGlobalData('Model_Vote', 'object');
         $data = $this->_modelVote->add($_POST);
         $this->_utilMsg->showMsg($data['msg'], $data['status'], $data['href']);
     } else {
         $users = $this->_getGlobalData('user');
         $this->_modelVoteConf = $this->_getGlobalData('Model_VoteConf', 'object');
         $selectedIsOpen = 1;
         //默认马上公开投票结果
         $voteConfList = $this->_modelVoteConf->findAll();
         foreach ($voteConfList as &$list) {
             $list['url_use_conf'] = Tools::url(CONTROL, 'VoteSetup', array('conf_id' => $list['Id'], 'doaction' => 'add'));
             if ($_GET['conf_id'] == $list['Id']) {
                 //如果有传过来conf_id就表示用户选择了投票配置,那么就会搜出此配置
                 $selectedConf = $list;
                 $selectedConf['content'] = unserialize($selectedConf['content']);
                 $selectedConf['vote_user'] = unserialize($selectedConf['vote_user']);
                 $this->_view->assign('selectedConf', $selectedConf);
                 $selectedIsOpen = $selectedConf['is_open'];
             }
         }
         $this->_view->assign('selectedIsOpen', $selectedIsOpen);
         $this->_view->assign('voteConfList', $voteConfList);
         $this->_view->assign('radioIsOpen', array('1' => '是', '0' => '否'));
         #------显示分组用户------#
         $this->_modelOrg = $this->_getGlobalData('Model_Org', 'object');
         $orgList = $this->_modelOrg->findUsersToCache();
         //获取所有用户,按分组来显示
         $this->_view->assign('org', $orgList);
         #------显示分组用户------#
         $this->_view->assign('users', $users);
         $this->_view->set_tpl(array('body' => 'ServiceTools/AddVote.html'));
         $this->_utilMsg->createNavBar();
         $this->_view->display();
     }
 }
Ejemplo n.º 3
0
 /**
  * Действие для отображения голосований
  */
 public function action_votes()
 {
     $votes = \Model_Vote::find(1);
     // Проверяем включён ли виджет
     if ($votes->enable) {
         $data['question'] = $votes->question;
         $data['answers'] = json_decode($votes->answers_json);
         // Если пользователь нажал "Проголосовать"
         if (\Input::method() == 'POST') {
             if (\Input::post('answers')) {
                 // Прибавляем 1 к счётчику ответа
                 foreach ($data['answers'] as $key => $item) {
                     if (\Input::post('answers') == $item->answer) {
                         $data['answers'][$key]->count++;
                     }
                 }
                 $votes->answers_json = json_encode($data['answers']);
                 $votes->save();
                 // Записываем куку на месяц
                 \Cookie::set('vote_' . $votes->hash, 1, 60 * 60 * 24 * 31);
                 \Response::redirect_back('');
             }
         }
         // Если пользователь проголосовал
         if (\Cookie::get('vote_' . $votes->hash)) {
             // Количество голосов
             $data['count'] = 0;
             foreach ($data['answers'] as $item) {
                 $data['count'] += $item->count;
             }
             return \View::forge('widgets/votes/results', $data, FALSE)->render();
         } else {
             return \View::forge('widgets/votes/quiz', $data, FALSE)->render();
         }
     } else {
         return \View::forge('widgets/votes/empty')->render();
     }
 }
Ejemplo n.º 4
0
 /**
  * Insert vote to DB
  * @return boolean
  */
 public function insert()
 {
     if (!$this->validate()) {
         return false;
     }
     $modelVote = new Model_Vote();
     $result = $modelVote->insert(array("fkEntryId" => $this->getFkEntryId(), "fkGroupId" => $this->getFkGroupId(), "ts" => $this->getTs()));
     if ($result === false) {
         // error
         $this->getErrorObj()->setError('VOTE_DB_ERROR');
         return false;
     } else {
         // all good
         $this->setId($result);
         return true;
     }
 }