Practice.php 23 KB

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