Grade.php 4.0 KB

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