Collection.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. use app\admin\model\teacher\Task;
  10. use app\admin\model\Fault;
  11. use think\Env;
  12. /**
  13. * sim-考试集合管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Collection extends Backend
  18. {
  19. /**
  20. * Collection模型对象
  21. * @var \app\admin\model\teacher\Collection
  22. */
  23. protected $model = null;
  24. protected $exam_model = null;
  25. protected $whereExtend = null;
  26. protected $noNeedRight = ['adduser','edituser', 'viewuser'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\teacher\Collection;
  31. $this->exam_model = new \app\admin\model\teacher\Exams;
  32. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  33. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  34. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  35. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  36. $this->whereExtend['exam_collection_type'] = 3;
  37. }
  38. /**
  39. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  40. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  41. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  42. */
  43. public function adduser($ids = null)
  44. {
  45. if (false === $this->request->isPost()) {
  46. $this->assignConfig('user_ids',[]);
  47. return $this->view->fetch();
  48. }
  49. $params = $this->request->post('row/a');
  50. if (empty($params)) {
  51. $this->error(__('Parameter %s can not be empty', ''));
  52. }
  53. if(empty($params['depart_ids'])){
  54. $this->error('请选择学员名称');
  55. }
  56. if(!empty($params['question_setting_method'])){
  57. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  58. $this->error('出题方式为考题自选,请选择考题');
  59. }
  60. if($params['question_setting_method']==3 && empty($params['task_id'])){
  61. $this->error('出题方式为任务自选,请选择任务');
  62. }
  63. }
  64. $depart_ids = $params['depart_ids'];
  65. //先判断是否已经存在
  66. $isset_exam = $this->exam_model->where(['user_id'=>['in',$depart_ids],'exam_collection_id'=>$ids])->find();
  67. if(!empty($isset_exam)){
  68. $this->error('添加的人员已存在该考试中,请重新选择');
  69. }
  70. $row = $this->model->get($ids);
  71. $params = $this->preExcludeFields($params);
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. if($params['question_setting_method']==3 && !empty($params['fault_name'])){
  76. $params['question_name'] = $params['fault_name'];
  77. unset($params['fault_name']);
  78. }
  79. // $result = $this->model->allowField(true)->save($params);
  80. $row->xueyuan_count = $row->xueyuan_count+$params['xueyuan_count'];
  81. $row->save();
  82. foreach(explode(',',$depart_ids) as $item){
  83. if(!empty($item)){
  84. $admin = Db::name('admin')->where('id',$item)->find();
  85. $arr = [
  86. 'exam_collection_id' => $ids,
  87. 'exam_collection_name' => $row->exam_collection_name,
  88. 'question_setting_method' => $params['question_setting_method'],
  89. 'exam_collection_type' => $row['exam_collection_type'],
  90. 'sim_type' => $params['sim_type'],
  91. 'user_username' => $admin['username'],
  92. 'user_nickname' => $admin['nickname'],
  93. 'user_id' => $item,
  94. 'seat_id'=>0,
  95. 'task_id'=>$params['task_id'],
  96. 'question_ids'=>$params['question_ids'],
  97. 'question_name'=>$params['question_name'],
  98. 'user_depart_id' => $admin['depart_id'],
  99. 'createtime'=>time(),
  100. 'updatetime'=>time(),
  101. 'create_time' => date('Y-m-d H:i:s'),
  102. 'update_time' => date('Y-m-d H:i:s'),
  103. ];
  104. $examid = $this->exam_model->insertGetId($arr);
  105. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  106. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  107. if($params['question_setting_method']==1){ //系统随机
  108. if($params['sim_type']=='0002'){
  109. $fault_key1 = 1;
  110. $fault_key2 = 5;
  111. $fault_key3 = rand(10,11);
  112. }else{
  113. $fault_key1 = rand(0, 1);
  114. $fault_key2 = rand(2, 3);
  115. $fault_key3 = 9;
  116. }
  117. $fault_key = [$fault_key1,$fault_key2,$fault_key3];
  118. }else if($params['question_setting_method']==2){//教师自选
  119. $question_ids = $params['question_ids'];
  120. }else if($params['question_setting_method']==3){ //任务自选
  121. $task_ids = Db::name('task_fault')->where(['task_id'=>['in',$params['task_id']]])->column('fault_id');
  122. }
  123. foreach ($fault_list as $key=> $item1){
  124. $flag = 0;
  125. if(!empty($fault_key) && in_array($key,$fault_key)){
  126. $flag = 1;
  127. }
  128. if(!empty($question_ids) && in_array($item1['fault_id'],explode(',',$question_ids))){
  129. $flag = 1;
  130. }
  131. if(!empty($task_ids) && in_array($item1['fault_id'],$task_ids)){
  132. $flag = 1;
  133. }
  134. $add= [
  135. 'exam_id'=>$examid,
  136. 'fault_id'=>$item1['fault_id'],
  137. 'ref_type'=>2,
  138. 'flag'=>$flag,
  139. 'ref_state'=>0,
  140. 'createtime'=>time(),
  141. 'updatetime'=>time(),
  142. 'create_time'=>date('Y-m-d H:i:s'),
  143. 'update_time'=>date('Y-m-d H:i:s'),
  144. ];
  145. Db::name('real_exam_fault')->insert($add);
  146. }
  147. unset($item1);
  148. }
  149. }
  150. $result = true;
  151. Db::commit();
  152. } catch (ValidateException|PDOException|Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. if ($result === false) {
  157. $this->error(__('No rows were inserted'));
  158. }
  159. $this->success();
  160. }
  161. public function edituser($ids = null)
  162. {
  163. $row = $this->model->get($ids);
  164. if (!$row) {
  165. $this->error(__('No Results were found'));
  166. }
  167. //获取当前考试的全部用户id
  168. $user_ids = $this->exam_model->where(['exam_collection_id'=>$ids])->column('user_id');
  169. if (false === $this->request->isPost()) {
  170. $this->view->assign('row', $row);
  171. $this->view->assign('user_ids',implode(',', $user_ids));
  172. $this->assignConfig('user_ids', $user_ids);
  173. return $this->view->fetch();
  174. }
  175. $params = $this->request->post('row/a');
  176. if (empty($params)) {
  177. $this->error(__('Parameter %s can not be empty', ''));
  178. }
  179. $params = $this->preExcludeFields($params);
  180. $depart_id = $params['depart_id'];
  181. //比较两者的不同,然后在删除
  182. $array_diff = array_diff($user_ids,explode(',',$depart_id));
  183. if(!empty($depart_id)){
  184. //判断是否有已经开始考试的人员
  185. $isset_exam = $this->exam_model->where(['user_id'=>['in',$array_diff],'exam_collection_id'=>$ids,'exam_status'=>4])->find();
  186. if(!empty($isset_exam)){
  187. $this->error('删除的人员已进行考试,请重新选择');
  188. }
  189. }
  190. $result = false;
  191. Db::startTrans();
  192. try {
  193. if(!empty($depart_id)){
  194. $depart_count = count($array_diff);
  195. $params['xueyuan_count'] = $row->xueyuan_count-$depart_count??0;
  196. $result = $row->allowField(true)->save($params);
  197. foreach($array_diff as $item){
  198. if(!empty($item)){
  199. $exam = $this->exam_model->where(['exam_collection_id'=>$ids,'user_id'=>$item])->find();
  200. //删除real_exam 的人员
  201. $this->exam_model->where(['exam_collection_id'=>$ids,'user_id'=>$item])->delete();
  202. //删除real_exam_fault 的故障内容
  203. Db::name('real_exam_fault')->where(['exam_id'=>$exam['exam_id']])->delete();
  204. }
  205. }
  206. }
  207. $result = true;
  208. Db::commit();
  209. } catch (ValidateException|PDOException|Exception $e) {
  210. Db::rollback();
  211. $this->error($e->getMessage());
  212. }
  213. if (false === $result) {
  214. $this->error(__('No rows were updated'));
  215. }
  216. $this->success();
  217. }
  218. //查看学员
  219. public function viewuser($ids= null)
  220. {
  221. //设置过滤方法
  222. $this->request->filter(['strip_tags', 'trim']);
  223. if (false === $this->request->isAjax()) {
  224. $this->assignConfig('ids', $ids);
  225. return $this->view->fetch();
  226. }
  227. //如果发送的来源是 Selectpage,则转发到 Selectpage
  228. if ($this->request->request('keyField')) {
  229. return $this->selectpage();
  230. }
  231. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  232. $list = $this->exam_model->where($where)->where('exam_collection_id',$ids)
  233. ->order($sort, $order)
  234. ->paginate($limit);
  235. unset($v);
  236. $result = ['total' => $list->total(), 'rows' => $list->items()];
  237. return json($result);
  238. }
  239. public function multi($ids = null)
  240. {
  241. if (false === $this->request->isPost()) {
  242. $this->error(__('Invalid parameters'));
  243. }
  244. $ids = $ids ?: $this->request->post('ids');
  245. if (empty($ids)) {
  246. $this->error(__('Parameter %s can not be empty', 'ids'));
  247. }
  248. if (false === $this->request->has('params')) {
  249. $this->error(__('No rows were updated'));
  250. }
  251. parse_str($this->request->post('params'), $values);
  252. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  253. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  254. if (empty($values)) {
  255. $this->error(__('You have no permission'));
  256. }
  257. $adminIds = $this->getDataLimitAdminIds();
  258. if (is_array($adminIds)) {
  259. $this->model->where($this->dataLimitField, 'in', $adminIds);
  260. }
  261. $count = 0;
  262. if(!empty($values['exam_collection_state'])){
  263. //开始考试集合
  264. if($values['exam_collection_state']==2){
  265. //触发Java端后台的接口数据,
  266. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  267. if(Env::get('app.is_fault')){
  268. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/open/'.$ids;
  269. $ret = json_decode(send_post($url),true);
  270. if($ret['code']!=200){
  271. $this->error($ret['msg']);
  272. }
  273. }else{
  274. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  275. if($state_count>0){
  276. $this->error('已有启用的考试,请先关闭原来的考试');
  277. }
  278. }
  279. $count = 1;
  280. }
  281. //关闭
  282. if($values['exam_collection_state']==3){
  283. //触发Java端后台的接口数据,
  284. //dev-api/sim/real-exam-collection/teacher/exam/open/{examcollectionId} $ids put
  285. if(Env::get('app.is_fault')){
  286. $url = config('site.url_type').'/sim/real-exam-collection/teacher/exam/close/'.$ids;
  287. $ret = json_decode(send_post($url),true);
  288. if($ret['code']!=200){
  289. $this->error($ret['msg']);
  290. }
  291. }
  292. $count = 1;
  293. }
  294. }
  295. Db::startTrans();
  296. try {
  297. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  298. foreach ($list as $item) {
  299. $count += $item->allowField(true)->isUpdate(true)->save($values);
  300. }
  301. Db::commit();
  302. } catch (PDOException|Exception $e) {
  303. Db::rollback();
  304. $this->error($e->getMessage());
  305. }
  306. if ($count) {
  307. $this->success();
  308. }
  309. $this->error(__('No rows were updated'));
  310. }
  311. //选择考题
  312. public function faults($type = null)
  313. {
  314. //设置过滤方法
  315. $this->request->filter(['strip_tags', 'trim']);
  316. if (false === $this->request->isAjax()) {
  317. $this->assignConfig('sim_type', $type);
  318. return $this->view->fetch();
  319. }
  320. //如果发送的来源是 Selectpage,则转发到 Selectpage
  321. if ($this->request->request('keyField')) {
  322. return $this->selectpage();
  323. }
  324. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  325. $where_fault = [
  326. 'sim_type' => $type,
  327. 'fault_type' => 3,
  328. 'fault_state'=>0
  329. ];
  330. $list = Fault::where($where)->where($where_fault)
  331. ->order($sort, $order)
  332. ->paginate($limit);
  333. foreach ($list as $k => $v){
  334. $exam = Fault::where(['fault_id'=>$v['parent_fault_id'],'fault_type'=>1])->find();
  335. if(isset($exam)){
  336. $v->parent_name = $exam['name'];
  337. }else{
  338. $v->parent_name = '';
  339. }
  340. }
  341. unset($v);
  342. unset($v);
  343. $result = ['total' => $list->total(), 'rows' => $list->items()];
  344. return json($result);
  345. }
  346. //选择任务
  347. public function task($type= null)
  348. {
  349. //设置过滤方法
  350. $this->request->filter(['strip_tags', 'trim']);
  351. if (false === $this->request->isAjax()) {
  352. $this->assignConfig('sim_type', $type);
  353. return $this->view->fetch();
  354. }
  355. //如果发送的来源是 Selectpage,则转发到 Selectpage
  356. if ($this->request->request('keyField')) {
  357. return $this->selectpage();
  358. }
  359. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  360. $list = Task::where($where)->where('sim_type',$type)
  361. ->order($sort, $order)
  362. ->paginate($limit);
  363. unset($v);
  364. $result = ['total' => $list->total(), 'rows' => $list->items()];
  365. return json($result);
  366. }
  367. /**
  368. 老师开启考试
  369. */
  370. public function start($ids = null){
  371. $row = $this->model->get($ids);
  372. if (!$row) {
  373. $this->error(__('No Results were found'));
  374. }
  375. $params['start_time'] = date('Y-m-d H:i:s');
  376. $params['starttime'] = time();
  377. $result = $row->allowField(true)->save($params);
  378. $this->success('开始成功');
  379. }
  380. public function del($ids = null)
  381. {
  382. if (false === $this->request->isPost()) {
  383. $this->error(__("Invalid parameters"));
  384. }
  385. $ids = $ids ?: $this->request->post("ids");
  386. if (empty($ids)) {
  387. $this->error(__('Parameter %s can not be empty', 'ids'));
  388. }
  389. $pk = $this->model->getPk();
  390. $adminIds = $this->getDataLimitAdminIds();
  391. if (is_array($adminIds)) {
  392. $this->model->where($this->dataLimitField, 'in', $adminIds);
  393. }
  394. $list = $this->model->where($pk, 'in', $ids)->select();
  395. $count = 0;
  396. Db::startTrans();
  397. try {
  398. //删除考试集合 real_exam_collection
  399. foreach ($list as $item) {
  400. if($item['yikao_count']>0){
  401. $this->error('已有学生参加考试,无法删除');
  402. }
  403. $count += $item->delete();
  404. }
  405. //删除学员考试 real_exam
  406. $exam_list = $this->exam_model->where('exam_collection_id',$ids)->select();
  407. if(!empty($exam_list)){
  408. foreach ($exam_list as $k=>$it)
  409. {
  410. //删除学员考试故障表 mx_real_exam_fault
  411. Db::name('real_exam_fault')->where('exam_id',$it['exam_id'])->delete();
  412. $it->delete();
  413. }
  414. }
  415. //删除考试关联区队表 mx_real_exam_collection_dept
  416. // Db::name('real_exam_collection_dept')->where('exam_collection_id',$ids)->delete();
  417. Db::commit();
  418. } catch (PDOException|Exception $e) {
  419. Db::rollback();
  420. $this->error($e->getMessage());
  421. }
  422. if ($count) {
  423. $this->success();
  424. }
  425. $this->error(__('No rows were deleted'));
  426. }
  427. public function rand_fault($fault_list = null){
  428. // $fault_key = array_rand($fault_list,3);
  429. // $fault_key1 = array_rand($fault_list);
  430. // $fault_key2 = array_rand($fault_list);
  431. // $fault_key3 = array_rand($fault_list);
  432. // if($fault_list[$fault_key1]['parent_fault_id']==$fault_list[$fault_key2]['parent_fault_id']){
  433. // $fault_key2 = array_rand($fault_list);
  434. // }
  435. // if($fault_list[$fault_key2]['parent_fault_id']==$fault_list[$fault_key3]['parent_fault_id']){
  436. // $fault_key3 = array_rand($fault_list);
  437. // }
  438. // $arr = [$fault_key1,$fault_key2,$fault_key3];
  439. $arr = [1,5,7];
  440. return $arr;
  441. }
  442. }