Collection.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. $row->other_report = $params['other_report'];
  169. $result = $row->save();
  170. $info = $this->exam_model->where(['exam_id'=>$ids])->find();
  171. //更新考试关联表,计算得分 mx_real_exam_fault
  172. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  173. foreach ($fault_list as $it)
  174. {
  175. $update['sim_fault_answer_value'] ='0000000'.rand(1,2);
  176. if($update['sim_fault_answer_value']==$it['sim_fault_question_value']){
  177. $update['answer_right'] = 1;
  178. }else{
  179. $update['answer_right'] = 2;
  180. }
  181. Db::name('real_exam_fault')->where('ref_id',$it['ref_id'])->update($update);
  182. }
  183. unset($it);
  184. //计算得分,故障是否有扣分 扣分制
  185. $fault_right_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1,'answer_right'=>['>',0]])->select();
  186. if($fault_right_list[0]['answer_right']==2){
  187. $fault_one_score = 25;
  188. }
  189. if($fault_right_list[1]['answer_right']==2){
  190. $fault_two_score = 25;
  191. }
  192. if($fault_right_list[2]['answer_right']==2){
  193. $fault_three_score = 25;
  194. }
  195. $xianxian_score = rand(1,3);
  196. $yuanyin_socre= rand(1,3);
  197. $buwei_score= rand(1,3);
  198. $fangfa_score= rand(1,3);
  199. if(empty($params['other_jielun'])){
  200. $jielun_score = rand(1,3);
  201. }
  202. //是否超时
  203. $overtime_fen = intval((time()-$info['endtime']) / 60);
  204. if($overtime_fen>=10){
  205. $overtime_score = 10;
  206. }else if($overtime_fen>0 && $overtime_fen<10){
  207. $overtime_score = $overtime_fen ;
  208. }else{
  209. $overtime_score =0;
  210. }
  211. $fault_score = $score-$fault_one_score-$fault_two_score-$fault_three_score-$xianxian_score-$yuanyin_socre-$buwei_score-$fangfa_score-$jielun_score-$overtime_score;
  212. //更新考试结果表
  213. $params['exam_id'] = $ids;
  214. $params['total'] = $fault_score;//得分;
  215. $params['fault_one_score'] = $fault_one_score;//得分;
  216. $params['fault_two_score'] = $fault_two_score;//得分;
  217. $params['fault_three_score'] = $fault_three_score;//得分;
  218. $params['xianxian_score'] = $xianxian_score;//得分;
  219. $params['yuanyin_socre'] = $yuanyin_socre;//得分;
  220. $params['buwei_score'] = $buwei_score;//得分;
  221. $params['fangfa_score'] = $fangfa_score;//得分;
  222. $params['jielun_score'] = $jielun_score;//得分;
  223. $params['overtime_score'] = $overtime_score;//得分;
  224. Db::name('real_exam_score')->insert($params);
  225. $this->exam_model->where(['exam_id'=>$ids])->update(['total_score'=>$fault_score]);
  226. Db::commit();
  227. } catch (ValidateException|PDOException|Exception $e) {
  228. Db::rollback();
  229. $this->error($e->getMessage());
  230. }
  231. if ($result === false) {
  232. $this->error(__('No rows were inserted'));
  233. }
  234. $this->success('交卷成功,待老师确认成绩','/ZQOtIMLKud.php/student/exam/index');
  235. }
  236. //还未开始考试
  237. $row->limit_duration = $this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration');
  238. if(empty($row->starttime)){
  239. $timer = 60*$row->limit_duration;
  240. }else{
  241. $fen = $row->limit_duration - intval((time()-$row->starttime) / 60);
  242. $timer = 60*$fen;
  243. }
  244. $row->replace_list =Db::name('real_exam_comp_request')->where(['exam_id'=>$ids])->select();
  245. $this->assignConfig('ids',$ids);
  246. $this->assignConfig('timer',$timer);
  247. $this->view->assign('row', $row);
  248. $departmentdata = [];
  249. $departmentdata = Fault::where(['replace_part'=>1,'sim_type'=>$row->sim_type])->select();
  250. //更换件数据
  251. $this->view->assign('departmentdata', $departmentdata);
  252. //故障现象
  253. $xianxiang = Fault::where(['fault_type'=>1,'sim_type'=>$row->sim_type])->select();
  254. $this->view->assign('xianxiang', $xianxiang);
  255. //可能原因
  256. $yuanyin = Fault::where(['fault_type'=>2,'sim_type'=>$row->sim_type])->select();
  257. $this->view->assign('yuanyin', $yuanyin);
  258. //故障部位
  259. $buwei = Fault::where(['fault_type'=>3,'sim_type'=>$row->sim_type])->select();
  260. $this->view->assign('buwei', $buwei);
  261. //排除方法
  262. $paichu = Fault::where(['fault_type'=>4,'sim_type'=>$row->sim_type])->select();
  263. $this->view->assign('paichu', $paichu);
  264. return $this->view->fetch();
  265. }
  266. public function replace($ids = null)
  267. {
  268. if ($this->request->isPost()) {
  269. $params = $this->request->post('other_replace','');
  270. $param = json_decode($params,true);
  271. if(!empty($param)){
  272. foreach ($param as $key=>$item){
  273. $isreplace = Db::name('real_exam_comp_request')->where(['exam_id'=>$ids,'fault_id'=>$item['fault_id']])->find();
  274. if(empty($isreplace)){
  275. $add = [
  276. 'exam_id'=>$ids,
  277. 'fault_id'=>$item['fault_id'],
  278. 'request_status'=>1,
  279. 'fault_name'=>Db::name('fault')->where('fault_id',$item['fault_id'])->value('replace_name'),
  280. 'create_by_user_id'=>$this->auth->id,
  281. 'create_by'=>$this->auth->nickname,
  282. 'createtime'=>time(),
  283. 'updatetime'=>time(),
  284. 'create_time'=>date('Y-m-d H:i:s'),
  285. 'update_time'=>date('Y-m-d H:i:s'),
  286. ];
  287. Db::name('real_exam_comp_request')->insert($add);
  288. $status = 1;
  289. }else{
  290. $status = $isreplace['request_status'];
  291. }
  292. $faultid_arr[] = [
  293. 'fault_id'=>$item['fault_id'],
  294. 'request_status'=>$status,
  295. ];
  296. }
  297. unset($item);
  298. $row = $this->exam_model->get($ids);
  299. $row-> other_replace = json_encode($faultid_arr);
  300. $result = $row->save();
  301. }
  302. $this->success('申请成功,待老师审批');
  303. }
  304. }
  305. public function analysis($ids = null)
  306. {
  307. $row = $this->exam_model->get($ids);
  308. if(!$row){
  309. $this->error('未找到记录');
  310. }
  311. if ($this->request->isPost()) {
  312. $params = $this->request->post('row/a');
  313. //计算总分到学员考试表
  314. $this->success('操作成功','/ZQOtIMLKud.php/student/collection/index');
  315. }
  316. return $this->view->fetch();
  317. }
  318. }