Action Create
Код: Выделить всё
public function actionCreate()
{
$model = new TestsQuestions();
$answer = [new TestsAnswers()];
if(isset(Yii::$app->request->post()['TestsQuestions']['TestsAnswers'])) {
$data = Yii::$app->request->post()['TestsQuestions']['TestsAnswers'];
foreach (array_keys($data) as $index) {
$answer[$index] = new TestsAnswers();
}
}
$model->testsAnswers = $answer;
if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($answer, Yii::$app->request->post()['TestsQuestions'])) {
foreach ($model->testsAnswers as $testsAnswer){
$testsAnswer->test_id = $model->test_id;
}
$model->save();
Yii::$app->session->setFlash('success', "Вопрос создан");
return $this->redirect('create');
}
return $this->render('create', [
'model' => $model,
'answer' => $answer,
]);
}
Код: Выделить всё
<?php
use unclead\multipleinput\MultipleInput;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use vova07\imperavi\Widget;
/* @var $this yii\web\View */
/* @var $model common\models\TestsQuestions */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="tests-questions-form">
<?php if( Yii::$app->session->hasFlash('success') ): ?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo Yii::$app->session->getFlash('success'); ?>
</div>
<?php endif;?>
<?php $form = ActiveForm::begin(); ?>
<div class="form th-question-form">
<div class="test-question-fields">
<div class="row">
<?= $form->field($model, 'question_text')->widget(Widget::className(), [
'settings' => [
'lang' => 'ru',
'minHeight' => 200,
'imageUpload' => Url::to(['/default/image-upload']),
'plugins' => [
'clips',
'fullscreen',
'imagemanager',
],
],
])->hint('Текст вопроса')->label('Текст вопроса'); ?>
</div>
<div class="row">
<?= $form->field($model, 'question_type')->dropDownList(ArrayHelper::map(\common\models\TestType::find()->all(), 'id', 'type'))->hint('Тип вопроса')->label('Тип вопроса'); ?>
</div>
<div class="row">
<?= $form->field($model, 'points_question')->textInput()->hint('Балл вопроса')->label('Балл вопроса') ?>
</div>
<div class="row">
<?= $form->field($model, 'test_id')->dropDownList(ArrayHelper::map(\common\models\Tests::find()->all(), 'id', 'name'))->hint('Тест')->label('Тест'); ?>
</div>
</div>
</div>
<h1 class="first-header">Создать ответы на вопрос</h1>
<div class="form th-answer-form">
<div class="test-answer-fields">
<div class="row">
<?= $form->field($model, 'TestsAnswers')->widget(MultipleInput::className(), [
'max' => 4,
'columns' => [
[
'name' => 'answer_text',
'type' => \vova07\imperavi\Widget::className(),
'title' => 'Answer Test',
'headerOptions' => [
// 'style' => 'width: 250px;',
'class' => 'col-lg-11'
],
'options' => [
'class' => 'input-priority'
]
],
[
'name' => 'correct',
'title' => 'Correct',
'headerOptions' => [
'class' => 'col-lg-1'
],
]
]
]); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Код: Выделить всё
<?php
namespace common\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use Yii;
/**
* This is the model class for table "tests_questions".
*
* @property int $id
* @property int|null $test_id
* @property string $question_text
* @property int|null $question_type
* @property int $points_question
* @property string|null $testAnswersJson
*
* @property TestsAnswers[] $testsAnswers
* @property TestType $questionType
* @property Tests $test
*/
class TestsQuestions extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'tests_questions';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['test_id', 'question_type', 'points_question'], 'integer'],
[['question_text', 'points_question'], 'required'],
[['testAnswersJson'], 'safe'],
[['question_text'], 'string', 'max' => 255],
[['question_type'], 'exist', 'skipOnError' => true, 'targetClass' => TestType::className(), 'targetAttribute' => ['question_type' => 'id']],
[['test_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tests::className(), 'targetAttribute' => ['test_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['testsAnswers']
],
];
}
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'test_id' => 'Test ID',
'question_text' => 'Question Text',
'question_type' => 'Question Type',
'points_question' => 'Points Question',
'testAnswersJson' => 'Test Answers Json',
];
}
/**
* Gets query for [[TestsAnswers]].
*
* @return \yii\db\ActiveQuery
*/
public function getTestsAnswers()
{
return $this->hasMany(TestsAnswers::className(), ['question_id' => 'id']);
}
/**
* Gets query for [[QuestionType]].
*
* @return \yii\db\ActiveQuery
*/
public function getQuestionType()
{
return $this->hasOne(TestType::className(), ['id' => 'question_type']);
}
/**
* Gets query for [[Test]].
*
* @return \yii\db\ActiveQuery
*/
public function getTest()
{
return $this->hasOne(Tests::className(), ['id' => 'test_id']);
}
}
Код: Выделить всё
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "tests_answers".
*
* @property int $id
* @property int|null $test_id
* @property string $answer_text
* @property int $correct
* @property int $question_id
*
* @property Tests $test
* @property TestsQuestions $question
*/
class TestsAnswers extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'tests_answers';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['test_id', 'correct', 'question_id'], 'integer'],
// [['answer_text', 'correct', 'question_id'], 'required'],
[['answer_text', 'correct'], 'required'],
[['answer_text'], 'string', 'max' => 255],
[['test_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tests::className(), 'targetAttribute' => ['test_id' => 'id']],
[['question_id'], 'exist', 'skipOnError' => true, 'targetClass' => TestsQuestions::className(), 'targetAttribute' => ['question_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'test_id' => 'Test ID',
'answer_text' => 'Answer Text',
'correct' => 'Correct',
'question_id' => 'Question ID',
];
}
/**
* Gets query for [[Test]].
*
* @return \yii\db\ActiveQuery
*/
public function getTest()
{
return $this->hasOne(Tests::className(), ['id' => 'test_id']);
}
/**
* Gets query for [[Question]].
*
* @return \yii\db\ActiveQuery
*/
public function getQuestion()
{
return $this->hasOne(TestsQuestions::className(), ['id' => 'question_id']);
}
}