Exams.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\admin\model\department\Department;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\teacher\ExamsScore;
  9. use app\admin\model\Fault;
  10. use app\admin\model\Report;
  11. /**
  12. * sim-考试表/成绩总分
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Exams extends Backend
  17. {
  18. /**
  19. * Exams模型对象
  20. * @var \app\admin\model\teacher\Exams
  21. */
  22. protected $model = null;
  23. protected $whereExtend = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\teacher\Exams;
  28. $this->whereExtend['exam_collection_type'] = 3;
  29. $this->whereExtend['starttime'] = ['>',0];
  30. $this->whereExtend['endtime'] = ['>',0];
  31. $this->relationtTable = 'collection';
  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. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $list = $this->model
  51. ->where($where)->where($this->whereExtend)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. //最高分 最低分 平均分
  55. $select = $this->model->where($where)->where($this->whereExtend)->field('count(*) as count,sum(total_score) as totalscore,min(total_score) as min,max(total_score) as max')->select();
  56. $pingfen = '';
  57. if(!empty($select[0]['totalscore']) && !empty($select[0]['count'])){
  58. $pingfen = bcdiv($select[0]['totalscore'],$select[0]['count']);
  59. }
  60. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  61. $one_static = [];
  62. $two_static = [];
  63. $three_static = [];
  64. $where_falut = ['fault_type' => 3,'fault_state'=>0];
  65. $exam_ids = $this->model->where($where)->where($this->whereExtend)->column('exam_id');
  66. foreach ($list as $k => $val){
  67. $one = Fault::where(['sim_type'=>'0001'])->where($where_falut)->field('fault_id,name')->select();
  68. if($val['sim_type']=='0001'){
  69. foreach ($one as $k1 => $val1){
  70. //先查分配次数,在查错误率
  71. $one_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id']])->count();
  72. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id'],'answer_right'=>2])->count();
  73. $one[$k1]['epercent'] = '';
  74. if($one_total>0){
  75. $bili = bcmul(bcdiv($error_total,$one_total,2),100);
  76. $one[$k1]['epercent'] = $bili>0 ? $bili.'%' : '';
  77. }
  78. }
  79. $vaccineCount = array_column($one, 'epercent');
  80. array_multisort($vaccineCount,SORT_DESC,$one);
  81. $one_static = $one;
  82. }
  83. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  84. if($val['sim_type']=='0002'){
  85. foreach ($two as $k2 => $val2){
  86. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  87. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  88. $two[$k2]['epercent'] = '';
  89. if($two_total>0){
  90. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  91. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  92. }
  93. }
  94. $vaccineCount1 = array_column($two, 'epercent');
  95. array_multisort($vaccineCount1,SORT_DESC,$two);
  96. }
  97. $two_static = $two;
  98. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  99. if($val['sim_type']=='0003'){
  100. foreach ($three as $k3 => $val3){
  101. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  102. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  103. $three[$k3]['epercent'] = '';
  104. if($three_total>0){
  105. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  106. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  107. }
  108. }
  109. $vaccineCount2 = array_column($three, 'epercent');
  110. array_multisort($vaccineCount2,SORT_DESC,$three);
  111. }
  112. $three_static = $three;
  113. }
  114. //统计错误率
  115. $statistics = [
  116. 'one_static' => $one_static,
  117. 'two_static' => $two_static,
  118. 'three_static' => $three_static,
  119. ];
  120. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  121. return json($result);
  122. }
  123. public function edit($ids = null)
  124. {
  125. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  126. if (!$row) {
  127. $this->error(__('No Results were found'));
  128. }
  129. $rows = $this->model->get($ids);
  130. $row['seat_id'] = $rows->seat_id;
  131. $row['user_nickname'] = $rows->user_nickname;
  132. $row['user_username'] = $rows->user_username;
  133. $row['is_sure'] = $rows->is_sure;
  134. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  135. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  136. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  137. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  138. $row['xianxian_content_name'] = '';
  139. if(!empty($row['xianxian_content']))
  140. {
  141. $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
  142. }
  143. $row['yuanyin_content_name'] = '';
  144. if(!empty($row['yuanyin_content']))
  145. {
  146. $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
  147. }
  148. $row['buwei_content_name'] = '';
  149. if(!empty($row['buwei_content']))
  150. {
  151. $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
  152. }
  153. $row['fangfa_content_name'] = '';
  154. if(!empty($row['fangfa_content']))
  155. {
  156. $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
  157. }
  158. $other_report = !empty($row['other_report']) ? json_decode($row['other_report'],true):[];
  159. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  160. //根据故障部位 数组
  161. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  162. //查找故障现象 数组
  163. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr],'sim_type'=>$rows['sim_type']])->column('parent_fault_id');
  164. $arr1= [];
  165. foreach ($partent_fault_id as $key => $value) {
  166. $arr1[] = [
  167. 'gzxz_id' => $value,
  168. 'xianxian_content' => $row['xianxian_content_name'],
  169. 'other_report' => $other_report
  170. ];
  171. }
  172. if (false === $this->request->isPost()) {
  173. $this->view->assign('partent_fault_id', $arr1);
  174. $this->view->assign('other_report', $other_report);
  175. $this->view->assign('row', $row);
  176. return $this->view->fetch();
  177. }
  178. $params = $this->request->post('row/a');
  179. if (empty($params)) {
  180. $this->error(__('Parameter %s can not be empty', ''));
  181. }
  182. $params = $this->preExcludeFields($params);
  183. $result = false;
  184. Db::startTrans();
  185. try {
  186. //是否采用模型验证
  187. $params['total'] = 100-$params['fault_one_score']-$params['fault_two_score']-$params['fault_three_score']-$params['xianxian_score']-$params['yuanyin_socre']-$params['buwei_score']-$params['fangfa_score']-$params['overtime_score'];
  188. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  189. $rows->total_score = $params['total'];
  190. $rows->is_sure = 1;
  191. $rows->save();
  192. Db::commit();
  193. } catch (ValidateException|PDOException|Exception $e) {
  194. Db::rollback();
  195. $this->error($e->getMessage());
  196. }
  197. if (false === $result) {
  198. $this->error(__('No rows were updated'));
  199. }
  200. $this->success();
  201. }
  202. public function view($ids = null)
  203. {
  204. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  205. if (!$row) {
  206. $this->error(__('No Results were found'));
  207. }
  208. $rows = $this->model->get($ids);
  209. $row['seat_id'] = $rows->seat_id;
  210. $row['user_nickname'] = $rows->user_nickname;
  211. $row['user_username'] = $rows->user_username;
  212. $row['user_depart_id'] = $rows->user_depart_id;
  213. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  214. $row['start_time'] = $rows->start_time;
  215. $row['end_time'] = $rows->end_time;
  216. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  217. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  218. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  219. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  220. $row['fault_score'] = 75-$row['fault_one_score']-$row['fault_two_score']-$row['fault_three_score'];
  221. $report_score = 15-$row['xianxian_score']-$row['yuanyin_socre']-$row['buwei_score']-$row['fangfa_score'];
  222. $row['report_score'] = $report_score>0?$report_score:0;
  223. $row['weixiu_score'] = 10-$row['overtime_score']??0;
  224. $row['xianxian_content_name'] = '';
  225. if(!empty($row['xianxian_content']))
  226. {
  227. $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
  228. }
  229. $row['yuanyin_content_name'] = '';
  230. if(!empty($row['yuanyin_content']))
  231. {
  232. $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
  233. }
  234. $row['buwei_content_name'] = '';
  235. if(!empty($row['buwei_content']))
  236. {
  237. $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
  238. }
  239. $row['fangfa_content_name'] = '';
  240. if(!empty($row['fangfa_content']))
  241. {
  242. $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
  243. }
  244. $other_report = !empty($row['other_report']) ? json_decode($row['other_report'],true):[];
  245. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  246. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  247. //根据故障部位 数组
  248. $fault_arr = [$fault_right_list[0]['fault_id'],$fault_right_list[1]['fault_id'],$fault_right_list[2]['fault_id']];
  249. //查找故障现象 数组
  250. $partent_fault_id = Fault::where(['fault_id'=>['in',$fault_arr],'sim_type'=>$rows['sim_type']])->column('parent_fault_id');
  251. $arr1= [];
  252. foreach ($partent_fault_id as $key => $value) {
  253. $arr1[] = [
  254. 'gzxz_id' => $value,
  255. 'xianxian_content' => $row['xianxian_content_name'],
  256. 'other_report' => $other_report,
  257. 'other_jielun' => $other_jielun,
  258. ];
  259. }
  260. $this->view->assign('other_report', $other_report);
  261. $this->view->assign('partent_fault_id', $arr1);
  262. $this->view->assign('row', $row);
  263. return $this->view->fetch();
  264. }
  265. //正在考试
  266. public function persent($ids = null)
  267. {
  268. //设置过滤方法
  269. $this->request->filter(['strip_tags', 'trim']);
  270. if (false === $this->request->isAjax()) {
  271. $this->assignConfig('ids', $ids);
  272. return $this->view->fetch();
  273. }
  274. //如果发送的来源是 Selectpage,则转发到 Selectpage
  275. if ($this->request->request('keyField')) {
  276. return $this->selectpage();
  277. }
  278. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  279. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  280. $list = Db::name('real_exam_comp_request')
  281. // ->where('exam_collection_type',3)
  282. ->where($where)->where('exam_id','in',$exam_ids)
  283. ->order($sort, $order)
  284. ->paginate($limit);
  285. unset($v);
  286. $result = ['total' => $list->total(), 'rows' => $list->items()];
  287. return json($result);
  288. }
  289. public function examing($ids = null)
  290. {
  291. //设置过滤方法
  292. $this->request->filter(['strip_tags', 'trim']);
  293. if (false === $this->request->isAjax()) {
  294. $this->assignConfig('ids', $ids);
  295. return $this->view->fetch();
  296. }
  297. //如果发送的来源是 Selectpage,则转发到 Selectpage
  298. if ($this->request->request('keyField')) {
  299. return $this->selectpage();
  300. }
  301. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  302. // $list = $this->model
  303. // ->where($where)->where('exam_collection_id',$ids)
  304. // ->where($this->whereExtend)->with($this->relationtTable)
  305. // ->order($sort, $order)
  306. // ->paginate($limit);
  307. $sim_type = Db::name('real_exam_collection')->where('exam_collection_id',$ids)->value('sim_type');
  308. $list = Db::name('seat')->select();
  309. foreach ($list as $key => $value) {
  310. $sim = Db::name('sim')->where('seat_id',$value['seat_id'])->where('sim_type',$sim_type)->find();
  311. if(!empty($sim['sim_state'])){
  312. switch ($sim['sim_state']) {
  313. case '1':
  314. $sim_state = '在线';
  315. break;
  316. case '2':
  317. $sim_state = '离线';
  318. break;
  319. default:
  320. $sim_state = '';
  321. break;
  322. }
  323. }else{
  324. $sim_state = '';
  325. }
  326. $exam = $this->model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_id'])->find();
  327. $user_username = '';
  328. $user_nickname = '';
  329. $exam_status = '未登录';
  330. $total_score = 0;
  331. $fault_names = '';
  332. $exam_id = 0;
  333. if(!empty($exam)){
  334. $user_username = $exam['user_username']??'';
  335. $user_nickname = $exam['user_nickname']??'';
  336. if($exam['exam_status']==1){
  337. $exam_status= '已登录未开始考试';
  338. }else if($exam['exam_status']==4){
  339. $exam_status= '已开始考试';
  340. }else if($exam['exam_status']==5){
  341. $exam_status= '已交卷';
  342. }
  343. $total_score = $exam['total_score']??'';
  344. $fault_names = $exam['fault_names']??'';
  345. $exam_id = $exam['exam_id']??0;
  346. }
  347. $list[$key]['user_username'] = $user_username;
  348. $list[$key]['user_nickname'] = $user_nickname;
  349. $list[$key]['exam_status'] = $exam_status;
  350. $list[$key]['total_score'] = $total_score;
  351. $list[$key]['fault_names'] = $fault_names;
  352. $list[$key]['exam_id'] = $exam_id;
  353. $list[$key]['sim_state'] = $sim_state;
  354. }
  355. $result = ['total' => count($list), 'rows' => $list];
  356. return json($result);
  357. }
  358. public function score($ids = null)
  359. {
  360. //设置过滤方法
  361. $this->request->filter(['strip_tags', 'trim']);
  362. if (false === $this->request->isAjax()) {
  363. $this->assignConfig('ids', $ids);
  364. return $this->view->fetch();
  365. }
  366. //如果发送的来源是 Selectpage,则转发到 Selectpage
  367. if ($this->request->request('keyField')) {
  368. return $this->selectpage();
  369. }
  370. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  371. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  372. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  373. ->order($sort, $order)
  374. ->paginate($limit);
  375. foreach ($list as $k => $v){
  376. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  377. if($exam){
  378. $v->seat_id = $exam['seat_id'];
  379. $v->user_username = $exam['user_username'];
  380. $v->user_nickname = $exam['user_nickname'];
  381. }
  382. }
  383. unset($v);
  384. $result = ['total' => $list->total(), 'rows' => $list->items()];
  385. return json($result);
  386. }
  387. public function handle($type = null,$ids = null)
  388. {
  389. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  390. if(!$row){
  391. $this->error('未找到记录');
  392. }
  393. if($type ==1){
  394. $request_status = 2;
  395. }
  396. if($type==2)
  397. {
  398. $request_status= 3;
  399. }
  400. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  401. //同步更新考试表json串
  402. $exam_info = $this->model->get($row['exam_id']);
  403. if(!empty($exam_info)){
  404. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  405. foreach ($other_replace as $k=>$v){
  406. if($v['fault_id'] == $row['fault_id']){
  407. $other_replace[$k]['request_status'] = $request_status;
  408. }
  409. }
  410. $exam_info->other_replace = json_encode($other_replace);
  411. $exam_info->save();
  412. unset($v);
  413. }
  414. $this->success();
  415. }
  416. //啦
  417. public function editscore($ids = "")
  418. {
  419. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  420. if (!$row) {
  421. $this->error(__('No Results were found'));
  422. }
  423. $rows = $this->model->get($ids);
  424. $params = $this->request->post('row/a');
  425. if (empty($params)) {
  426. $this->error(__('Parameter %s can not be empty', ''));
  427. }
  428. $params = $this->preExcludeFields($params);
  429. $result = false;
  430. Db::startTrans();
  431. try {
  432. //是否采用模型验证
  433. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  434. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  435. $total =100-$info['fault_one_score']-$info['fault_two_score']-$info['fault_three_score']-$info['xianxian_score']-$info['yuanyin_socre']-$info['buwei_score']-$info['fangfa_score']-$info['overtime_score']-$info['jielun_score'];;
  436. $rows->total_score = $total;
  437. $rows->is_sure = 1;
  438. $rows->save();
  439. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  440. Db::commit();
  441. } catch (ValidateException|PDOException|Exception $e) {
  442. Db::rollback();
  443. $this->error($e->getMessage());
  444. }
  445. if (false === $result) {
  446. $this->error(__('No rows were updated'));
  447. }
  448. $this->success();
  449. }
  450. }