Collection.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\department\Department as DepartmentModel;
  9. /**
  10. * sim-考试集合管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Collection 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->assign('sim_sim_type', ConfigModel::getSimTypeList());
  28. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  29. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  30. $this->whereExtend['exam_collection_type'] = 3;
  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. $params = $this->preExcludeFields($params);
  50. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  51. $params[$this->dataLimitField] = $this->auth->id;
  52. }
  53. $result = false;
  54. Db::startTrans();
  55. try {
  56. $depart_ids = $params['depart_ids'];
  57. //是否采用模型验证
  58. if ($this->modelValidate) {
  59. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  60. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  61. $this->model->validateFailException()->validate($validate);
  62. }
  63. $result = $this->model->allowField(true)->save($params);
  64. foreach(explode(',',$depart_ids) as $item){
  65. if(!empty($item)){
  66. $arr = [
  67. 'exam_collection_id' => $this->model->exam_collection_id,
  68. 'dept_id' => $item,
  69. 'update_time' => date('Y-m-d H:i:s'),
  70. ];
  71. Db::name('real_exam_collection_dept')->insert($arr);
  72. }
  73. }
  74. Db::commit();
  75. } catch (ValidateException|PDOException|Exception $e) {
  76. Db::rollback();
  77. $this->error($e->getMessage());
  78. }
  79. if ($result === false) {
  80. $this->error(__('No rows were inserted'));
  81. }
  82. $this->success();
  83. }
  84. public function multi($ids = null)
  85. {
  86. if (false === $this->request->isPost()) {
  87. $this->error(__('Invalid parameters'));
  88. }
  89. $ids = $ids ?: $this->request->post('ids');
  90. if (empty($ids)) {
  91. $this->error(__('Parameter %s can not be empty', 'ids'));
  92. }
  93. if (false === $this->request->has('params')) {
  94. $this->error(__('No rows were updated'));
  95. }
  96. parse_str($this->request->post('params'), $values);
  97. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  98. if (empty($values)) {
  99. $this->error(__('You have no permission'));
  100. }
  101. $adminIds = $this->getDataLimitAdminIds();
  102. if (is_array($adminIds)) {
  103. $this->model->where($this->dataLimitField, 'in', $adminIds);
  104. }
  105. // if(!empty($values['exam_collection_state'])){
  106. // if($values['exam_collection_state']==2){
  107. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_id'=>$ids])->count();
  108. // if($state_count>0){
  109. // $this->error('已有启用的考试,请先关闭');
  110. // }
  111. // }
  112. // }
  113. $count = 0;
  114. Db::startTrans();
  115. try {
  116. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  117. foreach ($list as $item) {
  118. $count += $item->allowField(true)->isUpdate(true)->save($values);
  119. }
  120. Db::commit();
  121. } catch (PDOException|Exception $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. }
  125. if ($count) {
  126. $this->success();
  127. }
  128. $this->error(__('No rows were updated'));
  129. }
  130. }