Grade.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ->where('grade_model.status',2)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. foreach ($list as $row) {
  50. $row->getRelation('cate')->visible(['name']);
  51. $row->getRelation('paper')->visible(['title']);
  52. }
  53. $result = array("total" => $list->total(), "rows" => $list->items());
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 详情
  60. */
  61. public function detail($ids = null)
  62. {
  63. $row = $this->getDetail($ids);
  64. if (!$row) {
  65. $this->error(__('No Results were found'));
  66. }
  67. $adminIds = $this->getDataLimitAdminIds();
  68. if (is_array($adminIds)) {
  69. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  70. $this->error(__('You have no permission'));
  71. }
  72. }
  73. $this->view->assign("row", $row);
  74. $this->view->engine->layout(false);
  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. * @param $ids
  88. * @return \app\admin\model\exam\GradeModel|array
  89. */
  90. protected function getDetail($ids)
  91. {
  92. $row = $this->model->get($ids, ['user', 'paper', 'cate']);
  93. if (
  94. (!isset($row['question_ids']) || !$row['question_ids']) ||
  95. (!isset($row['user_answers']) || !$row['user_answers']) ||
  96. (!isset($row['configs']) || !$row['configs'])
  97. ) {
  98. $this->error('答卷数据缺少,无法查看(可能是旧版本未记录数据导致)');
  99. }
  100. if ($row['mode'] == PaperMode::FIX) {
  101. $row['questions'] = QuestionModel::getFixListByPaper($row['paper_id'], ['materialQuestions.question']);
  102. // 合并材料题子题目
  103. $row['questions'] = QuestionModel::mergeMaterialQuestions($row['questions']);
  104. } else {
  105. $row['questions'] = QuestionModel::whereIn('id', $row['question_ids'])->orderRaw("find_in_set(id, '" . $row['question_ids'] . "')")->select();
  106. }
  107. $row['user_answers'] = json_decode($row['user_answers'], true);
  108. $row['configs'] = json_decode($row['configs'], true);
  109. $row['finishtime_text'] = date('Y-m-d H:i:s', $row['finish_time']);
  110. return $row;
  111. }
  112. }