コード例 #1
0
ファイル: QuestionController.php プロジェクト: fnoorman/dev
 /**
  * Creates a new Question model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Question();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 /**
  * Vote for selected float answer
  * @param $matchID int
  * @return mixed Json
  */
 public function actionAutogen($matchID)
 {
     $match = \common\models\Match::findOne($matchID);
     if (!isset($match)) {
         throw new NotFoundHttpException('Страница не найдена.');
     }
     if (in_array($match->teamHome->id, \common\models\Team::getTeamsConstants())) {
         $ourTeam = $match->teamHome;
         $opponentTeam = $match->teamGuest;
     } else {
         $ourTeam = $match->teamGuest;
         $opponentTeam = $match->teamHome;
     }
     $basisPlayers = \common\models\Composition::find()->where(['match_id' => $match->id, 'command_id' => $ourTeam->id, 'is_basis' => 1])->all();
     $compositionTable = \common\models\Composition::tableName();
     $matchEventTable = \common\models\MatchEvent::tableName();
     $substitutionPlayers = \common\models\Composition::find()->innerJoin($matchEventTable, "{$matchEventTable}.substitution_id = {$compositionTable}.id")->where([$compositionTable . '.match_id' => $match->id, 'command_id' => $ourTeam->id])->all();
     $teamPlayers = array_merge($basisPlayers, $substitutionPlayers);
     $question = new Question();
     $question->title = 'Оценки игрокам ' . $ourTeam->name . ' в матче с ' . $opponentTeam->name;
     $question->voutes = 0;
     $question->is_active = 1;
     $question->is_float = 1;
     $question->is_multipart = 0;
     $question->mark = 0;
     if ($question->save()) {
         foreach ($teamPlayers as $teamPlayer) {
             $answer = new Question();
             $answer->parent_id = $question->id;
             $answer->title = $teamPlayer->name;
             $answer->mark = 0;
             $answer->save();
         }
     }
     return $this->redirect(['view', 'id' => $question->id]);
 }
コード例 #3
0
 /**
  *  分数型测试问题
  * @param unknown $posts
  * @param unknown $id
  */
 public function step4_2_questionSave($posts, $id, $page)
 {
     $url = '';
     $error = '';
     if (isset($posts['label']['option-label'][0])) {
         //保存问题!empty($posts['label-name'] )
         if (isset($posts['label-name'])) {
             $transacation = Yii::$app->db->beginTransaction();
             try {
                 $question_id = isset($posts['qid']) ? $posts['qid'] : 0;
                 if ($question_id > 0) {
                     $model_Question = Question::findOne(['question_id' => $question_id]);
                     //不是当前测试的问题
                     if ($model_Question && $model_Question->table_id != $id) {
                         $model_Question = new Question();
                     }
                 } else {
                     $model_Question = new Question();
                 }
                 $model_Question->label = $posts['label-name'];
                 $model_Question->table_id = $id;
                 $model_Question->uid = ZCommonSessionFun::get_user_id();
                 $model_Question->update_time = date('Y-m-d H:i:s');
                 $save = 0;
                 $len = count($posts['label']['option-label']);
                 if ($model_Question->save()) {
                     $this->save_question = true;
                     foreach ($posts['label']['option-label'] as $key => $value) {
                         $qo_id = isset($posts['label']['qo-id'][$key]) ? intval($posts['label']['qo-id'][$key]) : 0;
                         //验证问题
                         if (empty($value)) {
                             //删除空选项
                             $model_QuestionOptions = $qo_id > 0 ? QuestionOptions::findOne($qo_id) : null;
                             //不是当前测试的选项
                             if ($model_QuestionOptions && $model_QuestionOptions->table_id != $id) {
                                 continue;
                             } else {
                                 if ($model_QuestionOptions) {
                                     $model_QuestionOptions->delete();
                                 }
                             }
                             continue;
                         }
                         if ($qo_id > 0) {
                             $model_QuestionOptions = QuestionOptions::findOne($qo_id);
                             //不是当前测试的选项
                             if ($model_QuestionOptions->table_id != $id) {
                                 continue;
                             }
                             $save++;
                         } else {
                             $model_QuestionOptions = new QuestionOptions();
                         }
                         $model_QuestionOptions->question_id = $model_Question->question_id;
                         $model_QuestionOptions->table_id = $id;
                         $model_QuestionOptions->uid = ZCommonSessionFun::get_user_id();
                         $model_QuestionOptions->option_label = $value;
                         $model_QuestionOptions->question_id = $model_Question->question_id;
                         $score = isset($posts['label']['option-score'][$key]) ? $posts['label']['option-score'][$key] : 1;
                         $score = (int) $score;
                         $model_QuestionOptions->option_score = $score;
                         if ($model_QuestionOptions->save()) {
                             $save++;
                         }
                     }
                     if ($save > 0 || !empty($posts['label-name'])) {
                         $error = '保存成功';
                         $is_commit = true;
                         $this->save_question = true;
                         $transacation->commit();
                     } else {
                         $this->save_question = false;
                         if ($model_Question && $model_Question->question_id > 0 && $model_Question->delete()) {
                             $error = '删除成功';
                             $is_commit = true;
                             $transacation->commit();
                         } else {
                             $error = '删除选项失败';
                             throw new \Exception('删除选项失败');
                         }
                     }
                 } else {
                     $this->save_question = false;
                     $error = '保存失败';
                     throw new \Exception($error);
                 }
             } catch (\Exception $e) {
                 if (isset($is_commit) && $is_commit === true) {
                 } else {
                     $transacation->rollBack();
                     $error = '事物异常';
                 }
                 $this->errorResulte = $error;
             }
         } else {
             $this->save_question = false;
             $error = '提交表单错误';
         }
         if (isset($posts['save-next'])) {
             //                ZCommonFun::print_r_debug($save);
             //                exit;
             return $url = ['step4_2_question', 'id' => $id, 'page' => $page];
         } else {
             return $url = ['step4_2', 'id' => $id];
         }
     }
 }