Practice.php 19 KB

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