Exercise.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 Exercise 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->assign('sim_sim_type', ConfigModel::getSimTypeList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. if ($this->request->isPost()) {
  46. $params = $this->request->post('row/a');
  47. $count = 0;
  48. Db::startTrans();
  49. try {
  50. $params['exam_collection_type'] = 2;
  51. $params['exam_collection_state'] = 2;
  52. $params['question_setting_method'] = 1;
  53. $params['exam_collection_name'] = $this->auth->nickname.'自主练习';
  54. $params['limit_duration'] = 30;
  55. $params['start_time'] = date('Y-m-d');
  56. $params['end_time'] = date('Y-m-d');
  57. $exam_id = $this->model->insertGetId($params);
  58. $data = [
  59. 'exam_collection_id' => $exam_id,
  60. 'user_id' => $this->auth->id,
  61. 'user_username' => $this->auth->username,
  62. 'user_nickname' => $this->auth->nickname,
  63. 'user_depart_id' => $this->auth->depart_id,
  64. 'exam_collection_name' => $params['exam_collection_name'],
  65. 'exam_collection_type' =>$params['exam_collection_type'],
  66. 'sim_type' => $params['sim_type'],
  67. 'seat_id' =>1,
  68. 'seat_id' =>12,
  69. 'exam_status'=>4,
  70. 'start_time'=>date('Y-m-d H:i:s'),
  71. 'starttime'=>time(),
  72. 'create_time'=>date('Y-m-d H:i:s'),
  73. 'createtime'=>time(),
  74. 'update_time'=>date('Y-m-d H:i:s'),
  75. 'updatetime'=>time(),
  76. ];
  77. $examid = $this->exam_model->insertGetId($data);
  78. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  79. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  80. foreach ($fault_list as $key=> $item1){
  81. $fault_key = array_rand($fault_list, 3);
  82. $flag = 0;
  83. if(!empty($fault_key) && in_array($key,$fault_key)){
  84. $flag = 1;
  85. }
  86. $add= [
  87. 'exam_id'=>$examid,
  88. 'fault_id'=>$item1['fault_id'],
  89. 'ref_type'=>2,
  90. 'flag'=>$flag,
  91. 'ref_state'=>0,
  92. 'createtime'=>time(),
  93. 'updatetime'=>time(),
  94. 'create_time'=>date('Y-m-d H:i:s'),
  95. 'update_time'=>date('Y-m-d H:i:s'),
  96. ];
  97. Db::name('real_exam_fault')->insert($add);
  98. }
  99. $count = $examid;
  100. Db::commit();
  101. } catch (PDOException|Exception $e) {
  102. Db::rollback();
  103. $this->error($e->getMessage());
  104. }
  105. if ($count) {
  106. $this->success('开始成功','/ZQOtIMLKud.php/student/exercise/examing/ids/'.$count);
  107. }
  108. $this->error(__('No rows were updated'));
  109. }
  110. }
  111. public function examing($ids = null)
  112. {
  113. $row = $this->exam_model->get($ids);
  114. if(!$row){
  115. $this->error('未找到记录');
  116. }
  117. if ($this->request->isPost()) {
  118. //先请求接口判断,再进行处理
  119. if(Env::get('app.is_fault')){
  120. $url = config('site.url_type').'/sim/real-exam/student/exam/submit/'.$ids;
  121. // /dev-api/sim/real-exam/student/exam/submit/{examId}
  122. $ret = json_decode(send_get($url),true);
  123. // halt($ret);
  124. if($ret['code']!=200){
  125. $this->error($ret['msg']);
  126. }
  127. }
  128. //计算分数,保存记录
  129. $params = $this->request->post('row/a');
  130. $result = false;
  131. $score = 100;
  132. $fault_one_score = 25;
  133. $fault_two_score = 25;
  134. $fault_three_score = 25;
  135. $xianxian_score = 0;
  136. $yuanyin_socre= 0;
  137. $buwei_score= 0;
  138. $fangfa_score= 0;
  139. $xianxian_arr = [];
  140. $yuanyin_arr = [];
  141. $buwei_arr = [];
  142. $fangfa_arr = [];
  143. $jielun_score = 0;
  144. Db::startTrans();
  145. try {
  146. //更新考试结束时间
  147. $row->end_time = date('Y-m-d H:i:s');
  148. $row->endtime = time();
  149. $row->exam_status = 5;
  150. $row->other_report = $params['other_report'];
  151. $result = $row->save();
  152. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  153. //更新故障是否正确
  154. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  155. if(!empty($fault_list)){
  156. foreach ($fault_list as $k =>$t){
  157. $answer_right = 2;
  158. if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
  159. if($t['sim_fault_question_value']!=$t['sim_fault_answer_value'] ){
  160. //真实故障id 000200010001
  161. if($t['sim_fault_answer_value']=='00000000' && $t['fault_id'] !='000200010001'){
  162. $answer_right=2;
  163. }
  164. $answer_right=1;
  165. }
  166. }
  167. Db::name('real_exam_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
  168. }
  169. }
  170. //计算得分,故障是否有扣分 扣分制
  171. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  172. if(!empty($fault_right_list)){
  173. if($fault_right_list[0]['answer_right']==1){
  174. $fault_one_score = 0;
  175. }
  176. if($fault_right_list[1]['answer_right']==1){
  177. $fault_two_score = 0;
  178. }
  179. if($fault_right_list[2]['answer_right']==1){
  180. $fault_three_score = 0;
  181. }
  182. //根据故障部位,查找故障现象
  183. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  184. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr]])->column('parent_fault_id');
  185. //故障保存的是部位,需要查找故障现象
  186. $other_report = json_decode($params['other_report'],true);
  187. if(count($other_report)<3){
  188. $xianxian_score = 15-5*count($other_report)+$xianxian_score;
  189. $buwei_score = 3-1*count($other_report)+$buwei_score;
  190. $yuanyin_socre = 3-1*count($other_report)+$yuanyin_socre;
  191. $fangfa_score = 3-1*count($other_report)+$fangfa_score;
  192. }
  193. foreach ($other_report as $key => $em){
  194. //故障现象
  195. if(!in_array($em['xx_id'],$partent_fault_id)){
  196. $xianxian_score = 5+$xianxian_score;
  197. $xianxian_arr[] = $em['xx_id'];
  198. }
  199. //故障部位
  200. if(!empty($em['bw_id'])){
  201. $report_xx_id = Report::where(['bw_id'=>['in',$em['bw_id']]])->column('xx_id');
  202. if(!in_array($em['xx_id'],$report_xx_id)){ //现象不对,直接扣分
  203. $buwei_score = 1+$buwei_score;
  204. $buwei_arr[] = $em['bw_id'];
  205. }else{
  206. //现象正确,判断是不是正确的选项
  207. foreach (explode(',',$em['bw_id']) as $s => $bs){
  208. if(!in_array($bs,$fault_arr)){
  209. $buwei_score = 1+$buwei_score;
  210. $buwei_arr[] = $bs;
  211. }
  212. }
  213. }
  214. }else{
  215. //部位为空 +1
  216. $buwei_score = 1+ $buwei_score;
  217. }
  218. //可能原因
  219. if(!empty($em['yy_id'])){
  220. $report_bw_id = Report::where(['yy_id'=>['in',$em['yy_id']]])->column('bw_id');
  221. if(!in_array($em['bw_id'],$report_bw_id)){ //现象不对,直接扣分
  222. $yuanyin_socre = 1+$yuanyin_socre;
  223. $yuanyin_arr[] = $em['yy_id'];
  224. }else{
  225. }
  226. }else{
  227. //部位为空 +1
  228. $yuanyin_socre = 1+$yuanyin_socre;
  229. }
  230. //排除方法
  231. if(!empty($em['pc_id'])){
  232. $report_pc_id = Report::where(['pc_id'=>['in',$em['pc_id']]])->column('bw_id');
  233. if(!in_array($em['bw_id'],$report_pc_id)){ //现象不对,直接扣分
  234. $fangfa_score = 1+$fangfa_score;
  235. $fangfa_arr[] = $em['pc_id'];
  236. }else{
  237. }
  238. }else{
  239. $fangfa_score = 1+$fangfa_score;
  240. }
  241. }
  242. }
  243. // echo "<pre>";
  244. // print_r($buwei_arr);
  245. // echo '原因:'.$yuanyin_socre.'<br/>';
  246. // echo '部位:'.$buwei_score.'<br/>';
  247. // echo '现象:'.$xianxian_score;
  248. // die();
  249. $weixiu_score = $xianxian_score+$yuanyin_socre+$buwei_score+$fangfa_score;
  250. if($weixiu_score>15){
  251. $weixiu_score = 15;
  252. }
  253. //是否超时
  254. $overtime_fen = intval((time()-$info['endtime']) / 60);
  255. if($overtime_fen>=10){
  256. $overtime_score = 10;
  257. }else if($overtime_fen>0 && $overtime_fen<10){
  258. $overtime_score = $overtime_fen ;
  259. }else{
  260. $overtime_score =0;
  261. }
  262. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$weixiu_score-$overtime_score;
  263. //更新考试结果表
  264. $params['exam_id'] = $ids;
  265. $params['total'] = $fault_score;//得分;
  266. $params['fault_one_score'] = $fault_one_score;//得分;
  267. $params['fault_two_score'] = $fault_two_score;//得分;
  268. $params['fault_three_score'] = $fault_three_score;//得分;
  269. $params['xianxian_score'] = $xianxian_score;//得分;
  270. $params['xianxian_content'] = !empty($xianxian_arr) ? json_encode($xianxian_arr):'';//错题;
  271. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  272. $params['yuanyin_content'] = !empty($yuanyin_arr) ? json_encode($yuanyin_arr) : '';//错题;
  273. $params['buwei_score'] = $buwei_score;//得分;
  274. $params['buwei_content'] = !empty($buwei_arr) ? json_encode($buwei_arr) :'';//错题;
  275. $params['fangfa_score'] = $fangfa_score;//得分;
  276. $params['fangfa_content'] = !empty($fangfa_arr) ? json_encode($fangfa_arr):'';//错题;
  277. // $params['jielun_score'] = $jielun_score;//得分;
  278. $params['overtime_score'] = $overtime_score;//得分;
  279. Db::name('real_exam_score')->insert($params);
  280. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  281. Db::commit();
  282. } catch (ValidateException|PDOException|Exception $e) {
  283. Db::rollback();
  284. $this->error($e->getMessage());
  285. }
  286. if ($result === false) {
  287. $this->error(__('No rows were inserted'));
  288. }
  289. $this->success('交卷成功','/ZQOtIMLKud.php/student/exercise/analysis/ids/'.$row['exam_id']);
  290. }
  291. $isloading =1;
  292. if(empty($row->other_replace)){
  293. $isloading = 0;
  294. $row->other_replace = '[{"fault_id":"","request_status":"0"}]';
  295. }
  296. $row->other_report_text = !empty($row->other_report) ? json_decode($row->other_report,true) : [];
  297. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  298. if(empty($row->starttime)){
  299. $timer = 60*$row->limit_duration;
  300. }else{
  301. $timer = $row->limit_duration*60 - abs(time() - $row->starttime);
  302. }
  303. $departmentdata = [];
  304. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->order('fault_id asc')->select();
  305. $this->view->assign('departmentdata', $departmentdata);
  306. //故障现象
  307. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  308. $this->view->assign('xianxiang', $xianxiang);
  309. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  310. $this->assignConfig('ids',$ids);
  311. $this->assignConfig('timer',$timer);
  312. $this->assignConfig('isloading',$isloading);
  313. $this->view->assign('row', $row);
  314. return $this->view->fetch();
  315. }
  316. public function analysis($ids = null)
  317. {
  318. if ($this->request->isPost()) {
  319. $params = $this->request->post('row/a');
  320. //计算总分到学员考试表
  321. $this->success('操作成功','/ZQOtIMLKud.php/student/exercise/index');
  322. }
  323. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  324. if (!$row) {
  325. $this->error(__('No Results were found'));
  326. }
  327. $rows = $this->exam_model->get($ids);
  328. $row['seat_id'] = $rows->seat_id;
  329. $row['user_nickname'] = $rows->user_nickname;
  330. $row['user_username'] = $rows->user_username;
  331. $row['user_depart_id'] = $rows->user_depart_id;
  332. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  333. $row['start_time'] = $rows->start_time;
  334. $row['end_time'] = $rows->end_time;
  335. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  336. $row['fault_name_one'] = !empty($fault_list[0]['fault_id']) ? Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name'):'';
  337. $row['fault_name_two'] = !empty($fault_list[1]['fault_id']) ? Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name'):'';
  338. $row['fault_name_three'] = !empty($fault_list[2]['fault_id']) ? Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name'):'';
  339. if($row['xianxian_score']>0 && empty($row['xianxian_content']))
  340. {
  341. $row['xianxian_content_name'] = '未选择';
  342. }else{
  343. $xianxian = Report::where(['xx_id'=>['in',json_decode($row['xianxian_content'],true)]])->group('xx_name')->column('xx_name');
  344. $row['xianxian_content_name'] = !empty($xianxian) ? implode(',',$xianxian) :'不存在或未选择';
  345. }
  346. if($row['yuanyin_socre']>0 && empty($row['yuanyin_content']))
  347. {
  348. $row['yuanyin_content_name'] = '未选择';
  349. }else{
  350. $yuanyin = Report::where(['yy_id'=>['in',json_decode($row['yuanyin_content'],true)]])->group('yy_name')->column('yy_name');
  351. $row['yuanyin_content_name'] = !empty($yuanyin) ? implode(',',$yuanyin) :'不存在或未选择';
  352. }
  353. if($row['buwei_score']>0 &&empty($row['buwei_content']))
  354. {
  355. $row['buwei_content_name'] = '未选择';
  356. }else{
  357. $buwei = Report::where(['bw_id'=>['in',json_decode($row['buwei_content'],true)]])->group('bw_name')->column('bw_name');
  358. $row['buwei_content_name'] = !empty($buwei) ? implode(',',$buwei) :'不存在或未选择';
  359. }
  360. if($row['fangfa_score']>0 && empty($row['fangfa_content']))
  361. {
  362. $row['fangfa_content_name'] = '未选择';
  363. }else{
  364. $fangfa = Report::where(['pc_id'=>['in',json_decode($row['fangfa_content'],true)]])->group('pc_name')->column('pc_name');
  365. $row['fangfa_content_name'] = !empty($fangfa) ? implode(',',$fangfa) :'不存在或未选择';
  366. }
  367. $this->view->assign('row', $row);
  368. return $this->view->fetch();
  369. }
  370. }