/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = QuestionList::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]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
public function getQuestionList() { return $this->hasOne(QuestionList::className(), ['list_id' => 'id']); }
/** * Updates an existing Question model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @param integer $list_id * @return mixed */ public function actionUpdate($id, $list_id) { // Проверяем можем ли изменить вопрос. if (!$this->canCreateQuestion($list_id)) { return $this->render('question_list_is_lock', ['modelQuestionList' => QuestionList::findOne($list_id), 'action' => 'update']); } $model = $this->findModel($id); $post = Yii::$app->request->post(); //$model->answerVariants //если данные загрузились в объект вопроса и он валиден if ($model->load($post) && ($valid = $model->validate())) { $transaction = \Yii::$app->db->beginTransaction(); // Если сохранили успешно объек вопроса if ($model->save()) { //Если для этого типа нужно сохранять опции switch ($model->type) { case 'select_one': case 'select_multiple': case 'radio': $modelsAnswerVariants = []; $deletedAnswerVariantsIDs = array_values(ArrayHelper::map($model->answerVariants, 'id', 'id')); if ($post['AnswerVariant']) { foreach ($post['AnswerVariant'] as $av) { $m = new AnswerVariant(); $m->id = $av['id']; $modelsAnswerVariants[] = $m; } } Model::loadMultiple($modelsAnswerVariants, $post); if (!empty($deletedAnswerVariantsIDs)) { AnswerVariant::deleteAll(['id' => $deletedAnswerVariantsIDs]); } foreach ($modelsAnswerVariants as $answerVariantModel) { $answerVariantModel->question_id = $model->id; $valid = $answerVariantModel->save(); } if ($valid) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id, 'list_id' => $list_id]); } else { $transaction->rollBack(); return $this->render('update', ['model' => $model, 'list_id' => $list_id]); } break; case 'checkbox': $answerVariantModel = $model->answerVariants[0]; // если не переданы кол-во балов при установке чек-бокса if (!(int) $post['checkboxUpScores']) { //но они были ранее if ($answerVariantModel) { $answerVariantModel->delete(); } break; } if (!$answerVariantModel) { $answerVariantModel = new AnswerVariant(); $answerVariantModel->answer = '1'; $answerVariantModel->question_id = $model->id; } //а если переданы, то заменяем $answerVariantModel->scores = (int) $post['checkboxUpScores']; $valid = $answerVariantModel->save(); break; default: //На тот случай, если сменили тип поля с ВЫБОР ИЗ ВАРИАНТОВ на ТЕКСТ, удаляем варианты ответа AnswerVariant::deleteAll(['id' => array_values(ArrayHelper::map($model->answerVariants, 'id', 'id'))]); } } else { $transaction->rollBack(); } if ($valid) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id, 'list_id' => $list_id]); } else { $transaction->rollBack(); return $this->render('create', ['model' => $model, 'list_id' => $list_id]); } } else { return $this->render('update', ['model' => $model, 'list_id' => $list_id]); } }
/** * Finds the QuestionList model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return QuestionList the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = QuestionList::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing AnswerList model. * For ajax request will return json object * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $request = Yii::$app->request; $model = $this->findModelAnswerList($id); if ($model->status != 'clear') { if ($request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ['title' => "Обновить невозможно", 'content' => $this->renderAjax('view', ['modelAnswerList' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])]; } else { return $this->render('view', ['modelAnswerList' => $model]); } } $modelsQuestionList = QuestionList::find()->all(); $questionLists = ArrayHelper::map($modelsQuestionList, 'id', 'title'); $statusList = $model->statusList; $modelsOffice = Office::find()->all(); $DoList = ArrayHelper::map($modelsOffice, 'id', 'name'); if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; if ($request->isGet) { return ['title' => "Изменение опроса №" . $id, 'content' => $this->renderAjax('update', ['model' => $model, 'questionLists' => $questionLists, 'DoList' => $DoList, 'statusList' => $statusList]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } else { if ($model->load($request->post()) && $model->save()) { return ['forceReload' => '#crud-datatable-pjax', 'title' => "Опросный лист №" . $id, 'content' => $this->renderAjax('view', ['modelAnswerList' => $model]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])]; } else { return ['title' => "Изменение опроса №" . $id, 'content' => $this->renderAjax('update', ['model' => $model, 'questionLists' => $questionLists, 'DoList' => $DoList, 'statusList' => $statusList]), 'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])]; } } } else { /* * Process for non-ajax request */ if ($model->load($request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'questionLists' => $questionLists, 'DoList' => $DoList, 'statusList' => $statusList]); } } }