Exams.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 = bcdiv($select[0]['totalscore'],$select[0]['count']);
  56. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  57. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen];
  58. return json($result);
  59. }
  60. public function edit($ids = null)
  61. {
  62. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  63. if (!$row) {
  64. $this->error(__('No Results were found'));
  65. }
  66. $rows = $this->model->get($ids);
  67. $row['seat_id'] = $rows->seat_id;
  68. $row['user_nickname'] = $rows->user_nickname;
  69. $row['user_username'] = $rows->user_username;
  70. $row['is_sure'] = $rows->is_sure;
  71. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  72. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  73. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  74. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  75. if (false === $this->request->isPost()) {
  76. $this->view->assign('row', $row);
  77. return $this->view->fetch();
  78. }
  79. $params = $this->request->post('row/a');
  80. if (empty($params)) {
  81. $this->error(__('Parameter %s can not be empty', ''));
  82. }
  83. $params = $this->preExcludeFields($params);
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. $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'];
  89. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  90. $rows->total_score = $params['total'];
  91. $rows->is_sure = 1;
  92. $rows->save();
  93. Db::commit();
  94. } catch (ValidateException|PDOException|Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if (false === $result) {
  99. $this->error(__('No rows were updated'));
  100. }
  101. $this->success();
  102. }
  103. public function view($ids = null)
  104. {
  105. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  106. if (!$row) {
  107. $this->error(__('No Results were found'));
  108. }
  109. $rows = $this->model->get($ids);
  110. $row['seat_id'] = $rows->seat_id;
  111. $row['user_nickname'] = $rows->user_nickname;
  112. $row['user_username'] = $rows->user_username;
  113. $row['user_depart_id'] = $rows->user_depart_id;
  114. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  115. $row['start_time'] = $rows->start_time;
  116. $row['end_time'] = $rows->end_time;
  117. $this->view->assign('row', $row);
  118. return $this->view->fetch();
  119. }
  120. //正在考试
  121. public function persent()
  122. {
  123. //设置过滤方法
  124. $this->request->filter(['strip_tags', 'trim']);
  125. if (false === $this->request->isAjax()) {
  126. $examlist = $this->model->select();
  127. foreach ($examlist as $k => $v){
  128. $examlist[$k]['score'] = Db::name('real_exam_score')->where('exam_id',$v['exam_id'])->find();
  129. }
  130. $this->view->assign('examlist', $examlist);
  131. return $this->view->fetch();
  132. }
  133. //如果发送的来源是 Selectpage,则转发到 Selectpage
  134. if ($this->request->request('keyField')) {
  135. return $this->selectpage();
  136. }
  137. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  138. $list = Db::name('real_exam_comp_request')->where('exam_collection_type',3)
  139. ->where($where)
  140. ->order($sort, $order)
  141. ->paginate($limit);
  142. unset($v);
  143. $result = ['total' => $list->total(), 'rows' => $list->items()];
  144. return json($result);
  145. }
  146. public function examing()
  147. {
  148. //设置过滤方法
  149. $this->request->filter(['strip_tags', 'trim']);
  150. if (false === $this->request->isAjax()) {
  151. return $this->view->fetch();
  152. }
  153. //如果发送的来源是 Selectpage,则转发到 Selectpage
  154. if ($this->request->request('keyField')) {
  155. return $this->selectpage();
  156. }
  157. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  158. $list = $this->model
  159. ->where($where)->where($this->whereExtend)->with($this->relationtTable)
  160. ->order($sort, $order)
  161. ->paginate($limit);
  162. $result = ['total' => $list->total(), 'rows' => $list->items()];
  163. return json($result);
  164. }
  165. public function score()
  166. {
  167. //设置过滤方法
  168. $this->request->filter(['strip_tags', 'trim']);
  169. if (false === $this->request->isAjax()) {
  170. return $this->view->fetch();
  171. }
  172. //如果发送的来源是 Selectpage,则转发到 Selectpage
  173. if ($this->request->request('keyField')) {
  174. return $this->selectpage();
  175. }
  176. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  177. $list = ExamsScore::where($where)
  178. ->order($sort, $order)
  179. ->paginate($limit);
  180. foreach ($list as $k => $v){
  181. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  182. if($exam){
  183. $v->seat_id = $exam['seat_id'];
  184. $v->user_username = $exam['user_username'];
  185. $v->user_nickname = $exam['user_nickname'];
  186. }
  187. }
  188. unset($v);
  189. $result = ['total' => $list->total(), 'rows' => $list->items()];
  190. return json($result);
  191. }
  192. public function handle($type = null,$ids = null)
  193. {
  194. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  195. if(!$row){
  196. $this->error('未找到记录');
  197. }
  198. if($type ==1){
  199. $request_status = 2;
  200. }
  201. if($type==2)
  202. {
  203. $request_status= 3;
  204. }
  205. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  206. //同步更新考试表json串
  207. $exam_info = $this->model->get($row['exam_id']);
  208. if(!empty($exam_info)){
  209. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  210. foreach ($other_replace as $k=>$v){
  211. if($v['fault_id'] == $row['fault_id']){
  212. $other_replace[$k]['request_status'] = $request_status;
  213. }
  214. }
  215. $exam_info->other_replace = json_encode($other_replace);
  216. $exam_info->save();
  217. unset($v);
  218. }
  219. $this->success();
  220. }
  221. //啦
  222. public function editscore($ids = "")
  223. {
  224. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  225. if (!$row) {
  226. $this->error(__('No Results were found'));
  227. }
  228. $rows = $this->model->get($ids);
  229. $params = $this->request->post('row/a');
  230. if (empty($params)) {
  231. $this->error(__('Parameter %s can not be empty', ''));
  232. }
  233. $params = $this->preExcludeFields($params);
  234. $result = false;
  235. Db::startTrans();
  236. try {
  237. //是否采用模型验证
  238. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  239. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  240. $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'];;
  241. $rows->total_score = $total;
  242. $rows->is_sure = 1;
  243. $rows->save();
  244. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  245. Db::commit();
  246. } catch (ValidateException|PDOException|Exception $e) {
  247. Db::rollback();
  248. $this->error($e->getMessage());
  249. }
  250. if (false === $result) {
  251. $this->error(__('No rows were updated'));
  252. }
  253. $this->success();
  254. }
  255. }