/**
  * Vote for selected float answer
  * @return mixed Json
  */
 public function actionVoteFloat()
 {
     $answerValues = Yii::$app->request->post('answer');
     $userID = isset(Yii::$app->user->id) ? Yii::$app->user->id : false;
     // validation
     $answers = [];
     if (is_array($answerValues) && $userID) {
         foreach ($answerValues as $aid => $value) {
             $answer = $this->findModel($aid);
             if (isset($answer)) {
                 $answers[] = $answer;
             }
         }
     }
     if (count($answers) == 0) {
         Yii::$app->getSession()->setFlash('error-question', 'Ошибка при сохранении ответа в опросе: Нет выбранных вариантов.');
         return Yii::$app->getResponse()->redirect(Url::to('/'));
     }
     $first = $answers[0];
     $question = $this->findModel($first->parent_id);
     if (isset($question)) {
         $question->voutes++;
         $vote = new QuestionVote();
         $vote->question_id = $question->id;
         $vote->user_id = $userID;
         if (!$vote->validate() || !$question->validate()) {
             Yii::$app->getSession()->setFlash('error-question', 'Ошибка при сохранении ответа в опросе.');
             return Yii::$app->getResponse()->redirect(Url::to('/'));
         }
         $vote->save(false);
         $question->save(false);
         foreach ($answers as $answer) {
             if (!isset($answerValues[$answer->id])) {
                 continue;
             }
             $value = $answerValues[$answer->id];
             if ($answer->mark == 0) {
                 $answer->mark = $value;
             } else {
                 $answer->mark = round(($value + $answer->mark * ($question->voutes - 1)) / $question->voutes, 4);
             }
             $answer->save();
         }
         Yii::$app->getSession()->setFlash('success-question', 'Ваш ответ на опрос успешно сохранен.');
     }
     return Yii::$app->getResponse()->redirect(Url::to('/'));
 }
 /**
  * Vote for selected float answer
  * @return mixed Json
  */
 public function actionVoteFloat()
 {
     $answerValues = Yii::$app->request->get('answer');
     $userID = isset(Yii::$app->user->id) ? Yii::$app->user->id : false;
     // validation
     $answers = [];
     if (is_array($answerValues) && $userID) {
         foreach ($answerValues as $aid => $value) {
             $answer = $this->findModel($aid);
             if (isset($answer)) {
                 $answers[] = $answer;
             }
         }
     }
     if (count($answers) == 0) {
         return Json::encode(['success' => false, 'msg' => 'answers']);
     }
     $first = $answers[0];
     $question = $this->findModel($first->parent_id);
     if (isset($question)) {
         $question->voutes++;
         $vote = new QuestionVote();
         $vote->question_id = $answer->parent_id;
         $vote->user_id = $userID;
         if (!$vote->validate() || !$question->validate()) {
             return Json::encode(['success' => false, 'msg' => 'vote']);
         }
         $vote->save();
         $question->save();
         foreach ($answers as $answer) {
             $answer->mark = round(($value + $answer->mark * ($question->voutes - 1)) / $question->voutes, 4);
             $answer->save();
         }
         return Json::encode(['success' => true]);
     }
     return Json::encode(['success' => false]);
 }
 /**
  * Get question block
  * @param \common\models\Question|bool|false $question
  * @param int $id
  * @return array Data
  */
 public static function getQuestionBlockTitle($question = false, $id = NULL)
 {
     if (isset($id)) {
         $question = Question::find()->where(['id' => $id])->orderBy(['created_at' => SORT_DESC])->one();
     } else {
         if (!$question) {
             $question = Question::find()->where(['is_active' => 1])->orderBy(['created_at' => SORT_DESC])->one();
         }
     }
     if (!isset($question)) {
         $question = Question::find()->where(['parent_id' => null])->orderBy(['created_at' => SORT_DESC])->one();
     }
     $uid = isset(Yii::$app->user->id) ? Yii::$app->user->id : 0;
     $userVote = QuestionVote::find()->where(['question_id' => $question->id, 'user_id' => $uid])->one();
     if (isset($userVote->id) || !$question->is_active) {
         $block = true;
     } else {
         $block = false;
     }
     $answers = Question::find()->where(['parent_id' => $question->id])->orderBy(['id' => SORT_ASC])->all();
     if (!isset($id)) {
         if ($block) {
             $view = $question->is_float ? 'blocks/question_float_block_title' : 'blocks/question_block_title';
         } else {
             $view = $question->is_float ? 'forms/question_float_form' : 'forms/question_form';
         }
     } else {
         if ($block) {
             $view = $question->is_float ? 'blocks/question_float_block' : 'blocks/question_block';
         } else {
             $view = $question->is_float ? 'forms/question_float_form' : 'forms/question_form';
         }
     }
     $block = ['view' => '@frontend/views/' . $view, 'data' => compact('question', 'answers')];
     return $block;
 }