123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 培训活动
- */
- class Trainactive extends Api
- {
- protected $noNeedLogin = ['info'];
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = ['test2'];
- //我的培训
- public function my_list(){
- $now = time();
- $papers = Db::name('train_active')->field('id,name as title,logo_image,starttime,endtime,pingjia_time,pingjia_uid')
- // ->where('status', 1)
- ->where('deletetime', NULL)
- ->where("(userauth_status = 1) or (find_in_set('".$this->auth->id."',user_ids) )")
- ->autopage()
- ->order('starttime desc')
- ->select();
- foreach($papers as $key => &$val){
- $val['logo_image'] = localpath_to_netpath($val['logo_image']);
- if($val['pingjia_time'] != 0){ //时间或评价,都行
- $val['show_status'] = 3;
- $val['show_status_text'] = '已结束';
- }elseif($now < $val['starttime']){
- $val['show_status'] = 1;
- $val['show_status_text'] = '待开始';
- }else{
- $val['show_status'] = 2;
- $val['show_status_text'] = '进行中';
- }
- }
- $this->success('', $papers);
- }
- //培训详情
- public function info(){
- $id = input('id',0);
- $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')
- ->where('id', $id)
- ->find();
- if(!empty($info)){
- $info['logo_image'] = localpath_to_netpath($info['logo_image']);
- $info['pingjia_nickname'] = '';
- if($info['pingjia_uid']){
- $info['pingjia_nickname'] = Db::name('user')->where('id',$info['pingjia_uid'])->value('nickname');
- }
- $now = time();
- if($info['pingjia_time'] != 0){ //时间或评价,都行
- $info['show_status'] = 3;
- $info['show_status_text'] = '已结束';
- }elseif($now < $info['starttime']){
- $info['show_status'] = 1;
- $info['show_status_text'] = '待开始';
- }else{
- $info['show_status'] = 2;
- $info['show_status_text'] = '进行中';
- }
- //总分
- //已评分人数
- //应该评分人数
- if($info['userauth_status'] == 0){ //是否允许非实名人员:0=否,1=是
- $info['score_times'] = count(explode(',',$info['user_ids']));
- }else{
- $info['score_times'] = Db::name('user_train')->where('train_id',$id)->count();
- }
- //我的评分
- $info['score_my'] = 0;
- if($this->auth->isLogin()){
- $score_my = Db::name('user_train_evaluate')->where('train_id',$id)->where('user_id',$this->auth->id)->value('score');
- $info['score_my'] = empty($score_my) ? 0 : $score_my;
- }
- //我是否能评价
- $info['score_button'] = 0;
- if($this->auth->isLogin()){
- if(empty($score_my)){
- if($info['userauth_status'] == 1){ //是否允许非实名人员:0=否,1=是
- $info['score_button'] = 1;
- }else{
- if(in_array($this->auth->id,explode(',',$info['user_ids']))){
- $info['score_button'] = 1;
- }
- }
- }
- }
- //
- unset($info['userauth_status']);
- unset($info['user_ids']);
- }
- $this->success(1,$info);
- }
- //签到成功
- public function signin(){
- $sign_image = input('sign_image','');
- if(empty($sign_image)){
- $this->error();
- }
- //
- $id = input('id');
- $info = Db::name('train_active')->where('id',$id)->find();
- if(empty($info)){
- $this->error('不存在的培训活动');
- }
- //判断
- if($info['status'] != 1){
- $this->error('该培训已下架');
- }
- if($info['userauth_status'] == 0){
- if(!in_array($this->auth->id,explode(',',$info['user_ids']))){
- $this->error('您不在该培训活动名单');
- }
- }
- //判断
- $map = [
- 'train_id' => $id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('user_train')->where($map)->find();
- if($check){
- $this->error('您已经签过到了');
- }
- //判断
- if(time() < $info['sign_starttime']){
- $this->error('还没到签到开始时间');
- }
- if(time() > $info['endtime']){
- $this->error('培训已经结束了');
- // $this->error('已错过签到截止时间');
- }
- //签到
- $status = 1;
- if(time() > $info['sign_endtime']){
- $status = 2;//迟到
- }
- $data = [
- 'train_id' => $id,
- 'user_id' => $this->auth->id,
- 'sign_time' => time(),
- 'status' => $status,
- 'sign_image' => $sign_image,
- ];
- Db::name('user_train')->insertGetId($data);
- $this->success('签到成功');
- }
- //参与者评价提交
- public function evaluate(){
- $score = input('score',6);
- if(!in_array($score,[6,7,8,9,10])){
- $this->error('最低6分,最高10分');
- }
- //
- $id = input('id');
- $info = Db::name('train_active')->where('id',$id)->find();
- if(empty($info)){
- $this->error('不存在的培训活动');
- }
- //判断
- if($info['status'] != 1){
- $this->error('该培训已下架');
- }
- if($info['userauth_status'] == 0){
- if(!in_array($this->auth->id,explode(',',$info['user_ids']))){
- $this->error('您不在该培训活动名单');
- }
- }
- //判断
- $map = [
- 'train_id' => $id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('user_train')->where($map)->find();
- if(empty($check)){
- $this->error('您还没签到呢');
- }
- //判断
- $map = [
- 'train_id' => $id,
- 'user_id' => $this->auth->id,
- ];
- $check = Db::name('user_train_evaluate')->where($map)->find();
- if($check){
- $this->error('您已经评分了');
- }
- //判断
- if(time() < $info['starttime']){
- $this->error('还没到培训开始时间');
- }
- if(time() > $info['endtime'] + 3600){
- $this->error('培训已经结束一小时了');
- }
- Db::startTrans();
- //入库
- $data = [
- 'train_id' => $id,
- 'user_id' => $this->auth->id,
- 'evaluate_time' => time(),
- 'score' => $score,
- ];
- $log_id = Db::name('user_train_evaluate')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('评分失败');
- }
- //冗余:打分人数,应该打分人数,总分数
- //总分
- $update['score_sum'] = Db::name('user_train_evaluate')->where('train_id',$id)->sum('score');
- //已评分人数
- $update['score_count'] = Db::name('user_train_evaluate')->where('train_id',$id)->count();
- //平均分
- $update['score_avg'] = bcdiv($update['score_sum'],$update['score_count'],1);
- //应该评分人数
- if($info['userauth_status'] == 0){ //是否允许非实名人员:0=否,1=是
- $update['score_times'] = count(explode(',',$info['user_ids']));
- }else{
- $update['score_times'] = Db::name('user_train')->where('train_id',$id)->count();
- }
- $rs = Db::name('train_active')->where('id',$id)->update($update);
- if($rs === false){
- Db::rollback();
- $this->error('评分失败');
- }
- Db::commit();
- $this->success('评分成功');
- }
- //评价检测权限
- public function check(){
- $type = input('type','pingjia');
- $id = input('id',0);
- if($type == 'pingjia'){
- $info = Db::name('train_active')->where('id',$id)->find();
- if($info['pingjia_uid'] != $this->auth->id){
- $this->error('您不能评价当前培训');
- }
- $this->success('验证成功');
- }
- if($type == 'sign'){
- $this->success('验证成功');
- }
- $this->error('验证失败');
- }
- //评价成功
- public function pingjia(){
- $pingjia = input('pingjia','');
- $pingjia_image = input('sign_image','');
- if(empty($pingjia) || empty($pingjia_image)){
- $this->error();
- }
- //
- $id = input('id');
- $info = Db::name('train_active')->where('id',$id)->find();
- if(empty($info)){
- $this->error('不存在的培训活动');
- }
- //判断
- if($info['status'] != 1){
- $this->error('该培训已下架');
- }
- if($this->auth->id != $info['pingjia_uid']){
- $this->error('您不能评价该培训活动');
- }
- //评价
- $data = [
- 'pingjia' => $pingjia,
- 'pingjia_image' => $pingjia_image,
- 'pingjia_time' => time(),
- 'status' => 0, //评价结束,自动下架
- ];
- Db::name('train_active')->where('id',$id)->update($data);
- $this->success('评价成功');
- }
- }
|