/** * Finds the Question model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $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.'); } }
/** * 分数型测试问题 * @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]; } } }