public function run() { $voting = null; if (is_numeric(HU::post('id_voting'))) { $voting = Voting::model()->onlyActive()->with('answer')->findByPk(HU::post('id_voting')); } if ($voting == null) { //echo CHtml::encode($this->controller->widget('vote.widgets.VoteWidget', null, true)); return; } if (Yii::app()->vote->check($voting->id_voting)) { $answers = $_POST['VotingAnswer']['name']; $cr = new CDbCriteria(); $cr->addColumnCondition(array('id_voting' => $voting->id_voting)); if (is_array($answers)) { $cr->addInCondition('id_voting_answer', $answers); } else { if (is_numeric($answers)) { $cr->addColumnCondition(array('id_voting_answer' => $answers)); } } VotingAnswer::model()->updateCounters(array('count' => 1), $cr); VisitSite::saveCurrentVisit(Voting::ID_OBJECT, $voting->id_voting); Yii::app()->user->setState('vote_' . $voting->id_voting, time()); // перегружаем голосовалку, чтоб обновились показатели счетчиков $voting = Voting::model()->onlyActive()->with('answer')->findByPk($voting->id_voting); } $voteCount = $voting->getSumVote(); echo CHtml::encode($this->controller->renderPartial("vote.widgets.views.statistic", array('voting' => $voting, 'voteCount' => $voteCount)), null, true); }
public function afterSave($event) { $className = get_class(VotingAnswer::model()); $postAnswers = HU::post($className, array()); foreach ($this->answers as $answer) { /** * @var $answer VotingAnswer */ if (isset($postAnswers[$answer->id_voting_answer]['name']) && trim($postAnswers[$answer->id_voting_answer]['name']) != '') { $answer->name = trim($postAnswers[$answer->id_voting_answer]['name']); $answer->update(array('name')); } else { $answer->delete(); } } foreach ($postAnswers as $i => $name) { if ($i > 0) { continue; } if (trim($name['name']) == '') { continue; } $answer = BaseActiveRecord::newModel($className, 'backendInsert'); $answer->id_voting = $this->model->id_voting; $answer->name = trim($name['name']); $answer->save(); } }
* @var $this AnswerListWidget * @var $model Voting * @var $form CActiveForm */ $answers = $this->answers; $totalCount = $this->countAnswers - count($answers) < 5 ? count($answers) + 5 : $this->countAnswers; // минимум будет 5 доп. пустых ответов $totalVote = 0; foreach ($answers as $answer) { $totalVote += $answer->count; } foreach ($answers as $i => $answer) { /** * @var $answer VotingAnswer */ echo '<tr> <td>' . $form->textField($answer, '[' . $answer->id_voting_answer . ']name', array('maxlength' => '100', 'style' => 'width:100%')) . '</td> <td> Голосов: ' . $answer->count . ' (' . ($totalVote > 0 ? round($answer->count / $totalVote * 100) : 0) . '%)</td> </tr>'; } $className = get_class(VotingAnswer::model()); for ($i = 0; $i < $totalCount - count($answers); $i++) { echo '<tr> <td>' . CHtml::textField($className . '[' . -1 * ($i + 1) . '][name]', '', array('maxlength' => '100', 'style' => 'width:100%')) . '</td> <td> </td> </tr>'; } ?> </table> </div>