Ejemplo n.º 1
0
 /**
  * 分数型心里测试 问题
  */
 public function actionStep4_2_question($id)
 {
     $this->layout = false;
     $model = Survey::findOne($id);
     // 没有找到
     if (!$model || $model->uid != ZCommonSessionFun::get_user_id()) {
         $model = new Survey();
         if (!$model) {
             return $this->redirect(['my']);
         }
     }
     $page = Yii::$app->request->get('page', 1);
     $error = '';
     $posts = Yii::$app->request->post();
     //         ZCommonFun::print_r_debug($_POST);
     //         exit;
     // post提交
     $model_SurveyResulte = new SurveyOperation();
     $url = $model_SurveyResulte->step4_2_questionSave($posts, $id, $page);
     if ($url || isset($_POST['save']) && $questionData['count'] > -1) {
         // ZCommonFun::print_r_debug($url);
         // ZCommonFun::print_r_debug($posts);
         // ZCommonFun::print_r_debug($model);
         // exit;
         $model->is_publish = 0;
         $model->save();
         switch ($model->tax) {
             // 分数型心里测试
             case 2:
                 if (isset($posts['save-next'])) {
                     return $this->redirect($url);
                 } else {
                     return $this->redirect(['step4_3', 'id' => $model->id]);
                 }
                 break;
                 // 跳转型心里测试
             // 跳转型心里测试
             case 3:
                 if (isset($posts['save-next'])) {
                     return $this->redirect($url);
                 } else {
                     return $this->redirect(['step4_2', 'id' => $model->id]);
                 }
                 break;
             default:
                 break;
         }
     }
     // 查找问题
     $questionData = $model->findOneQuestion($model->id, 1, $page - 1);
     if (count($questionData['question']) < 1) {
         if (!$model_SurveyResulte->save_question) {
             //                 $page = $page - 1;
         }
     }
     return $this->render('step4_2_question', ['tax' => $model->tax, 'model' => $model, 'page' => $page, 'questionData' => $questionData]);
 }
Ejemplo n.º 2
0
 /**
  * 分数型问题回答2
  */
 public function actionStep2Answer2($id)
 {
     $this->layout = false;
     $model = Survey::findOne($id);
     if (!$model) {
         //没找到
         $model = new Survey();
     }
     if ($model->is_publish < 1) {
         $this->popupMessage($model->title . '未发布,不能测试');
         exit;
     }
     $data = $model->FindAllQuestionsOptions($id);
     //                 ZCommonFun::print_r_debug($_POST);
     //                 exit;
     $posts = Yii::$app->request->post();
     $error = '';
     if (isset($posts['name'])) {
         is_array($posts['options']) ?: ($posts['options'] = array());
         $op = count($posts['options']) > 0 ? true : false;
         $res = isset($posts['res']) ? intval($posts['res']) : 0;
         $res_model_SurveyResulte = $res > 0 ? SurveyResulte::findOne($res) : null;
         if ($res_model_SurveyResulte && $res_model_SurveyResulte->s_id == $id) {
         } else {
             $res_model_SurveyResulte = null;
         }
         $total_score = 0;
         $save = 0;
         $result = null;
         //保存结果
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $name = isset($posts['name']) ? $posts['name'] : '';
             $year = isset($posts['birth']['year']) ? $posts['birth']['year'] : 2015;
             $month = isset($posts['birth']['month']) ? $posts['birth']['month'] : '01';
             $day = isset($posts['birth']['day']) ? $posts['birth']['day'] : '01';
             $constellation = isset($posts['constellation']) ? intval($posts['constellation']) : 0;
             $constellation = $constellation > 0 && $constellation < 13 ? $constellation : 0;
             $birth = $year . '-' . $month . '-' . $day;
             $birth = date('Y-m-d', strtotime($birth));
             $model_AnswerUser = new AnswerUser();
             $model_AnswerUser->sid = $id;
             $model_AnswerUser->table = 'survey_resulte';
             $res_model_SurveyResulte ? $model_AnswerUser->table_id = $res_model_SurveyResulte->sr_id : null;
             $model_AnswerUser->answer_name = $name;
             $model_AnswerUser->answer_age = $birth;
             $model_AnswerUser->constellation = $constellation;
             $model_AnswerUser->ip = self::getUserIP();
             if (!$model_AnswerUser->save()) {
                 $error = '用户操作失败';
                 $transaction->rollBack();
             } else {
                 foreach ($data['options'] as $key => $value) {
                     foreach ($value as $option) {
                         foreach ($posts['options'] as $question_id => $row) {
                             //找到选项
                             if ($option->qo_id = $row[0]) {
                                 $model_Answer = new Answer();
                                 $model_Answer->table_id = $id;
                                 $model_Answer->question_id = $option->question_id;
                                 $model_Answer->qo_id = $row[0];
                                 $model_Answer->answer_score += $option->option_score;
                                 $model_Answer->au_id = $model_AnswerUser->au_id;
                                 $total_score += $model_Answer->answer_score;
                                 $model_Answer->save() ? $save++ : null;
                             }
                         }
                     }
                 }
                 if ($save > 0) {
                     $model_SurveyResulte = new SurveyResulte();
                     //如果直接选择了答案
                     if ($model_AnswerUser->table == 'survey_resulte' && $model_AnswerUser->table_id > 0) {
                         $result = $res_model_SurveyResulte;
                         //
                     } else {
                         $result = $model_SurveyResulte->getStep2Result($id, $model_AnswerUser->answer_score);
                     }
                     if ($result && !$res_model_SurveyResulte) {
                         //保存回答者结果,防止用户更改数据导致结果丢失
                         $mode_AnswerSurveyResulte = new AnswerSurveyResulte();
                         $mode_AnswerSurveyResulte->attributes = $result->attributes;
                         $mode_AnswerSurveyResulte->save();
                         $model_AnswerUser->table == 'answer_survey_resulte';
                         $model_AnswerUser->table_id = $mode_AnswerSurveyResulte->answer_sr_id;
                     }
                     $model_AnswerUser->save();
                     $transaction->commit();
                     //设置测试数量
                     $model->setAnswerCount($id);
                     $model_UserProfile = new UserProfile();
                     $model_UserProfile->setTestCount($model_AnswerUser->uid);
                     return $this->redirect(['resulte', 'au_id' => $model_AnswerUser->au_id]);
                 } else {
                     $error = '没有选项';
                     $transaction->rollBack();
                 }
             }
         } catch (\Exception $e) {
             $error = '事物异常';
             ZCommonFun::print_r_debug($e);
             $transaction->rollBack();
         }
     }
     $model_Users = null;
     $model_UsersProfile = null;
     if ($model->uid) {
         $zhao_condition['uid'] = $model->uid;
         $model_Users = User::findOne($zhao_condition);
         $model_UsersProfile = UserProfile::findOne($zhao_condition);
     }
     !$model_Users ? $model_Users = new User() : '';
     !$model_UsersProfile ? $model_UsersProfile = new UserProfile() : '';
     $model_UsersProfile->uid = $model->uid;
     //         echo $error;
     //         exit;
     //         ZCommonFun::print_r_debug($result);
     $model_SurveyOperation = new SurveyOperation();
     $models_SurveyOperation = $model_SurveyOperation->getIsTop();
     return $this->render('step2answer3', ['models_SurveyOperation' => $models_SurveyOperation, 'data' => $data, 'model' => $model, 'error' => $error, 'posts' => $posts, 'image' => Survey::getImageUrl($model), 'model_Users' => $model_Users, 'model_UsersProfile' => $model_UsersProfile, 'randSurvey' => $this->getRandSurvey()]);
     //         return $this->render('step2answer',[
     //             'data'=>$data,
     //             'model'=>$model,
     //             'error'=>$error,
     //             'posts'=>$posts,
     //             'image'=>Survey::getImageUrl($model),
     //             'model_Users'=>$model_Users,
     //             'model_UsersProfile'=>$model_UsersProfile,
     //         ]);
 }