Grade.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\enum\PaperMode;
  4. use addons\exam\model\QuestionModel;
  5. use app\common\controller\Backend;
  6. /**
  7. * 考试成绩
  8. * @icon fa fa-circle-o
  9. */
  10. class Grade extends Backend
  11. {
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * GradeModel模型对象
  15. * @var \app\admin\model\exam\GradeModel
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\exam\GradeModel;
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $list = $this->model
  44. ->with(['cate', 'paper', 'user'])
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. foreach ($list as $row) {
  49. $row->getRelation('cate')->visible(['name']);
  50. $row->getRelation('paper')->visible(['title']);
  51. }
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 详情
  59. */
  60. public function detail($ids = null)
  61. {
  62. $row = $this->getDetail($ids);
  63. if (!$row) {
  64. $this->error(__('No Results were found'));
  65. }
  66. $adminIds = $this->getDataLimitAdminIds();
  67. if (is_array($adminIds)) {
  68. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  69. $this->error(__('You have no permission'));
  70. }
  71. }
  72. $this->view->assign("row", $row);
  73. $this->view->engine->layout(false);
  74. return $this->view->fetch('detailnew');
  75. }
  76. /**
  77. * 详情
  78. */
  79. public function one($ids = null)
  80. {
  81. $row = $this->getDetail($ids);
  82. return json($row, 1);
  83. }
  84. /**
  85. * 获取并处理详情数据
  86. * @param $ids
  87. * @return \app\admin\model\exam\GradeModel|array
  88. */
  89. protected function getDetail($ids)
  90. {
  91. $row = $this->model->get($ids, ['user', 'paper', 'cate']);
  92. if (
  93. (!isset($row['question_ids']) || !$row['question_ids']) ||
  94. (!isset($row['user_answers']) || !$row['user_answers']) ||
  95. (!isset($row['configs']) || !$row['configs'])
  96. ) {
  97. $this->error('答卷数据缺少,无法查看(可能是旧版本未记录数据导致)');
  98. }
  99. if ($row['mode'] == PaperMode::FIX) {
  100. $row['questions'] = QuestionModel::getFixListByPaper($row['paper_id'], ['materialQuestions.question']);
  101. // 合并材料题子题目
  102. $row['questions'] = QuestionModel::mergeMaterialQuestions($row['questions']);
  103. } else {
  104. $row['questions'] = QuestionModel::whereIn('id', $row['question_ids'])->orderRaw("find_in_set(id, '" . $row['question_ids'] . "')")->select();
  105. }
  106. $row['user_answers'] = json_decode($row['user_answers'], true);
  107. $row['configs'] = json_decode($row['configs'], true);
  108. $row['finishtime_text'] = date('Y-m-d H:i:s', $row['finish_time']);
  109. return $row;
  110. }
  111. }