123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- <?php
- namespace Api\Controller;
- use Common\Controller\ApiController;
- class TestController extends ApiController {
- public function __construct(){
- parent::__construct();
- }
-
- //设置测试章节
- public function add(){
- $partId = I('part_id');
- $courseId = I('course_id');
- if (empty($partId)){
- $data = array('code'=>1,'msg'=>'章节ID错误');
- $this->returnData($data);exit;
- }
- if (empty($courseId) || $courseId<1000000){
- $data = array('code'=>1,'msg'=>'课程ID错误');
- $this->returnData($data);exit;
- }
- $info = M('TestPart')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('tp_id')->find();
- if ($info==NULL){
- M('TestPart')->add(array('course_id'=>$courseId,'user_id'=>$this->userId,'part_id'=>$partId,'operate_dt'=>time()));
- }else {
- M('TestPart')->where(array('tp_id'=>$info['tp_id']))->save(array('part_id'=>$partId));
- }
- $data = array('code'=>0,'msg'=>'设置章节成功');
- $this->returnData($data);exit;
- }
-
- //获取测试题目
- public function index(){
- $num = I('num');
- $partId = I('part_id');
- //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
- $testType = I('test_type');
- $courseId = I('course_id');
- if (empty($partId)){
- $data = array('code'=>1,'msg'=>'章节ID错误');
- $this->returnData($data);exit;
- }
- if (empty($courseId) || $courseId<1000000){
- $data = array('code'=>1,'msg'=>'课程ID错误');
- $this->returnData($data);exit;
- }
- if (!in_array($testType, array(1,2,3,4,5,6))){
- $data = array('code'=>1,'msg'=>'题型错误');
- $this->returnData($data);exit;
- }
- if ($testType==3){
- $data = array('code'=>1,'msg'=>'即将推出');
- $this->returnData($data);exit;
- }
- /* if ($num==0){
- $num = M('Words')->where(array('course_id'=>$courseId))->count();
- } */
- if ($num<20){
- $data = array('code'=>1,'msg'=>'题量不能少于20');
- $this->returnData($data);exit;
- }
- $partInfo = M('UserWords')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('user_part')->find();
- if ($partInfo==NULL){
- $partInfo['user_part'] = 15;
- }
- $total = M('Words')->where(array('course_id'=>$courseId))->count();
- //每章节单词数量(10/3)=4 3,3,3,1
- $partWordsNum = ceil($total/$partInfo['user_part']);
- $partArr = explode(',', trim($partId,','));
- sort($partArr);
- //每章节获取单词数量(6/4)=1 2,2,1,1
- $partNum = floor($num/count($partArr));
- $number = $num - $partNum*count($partArr);
- //章节数大于出题数,每章节获取一个单词
- if ($partNum==0){
- $partNum = 1;
- $partWordsNum = $num;
- $number = 0;
- }
-
- /* if ($testType==3){
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'similar_en'=>array('neq','')))->order('words_id asc')->select();
- }else{
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order('words_id asc')->select();
- } */
- $wordsSortInfo = M('WordsSort')->where(array('course_id'=>$courseId,'user_id'=>$this->userId,'sort_status'=>1))->field('word_ids')->find();
- if ($wordsSortInfo==NULL){
- $orderby = 'words_id asc';
- }else{
- $orderby = 'field(words_id,'. $wordsSortInfo['word_ids'] .')';
- }
- //$wordsInfo = M('Words')->where(array('course_id'=>$courseId,'words_voice1|words_voice2'=>array('NEQ','')))->order($orderby)->select();
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order($orderby)->select();
- if ($wordsInfo==NULL){
- $data = array('code'=>0,'msg'=>'','data'=>array());
- $this->returnData($data);exit;
- }
- $wordsList = array();
- for($i=1;$i<=$partWordsNum;$i++){
- if (in_array($i, $partArr)){
- $page = ($i-1)*$partInfo['user_part'];
- //截取每章节单词
- $partWordsList = array_slice($wordsInfo,$page,$partInfo['user_part']);
- //随机获取指定数量的单词
- if ($number>0){
- $n = $partNum+1;
- if ($n<count($partWordsList)){
- $keys = array_rand($partWordsList,$n);
- }
- //echo $n,'---',count($partWordsList),'---',count($keys),'<br/>';
- }else{
- //echo $partNum,'<br/>';
- $n = $partNum;
- if ($n<count($partWordsList)){
- $keys = array_rand($partWordsList,$n);
- }
- }
- if ($n<count($partWordsList)){
- if (is_array($keys)){
- foreach ($keys as $k){
- $wordsList[] = $partWordsList[$k];
- }
- }else{
- $wordsList[] = $partWordsList[$keys];
- }
- }else{
- foreach ($partWordsList as $row){
- $wordsList[] = $row;
- }
- }
- $number--;
- }
- }
- $this->getWords($wordsList,$courseId,$testType);
- }
-
- //获取特殊课程测试题目
- public function specialIndex(){
- $num = I('num');
- $partId = I('part_id');
- //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
- $testType = I('test_type');
- $courseId = I('course_id');
- if (empty($partId)){
- $data = array('code'=>1,'msg'=>'章节ID错误');
- $this->returnData($data);exit;
- }
- if (empty($courseId) || $courseId<1000000){
- $data = array('code'=>1,'msg'=>'课程ID错误');
- $this->returnData($data);exit;
- }
- if (!in_array($testType, array(1,2,3,4,5,6))){
- $data = array('code'=>1,'msg'=>'题型错误');
- $this->returnData($data);exit;
- }
- if ($testType==3){
- $data = array('code'=>1,'msg'=>'即将推出');
- $this->returnData($data);exit;
- }
- /* if ($num==0){
- $num = M('Words')->where(array('course_id'=>$courseId))->count();
- } */
- if ($num<20){
- $data = array('code'=>1,'msg'=>'题量不能少于20');
- $this->returnData($data);exit;
- }
- $partArr = explode(',', trim($partId,','));
- sort($partArr);
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'operate_dt'=>array('IN',$partArr)))->order('rand()')->limit($num)->select();
- if ($wordsInfo==NULL){
- $data = array('code'=>0,'msg'=>'','data'=>array());
- $this->returnData($data);exit;
- }
- $this->getWords($wordsInfo,$courseId,$testType);
- }
-
- //特殊课程章节测试
- public function specialPart(){
- $partId = I('part_id');
- //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
- $testType = I('test_type');
- $courseId = I('course_id');
- if (empty($partId)){
- $data = array('code'=>1,'msg'=>'章节ID错误');
- $this->returnData($data);exit;
- }
- if (empty($courseId) || $courseId<1000000){
- $data = array('code'=>1,'msg'=>'课程ID错误');
- $this->returnData($data);exit;
- }
- if ($testType==3){
- $data = array('code'=>1,'msg'=>'即将推出');
- $this->returnData($data);exit;
- }
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'operate_dt'=>$partId))->order('rand()')->select();
- if ($wordsInfo==NULL){
- $data = array('code'=>0,'msg'=>'','data'=>array());
- $this->returnData($data);exit;
- }
- $this->getWords($wordsInfo,$courseId,$testType);
- }
-
- //章节测试
- public function part(){
- $partId = I('part_id');
- //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
- $testType = I('test_type');
- $courseId = I('course_id');
- if (empty($partId)){
- $data = array('code'=>1,'msg'=>'章节ID错误');
- $this->returnData($data);exit;
- }
- if (empty($courseId) || $courseId<1000000){
- $data = array('code'=>1,'msg'=>'课程ID错误');
- $this->returnData($data);exit;
- }
- if ($testType==3){
- $data = array('code'=>1,'msg'=>'即将推出');
- $this->returnData($data);exit;
- }
- $partInfo = M('UserWords')->where(array('course_id'=>$courseId,'user_id'=>$this->userId))->field('user_part')->find();
- if ($partInfo==NULL){
- $partInfo['user_part'] = 15;
- }
- $total = M('Words')->where(array('course_id'=>$courseId))->count();
- //每章节单词数量(10/3)=4 3,3,3,1
- $partWordsNum = ceil($total/$partInfo['user_part']);
- if ($testType==3){
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'similar_en'=>array('neq','')))->order('words_id asc')->select();
- }else if($testType==5){
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'jz_en1|jz_en2|jz_en3'=>array('neq','')))->order('words_id asc')->select();
- }else{
- $wordsInfo = M('Words')->where(array('course_id'=>$courseId))->order('words_id asc')->select();
- }
- if ($wordsInfo==NULL){
- $data = array('code'=>0,'msg'=>'','data'=>array());
- $this->returnData($data);exit;
- }
- $wordsList = array();
- for($i=1;$i<=$partWordsNum;$i++){
- if ($i == $partId){
- $page = ($i-1)*$partInfo['user_part'];
- //截取每章节单词
- $partWordsList = array_slice($wordsInfo,$page,$partInfo['user_part']);
- shuffle($partWordsList);
- }
- }
- $this->getWords($partWordsList,$courseId,$testType);
- }
-
- private function getWords($wordsList,$courseId,$testType){
- //生成错误项
- $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',);
- $returnData = array();
- foreach ($wordsList as $key=>$row){
- if ($testType==1){
- $returnData[$key]['words_id'] = $row['words_id'];
- $returnData[$key]['words_text'] = $this->getWordsText1($row,$testType);
- $returnData[$key]['words_title'] = $row['words_name'];
- }else if($testType==2){
- $returnData[$key]['words_id'] = $row['words_id'];
- $returnData[$key]['words_text'] = $this->getWordsText1($row,$testType);
- $returnData[$key]['words_title'] = $row['words_name'];
- }else if($testType==3){
- if (empty($row['similar_en'])){
- continue;
- }
- $keyName = '';
- foreach ($wordsKeys as $keys){
- if (!empty($row[$keys])){
- $keyName = $keys;
- break;
- }
- }
- //$condition = array('course_id'=>$courseId,'words_id'=>array('NEQ',$row['words_id']),$keyName=>array('neq',''),'similar_en'=>array('neq',''),);
- $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
- $subArr = $this->subString($row['words_name']);
- if (!empty($subArr[0])){
- $condition['words_name'] = array('LIKE',$subArr[0].'%');
- }
- if (!empty($subArr[1])){
- $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
- }
- if (!empty($subArr[2])){
- $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
- }
- //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
- $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
- $errorInfo[3] = $row;
- $returnData[$key] = $this->getStore($errorInfo, $testType);
- $returnData[$key]['words_id'] = $row['words_id'];
- $returnData[$key]['words_title'] = $row['similar_en'];
- }else if($testType==4){
- $keyName = '';
- foreach ($wordsKeys as $keys){
- if (!empty($row[$keys])){
- $keyName = $keys;
- break;
- }
- }
- //$condition = array('course_id'=>$courseId,'words_voice1|words_voice2'=>array('NEQ',''),$keyName=>array('neq',''),'words_id'=>array('NEQ',$row['words_id']));
- $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
- $subArr = $this->subString($row['words_name']);
- if (!empty($subArr[0])){
- $condition['words_name'] = array('LIKE',$subArr[0].'%');
- }
- if (!empty($subArr[1])){
- $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
- }
- if (!empty($subArr[2])){
- $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
- }
- //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
- $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
- $errorInfo[3] = $row;
- $returnData[$key] = $this->getStore($errorInfo, $testType);
- $returnData[$key]['words_id'] = $row['words_id'];
- $returnData[$key]['words_title'] = $row['words_voice1'];
- //判断读音是否空
- if (empty($row['words_read1'])){
- $returnData[$key]['words_title'] = $row['words_voice2'];
- }
- if (empty($returnData[$key]['words_title'])){
- unset($returnData[$key]);
- }
- }elseif($testType==5){
- $keyName = '';
- foreach ($wordsKeys as $keys){
- if (!empty($row[$keys])){
- $keyName = $keys;
- break;
- }
- }
- if (empty($row['jz_en1'])){
- if (!empty($row['jz_en2'])){
- $row['jz_en1'] = $row['jz_en2'];
- }else if(!empty($row['jz_en3'])){
- $row['jz_en1'] = $row['jz_en3'];
- }else{
- continue;
- }
- }
- //$condition = array('course_id'=>$courseId,'words_id'=>array('NEQ',$row['words_id']),$keyName=>array('neq',''),'jz_en1'=>array('neq',''),);
- $condition = array('words_name'=>array('NEQ',$row['words_name']),$keyName=>array('neq',''),'store_id'=>array('IN',array(17,16,14,13,12,11)),);
- $subArr = $this->subString($row['words_name']);
- if (!empty($subArr[0])){
- $condition['words_name'] = array('LIKE',$subArr[0].'%');
- }
- if (!empty($subArr[1])){
- $condition['words_name'] = array('LIKE','%'.$subArr[1].'%');
- }
- if (!empty($subArr[2])){
- $condition['words_name'] = array('LIKE','%'.$subArr[2].'%');
- }
- //$errorInfo = M('Words')->where($condition)->order('rand()')->limit(3)->select();
- $errorInfo = M('StoreWords')->where($condition)->order('store_id desc,rand()')->limit(3)->select();
- $errorInfo[3] = $row;
- $returnData[$key] = $this->getStore($errorInfo, $testType);
- $returnData[$key]['words_id'] = $row['words_id'];
- $flag = true;
- $jzEnNew = array();
- $jzEn = explode(' ', $row['jz_en1']);
- $i = 0;
- foreach ($jzEn as $val){
- $wordsVal = str_replace(array('.','?','!',',',':',';','-','"',"'",'(',')','{','}','[',']'),'',$val);
- if (strtolower($row['words_name'])==strtolower($val)){
- $flag = false;
- if ($i==0){
- $jzEnNew[] = '______';
- }else{
- $jzEnNew[] = $val;
- }
- $i++;
- }else{
- $jzEnNew[] = $val;
- }
- }
- if ($flag){
- $pos = strpos($row['jz_en1'], $row['words_name']);
- if ($pos === false) {
- $returnData[$key]['words_title'] = $row['jz_en1'];
- }else{
- $returnData[$key]['words_title'] = substr_replace($row['jz_en1'], '______', $pos, strlen($row['words_name']));
- }
- //$returnData[$key]['words_title'] = str_replace($row['words_name'], '______', $row['jz_en1']);
- }else{
- $returnData[$key]['words_title'] = implode(' ', $jzEnNew);
- }
- //$returnData[$key]['words_title'] = $row['jz_en1'];
- //$returnData[$key]['words_name'] = $row['words_voice1'];
- }
- }
- $returnNewData = array();
- foreach ($returnData as $r){
- $returnNewData[] = $r;
- }
- $data = array('code'=>0,'msg'=>'','data'=>$returnNewData);
- $this->returnData($data);exit;
- }
-
- private function getStore($info, $testType){
- $total = count($info);
- if ($total!=4){
- $limit = 4-$total;
- $condition = array('words_id'=>array('NEQ',$info[3]['words_id']),'jz_en1'=>array('neq',''),);
- $res = M('Words')->where($condition)->order('rand()')->limit($limit)->select();
- if ($limit==1){
- $info[2] = $res[0];
- }else if($limit==2){
- $info[2] = $res[0];
- $info[1] = $res[1];
- }else if($limit==1){
- $info[2] = $res[0];
- $info[1] = $res[1];
- $info[0] = $res[2];
- }
- }
- //1中文填词,2英文填意,3英文选词,4听音选意,5完形填空
- $wordsKey = array('a','b','c','d');
- $wordsArr = array();
- foreach ($info as $k=>$r){
- if ($testType==1){
- $wordsArr[$wordsKey[$k]] = $this->getWordsText1($r);
- }elseif ($testType==3){
- $wordsArr[$wordsKey[$k]] = $r['words_name'];
- }elseif ($testType==4){
- //$wordsArr[$wordsKey[$k]] = $r['words_name'];
- $wordsArr[$wordsKey[$k]] = $this->getWordsText1($r,4);
- }elseif ($testType==5){
- $wordsArr[$wordsKey[$k]] = $r['words_name'];
- }elseif ($testType==6){
-
- }
- }
- shuffle($wordsKey);
- $wordsNewArr = array();
- foreach ($wordsKey as $char){
- $wordsNewArr[] = $wordsArr[$char];
- }
- $rightKey = array_search('d', $wordsKey);
- $returnData = array(
- 'key'=>$rightKey+1,
- 'words_text' =>$wordsNewArr,
- );
- return $returnData;
- }
-
- public function subString($word){
- $firstNum = substr($word, 0, 1);
- $otherNum = substr($word, 1);
- $otherArr = array();
- if(strlen($otherNum)>0){
- $otherArr = str_split($otherNum,1);
- $otherArr = array_unique($otherArr);
- }
- if (strlen($otherNum)>5) {
- $keys = array_rand($otherArr,2);
- $twoNum = $otherArr[$keys[0]];
- $threeNum = $otherArr[$keys[1]];
- }else if (strlen($otherNum)>1) {
- $keys = array_rand($otherArr,1);
- $twoNum = $otherArr[$keys[0]];
- $threeNum = '';
- }else if(strlen($otherNum)==1){
- $twoNum = $otherNum;
- $threeNum = '';
- }else{
- $twoNum = '';
- $threeNum = '';
- }
- return array($firstNum,$twoNum,$threeNum);
- }
- }
|