Collection.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $result = false;
  61. Db::startTrans();
  62. try {
  63. $depart_ids = $params['depart_ids'];
  64. $result = $this->model->allowField(true)->save($params);
  65. foreach(explode(',',$depart_ids) as $item){
  66. if(!empty($item)){
  67. $arr = [
  68. 'exam_collection_id' => $this->model->exam_collection_id,
  69. 'dept_id' => $item,
  70. 'update_time' => date('Y-m-d H:i:s'),
  71. ];
  72. Db::name('real_exam_collection_dept')->insert($arr);
  73. }
  74. }
  75. //匹配的学员自动加入 mx_real_exam 考试表 总分
  76. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  77. Db::commit();
  78. } catch (ValidateException|PDOException|Exception $e) {
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. }
  82. if ($result === false) {
  83. $this->error(__('No rows were inserted'));
  84. }
  85. $this->success();
  86. }
  87. public function edit($ids = null)
  88. {
  89. $row = $this->model->get($ids);
  90. if (!$row) {
  91. $this->error(__('No Results were found'));
  92. }
  93. if (false === $this->request->isPost()) {
  94. $this->view->assign('row', $row);
  95. $this->assignConfig('row_info', $row);
  96. return $this->view->fetch();
  97. }
  98. $params = $this->request->post('row/a');
  99. if (empty($params)) {
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. if(!empty($params['question_setting_method'])){
  103. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  104. $this->error('出题方式为考题自选,请选择考题');
  105. }
  106. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  107. $this->error('出题方式为任务自选,请选择任务');
  108. }
  109. }
  110. $params = $this->preExcludeFields($params);
  111. $result = false;
  112. Db::startTrans();
  113. try {
  114. //是否采用模型验证
  115. if ($this->modelValidate) {
  116. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  117. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  118. $row->validateFailException()->validate($validate);
  119. }
  120. $result = $row->allowField(true)->save($params);
  121. Db::commit();
  122. } catch (ValidateException|PDOException|Exception $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. }
  126. if (false === $result) {
  127. $this->error(__('No rows were updated'));
  128. }
  129. $this->success();
  130. }
  131. public function multi($ids = null)
  132. {
  133. if (false === $this->request->isPost()) {
  134. $this->error(__('Invalid parameters'));
  135. }
  136. $ids = $ids ?: $this->request->post('ids');
  137. if (empty($ids)) {
  138. $this->error(__('Parameter %s can not be empty', 'ids'));
  139. }
  140. if (false === $this->request->has('params')) {
  141. $this->error(__('No rows were updated'));
  142. }
  143. parse_str($this->request->post('params'), $values);
  144. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  145. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  146. if (empty($values)) {
  147. $this->error(__('You have no permission'));
  148. }
  149. $adminIds = $this->getDataLimitAdminIds();
  150. if (is_array($adminIds)) {
  151. $this->model->where($this->dataLimitField, 'in', $adminIds);
  152. }
  153. if(!empty($values['exam_collection_state'])){
  154. if($values['exam_collection_state']==2){
  155. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  156. if($state_count>0){
  157. $this->error('已有启用的考试,请先关闭原来的考试');
  158. }
  159. }
  160. }
  161. $count = 0;
  162. Db::startTrans();
  163. try {
  164. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  165. foreach ($list as $item) {
  166. $count += $item->allowField(true)->isUpdate(true)->save($values);
  167. }
  168. Db::commit();
  169. } catch (PDOException|Exception $e) {
  170. Db::rollback();
  171. $this->error($e->getMessage());
  172. }
  173. if ($count) {
  174. $this->success();
  175. }
  176. $this->error(__('No rows were updated'));
  177. }
  178. //选择考题
  179. public function faults($type = null)
  180. {
  181. //设置过滤方法
  182. $this->request->filter(['strip_tags', 'trim']);
  183. if (false === $this->request->isAjax()) {
  184. $this->assignConfig('sim_type', $type);
  185. return $this->view->fetch();
  186. }
  187. //如果发送的来源是 Selectpage,则转发到 Selectpage
  188. if ($this->request->request('keyField')) {
  189. return $this->selectpage();
  190. }
  191. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  192. $where_fault = [
  193. 'sim_type' => $type,
  194. 'fault_type' => 3,
  195. 'fault_state'=>0
  196. ];
  197. $list = Fault::where($where)->where($where_fault)
  198. ->order($sort, $order)
  199. ->paginate($limit);
  200. foreach ($list as $k => $v){
  201. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  202. if(isset($exam)){
  203. $v->parent_name = $exam['name'];
  204. }else{
  205. $v->parent_name = '';
  206. }
  207. }
  208. unset($v);
  209. unset($v);
  210. $result = ['total' => $list->total(), 'rows' => $list->items()];
  211. return json($result);
  212. }
  213. //选择任务
  214. public function task($type= null)
  215. {
  216. //设置过滤方法
  217. $this->request->filter(['strip_tags', 'trim']);
  218. if (false === $this->request->isAjax()) {
  219. $this->assignConfig('sim_type', $type);
  220. return $this->view->fetch();
  221. }
  222. //如果发送的来源是 Selectpage,则转发到 Selectpage
  223. if ($this->request->request('keyField')) {
  224. return $this->selectpage();
  225. }
  226. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  227. $list = Task::where($where)->where('sim_type',$type)
  228. ->order($sort, $order)
  229. ->paginate($limit);
  230. unset($v);
  231. $result = ['total' => $list->total(), 'rows' => $list->items()];
  232. return json($result);
  233. }
  234. /**
  235. 老师开启考试
  236. */
  237. public function start($ids = null){
  238. $this->success('开始成功');
  239. }
  240. }