/** * 获取调查问题 * @param unknown $survey_id * @return multitype: */ public function FindAllQuestionsOptions($survey_id) { $model_Question = new Question(); $model_QuestionOptions = new QuestionOptions(); $condition['table_id'] = $survey_id; $a_Question = $model_Question->find()->where($condition)->orderBy(['question_id' => SORT_ASC])->all(); $a_QuestionOptions = $model_QuestionOptions->find()->where($condition)->orderBy(['question_id' => SORT_ASC, 'qo_id' => SORT_ASC])->all(); $a_Question ? null : ($a_Question = []); $a_QuestionOptions ? null : ($a_QuestionOptions = []); $a_options = []; $question_total_count = 0; $question_total_score = 0; $question_total_min_score = 0; //没有问题就没有选项 if (!isset($a_Question[0])) { $a_QuestionOptions = []; } else { foreach ($a_Question as $key => &$row) { $question_total_count++; //问题最大分数 $question_max_score = 0; $question_min_score = null; foreach ($a_QuestionOptions as $key2 => &$row2) { if ($row->question_id == $row2->question_id) { $a_options[$key][] = $row2; //获取问题最大得分 $question_max_score = max($row2->option_score, $question_max_score); $question_min_score = $question_min_score == null ? $row2->option_score : min($question_min_score, $row2->option_score); // echo $row2->option_score,'=',$question_total_score,'<br/>'; } } //设置问题总分数 $question_total_score += $question_max_score; $question_total_min_score += $question_min_score; // isset($a_Question[$key]['options'][0])? null :$a_Question[$key]['options']=[]; } } $data['questions'] = $a_Question; $data['options'] = $a_options; $data['question_total_count'] = $question_total_count; $data['question_total_score'] = $question_total_score; $data['question_total_min_score'] = $question_total_min_score; return $data; }
/** * 删除 */ public function actionQuestionDelete($id, $page) { $id = intval($id); $page = intval($page); $this->layout = false; $id > 0 ? $model = Survey::findOne($id) : ($model = false); // 没有找到 if (!$model || $model->uid != ZCommonSessionFun::get_user_id()) { if (!$model) { return $this->redirect(['my']); } } // 查找问题 $questionData = $model->findOneQuestion($model->id, 1, $page - 1); // ZCommonFun::print_r_debug($questionData); // ZCommonFun::print_r_debug($questionData['question']); // ZCommonFun::print_r_debug($questionData['options']); if (isset($questionData['question']->question_id)) { $qustion_id = $questionData['question']->question_id; // 删除问题 if ($questionData['question']->delete()) { // 删除选项 $model_QuestionOptions = new QuestionOptions(); $condition['question_id'] = $qustion_id; // 删除选项 $model_QuestionOptions->deleteAll($condition); } } $model->is_publish = 0; $model->save(); $this->redirect(['survey/step4_2_question', 'id' => $id]); }
/** * 分数型测试问题 * @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]; } } }