Practice.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\teacher\Exams;
  9. /**
  10. * sim-练习集合管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Practice extends Backend
  15. {
  16. /**
  17. * Collection模型对象
  18. * @var \app\admin\model\teacher\Collection
  19. */
  20. protected $model = null;
  21. protected $whereExtend = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\teacher\Collection;
  26. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  27. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  28. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  29. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  30. $this->whereExtend['exam_collection_type'] = 1;
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. public function add()
  38. {
  39. if (false === $this->request->isPost()) {
  40. return $this->view->fetch();
  41. }
  42. $params = $this->request->post('row/a');
  43. if (empty($params)) {
  44. $this->error(__('Parameter %s can not be empty', ''));
  45. }
  46. if(empty($params['depart_ids'])){
  47. $this->error('请选择区队名称');
  48. }
  49. if(!empty($params['question_setting_method'])){
  50. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  51. $this->error('出题方式为考题自选,请选择考题');
  52. }
  53. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  54. $this->error('出题方式为任务自选,请选择任务');
  55. }
  56. }
  57. $params = $this->preExcludeFields($params);
  58. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  59. $params[$this->dataLimitField] = $this->auth->id;
  60. }
  61. $result = false;
  62. Db::startTrans();
  63. try {
  64. $depart_ids = $params['depart_ids'];
  65. //是否采用模型验证
  66. if ($this->modelValidate) {
  67. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  68. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  69. $this->model->validateFailException()->validate($validate);
  70. }
  71. $result = $this->model->allowField(true)->save($params);
  72. foreach(explode(',',$depart_ids) as $item){
  73. if(!empty($item)){
  74. $arr = [
  75. 'exam_collection_id' => $this->model->exam_collection_id,
  76. 'dept_id' => $item,
  77. 'update_time' => date('Y-m-d H:i:s'),
  78. ];
  79. Db::name('real_exam_collection_dept')->insert($arr);
  80. }
  81. }
  82. Db::commit();
  83. } catch (ValidateException|PDOException|Exception $e) {
  84. Db::rollback();
  85. $this->error($e->getMessage());
  86. }
  87. if ($result === false) {
  88. $this->error(__('No rows were inserted'));
  89. }
  90. $this->success();
  91. }
  92. public function edit($ids = null)
  93. {
  94. $row = $this->model->get($ids);
  95. if (!$row) {
  96. $this->error(__('No Results were found'));
  97. }
  98. if (false === $this->request->isPost()) {
  99. $this->view->assign('row', $row);
  100. $this->assignConfig('row_info', $row);
  101. return $this->view->fetch();
  102. }
  103. $params = $this->request->post('row/a');
  104. if (empty($params)) {
  105. $this->error(__('Parameter %s can not be empty', ''));
  106. }
  107. if(!empty($params['question_setting_method'])){
  108. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  109. $this->error('出题方式为考题自选,请选择考题');
  110. }
  111. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  112. $this->error('出题方式为任务自选,请选择任务');
  113. }
  114. }
  115. $params = $this->preExcludeFields($params);
  116. $result = false;
  117. Db::startTrans();
  118. try {
  119. //是否采用模型验证
  120. if ($this->modelValidate) {
  121. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  122. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  123. $row->validateFailException()->validate($validate);
  124. }
  125. $result = $row->allowField(true)->save($params);
  126. Db::commit();
  127. } catch (ValidateException|PDOException|Exception $e) {
  128. Db::rollback();
  129. $this->error($e->getMessage());
  130. }
  131. if (false === $result) {
  132. $this->error(__('No rows were updated'));
  133. }
  134. $this->success();
  135. }
  136. public function multi($ids = null)
  137. {
  138. if (false === $this->request->isPost()) {
  139. $this->error(__('Invalid parameters'));
  140. }
  141. $ids = $ids ?: $this->request->post('ids');
  142. if (empty($ids)) {
  143. $this->error(__('Parameter %s can not be empty', 'ids'));
  144. }
  145. if (false === $this->request->has('params')) {
  146. $this->error(__('No rows were updated'));
  147. }
  148. parse_str($this->request->post('params'), $values);
  149. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  150. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  151. if (empty($values)) {
  152. $this->error(__('You have no permission'));
  153. }
  154. $adminIds = $this->getDataLimitAdminIds();
  155. if (is_array($adminIds)) {
  156. $this->model->where($this->dataLimitField, 'in', $adminIds);
  157. }
  158. // if(!empty($values['exam_collection_state'])){
  159. // if($values['exam_collection_state']==2){
  160. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>1])->count();
  161. // if($state_count>0){
  162. // $this->error('已有启用的考试,请先关闭原来的考试');
  163. // }
  164. // }
  165. // }
  166. $count = 0;
  167. Db::startTrans();
  168. try {
  169. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  170. foreach ($list as $item) {
  171. $count += $item->allowField(true)->isUpdate(true)->save($values);
  172. }
  173. Db::commit();
  174. } catch (PDOException|Exception $e) {
  175. Db::rollback();
  176. $this->error($e->getMessage());
  177. }
  178. if ($count) {
  179. $this->success();
  180. }
  181. $this->error(__('No rows were updated'));
  182. }
  183. public function persent()
  184. {
  185. //设置过滤方法
  186. $this->request->filter(['strip_tags', 'trim']);
  187. if (false === $this->request->isAjax()) {
  188. $examlist = Exams::select();
  189. foreach ($examlist as $k => $v){
  190. $examlist[$k]['score'] = Db::name('real_exam_score')->where('exam_id',$v['exam_id'])->find();
  191. }
  192. $this->view->assign('examlist', $examlist);
  193. return $this->view->fetch();
  194. }
  195. //如果发送的来源是 Selectpage,则转发到 Selectpage
  196. if ($this->request->request('keyField')) {
  197. return $this->selectpage();
  198. }
  199. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  200. $list = Db::name('real_exam_comp_request')
  201. ->where($where)->where('exam_collection_type',1)
  202. ->order($sort, $order)
  203. ->paginate($limit);
  204. unset($v);
  205. $result = ['total' => $list->total(), 'rows' => $list->items()];
  206. return json($result);
  207. }
  208. public function examing()
  209. {
  210. //设置过滤方法
  211. $this->request->filter(['strip_tags', 'trim']);
  212. if (false === $this->request->isAjax()) {
  213. return $this->view->fetch();
  214. }
  215. //如果发送的来源是 Selectpage,则转发到 Selectpage
  216. if ($this->request->request('keyField')) {
  217. return $this->selectpage();
  218. }
  219. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  220. $list = $this->model
  221. ->where($where)->where($this->whereExtend)
  222. ->order($sort, $order)
  223. ->paginate($limit);
  224. $result = ['total' => $list->total(), 'rows' => $list->items()];
  225. return json($result);
  226. }
  227. }