Practice.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use app\admin\model\teacher\Exams;
  10. /**
  11. * sim-练习集合管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Practice extends Backend
  16. {
  17. /**
  18. * Collection模型对象
  19. * @var \app\admin\model\teacher\Collection
  20. */
  21. protected $model = null;
  22. protected $whereExtend = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\teacher\Collection;
  27. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  28. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  29. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  30. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  31. $this->whereExtend['exam_collection_type'] = 1;
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. public function add()
  39. {
  40. if (false === $this->request->isPost()) {
  41. return $this->view->fetch();
  42. }
  43. $params = $this->request->post('row/a');
  44. if (empty($params)) {
  45. $this->error(__('Parameter %s can not be empty', ''));
  46. }
  47. if(empty($params['depart_ids'])){
  48. $this->error('请选择区队名称');
  49. }
  50. if(!empty($params['question_setting_method'])){
  51. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  52. $this->error('出题方式为考题自选,请选择考题');
  53. }
  54. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  55. $this->error('出题方式为任务自选,请选择任务');
  56. }
  57. }
  58. $params = $this->preExcludeFields($params);
  59. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  60. $params[$this->dataLimitField] = $this->auth->id;
  61. }
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $depart_ids = $params['depart_ids'];
  66. $result = $this->model->allowField(true)->save($params);
  67. foreach(explode(',',$depart_ids) as $item){
  68. if(!empty($item)){
  69. $arr = [
  70. 'exam_collection_id' => $this->model->exam_collection_id,
  71. 'dept_id' => $item,
  72. 'update_time' => date('Y-m-d H:i:s'),
  73. ];
  74. Db::name('real_exam_collection_dept')->insert($arr);
  75. }
  76. }
  77. //匹配的学员自动加入 mx_real_exam 考试表 总分
  78. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  79. foreach ($admin_list as $it){
  80. $arr1 = [
  81. 'exam_collection_id' => $this->model->exam_collection_id,
  82. 'exam_collection_name' => $params['exam_collection_name'],
  83. 'exam_collection_type' => $params['exam_collection_type'],
  84. 'sim_type' => $params['sim_type'],
  85. 'user_username' => $it['username'],
  86. 'user_nickname' => $it['nickname'],
  87. 'user_id' => $it['id'],
  88. 'user_depart_id' => $it['depart_id'],
  89. 'seat_id'=>01,
  90. 'sim_id'=>intval($params['sim_type']),
  91. 'create_time'=>date('Y-m-d H:i:s'),
  92. 'update_time' => date('Y-m-d H:i:s'),
  93. ];
  94. $examid = Db::name('real_exam')->insertGetId($arr1);
  95. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  96. if($params['question_setting_method']==1){ //系统随机
  97. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  98. }else if($params['question_setting_method']==2){//教师自选
  99. $fault_list = Fault::where(['sim_type' => $params['sim_type'],'fault_id'=>['in',$params['question_ids']], 'fault_state' => 0,'fault_type'=>3])->select();
  100. }else if($params['question_setting_method']==3){ //任务自选
  101. $fault_list = Db::name('task_fault')->where(['task_id'=>['in',$params['question_ids']]])->select();
  102. }
  103. $fault_key = array_rand($fault_list, 3);
  104. foreach ($fault_list as $key=> $item1){
  105. $flag = 0;
  106. if(in_array($key,$fault_key)){
  107. $flag = 1;
  108. }
  109. $add= [
  110. 'exam_id'=>$examid,
  111. 'fault_id'=>$item1->fault_id,
  112. 'ref_type'=>2,
  113. 'flag'=>$flag,
  114. 'ref_state'=>3,
  115. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  116. 'createtime'=>time(),
  117. 'updatetime'=>time(),
  118. 'create_time'=>date('Y-m-d H:i:s'),
  119. 'update_time'=>date('Y-m-d H:i:s'),
  120. ];
  121. Db::name('real_exam_fault')->insert($add);
  122. }
  123. unset($item1);
  124. }
  125. Db::commit();
  126. } catch (ValidateException|PDOException|Exception $e) {
  127. Db::rollback();
  128. $this->error($e->getMessage());
  129. }
  130. if ($result === false) {
  131. $this->error(__('No rows were inserted'));
  132. }
  133. $this->success();
  134. }
  135. public function edit($ids = null)
  136. {
  137. $row = $this->model->get($ids);
  138. if (!$row) {
  139. $this->error(__('No Results were found'));
  140. }
  141. if (false === $this->request->isPost()) {
  142. $this->view->assign('row', $row);
  143. $this->assignConfig('row_info', $row);
  144. return $this->view->fetch();
  145. }
  146. $params = $this->request->post('row/a');
  147. if (empty($params)) {
  148. $this->error(__('Parameter %s can not be empty', ''));
  149. }
  150. if(!empty($params['question_setting_method'])){
  151. if($params['question_setting_method']==2 && empty($params['question_ids'])){
  152. $this->error('出题方式为考题自选,请选择考题');
  153. }
  154. if($params['question_setting_method']==3 && empty($params['question_ids'])){
  155. $this->error('出题方式为任务自选,请选择任务');
  156. }
  157. }
  158. $params = $this->preExcludeFields($params);
  159. $result = false;
  160. Db::startTrans();
  161. try {
  162. $depart_ids = $params['depart_ids'];
  163. $result = $row->allowField(true)->save($params);
  164. Db::name('real_exam_collection_dept')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  165. foreach(explode(',',$depart_ids) as $item){
  166. if(!empty($item)){
  167. $arr = [
  168. 'exam_collection_id' => $row->exam_collection_id,
  169. 'dept_id' => $item,
  170. 'update_time' => date('Y-m-d H:i:s'),
  171. ];
  172. Db::name('real_exam_collection_dept')->insert($arr);
  173. }
  174. }
  175. Db::name('real_exam')->where(['exam_collection_id'=>$row->exam_collection_id])->delete();
  176. //匹配的学员自动加入 mx_real_exam 考试表 总分
  177. $admin_list = Db::name('admin')->where('depart_id','in',$depart_ids)->select();
  178. foreach ($admin_list as $it){
  179. $arr1 = [
  180. 'exam_collection_id' => $row->exam_collection_id,
  181. 'exam_collection_name' => $params['exam_collection_name'],
  182. 'exam_collection_type' => $row['exam_collection_type'],
  183. 'sim_type' => $params['sim_type'],
  184. 'user_username' => $it['username'],
  185. 'user_nickname' => $it['nickname'],
  186. 'user_id' => $it['id'],
  187. 'user_depart_id' => $it['depart_id'],
  188. 'seat_id'=>01,
  189. 'sim_id'=>intval($params['sim_type']),
  190. 'create_time'=>date('Y-m-d H:i:s'),
  191. 'update_time' => date('Y-m-d H:i:s'),
  192. ];
  193. $examid = Db::name('real_exam')->insertGetId($arr1);
  194. //匹配的学员自动加入 mx_real_exam_fault 考试故障关联表
  195. if($params['question_setting_method']==1){ //系统随机
  196. $fault_list = Fault::where(['sim_type' => $params['sim_type'], 'fault_state' => 0,'fault_type'=>3])->select();
  197. }else if($params['question_setting_method']==2){//教师自选
  198. $fault_list = Fault::where(['sim_type' => $params['sim_type'],'fault_id'=>['in',$params['question_ids']], 'fault_state' => 0,'fault_type'=>3])->select();
  199. }else if($params['question_setting_method']==3){ //任务自选
  200. $fault_list = Db::name('task_fault')->where(['task_id'=>['in',$params['question_ids']]])->select();
  201. }
  202. Db::name('real_exam_fault')->where(['exam_id'=>$examid])->delete();
  203. $fault_key = array_rand($fault_list, 3);
  204. foreach ($fault_list as $key=> $item1){
  205. $flag = 0;
  206. if(in_array($key,$fault_key)){
  207. $flag = 1;
  208. }
  209. $add= [
  210. 'exam_id'=>$examid,
  211. 'fault_id'=>$item1['fault_id'],
  212. 'ref_type'=>2,
  213. 'flag'=>$flag,
  214. 'ref_state'=>3,
  215. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  216. 'createtime'=>time(),
  217. 'updatetime'=>time(),
  218. 'create_time'=>date('Y-m-d H:i:s'),
  219. 'update_time'=>date('Y-m-d H:i:s'),
  220. ];
  221. Db::name('real_exam_fault')->insert($add);
  222. }
  223. unset($item1);
  224. }
  225. Db::commit();
  226. } catch (ValidateException|PDOException|Exception $e) {
  227. Db::rollback();
  228. $this->error($e->getMessage());
  229. }
  230. if (false === $result) {
  231. $this->error(__('No rows were updated'));
  232. }
  233. $this->success();
  234. }
  235. public function multi($ids = null)
  236. {
  237. if (false === $this->request->isPost()) {
  238. $this->error(__('Invalid parameters'));
  239. }
  240. $ids = $ids ?: $this->request->post('ids');
  241. if (empty($ids)) {
  242. $this->error(__('Parameter %s can not be empty', 'ids'));
  243. }
  244. if (false === $this->request->has('params')) {
  245. $this->error(__('No rows were updated'));
  246. }
  247. parse_str($this->request->post('params'), $values);
  248. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  249. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  250. if (empty($values)) {
  251. $this->error(__('You have no permission'));
  252. }
  253. $adminIds = $this->getDataLimitAdminIds();
  254. if (is_array($adminIds)) {
  255. $this->model->where($this->dataLimitField, 'in', $adminIds);
  256. }
  257. // if(!empty($values['exam_collection_state'])){
  258. // if($values['exam_collection_state']==2){
  259. // $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>1])->count();
  260. // if($state_count>0){
  261. // $this->error('已有启用的考试,请先关闭原来的考试');
  262. // }
  263. // }
  264. // }
  265. $count = 0;
  266. Db::startTrans();
  267. try {
  268. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  269. foreach ($list as $item) {
  270. $count += $item->allowField(true)->isUpdate(true)->save($values);
  271. }
  272. Db::commit();
  273. } catch (PDOException|Exception $e) {
  274. Db::rollback();
  275. $this->error($e->getMessage());
  276. }
  277. if ($count) {
  278. $this->success();
  279. }
  280. $this->error(__('No rows were updated'));
  281. }
  282. public function persent()
  283. {
  284. //设置过滤方法
  285. $this->request->filter(['strip_tags', 'trim']);
  286. if (false === $this->request->isAjax()) {
  287. $examlist = Exams::select();
  288. foreach ($examlist as $k => $v){
  289. $examlist[$k]['score'] = Db::name('real_exam_score')->where('exam_id',$v['exam_id'])->find();
  290. }
  291. $this->view->assign('examlist', $examlist);
  292. return $this->view->fetch();
  293. }
  294. //如果发送的来源是 Selectpage,则转发到 Selectpage
  295. if ($this->request->request('keyField')) {
  296. return $this->selectpage();
  297. }
  298. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  299. $list = Db::name('real_exam_comp_request')
  300. ->where($where)->where('exam_collection_type',1)
  301. ->order($sort, $order)
  302. ->paginate($limit);
  303. unset($v);
  304. $result = ['total' => $list->total(), 'rows' => $list->items()];
  305. return json($result);
  306. }
  307. public function examing()
  308. {
  309. //设置过滤方法
  310. $this->request->filter(['strip_tags', 'trim']);
  311. if (false === $this->request->isAjax()) {
  312. return $this->view->fetch();
  313. }
  314. //如果发送的来源是 Selectpage,则转发到 Selectpage
  315. if ($this->request->request('keyField')) {
  316. return $this->selectpage();
  317. }
  318. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  319. $list = $this->model
  320. ->where($where)->where($this->whereExtend)
  321. ->order($sort, $order)
  322. ->paginate($limit);
  323. $result = ['total' => $list->total(), 'rows' => $list->items()];
  324. return json($result);
  325. }
  326. public function start($ids = null){
  327. // $row = $this->model->get($ids);
  328. // if (!$row) {
  329. // $this->error(__('No Results were found'));
  330. // }
  331. // $params['starttime'] = time();
  332. // $result = $row->allowField(true)->save($params);
  333. $this->success('开始成功');
  334. }
  335. }