add module test task

This commit is contained in:
iironside 2022-03-09 19:10:44 +03:00
parent 7874fc3447
commit c4d35ace86
13 changed files with 575 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace backend\modules\test;
/**
* test module definition class
*/
class Test extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'backend\modules\test\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\test\controllers;
use Yii;
use backend\modules\test\models\TestTask;
use backend\modules\test\models\TestTaskSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* TestTaskController implements the CRUD actions for TestTask model.
*/
class TestTaskController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all TestTask models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new TestTaskSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single TestTask model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new TestTask model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new TestTask();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing TestTask model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing TestTask model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the TestTask model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return TestTask the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = TestTask::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace backend\modules\test\models;
class TestTask extends \common\models\TestTask
{
}

View File

@ -0,0 +1,71 @@
<?php
namespace backend\modules\test\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\test\models\TestTask;
/**
* TestTaskSearch represents the model behind the search form of `backend\modules\test\models\TestTask`.
*/
class TestTaskSearch extends TestTask
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'level', 'status'], 'integer'],
[['description', 'link'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = TestTask::find();
// add conditions that should always apply here
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'level' => $this->level,
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'link', $this->link]);
return $dataProvider;
}
}

View File

@ -0,0 +1,38 @@
<?php
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\test\models\TestTask */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="test-task-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'description')->textarea(['rows' => 4]) ?>
<?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'level')->dropDownList(
backend\modules\test\models\TestTask::getLevelList(),
['prompt' => '...']
) ?>
<?= $form->field($model, 'status')->dropDownList(
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]
) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,35 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\test\models\TestTaskSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="test-task-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'description') ?>
<?= $form->field($model, 'link') ?>
<?= $form->field($model, 'level') ?>
<?= $form->field($model, 'status') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,18 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\test\models\TestTask */
$this->title = 'Добавить тестовое задание';
$this->params['breadcrumbs'][] = ['label' => 'Test Tasks', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="test-task-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,57 @@
<?php
use backend\modules\test\models\TestTask;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\test\models\TestTaskSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Тестовые задания';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="test-task-index">
<p>
<?= Html::a('Создать', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'description',
[
'attribute' => 'link',
'value' => function ($model) {
return Html::a(Html::encode($model->link), Url::to($model->link));
},
'format' => 'raw',
],
[
'attribute' => 'level',
'format' => 'raw',
'filter' => TestTask::getLevelList(),
'value' => function($model){
return TestTask::getLevelLabel($model->level);
}
],
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function($model){
return StatusHelper::statusLabel($model->status);
}
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\test\models\TestTask */
$this->title = 'Изменить тестовое задание';
$this->params['breadcrumbs'][] = ['label' => 'Test Tasks', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="test-task-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,70 @@
<?php
use backend\modules\test\models\TestTask;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\test\models\TestTask */
$this->title = cut_title($model->description);
$this->params['breadcrumbs'][] = ['label' => 'Test Tasks', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
function cut_title($str)
{
if(strlen($str) > 35){
return mb_substr($str, 0, 35, 'UTF-8') . '...';
}
return $str;
}
?>
<div class="test-task-view">
<p>
<?= Html::a('Список', ['index', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'description',
[
'attribute' => 'link',
'value' => function ($model) {
return Html::a(Html::encode($model->link), Url::to($model->link));
},
'format' => 'raw',
],
[
'attribute' => 'level',
'format' => 'raw',
'filter' => TestTask::getLevelList(),
'value' => function($model){
return TestTask::getLevelLabel($model->level);
}
],
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function($model){
return StatusHelper::statusLabel($model->status);
}
],
],
]) ?>
</div>

View File

@ -110,6 +110,8 @@
],
'visible' => Yii::$app->user->can('confidential_information')
],
['label' => 'Тестовые задания', 'icon' => 'file-text-o', 'url' => ['/test/test-task'], 'active' => \Yii::$app->controller->id == 'options', 'visible' => Yii::$app->user->can('confidential_information')],
/*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']],
['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']],

View File

@ -0,0 +1,75 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "test_task".
*
* @property int $id
* @property string $description
* @property string $link
* @property int $level
* @property int $status
*/
class TestTask extends \yii\db\ActiveRecord
{
const LEVEL_JUNIOR = 1;
const LEVEL_MIDDLE = 2;
const LEVEL_MIDDLE_PLUS = 3;
const LEVEL_SENIOR = 4;
/**
* @return string[]
*/
public static function getLevelList(): array
{
return [
self::LEVEL_JUNIOR => 'Junior',
self::LEVEL_MIDDLE => 'Middle',
self::LEVEL_MIDDLE_PLUS => 'Middle+',
self::LEVEL_SENIOR => 'Senior',
];
}
public static function getLevelLabel(int $level): string
{
return self::getLevelList()[$level];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'test_task';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['level', 'status', 'description', 'link'], 'required'],
[['level', 'status'], 'integer'],
[['description'], 'string', 'max' => 500],
[['link'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'description' => 'Описание',
'link' => 'Ссылка',
'level' => 'Уровень',
'status' => 'Статус',
];
}
}

View File

@ -0,0 +1,31 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%test_task}}`.
*/
class m220309_134047_create_test_task_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%test_task}}', [
'id' => $this->primaryKey(),
'description' => $this->string(500),
'link' => $this->string(255),
'level' => $this->integer(1)->defaultValue(1),
'status' => $this->integer(1),
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%test_task}}');
}
}