WordapiController.php 74 KB

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