17 if(!isset($_POST[
'question'], $_POST[
'answer'], $_POST[
'additionalFields'])) {
18 throw new CHttpException(400,
'Bad fields');
21 if(is_array($_POST[
'answer'])) {
22 $answers =
Answer::model()->findAllByAttributes(array(
'question_id' => $_POST[
'question'],
'id' => $_POST[
'answer']));
24 throw new CHttpException(400);
26 foreach($answers as $answ)
29 $userAnswer->answer_id = $answ->id;
30 $userAnswer->user_id = $this->module->getUserId();
31 $userAnswer->additional = $_POST[
'additionalFields'];
32 if(!$userAnswer->save()) {
33 throw new Exception(print_r($userAnswer->getErrors(),
true));
37 $answer =
Answer::model()->findByAttributes(array(
'question_id' => $_POST[
'question'],
'id' => $_POST[
'answer']));
39 throw new CHttpException(400);
42 $userAnswer->answer_id = $answer->id;
43 $userAnswer->user_id = $this->module->getUserId();
44 $userAnswer->additional = $_POST[
'additionalFields'];
45 if(!$userAnswer->save()) {
46 throw new Exception(print_r($userAnswer->getErrors(),
true));
49 $this->module->setUserVoted($_POST[
'question']);
50 if(isset($_POST[
'forwardTo'])) {
51 $this->redirect($_POST[
'forwardTo']);
56 public function actionVoteResults()
58 if(isset($_GET[
'options'])){
59 $options = $_GET[
'options'];
60 $options = unserialize(Yii::app()->securityManager->validateData($options));
61 if($options !==
false) {
62 $this->renderpartial(
'voteResults', array(
'options' => $options));
67 throw new CHttpException(400,
'Bad options');
70 public function actionReset($id)
72 $this->module->setUserUnvoted($id);
73 if(isset($_SERVER[
'HTTP_REFERER'])) {
74 $this->redirect($_SERVER[
'HTTP_REFERER']);
78 public function actionDownloadCsv($id)
80 if(!$this->module->isAdmin()){
81 throw new CHttpException(400,
'You have no permission');
84 ->with(array(
'answers' => array(
'condition' =>
'active = true',
'with' => array(
'userAnswersCount'))))
86 $answers = $question->answers;
87 usort($answers,
function($a, $b){
return $a->order - $b->order;});
88 $csv =
"Question| Answer| Votes\n";
90 foreach($answers as $answ) {
91 $csv .=
"{$question->text}| {$answ->text}| {$answ->userAnswersCount}\n";
93 Yii::app()->request->sendFile(
'report.csv', $csv, null,
false);