Collection.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\admin\controller\student;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use app\admin\model\Report;
  6. use app\admin\model\Fault;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\response\Json;
  11. use think\Db;
  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->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  33. $this->whereExtend['exam_collection_type'] = 3;
  34. $this->whereExtend['exam_collection_state'] = 2;
  35. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  36. $groupIds = $this->auth->getGroupIds();
  37. //学员查看自己区队的考试集合
  38. if(in_array(8, $groupIds)){
  39. $departid = $this->auth->depart_id;
  40. $this->whereExtend[] = ['exp',Db::raw("FIND_IN_SET($departid,depart_ids)")];
  41. }
  42. }
  43. /**
  44. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  45. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  46. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  47. */
  48. /**
  49. * 查看
  50. *
  51. * @return string|Json
  52. * @throws \think\Exception
  53. * @throws DbException
  54. */
  55. public function index()
  56. {
  57. //设置过滤方法
  58. $this->request->filter(['strip_tags', 'trim']);
  59. if (false === $this->request->isAjax()) {
  60. return $this->view->fetch();
  61. }
  62. //如果发送的来源是 Selectpage,则转发到 Selectpage
  63. if ($this->request->request('keyField')) {
  64. return $this->selectpage();
  65. }
  66. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  67. $list = $this->model
  68. ->where($where)->where($this->whereExtend)
  69. ->order($sort, $order)
  70. ->paginate($limit);
  71. foreach ($list as $k => $v){
  72. $exam_isset = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$v['exam_collection_id'],'exam_collection_type'=>3,'endtime'=>['>',0]])->find();
  73. if(isset($exam_isset)){
  74. $v->is_user_examed = 1;
  75. }else{
  76. $v->is_user_examed = 0;
  77. }
  78. }
  79. unset($v);
  80. $result = ['total' => $list->total(), 'rows' => $list->items()];
  81. return json($result);
  82. }
  83. //进入考试
  84. public function into($ids = null)
  85. {
  86. $row = $this->model->get($ids);
  87. if(!$row){
  88. $this->error('未找到记录');
  89. }
  90. $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids])->find();
  91. if(empty($info)){
  92. //考试表
  93. $arr = [
  94. 'exam_collection_id' => $ids,
  95. 'user_id' => $this->auth->id,
  96. 'user_username' => $this->auth->username,
  97. 'user_nickname' => $this->auth->nickname,
  98. 'user_depart_id' => $this->auth->depart_id,
  99. 'exam_collection_name' => $row->exam_collection_name,
  100. 'exam_collection_type' => 3,
  101. 'sim_type' => $row->sim_type,
  102. 'seat_id' => 1,
  103. 'sim_id' => intval($row->sim_type),
  104. ];
  105. $this->exam_model->save($arr);
  106. //考试和故障关联表
  107. $fault_list = fault::where(['sim_type' => $row->sim_type, 'fault_state' => 0,'fault_type'=>3])->select();
  108. $fault_key = array_rand($fault_list, 3);
  109. foreach ($fault_list as $key=> $item){
  110. $flag = 0;
  111. if(in_array($key,$fault_key)){
  112. $flag = 1;
  113. }
  114. $add= [
  115. 'exam_id'=>$this->exam_model->exam_id,
  116. 'fault_id'=>$item->fault_id,
  117. 'ref_type'=>2,
  118. 'flag'=>$flag,
  119. 'ref_state'=>3,
  120. 'sim_fault_question_value'=>'0000000'.rand(1,2),
  121. 'createtime'=>time(),
  122. 'updatetime'=>time(),
  123. 'create_time'=>date('Y-m-d H:i:s'),
  124. 'update_time'=>date('Y-m-d H:i:s'),
  125. ];
  126. Db::name('real_exam_fault')->insert($add);
  127. }
  128. unset($item);
  129. }
  130. if ($this->request->isPost()) {
  131. $exam_collection_id = $this->request->post('exam_collection_id');
  132. if(empty($info->starttime)){
  133. $info->start_time = date('Y-m-d H:i:s');
  134. $info->starttime = time();
  135. }
  136. $info->exam_status = 4;
  137. $info->save();
  138. $this->success('开始成功','/ZQOtIMLKud.php/student/collection/examing/ids/'.$info['exam_id']);
  139. }
  140. $this->view->assign('row', $row);
  141. return $this->view->fetch();
  142. }
  143. public function examing($ids = null)
  144. {
  145. $row = $this->exam_model->get($ids);
  146. if(!$row){
  147. $this->error('未找到记录');
  148. }
  149. if ($this->request->isPost()) {
  150. //计算分数,保存记录
  151. $params = $this->request->post('row/a');
  152. $result = false;
  153. $score = 100;
  154. $fault_one_score = 0;
  155. $fault_two_score = 0;
  156. $fault_three_score = 0;
  157. $xianxian_score = 0;
  158. $yuanyin_socre= 0;
  159. $buwei_score= 0;
  160. $fangfa_score= 0;
  161. $jielun_score = 0;
  162. Db::startTrans();
  163. try {
  164. //更新考试结束时间
  165. $row->end_time = date('Y-m-d H:i:s');
  166. $row->endtime = time();
  167. $row->exam_status = 5;
  168. $result = $row->save();
  169. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  170. //更新考试关联表,计算得分 mx_real_exam_fault
  171. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  172. foreach ($fault_list as $it)
  173. {
  174. $update['sim_fault_answer_value'] ='0000000'.rand(1,2);
  175. if($update['sim_fault_answer_value']==$it['sim_fault_question_value']){
  176. $update['answer_right'] = 1;
  177. }else{
  178. $update['answer_right'] = 2;
  179. }
  180. Db::name('real_exam_fault')->where('ref_id',$it['ref_id'])->update($update);
  181. }
  182. unset($it);
  183. //计算得分,故障是否有扣分 扣分制
  184. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  185. if($fault_right_list[0]['answer_right']==2){
  186. $fault_one_score = 25;
  187. }
  188. if($fault_right_list[1]['answer_right']==2){
  189. $fault_two_score = 25;
  190. }
  191. if($fault_right_list[2]['answer_right']==2){
  192. $fault_three_score = 25;
  193. }
  194. $xianxian_score = rand(1,3);
  195. $yuanyin_socre= rand(1,3);
  196. $buwei_score= rand(1,3);
  197. $fangfa_score= rand(1,3);
  198. if(empty($params['other_jielun'])){
  199. $jielun_score = rand(1,3);
  200. }
  201. //是否超时
  202. $overtime_fen = intval((time()-$info['endtime']) / 60);
  203. if($overtime_fen>=5){
  204. $overtime_score = 5;
  205. }else if($overtime_fen>0 && $overtime_fen<5){
  206. $overtime_score = $overtime_fen ;
  207. }else{
  208. $overtime_score =0;
  209. }
  210. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$xianxian_score-$yuanyin_socre-$buwei_score-$fangfa_score-$jielun_score-$overtime_score;
  211. //更新考试结果表
  212. $params['exam_id'] = $ids;
  213. $params['total'] = $fault_score;//得分;
  214. $params['fault_one_score'] = $fault_one_score;//得分;
  215. $params['fault_two_score'] = $fault_two_score;//得分;
  216. $params['fault_three_score'] = $fault_three_score;//得分;
  217. $params['xianxian_score'] = $xianxian_score;//得分;
  218. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  219. $params['buwei_score'] = $buwei_score;//得分;
  220. $params['fangfa_score'] = $fangfa_score;//得分;
  221. $params['jielun_score'] = $jielun_score;//得分;
  222. $params['overtime_score'] = $overtime_score;//得分;
  223. Db::name('real_exam_score')->insert($params);
  224. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  225. Db::commit();
  226. } catch (ValidateException|PDOException|Exception $e) {
  227. Db::rollback();
  228. $this->error($e->getMessage());
  229. }
  230. if ($result === false) {
  231. $this->error(__('No rows were inserted'));
  232. }
  233. $this->success('交卷成功,待老师确认成绩','/ZQOtIMLKud.php/student/exam/index');
  234. }
  235. //还未开始考试
  236. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  237. if(empty($row->starttime)){
  238. $timer = 60*$row->limit_duration;
  239. }else{
  240. $fen = $row->limit_duration - intval((time()-$row->starttime) / 60);
  241. $timer = 60*$fen;
  242. }
  243. $row->other_replace_arr = json_decode($row->other_replace,true)??[];
  244. $this->assignConfig('other_replace_arr', $row->other_replace_arr);
  245. // halt($row->other_replace_arr);
  246. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  247. $this->assignConfig('ids',$ids);
  248. $this->assignConfig('timer',$timer);
  249. $this->view->assign('row', $row);
  250. $departmentdata = [];
  251. $departmentdata = Fault::where(['replace_part'=>1])->select();
  252. $this->view->assign('departmentdata', $departmentdata);
  253. // echo "<pre>";
  254. // print_r(collection($departmentdata)->toArray());
  255. return $this->view->fetch();
  256. }
  257. public function replace($ids = null)
  258. {
  259. if ($this->request->isPost()) {
  260. $params = $this->request->post('other_replace','');
  261. $param = json_decode($params,true);
  262. if(!empty($param)){
  263. foreach ($param as $key=>$item){
  264. $isreplace = Db::name('real_exam_comp_request')->where(['exam_id'=>$ids,'fault_id'=>$item['fault_id']])->find();
  265. if(empty($isreplace)){
  266. $add = [
  267. 'exam_id'=>$ids,
  268. 'fault_id'=>$item['fault_id'],
  269. 'request_status'=>1,
  270. 'create_by_user_id'=>$this->auth->id,
  271. 'create_by'=>$this->auth->nickname,
  272. 'createtime'=>time(),
  273. 'updatetime'=>time(),
  274. 'create_time'=>date('Y-m-d H:i:s'),
  275. 'update_time'=>date('Y-m-d H:i:s'),
  276. ];
  277. Db::name('real_exam_comp_request')->insert($add);
  278. }else{
  279. $add = [
  280. 'exam_id'=>$ids,
  281. 'fault_id'=>$item['fault_id'],
  282. 'fault_name'=>Fault::where('fault_id',$item['fault_id'])->value('replace_name'),
  283. 'request_status'=>$isreplace['request_status'],
  284. 'create_by_user_id'=>$this->auth->id,
  285. 'create_by'=>$this->auth->nickname,
  286. 'createtime'=>time(),
  287. 'updatetime'=>time(),
  288. 'create_time'=>date('Y-m-d H:i:s'),
  289. 'update_time'=>date('Y-m-d H:i:s'),
  290. ];
  291. Db::name('real_exam_comp_request')->where('rel_id',$isreplace['rel_id'])->update($add);
  292. }
  293. }
  294. unset($item);
  295. $row = $this->exam_model->get($ids);
  296. $row-> other_replace = $params;
  297. $result = $row->save();
  298. }
  299. $this->success('申请成功,待老师审批');
  300. }
  301. }
  302. public function analysis($ids = null)
  303. {
  304. $row = $this->exam_model->get($ids);
  305. if(!$row){
  306. $this->error('未找到记录');
  307. }
  308. if ($this->request->isPost()) {
  309. $params = $this->request->post('row/a');
  310. //计算总分到学员考试表
  311. $this->success('操作成功','/ZQOtIMLKud.php/student/collection/index');
  312. }
  313. return $this->view->fetch();
  314. }
  315. }