session['coachInfo']['leader'] !=1 ) { Yii::app()->jump->error('您无权限操作!'); } */ } // 学期列表 public function actionIndex(){ $word = Req::get("word"); $currSemester = $this->schoolManager->getCurrSemester(); $condition = array(); if($word){ if(is_numeric($word)){ $condition = array("school_year = '{$word}'"); } else{ $condition = array("semester_name like '%{$word}%'"); } } //$semesList = Semester::model()->getSemesList($condition); $semestersList = $this->schoolManager->getSemestersList($condition); // 是否有正在使用学期 $flag = 1; if (Yii::app()->session['coachInfo']['leader'] !=1 ) { foreach ($semestersList["rs"] as $list) { if ($list['status'] == 1) { $flag = 2; break; } } } else { $flag = 2; } $data = array(); $data['list'] = Arr::toObj($semestersList["rs"]); $data['pages'] = $semestersList["pager"]; $data['page_total'] = $semestersList["pager"]->rowsCount; $data["word"] = $word; $data["currSemester"] = $currSemester; $data["flag"] = $flag; unset($semesList); $this->render('index',$data); } // 添加学期 public function actionAdd(){ if (Yii::app()->request->getIsPostRequest()){ //$name = safe_replace($_POST['name']); //$period = safe_replace($_POST['period']); $start_time = safe_replace($_POST['start_time']); $end_time = safe_replace($_POST['end_time']); $status = safe_replace($_POST['status']); $school_year= safe_replace($_POST['school_year']); $semesterCode = Req::post("semester"); $name = $this->schoolManager->getSemesterNameByCode($semesterCode); if (empty($name) || empty($school_year) || empty($start_time) || empty($end_time)){ Yii::app()->jump->error('请确认学期信息是否填写完整!'); } if(!is_numeric($school_year) || strlen($school_year) > 4) Yii::app()->jump->error('学年不能超过4位数字!'); if (strtotime($start_time) >= strtotime($end_time)) { Yii::app()->jump->error('学期开始时间不能大于或等于结束时间!'); } $isExistence=$this->schoolManager->getOneSemester(); $isNowSemester=1; if($isExistence){ $isNowSemester=0; //设置为非当前学期 $lastSemester = $this->schoolManager->getTheYearLastSemester($school_year); if($lastSemester && strtotime($start_time) <= $lastSemester["start_time"]) Yii::app()->jump->error('学期开始时间不能小于之前的学期!'); if (Semester::model()->count('semester_name="'.$name.'"')){ Yii::app()->jump->error('该学期名称已存在!'); } //创建表 $inciseTables = $this->inciseTables; foreach ($inciseTables as $inciseTable) { $newTableName = $inciseTable . '_' . $semesterCode; //新学期表名 $existTable = $this->sConn->createCommand("SHOW TABLES LIKE '$newTableName';")->queryRow(); //查询新学期表是否存在 if (!$existTable) { $this->sConn->createCommand('CREATE TABLE '.$newTableName.' LIKE '.$inciseTable)->execute(); } } // 若新添加学期设置为可用则更新其他学期不可用 if ($status == 1 && Semester::model()->count('status=1')) { Semester::model()->updateAll(array('status'=>'0')); Yii::app()->session['session_semester_name'] = $name; $isNowSemester=1; } } $semesterModel = new Semester(); //$semesterModel->semester_id = getUUID(); $semesterModel->semester_id =getUniqueId($this->schoolId); $semesterModel->semester_name= $name; $semesterModel->school_year = $school_year; $semesterModel->refer_code = $semesterCode; //$semesterModel->period = $period; $semesterModel->start_time = strtotime($start_time); $semesterModel->end_time = strtotime($end_time); $semesterModel->create_time = time(); $semesterModel->status = $isNowSemester; if ($semesterModel->save()) { if ($status == 1) { Yii::app()->session['session_semester_id'] = $semesterModel->attributes['semester_id']; } $this->redirect(array('semester/index')); } else { Yii::app()->jump->error('学期创建失败!'); } } $data = array(); $this->render('add',$data); } // 学期编辑 public function actionEdit(){ $smtid = safe_replace(Yii::app()->request->getQuery('smtid')); if (empty($smtid)) { Yii::app()->jump->error('您访问的页面不存在!'); } // 当前学期信息 $info = Semester::model()->find('semester_id=:smtid',array(':smtid'=>$smtid)); if (empty($info)) { Yii::app()->jump->error('无法编辑该学期信息!'); } if (Yii::app()->request->getIsPostRequest()){ $school_year = safe_replace($_POST['school_year']); $start_time = safe_replace($_POST['start_time']); $end_time = safe_replace($_POST['end_time']); $status = safe_replace($_POST['status']); $semesterCode = safe_replace($_POST['semester']); $semester_name = $this->schoolManager->getSemesterNameByCode($semesterCode); if ( empty($start_time) || empty($end_time) || empty($semester_name)){ Yii::app()->jump->error('请确认学期信息是否填写完整!'); } if (strtotime($start_time) >= strtotime($end_time)) { Yii::app()->jump->error('学期开始时间不能大于结束时间!'); } $model = new Model(); if($model->isCurrentSemester($smtid) && $status == 0){ Yii::app()->jump->error('不能将当前学期设为未使用!'); } $checkRepeat=Semester::model()->count('semester_id != :seid and refer_code=:code',array(':seid'=>$info->semester_id,':code'=>$semesterCode)); if($checkRepeat){ Yii::app()->jump->error('已有相同学期!'); } /* $semester_one = $this->schoolManager->getThisYearSemesterName($semester_name); if(!$semester_one) { Yii::app()->jump->error('填写的学期格式不正确!'); }*/ // 若更新学期为可用则更新其他学期不可用 if ($status == 1 && Semester::model()->count('semester_id != :seid and status=1',array(':seid'=>$info->semester_id))) { if($return=$this->ExchangeTable(Yii::app()->session['session_semester_id'],$smtid)) { Semester::model()->updateAll(array('status'=>'0')); $expireTime = isset(Yii::app()->params["redisCloud"]["expire_time"]) ? Yii::app()->params["redisCloud"]["expire_time"] : 86400; $server = Yii::app()->params["redisCloud"]["servers"]; $redis = new Redis(); $redis->connect($server["host"], $server["port"]); if(isset($server['password'])){ $redis->auth($server['password']); } $redis->select($server['database']); $redis->set('zsy_student:school_semester_id:'.$this->schoolId,$smtid); $redis->expire('zsy_student:school_semester_id:'.$this->schoolId,$expireTime); }else{ Yii::app()->jump->error('学期修改失败!数据表重复,请联系技术支持处理'); } } $info->semester_name = $semester_name; $info->school_year = $school_year; $info->refer_code = $semesterCode; $info->start_time = strtotime($start_time); $info->end_time = strtotime($end_time); $info->status = $status; if ($info->save()) { if(Yii::app()->params['handle_log_on_off']) { writeFileLog(jsonEncode(array( "exam_group_id" => 0, "operate_project" => 'zsyas2', "school_id" => $this->schoolId, "title" => '学期修改', "operate_account" => Yii::app()->session['coachInfo']['coach_name'], "operate_method" => $this->action, "operate_url" => $this->getRoute(), "operate_sql" =>'', "operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET)), "date"=>date('Y-m-d H:i:s') ))); } if ($status == 1) { Yii::app()->session['session_semester_id'] = $smtid; } $this->redirect(array('semester/index')); } else { Yii::app()->jump->error('学期修改失败!'); } } $c_semester_data = $this->schoolManager->getThisYearSemesterDictionary($info->school_year); $data = array(); $data['info'] = $info; $data['c_info'] = $c_semester_data; if($info->status==1){ $this->render('edit',$data); }else{ $this->render('edit2',$data); } } public function actionDelSemesterAct(){ $semesterId = Req::post("semesterId"); $rs = $this->schoolManager->delSemester($semesterId); echo json_encode($rs);exit; } public function actionChange(){ $word = Req::get("word"); $currSemester = $this->schoolManager->getCurrSemester(); $condition = ""; if($word){ if(is_numeric($word)){ $condition = "school_year = '{$word}'"; } else{ $condition = "semester_name like '%{$word}%'"; } } $_class_data = ClassModel::model()->findAll('semester_id=:seid and is_hide=0 and grade<>3 and grade<>9',array(':seid'=>$this->semesterId)); $classes=array(); foreach ($_class_data as $datum){ $classes[$datum['class_type']][]=array( 'class_id'=>$datum['class_id'], 'class_name'=>$datum['class_name'], 'grade'=>$datum['grade'], ); } $semesList = Semester::model()->getSemesList($condition); $data = array(); $data['list'] = $semesList['result']; $data['pages'] = $semesList['page']; $data['page_total'] = $semesList['page_total']; $data["word"] = $word; $data["currSemester"] = $currSemester; $data['classes']=$classes; unset($semesList); $this->render('change',$data); } public function actionChangeSemesterAct(){ $semesterId = Req::post("semesterId"); //学期id $RetainClass=Req::post('RetainClass'); //保留班级 $Graduation=Req::post('Graduation'); //毕业班 $classUp=Req::post('classUp'); //升班 $classNameArr=Req::post('classNameArr'); //升班后班级名称 $gradeDirector=Req::post('gradeDirector'); //年级主任升班与否 1同步升班,2不同步 $classNewNameArr=array(); if($classUp==1){ foreach ($classNameArr as $item){ $arrTemp=explode('///',$item); $classNewNameArr[(string)$arrTemp[0]]=$arrTemp[1]; } } $oldSemesterId=Yii::app()->session['session_semester_id']; //原学期 $semester = $this->schoolManager->getSemester($semesterId); //切割表 // if($return=$this->ExchangeTable($oldSemesterId,$semesterId)){ $rs = $this->schoolManager->changeSemesterByStudentAndClass($semesterId,$oldSemesterId,$RetainClass,$Graduation,$classNewNameArr,$gradeDirector); if($rs["code"] == 0){ Yii::app()->session['session_semester_id'] = $semester["semester_id"]; Yii::app()->session['session_semester_name'] = $semester["semester_name"]; if ($semester["status"] == 1){ unset(Yii::app()->session['session_semester_state']); } $expireTime = isset(Yii::app()->params["redisCloud"]["expire_time"]) ? Yii::app()->params["redisCloud"]["expire_time"] : 86400; $server = Yii::app()->params["redisCloud"]["servers"]; $redis = new Redis(); $redis->connect($server["host"], $server["port"]); if(isset($server['password'])){ $redis->auth($server['password']); } $redis->select($server['database']); $redis->set('zsy_student:school_semester_id:'.$this->schoolId,$semesterId); $redis->expire('zsy_student:school_semester_id:'.$this->schoolId,$expireTime); } // }else{ // $rs["code"] =3001; // echo json_encode($rs);exit; // } if(Yii::app()->params['handle_log_on_off']) { writeFileLog(jsonEncode(array( "exam_group_id" => 0, "operate_project" => 'zsyas2', "school_id" => $this->schoolId, "title" => '切换学期', "operate_account" => Yii::app()->session['coachInfo']['coach_name'], "operate_method" => $this->action, "operate_url" => $this->getRoute(), "operate_sql" =>'', "operate_param" =>json_encode(array('post'=>$_POST,'get'=>$_GET)), "date"=>date('Y-m-d H:i:s') ))); } echo json_encode($rs);exit; } public function actionGetThisYearSemesterDictionary(){ $year = Req::get("year"); $rs = $this->schoolManager->getThisYearSemesterDictionary($year); echo json_encode($rs);exit; } public function actionGetThisYearSemesters(){ $year = Req::get("year"); $semesters = $this->schoolManager->getSemesters(array("school_year = '{$year}'"), array("semester_id desc"), 0, 20); echo json_encode($semesters);exit; } //切换学期交换表名 /* * $oldSemester 旧学期(当前学期) * $newSemester 新学期(切换到的学期) * */ private function ExchangeTable($oldSemester,$newSemester){ $inciseTables = $this->inciseTables; $oldSemester = $this->sConn->createCommand("SELECT semester_id,refer_code FROM `semester` WHERE `semester_id` = '".$oldSemester."'")->queryRow(); $newSemester = $this->sConn->createCommand("SELECT semester_id,refer_code FROM `semester` WHERE `semester_id` = '".$newSemester."'")->queryRow(); $trans = $this->sConn->beginTransaction(); try{ foreach ($inciseTables as $inciseTable) { $newTableName = $inciseTable . '_' . $newSemester['refer_code']; //新学期表名 $oldTableName= $inciseTable . '_' . $oldSemester['refer_code']; //旧学期(当前学期)需要改的名字 $existTable = $this->sConn->createCommand("SHOW TABLES LIKE '$newTableName';")->queryRow(); //查询新学期表是否存在 if($existTable){ //修改当前的表名 $existOldTable=$this->sConn->createCommand("SHOW TABLES LIKE '$oldTableName';")->queryRow(); //查询当前学期表是否存在 if($existOldTable){ //throw new Exception('3001'); //重复表进行重命名 $bakTableName=$oldTableName.'_'.date('YmdHis'); $this->sConn->createCommand("ALTER TABLE ".$oldTableName." RENAME TO ".$bakTableName)->execute(); } $this->sConn->createCommand("ALTER TABLE ".$inciseTable." RENAME TO ".$oldTableName)->execute(); //修改新学期表名 $this->sConn->createCommand("ALTER TABLE ".$newTableName." RENAME TO ".$inciseTable)->execute(); }else{ //修改当前的表名 $existOldTable=$this->sConn->createCommand("SHOW TABLES LIKE '$oldTableName';")->queryRow(); //查询当前学期表是否存在 if($existOldTable){ //throw new Exception('3001'); //重复表进行重命名 $bakTableName=$oldTableName.'_'.date('YmdHis'); $this->sConn->createCommand("ALTER TABLE ".$oldTableName." RENAME TO ".$bakTableName)->execute(); } $this->sConn->createCommand("ALTER TABLE ".$inciseTable." RENAME TO ".$oldTableName)->execute(); //创建新学期表 $this->sConn->createCommand('CREATE TABLE '.$inciseTable.' LIKE '.$oldTableName)->execute(); } } $trans->commit(); return true; }catch (Exception $e){ $trans->rollback(); return false; } return false; } //分表操作 private function InciseTable($semesterId){ $inciseTables = $this->inciseTables; $semester = $this->sConn->createCommand("SELECT semester_id,refer_code FROM `semester` WHERE `semester_id` = '".$semesterId."'")->queryRow(); if ($semester) { $isExistTable=false; foreach ($inciseTables as $inciseTable) { $isExistTable=false; $newTableName = $inciseTable . '_' . $semester['refer_code']; $existTable = $this->sConn->createCommand("SHOW TABLES LIKE '$newTableName';")->queryRow(); if ($existTable){ $checkData=$this->sConn->createCommand("select * from ".$newTableName)->queryRow(); if($checkData){ return true; }else{ $isExistTable=true; } } if($isExistTable){ switch ($inciseTable){ case 'student_paper_topic_rs': $rrs=$this->disposeStudentPaperTopicRs($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_paper_relation': $rrs=$this->disposeStudentPaperRelation($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_improve_score_plan_topics': $rrs=$this->disposeStudentImproveScorePlanTopics($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_wrong_plan_topics': $rrs=$this->disposeStudentWrongPlanTopics($semester['semester_id'], $newTableName); if(!$rrs) return false; break; } }else{ // 创建分表 if ($this->sConn->createCommand('CREATE TABLE '.$newTableName.' LIKE '.$inciseTable)->execute()) { switch ($inciseTable){ case 'student_paper_topic_rs': $rrs=$this->disposeStudentPaperTopicRs($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_paper_relation': $rrs=$this->disposeStudentPaperRelation($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_improve_score_plan_topics': $rrs=$this->disposeStudentImproveScorePlanTopics($semester['semester_id'], $newTableName); if(!$rrs) return false; break; case 'student_wrong_plan_topics': $rrs=$this->disposeStudentWrongPlanTopics($semester['semester_id'], $newTableName); if(!$rrs) return false; break; } } } } } return true; } //分割rs表 private function disposeStudentPaperTopicRs($semesterId, $newTableName) { if (empty($semesterId) || empty($newTableName) ) return true; // 学期总数 $countSql = 'SELECT count(sptr.id) as num FROM student_paper_topic_rs sptr JOIN paper p ON sptr.paper_id = p.paper_id JOIN exam e ON e.exam_id = p.exam_id WHERE e.semester_id = "'.$semesterId.'"'; $countInfo = $this->sConn->createCommand($countSql)->queryRow(); $totalNum = $countInfo['num']; if (!$totalNum) { return true; } $num = 1; $pageSize = 10000; $pageNum = ceil($totalNum / $pageSize); $offsetSql = 'SELECT sptr.id FROM student_paper_topic_rs sptr JOIN paper p ON sptr.paper_id = p.paper_id JOIN exam e ON e.exam_id = p.exam_id WHERE e.semester_id = "'.$semesterId.'" order by sptr.id asc limit 1'; $res = $this->sConn->createCommand($offsetSql)->queryRow(); if (!isset($res['id'])) return true; // 当前学期没有数据 $offset = $res['id']; // 获取上一条 $preSql = 'SELECT id from student_paper_topic_rs where id < '.$offset.' order by id DESC limit 1'; $preRes = $this->sConn->createCommand($preSql)->queryRow(); $offset = isset($preRes['id']) ? $preRes['id'] : $offset; while ($num <= $pageNum) { // 获取当前起点后的还需处理的数据总数 if (($num * $pageSize) > $totalNum) { $tmepCountSql = 'SELECT count(sptr.id) as num FROM student_paper_topic_rs sptr JOIN paper p ON sptr.paper_id = p.paper_id JOIN exam e ON e.exam_id = p.exam_id WHERE e.semester_id = "'.$semesterId.'" and sptr.id > "'.$offset.'"'; $tempCountRes = $this->sConn->createCommand($tmepCountSql)->queryRow(); if (empty($tempCountRes['num'])) break; $pageSize = $tempCountRes['num']; } $lastSql = 'SELECT sptr.id FROM student_paper_topic_rs sptr JOIN paper p ON sptr.paper_id = p.paper_id JOIN exam e ON e.exam_id = p.exam_id WHERE e.semester_id = "'.$semesterId.'" and sptr.id > "'.$offset.'" order by sptr.id asc limit '.($pageSize-1).', 1'; $offsetResult = $this->sConn->createCommand($lastSql)->queryRow(); if (isset($offsetResult['id'])) { $limitId = $offsetResult['id']; $sql = 'INSERT INTO '.$newTableName.' SELECT sptr.* FROM student_paper_topic_rs sptr JOIN paper p ON sptr.paper_id = p.paper_id JOIN exam e ON e.exam_id = p.exam_id WHERE e.semester_id = "'.$semesterId.'" and sptr.id > "'.$offset.'" AND sptr.id <= "'.$limitId.'"' ; if (!$this->sConn->createCommand($sql)->execute()) { return false; } $offset = $limitId; } ++$num; } unset($res, $preRes, $countInfo); return true; } //切割relation表 private function disposeStudentPaperRelation($semesterId, $newTableName) { if (empty($semesterId) || empty($newTableName) ) return true; $paperIds = array(); $paperSql = "SELECT paper_id from student_paper_relation WHERE semester_id = '{$semesterId}' GROUP BY paper_id"; $paperInfo = $this->sConn->createCommand($paperSql)->queryAll(); if ($paperInfo) { foreach ($paperInfo as $key=>$info) { $paperIds[] = $info['paper_id']; } $groupPaper = array_chunk($paperIds, 50); foreach ($groupPaper as $key=>$item) { $sql = "INSERT INTO {$newTableName} SELECT * FROM `student_paper_relation` WHERE semester_id = '{$semesterId}' and paper_id in (".implode(',', $item).")"; $this->sConn->createCommand($sql)->execute(); } } unset($paperInfo,$sql,$paperIds,$groupPaper); return true; } private function disposeStudentImproveScorePlanTopics($semesterId, $newTableName) { if (empty($semesterId) || empty($newTableName)) return true; $examSql = "SELECT exam_id FROM `exam` WHERE `semester_id` = '{$semesterId}'"; $examInfo = $this->sConn->createCommand($examSql)->queryAll(); if (empty($examInfo)) { return true; } $examIds = array(); foreach ($examInfo as $exam) { $examIds[] = $exam['exam_id']; } $groupExam = array_chunk($examIds, 30); foreach ($groupExam as $key => $loop) { $sql = 'INSERT INTO '.$newTableName.' SELECT * FROM student_improve_score_plan_topics WHERE `plan_id` in ('.implode(',',$loop).')'; $this->sConn->createCommand($sql)->execute(); } unset($examInfo,$examIds,$sql); return true; } private function disposeStudentWrongPlanTopics($semesterId, $newTableName) { if (empty($semesterId) || empty($newTableName)) return true; $examSql = "SELECT exam_id FROM `exam` WHERE `semester_id` = '{$semesterId}'"; $examInfo = $this->sConn->createCommand($examSql)->queryAll(); if (empty($examInfo)) { return true; } $examIds = array(); foreach ($examInfo as $exam) { $examIds[] = $exam['exam_id']; } $groupExam = array_chunk($examIds, 30); foreach ($groupExam as $key => $loop) { $sql = 'INSERT INTO '.$newTableName.' SELECT * FROM student_wrong_plan_topics WHERE `wrong_plan_id` in ('.implode(',',$loop).')'; $this->sConn->createCommand($sql)->execute(); } unset($examInfo,$examIds,$sql); return true; } }