Skip to content

Commit

Permalink
添加题目检索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yang committed Mar 2, 2019
1 parent 354fee3 commit 293eabf
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ JNOJ Change Log
SPJ 的参数输入顺序修改为输入文件、选手输出、标准答案。**为保证SPJ题目的准确性,更新后需要你改写题目的SPJ**。具体参考OJ 的 Wiki。示例区别在于:
旧版本SPJ写法示例为`FILE * f_in = fopen(args[1],"r"); FILE * f_outr = fopen(args[2],"r");FILE * f_use = fopen(args[3],"r");`
新版本SPJ写法示例为`FILE * f_in = fopen(args[1],"r"); FILE * f_user = fopen(args[2],"r");FILE * f_out = fopen(args[3],"r");`
- Enh: [web] 添加题目检索功能

0.7.0 2019.2.1
------------------------
Expand Down
4 changes: 1 addition & 3 deletions models/ProblemSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Problem;

/**
* ProblemSearch represents the model behind the search form of `app\models\Problem`.
Expand Down Expand Up @@ -41,7 +39,7 @@ public function scenarios()
*/
public function search($params)
{
$query = Problem::find()->where(['status' => Problem::STATUS_VISIBLE]);
$query = Problem::find()->orderBy(['id' => SORT_DESC])->with('user');

// add conditions that should always apply here

Expand Down
2 changes: 1 addition & 1 deletion models/SolutionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function search($params, $contest_id = NULL)
->from('{{%user}}')
->andWhere('nickname=:name', [':name' => $this->username])
->orWhere('username=:name', [':name' => $this->username])
->column();
->scalar();
}
// grid filtering conditions
$query->andFilterWhere([
Expand Down
10 changes: 4 additions & 6 deletions modules/admin/controllers/ProblemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace app\modules\admin\controllers;

use app\models\ContestProblem;
use app\models\ProblemSearch;
use Yii;
use yii\data\ActiveDataProvider;
use yii\db\Query;
Expand Down Expand Up @@ -61,12 +62,8 @@ public function behaviors()
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Problem::find()->orderBy(['id' => SORT_DESC])->with('user'),
'pagination' => [
'pageSize' => 50
]
]);
$searchModel = new ProblemSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

if (Yii::$app->request->isPost) {
$keys = Yii::$app->request->post('keylist');
Expand All @@ -81,6 +78,7 @@ public function actionIndex()

return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel
]);
}

Expand Down
41 changes: 41 additions & 0 deletions modules/admin/views/problem/_search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\ProblemSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="problem-search">

<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'class' => 'form-inline',
'data-pjax' => 1
],
]); ?>

<?= $form->field($model, 'id', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class='glyphicon glyphicon-sunglasses'></span> pid</span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => 'Problem ID'])->label(false) ?>

<?= $form->field($model, 'title', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\">标题</span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => Yii::t('app', 'Title')])->label(false) ?>

<?= $form->field($model, 'source', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\">来源</span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => Yii::t('app', 'Source')])->label(false) ?>

<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
2 changes: 2 additions & 0 deletions modules/admin/views/problem/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use yii\bootstrap\Modal;

/* @var $this yii\web\View */
/* @var $searchModel app\models\ProblemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Problems');
Expand All @@ -31,6 +32,7 @@
设为私有
</a>
</p>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'options' => ['id' => 'grid'],
Expand Down
15 changes: 6 additions & 9 deletions modules/polygon/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace app\modules\polygon\controllers;

use app\models\User;
use Yii;
use yii\web\Controller;
use app\modules\polygon\models\Problem;
use yii\data\ActiveDataProvider;
use app\modules\polygon\models\Problem;
use app\models\User;
use app\modules\polygon\models\ProblemSearch;

/**
* Default controller for the `polygon` module
Expand All @@ -19,16 +20,12 @@ class DefaultController extends Controller
*/
public function actionIndex()
{
$query = Problem::find()->with('user')->orderBy(['id' => SORT_DESC]);
if (Yii::$app->user->isGuest || Yii::$app->user->identity->role != User::ROLE_ADMIN) {
$query->andWhere(['created_by' => Yii::$app->user->id]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$searchModel = new ProblemSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel
]);
}
}
11 changes: 4 additions & 7 deletions modules/polygon/controllers/ProblemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use app\models\Solution;
use app\modules\polygon\models\PolygonStatus;
use app\modules\polygon\models\ProblemSearch;
use Yii;
use app\models\User;
use app\modules\polygon\models\Problem;
Expand Down Expand Up @@ -53,16 +54,12 @@ public function behaviors()
public function actionIndex()
{
$this->layout = '/main';
$query = Problem::find()->with('user')->orderBy(['id' => SORT_DESC]);
if (Yii::$app->user->identity->role != User::ROLE_ADMIN) {
$query->andWhere(['created_by' => Yii::$app->user->id]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$searchModel = new ProblemSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel
]);
}

Expand Down
34 changes: 34 additions & 0 deletions modules/polygon/models/ProblemQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace app\modules\polygon\models;

/**
* This is the ActiveQuery class for [[Problem]].
*
* @see PolygonProblem
*/
class ProblemQuery extends \yii\db\ActiveQuery
{
/*public function active()
{
return $this->andWhere('[[status]]=1');
}*/

/**
* {@inheritdoc}
* @return Problem[]|array
*/
public function all($db = null)
{
return parent::all($db);
}

/**
* {@inheritdoc}
* @return Problem|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
105 changes: 105 additions & 0 deletions modules/polygon/models/ProblemSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace app\modules\polygon\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use app\modules\polygon\models\Problem;
use app\models\User;

/**
* ProblemSearch represents the model behind the search form of `app\modules\polygon\models\Problem`.
*/
class ProblemSearch extends Problem
{
public $username;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'spj', 'spj_lang', 'time_limit', 'memory_limit', 'status', 'accepted', 'submit', 'solved', 'solution_lang'], 'integer'],
[['title', 'description', 'input', 'output', 'sample_input', 'sample_output', 'spj_source', 'hint',
'source', 'tags', 'solution_source', 'created_at', 'updated_at', 'created_by', 'username'], '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 = Problem::find()->with('user')->orderBy(['id' => SORT_DESC]);

if (Yii::$app->user->isGuest || Yii::$app->user->identity->role != User::ROLE_ADMIN) {
$query->andWhere(['created_by' => Yii::$app->user->id]);
}

$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;
}

if (!empty($this->username)) {
$this->created_by = (new Query())->select('id')
->from('{{%user}}')
->andWhere('nickname=:name', [':name' => $this->username])
->orWhere('username=:name', [':name' => $this->username])
->scalar();
}

// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'spj' => $this->spj,
'spj_lang' => $this->spj_lang,
'time_limit' => $this->time_limit,
'memory_limit' => $this->memory_limit,
'status' => $this->status,
'accepted' => $this->accepted,
'submit' => $this->submit,
'solved' => $this->solved,
'solution_lang' => $this->solution_lang,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'created_by' => $this->created_by,
]);

$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'input', $this->input])
->andFilterWhere(['like', 'output', $this->output])
->andFilterWhere(['like', 'sample_input', $this->sample_input])
->andFilterWhere(['like', 'sample_output', $this->sample_output])
->andFilterWhere(['like', 'spj_source', $this->spj_source])
->andFilterWhere(['like', 'hint', $this->hint])
->andFilterWhere(['like', 'source', $this->source])
->andFilterWhere(['like', 'tags', $this->tags])
->andFilterWhere(['like', 'solution_source', $this->solution_source]);

return $dataProvider;
}
}
3 changes: 3 additions & 0 deletions modules/polygon/views/default/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $searchModel app\modules\polygon\models\ProblemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Polygon System');
Expand All @@ -29,6 +30,8 @@
<?= Html::a(Yii::t('app', 'Create Problem'), ['/polygon/problem/create'], ['class' => 'btn btn-success']) ?>
</p>

<?php echo $this->render('/problem/_search', ['model' => $searchModel]); ?>

<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
Expand Down
42 changes: 42 additions & 0 deletions modules/polygon/views/problem/_search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\modules\polygon\models\ProblemSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="problem-search">

<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'class' => 'form-inline',
'data-pjax' => 1
],
]); ?>

<?= $form->field($model, 'id', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class='glyphicon glyphicon-sunglasses'></span></span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => 'ID'])->label(false) ?>

<?= $form->field($model, 'title', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class='glyphicon glyphicon-text-size'></span></span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => Yii::t('app', 'Title')])->label(false) ?>

<?= $form->field($model, 'username', [
'template' => "{label}\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class='glyphicon glyphicon-user'></span></span>{input}</div>",
])->textInput(['maxlength' => 128, 'autocomplete'=>'off', 'placeholder' => Yii::t('app', 'Who')])->label(false) ?>


<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
Loading

0 comments on commit 293eabf

Please sign in to comment.