Questionnaire.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class Questionnaire extends Auth
  6. {
  7. public function __construct(App $app = null) {
  8. parent::__construct($app);
  9. $this->model= new \app\common\model\Questionnaire();
  10. $this->table= $this->model->table;
  11. $this->table1= 'questionnaire_record';
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $title = input('title','','trim');
  24. if($title){
  25. $map[] = ['title','like','%'.$title.'%'];
  26. }
  27. $enable = input('enable','','trim');
  28. if($enable != ''){
  29. $map[] = ['enable','=',$enable];
  30. }
  31. $map[] = ['del','=',0];
  32. $map[] = ['org_id','=',$this->orgId];
  33. $map= empty($map) ? true: $map;
  34. //数据查询
  35. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  36. foreach ($lists as $k=>$v){
  37. $lists[$k]['records'] = db('questionnaire_record')
  38. ->where('questionnaire_id',$v['id'])
  39. ->count();
  40. }
  41. //数据返回
  42. $totalCount = db($this->table)->where($map)->count();
  43. $totalPage = ceil($totalCount/$length);
  44. $result['page'] = $page;
  45. $result['total'] = $totalPage;
  46. $result['records'] = $totalCount;
  47. $result['rows'] = $lists;
  48. return json($result);
  49. }else{
  50. $this->assign('m_name','问卷调查列表');
  51. return $this->fetch();
  52. }
  53. }
  54. public function record($id){
  55. if(request()->isAjax()){
  56. //分页参数
  57. $length = input('rows',10,'intval'); //每页条数
  58. $page = input('page',1,'intval'); //第几页
  59. $start = ($page - 1) * $length; //分页开始位置
  60. //排序
  61. $sortRow = input('sidx','id','trim'); //排序列
  62. $sort = input('sord','desc','trim'); //排序方式
  63. $order = $sortRow.' '.$sort;
  64. $title = input('title','','trim');
  65. if($title){
  66. $map[] = ['title','like','%'.$title.'%'];
  67. }
  68. $enable = input('enable','','trim');
  69. if($enable != ''){
  70. $map[] = ['enable','=',$enable];
  71. }
  72. $map[] = ['questionnaire_id','=',$id];
  73. $map= empty($map) ? true: $map;
  74. //数据查询
  75. $lists = db($this->table1)->where($map)->limit($start,$length)->order($order)->select();
  76. //数据返回
  77. $totalCount = db($this->table1)->where($map)->count();
  78. $totalPage = ceil($totalCount/$length);
  79. $result['page'] = $page;
  80. $result['total'] = $totalPage;
  81. $result['records'] = $totalCount;
  82. $result['rows'] = $lists;
  83. return json($result);
  84. }else{
  85. $info = db($this->table)
  86. ->where('id',$id)
  87. ->find();
  88. $this->assign('m_name','['.$info['title'].']'.'回收记录');
  89. $this->assign('id',$id);
  90. return $this->fetch();
  91. }
  92. }
  93. /**
  94. * 新增/编辑
  95. */
  96. public function add($id=0){
  97. if(request()->isPost()){
  98. $question = input('question');
  99. $title = input('title');
  100. $description = input('description');
  101. if(!$question){
  102. $this->error('未设置题目');
  103. }
  104. if(!$title){
  105. $this->error('未设置名称');
  106. }
  107. $data = [
  108. 'title' => $title,
  109. 'description' => $description,
  110. 'questions' => $question,
  111. 'enable' => 0,
  112. 'user_id' => $this->userId,
  113. 'create_time' => getTime(),
  114. 'org_id' => $this->orgId
  115. ];
  116. $ret = db($this->table)->insertGetId($data);
  117. if($ret){
  118. $this->success('操作成功');
  119. }else{
  120. $this->error('操作失败');
  121. }
  122. }else{
  123. return $this->fetch();
  124. }
  125. }
  126. public function share($id){
  127. $info =db($this->table)
  128. ->where('id',$id)
  129. ->find();
  130. $strs = aes_encrypt('wj',config('app.encryption_key'));
  131. $code = getSite().'/h5/Wj/index?id='.$id.'&code='.$strs.'&orgId='.$info['org_id'];
  132. $this->assign('url',$code);
  133. return $this->fetch();
  134. }
  135. /**
  136. * 删除记录
  137. * @param int $id
  138. */
  139. public function del($id=0){
  140. if(!$id){
  141. $this->error('参数错误');
  142. }
  143. $res = db($this->table)->where('id',$id)->setField('del',1);
  144. if($res){
  145. $this->success('删除成功');
  146. }else{
  147. $this->error('删除失败');
  148. }
  149. }
  150. public function fb($id=0,$status=0){
  151. $enable = $status==1?0:1;
  152. $res = db($this->table)->where('id',$id)->setField('enable',$enable);
  153. if($res){
  154. $this->success('操作成功');
  155. }else{
  156. $this->error('操作失败');
  157. }
  158. }
  159. /**
  160. * 改变字段值
  161. * @param int $fv
  162. * @param string $fn
  163. * @param int $fv
  164. */
  165. public function changeField($id=0,$fn='',$fv=0){
  166. if(!$fn||!$id){
  167. $this->error('参数错误');
  168. }
  169. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  170. if($res){
  171. $this->success('操作成功');
  172. }else{
  173. $this->error('操作失败');
  174. }
  175. }
  176. //回收记录详情
  177. public function info($id){
  178. $info = $this->model->record_info($id);
  179. if (!$info) {
  180. exit('数据不存在');
  181. }
  182. $this->assign('info',$info);
  183. return $this->fetch();
  184. }
  185. // 统计
  186. public function tj($id = 0){
  187. $info = Db::name('questionnaire')
  188. ->where('id',$id)
  189. ->where('del',0)
  190. ->find();
  191. if(!$info){
  192. $this->error('问卷不存在');
  193. }
  194. $questions = json_decode($info['questions'],true);
  195. $total = Db::name('questionnaire_record')
  196. ->where('questionnaire_id',$id)
  197. ->count();
  198. foreach ($questions as $k=>$v){ // 量表题
  199. if($v['type'] == 'star'){
  200. $answer = [];
  201. for($i=$v['star_val_min'];$i<=$v['star_val_max'];$i++){
  202. $count = Db::name('questionnaire_record_answer')
  203. ->where('question_id',$v['id'])
  204. ->where('questionnaire_id',$id)
  205. ->where('answer',$i)
  206. ->count();
  207. $bl = $total>0?round($count/$total,2)*100:0;
  208. $answer[] = [
  209. 'id' => $i,
  210. 'text' => $i,
  211. 'count' => $count,
  212. 'count1' => $bl.'%'
  213. ];
  214. }
  215. $questions[$k]['answer'] = $answer;
  216. }else if($v['type'] == 'radio' || $v['type'] == 'checkbox'){ // 单选或多项
  217. $answer = [];
  218. foreach ($v['options'] as $kk=>$vv){
  219. $map = [];
  220. $map[] = ['question_id','=',$v['id']];
  221. $map[] = ['questionnaire_id','=',$id];
  222. if($v['type'] == 'radio'){
  223. $map[] = ['answer','=',$vv['id']];
  224. }else{
  225. $map[] = ['','exp', Db::raw("FIND_IN_SET({$vv['id']},answer)")];
  226. }
  227. $count = Db::name('questionnaire_record_answer')
  228. ->where($map)
  229. ->count();
  230. $bl = $total>0?round($count/$total,2)*100:0;
  231. $answer[] = [
  232. 'id' => $vv['id'],
  233. 'text' => $vv['text'],
  234. 'count' => $count,
  235. 'count1' => $bl.'%'
  236. ];
  237. }
  238. $questions[$k]['answer'] = $answer;
  239. }else{ // 单行文本题/多行文本题
  240. $answer = Db::name('questionnaire_record_answer')
  241. ->where('question_id',$v['id'])
  242. ->where('questionnaire_id',$id)
  243. ->count();
  244. $questions[$k]['answer'] = $answer;
  245. }
  246. }
  247. $info['questions'] = $questions;
  248. $this->assign('info',$info);
  249. return $this->fetch();
  250. }
  251. public function tjExport($id){
  252. $info = Db::name('questionnaire')
  253. ->where('id',$id)
  254. ->where('del',0)
  255. ->find();
  256. if(!$info){
  257. $this->error('问卷不存在');
  258. }
  259. $questions = json_decode($info['questions'],true);
  260. $total = Db::name('questionnaire_record')
  261. ->where('questionnaire_id',$id)
  262. ->count();
  263. foreach ($questions as $k=>$v){ // 量表题
  264. if($v['type'] == 'star'){
  265. $answer = [];
  266. for($i=$v['star_val_min'];$i<=$v['star_val_max'];$i++){
  267. $count = Db::name('questionnaire_record_answer')
  268. ->where('question_id',$v['id'])
  269. ->where('questionnaire_id',$id)
  270. ->where('answer',$i)
  271. ->count();
  272. $bl = $total>0?round($count/$total,2)*100:0;
  273. $answer[] = [
  274. 'id' => $i,
  275. 'text' => $i,
  276. 'count' => $count,
  277. 'count1' => $bl.'%'
  278. ];
  279. }
  280. $questions[$k]['answer'] = $answer;
  281. }else if($v['type'] == 'radio' || $v['type'] == 'checkbox'){ // 单选或多项
  282. $answer = [];
  283. foreach ($v['options'] as $kk=>$vv){
  284. $map = [];
  285. $map[] = ['question_id','=',$v['id']];
  286. $map[] = ['questionnaire_id','=',$id];
  287. if($v['type'] == 'radio'){
  288. $map[] = ['answer','=',$vv['id']];
  289. }else{
  290. $map[] = ['','exp', Db::raw("FIND_IN_SET({$vv['id']},answer)")];
  291. }
  292. $count = Db::name('questionnaire_record_answer')
  293. ->where($map)
  294. ->count();
  295. $bl = $total>0?round($count/$total,2)*100:0;
  296. $answer[] = [
  297. 'id' => $vv['id'],
  298. 'text' => $vv['text'],
  299. 'count' => $count,
  300. 'count1' => $bl.'%'
  301. ];
  302. }
  303. $questions[$k]['answer'] = $answer;
  304. }else{ // 单行文本题/多行文本题
  305. $answer = Db::name('questionnaire_record_answer')
  306. ->where('question_id',$v['id'])
  307. ->where('questionnaire_id',$id)
  308. ->count();
  309. $questions[$k]['answer'] = $answer;
  310. $questions[$k]['answer_list'] = Db::name('questionnaire_record_answer')
  311. ->where('question_id',$v['id'])
  312. ->where('questionnaire_id',$id)
  313. ->select();
  314. }
  315. }
  316. $data = [];
  317. foreach ($questions as $k=>$v){
  318. $data[] = [
  319. 'title'=>$v['title'],
  320. 'title1'=>'',
  321. 'num'=>is_array($v['answer'])?'':$v['answer'],
  322. 'bl'=>'',
  323. 'answer_list'=>is_array($v['answer'])?[]:$v['answer_list'],
  324. ];
  325. if(is_array($v['answer'])){
  326. foreach ($v['answer'] as $k1=>$v1){
  327. $data[] = [
  328. 'title'=>'',
  329. 'title1'=>$v1['text'],
  330. 'num'=>$v1['count'],
  331. 'bl'=>$v1['count1'],
  332. ];
  333. }
  334. }else if (is_array($v['answer_list']) && !empty($v['answer_list'])){
  335. foreach ($v['answer_list'] as $k2=>$v2){
  336. $data[] = [
  337. 'title'=>'',
  338. 'title1'=>$v2['answer'],
  339. 'num'=>'',
  340. 'bl'=>'',
  341. ];
  342. }
  343. }
  344. }
  345. include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
  346. $objPHPExcel = new \PHPExcel();
  347. //激活当前的sheet表
  348. $objPHPExcel->setActiveSheetIndex(0);
  349. //设置表格头(即excel表格的第一行)
  350. $objPHPExcel->setActiveSheetIndex(0)
  351. ->setCellValue('A1', '标题')
  352. ->setCellValue('B1', '答案')
  353. ->setCellValue('C1', '数量')
  354. ->setCellValue('D1', '比例');
  355. //循环刚取出来的数组,将数据逐一添加到excel表格。
  356. for ($i = 0; $i < count($data); $i++) {
  357. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $data[$i]['title']);
  358. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $data[$i]['title1']);
  359. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $data[$i]['num']);
  360. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $data[$i]['bl']);
  361. }
  362. //设置保存的Excel表格名称
  363. $filename = $info['title'].'问卷统计' . date('YmdHis', time()) . '.xls';
  364. //设置当前激活的sheet表格名称
  365. $objPHPExcel->getActiveSheet()->setTitle('问卷统计');
  366. //设置浏览器窗口下载表格
  367. ob_end_clean();
  368. header("Content-Type: application/force-download");
  369. header("Content-Type: application/octet-stream");
  370. header("Content-Type: application/download");
  371. header('Content-Disposition:inline;filename="' . $filename);
  372. //生成excel文件
  373. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  374. //下载文件在浏览器窗口
  375. return $objWriter->save('php://output');
  376. }
  377. public function answer($id=0,$qid=0){
  378. if(request()->isAjax()){
  379. //分页参数
  380. $length = input('rows',10,'intval'); //每页条数
  381. $page = input('page',1,'intval'); //第几页
  382. $start = ($page - 1) * $length; //分页开始位置
  383. //排序
  384. $sortRow = input('sidx','id','trim'); //排序列
  385. $sort = input('sord','desc','trim'); //排序方式
  386. $order = 'id desc';
  387. $title = input('title','','trim');
  388. if($title){
  389. $map[] = ['b.real_name','like','%'.$title.'%'];
  390. }
  391. $map[] = ['a.questionnaire_id','=',$id];
  392. $map[] = ['a.question_id','=',$qid];
  393. $map= empty($map) ? true: $map;
  394. //数据查询
  395. $lists = Db::name('questionnaire_record_answer')
  396. ->alias('a')
  397. ->join('questionnaire_record c','c.id = a.questionnaire_record_id')
  398. ->join('user b','b.id = c.user_id','left')
  399. ->where($map)
  400. ->field('a.*,b.real_name as name,c.create_time,c.user_id')
  401. ->limit($start,$length)
  402. ->order($order)
  403. ->select();
  404. foreach ($lists as $k=>$v){
  405. $lists[$k]['name'] = $v['user_id']>0?$v['name']:'游客';
  406. }
  407. //数据返回
  408. $totalCount = Db::name('questionnaire_record_answer')
  409. ->alias('a')
  410. ->join('questionnaire_record c','c.id = a.questionnaire_record_id')
  411. ->join('user b','b.id = c.user_id','left')
  412. ->where($map)->count();
  413. $totalPage = ceil($totalCount/$length);
  414. $result['page'] = $page;
  415. $result['total'] = $totalPage;
  416. $result['records'] = $totalCount;
  417. $result['rows'] = $lists;
  418. return json($result);
  419. }else{
  420. $this->assign('id',$id);
  421. $this->assign('qid',$qid);
  422. return $this->fetch();
  423. }
  424. }
  425. }