Practice.php 23 KB

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