ProductController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * 产品下载控制器类
  4. * @author li
  5. * @date 2022-12-19 17:50:00
  6. * @company 上海教育有限公司.
  7. */
  8. class ProductController extends Controller {
  9. // e学慧通
  10. public function actionIndex() {
  11. $type=(int)Req::get('type')?(int)Req::get('type'):1;
  12. $subjectId=Yii::app()->session['session_duoxueke_subject_id'];
  13. $subjectId=in_array($subjectId,array(3,6,51))?3:$subjectId;
  14. //分层作业 个性化学习手册 分层测评卷 分层作业数学物理3级目录,其他全是2级目录
  15. $level=2;
  16. if($type==1 && in_array($subjectId,array(3,6,51,12))){
  17. $level=3;
  18. }
  19. //版本 模块 单元 章节
  20. $textbooks=$modules=$units=$chapters=array();
  21. $postRs = $this->apiPost('all_textbook/getTextbookBySubjectId', array( 'subjectId' => $subjectId,'type'=>1));
  22. $textbookResult=json_decode(json_encode($postRs),true);
  23. if(isset($textbookResult['status']) && $textbookResult['status'] && isset($textbookResult['data']) && $textbookResult['data']){
  24. $textbookResult=$textbookResult['data'];
  25. foreach($textbookResult as $textbookKey=>$textbookItem){
  26. $textbooks[]=array('textbook_id'=>$textbookItem['textbook_id'],'textbook_name'=>$textbookItem['textbook_name']);
  27. if($textbookKey==0 && isset($textbookItem['child']) && $textbookItem['child']){
  28. foreach($textbookItem['child'] as $moduleKey=>$moduleItem){
  29. $modules[]=array('textbook_id'=>$moduleItem['textbook_id'],'textbook_name'=>$moduleItem['textbook_name']);
  30. if($moduleKey==0 && isset($moduleItem['child']) && $moduleItem['child']){
  31. foreach($moduleItem['child'] as $unitKey=>$unitItem){
  32. $units[]=array('textbook_id'=>$unitItem['textbook_id'],'textbook_name'=>$unitItem['textbook_name']);
  33. if($unitKey==0 && isset($unitItem['child']) && $unitItem['child']){
  34. foreach($unitItem['child'] as $chapterItem){
  35. $chapters[]=array('textbook_id'=>$chapterItem['textbook_id'],'textbook_name'=>$chapterItem['textbook_name']);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. //分层测评卷-高三备考的类别和试卷
  45. $productDetailsResult=$this->apiPost('all_product/getProductDetails',array('subjectId'=>$subjectId,));
  46. $productDetailsResult=json_decode(json_encode($productDetailsResult),true);
  47. $productDetails=array();
  48. if(isset($productDetailsResult['status']) && $productDetailsResult['status'] && isset($productDetailsResult['data']) && $productDetailsResult['data']){
  49. $productDetails=$productDetailsResult['data'];
  50. }
  51. $data=array(
  52. 'year' => $year = date("Y"),
  53. 'month' => $month = (int)date("m"),
  54. 'week' => $week = date("W", time()) - date("W", strtotime(date("Y-m-01", time()))) + 1,
  55. 'textbooks'=>$textbooks,//版本
  56. 'modules'=>$modules,//模块
  57. 'units'=>$units,//单元
  58. 'chapters'=>$chapters,//章节
  59. 'level'=>$level,
  60. 'type'=>$type,
  61. 'productDetails'=>$productDetails,
  62. 'subjectId'=>$subjectId,
  63. );
  64. $this->render('index', $data);
  65. }
  66. /**
  67. * 导出
  68. */
  69. public function actionExportExcel(){
  70. $type=(int)Req::get('type');//类型 1分层作业 2个性化学习手册 3分层测评卷
  71. $textbookId = (int)Req::get('textbookId');//版本
  72. $moduleId = (int)Req::get('moduleId');//模块
  73. $unitId = (int)Req::get('unitId');//单元
  74. $unitName = Req::get('unitName');//单元名称
  75. $chapterId = (int)Req::get('chapterId');//节
  76. $chapterName = Req::get('chapterName');//节名称
  77. $grade = (int)Req::get('grade');//年级
  78. $classIdsStr = trim(Req::get('selectClassIds'),',');//班级
  79. $markPaperType = (int)Req::get('markPaperType');//分层测评卷类型
  80. $layoutType = (int)Req::get('layoutType');//分层测评卷类别
  81. $layoutPaper = (string)Req::get('layoutPaper');//分层测评卷试卷
  82. $layoutPaperName = (string)Req::get('layoutPaperName');//分层测评卷试卷名称
  83. $level = (int)Req::get('level');//层级目录
  84. $subjectId=Yii::app()->session['session_duoxueke_subject_id'];
  85. $subjectId=in_array($subjectId,array(3,6,51))?3:$subjectId;
  86. if(!$classIdsStr){
  87. exit("请后退,缺少班级");
  88. }
  89. $classIds=explode(',',$classIdsStr);
  90. $classIdsStr=implode(',',$classIds);
  91. $sql="select class_id,class_name from class where semester_id = '{$this->semesterId}' and class_id in ($classIdsStr) and class_type=1 and is_hide=0 and grade={$grade}";
  92. $classInfo= $this->sConn->createCommand($sql)->queryAll();
  93. $classInfo=_array_column($classInfo,'class_name','class_id');
  94. $header=array('X-Basic-V:2',"schoolId:{$this->schoolId}");
  95. $cgi=Yii::app()->params['cgi'];
  96. $postParam=array(
  97. "clazzIds"=>$classIds,
  98. "schoolId"=>$this->schoolId,
  99. "subjectId"=>$subjectId,
  100. );
  101. $username=$cgi['username'];
  102. $password=$cgi['password'];
  103. $url=$cgi['domain'].'qxk/academic/student-stat/student-level';//是否可以创建新版词汇组
  104. $rs=$this->CrmApiPost($url,$postParam,$username,$password,'POST',$header);
  105. $rs=json_decode(json_encode($rs),true);
  106. $studentLevelArr=array();
  107. if(isset($rs['errCode']) && $rs['errCode']=="00" && isset($rs['data'])){
  108. $studentLevelArr=$rs['data'];
  109. }
  110. if(!$studentLevelArr){
  111. exit('获取层级数据为空');
  112. }
  113. //每个班级的初始化数据
  114. $classLevelArr=array();
  115. foreach($classIds as $itemClassId){
  116. $classLevelArr[$itemClassId]=array(
  117. 1=>array('count'=>0,'lists'=>array()),
  118. 2=>array('count'=>0,'lists'=>array()),
  119. 3=>array('count'=>0,'lists'=>array())
  120. );
  121. }
  122. $productTypeNameArr=array(1=>'分层作业',2=>'个性化学习手册',3=>'分层测评卷');//所有产品
  123. $excelFileName='e学惠通';//产品名称 规则:末级章节+“_"+ 产品类型,如:1.2 函数的定义_分层测评卷
  124. $productTypeName='';
  125. if(isset($productTypeNameArr[$type])){
  126. $productTypeName=$productTypeNameArr[$type];
  127. if(in_array($type,array(1,2)) || ($type==3 && $markPaperType==1)){
  128. if($level==3){
  129. $excelFileName=$chapterName;
  130. }else{
  131. $excelFileName=$unitName;
  132. }
  133. $excelFileName=$excelFileName?$excelFileName.'_'.$productTypeNameArr[$type]:$productTypeNameArr[$type];
  134. }elseif($type==3 && $markPaperType==2){
  135. $excelFileName=$layoutPaperName?$layoutPaperName.'_'.$productTypeNameArr[$type]:$productTypeNameArr[$type];
  136. }
  137. }
  138. $excelFileName.='_'.time();
  139. $levelArr=array(1=>'Ⅰ类', 2=>'Ⅱ类', 3=>'Ⅲ类',);//三个等级及对应的文字
  140. $levelTotal=array(1=>0,2=>0,3=>0);//三个层级的总份数
  141. $total=0;//总份数
  142. $classStudentTotalArr=array();//班级总份数
  143. $studentIdsStr='';
  144. foreach($studentLevelArr as $item){
  145. $studentIdsStr.=$item['studentId'].',';
  146. }
  147. $studentIdsStr=trim($studentIdsStr,',');
  148. $sql="select student_id,realname from student_info where student_id in ($studentIdsStr) ";
  149. $students= $this->sConn->createCommand($sql)->queryAll();
  150. $students=_array_column($students,'realname','student_id');
  151. foreach($studentLevelArr as $item){
  152. $classId=$item['clazzId'];
  153. $studentId=$item['studentId'];
  154. $studentLevel=$item['level'];
  155. if(!isset($classLevelArr[$classId][$studentLevel])){
  156. continue;
  157. }
  158. $studentName=isset($students[$studentId])?$students[$studentId]:'';
  159. unset($students[$studentId]);
  160. $classLevelArr[$classId][$studentLevel]['lists'][]=array('student_id'=>$studentId,'student_name'=>$studentName);
  161. $classLevelArr[$classId][$studentLevel]['count']=$classLevelArr[$classId][$studentLevel]['count']+1;
  162. $levelTotal[$studentLevel]=$levelTotal[$studentLevel]+1;
  163. $total++;
  164. if(!isset($classStudentTotalArr[$classId])){
  165. $classStudentTotalArr[$classId]=0;
  166. }
  167. $classStudentTotalArr[$classId]=$classStudentTotalArr[$classId]+1;
  168. }
  169. $levelTotalArr=array();
  170. foreach($levelTotal as $studentLevel=>$itemTotal){
  171. $levelTotalArr[$studentLevel]="共{$itemTotal}份";
  172. }
  173. unset($studentLevelArr);unset($studentIdsStr);unset($levelTotal);
  174. $obpe = new PHPExcel();
  175. $obpe->setactivesheetindex(0);
  176. $obpe->getActiveSheet()->setTitle($productTypeName);
  177. $obpe->getActiveSheet()->mergeCells('A1:C1');//合并
  178. $obpe->setActiveSheetIndex(0)->setCellValue("A1","总计:共{$total}份");//总计
  179. $start=1;
  180. //非分层测评卷-高三备考
  181. if(!($type==3 && $markPaperType==2)) {
  182. $totalData = array(
  183. $levelArr,
  184. $levelTotalArr,
  185. );
  186. foreach ($totalData as $k => $v) {
  187. $start++;
  188. /* @func 设置列 */
  189. $obpe->getactivesheet()->setcellvalue('A' . $start, $v[1]);
  190. $obpe->getactivesheet()->setcellvalue('B' . $start, $v[2]);
  191. $obpe->getactivesheet()->setcellvalue('C' . $start, $v[3]);
  192. }
  193. }
  194. $start=$start+2;
  195. foreach($classLevelArr as $keyClassId=>$classData){
  196. $className=isset($classInfo[$keyClassId])?$classInfo[$keyClassId]:'';
  197. $classTotal=isset($classStudentTotalArr[$keyClassId])?$classStudentTotalArr[$keyClassId]:'';
  198. $classTitle="{$className} 共{$classTotal}人";
  199. $obpe->getActiveSheet()->mergeCells("A{$start}:C{$start}");
  200. $obpe->setActiveSheetIndex(0)->setCellValue("A{$start}",$classTitle);
  201. $max=0;//增加最大的行数
  202. $originNum = 65;//A对饮的ascii码
  203. //非分层测评卷-高三备考(分层测评卷-高三备考不需要层次和学科信息)
  204. if(!($type==3 && $markPaperType==2)) {
  205. foreach ($classData as $studentLevel => $levelData) {
  206. $index = $start;
  207. $col =chr($originNum);
  208. $levelName = isset($levelArr[$studentLevel]) ? $levelArr[$studentLevel] : '';
  209. $index++;
  210. $obpe->getactivesheet()->setcellvalue($col . $index, "{$levelName}");
  211. $index++;
  212. $obpe->getactivesheet()->setcellvalue($col . $index, "共{$levelData['count']}人");
  213. $max = $levelData['count'] + 2 > $max ? $levelData['count'] + 2 : $max;
  214. foreach ($levelData['lists'] as $studentInfo) {
  215. $index++;
  216. $obpe->getactivesheet()->setcellvalue($col . $index, $studentInfo['student_name']);
  217. }
  218. $originNum++;
  219. }
  220. }
  221. $start=$start+$max+2;
  222. }
  223. unset($classLevelArr);unset($classInfo);unset($classStudentTotalArr);
  224. //创建一个新sheet 下载地址
  225. $obpe->createSheet();
  226. $obpe->setactivesheetindex(1);
  227. $obpe->getActiveSheet()->setTitle('下载地址');
  228. //分层测评卷-高三备考
  229. if($type==3 && $markPaperType==2){
  230. $data = array(
  231. array('url'=>'地址'),
  232. array('url'=>$layoutPaper),
  233. );
  234. foreach($data as $k=>$v){
  235. $k = $k+1;
  236. $obpe->getactivesheet()->setcellvalue('A'.$k, $v['url']);
  237. }
  238. }else{//非分层测评卷-高三备考
  239. //201-同步分层练习 202-阶段学习手册 203-高一高二单元训练卷 204-高三备考训练卷
  240. $productTypeArr=array(1=>201,2=>202,3=>203);
  241. $productType=isset($productTypeArr[$type])?$productTypeArr[$type]:0;
  242. $gpId=$level==3?$chapterId:$unitId;
  243. //新高考 数学、物理-分层测评卷(同步/一轮) 英语-分层作业、个性化学习手册、分层测评卷(同步/一轮) 表里sub_type默认为0
  244. if(($type==3 && in_array($subjectId,array(3,12)) && $markPaperType==1) || ($subjectId==8 && $markPaperType!=2)) {
  245. $examType=1;
  246. $schoolInfo = BusinessSchool::model()->find('school_id=:sid', array(':sid' => $this->schoolId));
  247. if (isset($schoolInfo['province_id'])) {
  248. $provinceGradeIsnewResult = $this->apiPost('all_content/provinceGradeIsnew', array('subjectId' => array($subjectId), 'provinceId' => $schoolInfo['province_id'], 'grade' => $grade));
  249. $provinceGradeIsnewResult = json_decode(json_encode($provinceGradeIsnewResult), true);
  250. if (isset($provinceGradeIsnewResult['status']) && $provinceGradeIsnewResult['status'] && isset($provinceGradeIsnewResult['data'][0]['is_new'])) {
  251. $examType = $provinceGradeIsnewResult['data'][0]['is_new'];
  252. }
  253. }
  254. $subType=$examType;
  255. }else{
  256. $subType=0;
  257. }
  258. $sql="SELECT student_id,qcloud_url FROM `layered_isp_qcloud` where gp_id=$gpId and product_type={$productType} and subject_id={$subjectId} and sub_type={$subType} group by student_id order by student_id";
  259. $layeredIspQclouds=Yii::app()->businessDb->createCommand($sql)->queryAll();
  260. $layeredIspQclouds=_array_column($layeredIspQclouds,'qcloud_url','student_id');
  261. $data = array(
  262. array('level'=>'层次','url'=>'地址'),
  263. array('level'=>'Ⅰ类','url'=>isset($layeredIspQclouds[1])?$layeredIspQclouds[1]:''),
  264. array('level'=>'Ⅱ类','url'=>isset($layeredIspQclouds[2])?$layeredIspQclouds[2]:''),
  265. array('level'=>'Ⅲ类','url'=>isset($layeredIspQclouds[3])?$layeredIspQclouds[3]:''),
  266. );
  267. foreach($data as $k=>$v){
  268. $k = $k+1;
  269. /* @func 设置列 */
  270. $obpe->getactivesheet()->setcellvalue('A'.$k, $v['level']);
  271. $obpe->getactivesheet()->setcellvalue('B'.$k, $v['url']);
  272. }
  273. }
  274. $obpe->setactivesheetindex(0);
  275. //写入类容
  276. $obwrite = PHPExcel_IOFactory::createWriter($obpe, 'Excel5');
  277. ob_end_clean();
  278. //直接在浏览器输出
  279. header("Accept-Ranges:bytes");
  280. header('Content-Type:application/vnd.ms-excel');
  281. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  282. header("Content-Disposition:attachment;filename={$excelFileName}.xls");
  283. header('Cache-Control:max-age=0');
  284. $obwrite->save('php://output');
  285. }
  286. /**
  287. * 设置
  288. */
  289. public function actionAjaxSaveSetting(){
  290. $error = array();
  291. if (! Yii::app()->request->isAjaxRequest OR ! Yii::app()->request->isPostRequest){
  292. $error[] = '错误的来源!';
  293. }else{
  294. $type=(int)Req::post('type');//类型 1分层作业 2个性化学习手册 3分层测评卷
  295. $textbookId = (int)Req::post('textbookId');//版本
  296. $moduleId = (int)Req::post('moduleId');//模块
  297. $unitId = (int)Req::post('unitId');//单元
  298. $chapterId = (int)Req::post('chapterId');//节
  299. $grade = (int)Req::post('grade');//年级
  300. $classIds = Req::post('selectClassIds');//班级
  301. $markPaperType = (int)Req::post('markPaperType');//分层测评卷类型
  302. $layoutType = (int)Req::post('layoutType');//分层测评卷类别
  303. $layoutPaper = (string)Req::post('layoutPaper');//分层测评卷试卷
  304. $level = (int)Req::post('level');//层级
  305. if(!in_array($type,array(1,2,3))){
  306. $error[] = '请选择分层作业或个性化学习手册或分层测评卷';
  307. }
  308. if($type==1 || $type==2 || ($type==3 && $markPaperType==1)){
  309. if(!$unitId){
  310. $error[] = '单元数据不能为空';
  311. }
  312. if($level==3 && !$chapterId){
  313. $error[] = '节数据不能为空';
  314. }
  315. }
  316. if($type==3 && !in_array($markPaperType,array(1,2))){
  317. $error[] = '分层测评卷时请选择同步/一轮或高三备考';
  318. }
  319. if($type==3 && $markPaperType==2){
  320. if(!$markPaperType){
  321. $error[] = '分层测评卷类型不能为空';
  322. }
  323. if(!$layoutType){
  324. $error[] = '分层测评卷类别不能为空';
  325. }
  326. if(!$layoutPaper){
  327. $error[] = '分层测评卷-高三备考试卷不能为空';
  328. }
  329. }
  330. if(!$grade){
  331. $error[] = '请选择年级';
  332. }
  333. if(!$classIds){
  334. $error[] = '请选择班级';
  335. }
  336. $subjectIds=$this->subjectId;
  337. if(in_array($subjectIds,array(3,6,51))){
  338. $subjectIds=array(3,6,51);
  339. }else{
  340. $subjectIds=array($subjectIds);
  341. }
  342. $subjectIds=implode(',',$subjectIds);
  343. $classIdsStr=implode(',',$classIds);
  344. $sql="select class_id from exam where class_id in ({$classIdsStr}) and status=1 and subject_id in ({$subjectIds}) group by class_id";
  345. $examInfo = $this->sConn->createCommand($sql)->queryAll();
  346. $examClassIds=_array_column($examInfo,'class_id');
  347. if(count($classIds)!=count($examClassIds)){
  348. $diff=array_diff($classIds,$examClassIds);
  349. $diffClassIdsStr=implode(',',$diff);
  350. $sql="select class_id,class_name from class where class_id in ({$diffClassIdsStr})";
  351. $classInfo=$this->sConn->createCommand($sql)->queryAll();
  352. if(!$classInfo){
  353. $error[] = '没有查到班级数据';
  354. }
  355. $classNames=implode(',',_array_column($classInfo,'class_name'));
  356. $msg="{$classNames},班级无考试数据!<br/>无考试数据的班级,可以至试卷管理中导入一场考试数据";
  357. $error[] = $msg;
  358. }
  359. }
  360. if ($error) {
  361. exit(json_encode(array('status' => 0, 'error' => $error)));
  362. } else {
  363. exit(json_encode(array('status' => 1)));
  364. }
  365. }
  366. public function actionAjaxGetClasses(){
  367. $result = array();
  368. $grade=(int)Req::post('grade');
  369. if($grade){
  370. $sql = "select class_id,class_name from class where semester_id = '{$this->semesterId}' and grade = {$grade} and class_type = 1 and is_hide=0";
  371. $result = $this->sConn->createCommand($sql)->queryAll();
  372. }
  373. echo json_encode($result);exit;
  374. }
  375. public function CrmApiPost($url, $arr, $username,$password,$type='POST',$header=array())
  376. {
  377. $ch = @curl_init();
  378. $result = FALSE;
  379. if ($ch)
  380. {
  381. $data = json_encode($arr);
  382. $headerArr=array(
  383. 'Content-Type: application/json',
  384. 'Content-Length: '. strlen($data),
  385. 'Connection: false',
  386. 'Expect:'
  387. );
  388. if($header){
  389. $headerArr=array_merge($headerArr,$header);
  390. }
  391. if($type=='POST'){
  392. // Digest认证
  393. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  394. curl_setopt($ch, CURLOPT_USERPWD, $username .':'. $password);
  395. // 不输出头部
  396. curl_setopt($ch, CURLOPT_HEADER, 0);
  397. // curl_exec 获取到的内容不直接输出, 而是返回
  398. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  399. curl_setopt($ch, CURLOPT_TIMEOUT,120);
  400. curl_setopt($ch, CURLOPT_NOSIGNAL,1);
  401. curl_setopt($ch, CURLOPT_TIMEOUT_MS,120000);
  402. // 请求重启路由器的地址 传参 进行重启
  403. curl_setopt($ch, CURLOPT_URL, $url);
  404. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  405. curl_setopt($ch, CURLOPT_POST, 1);
  406. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  407. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  408. if( !curl_errno($ch))
  409. {
  410. $result = json_decode(curl_exec($ch));
  411. }
  412. // 释放资源
  413. curl_close($ch);
  414. }elseif($type=='PUT'){
  415. $data = json_encode($arr);
  416. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  417. curl_setopt($ch, CURLOPT_USERPWD, $username .':'. $password);
  418. curl_setopt($ch, CURLOPT_HEADER, 0);
  419. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  420. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  421. curl_setopt($ch, CURLOPT_URL, $url);
  422. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  423. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  424. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  425. $result = json_decode(curl_exec($ch));
  426. curl_close($ch);
  427. }
  428. }
  429. return $result;
  430. }
  431. }