Exams.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  16. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  17. /**
  18. * sim-考试表/成绩总分
  19. *
  20. * @icon fa fa-circle-o
  21. */
  22. class Exams extends Backend
  23. {
  24. /**
  25. * Exams模型对象
  26. * @var \app\admin\model\teacher\Exams
  27. */
  28. protected $model = null;
  29. protected $whereExtend = null;
  30. protected $noNeedRight = ['export'];
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \app\admin\model\teacher\Exams;
  35. $this->whereExtend['exam_collection_type'] = 3;
  36. $this->whereExtend['starttime'] = ['>',0];
  37. $this->whereExtend['endtime'] = ['>',0];
  38. $this->relationtTable = 'collection';
  39. }
  40. /**
  41. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  42. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  43. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  44. */
  45. public function index()
  46. {
  47. //查询最后一场考试id
  48. $exam_collection_id = $this->model->where($this->whereExtend)->order('exam_id desc')->value('exam_collection_id');
  49. $collection_name = $this->model->where(['exam_collection_id'=>$exam_collection_id])->value('exam_collection_name');
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags', 'trim']);
  52. if (false === $this->request->isAjax()) {
  53. $this->assignConfig('collection_name', $collection_name);
  54. return $this->view->fetch();
  55. }
  56. //如果发送的来源是 Selectpage,则转发到 Selectpage
  57. if ($this->request->request('keyField')) {
  58. return $this->selectpage();
  59. }
  60. //查询考试
  61. $filter = $this->request->get("filter", '');
  62. $filter = (array)json_decode($filter, true);
  63. $op = $this->request->get("op", '');
  64. $op = (array)json_decode($op, true);
  65. if(isset($filter) &&!empty($filter['exam_collection_name'])){
  66. unset($this->whereExtend['exam_collection_id']);
  67. $this->request->get(['filter'=>json_encode($filter),'op'=>json_encode($op)]);
  68. }else{
  69. $this->whereExtend['exam_collection_id'] = $exam_collection_id;
  70. }
  71. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  72. $list = $this->model
  73. ->where($where)->where($this->whereExtend)
  74. ->order($sort, $order)
  75. ->paginate($limit);
  76. //最高分 最低分 平均分
  77. $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();
  78. $pingfen = '';
  79. if(!empty($select[0]['totalscore']) && !empty($select[0]['count'])){
  80. $pingfen = bcdiv($select[0]['totalscore'],$select[0]['count']);
  81. }
  82. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  83. $one_static = [];
  84. $two_static = [];
  85. $three_static = [];
  86. $where_falut = ['fault_type' => 3,'fault_state'=>0];
  87. $exam_ids = $this->model->where($where)->where($this->whereExtend)->column('exam_id');
  88. foreach ($list as $k => $val){
  89. $one = Fault::where(['sim_type'=>'0001'])->where($where_falut)->field('fault_id,name')->select();
  90. if($val['sim_type']=='0001'){
  91. foreach ($one as $k1 => $val1){
  92. //先查分配次数,在查错误率
  93. $one_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id']])->count();
  94. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id'],'answer_right'=>2])->count();
  95. $one[$k1]['epercent'] = '';
  96. if($one_total>0){
  97. $bili = bcmul(bcdiv($error_total,$one_total,2),100);
  98. $one[$k1]['epercent'] = $bili>0 ? $bili.'%' : '';
  99. }
  100. }
  101. $vaccineCount = array_column($one, 'epercent');
  102. array_multisort($vaccineCount,SORT_DESC,$one);
  103. $one_static = $one;
  104. }
  105. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  106. if($val['sim_type']=='0002'){
  107. foreach ($two as $k2 => $val2){
  108. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  109. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  110. $two[$k2]['epercent'] = '';
  111. if($two_total>0){
  112. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  113. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  114. }
  115. }
  116. $vaccineCount1 = array_column($two, 'epercent');
  117. array_multisort($vaccineCount1,SORT_DESC,$two);
  118. }
  119. $two_static = $two;
  120. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  121. if($val['sim_type']=='0003'){
  122. foreach ($three as $k3 => $val3){
  123. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  124. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  125. $three[$k3]['epercent'] = '';
  126. if($three_total>0){
  127. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  128. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  129. }
  130. }
  131. $vaccineCount2 = array_column($three, 'epercent');
  132. array_multisort($vaccineCount2,SORT_DESC,$three);
  133. }
  134. $three_static = $three;
  135. }
  136. //统计错误率
  137. $statistics = [
  138. 'one_static' => $one_static,
  139. 'two_static' => $two_static,
  140. 'three_static' => $three_static,
  141. ];
  142. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  143. return json($result);
  144. }
  145. public function edit($ids = null)
  146. {
  147. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  148. if (!$row) {
  149. $this->error(__('No Results were found'));
  150. }
  151. $rows = $this->model->get($ids);
  152. $row['seat_id'] = $rows->seat_id;
  153. $row['user_nickname'] = $rows->user_nickname;
  154. $row['user_username'] = $rows->user_username;
  155. $row['is_sure'] = $rows->is_sure;
  156. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  157. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  158. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  159. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  160. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  161. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  162. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  163. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  164. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  165. if (false === $this->request->isPost()) {
  166. $this->view->assign('other_jielun', $other_jielun);
  167. $this->view->assign('row', $row);
  168. return $this->view->fetch();
  169. }
  170. $params = $this->request->post('row/a');
  171. if (empty($params)) {
  172. $this->error(__('Parameter %s can not be empty', ''));
  173. }
  174. $params = $this->preExcludeFields($params);
  175. $result = false;
  176. Db::startTrans();
  177. try {
  178. //是否采用模型验证
  179. $koufen = 0;
  180. if(!empty($other_jielun)){
  181. foreach ($other_jielun as $key => $value) {
  182. if(!empty($value['cx_score'])){
  183. $koufen = $koufen+abs($value['cx_score']);
  184. }
  185. }
  186. }else{
  187. $koufen = 15; //超时未自动交卷的
  188. }
  189. $params['total'] = 100-$params['fault_one_score']-$params['fault_two_score']-$params['fault_three_score']-$params['overtime_score']-$koufen;
  190. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  191. $rows->total_score = $params['total'];
  192. $rows->is_sure = 1;
  193. $rows->save();
  194. Db::commit();
  195. } catch (ValidateException|PDOException|Exception $e) {
  196. Db::rollback();
  197. $this->error($e->getMessage());
  198. }
  199. if (false === $result) {
  200. $this->error(__('No rows were updated'));
  201. }
  202. $this->success();
  203. }
  204. public function view($type = null,$ids = null)
  205. {
  206. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  207. if (!$row) {
  208. $this->error(__('No Results were found'));
  209. }
  210. $rows = $this->model->get($ids);
  211. $row['fault_total'] = $rows->fault_total;
  212. $row['seat_id'] = $rows->seat_id;
  213. $row['user_nickname'] = $rows->user_nickname;
  214. $row['user_username'] = $rows->user_username;
  215. $row['user_depart_id'] = $rows->user_depart_id;
  216. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  217. $row['start_time'] = $rows->start_time;
  218. $row['end_time'] = $rows->end_time;
  219. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  220. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  221. if($rows['fault_total']>0){
  222. if($rows['fault_total']==2){
  223. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  224. }
  225. if($rows['fault_total']==3){
  226. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  227. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  228. }
  229. }else{
  230. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  231. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  232. }
  233. $row['fault_score'] = 75-$row['fault_one_score']-$row['fault_two_score']-$row['fault_three_score'];
  234. $row['weixiu_score'] = 10-$row['overtime_score']??0;
  235. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  236. $koufen = 0;
  237. if(!empty($other_jielun)){
  238. foreach ($other_jielun as $key => $value) {
  239. if(!empty($value['cx_score'])){
  240. $koufen = $koufen+abs($value['cx_score']);
  241. }
  242. }
  243. }else{
  244. $koufen = 15;
  245. }
  246. $report_score = 15-$koufen;
  247. $row['report_score'] = $report_score>0?$report_score:0;
  248. $total = $row['fault_score']+$row['report_score']+$row['weixiu_score'];
  249. Db::name('real_exam_score')->where('exam_id', $ids)->update(['total'=>$total]);
  250. Db::name('real_exam')->where('exam_id', $ids)->update(['total_score'=>$total]);
  251. $row['total'] = $total;
  252. $this->view->assign('other_jielun', $other_jielun);
  253. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  254. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  255. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  256. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  257. $this->view->assign('row', $row);
  258. if(!empty($type)){
  259. return $this->view->fetch('view1');
  260. }else{
  261. return $this->view->fetch();
  262. }
  263. }
  264. //正在考试
  265. public function persent($ids = null)
  266. {
  267. //设置过滤方法
  268. $this->request->filter(['strip_tags', 'trim']);
  269. if (false === $this->request->isAjax()) {
  270. $this->assignConfig('ids', $ids);
  271. return $this->view->fetch();
  272. }
  273. //如果发送的来源是 Selectpage,则转发到 Selectpage
  274. if ($this->request->request('keyField')) {
  275. return $this->selectpage();
  276. }
  277. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  278. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  279. $list = Db::name('real_exam_comp_request')
  280. // ->where('exam_collection_type',3)
  281. ->where($where)->where('exam_id','in',$exam_ids)
  282. ->order($sort, $order)
  283. ->paginate($limit);
  284. unset($v);
  285. $result = ['total' => $list->total(), 'rows' => $list->items()];
  286. return json($result);
  287. }
  288. public function examing($ids = null)
  289. {
  290. //设置过滤方法
  291. $this->request->filter(['strip_tags', 'trim']);
  292. if (false === $this->request->isAjax()) {
  293. $this->assignConfig('ids', $ids);
  294. return $this->view->fetch();
  295. }
  296. //如果发送的来源是 Selectpage,则转发到 Selectpage
  297. if ($this->request->request('keyField')) {
  298. return $this->selectpage();
  299. }
  300. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  301. $list = Db::name('seat')->select();
  302. foreach ($list as $key => $value) {
  303. $exam = $this->model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_num'])->find();
  304. $user_username = '';
  305. $user_nickname = '';
  306. $exam_status = '未登录';
  307. $total_score = 0;
  308. $fault_names = '';
  309. $exam_id = 0;
  310. $simtype = '';
  311. $sim_state = '';
  312. if(!empty($exam)){
  313. if(!empty($value['current_sim_id'])){
  314. $sim = Db::name('sim')->where('sim_id',$value['current_sim_id'])->find();
  315. if(!empty($sim['sim_state'])){
  316. switch ($sim['sim_state']) {
  317. case '1':
  318. $sim_state = '在线';
  319. break;
  320. case '2':
  321. $sim_state = '离线';
  322. break;
  323. }
  324. }
  325. $simtype = $sim['sim_type'];
  326. }
  327. $user_username = $exam['user_username']??'';
  328. $user_nickname = $exam['user_nickname']??'';
  329. if($exam['exam_status']==1){
  330. $exam_status= '已登录未开始考试';
  331. }else if($exam['exam_status']==4){
  332. $exam_status= '已开始考试';
  333. }else if($exam['exam_status']==5){
  334. $exam_status= '已交卷';
  335. }
  336. $total_score = $exam['total_score']??'';
  337. $fault_names = $exam['fault_names']??'';
  338. $exam_id = $exam['exam_id']??0;
  339. $simtype = $simtype;
  340. }
  341. $list[$key]['user_username'] = $user_username;
  342. $list[$key]['user_nickname'] = $user_nickname;
  343. $list[$key]['exam_status'] = $exam_status;
  344. $list[$key]['total_score'] = $total_score;
  345. $list[$key]['fault_names'] = $fault_names;
  346. $list[$key]['exam_id'] = $exam_id;
  347. $list[$key]['sim_state'] = $sim_state;
  348. $list[$key]['sim_type'] = $simtype;
  349. }
  350. $result = ['total' => count($list), 'rows' => $list];
  351. return json($result);
  352. }
  353. public function score($ids = null)
  354. {
  355. //设置过滤方法
  356. $this->request->filter(['strip_tags', 'trim']);
  357. if (false === $this->request->isAjax()) {
  358. $this->assignConfig('ids', $ids);
  359. return $this->view->fetch();
  360. }
  361. //如果发送的来源是 Selectpage,则转发到 Selectpage
  362. if ($this->request->request('keyField')) {
  363. return $this->selectpage();
  364. }
  365. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  366. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  367. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  368. ->order($sort, $order)
  369. ->paginate($limit);
  370. foreach ($list as $k => $v){
  371. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  372. if($exam){
  373. $v->seat_id = $exam['seat_id'];
  374. $v->user_username = $exam['user_username'];
  375. $v->user_nickname = $exam['user_nickname'];
  376. $v->sim_type = $exam['sim_type'];
  377. }
  378. }
  379. unset($v);
  380. $result = ['total' => $list->total(), 'rows' => $list->items()];
  381. return json($result);
  382. }
  383. public function handle($type = null,$ids = null)
  384. {
  385. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  386. if(!$row){
  387. $this->error('未找到记录');
  388. }
  389. if($type ==1){
  390. $request_status = 2;
  391. }
  392. if($type==2)
  393. {
  394. $request_status= 3;
  395. }
  396. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  397. //同步更新考试表json串
  398. $exam_info = $this->model->get($row['exam_id']);
  399. if(!empty($exam_info)){
  400. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  401. foreach ($other_replace as $k=>$v){
  402. if($v['fault_id'] == $row['fault_id']){
  403. $other_replace[$k]['request_status'] = $request_status;
  404. }
  405. }
  406. $exam_info->other_replace = json_encode($other_replace);
  407. $exam_info->save();
  408. unset($v);
  409. }
  410. $this->success();
  411. }
  412. //啦
  413. public function editscore($ids = "")
  414. {
  415. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  416. if (!$row) {
  417. $this->error(__('No Results were found'));
  418. }
  419. $rows = $this->model->get($ids);
  420. $params = $this->request->post('row/a');
  421. if (empty($params)) {
  422. $this->error(__('Parameter %s can not be empty', ''));
  423. }
  424. $params = $this->preExcludeFields($params);
  425. $result = false;
  426. Db::startTrans();
  427. try {
  428. //是否采用模型验证
  429. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  430. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  431. $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'];;
  432. $rows->total_score = $total;
  433. $rows->is_sure = 1;
  434. $rows->save();
  435. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  436. Db::commit();
  437. } catch (ValidateException|PDOException|Exception $e) {
  438. Db::rollback();
  439. $this->error($e->getMessage());
  440. }
  441. if (false === $result) {
  442. $this->error(__('No rows were updated'));
  443. }
  444. $this->success();
  445. }
  446. public function export()
  447. {
  448. set_time_limit(0);
  449. ini_set('memory_limit', '2560M');
  450. //如果发送的来源是 Selectpage,则转发到 Selectpage
  451. if ($this->request->request('keyField')) {
  452. return $this->selectpage();
  453. }
  454. $filter = $this->request->get("filter", '');
  455. $filter = (array)json_decode($filter, true);
  456. $op = $this->request->get("op", '');
  457. $op = (array)json_decode($op, true);
  458. if(isset($filter) &&!empty($filter['exam_collection_name'])){
  459. $this->whereExtend['exam_collection_name'] = $filter['exam_collection_name'];
  460. $this->request->get(['filter'=>json_encode($filter),'op'=>json_encode($op)]);
  461. }
  462. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  463. $list = $this->model
  464. ->where($where)->where($this->whereExtend)
  465. ->order($sort, $order)
  466. ->paginate($limit);
  467. $depart_ids = [];
  468. foreach ($list as $k => $v){
  469. if(!in_array($v['user_depart_id'],$depart_ids)){
  470. $depart_ids[] = $v['user_depart_id'];
  471. }
  472. }
  473. $xlsName = '学员成绩信息';
  474. $this->exportExcel($xlsName,'Excel2007', $list,$depart_ids);
  475. }
  476. /**
  477. * 输出到浏览器(需要设置header头)
  478. * @param string $fileName 文件名
  479. * @param string $fileType 文件类型
  480. */
  481. function exportExcel($fileName, $fileType,$data,$depart_ids)
  482. {
  483. //文件名称校验
  484. if (!$fileName) {
  485. trigger_error('文件名不能为空', E_USER_ERROR);
  486. }
  487. //Excel文件类型校验
  488. $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
  489. if (!in_array($fileType, $type)) {
  490. trigger_error('未知文件类型', E_USER_ERROR);
  491. }
  492. $ext = '';
  493. if ($fileType == 'Excel2007' || $fileType == 'Xlsx') {
  494. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  495. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  496. header('Cache-Control: max-age=0');
  497. $ext = 'Xlsx';
  498. } else { //Excel5
  499. header('Content-Type: application/vnd.ms-excel');
  500. header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
  501. header('Cache-Control: max-age=0');
  502. $ext = 'Xls';
  503. }
  504. $spreadsheet = new Spreadsheet();
  505. $worksheet = $spreadsheet->getActiveSheet();
  506. $worksheet->getColumnDimension('A')->setWidth(15);
  507. $worksheet->getColumnDimension('B')->setWidth(20);
  508. $worksheet->getColumnDimension('C')->setWidth(20);
  509. $worksheet->getColumnDimension('D')->setWidth(25);
  510. $worksheet->getColumnDimension('E')->setWidth(30);
  511. $worksheet->getRowDimension('1')->setRowHeight(40);
  512. //设置工作表标题名称
  513. $worksheet->setTitle('学员成绩信息');
  514. //1745459433
  515. $kaoshi = isset($data[0]['endtime']) ? date('Y-m-d',$data[0]['endtime']) : '';
  516. $a = '';
  517. foreach ($depart_ids as $k1=>$v1){
  518. $wu = Db::name('department')->where('id',$v1)->find();
  519. $si = Db::name('department')->where('id',$wu['parent_id'])->find();
  520. $san = Db::name('department')->where('id',$si['parent_id'])->find();
  521. $er = Db::name('department')->where('id',$san['parent_id'])->find();
  522. $yi = Db::name('department')->where('id',$er['parent_id'])->find();
  523. $a = $a.$yi['name']."/".$er['name']."/".$san['name']."/".$si['name']."/".$wu['name']."\n";
  524. }
  525. // $a = "预选军士\n预选军士一区队";
  526. $worksheet->setCellValue('A1',$a);
  527. $worksheet->mergeCells('A1:D1');
  528. $worksheet->setCellValue('E1',"考试日期 ".$kaoshi);
  529. $worksheet->setCellValue('A2',"序号");
  530. $worksheet->setCellValue('B2',"学员账号");
  531. $worksheet->setCellValue('C2',"学员姓名");
  532. $worksheet->setCellValue('D2',"模拟器类型");
  533. $worksheet->setCellValue('E2',"总分");
  534. foreach ($data as $ky => $value)
  535. {
  536. $lie = $ky+3;
  537. $sim_type= '';
  538. if($value['sim_type'] == '0001'){
  539. $sim_type = 'FZD04B型侦毒器';
  540. }else if($value['sim_type'] == '0002'){
  541. $sim_type = 'FZB006型毒剂报警器';
  542. }else if($value['sim_type'] == '0003'){
  543. $sim_type = '防化兵用毒剂报警器';
  544. }
  545. $worksheet->setCellValue('A'.$lie,++$ky);
  546. $worksheet->setCellValue('B'.$lie,$value['user_username']);
  547. $worksheet->setCellValue('C'.$lie,$value['user_nickname']);
  548. $worksheet->setCellValue('D'.$lie,$sim_type);
  549. $worksheet->setCellValue('E'.$lie,$value['total_score']);
  550. }
  551. $titlestyleArray = [
  552. 'font' => [
  553. 'name' => '黑体',
  554. 'size' => 14
  555. ],
  556. ];
  557. $titlestyleArray2 = [
  558. 'font' => [
  559. 'size' => 12
  560. ],
  561. ];
  562. $headerStyleArray = [
  563. 'font' => [
  564. 'name' => '方正小标宋简体',
  565. 'size' => 18
  566. ],
  567. ];
  568. $headStyleArray1 = [
  569. 'alignment' => [
  570. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  571. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  572. 'wrapText' => true,
  573. ],
  574. ];
  575. $commonStyleArray = [
  576. 'alignment' => [
  577. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  578. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  579. 'wrapText' => true,
  580. ]
  581. ];
  582. $borderStyleArray = [
  583. 'borders' => [
  584. 'allBorders' => [
  585. // 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  586. ],
  587. ],
  588. 'font' => [
  589. 'name' => '黑体',
  590. ],
  591. 'alignment' => [
  592. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  593. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  594. 'wrapText' => true,
  595. ]
  596. ];
  597. $worksheet->getStyle('A1:E1')->applyFromArray($headStyleArray1);//换行
  598. $worksheet->getStyle('A1:E1')->applyFromArray($titlestyleArray);
  599. $worksheet->getStyle('A2:E2')->applyFromArray($titlestyleArray2);
  600. $worksheet->getStyle('A2:E2')->applyFromArray($commonStyleArray);
  601. // foreach ($data as $k=>$v){
  602. // $kk = $k+3;
  603. // $worksheet->getStyle('A'.$kk.':E'.$kk)->applyFromArray($borderStyleArray);
  604. // }
  605. // $worksheet->getStyle('A3:R3')->applyFromArray($commonStyleArray);
  606. // $worksheet->getStyle('A'.$footer_total.':R'.$footer_total)->applyFromArray($commonStyleArray);
  607. // $worksheet->getStyle('A1:C1')->applyFromArray($borderStyleArray);
  608. // $worksheet->mergeCells('A1:R1');
  609. $writer = IOFactory::createWriter($spreadsheet,$ext);
  610. $writer->save('php://output');
  611. die();
  612. }
  613. public function multi($ids = null)
  614. {
  615. if (false === $this->request->isPost()) {
  616. $this->error(__('Invalid parameters'));
  617. }
  618. $ids = $ids ?: $this->request->post('ids');
  619. if (empty($ids)) {
  620. $this->error(__('Parameter %s can not be empty', 'ids'));
  621. }
  622. if (false === $this->request->has('params')) {
  623. $this->error(__('No rows were updated'));
  624. }
  625. parse_str($this->request->post('params'), $values);
  626. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  627. // if (empty($values)) {
  628. // $this->error(__('You have no permission'));
  629. // }
  630. $adminIds = $this->getDataLimitAdminIds();
  631. if (is_array($adminIds)) {
  632. $this->model->where($this->dataLimitField, 'in', $adminIds);
  633. }
  634. $count = 0;
  635. Db::startTrans();
  636. try {
  637. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  638. foreach ($list as $item) {
  639. $count += $item->allowField(true)->isUpdate(true)->save($values);
  640. }
  641. Db::commit();
  642. } catch (PDOException|Exception $e) {
  643. Db::rollback();
  644. $this->error($e->getMessage());
  645. }
  646. if ($count) {
  647. $this->success();
  648. }
  649. $this->error(__('No rows were updated'));
  650. }
  651. }