Practice.php 20 KB

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