HtbController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/7/6 0006
  6. * Time: 16:26
  7. */
  8. class HtbController extends Controller
  9. {
  10. public function actionIndex()
  11. {
  12. Url::clean();
  13. $data = array();
  14. $printName = Req::get("name");
  15. $classId = Req::get("classId");
  16. $grade = (int)Req::get("grade");
  17. $newCondition = array();
  18. if($grade){
  19. $newCondition[] = 'htb.grade='.$grade;
  20. }
  21. if($printName){
  22. $newCondition[] = 'htb.name like "%'.$printName.'%"';
  23. }
  24. if($classId){
  25. $newCondition[] = 'htb.class_id='.$classId;
  26. }
  27. $resultList = $this->schoolManager->getHtb($newCondition,array('htb.create_time desc','htb.htb_id desc'));
  28. $printList = array();
  29. if($resultList["rs"]){
  30. foreach ($resultList['rs'] as $k=>$v) {
  31. $printList[$k]=$v;
  32. }
  33. }
  34. if (empty($grade)) {
  35. $grade = 'ALL';
  36. }
  37. $classes = $this->schoolManager->getClasses($grade);
  38. $data['pages'] = $resultList['pager'];
  39. $data['page_total'] = $resultList['pager']->rowsCount;
  40. $data['printList'] = $printList;
  41. $data["printName"] = $printName;
  42. $data["classes"] = $classes;
  43. $data["classId"] = $classId;
  44. $data["grade"] = $grade;
  45. $this->render('index',$data);
  46. }
  47. //二轮复习个性化提分手册设置
  48. public function actionSetting()
  49. {
  50. $data =array();
  51. /*$result = $this->apiPost('/product/batch',array(
  52. 'productId' => 1,
  53. 'parentId' => 0
  54. ));*/
  55. $result = $this->apiPost('all_product/getCatalog',array('product_id' => 42,));
  56. $data['topNode']= isset($result->data) ? $result->data : array();
  57. $this->render('setting', $data);
  58. }
  59. //获取好题本子节点
  60. public function actionAjaxChildNode()
  61. {
  62. $parentId = Req::get("parentId");
  63. /*$data = $this->apiPost('/product/batch',array(
  64. 'productId' => 1,
  65. 'parentId' => $parentId
  66. ));*/
  67. $data = $this->apiPost('all_product/getCatalogByParentId',array('parent_id' => $parentId));
  68. if ($data->status!= 1 && !empty($data->error)) {
  69. echo json_encode(array("status" => 0, "message" => $data->error));exit();
  70. }
  71. $data = !empty($data->data) ? $data->data : null;
  72. echo json_encode(array('status'=>1,'data'=>$data));exit;
  73. }
  74. //保存二轮复习个性化提分手册设置
  75. public function actionAjaxSaveHtb(){
  76. $result = array();
  77. $error = array();
  78. if (! Yii::app()->request->isAjaxRequest || ! Yii::app()->request->isPostRequest){
  79. $error[] = '错误的来源!';
  80. }else{
  81. $grade=(int)Req::post('grade');
  82. $classIds = Req::post('selectClassIds');
  83. $pcIds = (array)Req::post('pcIds');
  84. $isAnswerMerge=(int)Req::post('isAnswerMerge');
  85. $isNewVersion=(int)Req::post('isNewVersion');
  86. if(!$grade){
  87. $error[] = '请选择年级';
  88. }
  89. if(!$classIds){
  90. $error[] = '请选择班级';
  91. }
  92. if(!$pcIds){
  93. $error[] = '请指定目录';
  94. }
  95. }
  96. $nodeArr = self::createTree($pcIds);
  97. $lastNode = end($pcIds);
  98. if(!$error){
  99. $time = time();
  100. $strModel = new SStudentClassRelation();
  101. $transaction = $this->sConn->beginTransaction();
  102. try{
  103. $date = date("Y-m-d", $time);
  104. $lastday = date("Y-m-d",strtotime("$date Sunday"));
  105. $firstday = date("Y-m-d",strtotime("$lastday - 6 days"));
  106. $htbGroupId = getUniqueId($this->schoolId);
  107. $htbIds=array();
  108. foreach($classIds as $k=>$v){
  109. $htbId = getUniqueId($this->schoolId);
  110. $htbIds[]=$htbId;
  111. $this->sConn->createCommand()->insert('math_htb_setting',array(
  112. 'htb_group_id' =>$htbGroupId,
  113. 'htb_id' => $htbId,
  114. 'name' => isset($lastNode['pcName']) ? $lastNode['pcName'] : '',
  115. 'semester_id' => $this->semesterId,
  116. 'grade' => $grade,
  117. 'class_id' => $v,
  118. 'is_all_html' => 0,
  119. 'node_ids' => json_encode($nodeArr, true),
  120. 'school_group_id'=> $this->schoolGroupId,
  121. 'create_time' => $time,
  122. 'is_answer_merge' => $isAnswerMerge,
  123. 'is_new_version' => $isNewVersion,
  124. ));
  125. //FIXME 当前周已存在则不插入记录
  126. $sql = <<<SQL
  127. SELECT * FROM math_htb_class_limit l LEFT JOIN math_htb_setting s ON l.class_id=s.class_id
  128. WHERE l.class_id='{$v}' and '{$date}' BETWEEN l.week_start_time AND l.week_end_time;
  129. SQL;
  130. $data = $this->sConn->createCommand($sql)->queryRow();
  131. if(empty($data)) {
  132. $this->sConn->createCommand()->insert('math_htb_class_limit', array(
  133. 'semester_id' => $this->semesterId,
  134. 'grade' => $grade,
  135. 'class_id' => $v,
  136. 'limit' => 3,
  137. 'week_start_time' => $firstday,
  138. 'week_end_time' => $lastday,
  139. 'create_time' => $time,
  140. ));
  141. }
  142. //获取班级学生
  143. $stuData = $strModel->getRelationsByClassId_Status_0($v);
  144. if($stuData){
  145. foreach($stuData as $kk=>$vv){
  146. $this->sConn->createCommand()->insert('math_htb_student',array(
  147. 'htb_id' => $htbId,
  148. 'class_id' => $v,
  149. 'student_id' => $vv['student_id'],
  150. 'create_time'=>$time
  151. ));
  152. }
  153. }
  154. }
  155. $transaction->commit();
  156. }catch(Exception $e){
  157. $transaction->rollBack();
  158. if (YII_ENV == 'production') {
  159. $error[] = '系统错误[SQL]';
  160. } else {
  161. $error[] = $e->getMessage();
  162. }
  163. }
  164. }
  165. if($error){
  166. echo json_encode(array('status'=>0,'error'=>$error));exit;
  167. }else{
  168. if($isNewVersion){
  169. $kafkaData = array(
  170. 'school_id' => $this->schoolId,
  171. 'msg_type' => 1,//90:重置 1:新设置
  172. 'subject_id' => 3,
  173. 'send_type' => 'math_htb',
  174. 'product_type' => 21,
  175. 'gp_group_id' => $htbGroupId,
  176. 'gp_ids' => $htbIds,
  177. 'student_ids'=>array(),
  178. );
  179. sendDataToKafka("php-product-math-htb-html", $kafkaData);
  180. }
  181. echo json_encode(array('status'=>1));exit;
  182. }
  183. }
  184. /**
  185. * 获取菜单功能层级树
  186. */
  187. private static function createTree($data,$parentId = 0)
  188. {
  189. if(!$data){
  190. return array();
  191. }
  192. $tree = array();
  193. foreach($data as $k => $v)
  194. {
  195. //递归查找
  196. if($v['parentId'] == $parentId)
  197. {
  198. $v['level'] = $k + 1;
  199. $version=0;
  200. if($v['pcName']=='基础版'){
  201. $version=1;
  202. }elseif($v['pcName']=='培优版'){
  203. $version=2;
  204. }
  205. $v['version']=$version;
  206. $v['children'] = static::createTree($data, $v['pcId']);
  207. $tree[] = $v;
  208. }
  209. }
  210. return $tree;
  211. }
  212. /*
  213. * 获取班级
  214. */
  215. public function actionAjaxGetClasses(){
  216. $result = array();
  217. $grade=(int)Req::post('grade');
  218. $time = time();
  219. $date = date('Y-m-d',$time);
  220. $endDate = date("Y-m-d",strtotime("$date Sunday"));
  221. $startDate = date("Y-m-d",strtotime("$endDate - 6 days"));
  222. $startTime = strtotime($startDate. ' 00:00:00');
  223. $endTime = strtotime($endDate. ' 23:59:59');
  224. //获取班级
  225. $hasLimitClassIds = array();
  226. $sql = <<<SQL
  227. SELECT l.class_id,l.`limit`,count(*) as c_count FROM math_htb_class_limit l LEFT JOIN math_htb_setting s ON l.class_id=s.class_id
  228. WHERE s.create_time>={$startTime} and s.create_time<={$endTime} and '{$date}' BETWEEN l.week_start_time AND l.week_end_time GROUP BY s.class_id;
  229. SQL;
  230. $data = $this->sConn->createCommand($sql)->queryAll();
  231. if($data){
  232. foreach($data as $k=>$v){
  233. if($v['c_count'] >= $v['limit']){
  234. $hasLimitClassIds[] = (string)$v['class_id'];
  235. }
  236. }
  237. }
  238. if($grade){
  239. $sql = "select class_id,class_name from class where semester_id = '{$this->semesterId}' and grade = {$grade} and class_type = 1";
  240. $data = $this->sConn->createCommand($sql)->queryAll();
  241. if($data){
  242. $i = 0;
  243. foreach($data as $k => $v){
  244. $sql = "select count(student_id) count from student_class_relation where class_id = '{$v['class_id']}' and status = 0";
  245. $class_student = $this->sConn->createCommand($sql)->queryRow();
  246. if($class_student && $class_student['count']>0){
  247. $result[$i]['class_id'] = $v['class_id'];
  248. $result[$i]['class_name'] = $v['class_name'];
  249. if(in_array((string)$v['class_id'],$hasLimitClassIds,true)){
  250. $result[$i]['status'] = 1;
  251. }else{
  252. $result[$i]['status'] = 0;
  253. }
  254. $i++;
  255. }
  256. }
  257. }
  258. }
  259. echo json_encode($result);exit;
  260. }
  261. //选择学生 好题本
  262. public function actionSelectStu(){
  263. $data = array();
  264. $sname = (string)Req::get("sname"); //学生名字
  265. $htbId = (string)Req::get("htbId");
  266. if(!$htbId){
  267. Yii::app()->jump->error('参数错误!');
  268. }
  269. //获取班级名称以及周后推送名称
  270. $sql = "select s.name,c.class_name,s.grade,s.class_id,s.create_time,s.semester_id from math_htb_setting s join class c on s.class_id = c.class_id where s.htb_id = '{$htbId}'";
  271. $names = $this->sConn->createCommand($sql)->queryRow();
  272. if(!$names){
  273. $names = array();
  274. $names['name'] = '';
  275. $names['class_name'] = '';
  276. $names['grade'] = 0;
  277. }else{
  278. $data['name'] = isset($names['name'])?$names['name']:'';
  279. }
  280. $sql = "select student_id,is_htb_pdf from math_htb_student where htb_id = '{$htbId}'";
  281. $relateStudent = $this->sConn->createCommand($sql)->queryAll();
  282. $studentIds = array();
  283. $stuIsPdf = array();
  284. if($relateStudent){
  285. foreach ($relateStudent as $studentInfo) {
  286. $studentIds[] = (string)$studentInfo['student_id'];
  287. $stuIsPdf[(string)$studentInfo['student_id']] = $studentInfo['is_htb_pdf'];
  288. }
  289. }
  290. if($studentIds){
  291. $studentNames = SStudentInfo::model()->getStudents($studentIds); //求学生名字
  292. if($studentNames){
  293. if($sname){
  294. $i = 1;
  295. foreach($studentNames as $k=>$v){
  296. if(strpos($v['realname'],$sname) !== false){
  297. $data['list'][$i]['stu_id'] = (string)$v['student_id'];
  298. $data['list'][$i]['stu_name'] = $v['realname'];
  299. $data['list'][$i]['name'] = $names['class_name'].$v['realname'].$names['name'];
  300. $data['list'][$i]['is_pdf'] = isset($stuIsPdf[(string)$v['student_id']])?$stuIsPdf[(string)$v['student_id']]:0;
  301. $i++;
  302. }
  303. }
  304. }else{
  305. $i = 1;
  306. foreach($studentNames as $k=>$v){
  307. $data['list'][$i]['stu_id'] = (string)$v['student_id'];
  308. $data['list'][$i]['stu_name'] = $v['realname'];
  309. $data['list'][$i]['name'] = $names['class_name'].$v['realname'].$names['name'];
  310. $data['list'][$i]['is_pdf'] = isset($stuIsPdf[(string)$v['student_id']])?$stuIsPdf[(string)$v['student_id']]:0;
  311. $i++;
  312. }
  313. }
  314. }
  315. }
  316. $Sem=new SSemester();
  317. $code= $Sem->conn->createQuery()
  318. ->from('semester')
  319. ->where("semester_id = '".$names['semester_id']."'")
  320. ->limit(1)
  321. ->query()
  322. ->read();
  323. if($studentIds){
  324. $intarr=array(
  325. "schoolId"=>$this->schoolId,
  326. "clazzId"=>$names['class_id'],
  327. "semester"=>$code['refer_code'],
  328. "grade"=>$names['grade'],
  329. "students"=>$studentIds,
  330. "examTime"=>$names['create_time'],
  331. "classify"=>9,
  332. "subject"=>3
  333. );
  334. $basic = imsBasicAuth($this->schoolId.'_'.Yii::app()->session['coachInfo']['coach_name'],Yii::app()->params["zsy_api_key"]);
  335. $rs = json_decode(Curl::http_post_Basic_json(Yii::app()->params["get_limit_url"], json_encode($intarr),$basic));
  336. if(!$rs){
  337. Yii::app()->jump->error("接口错误");
  338. }
  339. if($rs->errCode!="00"){
  340. Yii::app()->jump->error($rs->errMsg);
  341. }
  342. if(!empty($rs->data)){
  343. $data['limitstu']=array();
  344. foreach ($rs->data as $v){
  345. $data['limitstu'][(string)$v]=$v;
  346. }
  347. }
  348. }
  349. $data['htbId'] = $htbId;
  350. $data['sname'] = $sname;
  351. $data['printType'] = 'htb_stu';
  352. $this->render('htb_stu',$data);
  353. }
  354. //下载好题本
  355. public function actionGetHtbFile(){
  356. $htbId = (string)Req::get("htbId");
  357. $studentsIds = (string)Req::get("studentsIds");
  358. $params = array();
  359. if(!$htbId){
  360. echo json_encode(array("success" => 0, "message" => "缺少参数"));
  361. exit();
  362. }
  363. if(!$studentsIds){
  364. $studentsIds = array();
  365. $sql = "select student_id from math_htb_student where htb_id = '{$htbId}' and is_htb_pdf = 1";
  366. $stuArr = $this->sConn->createCommand($sql)->queryAll();
  367. if($stuArr){
  368. foreach($stuArr as $k=>$v){
  369. $studentsIds[] = $v['student_id'];
  370. }
  371. }
  372. }else{
  373. $studentsIds = explode(',',$studentsIds);
  374. }
  375. if(!$studentsIds || empty($studentsIds)){
  376. echo json_encode(array("success" => 0, "message" => "无生成的学生"));
  377. exit();
  378. }
  379. $params['htbId'] = $htbId;
  380. $sql = "select class_id,grade,create_time,semester_id,school_group_id from math_htb_setting where htb_id = '{$htbId}'";
  381. $msg = $this->sConn->createCommand($sql)->queryRow();
  382. $Sem=new SSemester();
  383. $code= $Sem->conn->createQuery()
  384. ->from('semester')
  385. ->where("semester_id = '".$msg['semester_id']."'")
  386. ->limit(1)
  387. ->query()
  388. ->read();
  389. $intarr=array(
  390. "schoolId"=>$this->schoolId,
  391. "clazzId"=>$msg['class_id'],
  392. "semester"=>$code['refer_code'],
  393. "grade"=>$msg['grade'],
  394. "students"=>$studentsIds,
  395. "examTime"=>$msg['create_time'],
  396. "classify"=>9,
  397. "subject"=>3
  398. );
  399. if(isset($msg['school_group_id']) && $msg['school_group_id']){
  400. $apiUrl = isset(Yii::app()->params["improve_url"][$msg['school_group_id']]) ? Yii::app()->params["improve_url"][$msg['school_group_id']] : null;
  401. }else{
  402. $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null;
  403. }
  404. if(!$apiUrl){
  405. echo json_encode(array("success" => 0, "message" => "接口配置信息错误"));
  406. exit();
  407. }
  408. $schoolInfo = BusinessSchool::model()->find('school_id=:school_id', array(':school_id' => $this->schoolId));
  409. if(isset($schoolInfo['is_allow_download']) && ($schoolInfo['is_allow_download']==1)){
  410. if($studentsIds){
  411. $params["studentIds"] = $studentsIds;
  412. }
  413. }else{
  414. if(Yii::app()->params["limit_open"]){
  415. $basic = imsBasicAuth($this->schoolId.'_'.Yii::app()->session['coachInfo']['coach_name'],Yii::app()->params["zsy_api_key"]);
  416. $rs = json_decode(Curl::http_post_Basic_json(Yii::app()->params["get_limit_url"], json_encode($intarr),$basic));
  417. if(!$rs){
  418. Yii::app()->jump->error("接口错误");
  419. }
  420. if($rs->errCode!="00"){
  421. echo json_encode(array("success" => 0, "message" => $rs->errMsg));
  422. exit();
  423. }
  424. if(!empty($rs->data)){
  425. $params["studentIds"]=$rs->data;
  426. if(count($rs->data)<count($intarr['students'])){
  427. $limitinfo="购买过的".count($intarr['students'])-count($rs->data)."位学生可下载";
  428. }
  429. } else{
  430. echo json_encode(array("success" => 0, "message" =>"请先购买商品"));
  431. exit();
  432. }
  433. }else{
  434. if($studentsIds){
  435. $params["studentIds"] = $studentsIds;
  436. }
  437. }
  438. }
  439. $params['schoolId'] = $this->schoolId;
  440. $params['type'] = 5;
  441. $rs = Curl::post($apiUrl."/rest/download_week_pdf/index", $params);
  442. if(!($rs = json_decode($rs))){
  443. $rs = array(
  444. "success" => 0,
  445. "message" => "请求接口失败",
  446. );
  447. }else{
  448. //更新下载时间
  449. if($rs->success==1 || $rs->success==2){
  450. $time = time();
  451. if($studentsIds){
  452. $sql = "update math_htb_student set is_htb_download = 1,htb_download_time={$time} where htb_id = '{$htbId}' and student_id in (".implode(',',$studentsIds).")";
  453. $this->sConn->createCommand($sql)->execute();
  454. }
  455. }
  456. }
  457. if(isset($msg['school_group_id']) && $msg['school_group_id'] && isset($rs->downloadPath)){
  458. if (YII_ENV == 'pro' || YII_ENV == 'production') {
  459. $rs->downloadPath = preg_replace('/http\:\/\/zstatic\d{1,2}/','http://zstatic'.$msg['school_group_id'],$rs->downloadPath);
  460. }
  461. }
  462. echo json_encode($rs);exit;
  463. }
  464. //好题本预览
  465. public function actionPreview()
  466. {
  467. $this->layout = false;
  468. $pcIds = Req::get('pcIds');
  469. $lastNode = end($pcIds);
  470. $node_id_arr = self::createTree($pcIds);
  471. //设置的目录树
  472. $set_pc_ids = array();
  473. $this->getPcIds($node_id_arr,$set_pc_ids);
  474. //好题本数据
  475. $htb_rs_arr = $this->getHtbData(array('pcIds' => $set_pc_ids));
  476. //取出试题id
  477. $student_htb_arr = array();
  478. $all_topic_ids = array();
  479. foreach ($htb_rs_arr as $htb_rs) {
  480. if (isset($htb_rs['pcId']) && isset($htb_rs['pcName']) && isset($htb_rs['details'])) {
  481. $_student_htb_arr = array();
  482. $_student_htb_arr['pc_id'] = $htb_rs['pcId'];
  483. $_student_htb_arr['pc_name'] = $htb_rs['pcName'];
  484. $_student_htb_arr['details'] = array();
  485. if (is_array($htb_rs['details']) && $htb_rs['details']) {
  486. $_student_htb_arr['details'] = $htb_rs['details'];
  487. foreach ($htb_rs['details'] as $detail) {
  488. //控件ID:1文本编辑器 2试题
  489. if ($detail['pwId'] == 2) {
  490. $all_topic_ids[] = $detail['topicId'];
  491. }
  492. }
  493. }
  494. $student_htb_arr[$htb_rs['pcId']] = $_student_htb_arr;
  495. }
  496. }
  497. if (!$student_htb_arr) {
  498. echo '<h3 style="color: red">该节点下无内容,请不要生成,避免次数浪费!并把该情况及时反馈给研究院老师!</h3>';exit;
  499. }
  500. //题详情
  501. if ($all_topic_ids) {
  502. $_params = array('isFormat' => 1, 'isNoReplaceLable' => 0);
  503. $topic_info_arr = $this->apiBrainPost('/topic/batchAll', array('topicIds' => $all_topic_ids, 'subjectId' => $this->subjectId, 'params' => $_params), 25, true);
  504. }else{
  505. $topic_info_arr = array();
  506. }
  507. //题考点最高使用量
  508. $topic_method_use = array();
  509. $topic_rel_method_max = array();//试题对应的使用最多考点id
  510. if ($topic_info_arr) {
  511. foreach ($topic_info_arr as $topic_info) {
  512. $method_names = isset($topic_info['method_names']) && $topic_info['method_names'] ? $topic_info['method_names'] : array();
  513. $method_use_arr = array();
  514. foreach ($method_names as $key=>$method){
  515. $method_use_arr[$key] = Yii::app()->method_use_num->hmget('method_stat', $key);
  516. }
  517. if ($method_use_arr) {
  518. arsort($method_use_arr);
  519. foreach ($method_use_arr as $method_id => $num) {
  520. $topic_method_use[$topic_info['topic_id']] = $topic_info['method_names'][$method_id];
  521. $topic_rel_method_max[$topic_info['topic_id']] = $method_id;
  522. break;
  523. }
  524. }
  525. }
  526. }
  527. //考点视频二维码
  528. $method_qrcode_arr = array();
  529. if ($topic_rel_method_max) {
  530. $method_qrcode_rs = self::methodVideoQrcode(array_values($topic_rel_method_max));
  531. foreach ($topic_rel_method_max as $topic_id => $method_id) {
  532. if (isset($method_qrcode_rs[$method_id]) && $method_qrcode_rs[$method_id]) {
  533. $method_qrcode_arr[$topic_id] = $method_qrcode_rs[$method_id];
  534. }
  535. }
  536. }
  537. $html_data['htb_set_name'] = isset($lastNode['pcName']) ? $lastNode['pcName'] : '好题本';
  538. $html_data['node_id_arr'] = $node_id_arr;
  539. $html_data['htb_text_arr'] = $student_htb_arr;
  540. $html_data['topic_info_arr'] = $topic_info_arr;
  541. $html_data['topic_method_use'] = $topic_method_use;
  542. $html_data['method_video'] = $method_qrcode_arr;
  543. $this->render('preview', $html_data);
  544. }
  545. /**
  546. * 获取目录id
  547. * @param $node_id_arr
  548. * @param $set_pc_ids
  549. */
  550. private function getPcIds($node_id_arr,&$set_pc_ids)
  551. {
  552. foreach ($node_id_arr as $_node_arr) {
  553. if (isset($_node_arr['pcId'])) {
  554. $set_pc_ids[] = $_node_arr['pcId'];
  555. if (isset($_node_arr['children']) && $_node_arr['children']) {
  556. $this->getPcIds($_node_arr['children'], $set_pc_ids);
  557. break;
  558. }
  559. }
  560. }
  561. }
  562. /**
  563. * 获取好题本数据
  564. * @param $pc_ids
  565. * @return array
  566. */
  567. private function getHtbData($pc_ids)
  568. {
  569. $htb_rs = array();
  570. $rs = $this->apiPost('/product/detail',$pc_ids);
  571. $rs = json_decode(json_encode($rs),true);
  572. if (isset($rs['status'])) {
  573. if ($rs['status'] == 1 && isset($rs['data'])) {
  574. $htb_rs = $rs['data'];
  575. }else{
  576. echo'获取好题本数据失败,错误信息是:'.(isset($rs['error'])?$rs['error']:''),0,true;exit;
  577. }
  578. }else{
  579. echo 'brain:product/detail接口返回失败';
  580. }
  581. return $htb_rs;
  582. }
  583. /**
  584. * 试题考点二维码
  585. * @param $method_ids
  586. * @return array
  587. */
  588. static public function methodVideoQrcode($method_ids)
  589. {
  590. $rs = array();
  591. if ($method_ids) {
  592. foreach ($method_ids as $method_id) {
  593. $redis_json = Yii::app()->method_video->hget('content:method_video', $method_id);
  594. if ($redis_json) {
  595. if (is_array($redis_json)) {
  596. $video_arr = $redis_json;
  597. } else {
  598. $video_arr = json_decode($redis_json, true);
  599. }
  600. if (isset($video_arr[0]) && isset($video_arr[0]['video_qrcode'])) {
  601. $rs[$method_id] = $video_arr[0]['video_qrcode'];
  602. }
  603. }
  604. }
  605. }
  606. return $rs;
  607. }
  608. /**
  609. * 重置列表
  610. */
  611. public function actionResetHtb(){
  612. $htbGroupId=(string)Req::get('htbGroupId');
  613. $data = array();
  614. $resetCount = 0;
  615. if($htbGroupId){
  616. $sql = "select mhs.htb_id,mhs.`name`,mhs.class_id,mhs.is_reset,c.class_name from math_htb_setting mhs join class c on mhs.class_id = c.class_id where mhs.htb_group_id = '{$htbGroupId}'";
  617. $data = $this->sConn->createCommand($sql)->queryAll();
  618. if($data){
  619. foreach($data as &$item){
  620. if($item["is_reset"]){
  621. $resetCount++;
  622. }
  623. $item['total_count']=0;
  624. $item['create_count']=0;
  625. $sql = "select count(*) total_count,count(case when is_htb_pdf = 1 then 1 end) create_count from math_htb_student where htb_id = '{$item['htb_id']}'";
  626. $htbStudent = $this->sConn->createCommand($sql)->queryRow();
  627. if($htbStudent){
  628. $item['total_count'] = $htbStudent["total_count"];
  629. $item['create_count'] = $htbStudent["create_count"];
  630. }
  631. }
  632. }
  633. }
  634. if(!isset(Yii::app()->session['testFlag']) || Yii::app()->session['testFlag']==1 ){
  635. $resetCount = 0;
  636. }
  637. //debug($resetCount);
  638. $this->render('htb_reset',array("data"=>$data,"resetCount"=>$resetCount,'htb_group_id'=>$htbGroupId));
  639. }
  640. /**
  641. * 重置接口
  642. */
  643. public function actionAjaxResetHtb(){
  644. $error = array();
  645. if (! Yii::app()->request->isAjaxRequest OR ! Yii::app()->request->isPostRequest){
  646. $error[] = '错误的来源!';
  647. }else{
  648. $reqHtbIds=Req::post('htbIds');
  649. $reqhtbGroupId=Req::post('htbGroupId');
  650. if(!$reqHtbIds){
  651. $error[] = '请选择班级';
  652. }
  653. if(!$reqhtbGroupId){
  654. $error[]='无组id';
  655. }
  656. }
  657. if(!$error){
  658. $time = time();
  659. $transaction = $this->sConn->beginTransaction();
  660. try{
  661. $reqHtbIdsStr=implode(',',$reqHtbIds);
  662. $sql="select htb_group_id,htb_id,is_reset from math_htb_setting where htb_group_id={$reqhtbGroupId} and htb_id in ({$reqHtbIdsStr}) and is_new_version=1";
  663. $htb=$this->sConn->createCommand($sql)->queryAll();
  664. if(!$htb){
  665. throw new \Exception('无可重置的有效数据');
  666. }
  667. $htbIds=array();
  668. $htbGroupId=0;
  669. foreach($htb as $item){
  670. $htbGroupId=$item['htb_group_id'];
  671. $htbIds[]=$item['htb_id'];
  672. }
  673. $this->sConn->createCommand("update math_htb_setting set is_all_html = 0,is_reset=1 where htb_id in (".implode(',',$htbIds).") ")->execute();
  674. $sql = "update math_htb_student set htb_download_time = 0,is_htb_download=0,is_htb_html=0,htb_html_path='',is_htb_pdf=0,htb_pdf_path='' where htb_id in (".implode(',',$htbIds).") ";
  675. $this->sConn->createCommand($sql)->execute();
  676. //重置后清除已存在的下载任务
  677. /*$task_sql = "delete from pack_product_task where school_id ={$this->schoolId} and unique_key in (". implode(",", $htbIds).") and product_type=10";
  678. $this->conn->createCommand($task_sql)->execute();*/
  679. $transaction->commit();
  680. }catch(Exception $e){
  681. $transaction->rollBack();
  682. if (YII_ENV == 'production') {
  683. $error[] = '系统错误[SQL]';
  684. } else {
  685. $error[] = $e->getMessage();
  686. }
  687. }
  688. }
  689. if($error){
  690. echo json_encode(array('status'=>0,'error'=>$error));exit;
  691. }else{
  692. //发消息
  693. if($htbGroupId && $htbIds){
  694. $kafkaData = array(
  695. 'school_id' => $this->schoolId,
  696. 'msg_type' => 90,//90:重置 1:新设置
  697. 'subject_id' => 3,
  698. 'send_type' => 'math_htb',
  699. 'product_type' => 21,
  700. 'gp_group_id' => $htbGroupId,
  701. 'gp_ids' => $htbIds,
  702. 'student_ids'=>array(),
  703. );
  704. sendDataToKafka("php-product-math-htb-html", $kafkaData);
  705. }
  706. echo json_encode(array('status'=>1));exit;
  707. }
  708. }
  709. //好题本预览
  710. public function actionNewPreview()
  711. {
  712. $this->layout = false;
  713. $pcIds = Req::get('pcIds');
  714. $lastNode = end($pcIds);
  715. $nodeIdArr = self::createTree($pcIds);
  716. if(!isset($lastNode['pcId']) || !$lastNode['pcId']){
  717. echo '<h3 style="color: red">该节点下无内容,请不要生成</h3>';exit;
  718. }
  719. $lastPcId=$lastNode['pcId'];
  720. //设置的目录树
  721. $setPcIds =$htbRs= array();
  722. $this->getPcIds($nodeIdArr,$setPcIds);
  723. //好题本数据
  724. $htbRs = $this->getNewHtbData(array('catalogIds' => array($lastPcId)));
  725. //知识点和关联题对应关系
  726. //382:知识点1 384:知识点2 386:知识点3 388:知识点4 390:知识点5 392:知识点6 394:知识点7 396:知识点8 383:关联题1 385:关联题2 387:关联题3 389:关联题4 391:关联题5 393:关联题6 395:关联题7 397:关联题8
  727. $mapColumnIdArr=array(
  728. 383 => 382,
  729. 385=> 384,
  730. 387=> 386,
  731. 389=> 388,
  732. 391 => 390,
  733. 393=> 392,
  734. 395=> 394,
  735. 397=> 396,
  736. );
  737. $htbNodes=$topicIds=array();
  738. //处理知识点内容
  739. foreach($htbRs as $item){
  740. $columnId=$item['column_id'];
  741. if($item['topic_id']){
  742. $topicIds[]=(string)$item['topic_id'];
  743. }
  744. if(in_array($columnId,$mapColumnIdArr) && !isset($htbNodes[$columnId])){
  745. $item['topic_ids']=array();
  746. $htbNodes[$columnId]=$item;
  747. }
  748. }
  749. if (!$htbNodes) {
  750. echo '<h3 style="color: red">该节点下无内容,请不要生成,避免次数浪费!并把该情况及时反馈给研究院老师!</h3>';exit;
  751. }
  752. //知识点加入试题id
  753. foreach($htbRs as $item){
  754. $columnId=$item['column_id'];
  755. if(isset($mapColumnIdArr[$columnId]) && $mapColumnIdArr[$columnId] && $item['topic_id']){
  756. $mapColumnId=$mapColumnIdArr[$columnId];
  757. if(isset($htbNodes[$mapColumnId]) && $htbNodes[$mapColumnId]){
  758. $htbNodes[$mapColumnId]['topic_ids'][]=(string)$item['topic_id'];
  759. }
  760. }
  761. }
  762. //题详情
  763. if ($topicIds) {
  764. $params = array('isFormat' => 0 , 'isTird' => 0 ,'isNewEnglish' => 0);
  765. $topicInfoArr = $this->apiBrainPost('topic/batchAll', array('topicIds' => $topicIds, 'subjectId' => $this->subjectId, 'params' => $params), 25, true);
  766. if(isset($topicInfoArr['status']) && $topicInfoArr['status']===0){
  767. echo '<h3 style="color: red">没有获取到试题数据,请稍后再试!</h3>';exit;
  768. }
  769. }else{
  770. $topicInfoArr = array();
  771. }
  772. foreach($topicInfoArr as $key=>$item){
  773. //全学科逻辑题型,只显示单选多选
  774. $_type_name='';
  775. if(in_array($item['type_name'],array('单选题','多选题'))){
  776. $_type_name=str_replace('题','',$item['type_name']);
  777. }
  778. $item['_type_name']=$_type_name;
  779. $topicInfoArr[(string)$item['id']]=$item;
  780. unset( $topicInfoArr[$key]);
  781. }
  782. //知识点试题顺序
  783. if($topicInfoArr){
  784. $number=1;
  785. foreach($htbNodes as &$item){
  786. $topicIdBasicType=array();
  787. $item['number_word']=$this->numToWord($number);
  788. if(isset($item['topic_ids']) && $item['topic_ids']){
  789. foreach($item['topic_ids'] as $topicId){
  790. if(isset($topicInfoArr[$topicId]) && isset($topicInfoArr[$topicId]['basic_type_id']) ){
  791. $topicIdBasicType[$topicId]=$topicInfoArr[$topicId]['basic_type_id'];
  792. }
  793. }
  794. asort($topicIdBasicType);
  795. $item['topic_ids']=array_keys($topicIdBasicType);
  796. }
  797. $number++;
  798. }
  799. }
  800. $title=$versionStr='';
  801. $version=0;
  802. $versionArr=array('基础版'=>1,'培优版'=>2);
  803. foreach($pcIds as $val){
  804. if(!$version && !empty($versionArr[$val['pcName']])){
  805. $version=$versionArr[$val['pcName']];
  806. $versionStr=$val['pcName'];
  807. }
  808. $title.=$val['pcName'].' ';
  809. }
  810. $data=array(
  811. 'title'=>$title,
  812. 'version'=>$version,
  813. 'version_str'=>$versionStr,
  814. 'htbNodes'=>$htbNodes,
  815. 'topicInfos'=>$topicInfoArr,
  816. );
  817. $this->render('new_preview', $data);
  818. }
  819. /**
  820. * 获取好题本数据
  821. * @param $pc_ids
  822. * @return array
  823. */
  824. private function getNewHtbData($pc_ids)
  825. {
  826. $htb_rs = array();
  827. $rs = $this->apiPost('/all_content/detail',$pc_ids);
  828. $rs = json_decode(json_encode($rs),true);
  829. if (isset($rs['status'])) {
  830. if ($rs['status'] == 1 && isset($rs['data'])) {
  831. $htb_rs = $rs['data'];
  832. }else{
  833. echo'获取好题本数据失败,错误信息是:'.(isset($rs['error'])?$rs['error']:''),0,true;exit;
  834. }
  835. }else{
  836. echo 'brain:all_content/detail接口返回失败';exit;
  837. }
  838. return $htb_rs;
  839. }
  840. /**
  841. * 数学转汉字
  842. * @param $num
  843. * @return string
  844. */
  845. private function numToWord($num)
  846. {
  847. $chiNum =array('零','一','二','三','四','五','六','七','八','九');
  848. $chiUni =array('','十','百','千','万','亿','十','百','千');
  849. $num_str = (string)$num;
  850. $count =strlen($num_str);
  851. $last_flag = true;//上一个 是否为0
  852. $zero_flag = true;//是否第一个
  853. $temp_num = null;//临时数字
  854. $chiStr ='';//拼接结果
  855. if ($count == 2) {//两位数
  856. $temp_num =$num_str[0];
  857. $chiStr =$temp_num == 1 ?$chiUni[1] :$chiNum[$temp_num].$chiUni[1];
  858. $temp_num =$num_str[1];
  859. $chiStr .=$temp_num == 0 ?'' :$chiNum[$temp_num];
  860. }else if($count > 2){
  861. $index = 0;
  862. for ($i=$count-1;$i >= 0 ;$i--) {
  863. $temp_num =$num_str[$i];
  864. if ($temp_num == 0) {
  865. if (!$zero_flag && !$last_flag ) {
  866. $chiStr =$chiNum[$temp_num].$chiStr;
  867. $last_flag = true;
  868. }
  869. }else{
  870. $chiStr =$chiNum[$temp_num].$chiUni[$index%9] .$chiStr;
  871. $zero_flag = false;
  872. $last_flag = false;
  873. }
  874. $index ++;
  875. }
  876. }else{
  877. $chiStr =$chiNum[$num_str[0]];
  878. }
  879. return $chiStr;
  880. }
  881. }