123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- <?php
- /**
- * word试题解析入库操作类
- * @author Andy
- * @date 2021-08-20
- * @company 上海互教教育科技有限公司.
- */
- class ParseapiController extends CController
- {
- public $supportTypeName = array(1=>'选择题','2'=>'多选题',11=>'多选题',5=>'填空题',7=>'解答题');
- public $charToNum = array('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13);
- public function init()
- {
- set_time_limit(0);
- @ini_set('memory_limit', '512M');
- }
- public function actionIndex()
- {
- // 获取curl过来值
- $wordId = isset($_GET['wid']) ? intval($_GET['wid']) : 0;
- $schoolId = isset($_GET['sid']) ? intval($_GET['sid']) : 0;
- if (!$wordId || !$schoolId) {
- exit('word或学校ID异常');
- }
-
- // 连接业务库
- // $busDsn = 'mysql:host=' . Yii::app()->params["businessDb"]['addr'] . ';dbname=' . Yii::app()->params["default_db"]['name'] . ';';
- // $busDbh = new PDO($busDsn, Yii::app()->params["default_server"]['username'], Yii::app()->params["default_server"]['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
- $busDbh = Yii::app()->businessDb;
- $dataBaseInfo = $busDbh->createCommand('SELECT * FROM `database` WHERE `school_id`=' . $schoolId)->queryOne();
- if (empty($dataBaseInfo)) {
- exit('未找到数据库链接信息!');
- }
- // 连接学校库
- $busDbh = null;
- $schDsn = 'mysql:host=' . $dataBaseInfo['database_host'] . ';dbname=' . $dataBaseInfo['database_name'] . ';';
- $schDbh = new PDO($schDsn, $dataBaseInfo['database_user'], $dataBaseInfo['database_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
- $wordInfo = $schDbh->query('SELECT `word_id`,`word_name`,`subject_id`,`uploader_id`,`type_id`,`exam_group_id` FROM `topic_word` WHERE `word_id`=' . $wordId)->fetch(PDO::FETCH_ASSOC);
- if (empty($wordInfo)) {
- exit('未找到上传word信息!');
- }
- $parseJson = file_get_contents('php://input');
- $result = json_decode($parseJson, true);
- if (empty($result) || $result['errcode'] > 0) { // 解析失败
- $stmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
- $stmt->execute(array(':wid' => $wordId, ':reason' => $result['errmsg'], ':content' => $parseJson, ':time' => time()));
- exit('解析失败!');
- } else {
- if ($result['callback_type'] == 1) { // 表示人工智能解析完成,更新当前试卷为可预览状态
- $sctmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 5,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
- $sctmt->execute(array(':wid' => $wordId, ':time' => time(), ':content' => $parseJson));
- exit('更新试卷解析完成状态成功');
- } elseif ($result['callback_type'] == 2) { // 试卷解析确认完成,返回试题结构处理
- // 验证试题内容是否为空
- if (empty($result['items'])) {
- $stmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
- $stmt->execute(array(':wid' => $wordId, ':reason' => '返回word试题内容为空', ':content' => $parseJson, ':time' => time()));
- exit('返回试题内容为空');
- } else {
- $sctmt = $schDbh->prepare('UPDATE `topic_word` SET `content` = :content,parse_time=:time WHERE `word_id` =:wid');
- $sctmt->execute(array(':wid' => $wordId, ':time' => time(), ':content' => $parseJson));
- }
- // 处理各个端口上传的试题解析
- if ($wordInfo['exam_group_id']) { // 助教端第三方试卷解析
- if ($wordInfo['subject_id'] == 8) { // 高中英语解析单独处理
- self::thirdpartyEnglish($schDbh, $result['items'], $wordInfo,$schoolId);
- } else {
- self::thirdpartyPaper($schDbh, $result['items'], $wordInfo,$schoolId);
- }
- }
- } else {
- exit('解析请求异常,请指定合法的callback_type类型');
- }
- // 处理完
- $resu = $schDbh->prepare('UPDATE `topic_word` SET `status` = 2,`parse_time`=:time WHERE `word_id` =:wid');
- $resu->execute(array(':wid' => $wordId, ':time' => time()));
- // 插入解析时间记录
- $resPro = $schDbh->prepare("INSERT INTO `exam_process` (`exam_group_id`, `action_type`, `action_time`) VALUES (:exam_group_id, :tempType, :time)");
- $resPro->execute(array(':exam_group_id' => $wordInfo['exam_group_id'], ':tempType' => 4, ':time' => time()));
- $schDbh = null;
- exit('解析完成!');
- }
- }
-
- /**
- * 第三方试卷试题解析
- * @param $topics
- * @return string
- */
- public function thirdpartyPaper($schDbh, $topics, $word,$schoolId)
- {
- $apiTopics = array();
- $errorMsg = null;
- $apiParam = Yii::app()->params['api'][0];
- $apiParam['prefix'] .= 'topic_clear_redis/ctopic';
- $mathSubject = Yii::app()->params['math_subjects'];
- $topicDiffculty = array('超易'=>1,'易'=>2,'中档偏易'=>3,'中档偏难'=>4,'难'=>5,'超难'=>6);
- // 获取试卷试题并做相关验证等
- $paperTopics = $schDbh->query('select pt.topic_id,pt.type,pp.paper_id from paper_topic_relation pt JOIN (select p.paper_id from exam e left JOIN paper p on e.exam_id = p.exam_id where e.exam_group_id = ' . $word['exam_group_id'] . ' GROUP BY e.exam_group_id) as pp on pt.paper_id = pp.paper_id order by pt.order asc')->fetchAll(PDO::FETCH_ASSOC);
- if (empty($paperTopics)) {
- $errorMsg = '未找到考试试题';
- } elseif (count($paperTopics) != count($topics)) {
- $errorMsg = 'WORD题量与试卷题量不同';
- }
- if ($errorMsg) {
- $stmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,parse_time=:time WHERE `word_id` =:wid');
- $stmt->execute(array(':wid' => $word['word_id'], ':reason' => $errorMsg, ':time' => time()));
- exit($errorMsg);
- }
- // 删除试题缓存
- $tempTopicIds = array();
- foreach ($paperTopics as $tempTopic) {
- $tempTopicIds[] = $tempTopic['topic_id'];
- }
- if ($tempTopicIds) {
- self::getApiData($apiParam, 2, json_encode(array('topicIds' => $tempTopicIds)), 3);
- }
- // 更新试题操作
- foreach($topics as $key => $item) {
- $topicId = $paperTopics[$key]['topic_id'];
- $apiTopics[] = $topicId;
- $bankType = (int)$item['topic_type_id'];
- $diffName = isset($item['difficulty']) ? $item['difficulty'] : '中档偏易';
- $difficulty = isset($topicDiffculty[$diffName]) ? $topicDiffculty[$diffName] : 3;
- // 对高中数学与全学科题型及选项设置不同处理
- $optionsRank = isset($item['options_rank']) ? (int)$item['options_rank'] : 1;
- //if (in_array($word['subject_id'], $mathSubject)) {
- // $optionsRank = $item['options_rank'] - 1;
- //} else {
- // $optionsRank = isset($item['options_rank']) ? (int)$item['options_rank'] : 1;
- //}
- try {
- $schDbh->beginTransaction(); // 开启事务
- // topic表
- $exeTopic = $schDbh->prepare('UPDATE `topic` SET `topic_title` = :topic_title,`topic_difficulty` = :topic_difficulty,`source_title`=:source_title,`bank_type`=:bank_type,`parse_content`=:parse_content,`update_time`=:update_time WHERE `topic_id` =:topic_id');
- $exeTopic->execute(array(':topic_id' => $topicId, ':topic_title' => $item['stem'],':topic_difficulty'=>$difficulty,':bank_type'=>$bankType, ':source_title' => 'word上传试题', ':parse_content' => $item['parse'], ':update_time' => time()));
- // topic_item
- $exeItem = $schDbh->prepare('UPDATE `topic_item` SET `topic_title` = :topic_title WHERE `topic_id` =:topic_id');
- $exeItem->execute(array(':topic_id' => $topicId, ':topic_title' => $item['stem']));
-
- // 选项及答案处理
- $exeOption = $schDbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
- $exeOption->execute(array(':topic_id' => $topicId));
- // 选择题选项处理
- if (isset($item['options']) && $item['options']) { // 含有选项的试题
- $optionKeys = array();
- foreach ($this->charToNum as $char => $num) {
- if (strpos($item['key'], $char) !== false) {
- $optionKeys[] = $char;
- }
- }
- $correct = 'A';
- $rowThr = $schDbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
- foreach ($item['options'] as $vk => $option) {
- $isTrue = in_array($correct, $optionKeys) ? 1 : 0;
- $getRowThr = $rowThr->execute(array(':tpid'=>$topicId,':content'=>$option,':correct'=>$isTrue,':sort_order'=>$vk));
- ++$correct;
- }
- } else {
- $rowFiv = $schDbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
- if (is_array($item['key']) && $item['key']) {
- foreach ($item['key'] as $topicKey) {
- $getRowFiv = $rowFiv->execute(array(':tpid' => $topicId, ':content' => $topicKey));
- }
- } else {
- $getRowFiv = $rowFiv->execute(array(':tpid' => $topicId, ':content' => $item['key']));
- if (!$getRowFiv) {
- throw new PDOException('插入topic_item_option表失败!');
- }
- }
- }
- // 组卷完成标识
- $resu = $schDbh->prepare('UPDATE `exam_group` SET `is_answersheet` = 1 WHERE `exam_group_id` =:exam_group_id');
- $exeSheet = $resu->execute(array(':exam_group_id' => $word['exam_group_id']));
- if (!$exeSheet) {
- throw new PDOException('更新exam_group表失败!');
- }
- // word与试题关系
- $rowFor = $schDbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
- $getRowFor = $rowFor->execute(array(':wid' => $word['word_id'], ':toid' => $topicId));
- if (!$getRowFor) {
- throw new PDOException('插入word_to_topic表失败!');
- }
- $schDbh->commit();
- } catch (PDOException $e) {
- $schDbh->rollback();
- $exeReason = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,parse_time=:time WHERE `word_id` =:wid');
- $exeReason->execute(array(':wid' => $word['word_id'], ':reason' => $e->getMessage(), ':time' => time()));
- exit($e->getMessage());
- }
- }
- if($apiTopics){
- //调用试题检索接口
- $this->searchTopics($apiTopics,$schoolId);
- }
- }
- // 第三方英语新解析处理
- public function thirdpartyEnglish($schDbh, $topics, $word,$schoolId)
- {
- $errorMsg = null;
- $apiParam = Yii::app()->params['api'][0];
- $mathSubject = Yii::app()->params['math_subjects'];
- $topicDiffculty = array('超易'=>1,'易'=>2,'中档偏易'=>3,'中档偏难'=>4,'难'=>5,'超难'=>6,);
- // 获取全学科英语题型
- $basicType = array();
- $topicTypeConfig = array();
- $topicTypeJson = self::getApiData(array(
- 'username' => $apiParam['username'],
- 'password' => $apiParam['password'],
- 'prefix' => $apiParam['prefix'] . 'all_subject/topic_type/subjectId/' . $word['subject_id']
- ), 1, '', 3);
- $topicTypeRes = json_decode($topicTypeJson, true);
- if (isset($topicTypeRes['status']) && $topicTypeRes['status'] == 1 && $topicTypeRes['data']) {
- foreach ($topicTypeRes['data'] as $topicType) {
- $basicType[$topicType['topic_type_id']] = $topicType['btt_id'];
- $topicTypeConfig[$topicType['topic_type_id']] = $topicType;
- }
- unset($topicTypeJson,$topicTypeRes);
- }
- // 获取试卷试题并做相关验证等
- $paperTopics = $schDbh->query('select pt.topic_id,pt.stem_id,pt.paper_id from paper_topic_relation pt JOIN (select p.paper_id from exam e left JOIN paper p on e.exam_id = p.exam_id where e.exam_group_id = ' . $word['exam_group_id'] . ' GROUP BY e.exam_group_id) as pp on pt.paper_id = pp.paper_id order by pt.order asc')->fetchAll(PDO::FETCH_ASSOC);
- if (empty($paperTopics)) {
- $stmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,parse_time=:time WHERE `word_id` =:wid');
- $stmt->execute(array(':wid' => $word['word_id'], ':reason' => '未找到考试试题', ':time' => time()));
- exit('未找到考试试题');
- }
- $paperId = 0;
- $stemTopicIds = array();
- //$packItemRelate = array();
- foreach ($paperTopics as $paperTopic) {
- $paperId = $paperTopic['paper_id'];
- $stemTopicIds[] = $paperTopic['topic_id'];
- //$packItemRelate[$paperTopic['topic_id']] = $paperTopic['stem_id'];
- }
- // 更新试题内容
- foreach ($topics as $topic) {
- $slaveNum = isset($topic['slave']) && $topic['slave'] ? count($topic['slave']) : 0;
- $diffName = isset($topic['difficulty']) ? $topic['difficulty'] : '中';
- $difficulty = isset($topicDiffculty[$diffName]) ? $topicDiffculty[$diffName] : 3;
- $answerType = isset($topic['answer_type']) && $topic['answer_type'] ? (int)$topic['answer_type'] : 0;
- $basicTypeId = isset($basicType[$topic['topic_type_id']]) ? $basicType[$topic['topic_type_id']] : 0;
- try {
- // 判断并处理当前题目是否含有大小题
- //$schDbh->beginTransaction();
- if ($slaveNum > 0) {
- $slaveTopicIds = array_splice($stemTopicIds, 0, $slaveNum);
- if (empty($slaveTopicIds)) continue;
- $largeTopicId = self::insertLargeTopic($schDbh, $slaveTopicIds, array(
- 'title' => $topic['content'],
- 'analysis' => $topic['parse'],
- 'answer_type' => $answerType,
- 'word_id' => $word['word_id'],
- 'exam_group_id' => $word['exam_group_id'],
- 'difficulty' => $difficulty,
- 'bank_type' => $topic['topic_type_id'],
- 'type_id' => $basicTypeId
- ));
- // 更新小题试题内容
- foreach ($topic['slave'] as $skey => $slaveTopic) {
- if (!isset($slaveTopicIds[$skey])) continue;
- $tempSlaveTopicId = $slaveTopicIds[$skey];
- $parse = isset($slaveTopic['parse']) ? $slaveTopic['parse'] : '';
- $slaveAnswerType = isset($slaveTopic['answer_type']) ? (int)$slaveTopic['answer_type'] : 0;
- $slaveTitle = isset($slaveTopic['content']) ? $slaveTopic['content'] : '';
- $slaveOption = isset($slaveTopic['options']) && $slaveTopic['options'] ? $slaveTopic['options'] : '';
- // 特殊处理七选五等题型选项问题
- if ($basicTypeId == 2 && empty($slaveOption)) {
- if (isset($topicTypeConfig[$topic['topic_type_id']]['config']['general_setting']['slave_setting'])) {
- $slaveConfig = $topicTypeConfig[$topic['topic_type_id']]['config']['general_setting']['slave_setting'];
- $optionFixation = isset($slaveConfig['option_number']['fixation'])?$slaveConfig['option_number']['fixation']:array();
- if (isset($optionFixation['is_checked']) && $optionFixation['is_checked'] && isset( $optionFixation['number']) && $optionFixation['number']) {
- $letter = 'A';
- for ($i=0; $i<$optionFixation['number']; $i++){
- $slaveOption[] = array('option' => $letter, 'content' => '');
- ++$letter;
- }
- }
- }
- }
- self::updateTopicInfo($schDbh, array(
- 'topic_id' => $tempSlaveTopicId,
- 'parse' => $parse,
- 'answer_type' => $slaveAnswerType,
- 'title' => $slaveTitle,
- 'difficulty' => $difficulty,
- 'bank_type' => $topic['topic_type_id'],
- 'type_id' => $basicTypeId,
- 'options' => $slaveOption,
- 'answer' => isset($slaveTopic['answer']) ? $slaveTopic['answer'] : array()
- ));
- }
- } else {
- $spliceTopicIds = array_splice($stemTopicIds, 0, 1);
- if (!isset($spliceTopicIds[0])) continue;
- $topicId = $spliceTopicIds[0];
- self::updateTopicInfo($schDbh, array(
- 'topic_id' => $topicId,
- 'parse' => isset($topic['parse']) ? $topic['parse'] : '',
- 'answer_type' => $answerType,
- 'title' => isset($topic['content']) ? $topic['content'] : '',
- 'difficulty' => $difficulty,
- 'bank_type' => $topic['topic_type_id'],
- 'type_id' => $basicTypeId,
- 'options' => isset($topic['options']) && $topic['options'] ? $topic['options'] : '',
- 'answer' => isset($topic['answer']) ? $topic['answer'] : array()
- ));
- // 大题对应大题id更新
- $schDbh->prepare("UPDATE `paper_topic_relation` SET `logic_type` = {$topic['topic_type_id']} WHERE `topic_id` = '{$topicId}'")->execute();
- }
- //$schDbh->commit();
- } catch (PDOException $e) {
- //$schDbh->rollback();
- $exeReason = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,parse_time=:time WHERE `word_id` =:wid');
- $exeReason->execute(array(':wid' => $word['word_id'], ':reason' => $e->getMessage(), ':time' => time()));
- exit($e->getMessage());
- }
- }
- // 获取试卷的大题信息
- $paperLargeTopic = $schDbh->query('SELECT `stem_id`,`topic_id`,`logic_type`,`order` FROM `paper_topic_relation` pt WHERE pt.`paper_id`="'.$paperId.'" order by pt.order asc')->fetchAll(PDO::FETCH_ASSOC);
- if ($paperLargeTopic) {
- $largerRelate = array();
- $largeTopicInfo = array();
- $taskCheckTopics = array();
- $paperLargeTopicIds = array();
- foreach ($paperLargeTopic as $ptlInfo) {
- $paperLargeTopicIds[] = $ptlInfo['stem_id'];
- $largerRelate[$ptlInfo['topic_id']] = $ptlInfo['stem_id'];
- if (!isset($largeTopicInfo[$ptlInfo['stem_id']])) {
- $largeTopicInfo[$ptlInfo['stem_id']] = array(
- 'stem_id' => $ptlInfo['stem_id'],
- 'logic_type' => $ptlInfo['logic_type'],
- 'order' => $ptlInfo['order']
- );
- $taskCheckTopics[] = array('topic_id' => $ptlInfo['stem_id'], 'topic_type_id' => $ptlInfo['logic_type']);
- }
- }
- // 如果是全学科题库标注任务推送给题库
- $zsyapiParam = Yii::app()->params['api'][1];
- $zsyapiParam['prefix'] .= 'task_exam/check_topic';
- self::getApiData($zsyapiParam, 2, json_encode(array('examGroupId' => $word['exam_group_id'], 'topics' => $taskCheckTopics)), 5);
- // 更新试卷大题关联表
- $examInfo = array();
- $paperLargeSql = 'INSERT INTO `paper_topic_large` (`paper_id`, `large_topic_id`, `topic_no`, `logic_type`) VALUES ';
- $papers = $schDbh->query('select p.paper_id,e.exam_id,e.tpl_data from exam e left JOIN paper p on e.exam_id = p.exam_id where e.exam_group_id = ' . $word['exam_group_id'])->fetchAll(PDO::FETCH_ASSOC);
- foreach ($papers as $paper) {
- $examInfo[] = array('exam_id' => $paper['exam_id'], 'tpl_data' => $paper['tpl_data']);
- $exsitNum = $schDbh->query("SELECT count(*) as num FROM `paper_topic_large` WHERE `paper_id`='{$paper['paper_id']}'")->fetch(PDO::FETCH_ASSOC);
- if ($exsitNum['num'] > 0) continue;
- $paperLargeVal = array();
- foreach ($largeTopicInfo as $plTopic) {
- $paperLargeVal[] = "('{$paper['paper_id']}', '{$plTopic['stem_id']}', '{$plTopic['order']}', '{$plTopic['logic_type']}')";
- }
- $schDbh->prepare($paperLargeSql . implode(',', $paperLargeVal))->execute();
- }
- // 更新tpl_data new_items大小题关系及题型
- $packTopicBasic = array();
- $paperLargeTopicIdStr = implode(',', array_unique($paperLargeTopicIds));
- $topicBasic = $schDbh->query('SELECT `topic_id`,`topic_type`,`bank_type` FROM `topic` WHERE `topic_id` in ('.$paperLargeTopicIdStr.')')->fetchAll(PDO::FETCH_ASSOC);
- foreach ($topicBasic as $loopBasic) {
- $packTopicBasic[$loopBasic['topic_id']] = $loopBasic;
- }
- foreach ($examInfo as $tempExam) {
- $tplArr = json_decode($tempExam['tpl_data'], true);
- if ($tplArr && isset($tplArr['new_items']) && $tplArr['new_items']) {
- foreach ($tplArr['new_items'] as &$loopNewItem) {
- if (isset($loopNewItem['topicId'])) {
- $tplTopicId = (string)$loopNewItem['topicId'];
- if (isset($largerRelate[$tplTopicId]) && $largerRelate[$tplTopicId] != $loopNewItem['stemId']) {
- $loopNewItem['stemAlias'] = '';
- $loopNewItem['stemId'] = $largerRelate[$tplTopicId];
- }
- }
- if (isset($loopNewItem['stemId'])) {
- $tplStemId = (string)$loopNewItem['stemId'];
- if (isset($packTopicBasic[$loopNewItem['stemId']])) {
- $loopNewItem['topic_type'] = $packTopicBasic[$loopNewItem['stemId']]['bank_type'];
- // $loopNewItem['basicTopicType'] = $packTopicBasic[$loopNewItem['stemId']]['topic_type'];
- }
- }
- }
- $tplData = self::jsonEncode($tplArr);
- $schDbh->prepare("UPDATE `exam` SET `tpl_data` ='{$tplData}' WHERE `exam_id` ='{$tempExam['exam_id']}'")->execute();
- }
- }
- // 删除试题缓存
- self::getApiData(array(
- 'username' => $apiParam['username'],
- 'password' => $apiParam['password'],
- 'prefix' => $apiParam['prefix'] . 'topic_clear_redis/ctopic'
- ), 2, json_encode(array('topicIds' => array_keys($largeTopicInfo))), 3);
- //调用试题检索接口
- $this->searchTopics(array_keys($largeTopicInfo),$schoolId);
- }
- // 组卷完成标识
- $resu = $schDbh->prepare('UPDATE `exam_group` SET `is_answersheet` = 1 WHERE `exam_group_id` =:exam_group_id');
- $exeSheet = $resu->execute(array(':exam_group_id' => $word['exam_group_id']));
- if (!$exeSheet) {
- $stmt = $schDbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,parse_time=:time WHERE `word_id` =:wid');
- $stmt->execute(array(':wid' => $word['word_id'], ':reason' => '更新exam_group表失败!', ':time' => time()));
- exit('更新exam_group表失败!');
- }
- }
- // 更新试题内容
- private static function updateTopicInfo($schDbh, $topic)
- {
- // topic
- $exeTopic = $schDbh->prepare('UPDATE `topic` SET `topic_title` = :topic_title,`topic_difficulty` = :topic_difficulty,`source_title`=:source_title,`bank_type`=:bank_type,`parse_content`=:parse_content,`update_time`=:update_time,`answer_type`=:answer_type,`topic_type`=:topic_type WHERE `topic_id` =:topic_id');
- $exeTopic->execute(array(':topic_id' => $topic['topic_id'], ':topic_title' => $topic['title'],':topic_difficulty'=>$topic['difficulty'],':bank_type'=>$topic['bank_type'], ':source_title' => 'word上传试题', ':parse_content' => $topic['parse'], ':update_time' => time(), ':answer_type'=>$topic['answer_type'], ':topic_type'=>$topic['type_id']));
- $slaItem = $schDbh->prepare('UPDATE `topic_item` SET `topic_title` = :topic_title WHERE `topic_id` =:topic_id');
- $slaItem->execute(array(':topic_id' => $topic['topic_id'], ':topic_title' => $topic['title']));
- // topic_item_option
- if (isset($topic['options']) && $topic['options']) { // 含有选项的试题
- $slaOption = $schDbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
- $slaOption->execute(array(':topic_id' => $topic['topic_id']));
- $tempAnswer = isset($topic['answer']) ? $topic['answer'] : array();
- $rowThr = $schDbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
- foreach ($topic['options'] as $svk => $seOption) {
- $isslaTrue = in_array($seOption['option'], $tempAnswer) ? 1 : 0;
- $rowThr->execute(array(':tpid'=>$topic['topic_id'],':content'=>$seOption['content'],':correct'=>$isslaTrue,':sort_order'=>$svk));
- }
- } elseif (isset($topic['answer']) && $topic['answer']) {
- $slaOption = $schDbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
- $slaOption->execute(array(':topic_id' => $topic['topic_id']));
- $slaFiv = $schDbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
- foreach ($topic['answer'] as $vk => $slaAnswer) {
- $slaFiv->execute(array(':tpid'=>$topic['topic_id'],':content'=>$slaAnswer));
- }
- }
- return true;
- }
- // 新增大题并更新相关信息
- private static function insertLargeTopic($schDbh, $slaveTopic, $topic)
- {
- // 获取文件夹id
- $defaultFolder = $schDbh->query('SELECT `folder_id` FROM `topic_folder` WHERE `subject_id`=8 and `is_system`=1')->fetch(PDO::FETCH_ASSOC);
- $folderId = empty($defaultFolder) ? 0 : $defaultFolder['folder_id'];
- // 获取组卷教师id
- $groupInfo = $schDbh->query("SELECT `teacher_id` FROM `exam_group` WHERE `exam_group_id`='{$topic['exam_group_id']}'")->fetch(PDO::FETCH_ASSOC);
- $teacherId = empty($groupInfo) ? 0 : $groupInfo['teacher_id'];
- // 创建大题
- $largeRow = $schDbh->prepare('INSERT INTO `topic`(`topic_type`, `topic_difficulty`, `folder_id`, `subject_id`,`source_title`, `parse_content`, `creator_id`,`updater_id`, `create_time`,`update_time`,`is_word_topic`,`answer_type`,`bank_type`) VALUES (:ttype,:diff,:fid,:sid,:sotitle,:ana,:ceid,:upid,:time,:utime,1,:answer_type,:bank_type)');
- $largeRow->execute(array(':ttype' => $topic['type_id'], ':diff' => $topic['difficulty'], ':fid' => $folderId, ':sid' => 8, ':sotitle' => 'word上传试题', ':ana' => $topic['analysis'], ':ceid' => $teacherId, ':upid' => $teacherId, ':time' => time(), ':utime' => time(), ':answer_type'=>$topic['answer_type'], ':bank_type'=>$topic['bank_type']));
-
- $largeTopicId = $schDbh->lastInsertId();
- if (!$largeTopicId) {
- throw new PDOException('新增大题信息异常!');
- }
-
- // 更新大题相关表
- $tempSlaveTopicStr = implode(',', $slaveTopic);
- $tRelate = $schDbh->prepare("UPDATE `topic` SET `stem_id` = {$largeTopicId} WHERE `topic_id` in ({$tempSlaveTopicStr})");
- $tRelate->execute();
- $ptRelate = $schDbh->prepare("UPDATE `paper_topic_relation` SET `stem_id`={$largeTopicId},`logic_type`={$topic['bank_type']} WHERE `topic_id` in ({$tempSlaveTopicStr})");
- $ptRelate->execute();
- // 插入解析时间记录
- $tsInsert = $schDbh->prepare("INSERT INTO `topic_stem` (`stem_id`, `topic_ids`, `stem_content`, `subject_id`) VALUES (:stem_id, :topic_ids, :stem_content, :subject_id)");
- $tsInsert->execute(array(':stem_id' => $largeTopicId, ':topic_ids' => $tempSlaveTopicStr, ':stem_content' => $topic['title'], ':subject_id' => 8));
- // word与试题关系
- $rowFor = $schDbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
- $rowFor->execute(array(':wid' => $topic['word_id'], ':toid' => $largeTopicId));
- return true;
- }
- private static function getApiData($apiParam, $flag = 1, $data, $timeOut = 0,$extraHeaders = array())
- {
- $result = FALSE;
- $ch = @curl_init();
- if ($ch) {
- $headers = array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data),
- );
- if($extraHeaders){
- $headers = array_merge($headers,$extraHeaders);
- }
- // Digest认证
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($ch, CURLOPT_USERPWD, $apiParam['username'] . ':' . $apiParam['password']);
- // 不输出头部
- curl_setopt($ch, CURLOPT_HEADER, 0);
- if ($timeOut) {
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- // curl_exec 获取到的内容不直接输出, 而是返回
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // 请求重启路由器的地址 传参 进行重启
- curl_setopt($ch, CURLOPT_URL, $apiParam['prefix']);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
- if ($flag == 2) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- $result = curl_exec($ch);
- if (curl_errno($ch)) {
- return 'curl_errno:' . curl_errno($ch);
- }
- // 释放资源
- curl_close($ch);
- }
- return $result;
- }
- // json中文处理
- private static function jsonEncode($arr)
- {
- $jsonSupport = version_compare(PHP_VERSION, '5.4', '>=');
- if ($jsonSupport) {
- $result = json_encode($arr, JSON_UNESCAPED_UNICODE);
- } else {
- $result = json_encode($arr);
- $result = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $result);
- }
- return $result;
- }
-
- //全学科试题调用java检索缓存接口
- public function searchTopics($topicIds = array(),$schoolId = 0){
- $apiParam = Yii::app()->params['api'][3];
- if($apiParam && $topicIds && $schoolId){
- $url = $apiParam['prefix'].'cgi/teacher/schtk/qxk/school-topic-flush/'.$schoolId;
- self::getApiData(array(
- 'username' => $apiParam['username'],
- 'password' => $apiParam['password'],
- 'prefix' => $url
- ), 2, json_encode($topicIds), 3,array('X-Basic-V:2'));
- }
- }
- }
|