CourseController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 CourseController 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('Course');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $total = $m->alias('AS c')->join(' INNER JOIN __TEACHER__ AS t ON c.teacher_id = t.id INNER JOIN __CLASS__ AS cs ON c.class_id = cs.id INNER JOIN __SUBJECT__ AS s ON c.subject_id = s.id INNER JOIN __WEEK__ AS w ON c.week_id = w.id INNER JOIN __INTERVAL__ AS i ON c.interval_id = i.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('c.id', 'c.state', 't.username AS teacher_name', 'cs.name AS class_name', 'cs.pid AS class_pid', 's.name AS subject_name', 'w.name AS week_name', 'i.name AS interval_name', 'c.add_time AS add_time', 'r.pid AS room_pid', 'r.name AS room_name');
  55. $list = $this->SetDataHandle($m->alias('AS c')->join(' INNER JOIN __TEACHER__ AS t ON c.teacher_id = t.id INNER JOIN __CLASS__ AS cs ON c.class_id = cs.id INNER JOIN __SUBJECT__ AS s ON c.subject_id = s.id INNER JOIN __WEEK__ AS w ON c.week_id = w.id INNER JOIN __INTERVAL__ AS i ON c.interval_id = i.id INNER JOIN __ROOM__ AS r ON c.room_id = r.id')->where($where)->field($field)->limit($page->GetPageStarNumber(), $number)->select());
  56. // 数据列表
  57. $this->assign('list', $list);
  58. // 字段
  59. $field = array('id', 'name');
  60. // 条件
  61. $where = array('is_enable'=>1);
  62. // 班级
  63. $this->assign('class_list', $this->GetClassList());
  64. // 教室
  65. $this->assign('room_list', $this->GetRoomList());
  66. // 周天
  67. $this->assign('week_list', M('Week')->field($field)->where($where)->select());
  68. // 科目
  69. $this->assign('subject_list', M('Subject')->field($field)->where($where)->select());
  70. // 时段
  71. $this->assign('interval_list', M('Interval')->field($field)->where($where)->select());
  72. // 参数
  73. $this->assign('param', $param);
  74. // 分页
  75. $this->assign('page_html', $page->GetPageHtml());
  76. // Excel地址
  77. $this->assign('excel_url', U('Admin/Course/ExcelExport', $param));
  78. $this->display('Index');
  79. }
  80. /**
  81. * [ExcelExport excel文件导出]
  82. * @author Devil
  83. * @blog http://gong.gg/
  84. * @version 0.0.1
  85. * @datetime 2017-01-10T15:46:00+0800
  86. */
  87. public function ExcelExport()
  88. {
  89. // 条件
  90. $where = $this->GetIndexWhere();
  91. // 读取数据
  92. $field = array('c.id', 'c.state', 't.username AS teacher_name', 'cs.name AS class_name', 'cs.pid AS class_pid', 's.name AS subject_name', 'w.name AS week_name', 'i.name AS interval_name', 'c.add_time AS add_time', 'r.pid AS room_pid', 'r.name AS room_name');
  93. $data = $this->SetDataHandle(M('Course')->alias('AS c')->join(' INNER JOIN __TEACHER__ AS t ON c.teacher_id = t.id INNER JOIN __CLASS__ AS cs ON c.class_id = cs.id INNER JOIN __SUBJECT__ AS s ON c.subject_id = s.id INNER JOIN __WEEK__ AS w ON c.week_id = w.id INNER JOIN __INTERVAL__ AS i ON c.interval_id = i.id INNER JOIN __ROOM__ AS r ON c.room_id = r.id')->where($where)->field($field)->select());
  94. // Excel驱动导出数据
  95. $excel = new \My\Excel(array('filename'=>'course', 'title'=>L('excel_course_title_list'), 'data'=>$data, 'msg'=>L('common_not_data_tips')));
  96. $excel->Export();
  97. }
  98. /**
  99. * [SetDataHandle 数据处理]
  100. * @author Devil
  101. * @blog http://gong.gg/
  102. * @version 0.0.1
  103. * @datetime 2016-12-29T21:27:15+0800
  104. * @param [array] $data [教师课程数据]
  105. * @return [array] [处理好的数据]
  106. */
  107. private function SetDataHandle($data)
  108. {
  109. if(!empty($data))
  110. {
  111. $c = M('Class');
  112. $r = M('Room');
  113. foreach($data as $k=>$v)
  114. {
  115. // 班级
  116. if($v['class_pid'] != 0)
  117. {
  118. $p_name = $c->where(array('id'=>$v['class_pid']))->getField('name');
  119. $data[$k]['class_name'] = empty($p_name) ? $v['class_name'] : $p_name.'-'.$v['class_name'];
  120. }
  121. // 教室
  122. if($v['room_pid'] != 0)
  123. {
  124. $p_name = $r->where(array('id'=>$v['room_pid']))->getField('name');
  125. $data[$k]['room_name'] = empty($p_name) ? $v['room_name'] : $p_name.'-'.$v['room_name'];
  126. }
  127. // 添加时间
  128. $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  129. // 状态
  130. $data[$k]['state_text'] = L('common_state_list')[$v['state']]['name'];
  131. }
  132. }
  133. return $data;
  134. }
  135. /**
  136. * [GetIndexWhere 教师课程列表条件]
  137. * @author Devil
  138. * @blog http://gong.gg/
  139. * @version 0.0.1
  140. * @datetime 2016-12-10T22:16:29+0800
  141. */
  142. private function GetIndexWhere()
  143. {
  144. $where = array();
  145. // 学期id
  146. $where['c.semester_id'] = MyC('admin_semester_id');
  147. // 模糊
  148. if(!empty($_REQUEST['keyword']))
  149. {
  150. $where['t.username'] = array('like', '%'.I('keyword').'%');
  151. }
  152. // 是否更多条件
  153. if(I('is_more', 0) == 1)
  154. {
  155. // 等值
  156. if(I('class_id', 0) > 0)
  157. {
  158. $where['c.class_id'] = intval(I('class_id'));
  159. }
  160. if(I('week_id', 0) > 0)
  161. {
  162. $where['c.week_id'] = intval(I('week_id'));
  163. }
  164. if(I('subject_id', 0) > 0)
  165. {
  166. $where['c.subject_id'] = intval(I('subject_id'));
  167. }
  168. if(I('interval_id', 0) > 0)
  169. {
  170. $where['c.interval_id'] = intval(I('interval_id'));
  171. }
  172. if(I('room_id', 0) > 0)
  173. {
  174. $where['c.room_id'] = intval(I('room_id'));
  175. }
  176. }
  177. return $where;
  178. }
  179. /**
  180. * [SaveInfo 教师课程添加/编辑页面]
  181. * @author Devil
  182. * @blog http://gong.gg/
  183. * @version 0.0.1
  184. * @datetime 2016-12-14T21:37:02+0800
  185. */
  186. public function SaveInfo()
  187. {
  188. // 课程信息
  189. if(empty($_REQUEST['id']))
  190. {
  191. $data = array('teacher_id'=>I('teacher_id'));
  192. $request_url = U('Admin/Teacher/Index');
  193. } else {
  194. $data = M('Course')->find(I('id'));
  195. $request_url = U('Admin/Course/Index');
  196. }
  197. $this->assign('request_url', $request_url);
  198. $this->assign('data', $data);
  199. // 字段
  200. $field = array('id', 'name');
  201. // 条件
  202. $where = array('is_enable'=>1);
  203. // 班级
  204. $this->assign('class_list', $this->GetClassList());
  205. // 教室
  206. $this->assign('room_list', $this->GetRoomList());
  207. // 周天
  208. $this->assign('week_list', M('Week')->field($field)->where($where)->select());
  209. // 教师贯通的科目
  210. $subject_list = M('TeacherSubject')->alias('AS ts')->join('__SUBJECT__ AS s ON ts.subject_id = s.id')->where(array('ts.teacher_id'=>$data['teacher_id']))->field(array('s.id AS id', 's.name AS name'))->select();
  211. $this->assign('subject_list', $subject_list);
  212. // 时段
  213. $this->assign('interval_list', M('Interval')->field($field)->where($where)->select());
  214. $this->display('SaveInfo');
  215. }
  216. /**
  217. * [Save 教师课程添加/编辑]
  218. * @author Devil
  219. * @blog http://gong.gg/
  220. * @version 0.0.1
  221. * @datetime 2016-12-14T21:37:02+0800
  222. */
  223. public function Save()
  224. {
  225. // 是否ajax请求
  226. if(!IS_AJAX)
  227. {
  228. $this->error(L('common_unauthorized_access'));
  229. }
  230. // 添加
  231. if(empty($_POST['id']))
  232. {
  233. $this->Add();
  234. // 编辑
  235. } else {
  236. $this->Edit();
  237. }
  238. }
  239. /**
  240. * [Add 教师课程添加]
  241. * @author Devil
  242. * @blog http://gong.gg/
  243. * @version 0.0.1
  244. * @datetime 2016-12-18T16:20:59+0800
  245. */
  246. private function Add()
  247. {
  248. // 教师课程模型
  249. $m = D('Course');
  250. // 数据自动校验
  251. if($m->create($_POST, 1))
  252. {
  253. // 校验数据不能重复添加
  254. $this->IsExistData();
  255. // 额外数据处理
  256. $m->add_time = time();
  257. $m->semester_id = MyC('admin_semester_id');
  258. // 写入数据库
  259. if($m->add())
  260. {
  261. $this->ajaxReturn(L('common_operation_add_success'));
  262. } else {
  263. $this->ajaxReturn(L('common_operation_add_error'), -100);
  264. }
  265. } else {
  266. $this->ajaxReturn($m->getError(), -1);
  267. }
  268. }
  269. /**
  270. * [Edit 教师课程编辑]
  271. * @author Devil
  272. * @blog http://gong.gg/
  273. * @version 0.0.1
  274. * @datetime 2016-12-17T22:13:40+0800
  275. */
  276. private function Edit()
  277. {
  278. // 教师课程模型
  279. $m = D('Course');
  280. // 数据自动校验
  281. if($m->create($_POST, 2))
  282. {
  283. // 校验数据不能重复添加
  284. $this->IsExistData();
  285. // 移除 id
  286. unset($m->id, $m->teacher_id);
  287. // 更新数据库
  288. if($m->where(array('id'=>I('id'), 'teacher_id'=>I('teacher_id')))->save())
  289. {
  290. $this->ajaxReturn(L('common_operation_edit_success'));
  291. } else {
  292. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  293. }
  294. }
  295. }
  296. /**
  297. * [IsExistData 校验数据不能重复添加]
  298. * @author Devil
  299. * @blog http://gong.gg/
  300. * @version 0.0.1
  301. * @datetime 2017-01-08T22:08:46+0800
  302. */
  303. private function IsExistData()
  304. {
  305. // 课程模型
  306. $m = M('Course');
  307. // 整条数据是否已存在
  308. $where = array(
  309. 'teacher_id' => I('teacher_id'),
  310. 'class_id' => I('class_id'),
  311. 'subject_id' => I('subject_id'),
  312. 'week_id' => I('week_id'),
  313. 'interval_id' => I('interval_id'),
  314. 'room_id' => I('room_id'),
  315. 'semester_id' => MyC('admin_semester_id'),
  316. );
  317. $temp = $m->where($where)->getField('id');
  318. if(!empty($temp))
  319. {
  320. $this->ajaxReturn(L('common_data_is_exist_error'), -2);
  321. }
  322. // 学期-教室-周天-时段 是否已存在
  323. $where = array(
  324. 'week_id' => I('week_id'),
  325. 'interval_id' => I('interval_id'),
  326. 'room_id' => I('room_id'),
  327. 'semester_id' => MyC('admin_semester_id'),
  328. );
  329. $temp = $m->where($where)->getField('id');
  330. if(!empty($temp))
  331. {
  332. // 如果编辑的不是当前数据则校验
  333. if($temp != I('id'))
  334. {
  335. $this->ajaxReturn(L('course_room_occupy_tips'), -2);
  336. }
  337. }
  338. }
  339. /**
  340. * [Delete 教师课程删除]
  341. * @author Devil
  342. * @blog http://gong.gg/
  343. * @version 0.0.1
  344. * @datetime 2016-12-15T11:03:30+0800
  345. */
  346. public function Delete()
  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->ajaxReturn(L('common_param_error'), -1);
  357. }
  358. // 删除数据
  359. if(M('Course')->delete(I('id')))
  360. {
  361. $this->ajaxReturn(L('common_operation_delete_success'));
  362. } else {
  363. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  364. }
  365. }
  366. /**
  367. * [StateUpdate 状态更新]
  368. * @author Devil
  369. * @blog http://gong.gg/
  370. * @version 0.0.1
  371. * @datetime 2017-01-12T22:23:06+0800
  372. */
  373. public function StateUpdate()
  374. {
  375. // 参数
  376. if(empty($_POST['id']) || !isset($_POST['state']))
  377. {
  378. $this->ajaxReturn(L('common_param_error'), -1);
  379. }
  380. // 数据更新
  381. if(M('Course')->where(array('id'=>I('id')))->save(array('state'=>I('state'))))
  382. {
  383. $this->ajaxReturn(L('common_operation_edit_success'));
  384. } else {
  385. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  386. }
  387. }
  388. }
  389. ?>