Collection.php 21 KB

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