TeachingTemplate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class TeachingTemplate extends Model{
  3. public function tableName(){
  4. return 'teaching_template';
  5. }
  6. //获取模板列表
  7. public function getList($subjectId){
  8. $result = array();
  9. $sql = "select template_id,template_name,is_default from ".$this->tableName()." where subject_id = '{$subjectId}' order by is_default desc,create_time asc";
  10. $result = $this->sConn->createCommand($sql)->queryAll();
  11. return $result;
  12. }
  13. //模板复制
  14. public function copyTemplate($templateId,$subjectId){
  15. $result = '';
  16. $time = time();
  17. $sql = "select count(template_id) count from ".$this->tableName()." where subject_id ='{$subjectId}'";
  18. $count = $this->sConn->createCommand($sql)->queryRow();
  19. if($count && $count['count'] > 11){
  20. return "模板超过上限";
  21. }
  22. $sql = "select config_text from ".$this->tableName()." where template_id = ".$templateId;
  23. $data = $this->sConn->createCommand($sql)->queryRow();
  24. if($data){
  25. $json = $data['config_text'];
  26. $sql = "select count(template_id) count from ".$this->tableName()." where subject_id = ".$subjectId;
  27. $data_count = $this->sConn->createCommand($sql)->queryRow();
  28. if($data_count){
  29. $count = $data_count['count'];
  30. $templateName = "班级默认模板(".$count.")";
  31. $sql = "insert into ".$this->tableName()." (template_name,config_text,subject_id,is_default,create_time) "
  32. . "values ('".$templateName."','".$json."',".$subjectId.",0,".$time.")";
  33. $data = $this->sConn->createCommand($sql)->execute();
  34. if(!$data){
  35. $result = '操作失败';
  36. }
  37. }
  38. }
  39. return $result;
  40. }
  41. //模板改名
  42. public function changeName($templateId,$templateName,$subjectId){
  43. $status = 1;
  44. $error = '';
  45. $result = array(
  46. 'status'=>$status,
  47. 'error'=>$error
  48. );
  49. $sql = "select template_id from ".$this->tableName()." where subject_id = '{$subjectId}' and template_name = '{$templateName}'";
  50. $data = $this->sConn->createCommand($sql)->queryRow();
  51. if($data){
  52. return array("status"=>0,'error'=>'模板名称不能重复');
  53. }
  54. $sql = "update ".$this->tableName()." set template_name='{$templateName}' where template_id='{$templateId}'";
  55. $data = $this->sConn->createCommand($sql)->execute();
  56. if(!$data){
  57. return array("status"=>0,'error'=>'更新失败');
  58. }else{
  59. return array("status"=>1,'error'=>'');
  60. }
  61. }
  62. //模板删除
  63. public function delTemplate($templateId,$subjectId){
  64. $status = 1;
  65. $error = '';
  66. $default_template_id = 0;
  67. $sql = "select template_id from ".$this->tableName()." where is_default=1 and subject_id = '{$subjectId}'";
  68. $data = $this->sConn->createCommand($sql)->queryRow();
  69. if($data){
  70. $default_template_id = $data['template_id'];
  71. if($default_template_id == $templateId){
  72. return array("status"=>0,'error'=>'默认模板不能删除');
  73. }
  74. }
  75. $trans = $this->sConn->beginTransaction();
  76. try{
  77. $sql = "update teaching_class_set set template_id = ".$default_template_id." where template_id = ".$templateId;
  78. $this->sConn->createCommand($sql)->execute();
  79. $sql = "delete from ".$this->tableName()." where template_id = ".$templateId;
  80. $this->sConn->createCommand($sql)->execute();
  81. $trans->commit();
  82. } catch (Exception $e) {
  83. $trans->rollBack();
  84. $status = 0;
  85. $error = '删除失败';
  86. }
  87. return array("status"=>$status,"error"=>$error);
  88. }
  89. //模板查看
  90. public function detailTemplate($templateId){
  91. $result = '';
  92. $sql = "select template_id,config_text from ".$this->tableName()." where template_id =".$templateId;
  93. $data = $this->sConn->createCommand($sql)->queryRow();
  94. if($data){
  95. $result = $data['config_text'];
  96. }
  97. return $result;
  98. }
  99. //模板保存
  100. public function saveTemplate($templateId,$json){
  101. $status = true;
  102. $time = time();
  103. $sql = "update ".$this->tableName()." set config_text= '".$json."',update_time={$time} where template_id = ".$templateId;
  104. $data = $this->sConn->createCommand($sql)->execute();
  105. if(!$data){
  106. $status = false;
  107. }
  108. return $status;
  109. }
  110. //班级恢复默认
  111. public function restoreClassDefault($classId,$subjectId){
  112. $default_template_id = 0;
  113. $result = true;
  114. $sql = "select template_id from ".$this->tableName()." where subject_id = {$subjectId} and is_default=1";
  115. $data = $this->sConn->createCommand($sql)->queryRow();
  116. if($data){
  117. $default_template_id = $data['template_id'];
  118. }
  119. if($default_template_id){
  120. $sql = "select tt.template_id from teaching_template tt "
  121. . "join teaching_class_set tcs on tt.template_id=tcs.template_id "
  122. . "where tt.subject_id = '{$subjectId}' and tcs.class_id='{$classId}'";
  123. $template_id_data = $this->sConn->createCommand($sql)->queryRow();
  124. if($template_id_data){
  125. $template_id = $template_id_data['template_id'];
  126. if($template_id != $default_template_id){
  127. $sql = "update teaching_class_set set template_id = '{$default_template_id}' where template_id ='{$template_id}' and class_id = '{$classId}'";
  128. $status = $this->sConn->createCommand($sql)->execute();
  129. if(!$status){
  130. $result = false;
  131. }
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. //班级编辑模板
  138. public function editTemplate($classId,$subjectId,$templateId){
  139. $result = true;
  140. $trans = $this->sConn->beginTransaction();
  141. try{
  142. $sql = "select tcs.class_id,tcs.template_id from teaching_class_set tcs "
  143. . "join teaching_template tt on tcs.template_id=tt.template_id "
  144. . "where tcs.class_id in (".implode(",",$classId).") and tt.subject_id='{$subjectId}'";
  145. $data = $this->sConn->createCommand($sql)->queryAll();
  146. if($data){
  147. foreach($data as $k=>$v){
  148. $sql = "delete from teaching_class_set where template_id = '{$v['template_id']}' and class_id = '{$v['class_id']}'";
  149. $this->sConn->createCommand($sql)->execute();
  150. }
  151. }
  152. $insert = '';
  153. foreach($classId as $v){
  154. $insert .= "('".$v."','".$templateId."'),";
  155. }
  156. $insert_sql = "insert into teaching_class_set (class_id,template_id) values ".substr($insert, 0, -1);
  157. $this->sConn->createCommand($insert_sql)->execute();
  158. $trans->commit();
  159. } catch (Exception $e) {
  160. $trans->rollBack();
  161. $result = false;
  162. }
  163. return $result;
  164. }
  165. //产品班级列表
  166. public function listTemplate($subjectId,$teacherId,$grade,$classId,$limit=15,$page){
  167. $result = array();
  168. $where = array();
  169. $begin_limit = ($page - 1)*$limit;
  170. $end_limit = $page*$limit - 1;
  171. if($teacherId){
  172. $where[] = "tcr.teacher_id='{$teacherId}'";
  173. }
  174. if($grade){
  175. $where[] = "c.grade='{$grade}'";
  176. }
  177. if($classId){
  178. $where[] = "c.class_id='{$classId}'";
  179. }
  180. if($subjectId){
  181. $where[] = "tt.subject_id='{$subjectId}'";
  182. $where[] = "t.subjects='{$subjectId}'";
  183. }
  184. if($where){
  185. $where_sql = ' where '.implode(' and ',$where);
  186. }else{
  187. $where_sql = "";
  188. }
  189. $sql = "select count(*) count from "
  190. . "(select tcs.class_id from teaching_class_set tcs "
  191. . "join teaching_template tt on tcs.template_id = tt.template_id "
  192. . "join class c on c.class_id = tcs.class_id "
  193. . "join teacher_class_relation tcr on tcs.class_id = tcr.class_id "
  194. . "join teacher t on t.teacher_id=tcr.teacher_id ".$where_sql." "
  195. . "GROUP BY tcs.class_id) a";
  196. $data = $this->sConn->createCommand($sql)->queryRow();
  197. if($data){
  198. $count = $data['count'];
  199. $pages = ceil($count/$limit);
  200. $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 "
  201. . "join teaching_template tt on tcs.template_id = tt.template_id "
  202. . "join class c on c.class_id = tcs.class_id "
  203. . "join teacher_class_relation tcr on tcs.class_id = tcr.class_id "
  204. . "join teacher t on t.teacher_id=tcr.teacher_id ".$where_sql." "
  205. . "GROUP BY tcs.class_id "
  206. . "ORDER BY c.grade asc,tcs.class_id asc "
  207. . "limit ".$begin_limit.",".$limit;
  208. $data_list = $this->sConn->createCommand($sql)->queryAll();
  209. if($data_list){
  210. $result['count'] = $count;
  211. $result['list'] = $data_list;
  212. $result['page'] = $pages;
  213. $result['current_page'] = $page;
  214. }
  215. }
  216. return $result;
  217. }
  218. //教师列表
  219. public function teacherList($subjectId){
  220. $result = array();
  221. $sql = "select teacher_id,teacher_name from teacher where subjects = '{$subjectId}'";
  222. $result = $this->sConn->createCommand($sql)->queryAll();
  223. return $result;
  224. }
  225. //初始化模板
  226. public function initTemplate($subjectId,$json,$templateName){
  227. $result = true;
  228. $time = time();
  229. $sql = "select template_id from ".$this->tableName()." where subject_id = '{$subjectId}' and is_default = 1";
  230. $data = $this->sConn->createCommand($sql)->queryRow();
  231. if(!$data){
  232. $sql = "insert into ".$this->tableName()." (template_name,config_text,subject_id,is_default,create_time) "
  233. . "values ('".$templateName."','".$json."',".$subjectId.",1,".$time.")";
  234. $data = $this->sConn->createCommand($sql)->execute();
  235. if(!$data){
  236. $result = false;
  237. }
  238. }
  239. return $result;
  240. }
  241. }