Collection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  8. * sim-考试集合管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Collection extends Backend
  13. {
  14. /**
  15. * Collection模型对象
  16. * @var \app\admin\model\teacher\Collection
  17. */
  18. protected $model = null;
  19. protected $whereExtend = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\teacher\Collection;
  24. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  25. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  26. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  27. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  28. $this->whereExtend['exam_collection_type'] = 3;
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. public function multi($ids = null)
  36. {
  37. if (false === $this->request->isPost()) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $ids = $ids ?: $this->request->post('ids');
  41. if (empty($ids)) {
  42. $this->error(__('Parameter %s can not be empty', 'ids'));
  43. }
  44. if (false === $this->request->has('params')) {
  45. $this->error(__('No rows were updated'));
  46. }
  47. parse_str($this->request->post('params'), $values);
  48. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  49. if (empty($values)) {
  50. $this->error(__('You have no permission'));
  51. }
  52. $adminIds = $this->getDataLimitAdminIds();
  53. if (is_array($adminIds)) {
  54. $this->model->where($this->dataLimitField, 'in', $adminIds);
  55. }
  56. // if(!empty($values['exam_collection_state'])){
  57. // if($values['exam_collection_state']==2){
  58. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_id'=>$ids])->count();
  59. // if($state_count>0){
  60. // $this->error('已有启用的考试,请先关闭');
  61. // }
  62. // }
  63. // }
  64. $count = 0;
  65. Db::startTrans();
  66. try {
  67. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  68. foreach ($list as $item) {
  69. $count += $item->allowField(true)->isUpdate(true)->save($values);
  70. }
  71. Db::commit();
  72. } catch (PDOException|Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($count) {
  77. $this->success();
  78. }
  79. $this->error(__('No rows were updated'));
  80. }
  81. }