Exams.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. $one_static = $one;
  80. }
  81. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  82. if($val['sim_type']=='0002'){
  83. foreach ($two as $k2 => $val2){
  84. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  85. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  86. $two[$k2]['epercent'] = '';
  87. if($two_total>0){
  88. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  89. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  90. }
  91. }
  92. }
  93. $two_static = $two;
  94. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  95. if($val['sim_type']=='0003'){
  96. foreach ($three as $k3 => $val3){
  97. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  98. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  99. $three[$k3]['epercent'] = '';
  100. if($three_total>0){
  101. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  102. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  103. }
  104. }
  105. }
  106. $three_static = $three;
  107. }
  108. //统计错误率
  109. $statistics = [
  110. 'one_static' => $one_static,
  111. 'two_static' => $two_static,
  112. 'three_static' => $three_static,
  113. ];
  114. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  115. return json($result);
  116. }
  117. public function edit($ids = null)
  118. {
  119. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  120. if (!$row) {
  121. $this->error(__('No Results were found'));
  122. }
  123. $rows = $this->model->get($ids);
  124. $row['seat_id'] = $rows->seat_id;
  125. $row['user_nickname'] = $rows->user_nickname;
  126. $row['user_username'] = $rows->user_username;
  127. $row['is_sure'] = $rows->is_sure;
  128. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  129. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  130. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  131. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  132. $row['xianxian_content_name'] = '';
  133. if(!empty($row['xianxian_content']))
  134. {
  135. $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
  136. }
  137. $row['yuanyin_content_name'] = '';
  138. if(!empty($row['yuanyin_content']))
  139. {
  140. $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
  141. }
  142. $row['buwei_content_name'] = '';
  143. if(!empty($row['buwei_content']))
  144. {
  145. $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
  146. }
  147. $row['fangfa_content_name'] = '';
  148. if(!empty($row['fangfa_content']))
  149. {
  150. $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
  151. }
  152. $other_report = !empty($row['other_report']) ? json_decode($row['other_report'],true):[];
  153. if (false === $this->request->isPost()) {
  154. $this->view->assign('other_report', $other_report);
  155. $this->view->assign('row', $row);
  156. return $this->view->fetch();
  157. }
  158. $params = $this->request->post('row/a');
  159. if (empty($params)) {
  160. $this->error(__('Parameter %s can not be empty', ''));
  161. }
  162. $params = $this->preExcludeFields($params);
  163. $result = false;
  164. Db::startTrans();
  165. try {
  166. //是否采用模型验证
  167. $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'];
  168. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  169. $rows->total_score = $params['total'];
  170. $rows->is_sure = 1;
  171. $rows->save();
  172. Db::commit();
  173. } catch (ValidateException|PDOException|Exception $e) {
  174. Db::rollback();
  175. $this->error($e->getMessage());
  176. }
  177. if (false === $result) {
  178. $this->error(__('No rows were updated'));
  179. }
  180. $this->success();
  181. }
  182. public function view($ids = null)
  183. {
  184. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  185. if (!$row) {
  186. $this->error(__('No Results were found'));
  187. }
  188. $rows = $this->model->get($ids);
  189. $row['seat_id'] = $rows->seat_id;
  190. $row['user_nickname'] = $rows->user_nickname;
  191. $row['user_username'] = $rows->user_username;
  192. $row['user_depart_id'] = $rows->user_depart_id;
  193. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  194. $row['start_time'] = $rows->start_time;
  195. $row['end_time'] = $rows->end_time;
  196. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  197. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  198. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  199. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  200. $row['xianxian_content_name'] = '';
  201. if(!empty($row['xianxian_content']))
  202. {
  203. $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
  204. }
  205. $row['yuanyin_content_name'] = '';
  206. if(!empty($row['yuanyin_content']))
  207. {
  208. $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
  209. }
  210. $row['buwei_content_name'] = '';
  211. if(!empty($row['buwei_content']))
  212. {
  213. $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
  214. }
  215. $row['fangfa_content_name'] = '';
  216. if(!empty($row['fangfa_content']))
  217. {
  218. $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
  219. }
  220. $other_report = !empty($row['other_report']) ? json_decode($row['other_report'],true):[];
  221. $this->view->assign('other_report', $other_report);
  222. $this->view->assign('row', $row);
  223. return $this->view->fetch();
  224. }
  225. //正在考试
  226. public function persent($ids = null)
  227. {
  228. //设置过滤方法
  229. $this->request->filter(['strip_tags', 'trim']);
  230. if (false === $this->request->isAjax()) {
  231. $this->assignConfig('ids', $ids);
  232. return $this->view->fetch();
  233. }
  234. //如果发送的来源是 Selectpage,则转发到 Selectpage
  235. if ($this->request->request('keyField')) {
  236. return $this->selectpage();
  237. }
  238. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  239. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  240. $list = Db::name('real_exam_comp_request')
  241. // ->where('exam_collection_type',3)
  242. ->where($where)->where('exam_id','in',$exam_ids)
  243. ->order($sort, $order)
  244. ->paginate($limit);
  245. unset($v);
  246. $result = ['total' => $list->total(), 'rows' => $list->items()];
  247. return json($result);
  248. }
  249. public function examing($ids = null)
  250. {
  251. //设置过滤方法
  252. $this->request->filter(['strip_tags', 'trim']);
  253. if (false === $this->request->isAjax()) {
  254. $this->assignConfig('ids', $ids);
  255. return $this->view->fetch();
  256. }
  257. //如果发送的来源是 Selectpage,则转发到 Selectpage
  258. if ($this->request->request('keyField')) {
  259. return $this->selectpage();
  260. }
  261. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  262. // $list = $this->model
  263. // ->where($where)->where('exam_collection_id',$ids)
  264. // ->where($this->whereExtend)->with($this->relationtTable)
  265. // ->order($sort, $order)
  266. // ->paginate($limit);
  267. $sim_type = Db::name('real_exam_collection')->where('exam_collection_id',$ids)->value('sim_type');
  268. $list = Db::name('seat')->select();
  269. foreach ($list as $key => $value) {
  270. $sim = Db::name('sim')->where('seat_id',$value['seat_id'])->where('sim_type',$sim_type)->find();
  271. if(!empty($sim['sim_state'])){
  272. switch ($sim['sim_state']) {
  273. case '1':
  274. $sim_state = '在线';
  275. break;
  276. case '2':
  277. $sim_state = '离线';
  278. break;
  279. default:
  280. $sim_state = '';
  281. break;
  282. }
  283. }else{
  284. $sim_state = '';
  285. }
  286. $exam = $this->model->where('exam_collection_id',$ids)->where('seat_id',$value['seat_id'])->find();
  287. $user_username = '';
  288. $user_nickname = '';
  289. $exam_status = '未登录';
  290. $total_score = 0;
  291. $fault_names = '';
  292. $exam_id = 0;
  293. if(!empty($exam)){
  294. $user_username = $exam['user_username']??'';
  295. $user_nickname = $exam['user_nickname']??'';
  296. if($exam['exam_status']==1){
  297. $exam_status= '已登录未开始考试';
  298. }else if($exam['exam_status']==4){
  299. $exam_status= '已开始考试';
  300. }else if($exam['exam_status']==5){
  301. $exam_status= '已交卷';
  302. }
  303. $total_score = $exam['total_score']??'';
  304. $fault_names = $exam['fault_names']??'';
  305. $exam_id = $exam['exam_id']??0;
  306. }
  307. $list[$key]['user_username'] = $user_username;
  308. $list[$key]['user_nickname'] = $user_nickname;
  309. $list[$key]['exam_status'] = $exam_status;
  310. $list[$key]['total_score'] = $total_score;
  311. $list[$key]['fault_names'] = $fault_names;
  312. $list[$key]['exam_id'] = $exam_id;
  313. $list[$key]['sim_state'] = $sim_state;
  314. }
  315. $result = ['total' => count($list), 'rows' => $list];
  316. return json($result);
  317. }
  318. public function score($ids = null)
  319. {
  320. //设置过滤方法
  321. $this->request->filter(['strip_tags', 'trim']);
  322. if (false === $this->request->isAjax()) {
  323. $this->assignConfig('ids', $ids);
  324. return $this->view->fetch();
  325. }
  326. //如果发送的来源是 Selectpage,则转发到 Selectpage
  327. if ($this->request->request('keyField')) {
  328. return $this->selectpage();
  329. }
  330. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  331. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  332. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  333. ->order($sort, $order)
  334. ->paginate($limit);
  335. foreach ($list as $k => $v){
  336. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  337. if($exam){
  338. $v->seat_id = $exam['seat_id'];
  339. $v->user_username = $exam['user_username'];
  340. $v->user_nickname = $exam['user_nickname'];
  341. }
  342. }
  343. unset($v);
  344. $result = ['total' => $list->total(), 'rows' => $list->items()];
  345. return json($result);
  346. }
  347. public function handle($type = null,$ids = null)
  348. {
  349. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  350. if(!$row){
  351. $this->error('未找到记录');
  352. }
  353. if($type ==1){
  354. $request_status = 2;
  355. }
  356. if($type==2)
  357. {
  358. $request_status= 3;
  359. }
  360. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  361. //同步更新考试表json串
  362. $exam_info = $this->model->get($row['exam_id']);
  363. if(!empty($exam_info)){
  364. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  365. foreach ($other_replace as $k=>$v){
  366. if($v['fault_id'] == $row['fault_id']){
  367. $other_replace[$k]['request_status'] = $request_status;
  368. }
  369. }
  370. $exam_info->other_replace = json_encode($other_replace);
  371. $exam_info->save();
  372. unset($v);
  373. }
  374. $this->success();
  375. }
  376. //啦
  377. public function editscore($ids = "")
  378. {
  379. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  380. if (!$row) {
  381. $this->error(__('No Results were found'));
  382. }
  383. $rows = $this->model->get($ids);
  384. $params = $this->request->post('row/a');
  385. if (empty($params)) {
  386. $this->error(__('Parameter %s can not be empty', ''));
  387. }
  388. $params = $this->preExcludeFields($params);
  389. $result = false;
  390. Db::startTrans();
  391. try {
  392. //是否采用模型验证
  393. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  394. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  395. $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'];;
  396. $rows->total_score = $total;
  397. $rows->is_sure = 1;
  398. $rows->save();
  399. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  400. Db::commit();
  401. } catch (ValidateException|PDOException|Exception $e) {
  402. Db::rollback();
  403. $this->error($e->getMessage());
  404. }
  405. if (false === $result) {
  406. $this->error(__('No rows were updated'));
  407. }
  408. $this->success();
  409. }
  410. }