FractionController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 FractionController 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('Fraction');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $total = $m->alias('f')->join('__STUDENT__ AS s ON s.id = f.student_id')->where($where)->count();
  46. $page_param = array(
  47. 'number' => $number,
  48. 'total' => $total,
  49. 'where' => $param,
  50. 'url' => U('Admin/Fraction/Index'),
  51. );
  52. $page = new \My\Page($page_param);
  53. // 获取列表
  54. $field = array('s.username', 's.gender', 's.class_id', 'f.score', 'f.subject_id', 'f.score_id', 'f.id', 'f.student_id', 'f.comment', 'f.add_time');
  55. $list = $m->alias('f')->join('__STUDENT__ AS s ON s.id = f.student_id')->where($where)->field($field)->limit($page->GetPageStarNumber(), $number)->order('f.id desc')->select();
  56. // 数据列表
  57. $this->assign('list', $this->SetDataHandle($list));
  58. // 班级
  59. $this->assign('class_list', $this->GetClassList());
  60. // 学生成绩等级
  61. $this->assign('common_fraction_level_list', L('common_fraction_level_list'));
  62. // 科目
  63. $subject_list = M('Subject')->field(array('id', 'name'))->where(array('is_enable'=>1))->select();
  64. $this->assign('subject_list', $subject_list);
  65. // 成绩期号
  66. $this->assign('score_list', M('Score')->select());
  67. // 参数
  68. $this->assign('param', $param);
  69. // 分页
  70. $this->assign('page_html', $page->GetPageHtml());
  71. // Excel导出地址
  72. $this->assign('excel_url', U('Admin/Fraction/ExcelExport', $param));
  73. // Excel导入基础数据
  74. $this->assign('excel_import_tips', L('fraction_excel_import_tips'));
  75. $this->assign('excel_import_form_url', U('Admin/Fraction/ExcelImport'));
  76. $this->assign('excel_import_format_url', U('Admin/Fraction/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_fraction_import_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('FractionImport');
  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. if($this->IsExistData($v['student_id'], $v['subject_id'], $v['score_id']))
  120. {
  121. $error[] = $v['username'].' ['.L('common_data_is_exist_error').']';
  122. } else {
  123. // 写入数据库
  124. if($m->add())
  125. {
  126. $success++;
  127. } else {
  128. $error[] = $v['username'].' ['.L('common_operation_add_error').']';
  129. }
  130. }
  131. } else {
  132. $error[] = $v['username'].' ['.current($m->getError()).']';
  133. }
  134. }
  135. $this->ajaxReturn(L('common_operation_success'), 0, array('success'=>$success, 'error'=>$error));
  136. }
  137. /**
  138. * [ExcelImportDataDealWith 学生成绩excel导入数据处理]
  139. * @author Devil
  140. * @blog http://gong.gg/
  141. * @version 0.0.1
  142. * @datetime 2017-04-07T12:04:05+0800
  143. * @param [array] $data [学生数据]
  144. * @return [array] [处理好的数据]
  145. */
  146. private function ExcelImportDataDealWith($data)
  147. {
  148. if(!empty($data) && is_array($data))
  149. {
  150. // M对象
  151. $score = M('Score');
  152. $subject = M('Subject');
  153. $student = M('Student');
  154. // 学期号
  155. $data['semester_id'] = MyC('admin_semester_id');
  156. // 数据安全处理
  157. foreach($data as $ks=>$vs)
  158. {
  159. $data[$ks] = I('data.'.$ks, '', '', $data);
  160. }
  161. // 科目
  162. if(!empty($data['subject_name']))
  163. {
  164. $data['subject_id'] = $subject->where(array('name'=>$data['subject_name']))->getField('id');
  165. unset($data['subject_name']);
  166. }
  167. // 期号
  168. if(!empty($data['score_name']))
  169. {
  170. $data['score_id'] = $score->where(array('name'=>$data['score_name']))->getField('id');
  171. unset($data['score_name']);
  172. }
  173. // 学生
  174. if(!empty($data['username']) && isset($data['number']) && isset($data['id_card']))
  175. {
  176. $data['student_id'] = $student->where(array('username'=>$data['username'], 'number'=>$data['number'], 'id_card'=>$data['id_card'], 'semester_id'=>$data['semester_id']))->getField('id');
  177. unset($data['number'], $data['id_card']);
  178. }
  179. // 添加时间
  180. if(!empty($data['add_time']))
  181. {
  182. $data['add_time'] = strtotime($data['add_time']);
  183. } else {
  184. $data['add_time'] = time();
  185. }
  186. }
  187. return $data;
  188. }
  189. /**
  190. * [ExcelExport excel文件导出]
  191. * @author Devil
  192. * @blog http://gong.gg/
  193. * @version 0.0.1
  194. * @datetime 2017-01-10T15:46:00+0800
  195. */
  196. public function ExcelExport()
  197. {
  198. // 类型
  199. switch(I('type'))
  200. {
  201. // 格式下载类型不查询数据,只导出标题格式
  202. case 'format_download' :
  203. $title = L('excel_fraction_import_title_list');
  204. $data = array();
  205. break;
  206. // 默认按照当前条件查询数据
  207. default :
  208. $title = L('excel_fraction_title_list');
  209. $field = array('s.username', 's.gender', 's.class_id', 'f.score', 'f.subject_id', 'f.score_id', 'f.id', 'f.student_id', 'f.comment', 'f.add_time');
  210. $data = $this->SetDataHandle(M('Fraction')->alias('f')->join('__STUDENT__ AS s ON s.id = f.student_id')->where($this->GetIndexWhere())->field($field)->select());
  211. }
  212. // Excel驱动导出数据
  213. $excel = new \My\Excel(array('filename'=>'fraction', 'title'=>$title, 'data'=>$data, 'msg'=>L('common_not_data_tips')));
  214. $excel->Export();
  215. }
  216. /**
  217. * [GetIndexWhere 学生列表条件]
  218. * @author Devil
  219. * @blog http://gong.gg/
  220. * @version 0.0.1
  221. * @datetime 2016-12-10T22:16:29+0800
  222. */
  223. private function GetIndexWhere()
  224. {
  225. $where = array();
  226. // 学期id
  227. $where['f.semester_id'] = MyC('admin_semester_id');
  228. // 模糊
  229. if(!empty($_REQUEST['keyword']))
  230. {
  231. $where['s.username'] = array('like', '%'.I('keyword').'%');
  232. }
  233. // 是否更多条件
  234. if(I('is_more', 0) == 1)
  235. {
  236. // 等值
  237. if(I('class_id', 0) > 0)
  238. {
  239. $where['s.class_id'] = intval(I('class_id'));
  240. }
  241. if(I('score_id', 0) > 0)
  242. {
  243. $where['f.score_id'] = intval(I('score_id'));
  244. }
  245. if(I('subject_id', 0) > 0)
  246. {
  247. $where['f.subject_id'] = intval(I('subject_id'));
  248. }
  249. if(I('score_level', -1) > -1)
  250. {
  251. $level = L('common_fraction_level_list')[I('score_level')];
  252. $where[] = array(
  253. array('f.score' => array('egt', $level['min'])),
  254. array('f.score' => array('elt', $level['max'])),
  255. );
  256. }
  257. }
  258. return $where;
  259. }
  260. /**
  261. * [SetDataHandle 数据处理]
  262. * @author Devil
  263. * @blog http://gong.gg/
  264. * @version 0.0.1
  265. * @datetime 2016-12-29T21:27:15+0800
  266. * @param [array] $data [学生数据]
  267. * @return [array] [处理好的数据]
  268. */
  269. private function SetDataHandle($data)
  270. {
  271. if(!empty($data))
  272. {
  273. $score = M('Score');
  274. $subject = M('Subject');
  275. $class = M('Class');
  276. $score_level = L('common_fraction_level_list');
  277. foreach($data as $k=>$v)
  278. {
  279. // 班级
  280. $tmp_class = $class->field(array('pid', 'name'))->find($v['class_id']);
  281. if(!empty($tmp_class))
  282. {
  283. $p_name = ($tmp_class['pid'] > 0) ? $class->where(array('id'=>$tmp_class['pid']))->getField('name') : '';
  284. $data[$k]['class_name'] = empty($p_name) ? $tmp_class['name'] : $p_name.'-'.$tmp_class['name'];
  285. } else {
  286. $data[$k]['class_name'] = '';
  287. }
  288. // 科目
  289. $data[$k]['subject_name'] = $subject->where(array('id'=>$v['subject_id']))->getField('name');
  290. // 成绩期号
  291. $data[$k]['score_name'] = $score->where(array('id'=>$v['score_id']))->getField('name');
  292. // 成绩等级
  293. foreach($score_level as $level)
  294. {
  295. if($v['score'] >= $level['min'] && $v['score'] <= $level['max'])
  296. {
  297. $data[$k]['score_level'] = $level['name'];
  298. }
  299. }
  300. if(!isset($data[$k]['score_level']))
  301. {
  302. $data[$k]['score_level'] = '';
  303. }
  304. // 添加时间
  305. $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  306. // 性别
  307. $data[$k]['gender'] = L('common_gender_list')[$v['gender']]['name'];
  308. }
  309. }
  310. return $data;
  311. }
  312. /**
  313. * [SaveInfo 学生成绩添加页面]
  314. * @author Devil
  315. * @blog http://gong.gg/
  316. * @version 0.0.1
  317. * @datetime 2016-12-14T21:37:02+0800
  318. */
  319. public function SaveInfo()
  320. {
  321. // 参数
  322. if(empty($_REQUEST['id']))
  323. {
  324. $this->error(L('common_param_error'));
  325. }
  326. // 学生信息
  327. $student = M('Student')->field(array('id', 'username'))->find(I('id'));
  328. if(empty($student))
  329. {
  330. $this->error(L('fraction_student_error'));
  331. }
  332. $this->assign('student', $student);
  333. // 成绩
  334. $this->assign('score_list', M('Score')->select());
  335. // 科目
  336. $this->assign('subject_list', M('Subject')->select());
  337. $this->display('SaveInfo');
  338. }
  339. /**
  340. * [Save 学生成绩添加/编辑]
  341. * @author Devil
  342. * @blog http://gong.gg/
  343. * @version 0.0.1
  344. * @datetime 2016-12-14T21:37:02+0800
  345. */
  346. public function Save()
  347. {
  348. // 是否ajax请求
  349. if(!IS_AJAX)
  350. {
  351. $this->error(L('common_unauthorized_access'));
  352. }
  353. // 添加
  354. if(empty($_POST['id']))
  355. {
  356. $this->Add();
  357. // 编辑
  358. } else {
  359. $this->Edit();
  360. }
  361. }
  362. /**
  363. * [Add 学生成绩添加]
  364. * @author Devil
  365. * @blog http://gong.gg/
  366. * @version 0.0.1
  367. * @datetime 2016-12-18T16:20:59+0800
  368. */
  369. private function Add()
  370. {
  371. // 学生成绩对象
  372. $m = D('Fraction');
  373. // 数据自动校验
  374. if($m->create($_POST, 1))
  375. {
  376. // 数据不能重复
  377. if($this->IsExistData(I('student_id'), I('subject_id'), I('score_id')))
  378. {
  379. $this->ajaxReturn(L('common_data_is_exist_error'), -2);
  380. }
  381. // 额外数据处理
  382. $m->add_time = time();
  383. $m->comment = I('comment');
  384. // 学期id
  385. $m->semester_id = MyC('admin_semester_id');
  386. // 写入数据库
  387. if($m->add())
  388. {
  389. $this->ajaxReturn(L('common_operation_add_success'));
  390. } else {
  391. $this->ajaxReturn(L('common_operation_add_error'), -100);
  392. }
  393. } else {
  394. $this->ajaxReturn($m->getError(), -1);
  395. }
  396. }
  397. /**
  398. * [Edit 学生成绩编辑]
  399. * @author Devil
  400. * @blog http://gong.gg/
  401. * @version 0.0.1
  402. * @datetime 2016-12-17T22:13:40+0800
  403. */
  404. private function Edit()
  405. {
  406. $this->error(L('common_unauthorized_access'));
  407. }
  408. /**
  409. * [IsExistData 校验数据不能重复添加]
  410. * @author Devil
  411. * @blog http://gong.gg/
  412. * @version 0.0.1
  413. * @datetime 2017-01-08T22:08:46+0800
  414. * @return [boolean] [存在true, 不存在false]
  415. */
  416. private function IsExistData($student_id, $subject_id, $score_id)
  417. {
  418. // 数据是否已存在
  419. $where = array(
  420. 'semester_id' => MyC('admin_semester_id'),
  421. 'student_id' => $student_id,
  422. 'subject_id' => $subject_id,
  423. 'score_id' => $score_id,
  424. );
  425. $tmp = M('Fraction')->where($where)->getField('id');
  426. return !empty($tmp);
  427. }
  428. /**
  429. * [Delete 学生成绩删除]
  430. * @author Devil
  431. * @blog http://gong.gg/
  432. * @version 0.0.1
  433. * @datetime 2016-12-14T21:40:29+0800
  434. */
  435. public function Delete()
  436. {
  437. if(!IS_AJAX)
  438. {
  439. $this->error(L('common_unauthorized_access'));
  440. }
  441. // 参数是否有误
  442. if(empty($_POST['id']))
  443. {
  444. $this->ajaxReturn(L('common_param_error'), -1);
  445. }
  446. // 参数处理
  447. list($id, $student_id) = (stripos(I('id'), '-') === false) ? array() : explode('-', I('id'));
  448. // 删除数据
  449. if($id != null && $student_id != null)
  450. {
  451. if(M('Fraction')->where(array('id'=>$id, 'student_id'=>$student_id))->delete())
  452. {
  453. $this->ajaxReturn(L('common_operation_delete_success'));
  454. } else {
  455. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  456. }
  457. } else {
  458. $this->ajaxReturn(L('common_param_error'), -1);
  459. }
  460. }
  461. }
  462. ?>