Collection.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 $exam_model = null;
  24. protected $whereExtend = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = new \app\admin\model\teacher\Collection;
  29. $this->exam_model = new \app\admin\model\teacher\Exams;
  30. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  31. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  32. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  33. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  34. $this->whereExtend['exam_collection_type'] = 3;
  35. }
  36. /**
  37. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  38. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  39. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  40. */
  41. public function add()
  42. {
  43. if (false === $this->request->isPost()) {
  44. return $this->view->fetch();
  45. }
  46. $params = $this->request->post('row/a');
  47. if (empty($params)) {
  48. $this->error(__('Parameter %s can not be empty', ''));
  49. }
  50. if(empty($params['depart_ids'])){
  51. $this->error('请选择区队名称');
  52. }
  53. if(!empty($params['question_setting_method'])){
  54. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  55. $this->error('出题方式为考题自选,请选择考题');
  56. }
  57. if($params['question_setting_method']==3 && empty($params['task_id'])){
  58. $this->error('出题方式为任务自选,请选择任务');
  59. }
  60. }
  61. $params = $this->preExcludeFields($params);
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $depart_ids = $params['depart_ids'];
  66. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  67. $params['question_name'] = $params['fault_name'];
  68. unset($params['fault_name']);
  69. }
  70. $result = $this->model->allowField(true)->save($params);
  71. foreach(explode(',',$depart_ids) as $item){
  72. if(!empty($item)){
  73. $arr = [
  74. 'exam_collection_id' => $this->model->exam_collection_id,
  75. 'dept_id' => $item,
  76. 'update_time' => date('Y-m-d H:i:s'),
  77. ];
  78. Db::name('real_exam_collection_dept')->insert($arr);
  79. }
  80. }
  81. //匹配的学员自动加入 mx_real_exam 考试表 总分
  82. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  83. foreach ($admin_list as $it){
  84. $arr1 = [
  85. 'exam_collection_id' => $this->model->exam_collection_id,
  86. 'exam_collection_name' => $params['exam_collection_name'],
  87. 'exam_collection_type' => $params['exam_collection_type'],
  88. 'sim_type' => $params['sim_type'],
  89. 'user_username' => $it['username'],
  90. 'user_nickname' => $it['nickname'],
  91. 'user_id' => $it['id'],
  92. 'user_depart_id' => $it['depart_id'],
  93. 'create_time'=>date('Y-m-d H:i:s'),
  94. 'update_time' => date('Y-m-d H:i:s'),
  95. ];
  96. $examid = Db::name('real_exam')->insertGetId($arr1);
  97. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  98. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  99. if($params['question_setting_method']==1){ //系统随机
  100. $fault_key = array_rand($fault_list, 3);
  101. }else if($params['question_setting_method']==2){//教师自选
  102. $question_ids = $params['question_ids'];
  103. }else if($params['question_setting_method']==3){ //任务自选
  104. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  105. }
  106. // halt($question_ids);
  107. foreach ($fault_list as $key=> $item1){
  108. $flag = 0;
  109. if(!empty($fault_key) && in_array($key,$fault_key)){
  110. $flag = 1;
  111. }
  112. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  113. $flag = 1;
  114. }
  115. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  116. $flag = 1;
  117. }
  118. $add= [
  119. 'exam_id'=>$examid,
  120. 'fault_id'=>$item1['fault_id'],
  121. 'ref_type'=>2,
  122. 'flag'=>$flag,
  123. 'ref_state'=>3,
  124. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  125. 'createtime'=>time(),
  126. 'updatetime'=>time(),
  127. 'create_time'=>date('Y-m-d H:i:s'),
  128. 'update_time'=>date('Y-m-d H:i:s'),
  129. ];
  130. Db::name('real_exam_fault')->insert($add);
  131. }
  132. unset($item1);
  133. }
  134. Db::commit();
  135. } catch (ValidateException|PDOException|Exception $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. }
  139. if ($result === false) {
  140. $this->error(__('No rows were inserted'));
  141. }
  142. $this->success();
  143. }
  144. public function edit($ids = null)
  145. {
  146. $row = $this->model->get($ids);
  147. if (!$row) {
  148. $this->error(__('No Results were found'));
  149. }
  150. if (false === $this->request->isPost()) {
  151. // $examing_count = $this->exam_model->where(['exam_collection_id'=>$ids,'exam_status'=>4])->count();
  152. // if($examing_count>0){
  153. // $this->error('有学员考试中,无法修改');
  154. // }
  155. $this->view->assign('row', $row);
  156. $this->assignConfig('row_info', $row);
  157. return $this->view->fetch();
  158. }
  159. $params = $this->request->post('row/a');
  160. if (empty($params)) {
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. if(!empty($params['question_setting_method'])){
  164. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  165. $this->error('出题方式为考题自选,请选择考题');
  166. }
  167. if($params['question_setting_method']==3 && empty($params['task_id'])){
  168. $this->error('出题方式为任务自选,请选择任务');
  169. }
  170. }
  171. $params = $this->preExcludeFields($params);
  172. $result = false;
  173. Db::startTrans();
  174. try {
  175. $depart_ids = $params['depart_ids'];
  176. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  177. $params['question_name'] = $params['fault_name'];
  178. unset($params['fault_name']);
  179. }
  180. $result = $row->allowField(true)->save($params);
  181. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  182. foreach(explode(',',$depart_ids) as $item){
  183. if(!empty($item)){
  184. $arr = [
  185. 'exam_collection_id' => $row->exam_collection_id,
  186. 'dept_id' => $item,
  187. 'update_time' => date('Y-m-d H:i:s'),
  188. ];
  189. Db::name('real_exam_collection_dept')->insert($arr);
  190. }
  191. }
  192. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  193. //匹配的学员自动加入 mx_real_exam 考试表 总分
  194. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  195. foreach ($admin_list as $it){
  196. $arr1 = [
  197. 'exam_collection_id' => $row->exam_collection_id,
  198. 'exam_collection_name' => $params['exam_collection_name'],
  199. 'exam_collection_type' => $row['exam_collection_type'],
  200. 'sim_type' => $params['sim_type'],
  201. 'user_username' => $it['username'],
  202. 'user_nickname' => $it['nickname'],
  203. 'user_id' => $it['id'],
  204. 'user_depart_id' => $it['depart_id'],
  205. 'create_time'=>date('Y-m-d H:i:s'),
  206. 'update_time' => date('Y-m-d H:i:s'),
  207. ];
  208. $examid = Db::name('real_exam')->insertGetId($arr1);
  209. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  210. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  211. if($params['question_setting_method']==1){ //系统随机
  212. $fault_key = array_rand($fault_list, 3);
  213. }else if($params['question_setting_method']==2){//教师自选
  214. $question_ids = $params['question_ids'];
  215. }else if($params['question_setting_method']==3){ //任务自选
  216. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  217. }
  218. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  219. foreach ($fault_list as $key=> $item1){
  220. $flag = 0;
  221. if(!empty($fault_key) && in_array($key,$fault_key)){
  222. $flag = 1;
  223. }
  224. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  225. $flag = 1;
  226. }
  227. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  228. $flag = 1;
  229. }
  230. $add= [
  231. 'exam_id'=>$examid,
  232. 'fault_id'=>$item1['fault_id'],
  233. 'ref_type'=>2,
  234. 'flag'=>$flag,
  235. 'ref_state'=>3,
  236. // 'sim_fault_question_value'=>'0000000'.rand(1,2),
  237. 'createtime'=>time(),
  238. 'updatetime'=>time(),
  239. 'create_time'=>date('Y-m-d H:i:s'),
  240. 'update_time'=>date('Y-m-d H:i:s'),
  241. ];
  242. Db::name('real_exam_fault')->insert($add);
  243. }
  244. unset($item1);
  245. }
  246. Db::commit();
  247. } catch (ValidateException|PDOException|Exception $e) {
  248. Db::rollback();
  249. $this->error($e->getMessage());
  250. }
  251. if (false === $result) {
  252. $this->error(__('No rows were updated'));
  253. }
  254. $this->success();
  255. }
  256. public function multi($ids = null)
  257. {
  258. if (false === $this->request->isPost()) {
  259. $this->error(__('Invalid parameters'));
  260. }
  261. $ids = $ids ?: $this->request->post('ids');
  262. if (empty($ids)) {
  263. $this->error(__('Parameter %s can not be empty', 'ids'));
  264. }
  265. if (false === $this->request->has('params')) {
  266. $this->error(__('No rows were updated'));
  267. }
  268. parse_str($this->request->post('params'), $values);
  269. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  270. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  271. if (empty($values)) {
  272. $this->error(__('You have no permission'));
  273. }
  274. $adminIds = $this->getDataLimitAdminIds();
  275. if (is_array($adminIds)) {
  276. $this->model->where($this->dataLimitField, 'in', $adminIds);
  277. }
  278. $count = 0;
  279. if(!empty($values['exam_collection_state'])){
  280. //开始考试集合
  281. if($values['exam_collection_state']==2){
  282. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  283. // if($state_count>0){
  284. // $this->error('已有启用的考试,请先关闭原来的考试');
  285. // }
  286. //触发Java端后台的接口数据,
  287. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  288. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  289. $ret = json_decode(send_post($url),true);
  290. if($ret['code']!=200){
  291. $this->error($ret['msg']);
  292. }
  293. $count = 1;
  294. }
  295. //关闭
  296. if($values['exam_collection_state']==3){
  297. //触发Java端后台的接口数据,
  298. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  299. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  300. $ret = json_decode(send_post($url),true);
  301. if($ret['code']!=200){
  302. $this->error($ret['msg']);
  303. }
  304. $count = 1;
  305. }
  306. }
  307. Db::startTrans();
  308. try {
  309. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  310. foreach ($list as $item) {
  311. $count += $item->allowField(true)->isUpdate(true)->save($values);
  312. }
  313. Db::commit();
  314. } catch (PDOException|Exception $e) {
  315. Db::rollback();
  316. $this->error($e->getMessage());
  317. }
  318. if ($count) {
  319. $this->success();
  320. }
  321. $this->error(__('No rows were updated'));
  322. }
  323. //选择考题
  324. public function faults($type = null)
  325. {
  326. //设置过滤方法
  327. $this->request->filter(['strip_tags', 'trim']);
  328. if (false === $this->request->isAjax()) {
  329. $this->assignConfig('sim_type', $type);
  330. return $this->view->fetch();
  331. }
  332. //如果发送的来源是 Selectpage,则转发到 Selectpage
  333. if ($this->request->request('keyField')) {
  334. return $this->selectpage();
  335. }
  336. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  337. $where_fault = [
  338. 'sim_type' => $type,
  339. 'fault_type' => 3,
  340. 'fault_state'=>0
  341. ];
  342. $list = Fault::where($where)->where($where_fault)
  343. ->order($sort, $order)
  344. ->paginate($limit);
  345. foreach ($list as $k => $v){
  346. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  347. if(isset($exam)){
  348. $v->parent_name = $exam['name'];
  349. }else{
  350. $v->parent_name = '';
  351. }
  352. }
  353. unset($v);
  354. unset($v);
  355. $result = ['total' => $list->total(), 'rows' => $list->items()];
  356. return json($result);
  357. }
  358. //选择任务
  359. public function task($type= null)
  360. {
  361. //设置过滤方法
  362. $this->request->filter(['strip_tags', 'trim']);
  363. if (false === $this->request->isAjax()) {
  364. $this->assignConfig('sim_type', $type);
  365. return $this->view->fetch();
  366. }
  367. //如果发送的来源是 Selectpage,则转发到 Selectpage
  368. if ($this->request->request('keyField')) {
  369. return $this->selectpage();
  370. }
  371. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  372. $list = Task::where($where)->where('sim_type',$type)
  373. ->order($sort, $order)
  374. ->paginate($limit);
  375. unset($v);
  376. $result = ['total' => $list->total(), 'rows' => $list->items()];
  377. return json($result);
  378. }
  379. /**
  380. 老师开启考试
  381. */
  382. public function start($ids = null){
  383. $row = $this->model->get($ids);
  384. if (!$row) {
  385. $this->error(__('No Results were found'));
  386. }
  387. $params['start_time'] = date('Y-m-d H:i:s');
  388. $params['starttime'] = time();
  389. $result = $row->allowField(true)->save($params);
  390. $this->success('开始成功');
  391. }
  392. public function del($ids = null)
  393. {
  394. if (false === $this->request->isPost()) {
  395. $this->error(__("Invalid parameters"));
  396. }
  397. $ids = $ids ?: $this->request->post("ids");
  398. if (empty($ids)) {
  399. $this->error(__('Parameter %s can not be empty', 'ids'));
  400. }
  401. $pk = $this->model->getPk();
  402. $adminIds = $this->getDataLimitAdminIds();
  403. if (is_array($adminIds)) {
  404. $this->model->where($this->dataLimitField, 'in', $adminIds);
  405. }
  406. $list = $this->model->where($pk, 'in', $ids)->select();
  407. $count = 0;
  408. Db::startTrans();
  409. try {
  410. //删除考试集合 mx_real_exam_collection
  411. foreach ($list as $item) {
  412. if($item['yikao_count']>0){
  413. $this->error('已有学生参加考试,无法删除');
  414. }
  415. $count += $item->delete();
  416. }
  417. //删除学员考试 mx_real_exam
  418. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  419. if(!empty($exam_list)){
  420. foreach ($exam_list as $k=>$it)
  421. {
  422. //删除学员考试故障表 mx_real_exam_fault
  423. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  424. $it->delete();
  425. }
  426. }
  427. //删除考试关联区队表 mx_real_exam_collection_dept
  428. Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  429. Db::commit();
  430. } catch (PDOException|Exception $e) {
  431. Db::rollback();
  432. $this->error($e->getMessage());
  433. }
  434. if ($count) {
  435. $this->success();
  436. }
  437. $this->error(__('No rows were deleted'));
  438. }
  439. }