Collection.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\department\Department as DepartmentModel;
  9. use app\admin\model\teacher\Task;
  10. use app\admin\model\Fault;
  11. /**
  12. * sim-考试集合管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Collection extends Backend
  17. {
  18. /**
  19. * Collection模型对象
  20. * @var \app\admin\model\teacher\Collection
  21. */
  22. protected $model = null;
  23. protected $whereExtend = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\teacher\Collection;
  28. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  29. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  30. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  31. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  32. $this->whereExtend['exam_collection_type'] = 3;
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. public function add()
  40. {
  41. if (false === $this->request->isPost()) {
  42. return $this->view->fetch();
  43. }
  44. $params = $this->request->post('row/a');
  45. if (empty($params)) {
  46. $this->error(__('Parameter %s can not be empty', ''));
  47. }
  48. if(empty($params['depart_ids'])){
  49. $this->error('请选择区队名称');
  50. }
  51. if(!empty($params['question_setting_method'])){
  52. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  53. $this->error('出题方式为考题自选,请选择考题');
  54. }
  55. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  56. $this->error('出题方式为任务自选,请选择任务');
  57. }
  58. }
  59. $params = $this->preExcludeFields($params);
  60. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  61. $params[$this->dataLimitField] = $this->auth->id;
  62. }
  63. $result = false;
  64. Db::startTrans();
  65. try {
  66. $depart_ids = $params['depart_ids'];
  67. //是否采用模型验证
  68. if ($this->modelValidate) {
  69. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  70. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  71. $this->model->validateFailException()->validate($validate);
  72. }
  73. $result = $this->model->allowField(true)->save($params);
  74. foreach(explode(',',$depart_ids) as $item){
  75. if(!empty($item)){
  76. $arr = [
  77. 'exam_collection_id' => $this->model->exam_collection_id,
  78. 'dept_id' => $item,
  79. 'update_time' => date('Y-m-d H:i:s'),
  80. ];
  81. Db::name('real_exam_collection_dept')->insert($arr);
  82. }
  83. }
  84. Db::commit();
  85. } catch (ValidateException|PDOException|Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($result === false) {
  90. $this->error(__('No rows were inserted'));
  91. }
  92. $this->success();
  93. }
  94. public function edit($ids = null)
  95. {
  96. $row = $this->model->get($ids);
  97. if (!$row) {
  98. $this->error(__('No Results were found'));
  99. }
  100. if (false === $this->request->isPost()) {
  101. $this->view->assign('row', $row);
  102. $this->assignConfig('row_info', $row);
  103. return $this->view->fetch();
  104. }
  105. $params = $this->request->post('row/a');
  106. if (empty($params)) {
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. if(!empty($params['question_setting_method'])){
  110. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  111. $this->error('出题方式为考题自选,请选择考题');
  112. }
  113. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  114. $this->error('出题方式为任务自选,请选择任务');
  115. }
  116. }
  117. $params = $this->preExcludeFields($params);
  118. $result = false;
  119. Db::startTrans();
  120. try {
  121. //是否采用模型验证
  122. if ($this->modelValidate) {
  123. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  125. $row->validateFailException()->validate($validate);
  126. }
  127. $result = $row->allowField(true)->save($params);
  128. Db::commit();
  129. } catch (ValidateException|PDOException|Exception $e) {
  130. Db::rollback();
  131. $this->error($e->getMessage());
  132. }
  133. if (false === $result) {
  134. $this->error(__('No rows were updated'));
  135. }
  136. $this->success();
  137. }
  138. public function multi($ids = null)
  139. {
  140. if (false === $this->request->isPost()) {
  141. $this->error(__('Invalid parameters'));
  142. }
  143. $ids = $ids ?: $this->request->post('ids');
  144. if (empty($ids)) {
  145. $this->error(__('Parameter %s can not be empty', 'ids'));
  146. }
  147. if (false === $this->request->has('params')) {
  148. $this->error(__('No rows were updated'));
  149. }
  150. parse_str($this->request->post('params'), $values);
  151. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  152. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  153. if (empty($values)) {
  154. $this->error(__('You have no permission'));
  155. }
  156. $adminIds = $this->getDataLimitAdminIds();
  157. if (is_array($adminIds)) {
  158. $this->model->where($this->dataLimitField, 'in', $adminIds);
  159. }
  160. if(!empty($values['exam_collection_state'])){
  161. if($values['exam_collection_state']==2){
  162. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  163. if($state_count>0){
  164. $this->error('已有启用的考试,请先关闭原来的考试');
  165. }
  166. }
  167. }
  168. $count = 0;
  169. Db::startTrans();
  170. try {
  171. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  172. foreach ($list as $item) {
  173. $count += $item->allowField(true)->isUpdate(true)->save($values);
  174. }
  175. Db::commit();
  176. } catch (PDOException|Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. if ($count) {
  181. $this->success();
  182. }
  183. $this->error(__('No rows were updated'));
  184. }
  185. //选择考题
  186. public function faults($type = null)
  187. {
  188. //设置过滤方法
  189. $this->request->filter(['strip_tags', 'trim']);
  190. if (false === $this->request->isAjax()) {
  191. $this->assignConfig('sim_type', $type);
  192. return $this->view->fetch();
  193. }
  194. //如果发送的来源是 Selectpage,则转发到 Selectpage
  195. if ($this->request->request('keyField')) {
  196. return $this->selectpage();
  197. }
  198. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  199. $where_fault = [
  200. 'sim_type' => $type,
  201. 'fault_type' => 3,
  202. 'fault_state'=>0
  203. ];
  204. $list = Fault::where($where)->where($where_fault)
  205. ->order($sort, $order)
  206. ->paginate($limit);
  207. foreach ($list as $k => $v){
  208. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  209. if(isset($exam)){
  210. $v->parent_name = $exam['name'];
  211. }else{
  212. $v->parent_name = '';
  213. }
  214. }
  215. unset($v);
  216. unset($v);
  217. $result = ['total' => $list->total(), 'rows' => $list->items()];
  218. return json($result);
  219. }
  220. //选择任务
  221. public function task($type= null)
  222. {
  223. //设置过滤方法
  224. $this->request->filter(['strip_tags', 'trim']);
  225. if (false === $this->request->isAjax()) {
  226. $this->assignConfig('sim_type', $type);
  227. return $this->view->fetch();
  228. }
  229. //如果发送的来源是 Selectpage,则转发到 Selectpage
  230. if ($this->request->request('keyField')) {
  231. return $this->selectpage();
  232. }
  233. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  234. $list = Task::where($where)->where('sim_type',$type)
  235. ->order($sort, $order)
  236. ->paginate($limit);
  237. unset($v);
  238. $result = ['total' => $list->total(), 'rows' => $list->items()];
  239. return json($result);
  240. }
  241. }