ScoreController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Extensions\ExcelExpoter;
  4. use App\Examarg;
  5. use App\Grade;
  6. use App\School;
  7. use App\Sclass;
  8. use App\Score;
  9. use App\Http\Controllers\Controller;
  10. use App\Test;
  11. use App\User;
  12. use Encore\Admin\Controllers\HasResourceActions;
  13. use Encore\Admin\Form;
  14. use Encore\Admin\Grid;
  15. use Encore\Admin\Layout\Content;
  16. use Encore\Admin\Show;
  17. class ScoreController extends Controller
  18. {
  19. use HasResourceActions;
  20. /**
  21. * Index interface.
  22. *
  23. * @param Content $content
  24. * @return Content
  25. */
  26. public function index(Content $content)
  27. {
  28. return $content
  29. ->header('成绩管理')
  30. ->description('列表')
  31. ->body($this->grid());
  32. }
  33. /**
  34. * Show interface.
  35. *
  36. * @param mixed $id
  37. * @param Content $content
  38. * @return Content
  39. */
  40. public function show($id, Content $content)
  41. {
  42. $test = Test::where('id', $id)->first();
  43. return $content
  44. ->header('用户成绩')
  45. ->description('详情')
  46. ->body(view('question_test', compact('test')));
  47. }
  48. /**
  49. * Edit interface.
  50. *
  51. * @param mixed $id
  52. * @param Content $content
  53. * @return Content
  54. */
  55. public function edit($id, Content $content)
  56. {
  57. return $content
  58. ->header('编辑界面')
  59. ->description('编辑界面')
  60. ->body($this->form()->edit($id));
  61. }
  62. /**
  63. * Create interface.
  64. *
  65. * @param Content $content
  66. * @return Content
  67. */
  68. public function create(Content $content)
  69. {
  70. return $content
  71. ->header('创建页面')
  72. ->description('描述')
  73. ->body($this->form());
  74. }
  75. /**
  76. * Make a grid builder.
  77. *
  78. * @return Grid
  79. */
  80. protected function grid()
  81. {
  82. $grid = new Grid(new Test());
  83. $grid->id('Id');
  84. $grid->user()->name('用户');
  85. $grid->time('用时');
  86. $grid->paper()->name('试卷名');
  87. $grid->mark('成绩');
  88. $grid->school('学校');
  89. $grid->grade('年级');
  90. $grid->class('班级');
  91. $grid->result('结果')->display(function ($val) {
  92. $g = count($val['list']);
  93. return "共{$g}道题,其中正确:{$val['right']}道题,错误:{$val['error']}道试题。";
  94. });
  95. $grid->created_at('提交时间');
  96. $grid->disableCreateButton();
  97. $grid->disableRowSelector();
  98. $grid->actions(function (Grid\Displayers\Actions $actions) {
  99. // $actions->disableDelete();
  100. $actions->disableEdit();
  101. $actions->disableView();
  102. $id = $actions->getKey();
  103. $actions->prepend('<a href="/admin/score/' . $id . '">
  104. <i class="fa fa-eye"></i>
  105. </a>');
  106. });
  107. // $grid->exporter(new TestExpote());
  108. $grid->filter(function (Grid\Filter $filter) {
  109. $filter->disableIdFilter();
  110. $filter->where(function ($query) {
  111. $query->whereHas('user', function ($query) {
  112. $query->where('name', 'like', "%{$this->input}%");
  113. });
  114. }, '用户名');
  115. $filter->in('paper_id', '试卷名')->multipleSelect(Examarg::pluck('name', 'id'));
  116. $filter->between('created_at', '提交时间')->date();
  117. });
  118. $grid->disableExport();
  119. return $grid;
  120. }
  121. /**
  122. * Make a show builder.
  123. *
  124. * @param mixed $id
  125. * @return Show
  126. */
  127. protected function detail($id)
  128. {
  129. $show = new Show(Score::findOrFail($id));
  130. $show->id('编号');
  131. $show->user_id('用户')->as(function () {
  132. $name1 = User::where('id', $this->user_id)->select("name")->first();
  133. if ($name1) {
  134. return $name1['name'];
  135. }
  136. return null;
  137. });
  138. $show->school('学校信息')->as(function () {
  139. $name1 = User::where('id', $this->user_id)->select("school_id", "class_id", "grade_id")->first();
  140. if ($name1) {
  141. $school_name = School::where("id", $name1['school_id'])->select('name')->first();
  142. if ($school_name) {
  143. $school_name = $school_name['name'];
  144. }
  145. $grade = Grade::where("id", $name1['grade_id'])->select('name')->first();
  146. if ($grade) {
  147. $grade = $grade['name'];
  148. }
  149. $sclass = Sclass::where("id", $name1['sclass_id'])->select('name')->first();
  150. if ($sclass) {
  151. $sclass = $sclass['name'];
  152. }
  153. return $school_name . '-' . $grade . '-' . $sclass;
  154. }
  155. return null;
  156. });
  157. $show->examarg_id('试卷名称')->as(function () {
  158. $exam = Examarg::where('id', $this->examarg_id)->select('name')->first();
  159. if ($exam) {
  160. return $exam['name'];
  161. }
  162. return '';
  163. });
  164. $show->exam_id('考试id')->unescape()->as(function () {
  165. $question = '';
  166. foreach ($this->exam['exam'] as $item => $value) {
  167. $question = '题目:' . $value['q'] . '</br>';
  168. foreach ($value['o'] as $ke => $opt) {
  169. $question = $question . $opt['val'] . '</br>';
  170. }
  171. if ($this->grade == null) {
  172. //如果正在考试直接列出答案
  173. $question = $question . '答案' . '</br>';
  174. foreach ($value['a'] as $key => $ans) {
  175. foreach ($value['o'] as $ke => $opt) {
  176. if ($opt['num'] == $ans) {
  177. $question = $question . $opt['val'];
  178. }
  179. }
  180. }
  181. } else {
  182. $question = $question . '正确答案' . '</br>';
  183. foreach ($value['a'] as $key => $ans) {
  184. foreach ($value['o'] as $ke => $opt) {
  185. if ($opt['num'] == $ans) {
  186. $question = $question . $opt['val'];
  187. }
  188. }
  189. }
  190. $question = $question . '你的答案' . '</br>';
  191. foreach ($value['ya'] as $key => $ans) {
  192. foreach ($value['o'] as $ke => $opt) {
  193. if ($opt['num'] == $ans) {
  194. $question = $question . $opt['val'];
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return $question;
  201. });
  202. // $show->exam('Exam');
  203. $show->grade('成绩')->as(function () {
  204. if ($this->grade == null) {
  205. return "考试中";
  206. }
  207. });
  208. $show->created_at('创建时间');
  209. $show->updated_at('更新时间');
  210. return $show;
  211. }
  212. /**
  213. * Make a form builder.
  214. *
  215. * @return Form
  216. */
  217. protected function form()
  218. {
  219. $form = new Form(new Test());
  220. $form->display('id', 'id');
  221. return $form;
  222. }
  223. }