StudentController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 学生管理
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class StudentController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2016-12-03T12:39:08+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. // 权限校验
  26. $this->Is_Power();
  27. }
  28. /**
  29. * [Index 学生列表]
  30. * @author Devil
  31. * @blog http://gong.gg/
  32. * @version 0.0.1
  33. * @datetime 2016-12-06T21:31:53+0800
  34. */
  35. public function Index()
  36. {
  37. // 参数
  38. $param = array_merge($_POST, $_GET);
  39. // 模型对象
  40. $m = M('Student');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->where($where)->count(),
  48. 'where' => $param,
  49. 'url' => U('Admin/Student/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 获取列表
  53. $list = $this->SetDataHandle($m->where($where)->limit($page->GetPageStarNumber(), $number)->order('id desc')->select());
  54. // 性别
  55. $this->assign('common_gender_list', L('common_gender_list'));
  56. // 学生状态
  57. $this->assign('common_student_state_list', L('common_student_state_list'));
  58. // 缴费状态
  59. $this->assign('common_tuition_state_list', L('common_tuition_state_list'));
  60. // 地区
  61. $region_list = M('Region')->field(array('id', 'name'))->where(array('is_enable'=>1))->select();
  62. $this->assign('region_list', $region_list);
  63. // 班级
  64. $this->assign('class_list', $this->GetClassList());
  65. // 参数
  66. $this->assign('param', $param);
  67. // 分页
  68. $this->assign('page_html', $page->GetPageHtml());
  69. // 数据列表
  70. $this->assign('list', $list);
  71. // Excel导出地址
  72. $this->assign('excel_url', U('Admin/Student/ExcelExport', $param));
  73. // Excel导入基础数据
  74. $this->assign('excel_import_tips', L('student_excel_import_tips'));
  75. $this->assign('excel_import_form_url', U('Admin/Student/ExcelImport'));
  76. $this->assign('excel_import_format_url', U('Admin/Student/ExcelExport', ['type'=>'format_download']));
  77. $this->display('Index');
  78. }
  79. /**
  80. * [ExcelImport excel导入]
  81. * @author Devil
  82. * @blog http://gong.gg/
  83. * @version 0.0.1
  84. * @datetime 2017-04-06T16:48:56+0800
  85. */
  86. public function ExcelImport()
  87. {
  88. // 是否ajax请求
  89. if(!IS_AJAX)
  90. {
  91. $this->error(L('common_unauthorized_access'));
  92. }
  93. // 文件上传校验
  94. $error = FileUploadError('excel');
  95. if($error !== true)
  96. {
  97. $this->ajaxReturn($error, -1);
  98. }
  99. // excel驱动
  100. $excel = new \My\Excel(array('title'=>L('excel_student_impoet_title_list'), 'msg'=>L('common_not_data_tips')));
  101. $data = $excel->Import();
  102. if(empty($data))
  103. {
  104. $this->ajaxReturn(L('common_not_data_tips'), -2);
  105. }
  106. // 学生导入模型
  107. $m = D('StudentImport');
  108. // 开始处理导入数据
  109. $success = 0;
  110. $error = array();
  111. foreach($data as $k=>$v)
  112. {
  113. // 数据处理
  114. $v = $this->ExcelImportDataDealWith($v);
  115. // 数据自动校验
  116. if($m->create($v, 1) !== false)
  117. {
  118. // 额外数据处理
  119. $m->birthday = strtotime($v['birthday']);
  120. $m->semester_id = MyC('admin_semester_id');
  121. // 开启事务
  122. $m->startTrans();
  123. // 学生是否已存在编号
  124. $number = $m->where(array('id_card'=>I('id_card')))->getField('number');
  125. // 数据写入
  126. $student_id = $m->add();
  127. // 更新学号
  128. if(empty($number))
  129. {
  130. $number = GenerateStudentNumber($student_id);
  131. }
  132. $number_state = $m->where(array('id'=>$student_id))->save(array('number'=>$number));
  133. if($student_id > 0 && $number_state !== false)
  134. {
  135. // 提交事务
  136. $m->commit();
  137. $success++;
  138. } else {
  139. // 回滚事务
  140. $m->rollback();
  141. $error[] = $v['username'].' ['.L('common_operation_add_error').']';
  142. }
  143. } else {
  144. $error[] = $v['username'].' ['.current($m->getError()).']';
  145. }
  146. }
  147. $this->ajaxReturn(L('common_operation_success'), 0, array('success'=>$success, 'error'=>$error));
  148. }
  149. /**
  150. * [ExcelImportDataDealWith 学生excel导入数据处理]
  151. * @author Devil
  152. * @blog http://gong.gg/
  153. * @version 0.0.1
  154. * @datetime 2017-04-07T12:04:05+0800
  155. * @param [array] $data [学生数据]
  156. * @return [array] [处理好的数据]
  157. */
  158. private function ExcelImportDataDealWith($data)
  159. {
  160. if(!empty($data) && is_array($data))
  161. {
  162. // 数据列表
  163. $gender_list = LangValueKeyFlip(L('common_gender_list'), 0);
  164. $student_state_list = LangValueKeyFlip(L('common_student_state_list'), 0);
  165. $tuition_state_list = LangValueKeyFlip(L('common_tuition_state_list'), 0);
  166. // M对象
  167. $r = M('Region');
  168. $c = M('Class');
  169. // 删除n学生编号
  170. if(isset($data['number']))
  171. {
  172. unset($data['number']);
  173. }
  174. // 数据安全处理
  175. foreach($data as $ks=>$vs)
  176. {
  177. $data[$ks] = I('data.'.$ks, '', '', $data);
  178. }
  179. // 性别
  180. if(!empty($data['gender']))
  181. {
  182. // 性别
  183. $data['gender'] = isset($gender_list[$data['gender']]) ? $gender_list[$data['gender']] : $gender_list['default'];
  184. }
  185. // 学生状态
  186. if(!empty($data['state']))
  187. {
  188. $data['state'] = isset($student_state_list[$data['state']]) ? $student_state_list[$data['state']] : $student_state_list['default'];
  189. }
  190. // 缴费状态
  191. if(!empty($data['tuition_state']))
  192. {
  193. $data['tuition_state'] = isset($tuition_state_list[$data['tuition_state']]) ? $tuition_state_list[$data['tuition_state']] : $tuition_state_list['default'];
  194. }
  195. // 地区
  196. if(!empty($data['region_name']))
  197. {
  198. $data['region_id'] = $r->where(array('name'=>$data['region_name']))->getField('id');
  199. unset($data['region_name']);
  200. }
  201. // 班级
  202. if(!empty($data['class_name']))
  203. {
  204. if(strpos($data['class_name'], '-') === false)
  205. {
  206. $temp_class_name = $data['class_name'];
  207. } else {
  208. $temp_ex = explode('-', $data['class_name']);
  209. $temp_class_name = $temp_ex[1];
  210. }
  211. $data['class_id'] = $c->where(array('name'=>$temp_class_name))->getField('id');
  212. unset($data['class_name']);
  213. }
  214. // 添加时间
  215. if(!empty($data['add_time']))
  216. {
  217. $data['add_time'] = strtotime($data['add_time']);
  218. } else {
  219. $data['add_time'] = time();
  220. }
  221. }
  222. return $data;
  223. }
  224. /**
  225. * [ExcelExport excel文件导出]
  226. * @author Devil
  227. * @blog http://gong.gg/
  228. * @version 0.0.1
  229. * @datetime 2017-01-10T15:46:00+0800
  230. */
  231. public function ExcelExport()
  232. {
  233. // 类型
  234. switch(I('type'))
  235. {
  236. // 格式下载类型不查询数据,只导出标题格式
  237. case 'format_download' :
  238. $title = L('excel_student_impoet_title_list');
  239. $data = array();
  240. break;
  241. // 默认按照当前条件查询数据
  242. default :
  243. $title = L('excel_student_title_list');
  244. $data = $this->SetDataHandle(M('Student')->where($this->GetIndexWhere())->select());
  245. }
  246. // Excel驱动导出数据
  247. $excel = new \My\Excel(array('filename'=>'student', 'title'=>$title, 'data'=>$data, 'msg'=>L('common_not_data_tips')));
  248. $excel->Export();
  249. }
  250. /**
  251. * [SetDataHandle 数据处理]
  252. * @author Devil
  253. * @blog http://gong.gg/
  254. * @version 0.0.1
  255. * @datetime 2016-12-29T21:27:15+0800
  256. * @param [array] $data [学生数据]
  257. * @return [array] [处理好的数据]
  258. */
  259. private function SetDataHandle($data)
  260. {
  261. if(!empty($data))
  262. {
  263. $c = M('Class');
  264. $r = M('Region');
  265. foreach($data as $k=>$v)
  266. {
  267. // 班级
  268. $tmp_class = $c->field(array('pid', 'name'))->find($v['class_id']);
  269. if(!empty($tmp_class))
  270. {
  271. $p_name = ($tmp_class['pid'] > 0) ? $c->where(array('id'=>$tmp_class['pid']))->getField('name') : '';
  272. $data[$k]['class_name'] = empty($p_name) ? $tmp_class['name'] : $p_name.'-'.$tmp_class['name'];
  273. } else {
  274. $data[$k]['class_name'] = '';
  275. }
  276. // 地区
  277. $data[$k]['region_name'] = $r->where(array('id'=>$v['region_id']))->getField('name');
  278. // 出生
  279. $data[$k]['birthday'] = date('Y-m-d', $v['birthday']);
  280. // 报名时间
  281. $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  282. // 更新时间
  283. $data[$k]['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
  284. // 性别
  285. $data[$k]['gender'] = L('common_gender_list')[$v['gender']]['name'];
  286. // 状态
  287. $data[$k]['state'] = L('common_student_state_list')[$v['state']]['name'];
  288. // 缴费状态
  289. $data[$k]['tuition_state'] = L('common_tuition_state_list')[$v['tuition_state']]['name'];
  290. }
  291. }
  292. return $data;
  293. }
  294. /**
  295. * [GetIndexWhere 学生列表条件]
  296. * @author Devil
  297. * @blog http://gong.gg/
  298. * @version 0.0.1
  299. * @datetime 2016-12-10T22:16:29+0800
  300. */
  301. private function GetIndexWhere()
  302. {
  303. $where = array();
  304. // 学期id
  305. $where['semester_id'] = MyC('admin_semester_id');
  306. // 模糊
  307. if(!empty($_REQUEST['keyword']))
  308. {
  309. $like_keyword = array('like', '%'.I('keyword').'%');
  310. $where[] = array(
  311. 'username' => $like_keyword,
  312. 'id_card' => $like_keyword,
  313. 'number' => $like_keyword,
  314. 'tel' => $like_keyword,
  315. 'my_mobile' => $like_keyword,
  316. 'parent_mobile' => $like_keyword,
  317. 'email' => $like_keyword,
  318. '_logic' => 'or',
  319. );
  320. }
  321. // 是否更多条件
  322. if(I('is_more', 0) == 1)
  323. {
  324. // 等值
  325. if(I('class_id', 0) > 0)
  326. {
  327. $where['class_id'] = intval(I('class_id'));
  328. }
  329. if(I('region_id', 0) > 0)
  330. {
  331. $where['region_id'] = intval(I('region_id'));
  332. }
  333. if(I('gender', -1) > -1)
  334. {
  335. $where['gender'] = intval(I('gender', 0));
  336. }
  337. if(I('state', -1) > -1)
  338. {
  339. $where['state'] = intval(I('state', 0));
  340. }
  341. if(I('tuition_state', -1) > -1)
  342. {
  343. $where['tuition_state'] = intval(I('tuition_state', 0));
  344. }
  345. // 表达式
  346. if(!empty($_REQUEST['time_start']))
  347. {
  348. $where['birthday'][] = array('gt', strtotime(I('time_start')));
  349. }
  350. if(!empty($_REQUEST['time_end']))
  351. {
  352. $where['birthday'][] = array('lt', strtotime(I('time_end')));
  353. }
  354. }
  355. return $where;
  356. }
  357. /**
  358. * [SaveInfo 学生添加/编辑页面]
  359. * @author Devil
  360. * @blog http://gong.gg/
  361. * @version 0.0.1
  362. * @datetime 2016-12-14T21:37:02+0800
  363. */
  364. public function SaveInfo()
  365. {
  366. // 学生信息
  367. $data = empty($_REQUEST['id']) ? array() : M('Student')->find(I('id'));
  368. if(!empty($data['birthday']))
  369. {
  370. $data['birthday'] = date('Y-m-d', $data['birthday']);
  371. }
  372. $this->assign('data', $data);
  373. // 地区
  374. $region_list = M('Region')->field(array('id', 'name'))->where(array('is_enable'=>1))->select();
  375. $this->assign('region_list', $region_list);
  376. // 班级
  377. $this->assign('class_list', $this->GetClassList());
  378. // 性别
  379. $this->assign('common_gender_list', L('common_gender_list'));
  380. // 学生状态
  381. $this->assign('common_student_state_list', L('common_student_state_list'));
  382. // 缴费状态
  383. $this->assign('common_tuition_state_list', L('common_tuition_state_list'));
  384. $this->display('SaveInfo');
  385. }
  386. /**
  387. * [Save 学生添加/编辑]
  388. * @author Devil
  389. * @blog http://gong.gg/
  390. * @version 0.0.1
  391. * @datetime 2016-12-14T21:37:02+0800
  392. */
  393. public function Save()
  394. {
  395. // 是否ajax请求
  396. if(!IS_AJAX)
  397. {
  398. $this->error(L('common_unauthorized_access'));
  399. }
  400. // 添加
  401. if(empty($_POST['id']))
  402. {
  403. $this->Add();
  404. // 编辑
  405. } else {
  406. $this->Edit();
  407. }
  408. }
  409. /**
  410. * [Add 学生添加]
  411. * @author Devil
  412. * @blog http://gong.gg/
  413. * @version 0.0.1
  414. * @datetime 2016-12-18T16:20:59+0800
  415. */
  416. private function Add()
  417. {
  418. // 学生模型
  419. $m = D('Student');
  420. // 数据自动校验
  421. if($m->create($_POST, 1))
  422. {
  423. // 额外数据处理
  424. $m->add_time = time();
  425. $m->birthday = strtotime($m->birthday);
  426. $m->semester_id = MyC('admin_semester_id');
  427. $m->username = I('username');
  428. $m->address = I('address');
  429. // 开启事务
  430. $m->startTrans();
  431. // 学生是否已存在编号
  432. $number = $m->where(array('id_card'=>I('id_card')))->getField('number');
  433. // 数据写入
  434. $student_id = $m->add();
  435. // 更新学号
  436. if(empty($number))
  437. {
  438. $number = GenerateStudentNumber($student_id);
  439. }
  440. $number_state = $m->where(array('id'=>$student_id))->save(array('number'=>$number));
  441. if($student_id > 0 && $number_state !== false)
  442. {
  443. // 提交事务
  444. $m->commit();
  445. $this->ajaxReturn(L('common_operation_add_success'));
  446. } else {
  447. // 回滚事务
  448. $m->rollback();
  449. $this->ajaxReturn(L('common_operation_add_error'), -100);
  450. }
  451. } else {
  452. $this->ajaxReturn($m->getError(), -1);
  453. }
  454. }
  455. /**
  456. * [Edit 学生编辑]
  457. * @author Devil
  458. * @blog http://gong.gg/
  459. * @version 0.0.1
  460. * @datetime 2016-12-17T22:13:40+0800
  461. */
  462. private function Edit()
  463. {
  464. // 学生模型
  465. $m = D('Student');
  466. // 数据自动校验
  467. if($m->create($_POST, 2))
  468. {
  469. // 额外数据处理
  470. if(!empty($m->birthday))
  471. {
  472. $m->birthday = strtotime($m->birthday);
  473. }
  474. $m->username = I('username');
  475. $m->address = I('address');
  476. $m->upd_time = time();
  477. // 移除不能更新的数据
  478. unset($m->id_card, $m->number);
  479. // 更新数据库
  480. if($m->where(array('id'=>I('id'), 'id_card'=>I('id_card'), 'number'=>I('number')))->save())
  481. {
  482. $this->ajaxReturn(L('common_operation_edit_success'));
  483. } else {
  484. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  485. }
  486. } else {
  487. $this->ajaxReturn($m->getError(), -1);
  488. }
  489. }
  490. /**
  491. * [Delete 学生删除]
  492. * @author Devil
  493. * @blog http://gong.gg/
  494. * @version 0.0.1
  495. * @datetime 2016-12-15T11:03:30+0800
  496. */
  497. public function Delete()
  498. {
  499. // 是否ajax请求
  500. if(!IS_AJAX)
  501. {
  502. $this->error(L('common_unauthorized_access'));
  503. }
  504. // 参数处理
  505. list($id, $id_card) = (stripos(I('id'), '-') === false) ? array() : explode('-', I('id'));
  506. // 删除数据
  507. if($id != null && $id_card != null)
  508. {
  509. // 学生模型
  510. $s = M('Student');
  511. // 学生是否存在
  512. $student = $s->where(array('id'=>$id, 'id_card'=>$id_card))->getField('id');
  513. if(empty($student))
  514. {
  515. $this->ajaxReturn(L('student_no_exist_error'), -2);
  516. }
  517. // 开启事务
  518. $s->startTrans();
  519. // 删除学生
  520. $s_state = $s->where(array('id'=>$id, 'id_card'=>$id_card))->delete();
  521. // 删除成绩
  522. $f_state = M('Fraction')->where(array('student_id'=>$id))->delete();
  523. if($s_state !== false && $f_state !== false)
  524. {
  525. // 提交事务
  526. $s->commit();
  527. $this->ajaxReturn(L('common_operation_delete_success'));
  528. } else {
  529. // 回滚事务
  530. $s->rollback();
  531. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  532. }
  533. } else {
  534. $this->ajaxReturn(L('common_param_error'), -1);
  535. }
  536. }
  537. }
  538. ?>