Paper.php 11 KB

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