Exams.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. if($row['xianxian_score']>0 && empty($row['xianxian_content']))
  133. {
  134. $row['xianxian_content_name'] = '未选择';
  135. }else{
  136. $xianxian = Report::where(['xx_id'=>['in',json_decode($row['xianxian_content'],true)]])->column('xx_name');
  137. $row['xianxian_content_name'] = !empty($xianxian) ? implode(',',$xianxian) :'不存在或未选择';
  138. }
  139. if($row['yuanyin_socre']>0 && empty($row['yuanyin_content']))
  140. {
  141. $row['yuanyin_content_name'] = '未选择';
  142. }else{
  143. $yuanyin = Report::where(['yy_id'=>['in',json_decode($row['yuanyin_content'],true)]])->column('yy_name');
  144. $row['yuanyin_content_name'] = !empty($yuanyin) ? implode(',',$yuanyin) :'不存在或未选择';
  145. }
  146. if($row['buwei_score']>0 &&empty($row['buwei_content']))
  147. {
  148. $row['buwei_content_name'] = '未选择';
  149. }else{
  150. $buwei = Report::where(['bw_id'=>['in',json_decode($row['buwei_content'],true)]])->column('bw_name');
  151. $row['buwei_content_name'] = !empty($buwei) ? implode(',',$buwei) :'不存在或未选择';
  152. }
  153. if($row['fangfa_score']>0 && empty($row['fangfa_content']))
  154. {
  155. $row['fangfa_content_name'] = '未选择';
  156. }else{
  157. $fangfa = Report::where(['pc_id'=>['in',json_decode($row['fangfa_content'],true)]])->column('pc_name');
  158. $row['fangfa_content_name'] = !empty($fangfa) ? implode(',',$fangfa) :'不存在或未选择';
  159. }
  160. if (false === $this->request->isPost()) {
  161. $this->view->assign('row', $row);
  162. return $this->view->fetch();
  163. }
  164. $params = $this->request->post('row/a');
  165. if (empty($params)) {
  166. $this->error(__('Parameter %s can not be empty', ''));
  167. }
  168. $params = $this->preExcludeFields($params);
  169. $result = false;
  170. Db::startTrans();
  171. try {
  172. //是否采用模型验证
  173. $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'];
  174. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  175. $rows->total_score = $params['total'];
  176. $rows->is_sure = 1;
  177. $rows->save();
  178. Db::commit();
  179. } catch (ValidateException|PDOException|Exception $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. }
  183. if (false === $result) {
  184. $this->error(__('No rows were updated'));
  185. }
  186. $this->success();
  187. }
  188. public function view($ids = null)
  189. {
  190. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  191. if (!$row) {
  192. $this->error(__('No Results were found'));
  193. }
  194. $rows = $this->model->get($ids);
  195. $row['seat_id'] = $rows->seat_id;
  196. $row['user_nickname'] = $rows->user_nickname;
  197. $row['user_username'] = $rows->user_username;
  198. $row['user_depart_id'] = $rows->user_depart_id;
  199. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  200. $row['start_time'] = $rows->start_time;
  201. $row['end_time'] = $rows->end_time;
  202. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  203. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  204. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  205. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  206. if($row['xianxian_score']>0 && empty($row['xianxian_content']))
  207. {
  208. $row['xianxian_content_name'] = '未选择';
  209. }else{
  210. $xianxian = Report::where(['xx_id'=>['in',json_decode($row['xianxian_content'],true)]])->column('xx_name');
  211. $row['xianxian_content_name'] = !empty($xianxian) ? implode(',',$xianxian) :'不存在或未选择';
  212. }
  213. if($row['yuanyin_socre']>0 && empty($row['yuanyin_content']))
  214. {
  215. $row['yuanyin_content_name'] = '未选择';
  216. }else{
  217. $yuanyin = Report::where(['yy_id'=>['in',json_decode($row['yuanyin_content'],true)]])->column('yy_name');
  218. $row['yuanyin_content_name'] = !empty($yuanyin) ? implode(',',$yuanyin) :'不存在或未选择';
  219. }
  220. if($row['buwei_score']>0 &&empty($row['buwei_content']))
  221. {
  222. $row['buwei_content_name'] = '未选择';
  223. }else{
  224. $buwei = Report::where(['bw_id'=>['in',json_decode($row['buwei_content'],true)]])->column('bw_name');
  225. $row['buwei_content_name'] = !empty($buwei) ? implode(',',$buwei) :'不存在或未选择';
  226. }
  227. if($row['fangfa_score']>0 && empty($row['fangfa_content']))
  228. {
  229. $row['fangfa_content_name'] = '未选择';
  230. }else{
  231. $fangfa = Report::where(['pc_id'=>['in',json_decode($row['fangfa_content'],true)]])->column('pc_name');
  232. $row['fangfa_content_name'] = !empty($fangfa) ? implode(',',$fangfa) :'不存在或未选择';
  233. }
  234. $this->view->assign('row', $row);
  235. return $this->view->fetch();
  236. }
  237. //正在考试
  238. public function persent($ids = null)
  239. {
  240. //设置过滤方法
  241. $this->request->filter(['strip_tags', 'trim']);
  242. if (false === $this->request->isAjax()) {
  243. $this->assignConfig('ids', $ids);
  244. return $this->view->fetch();
  245. }
  246. //如果发送的来源是 Selectpage,则转发到 Selectpage
  247. if ($this->request->request('keyField')) {
  248. return $this->selectpage();
  249. }
  250. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  251. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  252. $list = Db::name('real_exam_comp_request')->where('exam_collection_type',3)
  253. ->where($where)->where('exam_id','in',$exam_ids)
  254. ->order($sort, $order)
  255. ->paginate($limit);
  256. unset($v);
  257. $result = ['total' => $list->total(), 'rows' => $list->items()];
  258. return json($result);
  259. }
  260. public function examing($ids = null)
  261. {
  262. //设置过滤方法
  263. $this->request->filter(['strip_tags', 'trim']);
  264. if (false === $this->request->isAjax()) {
  265. $this->assignConfig('ids', $ids);
  266. return $this->view->fetch();
  267. }
  268. //如果发送的来源是 Selectpage,则转发到 Selectpage
  269. if ($this->request->request('keyField')) {
  270. return $this->selectpage();
  271. }
  272. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  273. $list = $this->model
  274. ->where($where)->where('exam_collection_id',$ids)->where($this->whereExtend)->with($this->relationtTable)
  275. ->order($sort, $order)
  276. ->paginate($limit);
  277. $result = ['total' => $list->total(), 'rows' => $list->items()];
  278. return json($result);
  279. }
  280. public function score($ids = null)
  281. {
  282. //设置过滤方法
  283. $this->request->filter(['strip_tags', 'trim']);
  284. if (false === $this->request->isAjax()) {
  285. $this->assignConfig('ids', $ids);
  286. return $this->view->fetch();
  287. }
  288. //如果发送的来源是 Selectpage,则转发到 Selectpage
  289. if ($this->request->request('keyField')) {
  290. return $this->selectpage();
  291. }
  292. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  293. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  294. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  295. ->order($sort, $order)
  296. ->paginate($limit);
  297. foreach ($list as $k => $v){
  298. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  299. if($exam){
  300. $v->seat_id = $exam['seat_id'];
  301. $v->user_username = $exam['user_username'];
  302. $v->user_nickname = $exam['user_nickname'];
  303. }
  304. }
  305. unset($v);
  306. $result = ['total' => $list->total(), 'rows' => $list->items()];
  307. return json($result);
  308. }
  309. public function handle($type = null,$ids = null)
  310. {
  311. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  312. if(!$row){
  313. $this->error('未找到记录');
  314. }
  315. if($type ==1){
  316. $request_status = 2;
  317. }
  318. if($type==2)
  319. {
  320. $request_status= 3;
  321. }
  322. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  323. //同步更新考试表json串
  324. $exam_info = $this->model->get($row['exam_id']);
  325. if(!empty($exam_info)){
  326. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  327. foreach ($other_replace as $k=>$v){
  328. if($v['fault_id'] == $row['fault_id']){
  329. $other_replace[$k]['request_status'] = $request_status;
  330. }
  331. }
  332. $exam_info->other_replace = json_encode($other_replace);
  333. $exam_info->save();
  334. unset($v);
  335. }
  336. $this->success();
  337. }
  338. //啦
  339. public function editscore($ids = "")
  340. {
  341. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  342. if (!$row) {
  343. $this->error(__('No Results were found'));
  344. }
  345. $rows = $this->model->get($ids);
  346. $params = $this->request->post('row/a');
  347. if (empty($params)) {
  348. $this->error(__('Parameter %s can not be empty', ''));
  349. }
  350. $params = $this->preExcludeFields($params);
  351. $result = false;
  352. Db::startTrans();
  353. try {
  354. //是否采用模型验证
  355. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  356. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  357. $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'];;
  358. $rows->total_score = $total;
  359. $rows->is_sure = 1;
  360. $rows->save();
  361. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  362. Db::commit();
  363. } catch (ValidateException|PDOException|Exception $e) {
  364. Db::rollback();
  365. $this->error($e->getMessage());
  366. }
  367. if (false === $result) {
  368. $this->error(__('No rows were updated'));
  369. }
  370. $this->success();
  371. }
  372. }