Paper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\enum\PaperMode;
  4. use app\admin\model\exam\PaperQuestionModel;
  5. use app\admin\model\exam\QuestionModel;
  6. use app\common\controller\Backend;
  7. use think\Db;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use app\common\model\Usergangwei;
  11. use fast\Tree;
  12. /**
  13. * 试卷
  14. * @icon fa fa-circle-o
  15. */
  16. class Paper extends Backend
  17. {
  18. /**
  19. * PaperModel模型对象
  20. * @var \app\admin\model\exam\PaperModel
  21. */
  22. protected $model = null;
  23. protected $noNeedRight = ['nograde','selectuser'];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\exam\PaperModel;
  28. $this->view->assign("modeList", $this->model->getModeList());
  29. $this->view->assign("kindList", $this->model->getKindList());
  30. $this->view->assign("statusList", $this->model->getStatusList());
  31. //岗位
  32. $tree = Tree::instance();
  33. $tree->init(Usergangwei::getCategoryArray(), 'pid');
  34. $gangweilist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  35. $this->view->assign("gangweilist", $gangweilist);
  36. }
  37. /**
  38. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  39. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  40. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  41. */
  42. /**
  43. * 查看
  44. */
  45. public function index()
  46. {
  47. //当前是否为关联查询
  48. $this->relationSearch = true;
  49. //设置过滤方法
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if ($this->request->isAjax()) {
  52. //如果发送的来源是Selectpage,则转发到Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  57. $list = $this->model
  58. ->with(['cate'])
  59. ->where($where)
  60. ->order($sort, $order)
  61. ->paginate($limit);
  62. foreach ($list as $row) {
  63. $row->getRelation('cate')->visible(['name']);
  64. }
  65. $result = array("total" => $list->total(), "rows" => $list->items());
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 添加
  72. */
  73. public function add()
  74. {
  75. if ($this->request->isPost()) {
  76. $params = $this->request->post("row/a");
  77. if ($params) {
  78. $params = $this->preExcludeFields($params);
  79. $this->valid($params);
  80. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  81. $params[$this->dataLimitField] = $this->auth->id;
  82. }
  83. $result = false;
  84. Db::startTrans();
  85. try {
  86. //是否采用模型验证
  87. if ($this->modelValidate) {
  88. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  89. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  90. $this->model->validateFailException(true)->validate($validate);
  91. }
  92. // dd($params);
  93. $result = $this->model->allowField(true)->save($params);
  94. // 保存试卷固定题目
  95. $this->saveFixQuestion($this->model, $params);
  96. Db::commit();
  97. } catch (ValidateException $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. } catch (PDOException $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. $this->error($e->getMessage());
  106. }
  107. if ($result !== false) {
  108. $this->success();
  109. } else {
  110. $this->error(__('No rows were inserted'));
  111. }
  112. }
  113. $this->error(__('Parameter %s can not be empty', ''));
  114. }
  115. return $this->view->fetch();
  116. }
  117. /**
  118. * 编辑
  119. */
  120. public function edit($ids = null)
  121. {
  122. $row = $this->model->get($ids);
  123. if (!$row) {
  124. $this->error(__('No Results were found'));
  125. }
  126. $adminIds = $this->getDataLimitAdminIds();
  127. if (is_array($adminIds)) {
  128. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  129. $this->error(__('You have no permission'));
  130. }
  131. }
  132. if ($this->request->isPost()) {
  133. $params = $this->request->post("row/a");
  134. if ($params) {
  135. $this->valid($params);
  136. $params = $this->preExcludeFields($params);
  137. $result = false;
  138. Db::startTrans();
  139. try {
  140. //是否采用模型验证
  141. if ($this->modelValidate) {
  142. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  143. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  144. $row->validateFailException(true)->validate($validate);
  145. }
  146. $result = $row->allowField(true)->save($params);
  147. // 保存试卷固定题目
  148. $this->saveFixQuestion($row, $params, 'edit');
  149. Db::commit();
  150. } catch (ValidateException $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. } catch (PDOException $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. } catch (\Exception $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. }
  160. if ($result !== false) {
  161. $this->success();
  162. } else {
  163. $this->error(__('No rows were updated'));
  164. }
  165. }
  166. $this->error(__('Parameter %s can not be empty', ''));
  167. }
  168. if ($row['mode'] == PaperMode::FIX) {
  169. $row['questions'] = QuestionModel::getFixListByPaper($row['id'], ['cates']);
  170. }
  171. $this->view->assign("row", $row);
  172. $this->view->assign("configs", json_decode($row['configs'], true));
  173. return $this->view->fetch();
  174. }
  175. /**
  176. * 验证参数
  177. * @param $params
  178. * @return void
  179. */
  180. protected function valid(&$params)
  181. {
  182. if ($params['pass_score'] > $params['total_score']) {
  183. $this->error('及格分数不能大于总分');
  184. }
  185. $params['start_time'] = $params['start_time'] ?: 0;
  186. $params['end_time'] = $params['end_time'] ?: 0;
  187. if ($params['start_time']) {
  188. if ($params['start_time'] > $params['end_time']) {
  189. $this->error('开始时间不能大于结束时间');
  190. }
  191. // if (strtotime($params['start_time']) < time()) {
  192. // $this->error('开始时间不能小于当前时间');
  193. // }
  194. }
  195. if ($params['end_time']) {
  196. if (!$params['start_time']) {
  197. $this->error('请先选择开始时间');
  198. }
  199. }
  200. // 固定选题模式
  201. if ($params['mode'] == 'FIX') {
  202. $params['questions'] = json_decode($params['questions'], true);
  203. if (!$params['questions']) {
  204. $this->error('请先选择题目');
  205. }
  206. if (count($params['questions']) < $params['quantity']) {
  207. $this->error('题目数量不能大于题目总数');
  208. }
  209. }
  210. $limit_time = 0;
  211. if ($params['limit_time_hour']) {
  212. $limit_time += $params['limit_time_hour'] * 60 * 60;
  213. }
  214. if ($params['limit_time_minute']) {
  215. $limit_time += $params['limit_time_minute'] * 60;
  216. }
  217. $params['limit_time'] = $limit_time;
  218. }
  219. /**
  220. * 保存固定选题
  221. * @param $paper
  222. * @param $params
  223. * @return void
  224. */
  225. public function saveFixQuestion($paper, $params, $method = 'add')
  226. {
  227. if ($paper['mode'] != 'FIX') {
  228. return;
  229. }
  230. if ($method == 'edit') {
  231. PaperQuestionModel::where('paper_id', $paper['id'])->delete();
  232. }
  233. $questions = $params['questions'];
  234. $data = [];
  235. foreach ($questions as $key => $question) {
  236. // $item = [
  237. // 'paper_id' => $paper['id'],
  238. // 'question_id' => $question['id'],
  239. // 'score' => $question['score'],
  240. // 'sort' => $key + 1,
  241. // 'createtime' => time(),
  242. // ];
  243. //
  244. // if ($question['kind'] == 'SHORT') {
  245. // $item['answer'] = $question['answer'];
  246. // }
  247. // $data[] = $item;
  248. $data[] = [
  249. 'paper_id' => $paper['id'],
  250. 'question_id' => $question['id'],
  251. 'score' => $question['score'],
  252. 'answer_config' => is_array($question['answer']) ? json_encode($question['answer'], JSON_UNESCAPED_UNICODE) : $question['answer'],
  253. 'sort' => $key + 1,
  254. 'createtime' => time(),
  255. ];
  256. }
  257. (new PaperQuestionModel())->saveAll($data);
  258. }
  259. /**
  260. * 缺考用户
  261. */
  262. public function nograde(){
  263. $id = input('id');
  264. $info = Db::name('exam_paper')->where('id',$id)->find();
  265. $user_ids = $info['user_ids'];
  266. $grade_uids = Db::name('exam_grade')->where('paper_id',$id)->where('status',2)->column('user_id');
  267. $lists = Db::name('user')->where('id','IN',$user_ids)->where('id','NOTIN',$grade_uids)->select();
  268. $this->assign('lists',$lists);
  269. return $this->view->fetch();
  270. }
  271. /**
  272. * 选人
  273. */
  274. public function selectuser(){
  275. $id = input('id');
  276. if($this->request->isPost()){
  277. $params = $this->request->post('row/a');
  278. $user_rule = explode(',',$params['user_rule']);
  279. $user_ids = [];
  280. foreach($user_rule as $key => $val){
  281. if(strpos($val,'u_') !== false){
  282. $user_ids[] = substr($val,2);
  283. }
  284. }
  285. $user_ids = implode(',',$user_ids);
  286. $update = [
  287. 'user_ids' => $user_ids,
  288. 'user_rule' => $params['user_rule'],
  289. ];
  290. Db::name('exam_paper')->where('id',$id)->update($update);
  291. $this->success('设置成功');
  292. }
  293. $id = input('id');
  294. $row = Db::name('exam_paper')->where('id',$id)->find();
  295. $user_rule = explode(',', $row['user_rule']);
  296. $nodeList = \app\admin\model\User::getTreeList($user_rule);
  297. $this->assign("nodeList", $nodeList);
  298. $this->assign('row',$row);
  299. return $this->view->fetch();
  300. }
  301. }