QuestionController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Question;
  4. use App\Http\Controllers\Controller;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Show;
  10. use App\Admin\Extensions\Tools\QuestionImport;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class QuestionController extends Controller
  13. {
  14. use HasResourceActions;
  15. /**
  16. * Index interface.
  17. *
  18. * @param Content $content
  19. * @return Content
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header('试题管理')
  25. ->description('列表')
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Show interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function show($id, Content $content)
  36. {
  37. return $content
  38. ->header('试题管理')
  39. ->description('详情')
  40. ->body($this->detail($id));
  41. }
  42. /**
  43. * Edit interface.
  44. *
  45. * @param mixed $id
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function edit($id, Content $content)
  50. {
  51. return $content
  52. ->header('试题管理')
  53. ->description('编辑')
  54. ->body($this->form()->edit($id));
  55. }
  56. /**
  57. * Create interface.
  58. *
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function create(Content $content)
  63. {
  64. return $content
  65. ->header('试题管理')
  66. ->description('创建')
  67. ->body($this->form());
  68. }
  69. /**
  70. * Make a grid builder.
  71. *
  72. * @return Grid
  73. */
  74. protected function grid()
  75. {
  76. $grid = new Grid(new Question);
  77. $grid->model()->orderBy('id', 'desc');
  78. $grid->tools(function ($tools) {
  79. $tools->append(new QuestionImport());
  80. });
  81. $grid->id('编号');
  82. $grid->name('题目名称');
  83. $grid->type('题目类型')->label();
  84. $grid->category('试题位置')->using(Question::$categoryMap);
  85. $grid->options('题目选项')->display(function ($options) {
  86. $str = '';
  87. foreach ($options as $option) {
  88. $str .= "{$option['name']}:{$option['val']}</br>";
  89. }
  90. return $str;
  91. });
  92. $grid->answers('题目答案')->label('default');
  93. $grid->mark('分值');
  94. $grid->updated_at('更新时间');
  95. $grid->actions(function (Grid\Displayers\Actions $actions) {
  96. $actions->disableView();
  97. });
  98. $grid->filter(function (Grid\Filter $filter) {
  99. $filter->disableIdFilter();
  100. $filter->like('name', '题目');
  101. $options = Question::distinct()->pluck('type', 'type');
  102. $filter->in('type', '题目类型')->multipleSelect($options);
  103. });
  104. $grid->disableExport();
  105. return $grid;
  106. }
  107. /**
  108. * Make a show builder.
  109. *
  110. * @param mixed $id
  111. * @return Show
  112. */
  113. protected function detail($id)
  114. {
  115. $show = new Show(Question::findOrFail($id));
  116. $show->id('编号');
  117. $show->name('题目名称');
  118. $show->options('题目选项');
  119. $show->answers('题目答案');
  120. $show->radio('是否多选');
  121. $show->type('类型');
  122. $show->created_at('创建时间');
  123. $show->updated_at('更新时间');
  124. return $show;
  125. }
  126. /**
  127. * Make a form builder.
  128. *
  129. * @return Form
  130. */
  131. protected function form()
  132. {
  133. $form = new Form(new Question);
  134. $form->text('name', '题目名称')->rules('required');
  135. $form->question('options', '试题');
  136. $form->select('category', '试题位置')->options(Question::$categoryMap)->default(Question::CATEGORY_TEST);
  137. // $form->textarea('options', '题目选项')->help("输入题目选项并且以换行符分开用选项用大写字母排序 A.");
  138. $form->multipleSelect('answers', '题目答案')->options(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G'])->rules('required');
  139. // $form->text('answers', '题目答案')->help("题目答案用大写字母多选题用逗号隔开如A,B");
  140. // $form->switch('radio', '是否多选');
  141. $form->text('type', '题目类型')->rules('required');
  142. $form->text('mark', '题目分值')->default(10)->rules('required');
  143. $form->textarea('analysis', '解析');
  144. $form->disableViewCheck();
  145. $form->disableEditingCheck();
  146. $form->disableCreatingCheck();
  147. return $form;
  148. }
  149. }