Paper.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\enum\PaperMode;
  4. use app\admin\model\exam\PaperQuestionModel;
  5. use app\admin\model\exam\QuestionModel;
  6. use app\common\controller\Backend;
  7. use think\Db;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 试卷
  12. * @icon fa fa-circle-o
  13. */
  14. class Paper extends Backend
  15. {
  16. /**
  17. * PaperModel模型对象
  18. * @var \app\admin\model\exam\PaperModel
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\exam\PaperModel;
  25. $this->view->assign("modeList", $this->model->getModeList());
  26. $this->view->assign("kindList", $this->model->getKindList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = true;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['cate'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('cate')->visible(['name']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params = $this->preExcludeFields($params);
  71. $this->valid($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. $result = false;
  76. Db::startTrans();
  77. try {
  78. //是否采用模型验证
  79. if ($this->modelValidate) {
  80. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  81. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  82. $this->model->validateFailException(true)->validate($validate);
  83. }
  84. // dd($params);
  85. $result = $this->model->allowField(true)->save($params);
  86. // 保存试卷固定题目
  87. $this->saveFixQuestion($this->model, $params);
  88. Db::commit();
  89. } catch (ValidateException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (PDOException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (\Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result !== false) {
  100. $this->success();
  101. } else {
  102. $this->error(__('No rows were inserted'));
  103. }
  104. }
  105. $this->error(__('Parameter %s can not be empty', ''));
  106. }
  107. return $this->view->fetch();
  108. }
  109. /**
  110. * 编辑
  111. */
  112. public function edit($ids = null)
  113. {
  114. $row = $this->model->get($ids);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $adminIds = $this->getDataLimitAdminIds();
  119. if (is_array($adminIds)) {
  120. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  121. $this->error(__('You have no permission'));
  122. }
  123. }
  124. if ($this->request->isPost()) {
  125. $params = $this->request->post("row/a");
  126. if ($params) {
  127. $this->valid($params);
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. //是否采用模型验证
  133. if ($this->modelValidate) {
  134. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  135. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  136. $row->validateFailException(true)->validate($validate);
  137. }
  138. $result = $row->allowField(true)->save($params);
  139. // 保存试卷固定题目
  140. $this->saveFixQuestion($row, $params, 'edit');
  141. Db::commit();
  142. } catch (ValidateException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (PDOException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (\Exception $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. }
  152. if ($result !== false) {
  153. $this->success();
  154. } else {
  155. $this->error(__('No rows were updated'));
  156. }
  157. }
  158. $this->error(__('Parameter %s can not be empty', ''));
  159. }
  160. if ($row['mode'] == PaperMode::FIX) {
  161. $row['questions'] = QuestionModel::getFixListByPaper($row['id'], ['cates']);
  162. }
  163. $this->view->assign("row", $row);
  164. $this->view->assign("configs", json_decode($row['configs'], true));
  165. return $this->view->fetch();
  166. }
  167. /**
  168. * 验证参数
  169. * @param $params
  170. * @return void
  171. */
  172. protected function valid(&$params)
  173. {
  174. if ($params['pass_score'] > $params['total_score']) {
  175. $this->error('及格分数不能大于总分');
  176. }
  177. $params['start_time'] = $params['start_time'] ?: 0;
  178. $params['end_time'] = $params['end_time'] ?: 0;
  179. if ($params['start_time']) {
  180. if ($params['start_time'] > $params['end_time']) {
  181. $this->error('开始时间不能大于结束时间');
  182. }
  183. // if (strtotime($params['start_time']) < time()) {
  184. // $this->error('开始时间不能小于当前时间');
  185. // }
  186. }
  187. if ($params['end_time']) {
  188. if (!$params['start_time']) {
  189. $this->error('请先选择开始时间');
  190. }
  191. }
  192. // 固定选题模式
  193. if ($params['mode'] == 'FIX') {
  194. $params['questions'] = json_decode($params['questions'], true);
  195. if (!$params['questions']) {
  196. $this->error('请先选择题目');
  197. }
  198. if (count($params['questions']) < $params['quantity']) {
  199. $this->error('题目数量不能大于题目总数');
  200. }
  201. }
  202. $limit_time = 0;
  203. if ($params['limit_time_hour']) {
  204. $limit_time += $params['limit_time_hour'] * 60 * 60;
  205. }
  206. if ($params['limit_time_minute']) {
  207. $limit_time += $params['limit_time_minute'] * 60;
  208. }
  209. $params['limit_time'] = $limit_time;
  210. }
  211. /**
  212. * 保存固定选题
  213. * @param $paper
  214. * @param $params
  215. * @return void
  216. */
  217. public function saveFixQuestion($paper, $params, $method = 'add')
  218. {
  219. if ($paper['mode'] != 'FIX') {
  220. return;
  221. }
  222. if ($method == 'edit') {
  223. PaperQuestionModel::where('paper_id', $paper['id'])->delete();
  224. }
  225. $questions = $params['questions'];
  226. $data = [];
  227. foreach ($questions as $key => $question) {
  228. // $item = [
  229. // 'paper_id' => $paper['id'],
  230. // 'question_id' => $question['id'],
  231. // 'score' => $question['score'],
  232. // 'sort' => $key + 1,
  233. // 'createtime' => time(),
  234. // ];
  235. //
  236. // if ($question['kind'] == 'SHORT') {
  237. // $item['answer'] = $question['answer'];
  238. // }
  239. // $data[] = $item;
  240. $data[] = [
  241. 'paper_id' => $paper['id'],
  242. 'question_id' => $question['id'],
  243. 'score' => $question['score'],
  244. 'answer_config' => is_array($question['answer']) ? json_encode($question['answer'], JSON_UNESCAPED_UNICODE) : $question['answer'],
  245. 'sort' => $key + 1,
  246. 'createtime' => time(),
  247. ];
  248. }
  249. (new PaperQuestionModel())->saveAll($data);
  250. }
  251. }