/**
  * Create Questionnaire.
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/content/questionnaires<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for creating 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 = [{"title": "math","type": "radio","order": 0,"options": [{"icon":
  *              "support","content": "A option" },{"icon": "support","content": "B option"}]},{"type":
  *              "input","order": 1,"title": "This is a problem"}]<br/>
  *     isPublished: boolean<br/>
  *
  * <b>Response Params:</b><br/>
  *     {
  *           "name": "name",
  *           "startTime": "1429000112193",
  *           "endTime": "1429000116193",
  *           "description": "good",
  *           "question": [
  *               {
  *                   "title": "math",
  *                   "type": "radio",
  *                   "order": 0,
  *                   "options": [
  *                       {
  *                           "icon": "support",
  *                           "content": "A option"
  *                       },
  *                       {
  *                           "icon": "support",
  *                           "content": "B option"
  *                       }
  *                   ]
  *               },
  *               {
  *                   "type": "input",
  *                   "order": 1,
  *                   "title": "This is a problem"
  *               }
  *           ],
  *           "isPublished": false
  *       }
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * {
  *     "message": "OK",
  *     "data": ""
  * }
  * <pre>
  * </pre>
  */
 public function actionCreate()
 {
     $params = $this->getParams();
     if (!isset($params['name']) || empty($params['startTime']) || empty($params['endTime']) || !isset($params['isPublished'])) {
         throw new InvalidParameterException(Yii::t('common', 'parameters_missing'));
     }
     Questionnaire::isNameExist($params['name']);
     $params['startTime'] = new \MongoDate(TimeUtil::ms2sTime($params['startTime']));
     $params['endTime'] = new \MongoDate(TimeUtil::ms2sTime($params['endTime']));
     $token = $this->getAccessToken();
     $tokenInfo = Token::getToken($token);
     $accountId = $tokenInfo['userId'];
     $params['accountId'] = $tokenInfo['accountId'];
     $condition = [];
     $question = [];
     $questionIds = [];
     $options = [];
     $questionTitles = [];
     if (!empty($params['question']) && count($params['question']) > 0) {
         foreach ($params['question'] as $questionInfo) {
             $questionId = new MongoId();
             $questionIds[] = $questionId;
             Question::checkTitle($questionInfo['title']);
             $question = ['_id' => $questionId, 'type' => $questionInfo['type'], 'title' => $questionInfo['title'], 'order' => $questionInfo['order'], 'createdAt' => new \MongoDate(), 'accountId' => $accountId];
             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'];
             $condition[] = $question;
         }
         $isSaveQuestions = Question::saveQuestions($condition);
         if (!$isSaveQuestions) {
             throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
         }
     }
     $account = User::findOne(['_id' => new MongoId($accountId)]);
     $questionnaire = new Questionnaire();
     $questionnaire->name = $params['name'];
     $questionnaire->startTime = $params['startTime'];
     $questionnaire->endTime = $params['endTime'];
     $questionnaire->description = !isset($params['description']) ? '' : $params['description'];
     $questionnaire->creator = ['id' => $account['_id'], 'name' => !isset($account['name']) ? '' : $account['name']];
     $questionnaire->questions = $questionIds;
     $questionnaire->accountId = $params['accountId'];
     $questionnaire->isPublished = $params['isPublished'];
     $questionnaire->createdAt = new \MongoDate();
     if (!$questionnaire->save()) {
         throw new ServerErrorHttpException(Yii::t('common', 'save_fail'));
     } else {
         return ["message" => "OK", "data" => ""];
     }
 }