UploadController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. class UploadController extends Controller
  3. {
  4. public function actionIndex()
  5. {
  6. $data = array();
  7. $teachers = $this->schoolManager->getTeachers(array("add_time DESC"));
  8. $teacherNames = array();
  9. $_teachers = array();
  10. if ($teachers) {
  11. foreach ($teachers as $v) {
  12. if($this->subjectId==3)
  13. {
  14. if($v['subjects']==$this->subjectId || $v['subjects']==6)
  15. {
  16. $teacherNames[$v['teacher_id']] = $v['teacher_name'];
  17. $_teachers[] = $v;
  18. }
  19. }else
  20. {
  21. if($v['subjects']==$this->subjectId)
  22. {
  23. $teacherNames[$v['teacher_id']] = $v['teacher_name'];
  24. $_teachers[] = $v;
  25. }
  26. }
  27. }
  28. }
  29. $data['teachers'] = $_teachers;
  30. $data['teacherNames'] = $teacherNames;
  31. $papers = $this->schoolManager->getUploadPaperList(array("add_time DESC", "id DESC"),$this->subjectId);
  32. $data['papers'] = $papers["rs"];
  33. $data['pages'] = $papers["pager"];
  34. $data['pageTotal'] = $papers["pager"]->rowsCount;
  35. //$subject_exam_data = $this->schoolManager->getxuekeStatus($this->semesterId);
  36. //["subject"] = $subject_exam_data;
  37. $this->render('index', $data);
  38. }
  39. public function actionView()
  40. {
  41. $data = array();
  42. $error = array();
  43. $id = (int)Yii::app()->request->getParam('id');
  44. if ($id <= 0) {
  45. $error[] = '请指定参数 Id';
  46. } else {
  47. $detail = $this->sConn->createCommand("SELECT * FROM upload_paper WHERE id={$id}")->query()->read();
  48. if (! $detail) {
  49. $error[] = '未找到数据';
  50. } elseif (empty($detail['json_data'])) {
  51. $error[] = 'JSON 数据未记录';
  52. } else {
  53. $zipPath = $detail['zip_path'];
  54. $jsonObj = json_decode($detail['json_data'], true);
  55. $apiUrl = Yii::app()->params['api_host'];
  56. $zipDir = pathinfo($zipPath, PATHINFO_DIRNAME);
  57. $zipName = pathinfo($zipPath, PATHINFO_FILENAME);
  58. $imgUrl = $apiUrl .'/'. $zipDir .'/'. $zipName .'/result/';
  59. $data['examName'] = $jsonObj['formtitle'];
  60. $items = array(
  61. 1 => array(),
  62. 5 => array(),
  63. 7 => array()
  64. );
  65. foreach ((array)$jsonObj['items'] as $key => $val) {
  66. $typeName = $val['type'];
  67. switch ($typeName) {
  68. case '选择':
  69. $typeId = 1;
  70. break;
  71. case '填空':
  72. $typeId = 5;
  73. break;
  74. case '简答':
  75. $typeId = 7;
  76. break;
  77. default:
  78. continue;
  79. }
  80. $arr = array();
  81. $arr['title'] = $this->replaceImgSrc($this->replaceMathtex($val['stem']), $imgUrl);
  82. $arr['analysis'] = isset($val['analysis']) ? $this->replaceImgSrc($this->replaceMathtex($val['analysis']), $imgUrl) : '';
  83. $arr['key'] = $this->replaceImgSrc($this->replaceMathtex($val['key']), $imgUrl);
  84. if ($typeId == 1) {
  85. $arr['options'] = array();
  86. $answer = strtoupper(trim(strip_tags($val['key'])));
  87. $answerNum = letterToNum($answer);
  88. foreach ($val['options'] as $optionIndex => $optionContent) {
  89. $arr['options'][] = array(
  90. 'content' => $this->replaceImgSrc($this->replaceMathtex($optionContent), $imgUrl),
  91. 'correct' => ($answerNum == $optionIndex ? 1 : 0),
  92. );
  93. }
  94. if ($val['optionlines'] == 1) {
  95. $arr['listType'] = 1;
  96. } elseif ($val['optionlines'] == 2) {
  97. $arr['listType'] = 2;
  98. } else {
  99. $arr['listType'] = 0;
  100. }
  101. }
  102. $items[$typeId][] = $arr;
  103. }
  104. $data['items'] = $items;
  105. }
  106. }
  107. if ($error) {
  108. Yii::app()->jump->error(implode('<br/>', $error));
  109. } else {
  110. $this->render('view', $data);
  111. }
  112. }
  113. public function actionAjaxConfirm()
  114. {
  115. $error = array();
  116. $result = array();
  117. if (! Yii::app()->request->isPostRequest or ! Yii::app()->request->isAjaxRequest) {
  118. $error[] = '错误的来源!';
  119. } else {
  120. $id = (int)Yii::app()->request->getPost('id');
  121. $detail = $this->sConn->createCommand("SELECT * FROM upload_paper WHERE id={$id}")->query()->read();
  122. if (! $detail) {
  123. $error[] = '未找到数据';
  124. } elseif ($detail['status'] != 0) {
  125. $error[] = '已经确认过了';
  126. } elseif (empty($detail['json_data'])) {
  127. $error[] = 'JSON 数据未记录';
  128. } else {
  129. $confirm = $this->apiPost('upload_paper/confirm', array(
  130. 'id' => $id,
  131. 'schoolId' => $this->schoolId,
  132. 'semesterId' => $this->semesterId,
  133. ), 1);
  134. if (! $confirm) {
  135. $error[] = 'Api system error';
  136. } elseif (! isset($confirm->success) or !isset($confirm->message)) {
  137. $error[] = 'Api response error';
  138. } elseif ($confirm->success != 1) {
  139. $error[] = $confirm->message;
  140. }
  141. }
  142. }
  143. header('Content-Type: application/json');
  144. if ($error) {
  145. echo json_encode(array('status' => 0, 'error'=> implode('<br/>', $error)));exit;
  146. } else {
  147. echo json_encode(array('status' => 1, 'error'=> ''));exit;
  148. }
  149. }
  150. public function actionAjaxGetTeacherSubjects()
  151. {
  152. $error = array();
  153. $result = array();
  154. if (! Yii::app()->request->isPostRequest or ! Yii::app()->request->isAjaxRequest) {
  155. $error[] = '错误的来源!';
  156. } else {
  157. $teacherId = Yii::app()->request->getPost('teacherId');
  158. if (! $teacherId or ! is_numeric($teacherId)) {
  159. $error[] = 'teacherId 错误';
  160. } else {
  161. /*
  162. $subjects = $this->sConn->createCommand("SELECT DISTINCT csr.subject_id FROM teacher_class_relation tcr JOIN class_subject_relation csr ON csr.class_id = tcr.class_id WHERE tcr.teacher_id = '{$teacherId}' AND tcr.semester_id = '{$this->semesterId}'")->query()->readAll();
  163. $subjectIds = array();
  164. $subjectNames = array();
  165. if ($subjects) {
  166. foreach ($subjects as $v) {
  167. $subjectIds[] = $v['subject_id'];
  168. }
  169. $subjects = $this->apiPost('/subject/batch', array('subjectIds' => $subjectIds));
  170. if (! $subjects) {
  171. $error[] = 'Brain subject/batch error';
  172. } elseif (isset($subjects->error)) {
  173. $error[] = $subjects->error;
  174. } else {
  175. $result = $subjects;
  176. }
  177. }
  178. */
  179. /* $subjectIds = array(3, 6);
  180. $subjects = $this->apiPost('/subject/batch', array('subjectIds' => $subjectIds));
  181. if (! $subjects) {
  182. $error[] = 'Brain subject/batch error';
  183. } elseif (isset($subjects->error)) {
  184. $error[] = $subjects->error;
  185. } else {
  186. $result = $subjects;
  187. }*/
  188. $teacherSubjectIds=$this->mathSubjectId;
  189. $teacherSubjectNames = array(3 => '高一高二专用', 6 => '高三专用',51=>'新高一高二');
  190. $teacherSubjectMaterialIds = array();
  191. $teacherSubjectMaterialNames = array();
  192. foreach ($teacherSubjectIds as $teacherSubjectId) {
  193. $teacherSubjectMaterialIds[$teacherSubjectId] = array();
  194. $teacherSubjectMaterialNames[$teacherSubjectId] = array();
  195. $textbookTree = $this->apiPost('/textbook/tree', array(
  196. 'subjectId' => $teacherSubjectId,
  197. 'depth' => 3
  198. ));
  199. if (! $textbookTree) {
  200. $error[] = 'Brain textbook/tree error';
  201. } elseif (isset($textbookTree->error)) {
  202. $error[] = $textbookTree->error;
  203. } else {
  204. foreach ($textbookTree as $textbook) {
  205. if (isset($textbook->modules)) {
  206. foreach ($textbook->modules as $module) {
  207. if (isset($module->chapters)) {
  208. $teacherSubjectMaterialIds[$teacherSubjectId][$textbook->textbook_id] = $textbook->textbook_id;
  209. $teacherSubjectMaterialNames[$teacherSubjectId][$textbook->textbook_id] = $textbook->textbook_name;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. $result['teacherSubject'] = array(
  217. array('subject_id'=>3,'subject_name'=>'高一高二专用'),
  218. array('subject_id'=>6,'subject_name'=>'高三专用'),
  219. array('subject_id'=>51,'subject_name'=>'新高一高二'),
  220. );
  221. $result['SubjectMaterialNames'] = $teacherSubjectMaterialNames;
  222. $result['SubjectMaterialIds'] = $teacherSubjectMaterialIds;
  223. }
  224. }
  225. header('Content-Type: application/json');
  226. if ($error) {
  227. echo json_encode(array('status' => 0, 'error'=> implode('<br/>', $error)));exit;
  228. } else {
  229. echo json_encode($result);exit;
  230. }
  231. }
  232. /**
  233. * 替换图片目录
  234. *
  235. * @author CeeFee
  236. */
  237. private function replaceImgSrc($content, $url)
  238. {
  239. $regex = "/<img[^>]*[\s]+src[\s]*=[\s]*(([\"](files\/image\d+\.(png|gif|jpg|jpeg|bmp))([^\"]*)[\"])|([\'](files\/image\d+\.(png|gif|jpg|jpeg|bmp))([^\']*)[\'])|(files\/image\d+\.(png|gif|jpg|jpeg|bmp))([^\s]*))[^>]*>/si";
  240. if (preg_match_all($regex, $content, $temp)) {
  241. $imgArr = array();
  242. $tmpArr = array_merge($temp[3], $temp[7], $temp[10]);
  243. foreach ($tmpArr as $v) {
  244. if (! empty($v) and ! in_array($v, $imgArr)) {
  245. $imgArr[] = $v;
  246. }
  247. }
  248. unset($tmpArr);
  249. foreach ($imgArr as $v) {
  250. $content = str_replace($v, $url. $v, $content);
  251. }
  252. }
  253. return $content;
  254. }
  255. /**
  256. * 替换 mathtex
  257. *
  258. * @author CeeFee
  259. */
  260. private function replaceMathtex($content)
  261. {
  262. // 题目中公式处理(一)
  263. /*if (preg_match_all('/\\\\\(.*?\\\\\)/', $content, $temp)) {
  264. $arr = array_unique($temp[0]);
  265. foreach ($arr as $v) {
  266. $content = str_replace($v, '<img src="'. Yii::app()->params['mt_prefix'] . $v.'" class="gsImgLatex mathType"/>', $content);
  267. }
  268. }
  269. // 题目中公式处理(二)
  270. if (preg_match_all('/\\\\\[.*?\\\\\]/', $content, $temp)) {
  271. $arr = array_unique($temp[0]);
  272. foreach ($arr as $v) {
  273. $content = str_replace($v, '<img src="'. Yii::app()->params['mt_prefix']. $v .'" class="gsImgLatex mathType"/>', $content);
  274. }
  275. }
  276. // 题目中公式处理(三)
  277. if (preg_match_all('/\$.*?\$/', $content, $temp)) {
  278. $arr = array_unique($temp[0]);
  279. foreach ($arr as $v) {
  280. $content = str_replace($v, '<img src="'. Yii::app()->params['mt_prefix']. $v .'" class="gsImgLatex mathType"/>', $content);
  281. }
  282. }*/
  283. if (preg_match_all("/(data-latex=\\\"([^\"]*\\\"))|(\\$.*?\\$)|(\\\\\\[.*?\\\\\\])/", $content, $temp)) {
  284. $arr = array_unique($temp[0]);
  285. foreach ($arr as $v) {
  286. if (substr($v, 0, 10) !== "data-latex") {
  287. //$content = str_replace($v, '<img src="'. $this->mtUrl. $v .'" class="gsImgLatex mathType"/>', $content);
  288. $content = str_replace($v, '<img src="'. Yii::app()->params['mt_prefix']. $v .'" class="gsImgLatex mathType"/>', $content);
  289. }
  290. }
  291. }
  292. /*
  293. // 空格替换
  294. if (preg_match_all('/_{5,}/', $content, $temp)) {
  295. foreach ($temp[0] as $k => $v) {
  296. $content = preg_replace('/_{5,}/', '<img class="tiankong" src="/images/list_'. ($k + 1) .'.png">', $content, 1);
  297. }
  298. }
  299. */
  300. return $content;
  301. }
  302. }