WordapiController.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. /**
  4. * word试题导入接口控制器类
  5. * @author jiangfei
  6. * @date 2016-03-12 10:30:00
  7. * @company 上海风车教育有限公司.
  8. */
  9. class WordapiController extends CController
  10. {
  11. public $ucloud = '';
  12. public $webSiteUrl = '';
  13. public $charToNum = array('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5);
  14. public $flag = 1; // 解析返回的图片是否上传ucloud 0否 1是
  15. public $mathSubject = array(3,6,51);
  16. public function init()
  17. {
  18. @ini_set('memory_limit', '512M');
  19. set_time_limit(0);
  20. //$this->ucloud = new Ucloud();
  21. $this->ucloud = new Qcloud();
  22. $this->webSiteUrl = Yii::app()->params['teacherSiteurl'];
  23. $this->flag = isset($_GET['flag']) ? intval($_GET['flag']) : 1;
  24. }
  25. public function actionIndex()
  26. {
  27. // 获取curl过来值
  28. $word_id = isset($_GET['wid']) ? intval($_GET['wid']) : 0;
  29. $school_id = isset($_GET['sid']) ? intval($_GET['sid']) : 0;
  30. if (empty($word_id) || empty($school_id)) {
  31. exit('未找到word Id或者学校id');
  32. }
  33. // 连接业务库
  34. // $busDsn = 'mysql:host=' . Yii::app()->params["default_server"]['addr'] . ';dbname=' . Yii::app()->params["default_db"]['name'] . ';';
  35. // $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";'));
  36. $busdbh = Yii::app()->businessDb;
  37. $dataBaseInfo = $busdbh->createCommand('SELECT * FROM `database` WHERE `school_id`=' . $school_id)->queryOne();
  38. if (empty($dataBaseInfo)) {
  39. exit('未找到数据库链接信息!');
  40. }
  41. $busdbh = null;
  42. // 连接学校库
  43. $schDsn = 'mysql:host=' . $dataBaseInfo['database_host'] . ';dbname=' . $dataBaseInfo['database_name'] . ';';
  44. $schdbh = new PDO($schDsn, $dataBaseInfo['database_user'], $dataBaseInfo['database_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  45. $wordInfo = $schdbh->query('SELECT `word_id`,`word_name`,`subject_id`,`uploader_id` FROM `topic_word` WHERE `word_id`=' . $word_id)->fetch(PDO::FETCH_ASSOC);
  46. if (empty($wordInfo)) {
  47. exit('未找到上传word信息!');
  48. }
  49. $getWordJson = file_get_contents('php://input');
  50. $jsonArray = json_decode($getWordJson, true);
  51. if (!$jsonArray || $jsonArray['errcode'] > 0) { // 解析失败
  52. $docUrl = isset($jsonArray['docurl']) ? $jsonArray['docurl'] : '';
  53. $word_down_url = self::downloadWord($wordInfo, $docUrl, $school_id);
  54. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,`file_path` = :path,parse_time=:time WHERE `word_id` =:wid');
  55. $stmt->execute(array(':wid' => $word_id, ':reason' => $jsonArray['errmsg'], ':content' => $getWordJson, ':path' => $word_down_url, ':time' => time()));
  56. exit('解析失败!');
  57. } else {
  58. if (empty($jsonArray['items'])) {
  59. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  60. $stmt->execute(array(':wid' => $word_id, ':reason' => '返回word试题内容为空', ':content' => $getWordJson, ':time' => time()));
  61. exit('返回试题内容为空');
  62. } else {
  63. $sctmt = $schdbh->prepare('UPDATE `topic_word` SET `content` = :content,parse_time=:time WHERE `word_id` =:wid');
  64. $sctmt->execute(array(':wid' => $word_id, ':time' => time(), ':content' => $getWordJson));
  65. }
  66. // 试题入库操作
  67. foreach ($jsonArray['items'] as $key => $val) {
  68. // 试题类型ID
  69. switch ($val['type']) {
  70. case '选择':
  71. $type_id = 1;
  72. break;
  73. case '多选':
  74. $type_id = 2;
  75. break;
  76. case '填空':
  77. $type_id = 5;
  78. break;
  79. default:
  80. $type_id = 7;
  81. }
  82. //处理答案
  83. if (!empty($val['key'])) {
  84. $val['key'] = self::strip_content_tags($val['key']);
  85. $val['key'] = self::downloadImg($val['key'], $school_id);
  86. }
  87. // 处理题干中
  88. if (empty($val['stem'])) {
  89. continue;
  90. } else {
  91. $val['stem'] = self::strip_content_tags($val['stem']);
  92. $val['stem'] = self::downloadImg($val['stem'], $school_id);
  93. // 若是填空题下划线处理
  94. if ($val['type'] == '填空') {
  95. preg_match_all('/_{5,}/', $val['stem'], $matgap);
  96. if (!empty($matgap[0])) {
  97. $nnum = 0;
  98. foreach ($matgap[0] as $km => $vm) {
  99. $nnum = $km + 1;
  100. $val['stem'] = preg_replace('/_{3,}/', '<img class="tiankong" src="/images/list_' . $nnum . '.png">', $val['stem'], 1);
  101. }
  102. // 填空题是否多个答案
  103. preg_match_all('/<p>(.*?)<\/p>/', $val['key'], $key_array);
  104. if ($nnum > 1 && $key_array[1]) {
  105. $result_array = array_filter($key_array[1]);
  106. if ($result_array) {
  107. if (count($result_array) == $nnum) { // 如果填空数与答案数匹配
  108. $val['key'] = $key_array[1];
  109. } else { // 出现填空数与答案数不一致情况处理
  110. $new_array = array();
  111. $new_nnum = $nnum - 1;
  112. for ($t = 0; $t <= $new_nnum; $t++) {
  113. if (isset($result_array[$t])) {
  114. $new_array[] = $result_array[$t];
  115. } else {
  116. $new_array[] = '';
  117. }
  118. }
  119. $val['key'] = $new_array;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. // 试题解析
  127. if (!empty($val['analysis'])) {
  128. $val['analysis'] = self::downloadImg($val['analysis'], $school_id);
  129. $val['analysis'] = self::strip_content_tags($val['analysis']);
  130. }
  131. // 单选题处理
  132. if (!empty($val['options'])) {
  133. foreach ($val['options'] as $vk => &$vp) {
  134. $vp = self::downloadImg($vp, $school_id);
  135. $vp = self::strip_content_tags($vp);
  136. // $val['options'][$vk] = $vp;
  137. }
  138. }
  139. // 获取默认文件夹id
  140. $subjectId = in_array($wordInfo['subject_id'],$this->mathSubject) ? 3 : $wordInfo['subject_id'];
  141. $defaultFolder = $schdbh->query('SELECT `folder_id` FROM `topic_folder` WHERE `subject_id`='.$subjectId.' and `is_system`=1')->fetch(PDO::FETCH_ASSOC);
  142. $folderId = empty($defaultFolder) ? 0 : $defaultFolder['folder_id'];
  143. // 获取试题id
  144. //$getUid = $schdbh->query('SELECT UUID_SHORT() AS uuid')->fetch(PDO::FETCH_ASSOC);
  145. //$resUid = substr($getUid['uuid'],-6);
  146. $now_time = time();
  147. $difficulty = isset($val['difficulty']) ? $val['difficulty'] : 1;
  148. // 试题入库处理
  149. try {
  150. $schdbh->beginTransaction(); // 开启事务
  151. // topic表
  152. $row = null;
  153. $row = $schdbh->prepare('INSERT INTO `topic`(`topic_type`, `topic_title`, `topic_difficulty`, `folder_id`, `subject_id`,`source_title`, `parse_content`, `creator_id`,`updater_id`, `create_time`,`update_time`,`is_word_topic`) VALUES (:ttype,:ttile,:tdiff,:fid,:sid,:sotitle,:ana,:ceid,:upid,:time,:utime,1)'); // 执行第一个 SQL
  154. $row->execute(array(':ttype' => $type_id, ':ttile' => $val['stem'], ':tdiff' => $difficulty, ':fid' => $folderId, ':sid' => $wordInfo['subject_id'], ':sotitle' => 'word上传试题', ':ana' => $val['analysis'], ':ceid' => $wordInfo['uploader_id'], ':upid' => $wordInfo['uploader_id'], ':time' => $now_time, ':utime' => $now_time));
  155. $resUid = $schdbh->lastInsertId();
  156. if (!$resUid) {
  157. throw new PDOException('插入topic表失败!');
  158. }
  159. // topic_item
  160. $rowTwo = null;
  161. $rowTwo = $schdbh->prepare('INSERT INTO `topic_item`(`topic_id`, `topic_type`, `topic_title`) VALUES (:tpid,:type,:title)');
  162. $getRowTwo = $rowTwo->execute(array(':tpid' => $resUid, ':type' => $type_id, ':title' => $val['stem']));
  163. if (!$getRowTwo) {
  164. throw new PDOException('插入topic_item表失败!');
  165. }
  166. // 单选题处理
  167. if ($type_id == 1 || $type_id == 2) {
  168. // 获取正确答案
  169. $answerNums = array();
  170. $getAnswer = str_replace(array(" ", " ", "\t", "\n", "\r"), '', strip_tags($val['key']));
  171. $getAnswer = strtoupper($getAnswer);
  172. foreach ($this->charToNum as $char => $num) {
  173. if (strpos($getAnswer, $char) !== false) {
  174. $answerNums[] = $num;
  175. }
  176. }
  177. $rowThr = null;
  178. $rowThr = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`) VALUES (:tpid,:content,:correct)');
  179. foreach ($val['options'] as $vk => $option) {
  180. $is_true = 0;
  181. if (in_array($vk, $answerNums)) {
  182. $is_true = 1;
  183. }
  184. $getRowThr = $rowThr->execute(array(':tpid' => $resUid, ':content' => $option, ':correct' => $is_true));
  185. }
  186. } else {
  187. $rowFiv = null;
  188. $rowFiv = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
  189. if (is_array($val['key']) && $val['key']) {
  190. foreach ($val['key'] as $topicKey) {
  191. $getRowFiv = $rowFiv->execute(array(':tpid' => $resUid, ':content' => $topicKey));
  192. }
  193. } else {
  194. $getRowFiv = $rowFiv->execute(array(':tpid' => $resUid, ':content' => $val['key']));
  195. if (!$getRowFiv) {
  196. throw new PDOException('插入topic_item_option表失败!');
  197. }
  198. }
  199. }
  200. // word与试题关系
  201. $rowFor = null;
  202. $rowFor = $schdbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
  203. $getRowFor = $rowFor->execute(array(':wid' => $word_id, ':toid' => $resUid));
  204. if (!$getRowFor) {
  205. throw new PDOException('插入word_to_topic表失败!');
  206. }
  207. $schdbh->commit();
  208. } catch (PDOException $e) {
  209. $schdbh->rollback(); // 执行失败,事务回滚
  210. exit($e->getMessage());
  211. }
  212. }
  213. // 处理完
  214. $resu = $schdbh->prepare('UPDATE `topic_word` SET `status` = 2,`parse_time`=:time WHERE `word_id` =:wid');
  215. $resu->execute(array(':wid' => $word_id, ':time' => time()));
  216. $schdbh = null;
  217. exit('解析完成!');
  218. }
  219. }
  220. public function actionCoach()
  221. {
  222. $apiParam = Yii::app()->params['api'][0];
  223. $apiParam['prefix'] .= 'topic_clear_redis/ctopic';
  224. $apiTopics = array();
  225. // 获取curl过来值
  226. $word_id = isset($_GET['wid']) ? intval($_GET['wid']) : 0;
  227. $school_id = isset($_GET['sid']) ? intval($_GET['sid']) : 0;
  228. if (empty($word_id) || empty($school_id)) {
  229. exit('未找到word Id或者学校id');
  230. }
  231. // 连接业务库
  232. $busDsn = 'mysql:host=' . Yii::app()->params["default_server"]['addr'] . ';dbname=' . Yii::app()->params["default_db"]['name'] . ';';
  233. $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";'));
  234. $getDataBase = $busdbh->query('SELECT * FROM `database` WHERE `school_id`=' . $school_id);
  235. $dataBaseInfo = $getDataBase->fetch(PDO::FETCH_ASSOC);
  236. if (empty($dataBaseInfo)) {
  237. exit('未找到数据库链接信息!');
  238. }
  239. $busdbh = null;
  240. $isQxk = 0;
  241. // 连接学校库
  242. $schDsn = 'mysql:host=' . $dataBaseInfo['database_host'] . ';dbname=' . $dataBaseInfo['database_name'] . ';';
  243. $schdbh = new PDO($schDsn, $dataBaseInfo['database_user'], $dataBaseInfo['database_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  244. $wordInfo = $schdbh->query('SELECT `word_id`,`word_name`,`subject_id`,`uploader_id`,`exam_group_id` FROM `topic_word` WHERE `word_id`=' . $word_id)->fetch(PDO::FETCH_ASSOC);
  245. if (empty($wordInfo)) {
  246. exit('未找到上传word信息!');
  247. }
  248. $examGroupInfo = $schdbh->query('SELECT `qxk_paper_id` FROM `exam_group` WHERE `exam_group_id`=' . $wordInfo['exam_group_id'])->fetch(PDO::FETCH_ASSOC);
  249. if($examGroupInfo && $examGroupInfo['qxk_paper_id']){
  250. $isQxk = 1;
  251. }
  252. $paper_topic_relation_data = $schdbh->query('select pt.*,pp.* from paper_topic_relation pt join (select p.paper_id ,e.subject_id as subject_id,e.exam_group_id from exam e left JOIN paper p on e.exam_id = p.exam_id where e.exam_group_id = ' . $wordInfo['exam_group_id'] . ' GROUP BY e.exam_group_id) as pp on pt.paper_id = pp.paper_id order by pt.type asc,pt.order asc')->fetchAll(PDO::FETCH_ASSOC);
  253. if (empty($paper_topic_relation_data)) {
  254. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  255. $stmt->execute(array(':wid' => $word_id, ':reason' => '此考试无题', ':content' => '', ':time' => time()));
  256. }
  257. $relate_topic_id = array();
  258. $p_topic_type_num = array();
  259. foreach ($paper_topic_relation_data as $v) {
  260. $relate_topic_id[] = $v['topic_id'];
  261. $p_topic_type_num[$v['type']][] = $v['topic_id'];
  262. }
  263. $getWordJson = file_get_contents('php://input');
  264. $jsonArray = json_decode($getWordJson, true);
  265. if (!$jsonArray || $jsonArray['errcode'] > 0) { // 解析失败
  266. $docUrl = isset($jsonArray['docurl']) ? $jsonArray['docurl'] : '';
  267. $word_down_url = self::downloadWord($wordInfo, $docUrl, $school_id);
  268. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,`file_path` = :path,parse_time=:time WHERE `word_id` =:wid');
  269. $stmt->execute(array(':wid' => $word_id, ':reason' => $jsonArray['errmsg'], ':content' => $getWordJson, ':path' => $word_down_url, ':time' => time()));
  270. exit('解析失败!');
  271. } else {
  272. if (empty($jsonArray['items'])) {
  273. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  274. $stmt->execute(array(':wid' => $word_id, ':reason' => '返回word试题内容为空', ':content' => $getWordJson, ':time' => time()));
  275. exit('返回试题内容为空');
  276. } else {
  277. $sctmt = $schdbh->prepare('UPDATE `topic_word` SET `content` = :content,parse_time=:time WHERE `word_id` =:wid');
  278. $sctmt->execute(array(':wid' => $word_id, ':time' => time(), ':content' => $getWordJson));
  279. }
  280. if (count($paper_topic_relation_data) != count($jsonArray['items'])) {
  281. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  282. $stmt->execute(array(':wid' => $word_id, ':reason' => 'WORD题量与试卷题量不同', ':content' => $getWordJson, ':time' => time()));
  283. exit('WORD题量与试卷题量不同');
  284. }
  285. // 删除已存在试题缓存
  286. if ($relate_topic_id) {
  287. self::getApiData($apiParam, 2, json_encode(array('topicIds' => $relate_topic_id)), 3);
  288. }
  289. $topic_type_num = array();
  290. foreach ($jsonArray['items'] as $key => $val) {
  291. // 试题类型ID
  292. $type_id = 0;
  293. if ($val['type'] == '选择') {
  294. $type_id = 1;
  295. $topic_type_num[$type_id][] = $type_id;
  296. } else if ($val['type'] == '多选') {
  297. $type_id = 2;
  298. $topic_type_num[$type_id][] = $type_id;
  299. } else if ($val['type'] == '填空') {
  300. $type_id = 5;
  301. $topic_type_num[$type_id][] = $type_id;
  302. } else if ($val['type'] == '简答') {
  303. if (isset($val['is_optional']) && $val['is_optional']) {
  304. $topic_type_num[17][] = 7;
  305. } else {
  306. $type_id = 7;
  307. $topic_type_num[$type_id][] = $type_id;
  308. }
  309. }
  310. }
  311. //验证题型题量
  312. if ($topic_type_num && $p_topic_type_num) {
  313. if (isset($topic_type_num[1]) && isset($p_topic_type_num[1])) {
  314. if (count($topic_type_num[1]) != count($p_topic_type_num[1])) {
  315. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  316. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word单选题量与试卷的单选题量不同', ':content' => $getWordJson, ':time' => time()));
  317. exit('word单选题量与试卷的单选题量不同');
  318. }
  319. } elseif (isset($p_topic_type_num[1]) && !isset($topic_type_num[1])) {
  320. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  321. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word单选题量与试卷的单选题量不同', ':content' => $getWordJson, ':time' => time()));
  322. exit('word单选题量与试卷的单选题量不同');
  323. }
  324. if (isset($topic_type_num[2]) && isset($p_topic_type_num[2])) {
  325. if (count($topic_type_num[2]) != count($p_topic_type_num[2])) {
  326. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  327. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word多选题量与试卷的多选题量不同', ':content' => $getWordJson, ':time' => time()));
  328. exit('word多选题量与试卷的多选题量不同');
  329. }
  330. } elseif (isset($p_topic_type_num[2]) && !isset($topic_type_num[2])) {
  331. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  332. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word多选题量与试卷的多选题量不同', ':content' => $getWordJson, ':time' => time()));
  333. exit('word多选题量与试卷的多选题量不同');
  334. }
  335. if (isset($topic_type_num[5]) && isset($p_topic_type_num[5])) {
  336. if (count($topic_type_num[5]) != count($p_topic_type_num[5])) {
  337. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  338. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word填空题量与试卷的填空题量不同', ':content' => $getWordJson, ':time' => time()));
  339. exit('word填空题量与试卷的填空题量不同');
  340. }
  341. } elseif (isset($p_topic_type_num[5]) && !isset($topic_type_num[5])) {
  342. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  343. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word填空题量与试卷的填空题量不同', ':content' => $getWordJson, ':time' => time()));
  344. exit('word填空题量与试卷的填空题量不同');
  345. }
  346. if (isset($topic_type_num[7]) && isset($p_topic_type_num[7])) {
  347. if (count($topic_type_num[7]) != count($p_topic_type_num[7])) {
  348. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  349. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word解答题量与试卷的解答题量不同', ':content' => $getWordJson, ':time' => time()));
  350. exit('word解答题量与试卷的解答题量不同');
  351. }
  352. } elseif (isset($p_topic_type_num[7]) && !isset($topic_type_num[7])) {
  353. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  354. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word解答题量与试卷的解答题量不同', ':content' => $getWordJson, ':time' => time()));
  355. exit('word解答题量与试卷的解答题量不同');
  356. }
  357. if (isset($topic_type_num[17]) && isset($p_topic_type_num[17])) {
  358. if (count($topic_type_num[17]) != count($p_topic_type_num[17])) {
  359. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  360. $stmt->execute(array(':wid' => $word_id, ':reason' => 'word选做题量与试卷的选做题量不同', ':content' => $getWordJson, ':time' => time()));
  361. exit('word选做题量与试卷的选做题量不同');
  362. }
  363. } elseif (isset($p_topic_type_num[17]) && !isset($topic_type_num[17])) {
  364. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  365. $stmt->execute(array(':wid' => $word_id, ':reason' => '上传Word需包含选做题', ':content' => $getWordJson, ':time' => time()));
  366. exit('上传Word需包含选做题');
  367. }
  368. }
  369. // 试题入库操作
  370. foreach ($jsonArray['items'] as $key => $val) {
  371. $apiTopics[] = $paper_topic_relation_data[$key]['topic_id'];
  372. // 试题类型ID
  373. switch ($val['type']) {
  374. case '选择':
  375. $type_id = 1;
  376. break;
  377. case '多选':
  378. $type_id = 2;
  379. break;
  380. case '填空':
  381. $type_id = 5;
  382. break;
  383. default:
  384. $type_id = 7;
  385. }
  386. //处理答案
  387. if (!empty($val['key'])) {
  388. $val['key'] = self::downloadImg($val['key'], $school_id);
  389. $val['key'] = self::strip_content_tags($val['key']);
  390. }
  391. // 处理题干中的公式
  392. if (empty($val['stem'])) {
  393. continue;
  394. } else {
  395. $val['stem'] = self::downloadImg($val['stem'], $school_id);
  396. $val['stem'] = self::strip_content_tags($val['stem']);
  397. // 若是填空题下划线处理
  398. if ($val['type'] == '填空') {
  399. preg_match_all('/_{5,}/', $val['stem'], $matgap);
  400. if (!empty($matgap[0])) {
  401. $nnum = 0;
  402. foreach ($matgap[0] as $km => $vm) {
  403. $nnum = $km + 1;
  404. $val['stem'] = preg_replace('/_{3,}/', '<img class="tiankong" src="/images/list_' . $nnum . '.png">', $val['stem'], 1);
  405. }
  406. // 填空题是否多个答案
  407. preg_match_all('/<p>(.*?)<\/p>/', $val['key'], $key_array);
  408. if ($nnum > 1 && $key_array[1]) {
  409. $result_array = array_filter($key_array[1]);
  410. if ($result_array) {
  411. if (count($result_array) == $nnum) { // 如果填空数与答案数匹配
  412. $val['key'] = $key_array[1];
  413. } else { // 出现填空数与答案数不一致情况处理
  414. $new_array = array();
  415. $new_nnum = $nnum - 1;
  416. for ($t = 0; $t <= $new_nnum; $t++) {
  417. if (isset($result_array[$t])) {
  418. $new_array[] = $result_array[$t];
  419. } else {
  420. $new_array[] = '';
  421. }
  422. }
  423. $val['key'] = $new_array;
  424. }
  425. }
  426. }
  427. }
  428. }
  429. }
  430. // 试题解析
  431. if (!empty($val['analysis'])) {
  432. $val['analysis'] = self::downloadImg($val['analysis'], $school_id);
  433. $val['analysis'] = self::strip_content_tags($val['analysis']);
  434. }
  435. // 单选题处理
  436. if (!empty($val['options'])) {
  437. foreach ($val['options'] as $vk => &$vp) {
  438. $vp = self::downloadImg($vp, $school_id);
  439. $vp = self::strip_content_tags($vp);
  440. // $val['options'][$vk] = $vp;
  441. }
  442. }
  443. // 获取默认文件夹id
  444. $defaultFolder = $schdbh->query('SELECT `folder_id` FROM `topic_folder` WHERE `is_system`=1')->fetch(PDO::FETCH_ASSOC);
  445. $folderId = empty($defaultFolder['folder_id']) ? 0 : $defaultFolder['folder_id'];
  446. // 获取试题id
  447. //$getUid = $schdbh->query('SELECT UUID_SHORT() AS uuid')->fetch(PDO::FETCH_ASSOC);
  448. //$resUid = substr($getUid['uuid'],-6);
  449. // 设置默认难度
  450. $difficulty = isset($val['difficulty']) ? $val['difficulty'] : 1;
  451. // 试题入库处理
  452. try {
  453. $schdbh->beginTransaction(); // 开启事务
  454. $now_time = time();
  455. // topic表
  456. $row = null;
  457. $stmt = $schdbh->prepare('UPDATE `topic` SET `topic_title` = :topic_title,`topic_difficulty` = :topic_difficulty,`source_title`=:source_title,`parse_content`=:parse_content,`update_time`=:update_time,`is_word_topic`=:is_word_topic WHERE `topic_id` =:topic_id');
  458. $stmt->execute(array(':topic_id' => $paper_topic_relation_data[$key]['topic_id'], ':topic_title' => $val['stem'], ':topic_difficulty' => $difficulty, ':source_title' => 'word上传试题', ':parse_content' => $val['analysis'], ':update_time' => time(), ':is_word_topic' => 0));
  459. // topic_item
  460. $stmt = $schdbh->prepare('UPDATE `topic_item` SET `topic_title` = :topic_title WHERE `topic_id` =:topic_id');
  461. $stmt->execute(array(':topic_id' => $paper_topic_relation_data[$key]['topic_id'], ':topic_title' => $val['stem']));
  462. // 单选题处理
  463. if ($type_id == 1 || $type_id == 2) {
  464. // 获取正确答案
  465. $answerNums = array();
  466. $getAnswer = str_replace(array(" ", " ", "\t", "\n", "\r"), '', strip_tags($val['key']));
  467. $getAnswer = strtoupper($getAnswer);
  468. foreach ($this->charToNum as $char => $num) {
  469. if (strpos($getAnswer, $char) !== false) {
  470. $answerNums[] = $num;
  471. }
  472. }
  473. $topic_option_data = $schdbh->query('SELECT * FROM `topic_item_option` WHERE `topic_id`=' . $paper_topic_relation_data[$key]['topic_id'] . ' and option_correct = 1')->fetch(PDO::FETCH_ASSOC);
  474. if ($topic_option_data) {
  475. if (count($val['options']) < ($topic_option_data['sort_order'] + 1)) {
  476. throw new PDOException('此题选项未包含设置的答案项!');
  477. }
  478. $stmt = $schdbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
  479. $stmt->execute(array(':topic_id' => $paper_topic_relation_data[$key]['topic_id']));
  480. $rowThr = null;
  481. $rowThr = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
  482. foreach ($val['options'] as $vk => $option) {
  483. $is_true = 0;
  484. if ($topic_option_data) {
  485. if (in_array($vk, $answerNums)) {
  486. $is_true = 1;
  487. }
  488. }
  489. $getRowThr = $rowThr->execute(array(':tpid' => $paper_topic_relation_data[$key]['topic_id'], ':content' => $option, ':correct' => $is_true, ':sort_order' => $vk));
  490. }
  491. } else {
  492. if (count($val['options']) < $anserNum + 1) {
  493. throw new PDOException('此题选项未包含设置的答案项!');
  494. }
  495. $stmt = $schdbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
  496. $stmt->execute(array(':topic_id' => $paper_topic_relation_data[$key]['topic_id']));
  497. $rowFiv = null;
  498. $rowThr = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
  499. foreach ($val['options'] as $vk => $option) {
  500. $is_true = 0;
  501. if ($anserNum == $vk) {
  502. $is_true = 1;
  503. }
  504. $getRowThr = $rowThr->execute(array(':tpid' => $paper_topic_relation_data[$key]['topic_id'], ':content' => $option, ':correct' => $is_true, ':sort_order' => $vk));
  505. }
  506. }
  507. } else {
  508. $stmt = $schdbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
  509. $stmt->execute(array(':topic_id' => $paper_topic_relation_data[$key]['topic_id']));
  510. $rowFiv = null;
  511. $rowFiv = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
  512. if (is_array($val['key']) && $val['key']) {
  513. foreach ($val['key'] as $topicKey) {
  514. $getRowFiv = $rowFiv->execute(array(':tpid' => $paper_topic_relation_data[$key]['topic_id'], ':content' => $topicKey));
  515. }
  516. } else {
  517. $getRowFiv = $rowFiv->execute(array(':tpid' => $paper_topic_relation_data[$key]['topic_id'], ':content' => $val['key']));
  518. if (!$getRowFiv) {
  519. throw new PDOException('插入topic_item_option表失败!');
  520. }
  521. }
  522. }
  523. // word与试题关系
  524. $rowFor = null;
  525. $rowFor = $schdbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
  526. $getRowFor = $rowFor->execute(array(':wid' => $word_id, ':toid' => $paper_topic_relation_data[$key]['topic_id']));
  527. if (!$getRowFor) {
  528. throw new PDOException('插入word_to_topic表失败!');
  529. }
  530. $schdbh->commit();
  531. } catch (PDOException $e) {
  532. $schdbh->rollback(); // 执行失败,事务回滚
  533. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  534. $stmt->execute(array(':wid' => $word_id, ':reason' => $e->getMessage(), ':content' => $getWordJson, ':time' => time()));
  535. exit($e->getMessage());
  536. }
  537. }
  538. // 处理完
  539. $resu = $schdbh->prepare('UPDATE `topic_word` SET `status` = 2,`parse_time`=:time WHERE `word_id` =:wid');
  540. $resu->execute(array(':wid' => $word_id, ':time' => time()));
  541. $resu = $schdbh->prepare('UPDATE `exam_group` SET `is_answersheet` = 1 WHERE `exam_group_id` =:exam_group_id');
  542. $resu->execute(array(':exam_group_id' => $wordInfo['exam_group_id']));
  543. // 插入解析时间记录
  544. $resPro = $schdbh->prepare("INSERT INTO `exam_process` (`exam_group_id`, `action_type`, `action_time`) VALUES (:exam_group_id, :tempType, :time)");
  545. $resPro->execute(array(':exam_group_id' => $wordInfo['exam_group_id'], ':tempType' => 4, ':time' => time()));
  546. // 监控日志
  547. //$teachInfo = $schdbh->query('SELECT `teacher_name` FROM `teacher` WHERE `teacher_id`="'.$wordInfo['uploader_id'].'"')->fetch(PDO::FETCH_ASSOC);
  548. BMonitorLog::model()->addLog(array(
  549. 'school_id' => $school_id,
  550. 'teacher_id' => $wordInfo['uploader_id'],
  551. 'log_content' => array(
  552. '助教',
  553. '上传了',
  554. $wordInfo['word_name'],
  555. '试卷'
  556. )
  557. ));
  558. $schdbh = null;
  559. if($apiTopics && $isQxk){
  560. //调用试题检索接口
  561. $this->searchTopics($apiTopics,$school_id);
  562. }
  563. exit('解析完成!');
  564. }
  565. }
  566. /**
  567. * @param $string
  568. * @return mixed
  569. * 校本卷库word解析
  570. */
  571. public function actionXbpaper()
  572. {
  573. // 获取curl过来值
  574. $word_id = isset($_GET['wid']) ? intval($_GET['wid']) : 0;
  575. $school_id = isset($_GET['sid']) ? intval($_GET['sid']) : 0;
  576. if (empty($word_id) || empty($school_id)) {
  577. exit('未找到word Id或者学校id');
  578. }
  579. // 连接业务库
  580. $busDsn = 'mysql:host=' . Yii::app()->params["default_server"]['addr'] . ';dbname=' . Yii::app()->params["default_db"]['name'] . ';';
  581. $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";'));
  582. $getDataBase = $busdbh->query('SELECT * FROM `database` WHERE `school_id`=' . $school_id);
  583. $dataBaseInfo = $getDataBase->fetch(PDO::FETCH_ASSOC);
  584. if (empty($dataBaseInfo)) {
  585. exit('未找到数据库链接信息!');
  586. }
  587. $busdbh = null;
  588. // 连接学校库
  589. $schDsn = 'mysql:host=' . $dataBaseInfo['database_host'] . ';dbname=' . $dataBaseInfo['database_name'] . ';';
  590. $schdbh = new PDO($schDsn, $dataBaseInfo['database_user'], $dataBaseInfo['database_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  591. $wordInfo = $schdbh->query('SELECT `word_id`,`word_name`,`subject_id`,`uploader_id`,`topic_paper_id` FROM `topic_word` WHERE `word_id`=' . $word_id)->fetch(PDO::FETCH_ASSOC);
  592. if (empty($wordInfo)) {
  593. exit('未找到上传word信息!');
  594. }
  595. $getWordJson = file_get_contents('php://input');
  596. $jsonArray = json_decode($getWordJson, true);
  597. if (!$jsonArray || $jsonArray['errcode'] > 0) { // 解析失败
  598. $docUrl = isset($jsonArray['docurl']) ? $jsonArray['docurl'] : '';
  599. $word_down_url = self::downloadWord($wordInfo, $docUrl, $school_id);
  600. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,`file_path` = :path,parse_time=:time WHERE `word_id` =:wid');
  601. $stmt->execute(array(':wid' => $word_id, ':reason' => $jsonArray['errmsg'], ':content' => $getWordJson, ':path' => $word_down_url, ':time' => time()));
  602. exit('解析失败!');
  603. } else {
  604. if (empty($jsonArray['items'])) {
  605. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  606. $stmt->execute(array(':wid' => $word_id, ':reason' => '返回word试题内容为空', ':content' => $getWordJson, ':time' => time()));
  607. exit('返回试题内容为空');
  608. } else {
  609. $sctmt = $schdbh->prepare('UPDATE `topic_word` SET `content` = :content,parse_time=:time WHERE `word_id` =:wid');
  610. $sctmt->execute(array(':wid' => $word_id, ':time' => time(), ':content' => $getWordJson));
  611. }
  612. // 试题入库操作
  613. foreach ($jsonArray['items'] as $key => $val) {
  614. // 试题类型ID
  615. switch ($val['type']) {
  616. case '选择':
  617. $type_id = 1;
  618. break;
  619. case '多选':
  620. $type_id = 2;
  621. break;
  622. case '填空':
  623. $type_id = 5;
  624. break;
  625. default:
  626. $type_id = 7;
  627. }
  628. //处理答案
  629. if (!empty($val['key'])) {
  630. $val['key'] = self::downloadImg($val['key'], $school_id);
  631. $val['key'] = self::strip_content_tags($val['key']);
  632. }
  633. // 处理题干中的公式
  634. if (empty($val['stem'])) {
  635. continue;
  636. } else {
  637. $val['stem'] = self::downloadImg($val['stem'], $school_id);
  638. $val['stem'] = self::strip_content_tags($val['stem']);
  639. // 若是填空题下划线处理
  640. if ($val['type'] == '填空') {
  641. preg_match_all('/_{5,}/', $val['stem'], $matgap);
  642. if (!empty($matgap[0])) {
  643. $nnum = 0;
  644. foreach ($matgap[0] as $km => $vm) {
  645. $nnum = $km + 1;
  646. $val['stem'] = preg_replace('/_{3,}/', '<img class="tiankong" src="/images/list_' . $nnum . '.png">', $val['stem'], 1);
  647. }
  648. // 填空题是否多个答案
  649. preg_match_all('/<p>(.*?)<\/p>/', $val['key'], $key_array);
  650. if ($nnum > 1 && $key_array[1]) {
  651. $result_array = array_filter($key_array[1]);
  652. if ($result_array) {
  653. if (count($result_array) == $nnum) { // 如果填空数与答案数匹配
  654. $val['key'] = $key_array[1];
  655. } else { // 出现填空数与答案数不一致情况处理
  656. $new_array = array();
  657. $new_nnum = $nnum - 1;
  658. for ($t = 0; $t <= $new_nnum; $t++) {
  659. if (isset($result_array[$t])) {
  660. $new_array[] = $result_array[$t];
  661. } else {
  662. $new_array[] = '';
  663. }
  664. }
  665. $val['key'] = $new_array;
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. // 试题解析
  673. if (!empty($val['analysis'])) {
  674. $val['analysis'] = self::downloadImg($val['analysis'], $school_id);
  675. $val['analysis'] = self::strip_content_tags($val['analysis']);
  676. }
  677. // 单选题处理
  678. if (!empty($val['options'])) {
  679. foreach ($val['options'] as $vk => &$vp) {
  680. $vp = self::downloadImg($vp, $school_id);
  681. $vp = self::strip_content_tags($vp);
  682. // $val['options'][$vk] = $vp;
  683. }
  684. }
  685. // 获取默认文件夹id
  686. $folderId = 0;
  687. // 获取试题id
  688. //$getUid = $schdbh->query('SELECT UUID_SHORT() AS uuid')->fetch(PDO::FETCH_ASSOC);
  689. //$resUid = substr($getUid['uuid'],-6);
  690. $now_time = time();
  691. $difficulty = isset($val['difficulty']) ? $val['difficulty'] : 1;
  692. // 试题入库处理
  693. try {
  694. $schdbh->beginTransaction(); // 开启事务
  695. // topic表 topic_from=1 校本卷库的题
  696. $row = null;
  697. $row = $schdbh->prepare('INSERT INTO `topic`(`topic_type`, `topic_title`, `topic_difficulty`, `folder_id`, `subject_id`,`source_title`, `parse_content`, `creator_id`,`updater_id`, `create_time`,`update_time`,`topic_from`) VALUES (:ttype,:ttile,:tdiff,:fid,:sid,:sotitle,:ana,:ceid,:upid,:time,:utime,1)'); // 执行第一个 SQL
  698. $row->execute(array(':ttype' => $type_id, ':ttile' => $val['stem'], ':tdiff' => $difficulty, ':fid' => $folderId, ':sid' => $wordInfo['subject_id'], ':sotitle' => 'word上传试题', ':ana' => $val['analysis'], ':ceid' => $wordInfo['uploader_id'], ':upid' => $wordInfo['uploader_id'], ':time' => $now_time, ':utime' => $now_time));
  699. $resUid = $schdbh->lastInsertId();
  700. if (!$resUid) {
  701. throw new PDOException('插入topic表失败!');
  702. }
  703. //topic_paper_relation
  704. $rowTpr = null;
  705. $rowTpr = $schdbh->prepare('INSERT INTO `topic_paper_relation`(`paper_id`, `topic_id`, `topic_type`, `topic_score`, `order`) VALUES (:paperId,:topicId,:topicType,:topicScore,:order)'); // 执行第一个 SQL
  706. $rowTpr->execute(array(':paperId' => $wordInfo['topic_paper_id'], ':topicId' => $resUid, ':topicType' => $type_id, ':topicScore' => $val['score'], ':order' => $key));
  707. $resTpr = $schdbh->lastInsertId();
  708. if (!$resTpr) {
  709. throw new PDOException('插入topic_paper_relation表失败!');
  710. }
  711. // topic_item
  712. $rowTwo = null;
  713. $rowTwo = $schdbh->prepare('INSERT INTO `topic_item`(`topic_id`, `topic_type`, `topic_title`) VALUES (:tpid,:type,:title)');
  714. $getRowTwo = $rowTwo->execute(array(':tpid' => $resUid, ':type' => $type_id, ':title' => $val['stem']));
  715. if (!$getRowTwo) {
  716. throw new PDOException('插入topic_item表失败!');
  717. }
  718. // 单选题处理
  719. if ($type_id == 1 || $type_id == 2) {
  720. // 获取正确答案
  721. $answerNums = array();
  722. $getAnswer = str_replace(array(" ", " ", "\t", "\n", "\r"), '', strip_tags($val['key']));
  723. $getAnswer = strtoupper($getAnswer);
  724. foreach ($this->charToNum as $char => $num) {
  725. if (strpos($getAnswer, $char) !== false) {
  726. $answerNums[] = $num;
  727. }
  728. }
  729. $rowThr = null;
  730. $rowThr = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
  731. foreach ($val['options'] as $vk => $option) {
  732. $is_true = 0;
  733. if (in_array($vk, $answerNums)) {
  734. $is_true = 1;
  735. }
  736. $getRowThr = $rowThr->execute(array(':tpid' => $resUid, ':content' => $option, ':correct' => $is_true, 'sort_order' => $vk));
  737. }
  738. } else {
  739. $rowFiv = null;
  740. $rowFiv = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
  741. if (is_array($val['key']) && $val['key']) {
  742. foreach ($val['key'] as $topicKey) {
  743. $getRowFiv = $rowFiv->execute(array(':tpid' => $resUid, ':content' => $topicKey));
  744. }
  745. } else {
  746. $getRowFiv = $rowFiv->execute(array(':tpid' => $resUid, ':content' => $val['key']));
  747. if (!$getRowFiv) {
  748. throw new PDOException('插入topic_item_option表失败!');
  749. }
  750. }
  751. }
  752. // word与试题关系
  753. $rowFor = null;
  754. $rowFor = $schdbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
  755. $getRowFor = $rowFor->execute(array(':wid' => $word_id, ':toid' => $resUid));
  756. if (!$getRowFor) {
  757. throw new PDOException('插入word_to_topic表失败!');
  758. }
  759. $schdbh->commit();
  760. } catch (PDOException $e) {
  761. $schdbh->rollback(); // 执行失败,事务回滚
  762. exit($e->getMessage());
  763. }
  764. }
  765. // 处理完
  766. $resu = $schdbh->prepare('UPDATE `topic_word` SET `status` = 2,`parse_time`=:time WHERE `word_id` =:wid');
  767. $resu->execute(array(':wid' => $word_id, ':time' => time()));
  768. // 监控日志
  769. $teachInfo = $schdbh->query('SELECT `teacher_name` FROM `teacher` WHERE `teacher_id`="' . $wordInfo['uploader_id'] . '"')->fetch(PDO::FETCH_ASSOC);
  770. BMonitorLog::model()->addLog(array(
  771. 'school_id' => $school_id,
  772. 'teacher_id' => $wordInfo['uploader_id'],
  773. 'log_content' => array(
  774. $teachInfo['teacher_name'],
  775. '上传了',
  776. $wordInfo['word_name'],
  777. '校本卷库'
  778. )
  779. ));
  780. $schdbh = null;
  781. exit('解析完成!');
  782. }
  783. }
  784. /**
  785. * 全学科第三方试卷解析
  786. * @param $string
  787. * @return mixed
  788. */
  789. public function actionAllSubjectCoach()
  790. {
  791. $apiTopics = array();
  792. // 获取curl过来值
  793. $word_id = isset($_GET['wid']) ? intval($_GET['wid']) : 0;
  794. $school_id = isset($_GET['sid']) ? intval($_GET['sid']) : 0;
  795. if (empty($word_id) || empty($school_id)) {
  796. exit('未找到word Id或者学校id');
  797. }
  798. // 连接业务库
  799. $busDsn = Yii::app()->businessDb;
  800. $dataBaseInfo = $busDsn->createCommand('SELECT * FROM `database` WHERE `school_id`=' . $school_id)->queryOne();
  801. if (empty($dataBaseInfo)) {
  802. exit('未找到数据库链接信息!');
  803. }
  804. $busdbh = null;
  805. // 连接学校库
  806. $schDsn = 'mysql:host=' . $dataBaseInfo['database_host'] . ';dbname=' . $dataBaseInfo['database_name'] . ';';
  807. $schdbh = new PDO($schDsn, $dataBaseInfo['database_user'], $dataBaseInfo['database_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES "utf8";'));
  808. $wordInfo = $schdbh->query('SELECT `word_id`,`word_name`,`subject_id`,`uploader_id`,`exam_group_id` FROM `topic_word` WHERE `word_id`=' . $word_id)->fetch(PDO::FETCH_ASSOC);
  809. if (empty($wordInfo)) {
  810. exit('未找到上传word信息!');
  811. }
  812. $getWordJson = file_get_contents('php://input');
  813. $jsonArray = json_decode($getWordJson, true);
  814. if (empty($jsonArray) || $jsonArray['errcode'] == 1 || empty($jsonArray['items'])) {
  815. $errMsg = array();
  816. if (isset($jsonArray['errmsgs']) && $jsonArray['errmsgs']) {
  817. if (is_array($jsonArray['errmsgs'])) {
  818. foreach ($jsonArray['errmsgs'] as $msg) {
  819. $errMsg[] = key($msg) . ':' . implode(', ', $msg[key($msg)]);
  820. }
  821. } else {
  822. $errMsg[] = $jsonArray['errmsgs'];
  823. }
  824. }
  825. $errMsg = $errMsg ? implode('<br/>', $errMsg) : '解析异常';
  826. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,`file_path` = :path,parse_time=:time WHERE `word_id` =:wid');
  827. $stmt->execute(array(':wid' => $word_id, ':reason' => $errMsg, ':content' => $getWordJson, ':path' => '', ':time' => time()));
  828. exit('解析失败!');
  829. } else {
  830. // 获取科目题型
  831. $tempTypeName = array(1=>'选择题','2'=>'多选题',11=>'多选题',5=>'填空题',7=>'解答题');
  832. $apiParam = Yii::app()->params['api'][0];
  833. $apiParam['prefix'] .= 'all_subject/topic_type/subjectId/' . $wordInfo['subject_id'];
  834. $topicTypeJson = self::getApiData($apiParam, 1, '', 3);
  835. $topicTypes = json_decode($topicTypeJson, true);
  836. if (isset($topicTypes['status'])) {
  837. if ($topicTypes['status'] == 0 || empty($topicTypes['data'])) {
  838. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason, parse_time=:time WHERE `word_id` =:wid');
  839. $stmt->execute(array(':wid' => $word_id, ':reason' => '未找到科目题型', ':time' => time()));
  840. exit('未找到科目题型');
  841. } elseif ($topicTypes['status'] == 1 && $topicTypes['data']) {
  842. $topicTypes = $topicTypes['data'];
  843. }
  844. }
  845. // 查询考试试题
  846. $paper_topic_relation_data = $schdbh->query('select pt.topic_id,pt.type,pp.* from paper_topic_relation pt join (select p.paper_id ,e.subject_id as subject_id,e.exam_group_id from exam e left JOIN paper p on e.exam_id = p.exam_id where e.exam_group_id = ' . $wordInfo['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);
  847. if (empty($paper_topic_relation_data)) {
  848. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  849. $stmt->execute(array(':wid' => $word_id, ':reason' => '未找到考试试题', ':content' => $getWordJson, ':time' => time()));
  850. exit('未找到考试试题');
  851. }
  852. if (count($paper_topic_relation_data) != count($jsonArray['items'])) {
  853. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status`=3,`wrong_reason`=:reason,`content`=:content,parse_time=:time WHERE `word_id`=:wid');
  854. $stmt->execute(array(':wid' => $word_id, ':reason' => 'WORD题量与试卷题量不同', ':content' => $getWordJson, ':time' => time()));
  855. exit('WORD题量与试卷题量不同');
  856. }
  857. $relate_topic_id = array();
  858. $p_topic_type_num = array();
  859. $relate_topic_type = array();
  860. foreach ($paper_topic_relation_data as $v) {
  861. $relate_topic_id[] = $v['topic_id'];
  862. $p_topic_type_num[$v['type']][] = $v['topic_id'];
  863. if (!in_array($v['type'], $relate_topic_type)) {
  864. $relate_topic_type[] = $v['type'];
  865. }
  866. }
  867. // 题型格式
  868. $topicTypeName = array();
  869. $topicTypeConfig = array();
  870. foreach ($topicTypes as $topicType) {
  871. $topicTypeName[$topicType['topic_type_name']] = $topicType['topic_type_id'];
  872. $topicTypeConfig[$topicType['topic_type_id']] = $topicType;
  873. }
  874. // 删除已存在试题缓存
  875. $apiParam = Yii::app()->params['api'][0];
  876. $apiParam['prefix'] .= 'topic_clear_redis/ctopic';
  877. self::getApiData($apiParam, 2, json_encode(array('topicIds' => $relate_topic_id)), 3);
  878. $topic_type_num = array(); // 解析结果各题型数量
  879. $topic_type_error = array(); // 验证是否存在非题库题型
  880. $parse_topic_type = array(); // 解析返回的题型ID
  881. $topic_type_name = array();
  882. $bankTypeParam = Yii::app()->params['subject_topic_type_matching']; // 全学科基本题型与老题库对应
  883. foreach ($jsonArray['items'] as $loop) {
  884. if (!isset($topicTypeName[$loop['type']])) {
  885. $topic_type_error[] = "题库不存在【{$loop['type']}】题型不存在";
  886. } else {
  887. $tempTypeId = (int)$topicTypeName[$loop['type']];
  888. $typeConfig = $topicTypeConfig[$tempTypeId];
  889. if (isset($bankTypeParam[$typeConfig['btt_id']])) {
  890. if (!isset($topic_type_num[$bankTypeParam[$typeConfig['btt_id']]])) {
  891. $parse_topic_type[] = $bankTypeParam[$typeConfig['btt_id']];
  892. $topic_type_num[$bankTypeParam[$typeConfig['btt_id']]] = array();
  893. }
  894. $topic_type_num[$bankTypeParam[$typeConfig['btt_id']]][] = 1;
  895. }
  896. }
  897. }
  898. if ($topic_type_error) {
  899. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content, parse_time=:time WHERE `word_id` =:wid');
  900. $stmt->execute(array(':wid' => $word_id, ':reason' => implode('<br/>', $topic_type_error), ':content' => $getWordJson, ':time' => time()));
  901. exit('题型不存在');
  902. }
  903. // 获取默认文件夹id
  904. $defaultFolder = $schdbh->query('SELECT `folder_id` FROM `topic_folder` WHERE `is_system`=1')->fetch(PDO::FETCH_ASSOC);
  905. $folderId = empty($defaultFolder['folder_id']) ? 0 : $defaultFolder['folder_id'];
  906. // 解析试题内容
  907. foreach ($jsonArray['items'] as $key => $item) {
  908. $topicId = $paper_topic_relation_data[$key]['topic_id'];
  909. $apiTopics[] = $topicId;
  910. $typeId = (int)$topicTypeName[$item['type']];
  911. $typeConfig = $topicTypeConfig[$typeId];
  912. // 试题入库处理
  913. try {
  914. $schdbh->beginTransaction(); // 开启事务
  915. $now_time = time();
  916. $difficulty = isset($item['difficulty']) ? $item['difficulty'] : 1;
  917. // topic表
  918. $stmt = $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');
  919. $stmt->execute(array(':topic_id' => $topicId, ':topic_title' => $item['stem'],':topic_difficulty'=>$difficulty,':bank_type'=>$typeId, ':source_title' => 'word上传试题', ':parse_content' => $item['analysis'], ':update_time' => time()));
  920. // topic_item
  921. $stmt = $schdbh->prepare('UPDATE `topic_item` SET `topic_title` = :topic_title WHERE `topic_id` =:topic_id');
  922. $stmt->execute(array(':topic_id' => $topicId, ':topic_title' => $item['stem']));
  923. $stmt = $schdbh->prepare('delete from `topic_item_option` WHERE `topic_id` =:topic_id');
  924. $stmt->execute(array(':topic_id' => $topicId));
  925. // 选择题选项处理
  926. if (isset($item['options']) && $item['options']) { // 含有选项的试题
  927. //$optionKeys = explode(',', $item['key']);
  928. $optionKeys = array();
  929. foreach ($this->charToNum as $char => $num) {
  930. if (strpos($item['key'], $char) !== false) {
  931. $optionKeys[] = $char;
  932. }
  933. }
  934. $rowThr = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`, `option_correct`,`sort_order`) VALUES (:tpid,:content,:correct,:sort_order)');
  935. $correct = 'A';
  936. foreach ($item['options'] as $vk => $option) {
  937. $is_true = in_array($correct, $optionKeys) ? 1 : 0;
  938. $getRowThr = $rowThr->execute(array(':tpid'=>$topicId,':content'=>$option,':correct'=>$is_true,':sort_order'=>$vk));
  939. ++$correct;
  940. }
  941. } else {
  942. $rowFiv = $schdbh->prepare('INSERT INTO `topic_item_option`(`topic_id`, `option_content`) VALUES (:tpid,:content)');
  943. if (is_array($item['key']) && $item['key']) {
  944. foreach ($item['key'] as $topicKey) {
  945. $getRowFiv = $rowFiv->execute(array(':tpid' => $topicId, ':content' => $topicKey));
  946. }
  947. } else {
  948. $getRowFiv = $rowFiv->execute(array(':tpid' => $topicId, ':content' => $item['key']));
  949. if (!$getRowFiv) {
  950. throw new PDOException('插入topic_item_option表失败!');
  951. }
  952. }
  953. }
  954. // word与试题关系
  955. $rowFor = null;
  956. $rowFor = $schdbh->prepare('INSERT INTO `word_topic_relation`(`word_id`, `topic_id`) VALUES (:wid,:toid)');
  957. $getRowFor = $rowFor->execute(array(':wid' => $word_id, ':toid' => $topicId));
  958. if (!$getRowFor) {
  959. throw new PDOException('插入word_to_topic表失败!');
  960. }
  961. $schdbh->commit();
  962. } catch (PDOException $e) {
  963. $schdbh->rollback(); // 执行失败,事务回滚
  964. $stmt = $schdbh->prepare('UPDATE `topic_word` SET `status` = 3,`wrong_reason` = :reason,`content` = :content,parse_time=:time WHERE `word_id` =:wid');
  965. $stmt->execute(array(':wid' => $word_id, ':reason' => $e->getMessage(), ':content' => $getWordJson, ':time' => time()));
  966. exit($e->getMessage());
  967. }
  968. }
  969. //exit($topicId);
  970. }
  971. // 处理完
  972. $resu = $schdbh->prepare('UPDATE `topic_word` SET `status` = 2,`parse_time`=:time,`content` = :content WHERE `word_id` =:wid');
  973. $resu->execute(array(':wid' => $word_id, ':time' => time(), ':content' => $getWordJson));
  974. $resu = $schdbh->prepare('UPDATE `exam_group` SET `is_answersheet` = 1 WHERE `exam_group_id` =:exam_group_id');
  975. $resu->execute(array(':exam_group_id' => $wordInfo['exam_group_id']));
  976. // 插入解析时间记录
  977. $resPro = $schdbh->prepare("INSERT INTO `exam_process` (`exam_group_id`, `action_type`, `action_time`) VALUES (:exam_group_id, :tempType, :time)");
  978. $resPro->execute(array(':exam_group_id' => $wordInfo['exam_group_id'], ':tempType' => 4, ':time' => time()));
  979. $schdbh = null;
  980. if($apiTopics){
  981. //调用试题检索接口
  982. $this->searchTopics($apiTopics,$school_id);
  983. }
  984. exit('解析完成!');
  985. }
  986. // 试题内容相关过滤处理等
  987. function strip_content_tags($str)
  988. {
  989. if (empty($str)) return $str;
  990. $allow_tags = "<br><dd><dl><div><dt><img><label><li><ol><p><span><sub><sup><table><tbody><td><tfoot><th><thead><tr><ul>";
  991. // $str = htmlspecialchars_decode($str);
  992. $str = preg_replace('/alt=[\\\'|\\"](.*?)[\\\'|\\"]/', '', $str);
  993. return $str;
  994. // return strip_tags($str, $allow_tags);
  995. }
  996. // 下载远程图片
  997. function downloadImg($content, $school_id)
  998. {
  999. $img_date = date('Ymd');
  1000. $school_id = empty($school_id) ? 1 : $school_id;
  1001. // 如果解析返回的图片是已上传ucloud则无需处理
  1002. if ($this->flag == 1) {
  1003. return $content;
  1004. }
  1005. // 匹配所有远程图片
  1006. $img_array = array();
  1007. preg_match_all("/<img.*?src=[\\\'| \\\"](.*?(?:[\.gif|\.jpg|\.png]))[\\\'|\\\"].*?[\/]?>/is", $content, $img_array);
  1008. if (empty($img_array[1])) {
  1009. return $content;
  1010. }
  1011. // 文件保存目录URL
  1012. $save_url = '/uploadfiles/wordimg/' . $school_id . '/' . date('Y') . '/' . date('m') . '/' . date('d');
  1013. $save_path = dirname(dirname(dirname(__FILE__))) . $save_url;
  1014. // 创建文件夹
  1015. if (!is_dir($save_path)) {
  1016. @mkdir($save_path, 0777, true);
  1017. }
  1018. foreach ($img_array[1] as $key => $value) {
  1019. // 保存到本地图片名称
  1020. $imgInfo = pathinfo($value);
  1021. $imgname = uniqid() . '.' . $imgInfo['extension'];
  1022. // 保存到本地的实际文件地址(包含路径和名称)
  1023. $fileName = $save_path . '/' . $imgname;
  1024. self::curlDownloadImage($value, $fileName);
  1025. // 教师端访问的地址
  1026. $fileurl = $this->webSiteUrl . $save_url . "/" . $imgname;
  1027. // 判断图片是否要压缩
  1028. self::imgCompress($fileName);
  1029. // 替换原来的图片地址
  1030. $uploadInfo = $this->ucloud->putFile('teacher'.$save_url .'/'. $imgname, $fileName);
  1031. if ($uploadInfo['status']) {
  1032. $content = str_replace($value, $uploadInfo['url'], $content);
  1033. @unlink($fileName);
  1034. } else {
  1035. $content = str_replace($value, $fileurl, $content);
  1036. }
  1037. }
  1038. return $content;
  1039. }
  1040. /**
  1041. * 采集远程图片
  1042. * @param string $url 远程文件地址
  1043. * @param string $fileName 保存后的文件名
  1044. * @return bool
  1045. */
  1046. function curlDownloadImage($url, $fileName = '')
  1047. {
  1048. if (empty($url) || empty($fileName)) {
  1049. return false;
  1050. }
  1051. // 获取远程文件资源
  1052. $ch = curl_init();
  1053. curl_setopt($ch, CURLOPT_URL, $url);
  1054. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1055. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  1056. $tempFile = curl_exec($ch);
  1057. curl_close($ch);
  1058. // 保存文件
  1059. $fp = @fopen($fileName, "w");
  1060. fwrite($fp, $tempFile);
  1061. fclose($fp);
  1062. return true;
  1063. }
  1064. // 压缩图片尺寸以及比例
  1065. public static function imgCompress($imgSrc = '')
  1066. {
  1067. $standardWidth = 2000;
  1068. if (empty($imgSrc)) {
  1069. return true;
  1070. } elseif (!file_exists($imgSrc)) {
  1071. return true;
  1072. }
  1073. list($width, $height, $type) = getimagesize($imgSrc);
  1074. if ($width < $standardWidth) {
  1075. return true;
  1076. }
  1077. $multiple = round($width / $standardWidth, 2);
  1078. $newWidth = 2000;
  1079. $newHeight = ceil($height / $multiple);
  1080. if (!$newWidth || !$newHeight) return true;
  1081. $imgType = image_type_to_extension($type, false);
  1082. $imageCreateType = 'image' . $imgType; // 创建新图像
  1083. $funCreateFromType = 'imagecreatefrom' . $imgType; // 创建新图像
  1084. $imageP = imagecreatetruecolor($newWidth, $newHeight);
  1085. $colorBlack = imagecolorallocatealpha($imageP, 0, 0, 0, 127);//拾取一个完全透明的颜色
  1086. imagealphablending($imageP, false);//关闭混合模式,以便透明颜色能覆盖原画布
  1087. imagefill($imageP, 0, 0, $colorBlack);//填充
  1088. $image = $funCreateFromType($imgSrc);
  1089. imagecopyresampled($imageP, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  1090. imagesavealpha($imageP, true);
  1091. $imageCreateType($imageP, $imgSrc);
  1092. imagedestroy($image);
  1093. imagedestroy($imageP);
  1094. return true;
  1095. }
  1096. // 下载错题word文本
  1097. function downloadWord($word_info, $word_url, $school_id)
  1098. {
  1099. //暂时修改
  1100. return "";
  1101. if (empty($word_info) || empty($word_url) || empty($school_id)) return '';
  1102. // 文件保存目录URL
  1103. $save_url = '/uploadfiles/words/wrong_word/' . $school_id . '/' . date('Y') . '/' . date('m') . '/' . date('d');
  1104. $save_path = dirname(dirname(dirname(__FILE__))) . $save_url;
  1105. // 创建文件夹
  1106. if (!is_dir($save_path)) {
  1107. @mkdir($save_path, 0777, true);
  1108. }
  1109. // 保存到本地word名称
  1110. $wordInfo = pathinfo($word_info['word_name']);
  1111. $wordname = $word_info['word_id'] . '.' . $wordInfo['extension'];
  1112. // 保存到本地的实际文件地址(包含路径和名称)
  1113. $fileName = $save_path . '/' . $wordname;
  1114. // 实际访问的地址
  1115. $fileurl = Yii::app()->params['siteurl'] . $save_url . "/" . $wordname;
  1116. // 读取远程图片
  1117. set_time_limit(0);
  1118. $get_file = @file_get_contents($word_url);
  1119. if ($get_file) {
  1120. $fp = @fopen($fileName, "w");
  1121. @fwrite($fp, $get_file);
  1122. @fclose($fp);
  1123. return $fileurl;
  1124. }
  1125. return '';
  1126. }
  1127. // 公式转换(2018-11-29停用)
  1128. function formulaTransform($string)
  1129. {
  1130. preg_match_all("/(data-latex=\\\"([^\"]*\\\"))|(\\$.*?\\$)|(\\\\\\[.*?\\\\\\])/", $string, $get_math_thr);
  1131. if ($get_math_thr[0]) {
  1132. $uniqueMath = array_unique($get_math_thr[0]);
  1133. foreach ($uniqueMath as $vvtr) {
  1134. if (substr($vvtr, 0, 10) !== "data-latex") {
  1135. $mathmlOptThr = saveMathtex($vvtr);
  1136. if ($mathmlOptThr['status'] == 1) {
  1137. $string = str_replace($vvtr, '<img src="' . $mathmlOptThr['fileUrl'] . '" class="gsImgLatex mathType" width="' . $mathmlOptThr['imgWidth'] . '" height="' . $mathmlOptThr['imgHeight'] . '"/>', $string);
  1138. }
  1139. }
  1140. }
  1141. }
  1142. // LaTeX与MathML转换(暂时注销 2016-10-08)
  1143. //$myMathml = new MathMlEncode();
  1144. //$string = $myMathml->replaceForJsMathml($string);
  1145. return $string;
  1146. }
  1147. private static function getApiData($apiParam, $flag = 1, $data, $timeOut = 0,$extraHeaders = array())
  1148. {
  1149. $result = FALSE;
  1150. $ch = @curl_init();
  1151. if ($ch) {
  1152. $headers = array(
  1153. 'Content-Type: application/json',
  1154. 'Content-Length: ' . strlen($data),
  1155. );
  1156. if($extraHeaders){
  1157. $headers = array_merge($headers,$extraHeaders);
  1158. }
  1159. // Digest认证
  1160. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  1161. curl_setopt($ch, CURLOPT_USERPWD, $apiParam['username'] . ':' . $apiParam['password']);
  1162. // 不输出头部
  1163. curl_setopt($ch, CURLOPT_HEADER, 0);
  1164. if ($timeOut) {
  1165. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
  1166. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  1167. }
  1168. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  1169. // curl_exec 获取到的内容不直接输出, 而是返回
  1170. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1171. // 请求重启路由器的地址 传参 进行重启
  1172. curl_setopt($ch, CURLOPT_URL, $apiParam['prefix']);
  1173. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  1174. if ($flag == 2) {
  1175. curl_setopt($ch, CURLOPT_POST, 1);
  1176. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1177. }
  1178. $result = curl_exec($ch);
  1179. if (curl_errno($ch)) {
  1180. return 'curl_errno:' . curl_errno($ch);
  1181. }
  1182. // 释放资源
  1183. curl_close($ch);
  1184. }
  1185. return $result;
  1186. }
  1187. //全学科试题调用java检索缓存接口
  1188. public function searchTopics($topicIds = array(),$schoolId = 0){
  1189. $apiParam = Yii::app()->params['api'][3];
  1190. if($apiParam && $topicIds && $schoolId){
  1191. $url = $apiParam['prefix'].'cgi/teacher/schtk/qxk/school-topic-flush/'.$schoolId;
  1192. self::getApiData(array(
  1193. 'username' => $apiParam['username'],
  1194. 'password' => $apiParam['password'],
  1195. 'prefix' => $url
  1196. ), 2, json_encode($topicIds), 3,array('X-Basic-V:2'));
  1197. }
  1198. }
  1199. }