TestController.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. namespace Api\Controller;
  3. use Common\Controller\ApiController;
  4. class TestController extends ApiController {
  5. public function __construct(){
  6. parent::__construct();
  7. }
  8. //设置测试章节
  9. public function add(){
  10. $partId = I('part_id');
  11. $courseId = I('course_id');
  12. if (empty($partId)){
  13. $data = array('code'=>1,'msg'=>'章节ID错误');
  14. $this->returnData($data);exit;
  15. }
  16. if (empty($courseId) || $courseId<1000000){
  17. $data = array('code'=>1,'msg'=>'课程ID错误');
  18. $this->returnData($data);exit;
  19. }
  20. $info = M('TestPart')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('tp_id')->find();
  21. if ($info==NULL){
  22. M('TestPart')->add(array('course_id'=>$courseId,'user_id'=>$this->userId,'part_id'=>$partId,'operate_dt'=>time()));
  23. }else {
  24. M('TestPart')->where(array('tp_id'=>$info['tp_id']))->save(array('part_id'=>$partId));
  25. }
  26. $data = array('code'=>0,'msg'=>'设置章节成功');
  27. $this->returnData($data);exit;
  28. }
  29. //获取测试题目
  30. public function index(){
  31. $num = I('num');
  32. $partId = I('part_id');
  33. //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
  34. $testType = I('test_type');
  35. $courseId = I('course_id');
  36. if (empty($partId)){
  37. $data = array('code'=>1,'msg'=>'章节ID错误');
  38. $this->returnData($data);exit;
  39. }
  40. if (empty($courseId) || $courseId<1000000){
  41. $data = array('code'=>1,'msg'=>'课程ID错误');
  42. $this->returnData($data);exit;
  43. }
  44. if (!in_array($testType, array(1,2,3,4,5,6))){
  45. $data = array('code'=>1,'msg'=>'题型错误');
  46. $this->returnData($data);exit;
  47. }
  48. if ($testType==3){
  49. $data = array('code'=>1,'msg'=>'即将推出');
  50. $this->returnData($data);exit;
  51. }
  52. /* if ($num==0){
  53. $num = M('Words')->where(array('course_id'=>$courseId))->count();
  54. } */
  55. if ($num<20){
  56. $data = array('code'=>1,'msg'=>'题量不能少于20');
  57. $this->returnData($data);exit;
  58. }
  59. $partInfo = M('UserWords')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('user_part')->find();
  60. if ($partInfo==NULL){
  61. $partInfo['user_part'] = 15;
  62. }
  63. $total = M('Words')->where(array('course_id'=>$courseId))->count();
  64. //每章节单词数量(10/3)=4 3,3,3,1
  65. $partWordsNum = ceil($total/$partInfo['user_part']);
  66. $partArr = explode(',', trim($partId,','));
  67. sort($partArr);
  68. //每章节获取单词数量(6/4)=1 2,2,1,1
  69. $partNum = floor($num/count($partArr));
  70. $number = $num - $partNum*count($partArr);
  71. //章节数大于出题数,每章节获取一个单词
  72. if ($partNum==0){
  73. $partNum = 1;
  74. $partWordsNum = $num;
  75. $number = 0;
  76. }
  77. /* if ($testType==3){
  78. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'similar_en'=>array('neq','')))->order('words_id asc')->select();
  79. }else{
  80. $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order('words_id asc')->select();
  81. } */
  82. $wordsSortInfo = M('WordsSort')->where(array('course_id'=>$courseId,'user_id'=>$this->userId,'sort_status'=>1))->field('word_ids')->find();
  83. if ($wordsSortInfo==NULL){
  84. $orderby = 'words_id asc';
  85. }else{
  86. $orderby = 'field(words_id,'. $wordsSortInfo['word_ids'] .')';
  87. }
  88. //$wordsInfo = M('Words')->where(array('course_id'=>$courseId,'words_voice1|words_voice2'=>array('NEQ','')))->order($orderby)->select();
  89. $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order($orderby)->select();
  90. if ($wordsInfo==NULL){
  91. $data = array('code'=>0,'msg'=>'','data'=>array());
  92. $this->returnData($data);exit;
  93. }
  94. $wordsList = array();
  95. for($i=1;$i<=$partWordsNum;$i++){
  96. if (in_array($i, $partArr)){
  97. $page = ($i-1)*$partInfo['user_part'];
  98. //截取每章节单词
  99. $partWordsList = array_slice($wordsInfo,$page,$partInfo['user_part']);
  100. //随机获取指定数量的单词
  101. if ($number>0){
  102. $n = $partNum+1;
  103. if ($n<count($partWordsList)){
  104. $keys = array_rand($partWordsList,$n);
  105. }
  106. //echo $n,'---',count($partWordsList),'---',count($keys),'<br/>';
  107. }else{
  108. //echo $partNum,'<br/>';
  109. $n = $partNum;
  110. if ($n<count($partWordsList)){
  111. $keys = array_rand($partWordsList,$n);
  112. }
  113. }
  114. if ($n<count($partWordsList)){
  115. if (is_array($keys)){
  116. foreach ($keys as $k){
  117. $wordsList[] = $partWordsList[$k];
  118. }
  119. }else{
  120. $wordsList[] = $partWordsList[$keys];
  121. }
  122. }else{
  123. foreach ($partWordsList as $row){
  124. $wordsList[] = $row;
  125. }
  126. }
  127. $number--;
  128. }
  129. }
  130. $this->getWords($wordsList,$courseId,$testType);
  131. }
  132. //获取特殊课程测试题目
  133. public function specialIndex(){
  134. $num = I('num');
  135. $partId = I('part_id');
  136. //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
  137. $testType = I('test_type');
  138. $courseId = I('course_id');
  139. if (empty($partId)){
  140. $data = array('code'=>1,'msg'=>'章节ID错误');
  141. $this->returnData($data);exit;
  142. }
  143. if (empty($courseId) || $courseId<1000000){
  144. $data = array('code'=>1,'msg'=>'课程ID错误');
  145. $this->returnData($data);exit;
  146. }
  147. if (!in_array($testType, array(1,2,3,4,5,6))){
  148. $data = array('code'=>1,'msg'=>'题型错误');
  149. $this->returnData($data);exit;
  150. }
  151. if ($testType==3){
  152. $data = array('code'=>1,'msg'=>'即将推出');
  153. $this->returnData($data);exit;
  154. }
  155. /* if ($num==0){
  156. $num = M('Words')->where(array('course_id'=>$courseId))->count();
  157. } */
  158. if ($num<20){
  159. $data = array('code'=>1,'msg'=>'题量不能少于20');
  160. $this->returnData($data);exit;
  161. }
  162. $partArr = explode(',', trim($partId,','));
  163. sort($partArr);
  164. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'operate_dt'=>array('IN',$partArr)))->order('rand()')->limit($num)->select();
  165. if ($wordsInfo==NULL){
  166. $data = array('code'=>0,'msg'=>'','data'=>array());
  167. $this->returnData($data);exit;
  168. }
  169. $this->getWords($wordsInfo,$courseId,$testType);
  170. }
  171. //特殊课程章节测试
  172. public function specialPart(){
  173. $partId = I('part_id');
  174. //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
  175. $testType = I('test_type');
  176. $courseId = I('course_id');
  177. if (empty($partId)){
  178. $data = array('code'=>1,'msg'=>'章节ID错误');
  179. $this->returnData($data);exit;
  180. }
  181. if (empty($courseId) || $courseId<1000000){
  182. $data = array('code'=>1,'msg'=>'课程ID错误');
  183. $this->returnData($data);exit;
  184. }
  185. if ($testType==3){
  186. $data = array('code'=>1,'msg'=>'即将推出');
  187. $this->returnData($data);exit;
  188. }
  189. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'operate_dt'=>$partId))->order('rand()')->select();
  190. if ($wordsInfo==NULL){
  191. $data = array('code'=>0,'msg'=>'','data'=>array());
  192. $this->returnData($data);exit;
  193. }
  194. $this->getWords($wordsInfo,$courseId,$testType);
  195. }
  196. //章节测试
  197. public function part(){
  198. $partId = I('part_id');
  199. //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
  200. $testType = I('test_type');
  201. $courseId = I('course_id');
  202. if (empty($partId)){
  203. $data = array('code'=>1,'msg'=>'章节ID错误');
  204. $this->returnData($data);exit;
  205. }
  206. if (empty($courseId) || $courseId<1000000){
  207. $data = array('code'=>1,'msg'=>'课程ID错误');
  208. $this->returnData($data);exit;
  209. }
  210. if ($testType==3){
  211. $data = array('code'=>1,'msg'=>'即将推出');
  212. $this->returnData($data);exit;
  213. }
  214. $partInfo = M('UserWords')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('user_part')->find();
  215. if ($partInfo==NULL){
  216. $partInfo['user_part'] = 15;
  217. }
  218. $total = M('Words')->where(array('course_id'=>$courseId))->count();
  219. //每章节单词数量(10/3)=4 3,3,3,1
  220. $partWordsNum = ceil($total/$partInfo['user_part']);
  221. if ($testType==3){
  222. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'similar_en'=>array('neq','')))->order('words_id asc')->select();
  223. }else if($testType==5){
  224. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'jz_en1|jz_en2|jz_en3'=>array('neq','')))->order('words_id asc')->select();
  225. }else{
  226. $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order('words_id asc')->select();
  227. }
  228. if ($wordsInfo==NULL){
  229. $data = array('code'=>0,'msg'=>'','data'=>array());
  230. $this->returnData($data);exit;
  231. }
  232. $wordsList = array();
  233. for($i=1;$i<=$partWordsNum;$i++){
  234. if ($i == $partId){
  235. $page = ($i-1)*$partInfo['user_part'];
  236. //截取每章节单词
  237. $partWordsList = array_slice($wordsInfo,$page,$partInfo['user_part']);
  238. shuffle($partWordsList);
  239. }
  240. }
  241. $this->getWords($partWordsList,$courseId,$testType);
  242. }
  243. private function getWords($wordsList,$courseId,$testType){
  244. //生成错误项
  245. $wordsKeys = array('words_n','words_pron','words_adj','words_num','words_v','words_adv','words_art','words_prep','words_conj','words_int','words_vt','words_vi','words_other',);
  246. $returnData = array();
  247. foreach ($wordsList as $key=>$row){
  248. if ($testType==1){
  249. $returnData[$key]['words_id'] = $row['words_id'];
  250. $returnData[$key]['words_text'] = $this->getWordsText1($row,$testType);
  251. $returnData[$key]['words_title'] = $row['words_name'];
  252. }else if($testType==2){
  253. $returnData[$key]['words_id'] = $row['words_id'];
  254. $returnData[$key]['words_text'] = $this->getWordsText1($row,$testType);
  255. $returnData[$key]['words_title'] = $row['words_name'];
  256. }else if($testType==3){
  257. if (empty($row['similar_en'])){
  258. continue;
  259. }
  260. $keyName = '';
  261. foreach ($wordsKeys as $keys){
  262. if (!empty($row[$keys])){
  263. $keyName = $keys;
  264. break;
  265. }
  266. }
  267. //$condition = array('course_id'=>$courseId,'words_id'=>array('NEQ',$row['words_id']),$keyName=>array('neq',''),'similar_en'=>array('neq',''),);
  268. $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
  269. $subArr = $this->subString($row['words_name']);
  270. if (!empty($subArr[0])){
  271. $condition['words_name'] = array('LIKE',$subArr[0].'%');
  272. }
  273. if (!empty($subArr[1])){
  274. $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
  275. }
  276. if (!empty($subArr[2])){
  277. $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
  278. }
  279. //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
  280. $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
  281. $errorInfo[3] = $row;
  282. $returnData[$key] = $this->getStore($errorInfo, $testType);
  283. $returnData[$key]['words_id'] = $row['words_id'];
  284. $returnData[$key]['words_title'] = $row['similar_en'];
  285. }else if($testType==4){
  286. $keyName = '';
  287. foreach ($wordsKeys as $keys){
  288. if (!empty($row[$keys])){
  289. $keyName = $keys;
  290. break;
  291. }
  292. }
  293. //$condition = array('course_id'=>$courseId,'words_voice1|words_voice2'=>array('NEQ',''),$keyName=>array('neq',''),'words_id'=>array('NEQ',$row['words_id']));
  294. $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
  295. $subArr = $this->subString($row['words_name']);
  296. if (!empty($subArr[0])){
  297. $condition['words_name'] = array('LIKE',$subArr[0].'%');
  298. }
  299. if (!empty($subArr[1])){
  300. $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
  301. }
  302. if (!empty($subArr[2])){
  303. $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
  304. }
  305. //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
  306. $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
  307. $errorInfo[3] = $row;
  308. $returnData[$key] = $this->getStore($errorInfo, $testType);
  309. $returnData[$key]['words_id'] = $row['words_id'];
  310. $returnData[$key]['words_title'] = $row['words_voice1'];
  311. //判断读音是否空
  312. if (empty($row['words_read1'])){
  313. $returnData[$key]['words_title'] = $row['words_voice2'];
  314. }
  315. if (empty($returnData[$key]['words_title'])){
  316. unset($returnData[$key]);
  317. }
  318. }elseif($testType==5){
  319. $keyName = '';
  320. foreach ($wordsKeys as $keys){
  321. if (!empty($row[$keys])){
  322. $keyName = $keys;
  323. break;
  324. }
  325. }
  326. if (empty($row['jz_en1'])){
  327. if (!empty($row['jz_en2'])){
  328. $row['jz_en1'] = $row['jz_en2'];
  329. }else if(!empty($row['jz_en3'])){
  330. $row['jz_en1'] = $row['jz_en3'];
  331. }else{
  332. continue;
  333. }
  334. }
  335. //$condition = array('course_id'=>$courseId,'words_id'=>array('NEQ',$row['words_id']),$keyName=>array('neq',''),'jz_en1'=>array('neq',''),);
  336. $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
  337. $subArr = $this->subString($row['words_name']);
  338. if (!empty($subArr[0])){
  339. $condition['words_name'] = array('LIKE',$subArr[0].'%');
  340. }
  341. if (!empty($subArr[1])){
  342. $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
  343. }
  344. if (!empty($subArr[2])){
  345. $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
  346. }
  347. //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
  348. $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
  349. $errorInfo[3] = $row;
  350. $returnData[$key] = $this->getStore($errorInfo, $testType);
  351. $returnData[$key]['words_id'] = $row['words_id'];
  352. $flag = true;
  353. $jzEnNew = array();
  354. $jzEn = explode(' ', $row['jz_en1']);
  355. $i = 0;
  356. foreach ($jzEn as $val){
  357. $wordsVal = str_replace(array('.','?','!',',',':',';','-','"',"'",'(',')','{','}','[',']'),'',$val);
  358. if (strtolower($row['words_name'])==strtolower($val)){
  359. $flag = false;
  360. if ($i==0){
  361. $jzEnNew[] = '______';
  362. }else{
  363. $jzEnNew[] = $val;
  364. }
  365. $i++;
  366. }else{
  367. $jzEnNew[] = $val;
  368. }
  369. }
  370. if ($flag){
  371. $pos = strpos($row['jz_en1'], $row['words_name']);
  372. if ($pos === false) {
  373. $returnData[$key]['words_title'] = $row['jz_en1'];
  374. }else{
  375. $returnData[$key]['words_title'] = substr_replace($row['jz_en1'], '______', $pos, strlen($row['words_name']));
  376. }
  377. //$returnData[$key]['words_title'] = str_replace($row['words_name'], '______', $row['jz_en1']);
  378. }else{
  379. $returnData[$key]['words_title'] = implode(' ', $jzEnNew);
  380. }
  381. //$returnData[$key]['words_title'] = $row['jz_en1'];
  382. //$returnData[$key]['words_name'] = $row['words_voice1'];
  383. }
  384. }
  385. $returnNewData = array();
  386. foreach ($returnData as $r){
  387. $returnNewData[] = $r;
  388. }
  389. $data = array('code'=>0,'msg'=>'','data'=>$returnNewData);
  390. $this->returnData($data);exit;
  391. }
  392. private function getStore($info, $testType){
  393. $total = count($info);
  394. if ($total!=4){
  395. $limit = 4-$total;
  396. $condition = array('words_id'=>array('NEQ',$info[3]['words_id']),'jz_en1'=>array('neq',''),);
  397. $res = M('Words')->where($condition)->order('rand()')->limit($limit)->select();
  398. if ($limit==1){
  399. $info[2] = $res[0];
  400. }else if($limit==2){
  401. $info[2] = $res[0];
  402. $info[1] = $res[1];
  403. }else if($limit==1){
  404. $info[2] = $res[0];
  405. $info[1] = $res[1];
  406. $info[0] = $res[2];
  407. }
  408. }
  409. //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
  410. $wordsKey = array('a','b','c','d');
  411. $wordsArr = array();
  412. foreach ($info as $k=>$r){
  413. if ($testType==1){
  414. $wordsArr[$wordsKey[$k]] = $this->getWordsText1($r);
  415. }elseif ($testType==3){
  416. $wordsArr[$wordsKey[$k]] = $r['words_name'];
  417. }elseif ($testType==4){
  418. //$wordsArr[$wordsKey[$k]] = $r['words_name'];
  419. $wordsArr[$wordsKey[$k]] = $this->getWordsText1($r,4);
  420. }elseif ($testType==5){
  421. $wordsArr[$wordsKey[$k]] = $r['words_name'];
  422. }elseif ($testType==6){
  423. }
  424. }
  425. shuffle($wordsKey);
  426. $wordsNewArr = array();
  427. foreach ($wordsKey as $char){
  428. $wordsNewArr[] = $wordsArr[$char];
  429. }
  430. $rightKey = array_search('d', $wordsKey);
  431. $returnData = array(
  432. 'key'=>$rightKey+1,
  433. 'words_text' =>$wordsNewArr,
  434. );
  435. return $returnData;
  436. }
  437. public function subString($word){
  438. $firstNum = substr($word, 0, 1);
  439. $otherNum = substr($word, 1);
  440. $otherArr = array();
  441. if(strlen($otherNum)>0){
  442. $otherArr = str_split($otherNum,1);
  443. $otherArr = array_unique($otherArr);
  444. }
  445. if (strlen($otherNum)>5) {
  446. $keys = array_rand($otherArr,2);
  447. $twoNum = $otherArr[$keys[0]];
  448. $threeNum = $otherArr[$keys[1]];
  449. }else if (strlen($otherNum)>1) {
  450. $keys = array_rand($otherArr,1);
  451. $twoNum = $otherArr[$keys[0]];
  452. $threeNum = '';
  453. }else if(strlen($otherNum)==1){
  454. $twoNum = $otherNum;
  455. $threeNum = '';
  456. }else{
  457. $twoNum = '';
  458. $threeNum = '';
  459. }
  460. return array($firstNum,$twoNum,$threeNum);
  461. }
  462. }