tableName()." where subject_id = '{$subjectId}' order by is_default desc,create_time asc"; $result = $this->sConn->createCommand($sql)->queryAll(); return $result; } //模板复制 public function copyTemplate($templateId,$subjectId){ $result = ''; $time = time(); $sql = "select count(template_id) count from ".$this->tableName()." where subject_id ='{$subjectId}'"; $count = $this->sConn->createCommand($sql)->queryRow(); if($count && $count['count'] > 11){ return "模板超过上限"; } $sql = "select config_text from ".$this->tableName()." where template_id = ".$templateId; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ $json = $data['config_text']; $sql = "select count(template_id) count from ".$this->tableName()." where subject_id = ".$subjectId; $data_count = $this->sConn->createCommand($sql)->queryRow(); if($data_count){ $count = $data_count['count']; $templateName = "班级默认模板(".$count.")"; $sql = "insert into ".$this->tableName()." (template_name,config_text,subject_id,is_default,create_time) " . "values ('".$templateName."','".$json."',".$subjectId.",0,".$time.")"; $data = $this->sConn->createCommand($sql)->execute(); if(!$data){ $result = '操作失败'; } } } return $result; } //模板改名 public function changeName($templateId,$templateName,$subjectId){ $status = 1; $error = ''; $result = array( 'status'=>$status, 'error'=>$error ); $sql = "select template_id from ".$this->tableName()." where subject_id = '{$subjectId}' and template_name = '{$templateName}'"; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ return array("status"=>0,'error'=>'模板名称不能重复'); } $sql = "update ".$this->tableName()." set template_name='{$templateName}' where template_id='{$templateId}'"; $data = $this->sConn->createCommand($sql)->execute(); if(!$data){ return array("status"=>0,'error'=>'更新失败'); }else{ return array("status"=>1,'error'=>''); } } //模板删除 public function delTemplate($templateId,$subjectId){ $status = 1; $error = ''; $default_template_id = 0; $sql = "select template_id from ".$this->tableName()." where is_default=1 and subject_id = '{$subjectId}'"; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ $default_template_id = $data['template_id']; if($default_template_id == $templateId){ return array("status"=>0,'error'=>'默认模板不能删除'); } } $trans = $this->sConn->beginTransaction(); try{ $sql = "update teaching_class_set set template_id = ".$default_template_id." where template_id = ".$templateId; $this->sConn->createCommand($sql)->execute(); $sql = "delete from ".$this->tableName()." where template_id = ".$templateId; $this->sConn->createCommand($sql)->execute(); $trans->commit(); } catch (Exception $e) { $trans->rollBack(); $status = 0; $error = '删除失败'; } return array("status"=>$status,"error"=>$error); } //模板查看 public function detailTemplate($templateId){ $result = ''; $sql = "select template_id,config_text from ".$this->tableName()." where template_id =".$templateId; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ $result = $data['config_text']; } return $result; } //模板保存 public function saveTemplate($templateId,$json){ $status = true; $time = time(); $sql = "update ".$this->tableName()." set config_text= '".$json."',update_time={$time} where template_id = ".$templateId; $data = $this->sConn->createCommand($sql)->execute(); if(!$data){ $status = false; } return $status; } //班级恢复默认 public function restoreClassDefault($classId,$subjectId){ $default_template_id = 0; $result = true; $sql = "select template_id from ".$this->tableName()." where subject_id = {$subjectId} and is_default=1"; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ $default_template_id = $data['template_id']; } if($default_template_id){ $sql = "select tt.template_id from teaching_template tt " . "join teaching_class_set tcs on tt.template_id=tcs.template_id " . "where tt.subject_id = '{$subjectId}' and tcs.class_id='{$classId}'"; $template_id_data = $this->sConn->createCommand($sql)->queryRow(); if($template_id_data){ $template_id = $template_id_data['template_id']; if($template_id != $default_template_id){ $sql = "update teaching_class_set set template_id = '{$default_template_id}' where template_id ='{$template_id}' and class_id = '{$classId}'"; $status = $this->sConn->createCommand($sql)->execute(); if(!$status){ $result = false; } } } } return $result; } //班级编辑模板 public function editTemplate($classId,$subjectId,$templateId){ $result = true; $trans = $this->sConn->beginTransaction(); try{ $sql = "select tcs.class_id,tcs.template_id from teaching_class_set tcs " . "join teaching_template tt on tcs.template_id=tt.template_id " . "where tcs.class_id in (".implode(",",$classId).") and tt.subject_id='{$subjectId}'"; $data = $this->sConn->createCommand($sql)->queryAll(); if($data){ foreach($data as $k=>$v){ $sql = "delete from teaching_class_set where template_id = '{$v['template_id']}' and class_id = '{$v['class_id']}'"; $this->sConn->createCommand($sql)->execute(); } } $insert = ''; foreach($classId as $v){ $insert .= "('".$v."','".$templateId."'),"; } $insert_sql = "insert into teaching_class_set (class_id,template_id) values ".substr($insert, 0, -1); $this->sConn->createCommand($insert_sql)->execute(); $trans->commit(); } catch (Exception $e) { $trans->rollBack(); $result = false; } return $result; } //产品班级列表 public function listTemplate($subjectId,$teacherId,$grade,$classId,$limit=15,$page){ $result = array(); $where = array(); $begin_limit = ($page - 1)*$limit; $end_limit = $page*$limit - 1; if($teacherId){ $where[] = "tcr.teacher_id='{$teacherId}'"; } if($grade){ $where[] = "c.grade='{$grade}'"; } if($classId){ $where[] = "c.class_id='{$classId}'"; } if($subjectId){ $where[] = "tt.subject_id='{$subjectId}'"; $where[] = "t.subjects='{$subjectId}'"; } if($where){ $where_sql = ' where '.implode(' and ',$where); }else{ $where_sql = ""; } $sql = "select count(*) count from " . "(select tcs.class_id from teaching_class_set tcs " . "join teaching_template tt on tcs.template_id = tt.template_id " . "join class c on c.class_id = tcs.class_id " . "join teacher_class_relation tcr on tcs.class_id = tcr.class_id " . "join teacher t on t.teacher_id=tcr.teacher_id ".$where_sql." " . "GROUP BY tcs.class_id) a"; $data = $this->sConn->createCommand($sql)->queryRow(); if($data){ $count = $data['count']; $pages = ceil($count/$limit); $sql = "select c.class_id,c.class_name,c.grade,tt.template_id,tt.template_name,GROUP_CONCAT(t.teacher_name) teacher_name from teaching_class_set tcs " . "join teaching_template tt on tcs.template_id = tt.template_id " . "join class c on c.class_id = tcs.class_id " . "join teacher_class_relation tcr on tcs.class_id = tcr.class_id " . "join teacher t on t.teacher_id=tcr.teacher_id ".$where_sql." " . "GROUP BY tcs.class_id " . "ORDER BY c.grade asc,tcs.class_id asc " . "limit ".$begin_limit.",".$limit; $data_list = $this->sConn->createCommand($sql)->queryAll(); if($data_list){ $result['count'] = $count; $result['list'] = $data_list; $result['page'] = $pages; $result['current_page'] = $page; } } return $result; } //教师列表 public function teacherList($subjectId){ $result = array(); $sql = "select teacher_id,teacher_name from teacher where subjects = '{$subjectId}'"; $result = $this->sConn->createCommand($sql)->queryAll(); return $result; } //初始化模板 public function initTemplate($subjectId,$json,$templateName){ $result = true; $time = time(); $sql = "select template_id from ".$this->tableName()." where subject_id = '{$subjectId}' and is_default = 1"; $data = $this->sConn->createCommand($sql)->queryRow(); if(!$data){ $sql = "insert into ".$this->tableName()." (template_name,config_text,subject_id,is_default,create_time) " . "values ('".$templateName."','".$json."',".$subjectId.",1,".$time.")"; $data = $this->sConn->createCommand($sql)->execute(); if(!$data){ $result = false; } } return $result; } }