Trainactive.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 培训活动
  7. */
  8. class Trainactive extends Api
  9. {
  10. protected $noNeedLogin = ['info'];
  11. // 无需鉴权的接口,*表示全部
  12. protected $noNeedRight = ['test2'];
  13. //我的培训
  14. public function my_list(){
  15. $now = time();
  16. $papers = Db::name('train_active')->field('id,name as title,logo_image,starttime,endtime,pingjia_time,pingjia_uid')
  17. // ->where('status', 1)
  18. ->where('deletetime', NULL)
  19. ->where("(userauth_status = 1) or (find_in_set('".$this->auth->id."',user_ids) )")
  20. ->autopage()
  21. ->order('starttime desc')
  22. ->select();
  23. foreach($papers as $key => &$val){
  24. $val['logo_image'] = localpath_to_netpath($val['logo_image']);
  25. if($val['pingjia_time'] != 0){ //时间或评价,都行
  26. $val['show_status'] = 3;
  27. $val['show_status_text'] = '已结束';
  28. }elseif($now < $val['starttime']){
  29. $val['show_status'] = 1;
  30. $val['show_status_text'] = '待开始';
  31. }else{
  32. $val['show_status'] = 2;
  33. $val['show_status_text'] = '进行中';
  34. }
  35. }
  36. $this->success('', $papers);
  37. }
  38. //培训详情
  39. public function info(){
  40. $id = input('id',0);
  41. $info = Db::name('train_active')->field('id,name as title,logo_image,starttime,endtime,pingjia,pingjia_time,pingjia_uid,userauth_status,user_ids,score_sum,score_count')
  42. ->where('id', $id)
  43. ->find();
  44. if(!empty($info)){
  45. $info['logo_image'] = localpath_to_netpath($info['logo_image']);
  46. $info['pingjia_nickname'] = '';
  47. if($info['pingjia_uid']){
  48. $info['pingjia_nickname'] = Db::name('user')->where('id',$info['pingjia_uid'])->value('nickname');
  49. }
  50. $now = time();
  51. if($info['pingjia_time'] != 0){ //时间或评价,都行
  52. $info['show_status'] = 3;
  53. $info['show_status_text'] = '已结束';
  54. }elseif($now < $info['starttime']){
  55. $info['show_status'] = 1;
  56. $info['show_status_text'] = '待开始';
  57. }else{
  58. $info['show_status'] = 2;
  59. $info['show_status_text'] = '进行中';
  60. }
  61. //总分
  62. //已评分人数
  63. //应该评分人数
  64. if($info['userauth_status'] == 0){ //是否允许非实名人员:0=否,1=是
  65. $info['score_times'] = count(explode(',',$info['user_ids']));
  66. }else{
  67. $info['score_times'] = Db::name('user_train')->where('train_id',$id)->count();
  68. }
  69. //我的评分
  70. $info['score_my'] = 0;
  71. if($this->auth->isLogin()){
  72. $score_my = Db::name('user_train_evaluate')->where('train_id',$id)->where('user_id',$this->auth->id)->value('score');
  73. $info['score_my'] = empty($score_my) ? 0 : $score_my;
  74. }
  75. //我是否能评价
  76. $info['score_button'] = 0;
  77. if($this->auth->isLogin()){
  78. if(empty($score_my)){
  79. if($info['userauth_status'] == 1){ //是否允许非实名人员:0=否,1=是
  80. $info['score_button'] = 1;
  81. }else{
  82. if(in_array($this->auth->id,explode(',',$info['user_ids']))){
  83. $info['score_button'] = 1;
  84. }
  85. }
  86. }
  87. }
  88. //
  89. unset($info['userauth_status']);
  90. unset($info['user_ids']);
  91. }
  92. $this->success(1,$info);
  93. }
  94. //签到成功
  95. public function signin(){
  96. $sign_image = input('sign_image','');
  97. if(empty($sign_image)){
  98. $this->error();
  99. }
  100. //
  101. $id = input('id');
  102. $info = Db::name('train_active')->where('id',$id)->find();
  103. if(empty($info)){
  104. $this->error('不存在的培训活动');
  105. }
  106. //判断
  107. if($info['status'] != 1){
  108. $this->error('该培训已下架');
  109. }
  110. if($info['userauth_status'] == 0){
  111. if(!in_array($this->auth->id,explode(',',$info['user_ids']))){
  112. $this->error('您不在该培训活动名单');
  113. }
  114. }
  115. //判断
  116. $map = [
  117. 'train_id' => $id,
  118. 'user_id' => $this->auth->id,
  119. ];
  120. $check = Db::name('user_train')->where($map)->find();
  121. if($check){
  122. $this->error('您已经签过到了');
  123. }
  124. //判断
  125. if(time() < $info['sign_starttime']){
  126. $this->error('还没到签到开始时间');
  127. }
  128. if(time() > $info['endtime']){
  129. $this->error('培训已经结束了');
  130. // $this->error('已错过签到截止时间');
  131. }
  132. //签到
  133. $status = 1;
  134. if(time() > $info['sign_endtime']){
  135. $status = 2;//迟到
  136. }
  137. $data = [
  138. 'train_id' => $id,
  139. 'user_id' => $this->auth->id,
  140. 'sign_time' => time(),
  141. 'status' => $status,
  142. 'sign_image' => $sign_image,
  143. ];
  144. Db::name('user_train')->insertGetId($data);
  145. $this->success('签到成功');
  146. }
  147. //参与者评价提交
  148. public function evaluate(){
  149. $score = input('score',6);
  150. if(!in_array($score,[6,7,8,9,10])){
  151. $this->error('最低6分,最高10分');
  152. }
  153. //
  154. $id = input('id');
  155. $info = Db::name('train_active')->where('id',$id)->find();
  156. if(empty($info)){
  157. $this->error('不存在的培训活动');
  158. }
  159. //判断
  160. if($info['status'] != 1){
  161. $this->error('该培训已下架');
  162. }
  163. if($info['userauth_status'] == 0){
  164. if(!in_array($this->auth->id,explode(',',$info['user_ids']))){
  165. $this->error('您不在该培训活动名单');
  166. }
  167. }
  168. //判断
  169. $map = [
  170. 'train_id' => $id,
  171. 'user_id' => $this->auth->id,
  172. ];
  173. $check = Db::name('user_train')->where($map)->find();
  174. if(empty($check)){
  175. $this->error('您还没签到呢');
  176. }
  177. //判断
  178. $map = [
  179. 'train_id' => $id,
  180. 'user_id' => $this->auth->id,
  181. ];
  182. $check = Db::name('user_train_evaluate')->where($map)->find();
  183. if($check){
  184. $this->error('您已经评分了');
  185. }
  186. //判断
  187. if(time() < $info['starttime']){
  188. $this->error('还没到培训开始时间');
  189. }
  190. if(time() > $info['endtime'] + 3600){
  191. $this->error('培训已经结束一小时了');
  192. }
  193. Db::startTrans();
  194. //入库
  195. $data = [
  196. 'train_id' => $id,
  197. 'user_id' => $this->auth->id,
  198. 'evaluate_time' => time(),
  199. 'score' => $score,
  200. ];
  201. $log_id = Db::name('user_train_evaluate')->insertGetId($data);
  202. if(!$log_id){
  203. Db::rollback();
  204. $this->error('评分失败');
  205. }
  206. //冗余:打分人数,应该打分人数,总分数
  207. //总分
  208. $update['score_sum'] = Db::name('user_train_evaluate')->where('train_id',$id)->sum('score');
  209. //已评分人数
  210. $update['score_count'] = Db::name('user_train_evaluate')->where('train_id',$id)->count();
  211. //平均分
  212. $update['score_avg'] = bcdiv($update['score_sum'],$update['score_count'],1);
  213. //应该评分人数
  214. if($info['userauth_status'] == 0){ //是否允许非实名人员:0=否,1=是
  215. $update['score_times'] = count(explode(',',$info['user_ids']));
  216. }else{
  217. $update['score_times'] = Db::name('user_train')->where('train_id',$id)->count();
  218. }
  219. $rs = Db::name('train_active')->where('id',$id)->update($update);
  220. if($rs === false){
  221. Db::rollback();
  222. $this->error('评分失败');
  223. }
  224. Db::commit();
  225. $this->success('评分成功');
  226. }
  227. //评价检测权限
  228. public function check(){
  229. $type = input('type','pingjia');
  230. $id = input('id',0);
  231. if($type == 'pingjia'){
  232. $info = Db::name('train_active')->where('id',$id)->find();
  233. if($info['pingjia_uid'] != $this->auth->id){
  234. $this->error('您不能评价当前培训');
  235. }
  236. $this->success('验证成功');
  237. }
  238. if($type == 'sign'){
  239. $this->success('验证成功');
  240. }
  241. $this->error('验证失败');
  242. }
  243. //评价成功
  244. public function pingjia(){
  245. $pingjia = input('pingjia','');
  246. $pingjia_image = input('sign_image','');
  247. if(empty($pingjia) || empty($pingjia_image)){
  248. $this->error();
  249. }
  250. //
  251. $id = input('id');
  252. $info = Db::name('train_active')->where('id',$id)->find();
  253. if(empty($info)){
  254. $this->error('不存在的培训活动');
  255. }
  256. //判断
  257. if($info['status'] != 1){
  258. $this->error('该培训已下架');
  259. }
  260. if($this->auth->id != $info['pingjia_uid']){
  261. $this->error('您不能评价该培训活动');
  262. }
  263. //评价
  264. $data = [
  265. 'pingjia' => $pingjia,
  266. 'pingjia_image' => $pingjia_image,
  267. 'pingjia_time' => time(),
  268. 'status' => 0, //评价结束,自动下架
  269. ];
  270. Db::name('train_active')->where('id',$id)->update($data);
  271. $this->success('评价成功');
  272. }
  273. }