Collection.php 20 KB

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