TeacherController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. <?php
  2. /**
  3. * 教师管理控制器类
  4. * @author jiangfei
  5. * @date 2015-08-03 17:30:00
  6. * @company 上海风车教育有限公司.
  7. */
  8. class TeacherController extends Controller
  9. {
  10. private $post = array(
  11. 0 => "任课教师",
  12. 1 => "班主任",
  13. 2 => "教研组长",
  14. 3 => "年级主任",
  15. 4 => "任课教师",
  16. 5 => "校长",
  17. );
  18. private $subjects = array(
  19. 1 => "语文",
  20. 3 => "数学",
  21. 6 => "数学",
  22. 2 => "英语",
  23. 12 => "物理",
  24. 13 => "化学",
  25. 14 => "生物",
  26. 7 => "政治",
  27. 8 => "历史",
  28. 9 => "地理",
  29. 10 => "信息技术",
  30. );
  31. private $title = array(
  32. 0 => "",
  33. 1 => "一级教师",
  34. 2 => "二级教师",
  35. 3 => "三级教师",
  36. 4 => "高级教师",
  37. );
  38. // 教师列表
  39. public function actionIndex()
  40. {
  41. $data = array();
  42. // 判断是否教员组长
  43. $teachArray = array();
  44. $teacherList = array();
  45. $realname = safe_replace(Yii::app()->request->getQuery('realname'));
  46. $subject_id = safe_replace(Yii::app()->request->getQuery('subject_id'));
  47. $class_id = safe_replace(Yii::app()->request->getQuery('class_id'));
  48. if (!isset($subject_id) || empty($subject_id)) {
  49. $subject_id = 0;
  50. }
  51. $class_data = array();
  52. if(is_numeric($realname)){
  53. //搜索教师账号
  54. $BTeacher=new BusinessTeacher();
  55. $teacherIdArr=$BTeacher->getTeacherIdByNumber($realname,$this->schoolId);
  56. if(!$teacherIdArr){
  57. $teachersList=array();
  58. $teachersList['rs']=array();
  59. $teachersList["pager"]=new CPagination(0, 10);
  60. }else{
  61. $teachersList = $this->schoolManager->getTeachersListre($teacherIdArr, $subject_id, $class_id, array("add_time desc"));
  62. }
  63. }else{
  64. $teachersList = $this->schoolManager->getTeachersListre($realname, $subject_id, $class_id, array("add_time desc"));
  65. }
  66. $classes = ClassModel::model()->findAll('semester_id=:semester_id and is_hide=0', array(':semester_id' => $this->semesterId));
  67. if ($classes) {
  68. foreach ($classes as $v) {
  69. $class_data[$v->class_id] = $v->class_name;
  70. }
  71. }
  72. $data['teachArray'] = $teachersList["rs"];
  73. $data['realname'] = $realname;
  74. $data['class_id'] = $class_id;
  75. $data['class_data'] = $class_data;
  76. $data['subject_id'] = $subject_id;
  77. $data['pages'] = $teachersList["pager"];
  78. $data['page_total'] = $teachersList["pager"]->rowsCount;
  79. $data['postlist'] = $this->post;
  80. $data['subjectslist'] = Yii::app()->params['subjectId'];
  81. if(isset($data['subjectslist'][6])){
  82. unset($data['subjectslist'][6]);
  83. }
  84. if(isset($data['subjectslist'][51])){
  85. unset($data['subjectslist'][51]);
  86. }
  87. if(isset($data['subjectslist'][18])){
  88. unset($data['subjectslist'][18]);
  89. }
  90. if(isset($data['subjectslist'][19])){
  91. unset($data['subjectslist'][19]);
  92. }
  93. $data['titlelist'] = $this->title;
  94. $this->render('index', $data);
  95. }
  96. // 添加老师
  97. public function actionAdd()
  98. {
  99. if (Yii::app()->request->getIsPostRequest()) {
  100. $class_id = Yii::app()->request->getPost('class_array');
  101. $teacher_name = safe_replace(Yii::app()->request->getPost('teacher_name'));
  102. //$account = safe_replace(Yii::app()->request->getPost('account'));
  103. $phone_number = safe_replace(Yii::app()->request->getPost('phone_number'));
  104. $teacher_email = safe_replace(Yii::app()->request->getPost('teacher_email'));
  105. $teacher_age = safe_replace(Yii::app()->request->getPost('teacher_age'));
  106. $teacher_sex = safe_replace(Yii::app()->request->getPost('teacher_sex'));
  107. $status = safe_replace(Yii::app()->request->getPost('status'));
  108. $password = safe_replace(Yii::app()->request->getPost('password'));
  109. $password2 = safe_replace(Yii::app()->request->getPost('password2'));
  110. $posts = safe_replace(Yii::app()->request->getPost('posts'));
  111. $title = safe_replace(Yii::app()->request->getPost('title'));
  112. $subjects = safe_replace(Yii::app()->request->getPost('subjects'));
  113. $grade = safe_replace(Yii::app()->request->getPost('grade')); ///年级
  114. //$changeSchool=safe_replace(Yii::app()->request->getPost('changeSchool')); //教师更新学校标记
  115. $changeSchool=0;
  116. if (empty($teacher_name) || empty($phone_number) || empty($password)) {
  117. Yii::app()->jump->error('请确认教师信息是否填写完整!');
  118. }
  119. if (!preg_match("/1[3456789]{1}\d{9}$/", $phone_number)) {
  120. Yii::app()->jump->error('手机号格式有误!');
  121. }
  122. if ($password != $password2) {
  123. Yii::app()->jump->error('两次输入的密码不一致!');
  124. } elseif (strlen($password) < 5) {
  125. Yii::app()->jump->error('密码长度至少需要6位数!');
  126. }
  127. // 判断当前手机是否在当前学校已使用
  128. $businessTeacher=BusinessTeacher::model()->find('phone_number=:num ', array(':num' => $phone_number));
  129. if ($businessTeacher && !$changeSchool) {
  130. Yii::app()->jump->error('手机号已存在!');
  131. }
  132. //同学科同年级只能有六个年级主任
  133. if ($posts==3){
  134. if(!$grade){
  135. Yii::app()->jump->error('岗位为年级主任时必须填写年级!');
  136. }
  137. $has_3_posts = Teacher::model()->count('grade=:grade and posts=:posts and subjects=:subjects',
  138. array(':grade'=> $grade, ':posts'=> $posts, ':subjects'=> $subjects));
  139. if ($has_3_posts>=6){
  140. Yii::app()->jump->error('同学科,同年级最多可以创建六个主任账号!');
  141. }
  142. }else{
  143. $grade=0;
  144. }
  145. // 将教师登录账号插入主数据
  146. $password=md5(sha1($password));
  147. $time=time();
  148. $businessRs=false;
  149. $transaction_conn = $this->conn->beginTransaction();
  150. try{
  151. // $phoneLog=BusinessTeacherMoveLog::model()->find('phone_number=:pn and school_id=:sid',array(':pn'=>$phone_number,':sid'=>$this->schoolId));
  152. // if($phoneLog){
  153. // $teacherId=$phoneLog->teacher_id;
  154. // }else{
  155. // $teacherId=getUniqueId($this->schoolId);
  156. // }
  157. // if($changeSchool && $businessTeacher){
  158. //
  159. // $insertLog="insert into teacher_move_log(`teacher_id`,`phone_number`,`school_id`,`move_time`,`because`) values ('".$businessTeacher->teacher_id."','".$phone_number."','".$businessTeacher->school_id."','".$time."','1')";
  160. // $this->conn->createCommand($insertLog)->execute();
  161. // $this->conn->createCommand("delete from teacher where teacher_id='".$businessTeacher->teacher_id."'")->execute();
  162. // }
  163. $teacherId=getUniqueId($this->schoolId);
  164. $insertSql="insert into xb_teacher(`teacher_id`,`phone_number`,`password`,`school_id`,`add_time`,`status`) values ('".$teacherId."','".$phone_number."','".$password."','".$this->schoolId."','".$time."','".$status."')";
  165. $this->conn->createCommand($insertSql)->execute();
  166. $businessRs=true;
  167. }catch (\Exception $e){
  168. $transaction_conn->rollback();
  169. Yii::app()->jump->error('添加老师失败!');
  170. }
  171. if($businessRs){
  172. $transaction = $this->sConn->beginTransaction();
  173. try{
  174. $teacherSql="insert into teacher(`teacher_id`,`teacher_name`,`teacher_email`,`teacher_age`,`teacher_sex`,`add_time`,`status`,`coach_id`,`posts`,`title`,`subjects`,`grade`) values ";
  175. $teacherSql.="('".$teacherId."','".$teacher_name."','".$teacher_email."','".$teacher_age."','".$teacher_sex."','".$time."','".$status."','".$this->coachId."','".$posts."','".$title."','".$subjects."','".$grade."')";
  176. $this->sConn->createCommand($teacherSql)->execute();
  177. $transaction_conn->commit();
  178. $transaction->commit();
  179. $this->redirect(array('teacher/index'));
  180. }catch (\Exception $e){
  181. $transaction->rollback();
  182. $transaction_conn->rollback();
  183. Yii::app()->jump->error('添加老师失败!');
  184. }
  185. }
  186. }
  187. $data = array();
  188. // 获取当前学校当前学期班级未分配班级
  189. $classInfo = ClassModel::model()->findAll('semester_id=' . Yii::app()->session['session_semester_id']);
  190. // 获取当前学期已分配班级
  191. $getClass = TeacherToClass::model()->findAll('semester_id=' . Yii::app()->session['session_semester_id']);
  192. foreach ($classInfo as $key => $val) {
  193. foreach ($getClass as $k => $v) {
  194. if ($v->class_id == $val->class_id) {
  195. unset($classInfo[$key]);
  196. }
  197. }
  198. }
  199. if (Yii::app()->session['coachInfo']['leader'] == 0) {
  200. // 获取当前教员管理班级
  201. $conclass = array();
  202. $coachClass = $this->schoolManager->getCoachClassesRelations($this->coachId, "OBJ");
  203. foreach ($coachClass as $cc => $aa) {
  204. array_push($conclass, $aa->class_id);
  205. }
  206. foreach ($classInfo as $ci => $cf) {
  207. if (!in_array($cf->class_id, $conclass)) {
  208. unset($classInfo[$ci]);
  209. }
  210. }
  211. }
  212. if (empty($classInfo)) {
  213. //Yii::app()->jump->error('暂无班级可分配请先添加班级!',$this->createUrl('classes/index'));
  214. }
  215. $data['classInfo'] = $classInfo;
  216. $this->render('add', $data);
  217. }
  218. public function actionVerifyphone_number()
  219. {
  220. $phone_number = safe_replace(Yii::app()->request->getPost('phone_number'));
  221. $is_edit = safe_replace(Yii::app()->request->getPost('is_edit'));
  222. if (!preg_match("/1[3456789]{1}\d{9}$/", $phone_number)) {
  223. Yii::app()->jump->error('手机号格式有误!');
  224. }
  225. if ($is_edit == 1) {
  226. $teacher_id = safe_replace(Yii::app()->request->getPost('teacher_id'));
  227. $getBtteacher = BusinessTeacher::model()->find('teacher_id=:tid', array(':tid' => $teacher_id));
  228. $Btteacher = BusinessTeacher::model()->find('phone_number=:phone_number', array(':phone_number' => $phone_number));
  229. if (!$getBtteacher) {
  230. echo json_encode(array('status' => 0));
  231. exit;
  232. }
  233. if(!$Btteacher || $Btteacher->teacher_id==$teacher_id){
  234. exit(json_encode(array('status' => 1)));
  235. }elseif($Btteacher->school_id==$this->schoolId){
  236. exit(json_encode(array('status' => -1)));
  237. }else{
  238. //查学校
  239. $teacherSchool=BusinessSchool::model()->find('school_id=:sid',array(':sid'=>$Btteacher->school_id));
  240. exit(json_encode(array('status' => 0,'data'=>$teacherSchool->school_name)));
  241. }
  242. } else {
  243. $teacher=BusinessTeacher::model()->find('phone_number=:phone_number', array(':phone_number' => $phone_number));
  244. if($teacher){
  245. if($teacher->school_id==$this->schoolId){
  246. echo json_encode(array('status' => -1));
  247. exit;
  248. }else{
  249. //查学校
  250. $teacherSchool=BusinessSchool::model()->find('school_id=:sid',array(':sid'=>$teacher->school_id));
  251. echo json_encode(array('status' => 0,'data'=>$teacherSchool->school_name));
  252. exit;
  253. }
  254. }
  255. echo json_encode(array('status' => 1));
  256. exit;
  257. }
  258. }
  259. // 编辑教师
  260. public function actionEdit()
  261. {
  262. $thid = safe_replace(Yii::app()->request->getQuery('thid'));
  263. if (empty($thid)) {
  264. Yii::app()->jump->error('您访问的页面不存在!');
  265. }
  266. // 当前教师信息
  267. $teacherInfo = Teacher::model()->find('teacher_id=:tid', array(':tid' => $thid));
  268. $getBtteacher = BusinessTeacher::model()->find('teacher_id=:tid', array(':tid' => $thid));
  269. if (empty($teacherInfo) || empty($getBtteacher)) {
  270. Yii::app()->jump->error('未找到该教师信息!');
  271. }
  272. if (Yii::app()->request->getIsPostRequest()) {
  273. $data = array();
  274. //$account = safe_replace(Yii::app()->request->getPost('account'));
  275. $teacher_name = safe_replace(Yii::app()->request->getPost('teacher_name'));
  276. $phone_number = safe_replace(Yii::app()->request->getPost('phone_number'));
  277. $teacher_email = safe_replace(Yii::app()->request->getPost('teacher_email'));
  278. $teacher_age = safe_replace(Yii::app()->request->getPost('teacher_age'));
  279. $teacher_sex = safe_replace(Yii::app()->request->getPost('teacher_sex'));
  280. $status = safe_replace(Yii::app()->request->getPost('status'));
  281. $password = safe_replace(Yii::app()->request->getPost('password'));
  282. $password2 = safe_replace(Yii::app()->request->getPost('password2'));
  283. //$class_id = Yii::app()->request->getPost('class_array');
  284. $posts = safe_replace(Yii::app()->request->getPost('posts'));
  285. $title = safe_replace(Yii::app()->request->getPost('title'));
  286. $subjects = safe_replace(Yii::app()->request->getPost('subjects'));
  287. $grade = safe_replace(Yii::app()->request->getPost('grade'));
  288. //$changeSchool = safe_replace(Yii::app()->request->getPost('changeSchool'));
  289. $changeSchool=0;
  290. if (empty($teacher_name) || empty($phone_number)) {
  291. Yii::app()->jump->error('请确认教师信息是否填写完整!');
  292. }
  293. $businessTeacher=BusinessTeacher::model()->find('phone_number=:num ', array(':num' => $phone_number));
  294. if($businessTeacher && $businessTeacher->teacher_id!=$thid){
  295. if($businessTeacher->school_id==$this->schoolId){
  296. Yii::app()->jump->error('手机号在本校已存在!');
  297. }elseif(!$changeSchool){
  298. Yii::app()->jump->error('手机号已存在!');
  299. }
  300. }
  301. if (!empty($password) && !empty($password2) && ($password == $password2)) {
  302. if (strlen($password) < 5) {
  303. Yii::app()->jump->error('密码长度至少需要6位数!');
  304. }
  305. //$teacherInfo->password = md5(sha1($password));
  306. //$getBtteacher->password = md5(sha1($password));
  307. $data = array(
  308. 'phone_number' => $phone_number,
  309. 'password' => md5(sha1($password)),
  310. 'status' => $status
  311. );
  312. } else {
  313. $data = array(
  314. 'phone_number' => $phone_number,
  315. 'status' => $status
  316. );
  317. }
  318. //同学科同年级只能有六个年级主任
  319. if ($posts==3){
  320. if(!$grade){
  321. Yii::app()->jump->error('岗位为年级主任时必须填写年级!');
  322. }
  323. $has_3_posts = Teacher::model()->count('teacher_id!=:teacher_id and grade=:grade and posts=:posts and subjects=:subjects',
  324. array(':teacher_id'=>$thid,':grade'=> $grade, ':posts'=> $posts, ':subjects'=> $subjects));
  325. if ($has_3_posts>=6){
  326. Yii::app()->jump->error('同学科,同年级最多可以创建六个主任账号!');
  327. }
  328. }else{
  329. $grade=0;
  330. }
  331. // 将教师登录账号插入主数据
  332. $time=time();
  333. $businessRs=false;
  334. $transaction_conn = $this->conn->beginTransaction();
  335. try{
  336. $updateSql="update xb_teacher set ";
  337. $setArr=array();
  338. foreach ($data as $key => $val){
  339. $setArr[]="`".$key."`='".$val."'";
  340. }
  341. $updateSql.=implode(',',$setArr);
  342. $updateSql.=" where teacher_id='".$thid."'";
  343. $this->conn->createCommand($updateSql)->execute();
  344. $businessRs=true;
  345. }catch (\Exception $e){
  346. $transaction_conn->rollback();
  347. Yii::app()->jump->error('修改老师失败!');
  348. }
  349. if($businessRs){
  350. $transaction = $this->sConn->beginTransaction();
  351. try{
  352. $teachdata['teacher_name'] = $teacher_name;
  353. $teachdata['teacher_email'] = $teacher_email;
  354. $teachdata['teacher_age'] = $teacher_age;
  355. $teachdata['teacher_sex'] = $teacher_sex;
  356. $teachdata['add_time'] = $time;
  357. $teachdata['status'] = $status;
  358. $teachdata['posts'] = $posts;
  359. $teachdata['title'] = $title;
  360. $teachdata['subjects'] = $subjects;
  361. $teachdata['grade'] = $grade;
  362. $updateSql="update teacher set ";
  363. $setArr=array();
  364. foreach ($teachdata as $key => $val){
  365. $setArr[]="`".$key."`='".$val."'";
  366. }
  367. $updateSql.=implode(',',$setArr);
  368. $updateSql.=" where teacher_id='".$thid."'";
  369. $this->sConn->createCommand($updateSql)->execute();
  370. $transaction_conn->commit();
  371. $transaction->commit();
  372. $this->redirect(array('teacher/index'));
  373. }catch (\Exception $e){
  374. $transaction->rollback();
  375. $transaction_conn->rollback();
  376. Yii::app()->jump->error('修改失败!');
  377. }
  378. }
  379. }
  380. $data = array();
  381. // 当前教师所执教班级
  382. $getClass = TeacherToClass::model()->findAll('teacher_id=:tid', array(':tid' => $thid));
  383. $toTeacher = array();
  384. foreach ($getClass as $va) {
  385. array_push($toTeacher, $va->class_id);
  386. }
  387. // 获取当前学校班级
  388. $classInfo = ClassModel::model()->findAll('semester_id=' . Yii::app()->session['session_semester_id']);
  389. if (Yii::app()->session['coachInfo']['leader'] == 0) {
  390. // 获取当前教员管理班级
  391. $coachClass = $this->schoolManager->getCoachClassesRelations($this->coachId, "OBJ");
  392. $ttttt = array();
  393. foreach ($coachClass as $cc => $aa) {
  394. array_push($ttttt, $aa->class_id);
  395. }
  396. foreach ($classInfo as $ci => $cf) {
  397. if (!in_array($cf->class_id, $ttttt)) {
  398. unset($classInfo[$ci]);
  399. }
  400. }
  401. }
  402. $data['classStr'] = implode(',', $toTeacher);
  403. $data['toTeacher'] = $toTeacher;
  404. $data['classInfo'] = $classInfo;
  405. $data['teacherInfo'] = $teacherInfo;
  406. $data['getBtteacher'] = $getBtteacher;
  407. $this->render('edit', $data);
  408. }
  409. public function actionDelTeacherAct()
  410. {
  411. $teacherId = Req::post("teacherId");
  412. echo json_encode($this->schoolManager->purgeTeacher($teacherId));
  413. exit;
  414. }
  415. public function actionExcelInfo(){
  416. $this->render('excelinfo');
  417. }
  418. //导入教师信息
  419. public function actionImport(){
  420. ini_set ('memory_limit', '300M');
  421. if(!$_FILES){
  422. Yii::app()->jump->error('文件大小超过范围');
  423. }else {
  424. if (!isset($_FILES['file']) || !isset($_FILES['file']['size']) || $_FILES['file']['size'] > 5242880) {
  425. Yii::app()->jump->error('文件大小超过范围');
  426. }
  427. }
  428. $uploader = new Uploader("upload/tmpDir/UploadClassesStudentsList/{$this->schoolId}/");
  429. $uploader->allowTypes = array("xls","xlsx");
  430. $uploader->fieldsMappings = array("exname" => array(0 => $this->schoolId));
  431. $file = $uploader->act();
  432. $file = Arr::current(Arr::current($file));
  433. if (!$file || !isset($file["error"]))
  434. Yii::app()->jump->error('请上传的Excel!');
  435. if ($file["error"] != 0) {
  436. switch ($file["error"]) {
  437. case 2001:
  438. Yii::app()->jump->error('文件类型不符');
  439. break;
  440. case 2002:
  441. Yii::app()->jump->error('文件大小超出允许范围');
  442. break;
  443. default:
  444. Yii::app()->jump->error('上传失败');
  445. break;
  446. }
  447. }
  448. $inputFileName = $file["src"];
  449. // 导入PHPExcel类
  450. Yii::import('application.extensions.*');
  451. require_once('phpexcel/PHPExcel/IOFactory.php');
  452. try{
  453. $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
  454. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
  455. //FIXME 2019-12-02
  456. @unlink($file["src"]);
  457. }catch (Exception $e){
  458. //FIXME 2019-12-02
  459. @unlink($file["src"]);
  460. Yii::app()->jump->error('Excel格式不正确:'.$e->getMessage());
  461. }
  462. if(!$sheetData)
  463. {
  464. Yii::app()->jump->error('请正确的Excel!');
  465. }
  466. unset($objPHPExcel);
  467. //模板校验
  468. $excelTemp = array('A'=>'*教师姓名','B'=>'*登录手机号','C'=>'*学科','D'=>'*岗位','E'=>'职称','F'=>'执教班级','G'=>'教龄','H'=>'性别','I'=>'年级');
  469. if(count($excelTemp) != count($sheetData[1])){
  470. Yii::app()->jump->error('导入的excel格式与模版不一致,请重新上传');
  471. }
  472. foreach($excelTemp as $key=>$temp){
  473. if(!(isset($sheetData[1][$key]) && $sheetData[1][$key] == trim($temp))){
  474. Yii::app()->jump->error('导入的excel格式与模版不一致,请重新上传');
  475. }
  476. }
  477. unset($sheetData[1]);
  478. if(!$sheetData)
  479. {
  480. Yii::app()->jump->error('请输入教师数据');
  481. }
  482. $updateSql = '';
  483. $password = '123456';
  484. $busTeacherData = $schoolTeacherData = array(); //新增记录
  485. $updateTeacherData=array(); //修改教师信息
  486. $delOldTeacherClass=array(); //删除同科目原执教教师记录
  487. $addTeacherClass=array(); //添加新执教教师
  488. Yii::app()->jump->setErrorWait('5');
  489. $schoolId = Yii::app()->session['coachInfo']['school_id'];
  490. $postArr = $this->post;
  491. unset($postArr[0]);
  492. $titleArr = $this->title;
  493. unset($titleArr[0]);
  494. $subjectArr = array(
  495. 3 => "数学",
  496. 8 => "英语",
  497. 9 => "语文",
  498. 12 => "物理",
  499. 13 => "化学",
  500. 14 => "生物",
  501. 15 => "政治",
  502. 16 => "历史",
  503. 17 => "地理",
  504. 20 => "其它",
  505. );
  506. $subjectArr=Yii::app()->params['subjectId'];
  507. if(Yii::app()->params['section']==1){
  508. unset($subjectArr[6]);
  509. unset($subjectArr[51]);
  510. }
  511. $phoneArr = array();
  512. foreach($sheetData as $value){
  513. $value['B'] = trim($value['B']);
  514. if(!empty($value['B'])){
  515. $phoneArr[] = '"'.$this->myTrim($value['B']).'"';
  516. }
  517. }
  518. //查询老师手机号
  519. $phoneInfo = BusinessTeacher::model()->getAddTeacherInfo($phoneArr);
  520. $samePhone = array();
  521. foreach($sheetData as $val){
  522. $samePhone[] = ''.$val['B'];
  523. }
  524. $countSame = array_count_values($samePhone);
  525. $alert = "教师手机号有重复请重新上传";
  526. $check = false;
  527. $count = 0;
  528. foreach($countSame as $key=>$item){
  529. if(empty($key)) continue;
  530. if($item>1){
  531. $check = true;
  532. foreach($sheetData as $v){
  533. if($v['B'] == $key){
  534. $count++;
  535. $alert .= "</br>{$v['A']}:{$v['B']}";
  536. }
  537. }
  538. }
  539. }
  540. if($check){
  541. Yii::app()->jump->setHeight(100+$count*15);
  542. Yii::app()->jump->error($alert);
  543. }
  544. $classError=array();
  545. $directorCount=array();
  546. $classSubject=array();
  547. foreach ($sheetData as $val){
  548. if(empty($val['B'])){
  549. Yii::app()->jump->error('手机号不能为空');
  550. }
  551. $oldTeacherId=0;
  552. $phoneNum = trim($val['B']);
  553. $phoneNum = $this->myTrim($phoneNum);
  554. if(empty($val['A'])){
  555. Yii::app()->jump->error('教师姓名不能为空');
  556. }
  557. $teacherName = $this->myTrim($val['A']);
  558. if(preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z0-9·]+$/u', $teacherName)>0){
  559. $teacherName = $this->myTrim($val['A']);
  560. }elseif(preg_match('/[\x{4e00}-\x{9fa5}0-9]/u', $teacherName)>0){
  561. Yii::app()->jump->error('确保教师姓名是汉字</br>'.$teacherName);
  562. }
  563. // if(preg_match('/^[\x{4e00}-\x{9fa5}·]+$/u', $teacherName)>0){
  564. // $teacherName = $this->myTrim($val['A']);
  565. // }else{
  566. // Yii::app()->jump->error('确保教师姓名是汉字</br>'.$teacherName);
  567. // }
  568. if (!preg_match("/1[3456789]{1}\d{9}$/", $phoneNum)) {
  569. Yii::app()->jump->error("教师手机号有误请重新上传</br>{$teacherName}:$phoneNum");
  570. }
  571. if(isset($phoneInfo[$phoneNum])){
  572. // Yii::app()->jump->error("教师手机号已存在,勿重复添加</br>{$teacherName}:$phoneNum");
  573. //对比姓名
  574. $teacherAccount=$this->schoolManager->getTeacherAccount($phoneInfo[$phoneNum]);
  575. if($teacherAccount){
  576. // 当前教师信息
  577. $teacherInfo = Teacher::model()->find('teacher_id=:tid', array(':tid' => $phoneInfo[$phoneNum]));
  578. if($teacherAccount['school_id']!=$this->schoolId){
  579. Yii::app()->jump->error("该手机号已存在,请仔细核查是否填写错误,若需转校可联系技术支持处理</br>{$teacherName}:$phoneNum");
  580. }
  581. if($teacherInfo->teacher_name!=$teacherName){
  582. Yii::app()->jump->error("教师姓名与系统内教师不匹配,请检查</br>{$teacherName}:$phoneNum");
  583. }
  584. $oldTeacherId=$phoneInfo[$phoneNum];
  585. }
  586. }
  587. if(empty($val['C'])){
  588. Yii::app()->jump->error('学科不能为空');
  589. }
  590. $subject = $this->myTrim($val['C']);
  591. if(!in_array($subject,$subjectArr)){
  592. Yii::app()->jump->error($subject.'学科不存在');
  593. }
  594. $subjectId = array_search($subject,$subjectArr);
  595. if(empty($val['D'])){
  596. Yii::app()->jump->error('岗位不能为空');
  597. }
  598. $post = $this->myTrim($val['D']);
  599. if(!in_array($post,$postArr)){
  600. Yii::app()->jump->error($post.'岗位不存在');
  601. }
  602. $post = array_search($post,$postArr);
  603. $grade=$val['I'];
  604. if(!empty($val['I']) && !isset(Yii::app()->params['grade_config'][$val['I']])){
  605. Yii::app()->jump->error('年级设置不正确');
  606. }
  607. if($post==3){
  608. if(empty($val['I'])){
  609. Yii::app()->jump->error('岗位为年级主任时必须填写年级!');
  610. }
  611. if(isset($phoneInfo[$phoneNum]) && $teacherInfo){
  612. if($teacherInfo->posts!=3 || $teacherInfo->grade!=$grade || $teacherInfo->subjects!=$subjectId){
  613. if(!isset($directorCount[$subjectId][$grade])){
  614. $directorCount[$subjectId][$grade]=1;
  615. }else{
  616. $directorCount[$subjectId][$grade]++;
  617. }
  618. }
  619. }else{
  620. if(!isset($directorCount[$subjectId][$grade])){
  621. $directorCount[$subjectId][$grade]=1;
  622. }else{
  623. $directorCount[$subjectId][$grade]++;
  624. }
  625. }
  626. }else{
  627. $grade=0;
  628. }
  629. $title = $this->myTrim($val['E']);
  630. if(empty($title)){
  631. // Yii::app()->jump->error('职称不能为空');
  632. }
  633. if(!empty($val['E']) && !in_array($title,$titleArr)){
  634. Yii::app()->jump->error($val['E'].'职称不存在');
  635. }
  636. //执教班级
  637. $needAddClass=array();
  638. if(!empty($val['F'])){
  639. $val['F']=str_replace(";",";",$val['F']);
  640. $classNames=explode(";",$val['F']);
  641. $notFondClass=array();
  642. if($classNames){
  643. foreach ($classNames as $className){
  644. if(!$className) continue;
  645. $classId=$this->schoolManager->getClassInfoByClassName($className,$this->semesterId);
  646. //查询
  647. if($classId){
  648. if(isset($classSubject[$classId]) && $classSubject[$classId]['subjectId']==$subjectId){
  649. Yii::app()->jump->error('多名同学科教师不能执教同一班级<br>'.$val['A'].','.$classSubject[$classId]['name']);
  650. }else{
  651. $classSubject[$classId]=array(
  652. 'subjectId'=>$subjectId,
  653. 'name'=>$val['A']
  654. );
  655. }
  656. $classSubjectTeacher=$this->schoolManager->getTeacherByClassSubject($classId,$subjectId);
  657. if($classSubjectTeacher){
  658. //已有相同科目教师,覆盖原记录
  659. $delOldTeacherClass[]="delete from teacher_class_relation where class_id='{$classId}' and teacher_id='{$classSubjectTeacher['teacher_id']}'";
  660. }
  661. $needAddClass[]=$classId;
  662. }else{
  663. $notFondClass[]=$className;
  664. }
  665. }
  666. if($notFondClass){
  667. $classError[]=$teacherName.":".$phoneNum." ".implode(',',$notFondClass);
  668. }
  669. }
  670. }
  671. $title = intval(array_search($title,$titleArr));
  672. if($val['G'] && !is_numeric($val['G'])){
  673. Yii::app()->jump->error('教龄需填写数字');
  674. }
  675. $teacherAge = intval($val['G']);
  676. $sex = trim($val['H']);
  677. $teacherSex = 0;
  678. if(strstr($sex,'女') !== false) $teacherSex = 1;
  679. if($oldTeacherId){
  680. $teacherId=$oldTeacherId;
  681. $updateTeacherData[]="update teacher set teacher_age='{$teacherAge}',teacher_sex='{$teacherSex}',posts='{$post}',title='{$title}',subjects='{$subjectId}',grade='{$grade}' where teacher_id='{$teacherId}'";
  682. }else{
  683. $teacherId=getUniqueId($this->schoolId);
  684. $pwd = '"'.md5(sha1($password)).'"';
  685. $busTeacherData[] = array($teacherId,$phoneNum,$pwd,$schoolId,time());
  686. $schoolTeacherData[] = array($teacherId,'"'.$teacherName.'"',$teacherAge,$teacherSex,time(),$this->coachId,$post,$title,$subjectId,$grade);
  687. }
  688. if($needAddClass){
  689. foreach ($needAddClass as $cid){
  690. $addTeacherClass[]="('".$cid."','".$teacherId."','".$this->semesterId."')";
  691. }
  692. }
  693. }
  694. if($classError){
  695. Yii::app()->jump->error('以下老师的部分执教班级系统不存在,请核对后重新输入<br>'.implode('<br>',$classError));
  696. }
  697. if($directorCount){
  698. foreach ($directorCount as $subject=>$val){
  699. foreach ($val as $grade =>$num){
  700. $has_3_posts = Teacher::model()->count('grade=:grade and posts=:posts and subjects=:subjects',
  701. array(':grade'=> $grade, ':posts'=> 3, ':subjects'=> $subject));
  702. if(($has_3_posts+$num)>6){
  703. Yii::app()->jump->error('同学科,同年级最多可以创建6个年级主任账号');
  704. }
  705. }
  706. }
  707. }
  708. $businessCommit=false;
  709. if( $updateTeacherData || $busTeacherData) {
  710. $schInsertSql='';
  711. if($busTeacherData && $schoolTeacherData){
  712. $busInsertSql = 'INSERT INTO xb_teacher (`teacher_id`, `phone_number`,`password`,`school_id`,`add_time`) VALUES ';
  713. $schInsertSql = 'INSERT INTO teacher (`teacher_id`, `teacher_name`,`teacher_age`,`teacher_sex`,`add_time`,`coach_id`,`posts`,`title`,`subjects`,`grade`) VALUES ';
  714. foreach($busTeacherData as $btVal){
  715. $busInsertSql .= '('.implode(',',$btVal).'),';
  716. }
  717. foreach($schoolTeacherData as $stVal){
  718. $schInsertSql .= '('.implode(',',$stVal).'),';
  719. }
  720. $busInsertSql = substr($busInsertSql,0,-1);
  721. $schInsertSql = substr($schInsertSql,0,-1);
  722. $busTran = $this->conn->beginTransaction();
  723. try{
  724. $this->conn->createCommand($busInsertSql)->execute();
  725. $businessCommit=true;
  726. }catch(Exception $e){
  727. $busTran->rollBack();
  728. Yii::app()->jump->error('教师批量上传失败!');
  729. }
  730. }
  731. if($businessCommit || !$busTeacherData){
  732. $transaction = $this->sConn->beginTransaction();
  733. try{
  734. if($schInsertSql){
  735. $this->sConn->createCommand($schInsertSql)->execute();
  736. }
  737. if($updateTeacherData){
  738. foreach ($updateTeacherData as $sql){
  739. $this->sConn->createCommand($sql)->execute();
  740. }
  741. }
  742. if($delOldTeacherClass){
  743. foreach ($delOldTeacherClass as $sql){
  744. $this->sConn->createCommand($sql)->execute();
  745. }
  746. }
  747. if($addTeacherClass){
  748. $insertTeacherClassRelationSql="insert into teacher_class_relation(`class_id`,`teacher_id`,`semester_id`) values ".implode(',',$addTeacherClass);
  749. $this->sConn->createCommand($insertTeacherClassRelationSql)->execute();
  750. }
  751. if($businessCommit){
  752. $busTran->commit();
  753. }
  754. $transaction->commit();
  755. $url=$this->createUrl('/teacher/index');
  756. $get_cookie = Yii::app()->request->getCookies();
  757. $nowAction=strtolower($this->controller.'/'.$this->action);
  758. if (isset($get_cookie['needMemoryAction']) && in_array($nowAction,Yii::app()->params['needMemoryAction'])) {
  759. $url=$get_cookie['needMemoryAction'];
  760. unset(Yii::app()->request->cookies['needMemoryAction']);
  761. }
  762. Yii::app()->jump->error('教师批量上传成功!',$url);
  763. }catch(Exception $e){
  764. if($businessCommit){
  765. $busTran->rollBack();
  766. }
  767. $transaction->rollBack();
  768. Yii::app()->jump->error('教师批量上传失败!');
  769. }
  770. }
  771. }else{
  772. Yii::app()->jump->error('教师批量上传失败!');
  773. }
  774. }
  775. protected function myTrim($str)
  776. {
  777. $search = array(" "," ","\n","\r","\t");
  778. $replace = array("","","","","");
  779. return str_replace($search, $replace, $str);
  780. }
  781. //技术支持修改手机号功能
  782. public function actionChange(){
  783. $phone_number = safe_replace(Yii::app()->request->getPost('phone_number'));
  784. if (Yii::app()->request->getIsPostRequest()) {
  785. if(isset(Yii::app()->session['testFlag']) && (Yii::app()->session['testFlag']==1)){
  786. if (!preg_match("/1[3456789]{1}\d{9}$/", $phone_number)) {
  787. Yii::app()->jump->error('手机号格式有误!');
  788. }
  789. // 判断当前手机是否在当前学校已使用
  790. $getPhone = BusinessTeacher::model()->find('phone_number=:phnum ', array(':phnum' => $phone_number));
  791. if (!$getPhone) {
  792. Yii::app()->jump->error('该手机号不存在!');
  793. }elseif($getPhone->school_id==$this->schoolId){
  794. Yii::app()->jump->error('该手机号已在当前学校注册!');
  795. }
  796. $allowChange=false;
  797. $needle='1';
  798. //校验修改后号码是否存在
  799. for($i=1;$i<10;$i++){
  800. $newPhoneNumber = substr_replace($phone_number,$i,strpos($phone_number,$needle),strlen($needle));
  801. $checkNumber=BusinessTeacher::model()->find('phone_number=:phnum ', array(':phnum' => $newPhoneNumber));
  802. if(!$checkNumber){
  803. $allowChange=true;
  804. break;
  805. }
  806. }
  807. if(!$allowChange){
  808. Yii::app()->jump->error('该手机号去重次数过多,不允许再次去重!');
  809. }
  810. if (BusinessTeacher::model()->updateAll(array('phone_number'=>$newPhoneNumber), "phone_number=:phone_number", array(":phone_number" => $phone_number))) {
  811. Yii::app()->jump->error('手机号修改成功!');
  812. }else{
  813. Yii::app()->jump->error('手机号修改失败!');
  814. }
  815. }else{
  816. Yii::app()->jump->error('请联系技术支持处理!');
  817. }
  818. }
  819. $this->render('change');
  820. }
  821. public function actionGetTeacherForNumber()
  822. {
  823. $phone_number = safe_replace(Yii::app()->request->getPost('phone_number'));
  824. if (!preg_match("/1[3456789]{1}\d{9}$/", $phone_number)) {
  825. echo json_encode(array('status' => 1));
  826. }
  827. $teacher=BusinessTeacher::model()->find('phone_number=:phone_number', array(':phone_number' => $phone_number));
  828. if($teacher){
  829. if($teacher->school_id==$this->schoolId){
  830. echo json_encode(array('status' => -1));
  831. exit;
  832. }else{
  833. $schoolName='';
  834. $teacherName='';
  835. //查学校
  836. $teacherSchool=BusinessSchool::model()->find('school_id=:sid',array(':sid'=>$teacher->school_id));
  837. if($teacherSchool){
  838. $schoolName=$teacherSchool->school_name;
  839. }
  840. //查询教师信息
  841. $schoolConn=$this->getSchoolDbConnection($teacher->school_id);
  842. if($schoolConn){
  843. $teacherInfo=$this->getSchoolDbConnection($teacher->school_id)->createCommand("select teacher_name from teacher where teacher_id='{$teacher->teacher_id}'")->queryRow();
  844. if($teacherInfo){
  845. $teacherName=$teacherInfo['teacher_name'];
  846. }
  847. }
  848. $data=array(
  849. 'schoolName'=>$schoolName,
  850. 'teacherName'=>$teacherName
  851. );
  852. exit(json_encode(array('status' => 0,'data'=>$data)));
  853. }
  854. }else{
  855. exit(json_encode(array('status' => -2)));
  856. }
  857. }
  858. /**
  859. * 导出教师
  860. */
  861. public function actionBatchImport(){
  862. $realname = safe_replace(Yii::app()->request->getQuery('realname'));
  863. $subject_id = safe_replace(Yii::app()->request->getQuery('subject_id'));
  864. $class_id = safe_replace(Yii::app()->request->getQuery('class_id'));
  865. $excel_title = '教师信息导出';
  866. $condition = array();
  867. // $condition[] = "(tcr.semester_id='{$this->semesterId}' or tcr.semester_id is null)";
  868. $condition[] = "(c.is_hide=0 or c.is_hide is null)";
  869. if($realname){
  870. $condition[] = "t.teacher_name like '%{$realname}%'";
  871. }
  872. if($subject_id){
  873. $condition[] = "t.subjects = {$subject_id}";
  874. }
  875. if($class_id){
  876. $condition[] = "tcr.class_id = '{$class_id}'";
  877. }
  878. if($condition){
  879. $condition = " where ".implode(" and ", $condition);
  880. }else{
  881. $condition = '';
  882. }
  883. $sql = "select t.*,GROUP_CONCAT(c.class_name SEPARATOR ';') class_name from teacher t "
  884. . "left join teacher_class_relation tcr on t.teacher_id = tcr.teacher_id "
  885. . "left join class c on c.class_id=tcr.class_id {$condition} "
  886. . "group by t.teacher_id";
  887. $teacher_data = $this->sConn->createCommand($sql)->queryAll();
  888. if($teacher_data){
  889. $teacher_phone = array();
  890. $subjects_name = array();
  891. $teacher_ids = _array_column($teacher_data, 'teacher_id');
  892. $subject_ids = _array_column($teacher_data, 'subjects');
  893. $sql = "select subject_id,subject_name from subject where subject_id in (".implode(',',$subject_ids).")";
  894. $subjectInfo = $this->conn->createCommand($sql)->queryAll();
  895. if($subjectInfo){
  896. foreach($subjectInfo as $k=>$v){
  897. $subjects_name[$v['subject_id']] = $v['subject_name'];
  898. }
  899. }
  900. $sql = "select teacher_id,phone_number from xb_teacher where teacher_id in (".implode(',',$teacher_ids).")";
  901. $teacherInfo = $this->conn->createCommand($sql)->queryAll();
  902. if($teacherInfo){
  903. foreach($teacherInfo as $val){
  904. $teacher_phone[$val['teacher_id']] = $val['phone_number'];
  905. }
  906. }
  907. foreach($teacher_data as $k=>$v){
  908. //查询班级
  909. $classNames=array();
  910. $teacherClass=$this->sConn->createCommand("select class_name from teacher_class_relation tcr left join class c on c.class_id=tcr.class_id where tcr.teacher_id='{$v['teacher_id']}' and c.semester_id='{$this->semesterId}'")->queryAll();
  911. if($teacherClass){
  912. foreach ($teacherClass as $c){
  913. $classNames[]=$c['class_name'];
  914. }
  915. $teacher_data[$k]['class_name']=implode(',',$classNames);
  916. }else{
  917. $teacher_data[$k]['class_name']='';
  918. }
  919. $teacher_data[$k]['phone_number'] = '';
  920. if(isset($teacher_phone[$v['teacher_id']])){
  921. $teacher_data[$k]['phone_number'] = $teacher_phone[$v['teacher_id']];
  922. }
  923. $teacher_data[$k]['subject_name'] = '';
  924. if(isset($subjects_name[$v['subjects']])){
  925. $teacher_data[$k]['subject_name'] = $subjects_name[$v['subjects']];
  926. }
  927. $teacher_data[$k]['posts_name'] = '';
  928. if($v['posts'] == 1){
  929. $teacher_data[$k]['posts_name'] = '班主任';
  930. }else if($v['posts'] == 2){
  931. $teacher_data[$k]['posts_name'] = '教研组长';
  932. }else if($v['posts'] == 3){
  933. $teacher_data[$k]['posts_name'] = '年级主任';
  934. }else if($v['posts'] == 4){
  935. $teacher_data[$k]['posts_name'] = '任课教师';
  936. }else if($v['posts'] == 5){
  937. $teacher_data[$k]['posts_name'] = '校长';
  938. }
  939. $teacher_data[$k]['title_name'] = '';
  940. if($v['title'] == 1){
  941. $teacher_data[$k]['title_name'] = '一级教师';
  942. }else if($v['title'] == 2){
  943. $teacher_data[$k]['title_name'] = '二级教师';
  944. }else if($v['title'] == 3){
  945. $teacher_data[$k]['title_name'] = '三级教师';
  946. }else if($v['title'] == 4){
  947. $teacher_data[$k]['title_name'] = '高级教师';
  948. }
  949. $teacher_data[$k]['teacher_sex_name'] = '';
  950. if($v['teacher_sex'] == 1){
  951. $teacher_data[$k]['teacher_sex_name'] = '女';
  952. }else{
  953. $teacher_data[$k]['teacher_sex_name'] = '男';
  954. }
  955. if($v['posts'] == 3){
  956. if(!$v['grade']){
  957. $teacher_data[$k]['grade_name'] = '年级未识别,请手动调整';
  958. }else{
  959. $teacher_data[$k]['grade_name'] = $v['grade'];
  960. }
  961. }else{
  962. $teacher_data[$k]['grade_name'] = '';
  963. }
  964. }
  965. // debug($teacherInfo);
  966. }
  967. if($teacher_data){
  968. $objPHPExcel = new PHPExcel();
  969. $objPHPExcel->getProperties()->setCreator("ctos")
  970. ->setLastModifiedBy("ctos")
  971. ->setTitle("教师信息")
  972. ->setSubject("教师信息")
  973. ->setDescription("导出教师信息")
  974. ->setKeywords("excel")
  975. ->setCategory("result file");
  976. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
  977. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
  978. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  979. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
  980. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  981. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(40);
  982. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
  983. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
  984. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
  985. $objPHPExcel->setActiveSheetIndex(0)
  986. ->setCellValue('A1', '*教师姓名')
  987. ->setCellValue('B1', '*登录手机号')
  988. ->setCellValue('C1', '*学科')
  989. ->setCellValue('D1', '*岗位')
  990. ->setCellValue('E1', '职称')
  991. ->setCellValue('F1', '执教班级')
  992. ->setCellValue('G1', '教龄')
  993. ->setCellValue('H1', '性别')
  994. ->setCellValue('I1', '年级');
  995. $i = 2;
  996. foreach($teacher_data as $key=>$val){
  997. $objPHPExcel->getActiveSheet(0)->setCellValue('A' . $i, $val['teacher_name']);
  998. $objPHPExcel->getActiveSheet(0)->setCellValue('B' . $i, $val['phone_number']);
  999. $objPHPExcel->getActiveSheet(0)->setCellValue('C' . $i, $val['subject_name']);
  1000. $objPHPExcel->getActiveSheet(0)->setCellValue('D' . $i, $val['posts_name']);
  1001. $objPHPExcel->getActiveSheet(0)->setCellValue('E' . $i, $val['title_name']);
  1002. $objPHPExcel->getActiveSheet(0)->setCellValue('F' . $i, $val['class_name']);
  1003. $objPHPExcel->getActiveSheet(0)->setCellValue('G' . $i, $val['teacher_age']);
  1004. $objPHPExcel->getActiveSheet(0)->setCellValue('H' . $i, $val['teacher_sex_name']);
  1005. $objPHPExcel->getActiveSheet(0)->setCellValue('I' . $i, $val['grade_name']);
  1006. $i++;
  1007. }
  1008. $objPHPExcel->setActiveSheetIndex(0);
  1009. ob_end_clean(); // Added by me
  1010. header("Accept-Ranges:bytes");
  1011. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  1012. header('Content-Disposition:attachment;filename="'.$excel_title.'.xls"');
  1013. header('Cache-Control:max-age=0');
  1014. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1015. $objWriter->save('php://output');
  1016. exit;
  1017. }else{
  1018. Yii::app()->jump->error('导出失败,无教师数据',Yii::app()->createUrl('teacher/index'));
  1019. }
  1020. debug($teacher_data);
  1021. }
  1022. }