Collection.php 3.2 KB

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