Practice.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. //// $this->redirect('/ZQOtIMLKud.php/student/collection/examing/ids/'.$isset['exam_id'].'?ref=addtabs');
  51. // echo "<script>location.href='/ZQOtIMLKud.php/student/collection/examing/ids/".$isset['exam_id']."';</script>";
  52. // //$this->redirect('/hotelmanage/room/displayindex/ids/1');
  53. // exit();
  54. echo "<script>location.href='/ZQOtIMLKud.php/student/practice/into/ids/".$isset['exam_collection_id']."'</script>";
  55. }
  56. die();
  57. //设置过滤方法
  58. $this->request->filter(['strip_tags', 'trim']);
  59. if (false === $this->request->isAjax()) {
  60. return $this->view->fetch();
  61. }
  62. //如果发送的来源是 Selectpage,则转发到 Selectpage
  63. if ($this->request->request('keyField')) {
  64. return $this->selectpage();
  65. }
  66. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  67. $list = $this->model
  68. ->where($where)->where($this->whereExtend)
  69. ->order($sort, $order)
  70. ->paginate($limit);
  71. foreach ($list as $k => $v){
  72. $exam_isset = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$v['exam_collection_id'],'exam_collection_type'=>1])->find();
  73. if(isset($exam_isset)){
  74. $v->is_user_examed = 1;
  75. }else{
  76. $v->is_user_examed = 0;
  77. }
  78. }
  79. unset($v);
  80. $result = ['total' => $list->total(), 'rows' => $list->items()];
  81. return json($result);
  82. }
  83. //进入考试
  84. public function into($ids = null)
  85. {
  86. $row = $this->model->get($ids);
  87. if(!$row){
  88. $this->error('未找到记录');
  89. }
  90. $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids])->find();
  91. if(empty($info)){
  92. $arr = [
  93. 'exam_collection_id' => $ids,
  94. 'user_id' => $this->auth->id,
  95. 'user_username' => $this->auth->username,
  96. 'user_nickname' => $this->auth->nickname,
  97. 'user_depart_id' => $this->auth->depart_id,
  98. 'exam_collection_name' => $row->exam_collection_name,
  99. 'exam_collection_type' =>1,
  100. 'sim_type' => $row->sim_type,
  101. 'seat_id' => 1,
  102. 'sim_id' => 12,
  103. ];
  104. $this->exam_model->save($arr);
  105. }
  106. if(!empty($info['starttime']))
  107. {
  108. echo "<script>location.href='/ZQOtIMLKud.php/student/practice/examing/ids/".$info['exam_id']."'</script>";
  109. die();
  110. }
  111. // if ($this->request->isPost()) {
  112. // $exam_collection_id = $this->request->post('exam_collection_id');
  113. // if(empty($info->starttime)){
  114. // $info->start_time = date('Y-m-d H:i:s');
  115. // $info->starttime = time();
  116. // }
  117. // $info->exam_status = 4;
  118. // $info->save();
  119. // $this->success('开始成功','/ZQOtIMLKud.php/student/practice/examing/ids/'.$info['exam_id']);
  120. // }
  121. $this->assignConfig('exam_id', $info['exam_id']);
  122. $this->assignConfig('url_type', config('site.url_type'));
  123. $this->assignConfig('is_fault', Env::get('app.is_fault'));
  124. $this->view->assign('row', $row);
  125. return $this->view->fetch();
  126. }
  127. public function examing($ids = null)
  128. {
  129. $row = $this->exam_model->get($ids);
  130. if(!$row){
  131. $this->error('未找到记录');
  132. }
  133. if ($this->request->isPost()) {
  134. //先请求接口判断,再进行处理
  135. if(Env::get('app.is_fault')){
  136. $url = config('site.url_type').'/sim/real-exam/student/exam/submit/'.$ids;
  137. // /dev-api/sim/real-exam/student/exam/submit/{examId}
  138. $ret = json_decode(send_get($url),true);
  139. // halt($ret);
  140. if($ret['code']!=200){
  141. $this->error($ret['msg']);
  142. }
  143. }
  144. //计算分数,保存记录
  145. $params = $this->request->post('row/a');
  146. $result = false;
  147. $score = 100;
  148. $fault_one_score = 25;
  149. $fault_two_score = 25;
  150. $fault_three_score = 25;
  151. $xianxian_score = 0;
  152. $yuanyin_socre= 0;
  153. $buwei_score= 0;
  154. $fangfa_score= 0;
  155. $xianxian_arr = [];
  156. $yuanyin_arr = [];
  157. $buwei_arr = [];
  158. $fangfa_arr = [];
  159. $jielun_score = 0;
  160. Db::startTrans();
  161. try {
  162. //更新考试结束时间
  163. $row->end_time = date('Y-m-d H:i:s');
  164. $row->endtime = time();
  165. $row->exam_status = 5;
  166. $row->other_report = $params['other_report'];
  167. $result = $row->save();
  168. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  169. //更新故障是否正确
  170. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  171. if(!empty($fault_list)){
  172. foreach ($fault_list as $k =>$t){
  173. $answer_right = 0;
  174. if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
  175. if($t['sim_fault_question_value']!=$t['sim_fault_answer_value']){
  176. $answer_right=1;
  177. }else{
  178. $answer_right=2;
  179. }
  180. }
  181. Db::name('real_exam_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
  182. }
  183. }
  184. //计算得分,故障是否有扣分 扣分制
  185. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  186. if(!empty($fault_right_list)){
  187. if($fault_right_list[0]['answer_right']==1){
  188. $fault_one_score = 0;
  189. }
  190. if($fault_right_list[1]['answer_right']==1){
  191. $fault_two_score = 0;
  192. }
  193. if($fault_right_list[2]['answer_right']==1){
  194. $fault_three_score = 0;
  195. }
  196. //根据故障部位,查找故障现象
  197. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  198. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr]])->column('parent_fault_id');
  199. //故障保存的是部位,需要查找故障现象
  200. $other_report = json_decode($params['other_report'],true);
  201. if(count($other_report)<3){
  202. $xianxian_score = 15-5*count($other_report)+$xianxian_score;
  203. $buwei_score = 3-1*count($other_report)+$buwei_score;
  204. $yuanyin_socre = 3-1*count($other_report)+$yuanyin_socre;
  205. $fangfa_score = 3-1*count($other_report)+$fangfa_score;
  206. }
  207. foreach ($other_report as $key => $em){
  208. //故障现象
  209. if(!in_array($em['xx_id'],$partent_fault_id)){
  210. $xianxian_score = 5+$xianxian_score;
  211. $xianxian_arr[] = $em['xx_id'];
  212. }
  213. //故障部位
  214. if(!empty($em['bw_id'])){
  215. $report_xx_id = Report::where(['bw_id'=>['in',$em['bw_id']]])->column('xx_id');
  216. if(!in_array($em['xx_id'],$report_xx_id)){ //现象不对,直接扣分
  217. $buwei_score = 1+$buwei_score;
  218. $buwei_arr[] = $em['bw_id'];
  219. }else{
  220. //现象正确,判断是不是正确的选项
  221. foreach (explode(',',$em['bw_id']) as $s => $bs){
  222. if(!in_array($bs,$fault_arr)){
  223. $buwei_score = 1+$buwei_score;
  224. $buwei_arr[] = $bs;
  225. }
  226. }
  227. }
  228. }else{
  229. //部位为空 +1
  230. $buwei_score = 1+ $buwei_score;
  231. }
  232. //可能原因
  233. if(!empty($em['yy_id'])){
  234. $report_bw_id = Report::where(['yy_id'=>['in',$em['yy_id']]])->column('bw_id');
  235. if(!in_array($em['bw_id'],$report_bw_id)){ //现象不对,直接扣分
  236. $yuanyin_socre = 1+$yuanyin_socre;
  237. $yuanyin_arr[] = $em['yy_id'];
  238. }else{
  239. }
  240. }else{
  241. //部位为空 +1
  242. $yuanyin_socre = 1+$yuanyin_socre;
  243. }
  244. //排除方法
  245. if(!empty($em['pc_id'])){
  246. $report_pc_id = Report::where(['pc_id'=>['in',$em['pc_id']]])->column('bw_id');
  247. if(!in_array($em['bw_id'],$report_pc_id)){ //现象不对,直接扣分
  248. $fangfa_score = 1+$fangfa_score;
  249. $fangfa_arr[] = $em['pc_id'];
  250. }else{
  251. }
  252. }else{
  253. $fangfa_score = 1+$fangfa_score;
  254. }
  255. }
  256. }
  257. // echo "<pre>";
  258. // print_r($buwei_arr);
  259. // echo '原因:'.$yuanyin_socre.'<br/>';
  260. // echo '部位:'.$buwei_score.'<br/>';
  261. // echo '现象:'.$xianxian_score;
  262. // die();
  263. $weixiu_score = $xianxian_score+$yuanyin_socre+$buwei_score+$fangfa_score;
  264. if($weixiu_score>15){
  265. $weixiu_score = 15;
  266. }
  267. // $xianxian_score = rand(1,3);
  268. // $yuanyin_socre= rand(1,3);
  269. // $buwei_score= rand(1,3);
  270. // $fangfa_score= rand(1,3);
  271. // if(empty($params['other_jielun'])){
  272. // $jielun_score = rand(1,3);
  273. // }
  274. //是否超时
  275. $overtime_fen = intval((time()-$info['endtime']) / 60);
  276. if($overtime_fen>=10){
  277. $overtime_score = 10;
  278. }else if($overtime_fen>0 && $overtime_fen<10){
  279. $overtime_score = $overtime_fen ;
  280. }else{
  281. $overtime_score =0;
  282. }
  283. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$weixiu_score-$overtime_score;
  284. //更新考试结果表
  285. $params['exam_id'] = $ids;
  286. $params['total'] = $fault_score;//得分;
  287. $params['fault_one_score'] = $fault_one_score;//得分;
  288. $params['fault_two_score'] = $fault_two_score;//得分;
  289. $params['fault_three_score'] = $fault_three_score;//得分;
  290. $params['xianxian_score'] = $xianxian_score;//得分;
  291. $params['xianxian_content'] = !empty($xianxian_arr) ? json_encode($xianxian_arr):'';//错题;
  292. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  293. $params['yuanyin_content'] = !empty($yuanyin_arr) ? json_encode($yuanyin_arr) : '';//错题;
  294. $params['buwei_score'] = $buwei_score;//得分;
  295. $params['buwei_content'] = !empty($buwei_arr) ? json_encode($buwei_arr) :'';//错题;
  296. $params['fangfa_score'] = $fangfa_score;//得分;
  297. $params['fangfa_content'] = !empty($fangfa_arr) ? json_encode($fangfa_arr):'';//错题;
  298. // $params['jielun_score'] = $jielun_score;//得分;
  299. $params['overtime_score'] = $overtime_score;//得分;
  300. Db::name('real_exam_score')->insert($params);
  301. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  302. Db::commit();
  303. } catch (ValidateException|PDOException|Exception $e) {
  304. Db::rollback();
  305. $this->error($e->getMessage());
  306. }
  307. if ($result === false) {
  308. $this->error(__('No rows were inserted'));
  309. }
  310. $this->success('交卷成功','/ZQOtIMLKud.php/student/practice/analysis/ids/'.$row['exam_id']);
  311. }
  312. if(empty($row->starttime)){
  313. $row->start_time = date('Y-m-d H:i:s');
  314. $row->starttime = time();
  315. $row->exam_status = 4;
  316. $row->save();
  317. }
  318. $isloading =1;
  319. if(empty($row->other_replace)){
  320. $isloading = 0;
  321. $row->other_replace = '[{"fault_id":"","request_status":"0"}]';
  322. }
  323. if(empty($row->other_report)){
  324. $row->other_report = '[{"xx_id":"","yy_id":"","bw_id":"","pc_id":""}]';
  325. }
  326. //还未开始练习
  327. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  328. if(empty($row->starttime)){
  329. $timer = 60*$row->limit_duration;
  330. }else{
  331. $timer = $row->limit_duration*60 - abs(time() - $row->starttime);
  332. }
  333. $departmentdata = [];
  334. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->order('fault_id asc')->select();
  335. $this->view->assign('departmentdata', $departmentdata);
  336. //故障现象
  337. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  338. $this->view->assign('xianxiang', $xianxiang);
  339. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  340. $this->assignConfig('ids',$ids);
  341. $this->assignConfig('timer',$timer);
  342. $this->assignConfig('isloading',$isloading);
  343. $this->view->assign('row', $row);
  344. return $this->view->fetch();
  345. }
  346. public function analysis($ids = null)
  347. {
  348. if ($this->request->isPost()) {
  349. $params = $this->request->post('row/a');
  350. //计算总分到学员考试表
  351. $this->success('操作成功','/ZQOtIMLKud.php/student/practice/index');
  352. }
  353. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  354. if (!$row) {
  355. $this->error(__('No Results were found'));
  356. }
  357. $rows = $this->exam_model->get($ids);
  358. $row['seat_id'] = $rows->seat_id;
  359. $row['user_nickname'] = $rows->user_nickname;
  360. $row['user_username'] = $rows->user_username;
  361. $row['user_depart_id'] = $rows->user_depart_id;
  362. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  363. $row['start_time'] = $rows->start_time;
  364. $row['end_time'] = $rows->end_time;
  365. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  366. $row['fault_name_one'] = !empty($fault_list[0]['fault_id']) ? Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name'):'';
  367. $row['fault_name_two'] = !empty($fault_list[1]['fault_id']) ? Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name'):'';
  368. $row['fault_name_three'] = !empty($fault_list[2]['fault_id']) ? Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name'):'';
  369. if($row['xianxian_score']>0 && empty($row['xianxian_content']))
  370. {
  371. $row['xianxian_content_name'] = '未选择';
  372. }else{
  373. $xianxian = Report::where(['xx_id'=>['in',json_decode($row['xianxian_content'],true)]])->column('xx_name');
  374. $row['xianxian_content_name'] = !empty($xianxian) ? implode(',',$xianxian) :'不存在或未选择';
  375. }
  376. if($row['yuanyin_socre']>0 && empty($row['yuanyin_content']))
  377. {
  378. $row['yuanyin_content_name'] = '未选择';
  379. }else{
  380. $yuanyin = Report::where(['yy_id'=>['in',json_decode($row['yuanyin_content'],true)]])->column('yy_name');
  381. $row['yuanyin_content_name'] = !empty($yuanyin) ? implode(',',$yuanyin) :'不存在或未选择';
  382. }
  383. if($row['buwei_score']>0 &&empty($row['buwei_content']))
  384. {
  385. $row['buwei_content_name'] = '未选择';
  386. }else{
  387. $buwei = Report::where(['bw_id'=>['in',json_decode($row['buwei_content'],true)]])->column('bw_name');
  388. $row['buwei_content_name'] = !empty($buwei) ? implode(',',$buwei) :'不存在或未选择';
  389. }
  390. if($row['fangfa_score']>0 && empty($row['fangfa_content']))
  391. {
  392. $row['fangfa_content_name'] = '未选择';
  393. }else{
  394. $fangfa = Report::where(['pc_id'=>['in',json_decode($row['fangfa_content'],true)]])->column('pc_name');
  395. $row['fangfa_content_name'] = !empty($fangfa) ? implode(',',$fangfa) :'不存在或未选择';
  396. }
  397. $this->view->assign('row', $row);
  398. return $this->view->fetch();
  399. }
  400. }