123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace App\Admin\Controllers;
- use App\Question;
- use App\Http\Controllers\Controller;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- use App\Admin\Extensions\Tools\QuestionImport;
- use Maatwebsite\Excel\Facades\Excel;
- class QuestionController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header('试题管理')
- ->description('列表')
- ->body($this->grid());
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header('试题管理')
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header('试题管理')
- ->description('编辑')
- ->body($this->form()->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header('试题管理')
- ->description('创建')
- ->body($this->form());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Question);
- $grid->model()->orderBy('id', 'desc');
- $grid->tools(function ($tools) {
- $tools->append(new QuestionImport());
- });
- $grid->id('编号');
- $grid->name('题目名称');
- $grid->type('题目类型')->label();
- $grid->category('试题位置')->using(Question::$categoryMap);
- $grid->options('题目选项')->display(function ($options) {
- $str = '';
- foreach ($options as $option) {
- $str .= "{$option['name']}:{$option['val']}</br>";
- }
- return $str;
- });
- $grid->answers('题目答案')->label('default');
- $grid->mark('分值');
- $grid->updated_at('更新时间');
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableView();
- });
- $grid->filter(function (Grid\Filter $filter) {
- $filter->disableIdFilter();
- $filter->like('name', '题目');
- $options = Question::distinct()->pluck('type', 'type');
- $filter->in('type', '题目类型')->multipleSelect($options);
- });
- $grid->disableExport();
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(Question::findOrFail($id));
- $show->id('编号');
- $show->name('题目名称');
- $show->options('题目选项');
- $show->answers('题目答案');
- $show->radio('是否多选');
- $show->type('类型');
- $show->created_at('创建时间');
- $show->updated_at('更新时间');
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new Question);
- $form->text('name', '题目名称')->rules('required');
- $form->question('options', '试题');
- $form->select('category', '试题位置')->options(Question::$categoryMap)->default(Question::CATEGORY_TEST);
- // $form->textarea('options', '题目选项')->help("输入题目选项并且以换行符分开用选项用大写字母排序 A.");
- $form->multipleSelect('answers', '题目答案')->options(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G'])->rules('required');
- // $form->text('answers', '题目答案')->help("题目答案用大写字母多选题用逗号隔开如A,B");
- // $form->switch('radio', '是否多选');
- $form->text('type', '题目类型')->rules('required');
- $form->text('mark', '题目分值')->default(10)->rules('required');
- $form->textarea('analysis', '解析');
- $form->disableViewCheck();
- $form->disableEditingCheck();
- $form->disableCreatingCheck();
- return $form;
- }
- }
|