Paper.php 9.8 KB

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