Collection.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace app\admin\controller\student;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use app\admin\model\Report;
  6. use app\admin\model\Fault;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\response\Json;
  11. use think\Db;
  12. use think\Env;
  13. /**
  14. * sim-考试集合管理
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Collection extends Backend
  19. {
  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->whereExtend['exam_collection_type'] = 3;
  35. $this->whereExtend['exam_collection_state'] = 2;
  36. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  37. $groupIds = $this->auth->getGroupIds();
  38. //学员查看自己区队的考试集合
  39. if(in_array(8, $groupIds)){
  40. $departid = $this->auth->depart_id;
  41. $this->whereExtend[] = ['exp',Db::raw("FIND_IN_SET($departid,depart_ids)")];
  42. }
  43. }
  44. /**
  45. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  46. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  47. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  48. */
  49. /**
  50. * 查看
  51. *
  52. * @return string|Json
  53. * @throws \think\Exception
  54. * @throws DbException
  55. */
  56. public function index()
  57. {
  58. $isset = $this->model->alias('z')
  59. ->join('real_exam r','z.exam_collection_id=r.exam_collection_id','left')
  60. ->where('z.exam_collection_type=3 and z.exam_collection_state=2 and r.endtime=0 and r.user_id='.$this->auth->id)->find();
  61. if(empty($isset)){
  62. //提示页面,没有开始的考试
  63. return $this->view->fetch('tishi');
  64. }else{
  65. //// $this->redirect('/ZQOtIMLKud.php/student/collection/examing/ids/'.$isset['exam_id'].'?ref=addtabs');
  66. // echo "<script>location.href='/ZQOtIMLKud.php/student/collection/examing/ids/".$isset['exam_id']."';</script>";
  67. // //$this->redirect('/hotelmanage/room/displayindex/ids/1');
  68. // exit();
  69. echo "<script>location.href='/ZQOtIMLKud.php/student/collection/into/ids/".$isset['exam_collection_id']."'</script>";
  70. }
  71. die();
  72. //设置过滤方法
  73. $this->request->filter(['strip_tags', 'trim']);
  74. if (false === $this->request->isAjax()) {
  75. return $this->view->fetch();
  76. }
  77. //如果发送的来源是 Selectpage,则转发到 Selectpage
  78. if ($this->request->request('keyField')) {
  79. return $this->selectpage();
  80. }
  81. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  82. $list = $this->model
  83. ->where($where)->where($this->whereExtend)
  84. ->order($sort, $order)
  85. ->paginate($limit);
  86. foreach ($list as $k => $v){
  87. $exam_isset = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$v['exam_collection_id'],'exam_collection_type'=>3,'endtime'=>['>',0]])->find();
  88. if(isset($exam_isset)){
  89. $v->is_user_examed = 1;
  90. }else{
  91. $v->is_user_examed = 0;
  92. }
  93. }
  94. unset($v);
  95. $result = ['total' => $list->total(), 'rows' => $list->items()];
  96. return json($result);
  97. }
  98. //进入考试
  99. public function into($ids = null)
  100. {
  101. $row = $this->model->get($ids);
  102. if(!$row){
  103. $this->error('未找到记录');
  104. }
  105. //如果考试时间结束,也不可进入考试,超时未进入考试的学员
  106. // $duration = 10+$row->limit_duration;
  107. // $futureTimestamp = strtotime("+$duration minutes", $row->starttime);
  108. // if(time()>$futureTimestamp){
  109. // return $this->view->fetch('tishi');
  110. // }
  111. $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids,'exam_collection_type'=>3,'endtime'=>0])->find();
  112. if(!empty($info)){
  113. $this->exam_model->where('exam_id',$info['exam_id'])->update(['seat_id'=>$this->auth->seat_id]);
  114. $info['seat_id'] = $this->auth->seat_id;
  115. // $sim = Db::name('sim')->where('seat_id',$this->auth->seat_id)->find();
  116. $sim = Db::name('sim')->where('sim_type',$info['sim_type'])->find();
  117. $sim_text = '';
  118. if(!empty($sim)){
  119. if($sim['sim_state']==1){
  120. $sim_text = '在线';
  121. }else if($sim['sim_state']==2){
  122. $sim_text = '模拟器离线';
  123. }else if($sim['sim_state']==3){
  124. $sim_text = '网关离线';
  125. }else if($sim['sim_state']==4){
  126. $sim_text = '硬件故障异常';
  127. }else if($sim['sim_state']==5){
  128. $sim_text = '手动禁用';
  129. }
  130. }
  131. // 模拟器状态 0:可用初始化 1:在线 2:模拟器离线 3:网关离线 4:硬件故障异常 5:手动禁用
  132. $info['sim_text'] = $sim_text;
  133. }
  134. if(!empty($info['starttime']))
  135. {
  136. echo "<script>location.href='/ZQOtIMLKud.php/student/collection/examing/ids/".$info['exam_id']."'</script>";
  137. die();
  138. }
  139. // if ($this->request->isPost()) {
  140. // $exam_collection_id = $this->request->post('exam_collection_id');
  141. // if(empty($info->starttime)){
  142. // $info->start_time = date('Y-m-d H:i:s');
  143. // $info->starttime = time();
  144. // }
  145. // $info->exam_status = 4;
  146. // $info->save();
  147. // $this->success('开始成功','/ZQOtIMLKud.php/student/collection/examing/ids/'.$info['exam_id']);
  148. // }
  149. $this->view->assign('info', $info);
  150. $this->view->assign('row', $row);
  151. $this->assignConfig('exam_id', $info['exam_id']);
  152. $this->assignConfig('url_type', config('site.url_type'));
  153. $this->assignConfig('is_fault', Env::get('app.is_fault'));
  154. return $this->view->fetch();
  155. }
  156. public function examing($ids = null)
  157. {
  158. $row = $this->exam_model->get($ids);
  159. if(!$row){
  160. $this->error('未找到记录');
  161. }
  162. if ($this->request->isPost()) {
  163. //先请求接口判断,再进行处理
  164. if(Env::get('app.is_fault')){
  165. $url = config('site.url_type').'/sim/real-exam/student/exam/submit/'.$ids;
  166. // /dev-api/sim/real-exam/student/exam/submit/{examId}
  167. $ret = json_decode(send_get($url),true);
  168. // halt($ret);
  169. if($ret['code']!=200){
  170. $this->error($ret['msg']);
  171. }
  172. }
  173. //计算分数,保存记录
  174. $params = $this->request->post('row/a');
  175. $result = false;
  176. $score = 100;
  177. $fault_one_score = 25;
  178. $fault_two_score = 25;
  179. $fault_three_score = 25;
  180. $xianxian_score = 0;
  181. $yuanyin_socre= 0;
  182. $buwei_score= 0;
  183. $fangfa_score= 0;
  184. $xianxian_arr = [];
  185. $yuanyin_arr = [];
  186. $buwei_arr = [];
  187. $fangfa_arr = [];
  188. $jielun_score = 0;
  189. Db::startTrans();
  190. try {
  191. //更新考试结束时间
  192. $row->end_time = date('Y-m-d H:i:s');
  193. $row->endtime = time();
  194. $row->exam_status = 5;
  195. $row->other_report = $params['other_report'];
  196. $result = $row->save();
  197. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  198. //更新故障是否正确
  199. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  200. if(!empty($fault_list)){
  201. foreach ($fault_list as $k =>$t){
  202. $answer_right = 2;
  203. if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
  204. if($t['sim_fault_question_value']!=$t['sim_fault_answer_value'] ){
  205. //真实故障id 000200010001
  206. if($t['sim_fault_answer_value']=='00000000' && $t['fault_id'] !='000200010001'){
  207. $answer_right=2;
  208. }
  209. $answer_right=1;
  210. }
  211. }
  212. Db::name('real_exam_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
  213. }
  214. }
  215. //计算得分,故障是否有扣分 扣分制
  216. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  217. if(!empty($fault_right_list)){
  218. if($fault_right_list[0]['answer_right']==1){
  219. $fault_one_score = 0;
  220. }
  221. if($fault_right_list[1]['answer_right']==1){
  222. $fault_two_score = 0;
  223. }
  224. if($fault_right_list[2]['answer_right']==1){
  225. $fault_three_score = 0;
  226. }
  227. //根据故障部位,查找故障现象
  228. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  229. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr]])->column('parent_fault_id');
  230. //故障保存的是部位,需要查找故障现象
  231. $other_report = json_decode($params['other_report'],true);
  232. if(count($other_report)<3){
  233. $xianxian_score = 15-5*count($other_report)+$xianxian_score;
  234. $buwei_score = 3-1*count($other_report)+$buwei_score;
  235. $yuanyin_socre = 3-1*count($other_report)+$yuanyin_socre;
  236. $fangfa_score = 3-1*count($other_report)+$fangfa_score;
  237. }
  238. foreach ($other_report as $key => $em){
  239. //故障现象
  240. if(!in_array($em['xx_id'],$partent_fault_id)){
  241. $xianxian_score = 5+$xianxian_score;
  242. $xianxian_arr[] = $em['xx_id'];
  243. }
  244. //故障部位
  245. if(!empty($em['bw_id'])){
  246. $report_xx_id = Report::where(['bw_id'=>['in',$em['bw_id']]])->column('xx_id');
  247. if(!in_array($em['xx_id'],$report_xx_id)){ //现象不对,直接扣分
  248. $buwei_score = 1+$buwei_score;
  249. $buwei_arr[] = $em['bw_id'];
  250. }else{
  251. //现象正确,判断是不是正确的选项
  252. foreach (explode(',',$em['bw_id']) as $s => $bs){
  253. if(!in_array($bs,$fault_arr)){
  254. $buwei_score = 1+$buwei_score;
  255. $buwei_arr[] = $bs;
  256. }
  257. }
  258. }
  259. }else{
  260. //部位为空 +1
  261. $buwei_score = 1+ $buwei_score;
  262. }
  263. //可能原因
  264. if(!empty($em['yy_id'])){
  265. $report_bw_id = Report::where(['yy_id'=>['in',$em['yy_id']]])->column('bw_id');
  266. if(!in_array($em['bw_id'],$report_bw_id)){ //现象不对,直接扣分
  267. $yuanyin_socre = 1+$yuanyin_socre;
  268. $yuanyin_arr[] = $em['yy_id'];
  269. }else{
  270. }
  271. }else{
  272. //部位为空 +1
  273. $yuanyin_socre = 1+$yuanyin_socre;
  274. }
  275. //排除方法
  276. if(!empty($em['pc_id'])){
  277. $report_pc_id = Report::where(['pc_id'=>['in',$em['pc_id']]])->column('bw_id');
  278. if(!in_array($em['bw_id'],$report_pc_id)){ //现象不对,直接扣分
  279. $fangfa_score = 1+$fangfa_score;
  280. $fangfa_arr[] = $em['pc_id'];
  281. }else{
  282. }
  283. }else{
  284. $fangfa_score = 1+$fangfa_score;
  285. }
  286. }
  287. }
  288. // echo "<pre>";
  289. // print_r($buwei_arr);
  290. // echo '原因:'.$yuanyin_socre.'<br/>';
  291. // echo '部位:'.$buwei_score.'<br/>';
  292. // echo '现象:'.$xianxian_score;
  293. // die();
  294. $weixiu_score = $xianxian_score+$yuanyin_socre+$buwei_score+$fangfa_score;
  295. if($weixiu_score>15){
  296. $weixiu_score = 15;
  297. }
  298. // $xianxian_score = rand(1,3);
  299. // $yuanyin_socre= rand(1,3);
  300. // $buwei_score= rand(1,3);
  301. // $fangfa_score= rand(1,3);
  302. // if(empty($params['other_jielun'])){
  303. // $jielun_score = rand(1,3);
  304. // }
  305. //是否超时
  306. $overtime_fen = intval((time()-$info['endtime']) / 60);
  307. if($overtime_fen>=10){
  308. $overtime_score = 10;
  309. }else if($overtime_fen>0 && $overtime_fen<10){
  310. $overtime_score = $overtime_fen ;
  311. }else{
  312. $overtime_score =0;
  313. }
  314. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$weixiu_score-$overtime_score;
  315. //更新考试结果表
  316. $params['exam_id'] = $ids;
  317. $params['total'] = $fault_score;//得分;
  318. $params['fault_one_score'] = $fault_one_score;//得分;
  319. $params['fault_two_score'] = $fault_two_score;//得分;
  320. $params['fault_three_score'] = $fault_three_score;//得分;
  321. $params['xianxian_score'] = $xianxian_score;//得分;
  322. $params['xianxian_content'] = !empty($xianxian_arr) ? json_encode($xianxian_arr):'';//错题;
  323. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  324. $params['yuanyin_content'] = !empty($yuanyin_arr) ? json_encode($yuanyin_arr) : '';//错题;
  325. $params['buwei_score'] = $buwei_score;//得分;
  326. $params['buwei_content'] = !empty($buwei_arr) ? json_encode($buwei_arr) :'';//错题;
  327. $params['fangfa_score'] = $fangfa_score;//得分;
  328. $params['fangfa_content'] = !empty($fangfa_arr) ? json_encode($fangfa_arr):'';//错题;
  329. // $params['jielun_score'] = $jielun_score;//得分;
  330. $params['overtime_score'] = $overtime_score;//得分;
  331. Db::name('real_exam_score')->insert($params);
  332. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  333. Db::commit();
  334. } catch (ValidateException|PDOException|Exception $e) {
  335. Db::rollback();
  336. $this->error($e->getMessage());
  337. }
  338. if ($result === false) {
  339. $this->error(__('No rows were inserted'));
  340. }
  341. $this->success('交卷成功,待老师确认成绩','/ZQOtIMLKud.php/student/exam/index');
  342. }
  343. if(empty($row->starttime)){
  344. $row->start_time = date('Y-m-d H:i:s');
  345. $row->starttime = time();
  346. $row->exam_status = 4;
  347. $row->save();
  348. }
  349. $isloading =1;
  350. if(empty($row->other_replace)){
  351. $isloading = 0;
  352. $row->other_replace = '[{"fault_id":"","request_status":"0"}]';
  353. }
  354. if(empty($row->other_report)){
  355. $row->other_report = '[{"xx_id":"","yy_id":"","bw_id":"","pc_id":""}]';
  356. }
  357. //还未开始考试
  358. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  359. if(empty($row->starttime)){
  360. $timer = 60*$row->limit_duration;
  361. }else{
  362. $timer = $row->limit_duration*60 - abs(time() - $row->starttime);
  363. }
  364. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  365. $this->assignConfig('ids',$ids);
  366. $this->assignConfig('timer',$timer);
  367. $this->assignConfig('isloading',$isloading);
  368. $this->view->assign('row', $row);
  369. $departmentdata = [];
  370. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->order('fault_id asc')->select();
  371. //更换件数据
  372. $this->view->assign('departmentdata', $departmentdata);
  373. //故障现象
  374. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  375. $this->view->assign('xianxiang', $xianxiang);
  376. //可能原因
  377. $yuanyin = Fault::where(['fault_type'=>2,'sim_type'=>$row->sim_type])->select();
  378. $this->view->assign('yuanyin', $yuanyin);
  379. //故障部位
  380. $buwei = Fault::where(['fault_type'=>3,'sim_type'=>$row->sim_type])->select();
  381. $this->view->assign('buwei', $buwei);
  382. //排除方法
  383. $paichu = Fault::where(['fault_type'=>4,'sim_type'=>$row->sim_type])->select();
  384. $this->view->assign('paichu', $paichu);
  385. return $this->view->fetch();
  386. }
  387. public function replace($ids = null)
  388. {
  389. if ($this->request->isPost()) {
  390. $params = $this->request->post('other_replace','');
  391. $other_report = $this->request->post('other_report','');
  392. $param = json_decode($params,true);
  393. if(!empty($param)){
  394. $faultid_arr = [];
  395. foreach ($param as $key=>$item){
  396. if(!empty($item['fault_id'])){
  397. $isreplace = Db::name('real_exam_comp_request')->where(['exam_id'=>$ids,'fault_id'=>$item['fault_id']])->find();
  398. if(empty($isreplace)){
  399. $add = [
  400. 'exam_id'=>$ids,
  401. 'fault_id'=>$item['fault_id'],
  402. 'request_status'=>1,
  403. 'fault_name'=>Db::name('fault')->where('fault_id',$item['fault_id'])->value('replace_name'),
  404. 'create_by_user_id'=>$this->auth->id,
  405. 'create_by'=>$this->auth->nickname,
  406. 'createtime'=>time(),
  407. 'updatetime'=>time(),
  408. 'create_time'=>date('Y-m-d H:i:s'),
  409. 'update_time'=>date('Y-m-d H:i:s'),
  410. ];
  411. Db::name('real_exam_comp_request')->insert($add);
  412. $status = 1;
  413. }else{
  414. $status = $isreplace['request_status'];
  415. }
  416. $faultid_arr[] = [
  417. 'fault_id'=>$item['fault_id'],
  418. 'request_status'=>$status,
  419. ];
  420. }
  421. }
  422. unset($item);
  423. $row = $this->exam_model->get($ids);
  424. $row-> other_replace = $faultid_arr ? json_encode($faultid_arr):'';
  425. $row-> other_report = $other_report;
  426. $result = $row->save();
  427. }
  428. $this->success('申请成功,待老师审批');
  429. }
  430. }
  431. public function analysis($ids = null)
  432. {
  433. $row = $this->exam_model->get($ids);
  434. if(!$row){
  435. $this->error('未找到记录');
  436. }
  437. if ($this->request->isPost()) {
  438. $params = $this->request->post('row/a');
  439. //计算总分到学员考试表
  440. $this->success('操作成功','/ZQOtIMLKud.php/student/collection/index');
  441. }
  442. return $this->view->fetch();
  443. }
  444. }