Exams.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /**
  11. * sim-考试表/成绩总分
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Exams extends Backend
  16. {
  17. /**
  18. * Exams模型对象
  19. * @var \app\admin\model\teacher\Exams
  20. */
  21. protected $model = null;
  22. protected $whereExtend = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\teacher\Exams;
  27. $this->whereExtend['exam_collection_type'] = 3;
  28. $this->whereExtend['starttime'] = ['>',0];
  29. $this->whereExtend['endtime'] = ['>',0];
  30. $this->relationtTable = 'collection';
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if (false === $this->request->isAjax()) {
  42. return $this->view->fetch();
  43. }
  44. //如果发送的来源是 Selectpage,则转发到 Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  49. $list = $this->model
  50. ->where($where)->where($this->whereExtend)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. //最高分 最低分 平均分
  54. $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();
  55. $pingfen = '';
  56. if(!empty($select[0]['totalscore']) && !empty($select[0]['count'])){
  57. $pingfen = bcdiv($select[0]['totalscore'],$select[0]['count']);
  58. }
  59. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  60. $one_static = [];
  61. $two_static = [];
  62. $three_static = [];
  63. $where_falut = ['fault_type' => 3,'fault_state'=>0];
  64. $exam_ids = $this->model->where($where)->where($this->whereExtend)->column('exam_id');
  65. foreach ($list as $k => $val){
  66. $one = Fault::where(['sim_type'=>'0001'])->where($where_falut)->field('fault_id,name')->select();
  67. if($val['sim_type']=='0001'){
  68. foreach ($one as $k1 => $val1){
  69. //先查分配次数,在查错误率
  70. $one_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id']])->count();
  71. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id'],'answer_right'=>2])->count();
  72. $one[$k1]['epercent'] = '';
  73. if($one_total>0){
  74. $bili = bcmul(bcdiv($error_total,$one_total,2),100);
  75. $one[$k1]['epercent'] = $bili>0 ? $bili.'%' : '';
  76. }
  77. }
  78. $one_static = $one;
  79. }
  80. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  81. if($val['sim_type']=='0002'){
  82. foreach ($two as $k2 => $val2){
  83. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  84. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  85. $two[$k2]['epercent'] = '';
  86. if($two_total>0){
  87. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  88. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  89. }
  90. }
  91. }
  92. $two_static = $two;
  93. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  94. if($val['sim_type']=='0003'){
  95. foreach ($three as $k3 => $val3){
  96. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  97. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  98. $three[$k3]['epercent'] = '';
  99. if($three_total>0){
  100. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  101. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  102. }
  103. }
  104. }
  105. $three_static = $three;
  106. }
  107. //统计错误率
  108. $statistics = [
  109. 'one_static' => $one_static,
  110. 'two_static' => $two_static,
  111. 'three_static' => $three_static,
  112. ];
  113. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  114. return json($result);
  115. }
  116. public function edit($ids = null)
  117. {
  118. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. $rows = $this->model->get($ids);
  123. $row['seat_id'] = $rows->seat_id;
  124. $row['user_nickname'] = $rows->user_nickname;
  125. $row['user_username'] = $rows->user_username;
  126. $row['is_sure'] = $rows->is_sure;
  127. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  128. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  129. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  130. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  131. if (false === $this->request->isPost()) {
  132. $this->view->assign('row', $row);
  133. return $this->view->fetch();
  134. }
  135. $params = $this->request->post('row/a');
  136. if (empty($params)) {
  137. $this->error(__('Parameter %s can not be empty', ''));
  138. }
  139. $params = $this->preExcludeFields($params);
  140. $result = false;
  141. Db::startTrans();
  142. try {
  143. //是否采用模型验证
  144. $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'];
  145. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  146. $rows->total_score = $params['total'];
  147. $rows->is_sure = 1;
  148. $rows->save();
  149. Db::commit();
  150. } catch (ValidateException|PDOException|Exception $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if (false === $result) {
  155. $this->error(__('No rows were updated'));
  156. }
  157. $this->success();
  158. }
  159. public function view($ids = null)
  160. {
  161. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  162. if (!$row) {
  163. $this->error(__('No Results were found'));
  164. }
  165. $rows = $this->model->get($ids);
  166. $row['seat_id'] = $rows->seat_id;
  167. $row['user_nickname'] = $rows->user_nickname;
  168. $row['user_username'] = $rows->user_username;
  169. $row['user_depart_id'] = $rows->user_depart_id;
  170. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  171. $row['start_time'] = $rows->start_time;
  172. $row['end_time'] = $rows->end_time;
  173. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  174. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  175. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  176. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  177. $this->view->assign('row', $row);
  178. return $this->view->fetch();
  179. }
  180. //正在考试
  181. public function persent($ids = null)
  182. {
  183. //设置过滤方法
  184. $this->request->filter(['strip_tags', 'trim']);
  185. if (false === $this->request->isAjax()) {
  186. // $examlist = $this->model->select();
  187. // foreach ($examlist as $k => $v){
  188. // $examlist[$k]['score'] = Db::name('real_exam_score')->where('exam_id',$v['exam_id'])->find();
  189. // }
  190. // $this->view->assign('examlist', $examlist);
  191. $this->assignConfig('ids', $ids);
  192. return $this->view->fetch();
  193. }
  194. //如果发送的来源是 Selectpage,则转发到 Selectpage
  195. if ($this->request->request('keyField')) {
  196. return $this->selectpage();
  197. }
  198. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  199. $list = Db::name('real_exam_comp_request')->where('exam_collection_type',3)
  200. ->where($where)
  201. ->order($sort, $order)
  202. ->paginate($limit);
  203. unset($v);
  204. $result = ['total' => $list->total(), 'rows' => $list->items()];
  205. return json($result);
  206. }
  207. public function examing()
  208. {
  209. //设置过滤方法
  210. $this->request->filter(['strip_tags', 'trim']);
  211. if (false === $this->request->isAjax()) {
  212. return $this->view->fetch();
  213. }
  214. //如果发送的来源是 Selectpage,则转发到 Selectpage
  215. if ($this->request->request('keyField')) {
  216. return $this->selectpage();
  217. }
  218. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  219. $list = $this->model
  220. ->where($where)->where($this->whereExtend)->with($this->relationtTable)
  221. ->order($sort, $order)
  222. ->paginate($limit);
  223. $result = ['total' => $list->total(), 'rows' => $list->items()];
  224. return json($result);
  225. }
  226. public function score()
  227. {
  228. //设置过滤方法
  229. $this->request->filter(['strip_tags', 'trim']);
  230. if (false === $this->request->isAjax()) {
  231. return $this->view->fetch();
  232. }
  233. //如果发送的来源是 Selectpage,则转发到 Selectpage
  234. if ($this->request->request('keyField')) {
  235. return $this->selectpage();
  236. }
  237. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  238. $list = ExamsScore::where($where)
  239. ->order($sort, $order)
  240. ->paginate($limit);
  241. foreach ($list as $k => $v){
  242. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  243. if($exam){
  244. $v->seat_id = $exam['seat_id'];
  245. $v->user_username = $exam['user_username'];
  246. $v->user_nickname = $exam['user_nickname'];
  247. }
  248. }
  249. unset($v);
  250. $result = ['total' => $list->total(), 'rows' => $list->items()];
  251. return json($result);
  252. }
  253. public function handle($type = null,$ids = null)
  254. {
  255. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  256. if(!$row){
  257. $this->error('未找到记录');
  258. }
  259. if($type ==1){
  260. $request_status = 2;
  261. }
  262. if($type==2)
  263. {
  264. $request_status= 3;
  265. }
  266. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  267. //同步更新考试表json串
  268. $exam_info = $this->model->get($row['exam_id']);
  269. if(!empty($exam_info)){
  270. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  271. foreach ($other_replace as $k=>$v){
  272. if($v['fault_id'] == $row['fault_id']){
  273. $other_replace[$k]['request_status'] = $request_status;
  274. }
  275. }
  276. $exam_info->other_replace = json_encode($other_replace);
  277. $exam_info->save();
  278. unset($v);
  279. }
  280. $this->success();
  281. }
  282. //啦
  283. public function editscore($ids = "")
  284. {
  285. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  286. if (!$row) {
  287. $this->error(__('No Results were found'));
  288. }
  289. $rows = $this->model->get($ids);
  290. $params = $this->request->post('row/a');
  291. if (empty($params)) {
  292. $this->error(__('Parameter %s can not be empty', ''));
  293. }
  294. $params = $this->preExcludeFields($params);
  295. $result = false;
  296. Db::startTrans();
  297. try {
  298. //是否采用模型验证
  299. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  300. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  301. $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'];;
  302. $rows->total_score = $total;
  303. $rows->is_sure = 1;
  304. $rows->save();
  305. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  306. Db::commit();
  307. } catch (ValidateException|PDOException|Exception $e) {
  308. Db::rollback();
  309. $this->error($e->getMessage());
  310. }
  311. if (false === $result) {
  312. $this->error(__('No rows were updated'));
  313. }
  314. $this->success();
  315. }
  316. }