Trainactive.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 培训活动
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Trainactive extends Backend
  11. {
  12. /**
  13. * Trainactive模型对象
  14. * @var \app\admin\model\Trainactive
  15. */
  16. protected $model = null;
  17. protected $noNeedLogin = ['qrcode','showinfo','nograde','transfer','selectuser'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Trainactive;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("userauthStatusList", $this->model->getUserauthStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['cate','level','type'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('cate')->visible(['name']);
  52. $row->getRelation('level')->visible(['name']);
  53. $row->getRelation('type')->visible(['name']);
  54. $row['usercount'] = count(explode(',',$row['user_ids']));
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 二维码
  63. */
  64. public function qrcode(){
  65. $id = input('id');
  66. $info = Db::name('train_active')->where('id',$id)->find();
  67. //二维码
  68. $sign_data = [
  69. 'type' => 'sign',
  70. 'name' => $info['name'],
  71. 'id' => $info['id'],
  72. ];
  73. $pingjia_data = [
  74. 'type' => 'pingjia',
  75. 'name' => $info['name'],
  76. 'id' => $info['id'],
  77. ];
  78. $info['sign_qrcode'] = $this->introimage(json_encode($sign_data));
  79. $info['pingjia_qrcode'] = $this->introimage(json_encode($pingjia_data));
  80. $this->assign('row',$info);
  81. //评价人
  82. $pingjiaren = Db::name('user')->where('id',$info['pingjia_uid'])->value('nickname');
  83. $this->assign('pingjiaren',$pingjiaren);
  84. $this->view->engine->layout(false);
  85. return $this->view->fetch();
  86. }
  87. //生成邀请码二维码图片
  88. private function introimage($data) {
  89. $params['text'] = $data;
  90. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  91. $qrcodePath = ROOT_PATH . 'public/uploads/trainqrcode/'.date('Ymd');
  92. if (!is_dir($qrcodePath)) {
  93. @mkdir($qrcodePath);
  94. }
  95. $filename = md5($data) . '.png';
  96. if (is_really_writable($qrcodePath)) {
  97. $filePath = $qrcodePath .'/'. $filename;
  98. $qrcode_service->writeFile($filePath);
  99. }
  100. return request()->domain().'/uploads/trainqrcode/'.date('Ymd') .'/'. $filename;
  101. }
  102. /**
  103. * 打印
  104. */
  105. public function showinfo(){
  106. $id = input('id');
  107. $info = Db::name('train_active')->alias('ta')->field('ta.*,cate.name as cate_name,level.name as level_name,type.name as type_name,user.nickname as pingjia_nickname')
  108. ->join('train_cate cate', 'ta.cate_id = cate.id' ,'LEFT')
  109. ->join('train_level level','ta.level_id = level.id','LEFT')
  110. ->join('train_type type', 'ta.type_id = type.id' ,'LEFT')
  111. ->join('user', 'ta.pingjia_uid = user.id' ,'LEFT')
  112. ->where('ta.id',$id)->find();
  113. $info = info_domain_image($info,['logo_image','files','pingjia_image']);
  114. //重新定义附件
  115. $files_json = json_decode($info['files_json'],true);
  116. if(!empty($files_json)){
  117. foreach($files_json as $key => $file){
  118. $files_json[$key]['url'] = localpath_to_netpath($file['url']);
  119. $url = explode('.',$file['url']);
  120. $files_json[$key]['suffix'] = $url[count($url)-1];
  121. }
  122. $info['files_json'] = $files_json;
  123. }else{
  124. $info['files_json'] = [];
  125. }
  126. //时间
  127. $info['pingjia_time'] = $info['pingjia_time'] == 0 ? '' : date('Y-m-d H:i:s',$info['pingjia_time']);
  128. $info['starttime'] = $info['starttime'] == 0 ? '' : date('Y-m-d H:i:s',$info['starttime']);
  129. $info['endtime'] = $info['endtime'] == 0 ? '' : date('Y-m-d H:i:s',$info['endtime']);
  130. $this->assign('row',$info);
  131. //全部应到用户,左外链接,没签到的则为NULL
  132. if(empty($info['user_ids'])){ $info['user_ids'] = 0;}
  133. $user_yingdao = Db::name('user')->field('user_train.*,user.nickname,user.id as user_pkid')
  134. ->join('user_train','user_train.user_id = user.id and user_train.train_id = '.$info['id'],'LEFT')
  135. ->where('user.id','IN',$info['user_ids'])
  136. ->orderRaw('field(user.id,'.$info['user_ids'].')')
  137. ->select();
  138. // dump($user_yingdao);
  139. $status_arr = [
  140. 1 => '正常',
  141. 2 => '迟到',
  142. ];
  143. $status_color = [
  144. 1 => 'green',
  145. 2 => 'blue',
  146. ];
  147. if(!empty($user_yingdao)){
  148. foreach($user_yingdao as $key => $val){
  149. $user_yingdao[$key]['sign_time'] = empty($val['sign_time']) ? '' : date('Y-m-d H:i:s',$val['sign_time']);
  150. $user_yingdao[$key]['sign_image'] = localpath_to_netpath($val['sign_image']);
  151. $user_yingdao[$key]['status_text'] = isset($status_arr[$val['status']]) ? $status_arr[$val['status']] : '未到';
  152. $user_yingdao[$key]['status_color'] = isset($status_color[$val['status']]) ? $status_color[$val['status']] : 'red';
  153. }
  154. }
  155. //其他用户 签到记录
  156. $user_train = Db::name('user_train')->field('user_train.*,user.nickname,user.id as user_pkid')
  157. ->join('user','user_train.user_id = user.id','LEFT')
  158. ->where('user_train.train_id',$id)
  159. ->where('user_train.user_id','NOTIN',$info['user_ids'])
  160. ->order('user_train.id desc')->select();
  161. // dump($user_train);
  162. foreach($user_train as $key => $val){
  163. $user_train[$key]['sign_time'] = empty($val['sign_time']) ? '' : date('Y-m-d H:i:s',$val['sign_time']);
  164. $user_train[$key]['sign_image'] = localpath_to_netpath($val['sign_image']);
  165. // $user_train[$key]['status_text'] = isset($status_arr[$val['status']]) ? $status_arr[$val['status']] : '';
  166. // $user_train[$key]['status_color'] = isset($status_color[$val['status']]) ? $status_color[$val['status']] : 'red';
  167. $user_train[$key]['status_text'] = '';
  168. $user_train[$key]['status_color'] = '';
  169. }
  170. //本活动给的所有评分
  171. $user_train_evaluate = Db::name('user_train_evaluate')->where('train_id',$id)->column('user_id,score');
  172. //合并
  173. $all_user_train = array_merge($user_yingdao,$user_train);
  174. foreach($all_user_train as $key => $usertrain){
  175. $all_user_train[$key]['score'] = isset($user_train_evaluate[$usertrain['user_pkid']]) ? $user_train_evaluate[$usertrain['user_pkid']] : '';
  176. }
  177. $this->assign('user_train',$all_user_train);
  178. //查看评分的权限
  179. $score_auth = 0; //无权限
  180. if($this->auth->check('trainactive/showinfo/score')){
  181. $score_auth = 1; //有权限
  182. }
  183. $this->assign('score_auth',$score_auth);
  184. //学习记录,过滤掉没签到的
  185. $all_user_train_signed = $all_user_train;
  186. foreach($all_user_train_signed as $key => $val){
  187. if(empty($val['sign_image'])){
  188. unset($all_user_train_signed[$key]);
  189. }
  190. }
  191. $this->assign('user_train_chunk',array_chunk($all_user_train_signed,8));
  192. //应到,但实到的
  193. $shidao = Db::name('user_train')->where('train_id',$id)->where('user_id','IN',$info['user_ids'])->count('id');
  194. //应到的
  195. $yingdao = $info['user_ids'] == 0 ? 0 :count(explode(',',$info['user_ids']));
  196. $result = [
  197. 'yingdao' => $yingdao,
  198. 'shidao' => $shidao,
  199. 'weidao' => $yingdao - $shidao,
  200. 'qita' => Db::name('user_train')->where('train_id',$id)->where('user_id','NOTIN',$info['user_ids'])->count('id'),
  201. ];
  202. $this->assign('result',$result);
  203. //
  204. $this->view->engine->layout(false);
  205. return $this->view->fetch();
  206. }
  207. /**
  208. * 未到用户
  209. */
  210. public function nograde(){
  211. $id = input('id');
  212. $info = Db::name('train_active')->where('id',$id)->find();
  213. $user_ids = $info['user_ids'];
  214. $grade_uids = Db::name('user_train')->where('train_id',$id)->column('user_id');
  215. $lists = Db::name('user')->where('id','IN',$user_ids)->where('id','NOTIN',$grade_uids)->select();
  216. $this->assign('lists',$lists);
  217. return $this->view->fetch();
  218. }
  219. /**
  220. * 穿梭
  221. */
  222. public function transfer(){
  223. //
  224. $this->view->engine->layout(false);
  225. return $this->view->fetch();
  226. }
  227. /**
  228. * 选人
  229. */
  230. public function selectuser(){
  231. $id = input('id');
  232. if($this->request->isPost()){
  233. $params = $this->request->post('row/a');
  234. $user_rule = explode(',',$params['user_rule']);
  235. $user_ids = [];
  236. foreach($user_rule as $key => $val){
  237. if(strpos($val,'u_') !== false){
  238. $user_ids[] = substr($val,2);
  239. }
  240. }
  241. $user_ids = implode(',',$user_ids);
  242. $update = [
  243. 'user_ids' => $user_ids,
  244. 'user_rule' => $params['user_rule'],
  245. 'userauth_status' => $params['userauth_status'],
  246. ];
  247. Db::name('train_active')->where('id',$id)->update($update);
  248. $this->success('设置成功');
  249. }
  250. $id = input('id');
  251. $row = Db::name('train_active')->where('id',$id)->find();
  252. $user_rule = explode(',', $row['user_rule']);
  253. $nodeList = \app\admin\model\User::getTreeList($user_rule);
  254. $this->assign("nodeList", $nodeList);
  255. $this->assign('row',$row);
  256. return $this->view->fetch();
  257. }
  258. }