Room.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\enum\RoomMode;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 考试考场
  10. * @icon fa fa-circle-o
  11. */
  12. class Room extends Backend
  13. {
  14. /**
  15. * RoomModel模型对象
  16. * @var \app\admin\model\exam\RoomModel
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\exam\RoomModel;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("signupModeList", $this->model->getSignupModeList());
  25. $this->view->assign("isMakeupList", $this->model->getIsMakeupList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['paper', 'cate'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('paper')->visible(['title']);
  54. $row->getRelation('cate')->visible(['name']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException(true)->validate($validate);
  81. }
  82. // 验证提交数据
  83. $this->validSubmit($params);
  84. $result = $this->model->allowField(true)->save($params);
  85. Db::commit();
  86. } catch (ValidateException $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. } catch (PDOException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (\Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result !== false) {
  97. $this->success();
  98. } else {
  99. $this->error(__('No rows were inserted'));
  100. }
  101. }
  102. $this->error(__('Parameter %s can not be empty', ''));
  103. }
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds)) {
  117. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. if ($this->request->isPost()) {
  122. $params = $this->request->post("row/a");
  123. if ($params) {
  124. $params = $this->preExcludeFields($params);
  125. $result = false;
  126. Db::startTrans();
  127. try {
  128. //是否采用模型验证
  129. if ($this->modelValidate) {
  130. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  131. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  132. $row->validateFailException(true)->validate($validate);
  133. }
  134. // 验证提交数据
  135. $this->validSubmit($params);
  136. $result = $row->allowField(true)->save($params);
  137. Db::commit();
  138. } catch (ValidateException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (PDOException $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. } catch (\Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if ($result !== false) {
  149. $this->success();
  150. } else {
  151. $this->error(__('No rows were updated'));
  152. }
  153. }
  154. $this->error(__('Parameter %s can not be empty', ''));
  155. }
  156. $this->view->assign("row", $row);
  157. return $this->view->fetch();
  158. }
  159. /**
  160. * 验证提交数据
  161. * @param $params
  162. * @return void
  163. */
  164. protected function validSubmit(&$params)
  165. {
  166. $start_time = $params['start_time'] ?? '';
  167. $end_time = $params['end_time'] ?? '';
  168. if ($start_time > $end_time) {
  169. throw new \Exception('考场开始时间不能大于结束时间');
  170. }
  171. $signup_mode = $params['signup_mode'] ?? RoomMode::GENERAL;
  172. $password = $params['password'] ?? '';
  173. if ($signup_mode == RoomMode::PASSWORD && !$password) {
  174. throw new \Exception('密码模式必须填写考场密码');
  175. }
  176. $is_makeup = $params['is_makeup'] ?? 0;
  177. $makeup_count = $params['makeup_count'] ?? 0;
  178. if ($is_makeup) {
  179. if (!$makeup_count) {
  180. throw new \Exception('开启补考模式,请填写大于0的补考次数');
  181. }
  182. } else {
  183. // 不补考,补考次数默认0
  184. $params['makeup_count'] = 0;
  185. }
  186. }
  187. }