Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Question::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'pid' => $this->pid, 'relid' => $this->relid, 'questioner' => $this->questioner, 'orderid' => $this->orderid, 'status' => $this->status, 'is_show' => $this->is_show, 'source' => $this->source, 'views' => $this->views, 'answers' => $this->answers, 'create_at' => $this->create_at, 'is_rec' => $this->is_rec]);
     $query->andFilterWhere(['like', 'question', $this->question])->andFilterWhere(['like', 'detail', $this->detail])->andFilterWhere(['like', 'label', $this->label]);
     return $dataProvider;
 }
 /**
  * Get question option answer's stats info
  * @throws BadRequestHttpException
  * @return array, [{"option": "Yes", "count": 12}]
  */
 public function actionAnswers()
 {
     $params = $this->getQuery();
     if (empty($params['questionId'])) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $question = Question::findByPk(new MongoId($params['questionId']));
     if (empty($question) || $question->type === Question::TYPE_INPUT) {
         throw new InvalidParameterException(Yii::t('content', 'invalid_question'));
     }
     //turn unix timestamp to string
     $startDateStr = isset($params['startTime']) ? TimeUtil::msTime2String($params['startTime'], 'Y-m-d') : null;
     $endDateStr = isset($params['endTime']) ? TimeUtil::msTime2String($params['endTime'], 'Y-m-d') : null;
     $stats = StatsQuestionnaireAnswerDaily::getQuestionOptionStats(new MongoId($params['questionId']), $startDateStr, $endDateStr);
     $statsMap = ArrayHelper::map($stats, 'option', 'count');
     $options = [];
     $count = [];
     foreach ($question->options as $option) {
         $options[] = $option['content'];
         $count[] = empty($statsMap[$option['content']]) ? 0 : $statsMap[$option['content']];
     }
     return ['options' => $options, 'count' => $count];
 }
Example #3
0
 /**
  * Finds the Question model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Question the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Question::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Update Questionnaire.
  *
  * <b>Request Type</b>: PUT<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/content/questionnaire/{questionnaireId}<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for updating questionnaire.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     name: string<br/>
  *     startTime: string, startTime = "1429000112193"<br/>
  *     endTime: string, endTime = "1429000112193"<br/>
  *     description: string<br/>
  *     question:Array, question = [{"id": "55d6cb8be9c2fb022c8b4577","title": "math","type": "radio",
  *              "order": 0,"options": [{"icon": "support","content": "A option"},{"icon": "support",
  *              "content": "B option"}]},{"id": "55d6cb8be9c2fb022c8b4577","type": "input","title":
  *              "This is a problem","order": 1}]<br/>
  *     isPublished: boolean<br/>
  *
  * <b>Response Params:</b><br/>
  *     {
  *           "name": "name",
  *           "startTime": "1429000112193",
  *           "endTime": "1429000116193",
  *           "description": "good",
  *           "question": [
  *               {
  *                   "id": "55d6cb8be9c2fb022c8b4577",
  *                   "title": "math",
  *                   "type": "radio",
  *                   "order": 0,
  *                   "options": [
  *                       {
  *                           "icon": "support",
  *                           "content": "A option"
  *                       },
  *                       {
  *                           "icon": "support",
  *                           "content": "B option"
  *                       }
  *                   ]
  *               },
  *               {
  *                   "id": "55d6cb8be9c2fb022c8b4577",
  *                   "type": "input",
  *                   "title": "This is a problem",
  *                   "order": 1
  *               }
  *           ],
  *           "isPublished": false
  *     }
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * {
  *     "message": "OK",
  *     "data": ""
  * }
  * <pre>
  * </pre>
  */
 public function actionUpdate($id)
 {
     $id = new MongoId($id);
     $params = $this->getParams();
     $questionnaire = Questionnaire::getById($id);
     $question = [];
     $questionTitles = [];
     $questionExistIds = [];
     if (empty($questionnaire)) {
         throw new BadRequestHttpException(Yii::t('content', 'questionnaire_missing'));
     }
     if (!empty($params['startTime'])) {
         $params['startTime'] = new \MongoDate(TimeUtil::ms2sTime($params['startTime']));
     }
     if (!empty($params['endTime'])) {
         $params['endTime'] = new \MongoDate(TimeUtil::ms2sTime($params['endTime']));
     }
     $questionsItems = Question::getByIds($questionnaire->questions);
     if (!empty($params['question']) && count($params['question']) > 0) {
         foreach ($params['question'] as $questionInfo) {
             Question::checkTitle($questionInfo['title']);
             $question = ['_id' => empty($questionInfo['_id']) ? new MongoId() : new MongoId($questionInfo['_id']), 'title' => $questionInfo['title'], 'type' => $questionInfo['type'], 'order' => $questionInfo['order']];
             if (strcasecmp($questionInfo['type'], Question::TYPE_INPUT) != 0) {
                 if (is_array($questionInfo['options'])) {
                     if (Question::isQuestionOptionRepeat($questionInfo['options']) != null) {
                         $question['options'] = $questionInfo['options'];
                     }
                 }
             }
             if (in_array($question['title'], $questionTitles)) {
                 throw new InvalidParameterException(Yii::t('content', 'question_incorrect'));
             }
             $questionTitles[] = $question['title'];
             if (empty($questionInfo['id'])) {
                 $questionOption = new Question();
                 $questionOption->_id = new MongoId();
             } else {
                 $questionOption = Question::findByPk(new MongoId($questionInfo['id']));
             }
             $questionOption->title = $questionInfo['title'];
             $questionOption->type = $questionInfo['type'];
             $questionOption->order = $questionInfo['order'];
             $questionOption->options = empty($questionInfo['options']) ? [] : $questionInfo['options'];
             $questionOption->createdAt = new \MongoDate();
             if (!$questionOption->save()) {
                 throw new ServerErrorHttpException(Yii::t('content', 'update_fail'));
             }
             $questionExistIds[] = $questionOption->_id;
         }
         if (!empty($questionExistIds) && count($questionnaire->questions) >= count($questionExistIds)) {
             $isDeleteQuestion = array_diff($questionnaire->questions, $questionExistIds);
             if (count($isDeleteQuestion) > 0) {
                 $isDeQuestion = Question::deleteAll(['_id' => ['$in' => $isDeleteQuestion]]);
                 if (!$isDeQuestion) {
                     throw new ServerErrorHttpException(Yii::t('content', 'update_fail'));
                 }
             }
         }
     }
     if (!empty($params['name']) && !empty($params['startTime']) && !empty($params['endTime'])) {
         $questionnaire->name = $params['name'];
         $questionnaire->startTime = $params['startTime'];
         $questionnaire->endTime = $params['endTime'];
         $questionnaire->description = !isset($params['description']) ? '' : $params['description'];
     }
     $questionnaire->isPublished = $params['isPublished'];
     $questionnaire->createdAt = new \MongoDate();
     if (!empty($params['question']) && count($params['question']) > 0) {
         $questionnaire->questions = $questionExistIds;
     }
     unset($questionnaire->createdAt);
     if (!$questionnaire->save()) {
         throw new ServerErrorHttpException(Yii::t('common', 'update_fail'));
     } else {
         return ["message" => "OK", "data" => ""];
     }
 }
Example #5
0
 /**
  * Save questions.
  * @param $account MongoId
  * @param $condition Object
  */
 public static function saveQuestions($condition)
 {
     if (count($condition) > 0) {
         return Question::batchInsert($condition);
     }
 }
Example #6
0
 /**
  * Get question by order.
  * @param $question
  */
 public function getByOrder($question)
 {
     return Question::find()->where(['_id' => ['$in' => $question]])->orderBy(['order' => SORT_ASC])->all();
 }