Questions.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\questions;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\merchant\model\questions\QuestionsCategpry as QuestionsCategpryModel;
  15. use service\PhpSpreadsheetService;
  16. use service\SystemConfigService;
  17. use \PhpOffice\PhpSpreadsheet\IOFactory;
  18. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  19. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  20. /**
  21. * 试题 Model
  22. * Class Questions
  23. * @package app\admin\model\questions
  24. */
  25. class Questions extends ModelBasic
  26. {
  27. use ModelTrait;
  28. /**条件处理
  29. * @param $where
  30. * @param array $arrays
  31. * @return Questions
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \think\exception\DbException
  35. */
  36. public static function setWhere($where, $arrays = [])
  37. {
  38. $model = self::order('sort desc,add_time desc')->where('is_del', 0);
  39. if (isset($where['type']) && $where['type']) $model = $model->where('question_type', $where['type']);
  40. if (isset($where['pid']) && $where['pid'] > 0) {
  41. $cate = QuestionsCategpryModel::where('id', $where['pid'])->find();
  42. if ($cate['pid'] > 0) {
  43. $model = $model->where('pid', $where['pid']);
  44. } else {
  45. $pids = QuestionsCategpryModel::categoryId($where['pid']);
  46. $model = $model->where('pid', 'in', $pids);
  47. }
  48. }
  49. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where('mer_id', $where['mer_id']);
  50. if ($arrays) $model = $model->where('id', 'not in', $arrays);
  51. if (isset($where['title']) && $where['title'] != '') $model = $model->where('stem', 'like', "%$where[title]%");
  52. return $model;
  53. }
  54. /**试题列表
  55. * @param $where
  56. * @param array $arrays
  57. */
  58. public static function questionsList($where, $arrays = [])
  59. {
  60. $model = self::setWhere($where, $arrays);
  61. if (isset($where['excel']) && $where['excel'] == 1) {
  62. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  63. } else {
  64. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  65. }
  66. foreach ($data as $key => &$value) {
  67. switch ($value['question_type']) {
  68. case 1:
  69. $value['types'] = '单选题';
  70. break;
  71. case 2:
  72. $value['types'] = '多选题';
  73. break;
  74. case 3:
  75. $value['types'] = '判断题';
  76. break;
  77. }
  78. $value['cate'] = QuestionsCategpryModel::where(['id' => $value['pid'], 'mer_id' => $where['mer_id'], 'is_del' => 0])->value('title');
  79. }
  80. if (isset($where['excel']) && $where['excel'] == 1) {
  81. self::SaveExcel($data);
  82. }
  83. $count = self::setWhere($where, $arrays)->count();
  84. return compact('data', 'count');
  85. }
  86. /**
  87. * 保存并下载excel
  88. * $list array
  89. * return
  90. */
  91. public static function SaveExcel($list)
  92. {
  93. $export = [];
  94. foreach ($list as $index => $item) {
  95. $export[] = [
  96. $item['id'],
  97. $item['cate'],
  98. $item['stem'],
  99. $item['image'],
  100. $item['types'],
  101. $item['option'],
  102. $item['answer'],
  103. $item['difficulty'],
  104. $item['analysis'],
  105. $item['number'],
  106. $item['add_time'] > 0 ? date('Y/m/d H:i', $item['add_time']) : '无'
  107. ];
  108. }
  109. $filename = '试题列表' . time() . '.xlsx';
  110. $head = ['编号', '分类', '题干', '配图', '提型', '选项', '答案', '难度', '答案解析', '答题人数', '添加时间'];
  111. PhpSpreadsheetService::outdata($filename, $export, $head);
  112. }
  113. /**
  114. * 下载试题导入文件
  115. */
  116. public static function getExcel()
  117. {
  118. $filename = 'template.xlsx'; //获取文件名称
  119. $site_url = SystemConfigService::get('site_url');//网站地址
  120. if (!$site_url) {
  121. $site_url = 'http';
  122. if ($_SERVER["HTTPS"] == "on") {
  123. $site_url .= 's';
  124. }
  125. $site_url = $site_url . '://' . $_SERVER['HTTP_HOST'] . '/'; //当前域名
  126. } else {
  127. $site_url .= '/';
  128. }
  129. //判断如果文件存在,则跳转到下载路径
  130. if (file_exists(ROOT_PATH . 'public' . DS . $filename)) {
  131. header('location:' . $site_url . $filename);
  132. exit;
  133. } else {
  134. header('HTTP/1.1 404 Not Found');
  135. }
  136. }
  137. /**文件导入
  138. * @param string $filename
  139. * @param int $startLine
  140. * @param array $width
  141. * @return array
  142. * @throws \PHPExcel_Exception
  143. * @throws \PHPExcel_Reader_Exception
  144. */
  145. public static function GetExcelData($filename = '1.xlsx', $startLine = 4)
  146. {
  147. $width = [
  148. 'question_type' => 'A',
  149. 'pid' => 'B',
  150. 'stem' => 'C',
  151. 'image' => 'D',
  152. 'is_img' => 'E',
  153. 'a' => 'F',
  154. 'b' => 'G',
  155. 'c' => 'H',
  156. 'd' => 'I',
  157. 'e' => 'J',
  158. 'f' => 'K',
  159. 'answer' => 'L',
  160. 'difficulty' => 'M',
  161. 'analysis' => 'N',
  162. 'sort' => 'O'
  163. ];
  164. $filename = ROOT_PATH . 'public' . $filename;
  165. $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  166. switch ($extension) {
  167. case 'xlsx':
  168. $reader = IOFactory::createReader('Xlsx');
  169. $spreadsheet = $reader->load($filename);
  170. break;
  171. case 'xls':
  172. $reader = IOFactory::createReader('Xls');
  173. $spreadsheet = $reader->load($filename);
  174. break;
  175. case 'csv':
  176. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
  177. $reader->setInputEncoding('GBK');
  178. $reader->setDelimiter(',');
  179. $reader->setEnclosure('');
  180. $reader->setSheetIndex(0);
  181. $spreadsheet = $reader->load($filename);
  182. break;
  183. }
  184. $highestRow = $spreadsheet->getSheet(0)->getHighestRow(); // 取得总行数
  185. $getvalue = $spreadsheet->getActiveSheet();
  186. $data = [];
  187. for ($j = $startLine; $j <= (int)$highestRow; $j++) {
  188. $value = [];
  189. foreach ($width as $key => $val) {
  190. if ($v = $getvalue->getCell($val . $j)->getValue()) $value[$key] = $v;
  191. else $value[$key] = '';
  192. }
  193. if ($value) $data[] = $value;
  194. }
  195. return $data;
  196. }
  197. /**返回分类下试题的ID
  198. * @param $question_type
  199. * @param array $cateIds
  200. * @return array
  201. */
  202. public static function getCateIds($question_type, $cateIds = [])
  203. {
  204. $question = self::where(['question_type' => $question_type, 'is_del' => 0])->where('pid', 'in', $cateIds)->field('id,question_type')->select();
  205. if ($question) return $question->toArray();
  206. else return $question;
  207. }
  208. /**批量导入试题
  209. * @param array $data
  210. */
  211. public static function importQuestions($data = [], $mer_id)
  212. {
  213. foreach ($data as $key => $value) {
  214. $dat = [];
  215. switch ($value['question_type']) {
  216. case 1:
  217. if ($value['a']) $dat['A'] = $value['a'];
  218. if ($value['b']) $dat['B'] = $value['b'];
  219. if ($value['c']) $dat['C'] = $value['c'];
  220. if ($value['d']) $dat['D'] = $value['d'];
  221. case 2:
  222. if ($value['a']) $dat['A'] = $value['a'];
  223. if ($value['b']) $dat['B'] = $value['b'];
  224. if ($value['c']) $dat['C'] = $value['c'];
  225. if ($value['d']) $dat['D'] = $value['d'];
  226. if ($value['e']) $dat['E'] = $value['e'];
  227. if ($value['f']) $dat['F'] = $value['f'];
  228. break;
  229. case 3:
  230. if ($value['a']) $dat['A'] = $value['a'];
  231. if ($value['b']) $dat['B'] = $value['b'];
  232. break;
  233. }
  234. $array['question_type'] = $value['question_type'];
  235. $array['pid'] = $value['pid'];
  236. $array['stem'] = $value['stem'];
  237. $array['image'] = $value['image'];
  238. $array['is_img'] = $value['is_img'];
  239. $array['answer'] = trim($value['answer'], " ");
  240. $array['difficulty'] = $value['difficulty'];
  241. $array['analysis'] = $value['analysis'];
  242. $array['sort'] = (int)$value['sort'];
  243. $array['option'] = json_encode($dat);
  244. $array['add_time'] = time();
  245. $array['mer_id'] = $mer_id;
  246. if (self::be(['stem' => $value['stem'], 'mer_id' => $mer_id, 'is_del' => 0])) continue;
  247. self::set($array);
  248. }
  249. return true;
  250. }
  251. }