/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Question::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'campaign_id' => $this->campaign_id, 'qType' => $this->qType, 'required' => $this->required, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'question', $this->question]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Question::find(); if (!isset($params['QuestionSearch']['parent_id']) || is_null($params['QuestionSearch']['parent_id'])) { $query->where(['parent_id' => null]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'voutes' => $this->voutes, 'parent_id' => $this->parent_id, 'is_active' => $this->is_active, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'position' => $this->position, 'is_multipart' => $this->is_multipart, 'is_float' => $this->is_float, 'mark' => $this->mark]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
public function actionStep() { $model = new Question(); $answers = []; $count = count(Yii::$app->request->post('Answer', [])); $codeBankCampaign = new CodeBankCampaign(); $codeBankCampaign->load(Yii::$app->request->post()); if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->validate()) { if ($count > 0) { if ($model->qType === '1') { $temp = Yii::$app->request->post('Answer', []); $count = count($temp['answer']); for ($i = 0; $i < $count; $i++) { $answer = new Answer(); $answer->scenario = 'objective'; $answer->answer = $temp['answer'][$i]; $answer->correctObjective = $temp['correctObjective'][$i]; $answer->validate(); $answers[] = $answer; } } } } return $this->render('step', ['model' => $model, 'codeBankCampaign' => $codeBankCampaign, 'answers' => $answers]); }
/** * 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 $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; }
/** * 分数型测试问题 * @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]; } } }
/** * @return \yii\db\ActiveQuery */ public function getQuestion() { return $this->hasOne(Question::className(), ['id' => 'question_id']); }
/** * Get question block * @param \common\models\Question|bool|false $question * @param int $id * @return array Data */ public static function getQuestionBlockTitle($question = false, $id = NULL) { if (isset($id)) { $question = Question::find()->where(['id' => $id])->orderBy(['created_at' => SORT_DESC])->one(); } else { if (!$question) { $question = Question::find()->where(['is_active' => 1])->orderBy(['created_at' => SORT_DESC])->one(); } } if (!isset($question)) { $question = Question::find()->where(['parent_id' => null])->orderBy(['created_at' => SORT_DESC])->one(); } $uid = isset(Yii::$app->user->id) ? Yii::$app->user->id : 0; $userVote = QuestionVote::find()->where(['question_id' => $question->id, 'user_id' => $uid])->one(); if (isset($userVote->id) || !$question->is_active) { $block = true; } else { $block = false; } $answers = Question::find()->where(['parent_id' => $question->id])->orderBy(['id' => SORT_ASC])->all(); if (!isset($id)) { if ($block) { $view = $question->is_float ? 'blocks/question_float_block_title' : 'blocks/question_block_title'; } else { $view = $question->is_float ? 'forms/question_float_form' : 'forms/question_form'; } } else { if ($block) { $view = $question->is_float ? 'blocks/question_float_block' : 'blocks/question_block'; } else { $view = $question->is_float ? 'forms/question_float_form' : 'forms/question_form'; } } $block = ['view' => '@frontend/views/' . $view, 'data' => compact('question', 'answers')]; return $block; }
/** * Url: /inquirers * @return mixed */ public function actionInquirers() { $query = Question::find()->where(['parent_id' => null])->orderBy(['created_at' => SORT_DESC]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 12]]); return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | Все опросы', 'columnFirst' => ['inquirers' => ['view' => '@frontend/views/site/inquirers', 'data' => compact('dataProvider')]], 'columnSecond' => ['short_news' => SiteBlock::getShortNews()]]); }