Questions.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\controller\questions;
  12. use app\merchant\controller\AuthController;
  13. use service\JsonService as Json;
  14. use service\UploadService as Upload;
  15. use think\Request;
  16. use think\Url;
  17. use app\merchant\model\questions\Questions as QuestionsModel;
  18. use app\merchant\model\questions\Relation;
  19. use app\merchant\model\questions\QuestionsCategpry;
  20. use app\merchant\model\questions\TestPaperQuestions;
  21. use app\merchant\model\questions\ExaminationTestRecord;
  22. /**
  23. * 试题
  24. * Class Questions
  25. */
  26. class Questions extends AuthController
  27. {
  28. /**
  29. * 题库列表
  30. */
  31. public function index()
  32. {
  33. $this->assign(['category' => QuestionsCategpry::taskCategoryAll(2, $this->merchantId)]);
  34. return $this->fetch();
  35. }
  36. /**
  37. * 获取题库列表
  38. */
  39. public function getQuestionsList()
  40. {
  41. $where = parent::getMore([
  42. ['page', 1],
  43. ['limit', 20],
  44. ['type', ''],
  45. ['pid', 0],
  46. ['title', ''],
  47. ['excel', 0]
  48. ]);
  49. $where['mer_id'] = $this->merchantId;
  50. return Json::successlayui(QuestionsModel::questionsList($where));
  51. }
  52. /**
  53. * 试题导入
  54. */
  55. public function importTestQuestions()
  56. {
  57. $where = parent::postMore([
  58. ['link', '']
  59. ]);
  60. $data = QuestionsModel::GetExcelData($where['link'], 4);
  61. if (count($data) <= 0) return Json::fail('导入文件内容为空');
  62. $res = QuestionsModel::importQuestions($data, $this->merchantId);
  63. if ($res)
  64. return Json::successful('导入成功');
  65. else
  66. return Json::fail('导入失败');
  67. }
  68. /**
  69. * 下载试题导入文件
  70. */
  71. public function downloadExcel()
  72. {
  73. QuestionsModel::getExcel();
  74. }
  75. /**
  76. * @return mixed
  77. */
  78. public function imports()
  79. {
  80. return $this->fetch('import');
  81. }
  82. /**
  83. * 上传文件
  84. * @return string
  85. */
  86. public function file_import_upload()
  87. {
  88. $res = Upload::file('file', 'config/file');
  89. if (!$res->status) return Json::fail($res->error);
  90. return Json::successful('上传成功!', ['filePath' => $res->filePath]);
  91. }
  92. /**
  93. * 快速编辑
  94. * @param string $field 字段名
  95. * @param int $id 修改的主键
  96. * @param string value 修改后的值
  97. * @return json
  98. */
  99. public function set_value($field = '', $id = '', $value = '')
  100. {
  101. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  102. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  103. $res = parent::getDataModification('questions', $id, $field, $value);
  104. if ($res)
  105. return Json::successful('保存成功');
  106. else
  107. return Json::fail('保存失败');
  108. }
  109. /**关联试题知识点
  110. * @param int $id
  111. * @return mixed
  112. */
  113. public function knowledge($id = 0)
  114. {
  115. if (!$id) Json::fail('缺少参数');
  116. $this->assign(['id' => $id]);
  117. return $this->fetch();
  118. }
  119. /**获取试题关联的专题
  120. * @param int $id
  121. * @throws \think\Exception
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. public function knowledge_points($id = 0)
  127. {
  128. if (!$id) Json::fail('缺少参数');
  129. $data = Relation::getQuestionsRelationSpecial($id, 3);
  130. foreach ($data['data'] as $k => &$v) {
  131. if ($v['type'] == 6) $v['type'] = $v['light_type'];
  132. $v['types'] = parent::specialTaskType($v['type']);
  133. }
  134. return Json::successlayui($data);
  135. }
  136. /**试题关联专题
  137. * @param int $id
  138. */
  139. public function add_knowledge_points($id, $special_ids)
  140. {
  141. if (!$id) Json::fail('缺少参数');
  142. $res = Relation::setRelations($id, $special_ids, 3);
  143. if ($res)
  144. return Json::successful('关联成功');
  145. else
  146. return Json::fail('关联失败');
  147. }
  148. /**试题知识点排序
  149. * @param int $id
  150. * @param int $special_id
  151. * @param $value
  152. */
  153. public function up_knowledge_points_sort($id, $special_id, $value)
  154. {
  155. if (!$id || !$special_id) Json::fail('缺少参数');
  156. $res = Relation::updateRelationSort($id, $special_id, 3, $value);
  157. if ($res)
  158. return Json::successful('修改成功');
  159. else
  160. return Json::fail('修改失败');
  161. }
  162. /**删除关联专题
  163. * @param int $id
  164. * @param int $special_id
  165. * @throws \think\exception\DbException
  166. */
  167. public function delete_knowledge_points($id = 0, $special_id = 0)
  168. {
  169. if (!$id || !$special_id) Json::fail('缺少参数');
  170. $res = Relation::delRelation($id, $special_id, 3);
  171. if ($res)
  172. return Json::successful('删除成功');
  173. else
  174. return Json::fail('删除失败');
  175. }
  176. /**关联专题
  177. * @param int $id
  178. */
  179. public function relation($id = 0)
  180. {
  181. if (!$id) Json::fail('缺少参数');
  182. $this->assign(['id' => $id]);
  183. return $this->fetch('relation');
  184. }
  185. /**添加/编辑
  186. * @param int $id
  187. * @return mixed
  188. */
  189. public function add($id = 0)
  190. {
  191. $questions = $id > 0 ? QuestionsModel::get($id) : [];
  192. $this->assign(['id' => $id, 'questions' => json_encode($questions)]);
  193. return $this->fetch('subject');
  194. }
  195. /**
  196. * 获取试题分类
  197. */
  198. public function get_subject_list()
  199. {
  200. $category = QuestionsCategpry::taskCategoryAll(2, $this->merchantId);
  201. return Json::successful($category);
  202. }
  203. /**添加/编辑试题
  204. * @param int $id
  205. */
  206. public function save_add($id = 0)
  207. {
  208. $data = parent::postMore([
  209. ['question_type', 1],
  210. ['is_img', 0],
  211. ['pid', 0],
  212. ['stem', ''],
  213. ['image', ''],
  214. ['option', ''],
  215. ['answer', ''],
  216. ['difficulty', ''],
  217. ['analysis', ''],
  218. ['sort', 0]
  219. ]);
  220. if ($data['pid'] <= 0) return Json::fail('请选择试题分类');
  221. if (!$data['stem']) return Json::fail('请输入试题题干');
  222. if (!$data['option']) return Json::fail('请输入试题选项');
  223. if (!$data['answer']) return Json::fail('请设置正确答案');
  224. if ($id) {
  225. $res = QuestionsModel::edit($data, $id);
  226. } else {
  227. $data['add_time'] = time();
  228. $data['mer_id'] = $this->merchantId;
  229. if (QuestionsModel::be(['stem' => $data['stem'], 'mer_id' => $this->merchantId, 'is_del' => 0])) {
  230. return Json::fail('试题题干已存在!');
  231. }
  232. $res = QuestionsModel::set($data);
  233. }
  234. if ($res) {
  235. return Json::successful('添加/编辑成功');
  236. } else {
  237. return Json::fail('添加/编辑失败');
  238. }
  239. }
  240. /**删除试题
  241. * @param int $id
  242. */
  243. public function delete($id = 0)
  244. {
  245. if (!$id) return Json::fail('参数错误');
  246. $questions = QuestionsModel::get($id);
  247. if (!$questions) return Json::fail('要删除的试题不存在');
  248. if (TestPaperQuestions::be(['questions_id' => $id])) return Json::fail('试题在试卷中使用,不能删除;请先在试卷中删除');
  249. $res = parent::getDataModification('questions', $id, 'is_del', 1);
  250. if ($res) {
  251. return Json::successful('删除成功');
  252. } else {
  253. return Json::fail('删除失败');
  254. }
  255. }
  256. /**获取答题情况
  257. * @param int $id
  258. */
  259. public function getQuestionsAnswer($id = 0)
  260. {
  261. $data = ExaminationTestRecord::testRecord($id);
  262. return Json::successful($data);
  263. }
  264. }