/** * View Questionnaire by questionnaire. * * <b>Request Type</b>: GET<br/><br/> * <b>Request Endpoint</b>:http://{server-domain}/api/questionnaire/{questionnaireId}&channelId={channelId}&openId={openId}br/><br/> * <b>Response Content-type</b>: application/json<br/><br/> * <b>Summary</b>: This api is used for viewing questionnaire. * <br/><br/> * * <b>Request Params</b>:<br/> * id, string<br/> * channelId, string<br/> * openId, string<br/> * * <b>Response Example:</b><br/> * { * "_id": "55d6cb8be9c2fb022c8b4579", * "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, * "answerTime": "2015-08-26 10:28:55", * "isAnswered": false * } * <pre> * </pre> */ public function actionView($id) { $channelId = $this->getQuery('channelId'); $openId = $this->getQuery('openId'); $isAnswered = false; $question = []; $answerTime = ''; $questionnaire = []; $user = ["channelId" => $channelId, "openId" => $openId]; $questionnaireInfo = Questionnaire::getById(new MongoId($id)); if (empty($questionnaireInfo)) { throw new InvalidParameterException(Yii::t('content', 'questionnaire_no_exist')); } if (!empty($channelId) && !empty($openId)) { $questionnaireLogInfo = QuestionnaireLog::getByQuestionnaireAndUser(new MongoId($id), $user); if (!empty($questionnaireLogInfo)) { $answerTime = MongodbUtil::MongoDate2String($questionnaireLogInfo->createdAt, 'Y-m-d H:i:s'); $isAnswered = true; } } $questionnaire = $questionnaireInfo->toArray(); $questionnaire['answerTime'] = $answerTime; $questionnaire['isAnswered'] = $isAnswered; return $questionnaire; }
/** * 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" => ""]; } }