ArticlePaper.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. class ArticlePaper extends Auth
  5. {
  6. public function __construct(App $app = null) {
  7. parent::__construct($app);
  8. $this->model= new \app\common\model\ArticlePaper();
  9. $this->table= $this->model->table;
  10. $this->table1= 'article_record';
  11. }
  12. public function index(){
  13. if(request()->isAjax()){
  14. //分页参数
  15. $length = input('rows',10,'intval'); //每页条数
  16. $page = input('page',1,'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx','id','trim'); //排序列
  20. $sort = input('sord','desc','trim'); //排序方式
  21. $order = $sortRow.' '.$sort;
  22. $title = input('title','','trim');
  23. if($title){
  24. $map[] = ['title','like','%'.$title.'%'];
  25. }
  26. $enable = input('enable','','trim');
  27. if($enable != ''){
  28. $map[] = ['enable','=',$enable];
  29. }
  30. $map[] = ['del','=',0];
  31. $map[] = ['org_id','=',$this->orgId];
  32. $map= empty($map) ? true: $map;
  33. //数据查询
  34. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  35. foreach ($lists as $k=>$v){
  36. $lists[$k]['article_title'] = db('article')
  37. ->where('id',$v['article_id'])
  38. ->value('title');
  39. }
  40. //数据返回
  41. $totalCount = db($this->table)->where($map)->count();
  42. $totalPage = ceil($totalCount/$length);
  43. $result['page'] = $page;
  44. $result['total'] = $totalPage;
  45. $result['records'] = $totalCount;
  46. $result['rows'] = $lists;
  47. return json($result);
  48. }else{
  49. $this->assign('m_name','试卷列表');
  50. return $this->fetch();
  51. }
  52. }
  53. public function record($id){
  54. if(request()->isAjax()){
  55. //分页参数
  56. $length = input('rows',10,'intval'); //每页条数
  57. $page = input('page',1,'intval'); //第几页
  58. $start = ($page - 1) * $length; //分页开始位置
  59. //排序
  60. $sortRow = input('sidx','id','trim'); //排序列
  61. $sort = input('sord','desc','trim'); //排序方式
  62. $order = $sortRow.' '.$sort;
  63. $title = input('title','','trim');
  64. if($title){
  65. $map[] = ['title','like','%'.$title.'%'];
  66. }
  67. $enable = input('enable','','trim');
  68. if($enable != ''){
  69. $map[] = ['enable','=',$enable];
  70. }
  71. $map[] = ['questionnaire_id','=',$id];
  72. $map= empty($map) ? true: $map;
  73. //数据查询
  74. $lists = db($this->table1)->where($map)->limit($start,$length)->order($order)->select();
  75. //数据返回
  76. $totalCount = db($this->table1)->where($map)->count();
  77. $totalPage = ceil($totalCount/$length);
  78. $result['page'] = $page;
  79. $result['total'] = $totalPage;
  80. $result['records'] = $totalCount;
  81. $result['rows'] = $lists;
  82. return json($result);
  83. }else{
  84. $info = db($this->table)
  85. ->where('id',$id)
  86. ->find();
  87. $this->assign('m_name','['.$info['title'].']'.'回收记录');
  88. $this->assign('id',$id);
  89. return $this->fetch();
  90. }
  91. }
  92. /**
  93. * 新增/编辑
  94. */
  95. public function add($id=0){
  96. if(request()->isPost()){
  97. $res = $this->model->updates();
  98. if($res){
  99. $this->success('操作成功',url('index'));
  100. }else{
  101. $this->error($this->model->getError());
  102. }
  103. }else{
  104. if($id){
  105. $info =db($this->table)->where('id',$id)->find();
  106. $this->assign('info',$info);
  107. }
  108. $article = (new \app\common\model\Article())->getList();
  109. $this->assign('cate',$article);
  110. return $this->fetch();
  111. }
  112. }
  113. public function share($id){
  114. $info =db($this->table)
  115. ->where('id',$id)
  116. ->find();
  117. $strs = aes_encrypt('wj',config('app.encryption_key'));
  118. $code = getSite().'/h5/Wj/index?id='.$id.'&code='.$strs.'&orgId='.$info['org_id'];
  119. $this->assign('url',$code);
  120. return $this->fetch();
  121. }
  122. /**
  123. * 删除记录
  124. * @param int $id
  125. */
  126. public function del_question($id=0){
  127. if(!$id){
  128. $this->error('参数错误');
  129. }
  130. $info = db('article_question')
  131. ->where('id',$id)
  132. ->find();
  133. $page = db('article_paper')
  134. ->where('id',$info['article_paper_id'])
  135. ->find();
  136. if($page['status'] ==2){
  137. $this->error("已发布的试卷不能删除题目");
  138. }
  139. $res = db('article_question')->where('id',$id)->setField('del',1);
  140. if($res){
  141. $this->success('删除成功');
  142. }else{
  143. $this->error('删除失败');
  144. }
  145. }
  146. public function fb($id=0,$status=0){
  147. $enable = $status==1 || $status==0 ?2:1;
  148. $res = db($this->table)->where('id',$id)->setField('status',$enable);
  149. if($res){
  150. $this->success('操作成功');
  151. }else{
  152. $this->error('操作失败');
  153. }
  154. }
  155. /**
  156. * 改变字段值
  157. * @param int $fv
  158. * @param string $fn
  159. * @param int $fv
  160. */
  161. public function changeField($id=0,$fn='',$fv=0){
  162. if(!$fn||!$id){
  163. $this->error('参数错误');
  164. }
  165. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  166. if($res){
  167. $this->success('操作成功');
  168. }else{
  169. $this->error('操作失败');
  170. }
  171. }
  172. //回收记录详情
  173. public function info($id){
  174. $info = $this->model->record_info($id);
  175. if (!$info) {
  176. exit('数据不存在');
  177. }
  178. $this->assign('info',$info);
  179. return $this->fetch();
  180. }
  181. //试题管理
  182. public function article($id=0){
  183. if(request()->isAjax()){
  184. //分页参数
  185. $length = input('rows',10,'intval'); //每页条数
  186. $page = input('page',1,'intval'); //第几页
  187. $start = ($page - 1) * $length; //分页开始位置
  188. //排序
  189. $sortRow = input('sidx','id','trim'); //排序列
  190. $sort = input('sord','desc','trim'); //排序方式
  191. $order = $sortRow.' '.$sort;
  192. $title = input('title','','trim');
  193. if($title){
  194. $map[] = ['title','like','%'.$title.'%'];
  195. }
  196. $enable = input('enable','','trim');
  197. if($enable != ''){
  198. $map[] = ['enable','=',$enable];
  199. }
  200. $map[] = ['del','=',0];
  201. $map[] = ['article_paper_id','=',$id];
  202. $map= empty($map) ? true: $map;
  203. //数据查询
  204. $lists = db('article_question')->where($map)->limit($start,$length)->order($order)->select();
  205. //数据返回
  206. $totalCount = db('article_question')->where($map)->count();
  207. $totalPage = ceil($totalCount/$length);
  208. $result['page'] = $page;
  209. $result['total'] = $totalPage;
  210. $result['records'] = $totalCount;
  211. $result['rows'] = $lists;
  212. return json($result);
  213. }else{
  214. $info = db('article_paper')
  215. ->where('id',$id)
  216. ->find();
  217. $this->assign('m_name','['.$info['title'].']试题列表');
  218. $this->assign('id',$id);
  219. return $this->fetch();
  220. }
  221. }
  222. //添加试题
  223. public function article_add($id=0){
  224. if(request()->isPost()){
  225. $data = request()->post();
  226. $info =db('article_paper')
  227. ->where('id',$data['article_paper_id'])
  228. ->find();
  229. if(!$info || $info['org_id'] != $this->orgId){
  230. $this->error("试卷不存在");
  231. }
  232. if(!in_array($info['status'],[0,1])){
  233. $this->error('已发布过的试卷无法添加新题目');
  234. }
  235. $title = trim($data['title']);
  236. $score = (int) $data['score'];
  237. $sort = (int) $data['sort'];
  238. $type = (int) $data['type'];
  239. $options = trim($data['options']);
  240. if(!$title){
  241. $this->error('未填写题干');
  242. }
  243. if(mb_strlen($title) > 500){
  244. $this->error('题干过长,请控制在500字以内');
  245. }
  246. if($score <= 0){
  247. $this->error('分值错误');
  248. }
  249. if(!in_array($type,array(0,1))){
  250. $this->error('类型错误');
  251. }
  252. if(!$options){
  253. $this->error('未填写题目选项');
  254. }
  255. $options = json_decode($options,true);
  256. if(!$options){
  257. $this->error('选项参数错误');
  258. }
  259. $correct = 0; // 正确答案个数
  260. foreach ($options as $k=>$v){
  261. $text = trim($v['text']);
  262. if(!$text){
  263. $this->error('选项不能为空');
  264. }
  265. $options[$k]['text'] = $text;
  266. if($v['answer'] == '1'){
  267. $correct++;
  268. }
  269. }
  270. if($correct == 0){
  271. $this->error('未设置正确答案');
  272. }
  273. if($correct > 1&&$type == 0){
  274. $this->error('单选题只能设置一个正确答案');
  275. }
  276. $data = array(
  277. 'article_paper_id' => $data['article_paper_id'],
  278. 'title' => $title,
  279. 'type' => $type,
  280. 'score' => $score,
  281. 'sort' => $sort>0?$sort:0,
  282. 'options' => json_encode($options,JSON_UNESCAPED_UNICODE),
  283. 'create_time' => date('Y-m-d h:i:s'),
  284. 'update_time' => date('Y-m-d h:i:s')
  285. );
  286. $res = db('article_question')
  287. ->insertGetId($data);
  288. if($res){
  289. $this->success('操作成功');
  290. }else{
  291. $this->error('操作失败');
  292. }
  293. }else{
  294. $this->assign('id',$id);
  295. return $this->fetch();
  296. }
  297. }
  298. public function edit_question($id){
  299. if ($id <= 0) {
  300. $this->error("参数错误");
  301. }
  302. $question = db('article_question')
  303. ->where('id',$id)
  304. ->find();
  305. if(!$question){
  306. $this->error("题目不存在");
  307. }
  308. $info = db('article_paper')
  309. ->where('id',$question['article_paper_id'])
  310. ->find();
  311. if(!$info || $info['org_id'] != $this->orgId){
  312. $this->error("试卷不存在");
  313. }
  314. if(request()->isPost()){
  315. $data = request()->post();
  316. $title = trim($data['title']);
  317. $options = trim($data['options']);
  318. if(!$title){
  319. $this->error('未填写题干');
  320. }
  321. if(mb_strlen($title) > 500){
  322. $this->error('题干过长,请控制在500字以内');
  323. }
  324. if(in_array($info['status'],[0,1])){
  325. $score = (int) $data['score'];
  326. $sort = (int) $data['sort'];
  327. $type = (int) $data['type'];
  328. if($score <= 0){
  329. $this->error('分值错误');
  330. }
  331. if(!in_array($type,array(0,1))){
  332. $this->error('类型错误');
  333. }
  334. }
  335. if(!$options){
  336. $this->error('未填写题目选项');
  337. }
  338. $options = json_decode($options,true);
  339. if(!$options){
  340. $this->error('选项参数错误');
  341. }
  342. $correct = 0; // 正确答案个数
  343. foreach ($options as $k=>$v){
  344. $text = trim($v['text']);
  345. if(!$text){
  346. $this->error('选项不能为空');
  347. }
  348. $options[$k]['text'] = $text;
  349. if($v['answer'] == '1'){
  350. $correct++;
  351. }
  352. }
  353. if($correct == 0){
  354. $this->error('未设置正确答案');
  355. }
  356. if($correct > 1&& in_array($info['status'],[0,1]) &&$type == 0){
  357. $this->error('单选题只能设置一个正确答案');
  358. }
  359. $oldoptions = json_decode($question['options'],true);
  360. if(count($oldoptions) != count($options)){
  361. $this->error('选项参数错误');
  362. }
  363. $data = array(
  364. 'title' => $title,
  365. 'options' => json_encode($options,JSON_UNESCAPED_UNICODE),
  366. 'update_time' => date('Y-m-d h:i:s')
  367. );
  368. if($info['status'] == 0){
  369. $data['score'] = $score;
  370. $data['type'] = $type;
  371. $data['sort'] = $sort>0?$sort:0;
  372. }
  373. $res = db('article_question')->where('id',$id)->update($data);
  374. if($res){
  375. $this->success('操作成功');
  376. }else{
  377. $this->error('操作失败');
  378. }
  379. }else{
  380. $this->assign('info',$info);
  381. $question['options'] = json_decode($question['options'],true);
  382. $this->assign('question',$question);
  383. return $this->fetch();
  384. }
  385. }
  386. public function article_rank($id){
  387. $ret = db('article_record')
  388. ->where('article_paper_id', $id)
  389. ->field('article_record.*,max(score) as score')
  390. ->group('user_id')
  391. ->order(['score'=>'desc','create_time'=>'asc'])
  392. ->select();
  393. foreach ($ret as $key => $value) {
  394. $ret[$key]['article_paper_name'] = db('article_paper')
  395. ->where('id',$value['article_paper_id'])
  396. ->value('title');
  397. $ret[$key]['user_name'] = db('user')
  398. ->where('id',$value['user_id'])
  399. ->value('real_name');
  400. }
  401. $this->assign('list',$ret);
  402. return $this->fetch();
  403. }
  404. public function article_log($id){
  405. if(request()->isAjax()){
  406. //分页参数
  407. $length = input('rows',10,'intval'); //每页条数
  408. $page = input('page',1,'intval'); //第几页
  409. $start = ($page - 1) * $length; //分页开始位置
  410. //排序
  411. $sortRow = input('sidx','id','trim'); //排序列
  412. $sort = input('sord','desc','trim'); //排序方式
  413. $order = $sortRow.' '.$sort;
  414. $title = input('title','','trim');
  415. if($title){
  416. $map[] = ['title','like','%'.$title.'%'];
  417. }
  418. $enable = input('enable','','trim');
  419. if($enable != ''){
  420. $map[] = ['enable','=',$enable];
  421. }
  422. $map[] = ['article_paper_id','=',$id];
  423. $map= empty($map) ? true: $map;
  424. //数据查询
  425. $lists = db('article_record')->where($map)->limit($start,$length)->order($order)->select();
  426. foreach ($lists as $key => $value) {
  427. $lists[$key]['article_paper_name'] = db('article_paper')
  428. ->where('id',$value['article_paper_id'])
  429. ->value('title');
  430. $lists[$key]['user_name'] = db('user')
  431. ->where('id',$value['user_id'])
  432. ->value('real_name');
  433. }
  434. //数据返回
  435. $totalCount = db('article_record')->where($map)->count();
  436. $totalPage = ceil($totalCount/$length);
  437. $result['page'] = $page;
  438. $result['total'] = $totalPage;
  439. $result['records'] = $totalCount;
  440. $result['rows'] = $lists;
  441. return json($result);
  442. }else{
  443. $info = db('article_paper')
  444. ->where('id',$id)
  445. ->find();
  446. $this->assign('m_name','['.$info['title'].']考试记录');
  447. $this->assign('id',$id);
  448. return $this->fetch();
  449. }
  450. }
  451. public function log_info($id){
  452. $list = $this->show_detail_info($id);
  453. $info = $this->show_info($id);
  454. $this->assign('list',$list);
  455. $this->assign('info',$info);
  456. return $this->fetch();
  457. }
  458. public function show_detail_info($id)
  459. {
  460. $ret = db('article_record_answer')
  461. ->alias('a')
  462. ->field('c.*,a.answer,b.user_id,b.article_paper_id')
  463. ->join('article_record b','b.id = a.article_record_id')
  464. ->join('article_question c','c.id = a.article_question_id')
  465. ->where('a.article_record_id', $id)
  466. ->order('c.sort', 'asc')
  467. ->select();
  468. foreach ($ret as $key => $value) {
  469. $ret[$key]['article_paper_name'] = db('article_paper')
  470. ->where('id',$value['article_paper_id'])
  471. ->value('title');
  472. $ret[$key]['user_name'] = db('user')
  473. ->where('id',$value['user_id'])
  474. ->value('real_name');
  475. $ret[$key]['options'] = json_decode($value['options'],true);
  476. }
  477. return $ret;
  478. }
  479. public function show_info($id)
  480. {
  481. $ret = db('article_record')
  482. ->where('id',$id)
  483. ->find();
  484. if ($ret) {
  485. $ret['article_paper_name'] =
  486. db('article_paper')
  487. ->where('id',$ret['article_paper_id'])
  488. ->value('title');
  489. $ret['user_name'] =
  490. db('user')
  491. ->where('id',$ret['user_id'])
  492. ->value('real_name');
  493. }
  494. return $ret;
  495. }
  496. }