Practice.php 23 KB

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