Exams.php 10 KB

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