Exercise.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 = [1,5,7];//array_rand($fault_list, 3);
  82. $fault_key1 = rand(0, 1);
  83. $fault_key2 = rand(2, 3);
  84. $fault_key3 = rand(4, 5);
  85. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  86. $flag = 0;
  87. if(!empty($fault_key) && in_array($key,$fault_key)){
  88. $flag = 1;
  89. }
  90. $add= [
  91. 'exam_id'=>$examid,
  92. 'fault_id'=>$item1['fault_id'],
  93. 'ref_type'=>2,
  94. 'flag'=>$flag,
  95. 'ref_state'=>0,
  96. 'createtime'=>time(),
  97. 'updatetime'=>time(),
  98. 'create_time'=>date('Y-m-d H:i:s'),
  99. 'update_time'=>date('Y-m-d H:i:s'),
  100. ];
  101. Db::name('real_exam_fault')->insert($add);
  102. }
  103. $count = $examid;
  104. Db::commit();
  105. } catch (PDOException|Exception $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. }
  109. if ($count) {
  110. $this->success('开始成功','/admin/student/exercise/examing/ids/'.$count);
  111. }
  112. $this->error(__('No rows were updated'));
  113. }
  114. }
  115. public function examing($ids = null)
  116. {
  117. $row = $this->exam_model->get($ids);
  118. if(!$row){
  119. $this->error('未找到记录');
  120. }
  121. if ($this->request->isPost()) {
  122. //先请求接口判断,再进行处理
  123. if(Env::get('app.is_fault')){
  124. $url = config('site.url_type').'/sim/real-exam/student/exam/submit/'.$ids;
  125. // /dev-api/sim/real-exam/student/exam/submit/{examId}
  126. $ret = json_decode(send_get($url),true);
  127. // halt($ret);
  128. if($ret['code']!=200){
  129. $this->error($ret['msg']);
  130. }
  131. }
  132. //计算分数,保存记录
  133. $params = $this->request->post('row/a');
  134. $result = false;
  135. $score = 100;
  136. $fault_one_score = 25;
  137. $fault_two_score = 25;
  138. $fault_three_score = 25;
  139. $xianxian_score = 0;
  140. $yuanyin_socre= 0;
  141. $buwei_score= 0;
  142. $fangfa_score= 0;
  143. $xianxian_arr = [];
  144. $yuanyin_arr = [];
  145. $buwei_arr = [];
  146. $fangfa_arr = [];
  147. $jielun_score = 0;
  148. Db::startTrans();
  149. try {
  150. //更新考试结束时间
  151. $row->end_time = date('Y-m-d H:i:s');
  152. $row->endtime = time();
  153. $row->exam_status = 5;
  154. $row->other_report = $params['other_report'];
  155. $result = $row->save();
  156. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  157. //更新故障是否正确
  158. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  159. if(!empty($fault_list)){
  160. foreach ($fault_list as $k =>$t){
  161. $answer_right = 2;
  162. if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
  163. if($t['sim_fault_question_value']!=$t['sim_fault_answer_value'] ){
  164. //真实故障id 000200010001
  165. if($t['sim_fault_answer_value']=='00000000' && $t['fault_id'] !='000200010001'){
  166. $answer_right=2;
  167. }
  168. $answer_right=1;
  169. }
  170. }
  171. Db::name('real_exam_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
  172. }
  173. }
  174. //计算得分,故障是否有扣分 扣分制
  175. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  176. if(!empty($fault_right_list)){
  177. if($fault_right_list[0]['answer_right']==1){
  178. $fault_one_score = 0;
  179. }
  180. if($fault_right_list[1]['answer_right']==1){
  181. $fault_two_score = 0;
  182. }
  183. if($fault_right_list[2]['answer_right']==1){
  184. $fault_three_score = 0;
  185. }
  186. }
  187. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  188. //根据故障部位,查找故障现象
  189. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  190. //根据故障部位查找,匹配的排除方法
  191. $paichu_arr = Report::where(['bw_id'=>['in',$fault_arr]])->column('pc_id');
  192. //根据故障部位查找,匹配可能原因
  193. $yy_arr = Report::where(['bw_id'=>['in',$fault_arr]])->column('yy_id');
  194. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr]])->column('parent_fault_id');
  195. //获取数组
  196. $other_report = json_decode($params['other_report'],true);
  197. if(!empty($other_report)){
  198. foreach ($other_report as $key => $em){
  199. //故障现象
  200. $xianxian_arr[] = $em['xx_id'];
  201. //故障部位
  202. $buwei_arr[] = $em['bw_id'];
  203. //可能原因
  204. $yuanyin_arr[] = $em['yy_id'];
  205. //排除方法
  206. $fangfa_arr[] = $em['pc_id'];
  207. }
  208. }
  209. //故障现象 分数 start
  210. foreach ($partent_fault_id as $k1 =>$t){
  211. if(!in_array($t,$xianxian_arr)){
  212. $xianxian_score = 1+$xianxian_score;
  213. $xianxian_content[] = [
  214. 'cx_id'=>$t,
  215. 'cx_type'=>'少写',
  216. 'cx_name'=>Fault::where(['fault_id'=>$t])->value('name'),
  217. ];
  218. }
  219. }
  220. foreach ($xianxian_arr as $k2 =>$t2){
  221. if(!in_array($t2,$partent_fault_id)){
  222. $xianxian_score = 1+$xianxian_score;
  223. $xianxian_content[] = [
  224. 'cx_id'=>$t2,
  225. 'cx_type'=>'错写',
  226. 'cx_name'=>Fault::where(['fault_id'=>$t2])->value('name'),
  227. ];
  228. }
  229. }
  230. //故障现象 end
  231. //故障部位 start
  232. foreach ($fault_arr as $k11 =>$t11){
  233. if(!in_array($t11,$buwei_arr)){
  234. $buwei_score = 1+$buwei_score;
  235. $buwei_content[] = [
  236. 'cx_id'=>$t11,
  237. 'cx_type'=>'少写',
  238. 'cx_name'=>Fault::where(['fault_id'=>$t11])->value('name'),
  239. ];
  240. }
  241. }
  242. foreach ($buwei_arr as $k12 =>$t12){
  243. if(!empty($t12) && !in_array($t12,$fault_arr)){
  244. //错写
  245. $buwei_score = 1+$buwei_score;
  246. $buwei_content[] = [
  247. 'cx_id'=>$t12,
  248. 'cx_type'=>'错写',
  249. 'cx_name'=>Fault::where(['fault_id'=>$t12])->value('name'),
  250. ];
  251. }
  252. }
  253. //故障部位 end
  254. //排除方法 start
  255. //循环固定的排除方法,匹配作答的排除方法
  256. foreach ($paichu_arr as $k21 =>$t21){
  257. if(!in_array($t21,$fangfa_arr)){
  258. $fangfa_score = 1+$fangfa_score;
  259. $fangfa_content[] = [
  260. 'cx_id'=>$t21,
  261. 'cx_type'=>'少写',
  262. 'cx_name'=>Fault::where(['fault_id'=>$t21])->value('name'),
  263. ];
  264. }
  265. }
  266. //循环作答的排除方法,,匹配固定的排除方法
  267. foreach ($fangfa_arr as $k22 =>$t22){
  268. if(!empty($t22) && !in_array($t22,$paichu_arr)){
  269. $fangfa_score = 1+$fangfa_score;
  270. $fangfa_content[] = [
  271. 'cx_id'=>$t22,
  272. 'cx_type'=>'错写',
  273. 'cx_name'=>Fault::where(['fault_id'=>$t22])->value('name'),
  274. ];
  275. }
  276. }
  277. //排除方法 end
  278. //可能原因 start
  279. //循环固定的可能原因,匹配作答的可能原因
  280. foreach ($yy_arr as $k31 =>$t31){
  281. if(!in_array($t31,$yuanyin_arr)){
  282. $yuanyin_socre = 1+$yuanyin_socre;
  283. $yuanyin_content[] = [
  284. 'cx_id'=>$t31,
  285. 'cx_type'=>'少写',
  286. 'cx_name'=>Fault::where(['fault_id'=>$t31])->value('name'),
  287. ];
  288. }
  289. }
  290. //循环作答的排除方法,,匹配固定的排除方法
  291. foreach ($yuanyin_arr as $k32 =>$t32){
  292. if(!empty($t32) && !in_array($t32,$yy_arr)){
  293. $yuanyin_socre = 1+$yuanyin_socre;
  294. $yuanyin_content[] = [
  295. 'cx_id'=>$t32,
  296. 'cx_type'=>'错写',
  297. 'cx_name'=>Fault::where(['fault_id'=>$t32])->value('name'),
  298. ];
  299. }
  300. }
  301. //可能原因 end
  302. $weixiu_score = $xianxian_score+$yuanyin_socre+$buwei_score+$fangfa_score;
  303. if($weixiu_score>15){
  304. $weixiu_score = 15;
  305. }
  306. //是否超时
  307. $overtime_fen = intval((time()-$info['endtime']) / 60);
  308. if($overtime_fen>=10){
  309. $overtime_score = 10;
  310. }else if($overtime_fen>0 && $overtime_fen<10){
  311. $overtime_score = $overtime_fen ;
  312. }else{
  313. $overtime_score =0;
  314. }
  315. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$weixiu_score-$overtime_score;
  316. //更新考试结果表
  317. $params['exam_id'] = $ids;
  318. $params['total'] = $fault_score;//得分;
  319. $params['fault_one_score'] = $fault_one_score;//得分;
  320. $params['fault_two_score'] = $fault_two_score;//得分;
  321. $params['fault_three_score'] = $fault_three_score;//得分;
  322. $params['xianxian_score'] = $xianxian_score;//得分;
  323. $params['xianxian_content'] = json_encode($xianxian_content);//错题;
  324. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  325. $params['yuanyin_content'] = json_encode($yuanyin_content);//错题;
  326. $params['buwei_score'] = $buwei_score;//得分;
  327. $params['buwei_content'] = json_encode($buwei_content);//错题;
  328. $params['fangfa_score'] = $fangfa_score;//得分;
  329. $params['fangfa_content'] = json_encode($fangfa_content);//错题;
  330. //
  331. $params['overtime_score'] = $overtime_score;//得分;
  332. Db::name('real_exam_score')->insert($params);
  333. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  334. Db::commit();
  335. } catch (ValidateException|PDOException|Exception $e) {
  336. Db::rollback();
  337. $this->error($e->getMessage());
  338. }
  339. if ($result === false) {
  340. $this->error(__('No rows were inserted'));
  341. }
  342. $this->success('交卷成功','/admin/student/exercise/analysis/ids/'.$row['exam_id']);
  343. }
  344. $isloading =1;
  345. if(empty($row->other_replace)){
  346. $isloading = 0;
  347. $row->other_replace = '[{"fault_id":"","request_status":"0"}]';
  348. }
  349. $row->other_report_text = !empty($row->other_report) ? json_decode($row->other_report,true) : [];
  350. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  351. if(empty($row->starttime)){
  352. $timer = 60*$row->limit_duration;
  353. }else{
  354. $timer = $row->limit_duration*60 - abs(time() - $row->starttime);
  355. }
  356. $departmentdata = [];
  357. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->order('fault_id asc')->select();
  358. $this->view->assign('departmentdata', $departmentdata);
  359. //故障现象
  360. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  361. $this->view->assign('xianxiang', $xianxiang);
  362. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  363. $this->assignConfig('ids',$ids);
  364. $this->assignConfig('timer',$timer);
  365. $this->assignConfig('isloading',$isloading);
  366. $this->view->assign('row', $row);
  367. return $this->view->fetch();
  368. }
  369. public function analysis($ids = null)
  370. {
  371. if ($this->request->isPost()) {
  372. $params = $this->request->post('row/a');
  373. //计算总分到学员考试表
  374. $this->success('操作成功','/admin/student/exercise/index');
  375. }
  376. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  377. if (!$row) {
  378. $this->error(__('No Results were found'));
  379. }
  380. $rows = $this->exam_model->get($ids);
  381. $row['seat_id'] = $rows->seat_id;
  382. $row['user_nickname'] = $rows->user_nickname;
  383. $row['user_username'] = $rows->user_username;
  384. $row['user_depart_id'] = $rows->user_depart_id;
  385. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  386. $row['start_time'] = $rows->start_time;
  387. $row['end_time'] = $rows->end_time;
  388. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  389. $row['fault_name_one'] = !empty($fault_list[0]['fault_id']) ? Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name'):'';
  390. $row['fault_name_two'] = !empty($fault_list[1]['fault_id']) ? Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name'):'';
  391. $row['fault_name_three'] = !empty($fault_list[2]['fault_id']) ? Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name'):'';
  392. $row['xianxian_content_name'] = '';
  393. if(!empty($row['xianxian_content']))
  394. {
  395. $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
  396. }
  397. $row['yuanyin_content_name'] = '';
  398. if(!empty($row['yuanyin_content']))
  399. {
  400. $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
  401. }
  402. $row['buwei_content_name'] = '';
  403. if(!empty($row['buwei_content']))
  404. {
  405. $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
  406. }
  407. $row['fangfa_content_name'] = '';
  408. if(!empty($row['fangfa_content']))
  409. {
  410. $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
  411. }
  412. $other_report = !empty($row['other_report']) ? json_decode($row['other_report'],true):[];
  413. $this->view->assign('other_report', $other_report);
  414. $this->view->assign('row', $row);
  415. return $this->view->fetch();
  416. }
  417. }