Exams.php 26 KB

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