Ejemplo n.º 1
0
 public function validateAnswers($attribute)
 {
     if ($attribute !== 'answers') {
         return true;
     }
     $answers = $this->{$attribute};
     $questionnaire = Questionnaire::findByPk($this->questionnaireId);
     $answeredQuestions = [];
     foreach ($answers as &$answer) {
         $answer['questionId'] = new MongoId($answer['questionId']);
         if (!isset($answer['questionId']) || !isset($answer['type']) || !isset($answer['value'])) {
             throw new BadRequestHttpException(Yii::t('content', 'invalid_answer'));
         }
         if (!in_array($answer['questionId'], $questionnaire->questions)) {
             throw new BadRequestHttpException(Yii::t('content', 'invalid_answer'));
         }
         if ($answer['type'] === Question::TYPE_CHECKBOX && !is_array($answer['value'])) {
             throw new BadRequestHttpException(Yii::t('content', 'invalid_answer'));
         }
         $answeredQuestions[] = $answer['questionId'];
     }
     //check if questionnaire has un answered question
     $noAnswerQuestions = array_diff($questionnaire->questions, $answeredQuestions);
     if (!empty($noAnswerQuestions)) {
         throw new InvalidParameterException(Yii::t('content', 'no_answer_error'));
     }
     $this->{$attribute} = $answers;
 }
 public function actionAnswer()
 {
     $params = $this->getParams();
     if (empty($params['questionnaireId']) || empty($params['answers'])) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $user = [];
     if (!empty($params['user']['channelId']) && !empty($params['user']['openId'])) {
         $channelId = $params['user']['channelId'];
         $openId = $params['user']['openId'];
         $user = $params['user'];
         try {
             $follower = Yii::$app->weConnect->getFollowerByOriginId($openId, $channelId);
             $user['name'] = empty($follower['nickname']) ? '' : $follower['nickname'];
         } catch (ApiDataException $e) {
             LogUtil::error(['message' => 'Answer questionnaire failed to get follower info', 'param' => ['openId' => $openId, 'channelId' => $channelId]]);
         }
     }
     $questionnaireId = new MongoId($params['questionnaireId']);
     $questionnaire = Questionnaire::findByPk($questionnaireId);
     //error questionnaire id or un published questionnaire
     if (empty($questionnaire) || !$questionnaire->isPublished) {
         throw new InvalidParameterException(Yii::t('content', 'invalid_questionnaire'));
     }
     //questionnaire has not begun
     $now = time();
     if (MongodbUtil::MongoDate2TimeStamp($questionnaire->startTime) > $now) {
         throw new InvalidParameterException(Yii::t('content', 'questionnaire_not_began'));
     }
     //questionnaire expired
     if (MongodbUtil::MongoDate2TimeStamp($questionnaire->endTime) < $now) {
         throw new InvalidParameterException(Yii::t('content', 'questionnaire_expired'));
     }
     if (!empty($user)) {
         $questionnaireLog = QuestionnaireLog::getByQuestionnaireAndUser($questionnaireId, $user);
         if (!empty($questionnaireLog)) {
             throw new InvalidParameterException(Yii::t('content', 'user_has_answered_questionnaire'));
         }
     }
     $quertionnaireLog = new QuestionnaireLog();
     $quertionnaireLog->questionnaireId = $questionnaireId;
     $quertionnaireLog->user = $user;
     $quertionnaireLog->answers = $params['answers'];
     $quertionnaireLog->accountId = $questionnaire->accountId;
     if ($quertionnaireLog->save()) {
         return ['message' => 'OK', 'data' => ''];
     } else {
         throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
     }
 }
 public function actionView($id)
 {
     $questionnaireId = new MongoId($id);
     $questionnaire = Questionnaire::findByPk($questionnaireId);
     if (empty($questionnaire)) {
         throw new InvalidParameterException(Yii::t('common', 'invalid_questionnaire'));
     }
     $questionnaire = $questionnaire->toArray();
     $questionnaire['userCount'] = QuestionnaireLog::countByQuestionnaireId($questionnaireId);
     foreach ($questionnaire['questions'] as &$question) {
         $questionId = new MongoId($question['id']);
         $stats = StatsQuestionnaireAnswerDaily::getQuestionOptionStats($questionId);
         $statsMap = ArrayHelper::map($stats, 'option', 'count');
         if ($question['type'] !== Question::TYPE_INPUT) {
             foreach ($question['options'] as &$option) {
                 $option['count'] = empty($statsMap[$option['content']]) ? 0 : $statsMap[$option['content']];
             }
         }
     }
     return $questionnaire;
 }