Collection.php 21 KB

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