123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- <?php
- declare(strict_types=1);
- namespace app\mobile\controller;
- use think\facade\Request;
- use think\facade\Log;
- use app\model\Org;
- use app\model\TrainClassCate;
- use app\model\TrainClass;
- use app\model\TrainDoneLog;
- use app\model\User;
- use app\model\Employee;
- use app\model\TrainCourse;
- use app\model\TrainSumup;
- use app\model\TrainThumbLog;
- use think\facade\Console;
- use app\model\Lecturer;
- use app\model\LecturerCompany;
- use app\model\TrainCourseView;
- use app\model\TrainClassView;
- use app\model\CreditsSetting;
- use app\model\TrainCredits;
- use app\model\PreformanceTasks;
- use app\model\PreformanceTasksOrg;
- class Train extends Base
- {
- /**
- * 一级分类
- */
- public function getCate()
- {
- $w[] = ['root_id','=',$this->rootId];
- $w[] = ['pid','=',0];
- $list = TrainClassCate::where($w)->field('id,name')->order(['order'=>'asc','id'=>'desc'])->select();
- return json(['code' => 0, 'data' => $list]);
- }
- /**
- * 店面课程列表
- * 培训课程列表from_type=3查询0,2
- * from_type:0自建,1集团指派,2店面共享
- */
- public function classList()
- {
- $param = Request::only(['page'=>1,'limit'=>10,'from_type'=>0,'cate'=>'','keyword'=>'','label'=>'']);
- $page = $param['page'];
- $limit = $param['limit'];
- $from_type = $param['from_type'];
- $cate = $param['cate'];
- $keyword = $param['keyword'];
- $label = $param['label'];
- $eid = $this->employeeId;
- $condition = [
- ['root_id','=',$this->rootId],
- ['del','=',0],
- ['publish','=',1]
- ];
- $condition[] = $from_type==3 ? ['from_type','in',[0,2]] : ['from_type','=',$from_type];
- if ($cate) $condition[] = ['cate','=',$cate];
- if ($label) $condition[] = ['label','=',$label];
- if ($keyword) $condition[] = ['title','like','%'.trim($keyword).'%'];
-
- $sql = TrainClass::field('id,title,cover,course_id,category,view,train_employee,addtime,type,from_type');
- //集团指派课程全员展示
- if($from_type != 1)
- {
- $sql->where(function ($query) use ($eid){
- $query->whereRaw("FIND_IN_SET(" . $eid . " , train_employee)")
- ->whereOr('train_employee', '=', '');
- });
- }
- $data = $sql->where($condition)->page((int)$page , (int)$limit)->order('addtime desc')->select()->toArray();
- $column_class_id = array_column($data , 'id');
- $doneLog = TrainDoneLog::where([['from','=',0],['class_id','in',$column_class_id] , ['root_id','=',$this->rootId] , ['done_percent','>',0]])->group('class_id')->column('count(class_id)','class_id');
-
- //查询最近完成人的头像取6个
- $res = TrainDoneLog::with(['employee'])->where([['from','=',0],['class_id','in',$column_class_id] , ['root_id','=',$this->rootId] , ['done_percent','=',100]])->order('addtime desc')->select()->toArray();
- $uid = array_column($res , 'uid');
- $user = User::where('id','in',$uid)->column('headimgurl','id');
- $headimgurl = [];
- foreach($res as &$r)
- {
- $headimgurl[$r['class_id']][] = isset($user[$r['uid']]) ? $user[$r['uid']] : '';
- }
- // 查询课程多少人参与,课程章节数
- foreach($data as &$item){
- $item['train_employee_count'] = count(explode(',',$item['train_employee']));
- $item['course_num'] = empty($item['course_id']) ? 0 : count(explode(',',$item['course_id']));
- $item['done_log_num'] = isset($doneLog[$item['id']]) ? $doneLog[$item['id']] : 0;
- $item['done_log_num'] = $item['view']>=$item['train_employee_count'] ? $item['train_employee_count'] : $item['view'];//学习人数太少先用浏览量展示
- $item['headimgurl'] = isset($headimgurl[$item['id']]) ? $headimgurl[$item['id']] : [];
- }
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 学习中心首页轮播图
- */
- public function banner()
- {
- $img = ['Train/6466e5d1f3385.png'];
- $url = config('app.ali_oss_bindurl');
- foreach ($img as $key => $value) {
- $img[$key] = 'https://' . $url . '/' . $value;
- }
- return json(['code' => 0, 'data' => $img,'msg'=>'']);
- }
- /**
- * 学习中心首页精品课程
- */
- public function boutiqueTrain()
- {
- $limit = input('limit',4,'intval');
- $res = TrainClass::withCount(['doneLog'])->where([['root_id','=',0],['publish','=',1],['del','=',0]])->visible(['id','cover','title','done_log_count'])->order('view desc')->limit($limit)->select()->toArray();
- foreach ($res as $key => $value) {
- $res[$key]['position'] = '威智慧 · 讲师';
- }
- return json(['code' => 0, 'data' => $res,'msg'=>'']);
- }
- /**
- * 讲师团队
- */
- public function lecturerList()
- {
- $limit = input('limit',4,'intval');
- $res = Lecturer::with(['company'=>function($query){
- $query->field('id,name')->bind(['company_name'=>'name']);
- }])->where([['root_id','=',0],['del','=',0]])->order('id desc')->limit($limit)->select()->toArray();
- foreach ($res as $key => $value) {
- // $res[$key]['company'] = '威智慧';
- }
- return json(['code' => 0, 'data' => $res,'msg'=>'']);
- }
- /**
- * 首页直播列表
- */
- public function broadcastList()
- {
- $limit = input('limit',4,'intval');
- $res = Lecturer::with(['company'=>function($query){
- $query->field('id,name')->bind(['company_name'=>'name']);
- }])->where([['root_id','=',0],['del','=',0]])->order('id desc')->limit($limit)->select()->toArray();
- foreach ($res as $key => $value) {
- // $res[$key]['company'] = '威智慧';
- }
- return json(['code' => 0, 'data' => $res,'msg'=>'']);
- }
- /**
- * 课程详情
- */
- public function classRead($class_id)
- {
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $token['uid'] = $this->uid;
- $from = input('from',0) ? 1 : 0;
- $data = TrainClass::where([['id','=',$class_id],['root_id','in',[$token['root_org'],0]],['del','=',0],['publish','=',1]])->field('imgs,introduce,id,title,cover,des,course_id,type,category,train_employee,addtime,from_type,from_root_id')->findOrEmpty();
- if($data->isEmpty()) return json(['code' => 0, 'data' => []]);
- //课件数量
- $data['course_num'] = empty($data['course_id']) ? 0 : count(explode(',',$data['course_id']));
- //已经学习人数
- $data['done_log_num'] = TrainDoneLog::where([['from','=',$from],['class_id','=',$data['id']] , ['root_id','in',[$token['root_org'],0]] , ['done_percent','>',0]])->count();
-
- $data['is_success_class'] = false;//是否全部完成
- $data['is_course_num'] = 0;//完成课件数量
- $myDoneLog = TrainDoneLog::where([['from','=',$from],['employee_id','=',$token['employee_id']] , ['class_id','=',$data['id']] , ['root_id','in',[$token['root_org'],0]]])->findOrEmpty();
- if (!$myDoneLog->isEmpty()) {
- $data['is_success_class'] = $myDoneLog->done_percent==100 ? true : false;//是否全部完成
- $data['is_course_num'] = $myDoneLog->course_id ? count(explode(',',$myDoneLog->course_id)) : 0;//完成课件数量
- }
- $data['train_employee_num'] = $data->from_type = 1 ? '全员' : ($data->train_employee ? count(explode(',',$data->train_employee)) : 0);//学习总人数
- //全部完成人员信息
- $myDoneLog = TrainDoneLog::where([['from','=',$from],['class_id','=',$data['id']] , ['root_id','=',$token['root_org']],['done_percent','=',100]])->order('id desc')->group('employee_id')->limit(3)->column('employee_id');
- $tw[] = ['id','in',$myDoneLog];
- $data['train_employee_info'] = Employee::with('userImg')->where($tw)->visible(['name','img'])->select()->toArray();//
- $data['train_employee_info_count'] = count($data['train_employee_info']);
-
- //课程来源
- if ($data['from_type'] == 0)
- {
- $data['from'] = '店面发布';
- } elseif ($data['from_type'] == 1) {
- $data['from'] = '集团指派';
- } else {
- $company = Company::where('root_id',$data['from_root_id'])->value('company_name');
- $data['from'] = '集团从'.$company.'共享';
- }
- //浏览量+1
- // $rand = mt_rand(1,10);
- $view = input('view', '', 'trim');
- if ($view){
- TrainClass::where('id',$class_id)->inc('view',1)->update();
- }
- //当前评论数量
- $data['sumup_count'] = TrainSumup::where([['class_id','=',$class_id],['course_id','=',0]])->count();
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 课程课件列表
- */
- public function trainCourseList($page,$class_id,$course_id=0)
- {
- $from = input('from',0) ? 1 : 0;
- $class = TrainClass::where([['id','=',$class_id ], ['root_id','in',[$this->rootId,0] ], ['publish','=',1],['del','=',0]])->field('course_id,type')->findOrEmpty();
- if($class->isEmpty()) return json(['code' => 0, 'data' => []]);
- $data = TrainCourse::field('id,title,type')->where([['id', 'in', $class['course_id']],['root_id','in',[$this->rootId,0]]])->page((int)$page , 40)->order('id desc')->select()->toArray();
- $employee_id = $this->employeeId;
- $doneLog = TrainDoneLog::where(['employee_id'=>$employee_id ,'from'=>$from ,'class_id'=>$class_id])->value('course_id');
- // 查询课件完成情况
- $arr = ['image'=>'图文课件','video'=>'视频','pdf'=>'PDF文件','audio'=>'音频'];
- $start_count = 0;
- if ($doneLog) {
- $doneLogArr = explode(',',$doneLog);
- $start_count = count($doneLogArr);
- foreach($data as $key => &$item){
- $item['done_state'] = $data[$key]['done_state'] = in_array($item['id'],$doneLogArr);
- $item['is_now'] = $course_id==$item['id'] ? true : false;
- $item['type_name'] = $arr[$item['type']];
- //是否可以学习
- $item['switch'] = ($class->type=='free' || $key==0) ? true : (isset($data[$key-1])&&$data[$key-1]['done_state'] ? true : false);
- }
- }else{
- $start_count = 0;
- foreach($data as $key => &$item){
- $item['done_state'] = false;
- $item['is_now'] = $course_id==$item['id'] ? true : false;
- $item['type_name'] = $arr[$item['type']];
- //是否可以学习
- $item['switch'] = ($class->type=='free'||$key==0) ? true : false;
- }
- }
- $count = count($data);
- return json(['code' => 0, 'data' => $data,'count'=>$count,'start_count'=>$start_count]);
- }
- /*
- * 课程观看感、课件读后感
- */
- public function watchFeel($page , $class_id , $course_id=0)
- {
- $course_id = empty($course_id) ? 0 : $course_id;
- $sumup = TrainSumup::with(['employee','user'])->where([['course_id','=',$course_id],['class_id','=',$class_id] , ['root_id','in',[$this->rootId,0]]])->page((int)$page , 10)->order('id desc')->select()->toArray();
- $column_sumup_id = array_column($sumup,'id');
-
- $employee_id = $this->employeeId;
- $log = TrainThumbLog::where([['sumup_id','in',$column_sumup_id] , ['employee_id','=',$employee_id] , ['root_id','in',[$this->rootId,0]]])->column('id','sumup_id');
- $class = TrainClass::where('id',$class_id)->field('sumup_keyword,sumup_score')->find();
- foreach($sumup as &$item)
- {
- $item['score'] = TrainSumup::sumup_score_num($class['sumup_keyword'] , $class['sumup_score'] , $item['content']);
- $item['thumblog'] = isset($log[$item['id']]) ? true : false;
- $item['addtime'] = date('Y-m-d',strtotime($item['addtime']));
- }
- $count = TrainSumup::where([['course_id','=',$course_id],['class_id','=',$class_id] , ['root_id','in',[$this->rootId,0]]])->count();
- return json(['code' => 0, 'data' => $sumup, 'count' => $count]);
- }
- /*
- * 根据时间计算距离现在多久
- */
- private function date_switch($time)
- {
- $now = time();
- $old = strtotime(date($time));
- $dur = $now - $old;
- if($dur <= 0){
- return $time;
- }elseif($dur < 60){
- return $dur.'秒前';
- }elseif($dur < 3600){
- return floor($dur/60).'分钟前';
- }elseif($dur < 86400){
- return floor($dur/3600).'小时前';
- }elseif($dur < 2678400){
- return floor($dur/86400).'天前';
- }elseif($dur < 2678400*12){
- return floor($dur/2678400).'月前';
- }else{
- return floor($dur/2678400/12).'年前';
- }
- }
- /*
- * 评论
- */
- public function commentAdd()
- {
- $param = request()->param(['class_id','course_id'=>0,'content','read_status','from'=>0,'study_time'=>0]);
- $where = [['root_id','in',[$this->rootId,0]],['id','=',$param['class_id']]];
- if($param['course_id']) $where[] = ['course_id','find in set',$param['course_id']];
- $TrainClass = TrainClass::where($where)->field('sumup_num,course_id')->findOrEmpty();
- if($TrainClass->isEmpty()) return json(['code' => 1, 'msg' => '提交失败1']);
- $data = [
- 'user_id' => $this->uid,
- 'root_id' => $this->rootId,
- 'employee_id' => $this->employeeId,
- 'org_id' => Employee::where('id',$this->employeeId)->value('org_id'),
- 'class_id' => $param['class_id'],
- 'course_id' => empty($param['course_id']) ? 0 : $param['course_id'],
- 'content' => $param['content']
- ];
-
- if(empty($param['course_id']) && $TrainClass->root_id)
- {
- //课程评论验证字数是否达标
- if(mb_strlen($param['content']) < $TrainClass['sumup_num']) return json(['code' => 1, 'msg' => '课程感悟不低于'.$TrainClass['sumup_num'].'字数']);
- }
-
- if(!empty($param['course_id']) && $TrainClass->root_id)
- {
- //查询课件评论字数限制
- $comment_word_num = TrainCourse::where(['root_id'=>$this->rootId , 'id'=>$param['course_id']])->value('comment_word_num');
- if($comment_word_num && mb_strlen($data['content'])<$comment_word_num) return json(['code' => 1, 'msg' => '课件评论不低于'.$comment_word_num.'字数']);
- //课件评论验证完成情况
- if(isset($param['read_status']) && $param['read_status'] == true)
- {
- $doneLog = TrainDoneLog::where(['from'=>$param['from'],'employee_id'=>$this->employeeId,'root_id'=>$this->rootId,'class_id'=>$param['class_id']])->find();
- $log_course_id = !$doneLog ? [] : explode(',',$doneLog['course_id']);
- if(!in_array($param['course_id'] , $log_course_id)){
- //完成课件操作
- $this->course_finish($TrainClass['course_id'],$doneLog,$param['course_id'],$param['class_id'],$param['from'],$param['study_time']);
-
- }
-
- }
- }
-
- if (TrainSumup::insert($data)) {
- //增加提交感悟送学分
- if($param['course_id']==0){
- $this->feeling_credits($this->rootId,$this->employeeId);
- }
- return json(['code' => 0, 'msg' => '提交成功']);
- } else {
- return json(['code' => 1, 'msg' => '提交失败']);
- }
- }
- /*
- * 精品课程添加评论 ,课件添加评论
- */
- public function boutiqueCommentAdd()
- {
- $param = request()->param(['class_id','content','course_id'=>0]);
- $where = [
- ['id','=',$param['class_id']],
- ['root_id','=',0]
- ];
- if($param['course_id']) $where[] = ['course_id','find in set',$param['course_id']];
- $check = TrainClass::where($where)->findOrEmpty();
- if($check->isEmpty()) return json(['code' => 1, 'msg' => '提交失败']);
- $data = [
- 'user_id' => $this->uid,
- 'root_id' => $this->rootId,
- 'employee_id' => $this->employeeId,
- 'org_id' => Employee::where('id',$this->employeeId)->value('org_id'),
- 'class_id' => $param['class_id'],
- 'course_id' => $param['course_id'],
- 'content' => $param['content']
- ];
- if (TrainSumup::insert($data)) {
- return json(['code' => 0, 'msg' => '提交成功']);
- } else {
- return json(['code' => 1, 'msg' => '提交失败']);
- }
- }
- //计算提交感悟送学分
- public function feeling_credits($root_id,$employee_id)
- {
- $maxcredit=$this->sumday_credit($root_id,$employee_id);
- $where[] = ['code', '=', 'studyfeeling_credit'];
- $where[] = ['root_id', '=', $root_id];
- $studyfeeling = CreditsSetting::where($where)->field('status,value')->find();
- if(empty($studyfeeling)) return;//如果没有设置感悟送学分直接返回
- $you=TrainCredits::where([['employee_id','=',$employee_id],['root_id','=',$root_id]])->order('id desc')->find();
- if($studyfeeling['status']==0 && $studyfeeling['value']>0){
- if(empty($maxcredit) || $maxcredit['status']==1 ||($maxcredit['status']!=1 && $maxcredit['yes_course'] > $studyfeeling['value'])){
-
- $add=['employee_id'=>$employee_id,'root_id'=>$root_id,'type'=>3,'credits'=>$studyfeeling['value'],'now_credits'=>$studyfeeling['value']];
- if(!empty($you)){
- $add['now_credits']=$you['now_credits']+$studyfeeling['value'];
- }
- $this->add_credit($add);
- }
- if(!empty($maxcredit) && $maxcredit['status']==0 && $maxcredit['yes_course']!=0 && $maxcredit['yes_course'] < $studyfeeling['value']){
- $add=['employee_id'=>$employee_id,'root_id'=>$root_id,'type'=>3,'credits'=>$maxcredit['yes_course'],'now_credits'=>$maxcredit['yes_course']+ (!empty($you['now_credits'])?$you['now_credits']:0)];
- $this->add_credit($add);
- }
- }
- }
- /*
- * 课件完成操作
- */
- private function course_finish($trainClassCourseId,$doneLog,$paramCourseId,$paramClassId,$from=0,$study_time=0)
- {
- //课程绑定的课件
- $course_id = explode(',',$trainClassCourseId);
- $class=TrainClass::find($paramClassId);
- if(!empty($doneLog))
- {
- $have = explode(',',$doneLog['course_id']);
- $checkCourse = [$paramCourseId];
- $newCourseArr = array_merge($have,$checkCourse);
-
- $GDP = floor((count($newCourseArr) / count($course_id))*100);
-
- $update = [
- 'done_percent' => $GDP,
- 'course_id' => implode(',',$newCourseArr)
- ];
- $res = TrainDoneLog::where(['from'=>$from,'employee_id'=>$this->employeeId,
- 'root_id'=>$this->rootId,
- 'class_id'=>$paramClassId])->update($update);
- if($res){
- if ($GDP==100) {//累计勋章
- $employee_id = $this->employeeId;
- $root_id = $this->rootId;
- Console::call('medal',['class',(string)$employee_id,(string)$root_id]);
- //完成课程增加学分
-
- $this->add_studylog($employee_id,$class,$root_id);
- }
- //完成单个课件增加学习时长
- $this->add_studytime($this->employeeId,$paramClassId,$paramCourseId,$study_time,$this->rootId);
- return true;
- }else{
- return false;
- }
- }else{
- $GDP = floor((1 / count($course_id))*100);
- //第一次完成课件直接添加
- if(TrainDoneLog::insert([
- 'employee_id' => $this->employeeId,
- 'root_id' => $this->rootId,
- 'org_id' => Employee::where('id',$this->employeeId)->value('org_id'),
- 'class_id' => $paramClassId,
- 'done_percent' => $GDP,
- 'course_id' => $paramCourseId,
- 'from' => $from
- ])){
- if ($GDP==100) {//累计勋章
- $employee_id = $this->employeeId;
- $root_id = $this->rootId;
- Console::call('medal',['class',(string)$employee_id,(string)$root_id]);
- //完成课程增加学分
- $this->add_studylog($employee_id,$class,$root_id);
- }
- //完成单个课件增加学习时长
- $this->add_studytime($this->employeeId,$paramClassId,$paramCourseId,$study_time,$this->rootId);
- return true;
- }else{
- return false;
- }
- }
- }
- //增加课程的学习完成记录
- public function add_studylog($employee_id, $class, $root_id)
- {
- if (empty($class['credit'])) return; //如果没有设置学分直接返回
- $maxcredit = $this->sumday_credit($root_id, $employee_id);
- $you = TrainCredits::where([['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->order('id desc')->find();
- if (empty($maxcredit) || $maxcredit['status'] == 1 || ($maxcredit['status'] == 0 && $maxcredit['yes_course'] > $class['credit'])) {
- $add = ['employee_id' => $employee_id, 'root_id' => $root_id, 'type' => 2, 'credits' => $class['credit'], 'now_credits' => $class['credit']];
- if (!empty($you)) {
- $add['now_credits'] = $you['now_credits'] + $class['credit'];
- }
- }
- if (!empty($maxcredit) && $maxcredit['status'] != 1 && $maxcredit['yes_course'] != 0 && $maxcredit['yes_course'] < $class['credit']) {
- $add = ['employee_id' => $employee_id, 'root_id' => $root_id, 'type' => 2, 'credits' => $maxcredit['yes_course'], 'now_credits' => $maxcredit['yes_course'] + (!empty($you['now_credits'])?$you['now_credits']:0)];
- }
- if (!empty($add)) {
- $this->add_credit($add);
- }
- }
- //增加课程的学习时间
- public function add_studytime($employee_id,$class_id,$course_id,$study_time,$root_id)
- {
- $where[] = ['code', '=', 'studytime_credit'];
- $where[] = ['root_id', '=', $root_id];
- $time_study=CreditsSetting::where($where)->field('status,value')->find();
- if(empty($time_study)) return;//如果没有设置学习时长直接返回;
- if($time_study['status']==0){
- $add=['empid'=>$employee_id,'classid'=>$class_id,'course_id'=>$course_id,'study_time'=>$study_time,'all_time'=>$study_time,'type'=>1];
- $you=TrainCouresStudytime::where([['empid','=',$employee_id]])->order('id desc')->find();
- if(!empty($you)){
- $add['all_time']=$you['all_time']+$study_time;
- }
- TrainCouresStudytime::insert($add);
- $this->call_studytime($employee_id);//增加完学习时长直接开始计算是否送学分
- }
- }
- //计算学习时长送学分
- public function call_studytime($employee_id)
- {
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $token['uid'] = $this->uid;
-
- $maxcredit=$this->sumday_credit($token['root_org'],$token['employee_id']);
- $all_time=TrainCouresStudytime::where('empid',$employee_id)->order('id desc')->value('all_time');
- $where[] = ['code', '=', 'studytime_credit'];
- $where[] = ['root_id', '=', $token['root_org']];
- $time_study=CreditsSetting::where($where)->field('status,value')->find();
- if(empty($time_study)) return;//如果没有设置学习时长直接返回
- $time_study['value']=!empty($time_study['value'])?json_decode($time_study['value'],true):['numtime' => 0, 'time_credit' => 0];
- if($time_study['status']==0 && $time_study['value']['numtime'] > 0 && $all_time >= $time_study['value']['numtime']*60){
- $b=floor($all_time/($time_study['value']['numtime']*60));
- $credits=$b*$time_study['value']['time_credit'];
- $reduce_time=$b*($time_study['value']['numtime']*60);
- $you=TrainCredits::where([['employee_id','=',$token['employee_id']],['root_id','=',$token['root_org']]])->order('id desc')->find();
- //正常情况所得学分不大于当天可获得最高学分
- if(empty($maxcredit) || $maxcredit['status']==1 ||($maxcredit['status']==0 && $maxcredit['yes_course'] > $credits)){
- $add=['employee_id'=>$token['employee_id'],'root_id'=>$token['root_org'],'type'=>1,'credits'=>$credits,'now_credits'=>$credits];
- if(!empty($you)){
- $add['now_credits']=$you['now_credits']+$credits;
- }
- $this->add_credit($add);
- $newadd=['empid'=>$token['employee_id'],'study_time'=>$reduce_time,'all_time'=>$all_time-$reduce_time,'type'=>2];
- $newadd['all_time'] = $newadd['all_time']>0 ? $newadd['all_time']:0;
- TrainCouresStudytime::insert($newadd);
- }
- //所得学分大于当天剩余可得学分时取差值补满当天最高学分
- if(!empty($maxcredit) && $maxcredit['status']==0 && $maxcredit['yes_course']!=0 && $maxcredit['yes_course'] < $credits){
- $add=['employee_id'=>$token['employee_id'],'root_id'=>$token['root_org'],'type'=>1,'credits'=>$maxcredit['yes_course'],'now_credits'=>$maxcredit['yes_course']+ (!empty($you['now_credits'])?$you['now_credits']:0)];
- $this->add_credit($add);
- $newadd=['empid'=>$token['employee_id'],'study_time'=>$reduce_time,'all_time'=>0,'type'=>2];
- TrainCouresStudytime::insert($newadd);
- }
- // if($maxcredit['status']!=1 && $maxcredit['yes_course'] < $credits){
- // $newadd=['empid'=>$token['employee_id'],'study_time'=>$reduce_time,'all_time'=>0,'type'=>2];
- // TrainCouresStudytime::insert($newadd);
- // }
- }
- }
- //赠送学分统一方法方便计划任务处理
- public function add_credit($add)
- {
- $id=TrainCredits::insertGetId($add);
- $this->preformance_tasks($add['credits'], $id);
- }
- /**
- * 计算业绩任务
- */
- public function preformance_tasks($credit = 0, $vislog_id)
- {
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $token['uid'] = $this->uid;
- //防止后台切换部门
- $token['org_id'] = Employee::where('id', $token['employee_id'])->value('org_id');
- $time = date('Y-m-d H:i:s');
- //查询进行中的指派到所属部门的所有进度
- // $w[] = ['org_id','=',$token['org_id']];
- $w[] = ['root_id', '=', $token['root_org']];
- $w[] = ['start_date', '<=', $time];
- $w[] = ['end_date', '>=', $time];
- $w[] = ['is_credit', '=', 1];
- $preformance_tasks = PreformanceTasks::where($w)->column('id');
- $w1[] = ['root_id', '=', $token['root_org']];
- $w1[] = ['org_id', '=', $token['org_id']];
- $w1[] = ['performance_tasks_id', 'in', $preformance_tasks];
- $w1[] = ['is_credit', '=', 1];
- //指派到自己部门
- $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*');
- $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')];
- $model = PreformanceTasks::where($w4)->column('*');
- foreach ($model as $k => $v) {
- $u = [];
- $u['ok_credit'] = $v['ok_credit'] + $credit;
- $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id;
- PreformanceTasks::where('id', $v['id'])->update($u);
- unset($u);
- }
- PreformanceTasksOrg::where($w1)->inc('ok_credit', (int)$credit)->update();
- return 1;
- }
- //统计当天获得的总学分
- public function sumday_credit($root_id,$employee_id)
- {
- $where3[] = ['code', '=', 'studyday_maxcredit'];
- $where3[] = ['root_id', '=', $root_id];
- $maxcredit = CreditsSetting::where($where3)->field('status,value')->find();//->findOrEmpty();
- if(!empty($maxcredit) && $maxcredit['status']==0){
- $day_course=TrainCredits::where([['employee_id','=',$employee_id],['root_id','=',$root_id],['addtime','>=',date('Y-m-d').' 00:00:00'],['addtime','<=',date('Y-m-d').' 23:59:59']])->sum('credits');
- $yes_course=$maxcredit['value']-$day_course;
- $maxcredit['yes_course']=$yes_course;
- }
- return $maxcredit;
- }
- /**
- * 课件内容详情
- */
- public function courseDetail($class_id,$course_id)
- {
- $where = [
- ['id','=',$class_id],
- ['course_id','find in set',$course_id],
- ['root_id','in',[$this->rootId,0]],
- ['del','=',0]
- ];
- $check = TrainClass::where($where)->field('id,train_employee,from_type,type')->findOrEmpty();
- if($check->isEmpty()) return json(['code' => 1, 'data' => [],'msg'=>'课件不存在']);
- $data = TrainCourse::where('id',$course_id)->find()->toArray();
- //评论数量
- $w[] = ['class_id','=',$class_id];
- $w[] = ['course_id','=',$course_id];
- $w[] = ['root_id','in',[$this->rootId,0]];
- $data['comment_count'] = TrainSumup::with('employee')->where($w)->count();
- //学习总人数
- $tw[] = ['root_id','in',[$this->rootId,0]];
- //如果是集团指派的课程查询全部人员
- if($check['from_type'] == 1){
- $data['train_count'] = Employee::where([['root_id','=',$this->rootId],['uid','>',0],['state','=','在职']])->count();
- }else{
- $data['train_count'] = count(array_filter(array_unique(explode(',',$check['train_employee']))));
- }
-
- //学习完成人数
- $dw = [['from','=',0],['root_id','in',[$this->rootId,0]], ['class_id', '=', $class_id],['course_id','find in set',$course_id]];
- $data['complete_count'] = TrainDoneLog::where($dw)->group('employee_id')->count();
- //未学习人数
- $data['no_count'] = $data['train_count']-$data['complete_count'] > 0 ? $data['train_count']-$data['complete_count'] : 0;
- //pdf 页数
- $data['page_num'] = 0;
- if ($data['type']=='pdf') {
- $data['page_num'] = $data['file'] ? $this->getPageTotal($data['file']) : 0;
- }
- $data['class_type'] = $check->type;
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 获取PDF的页数
- */
- public function getPageTotal($path){
- // 打开文件
- if (!$fp = @fopen($path,"r")) {
- $error = "打开文件{$path}失败";
- return 0;
- } else {
- $max=0;
- while(!feof($fp)) {
- $line = fgets($fp,255);
- if (preg_match('/\/Count [0-9]+/', $line, $matches)){
- preg_match('/[0-9]+/',$matches[0], $matches2);
- if ($max<$matches2[0]) $max=$matches2[0];
- }
- }
- fclose($fp);
- // 返回页数
- return $max;
- }
- }
- /**
- * 点赞
- */
- public function thumbing($sumup_id)
- {
- $root_id = $this->rootId;
- $employee_id = $this->employeeId;
-
- $sumup = TrainSumup::where(['root_id'=>$root_id , 'id'=>$sumup_id])->field('id')->findOrEmpty();
- if($sumup->isEmpty()) return json(['code' => 1, 'msg' => '点赞失败']);
-
- $log = TrainThumbLog::where([
- ['sumup_id','=',$sumup_id],
- ['employee_id','=',$employee_id],
- ['root_id','=',$root_id]
- ])->find();
- if ($log) {
- TrainThumbLog::where(['sumup_id' => $sumup_id, 'employee_id' => $employee_id, 'root_id'=>$root_id])->delete();
- TrainSumup::where(['root_id'=>$root_id , 'id'=>$sumup_id])->dec('thumb')->update();
- $msg = '点赞取消';
- $res = 0;
- } else {
- TrainThumbLog::insert(['sumup_id' => $sumup_id, 'employee_id' => $employee_id, 'root_id'=>$root_id]);
- TrainSumup::where(['root_id'=>$root_id , 'id'=>$sumup_id])->inc('thumb')->update();
- $msg = '点赞成功';
- $res = 1;
- }
- //点赞数量
- $count = TrainThumbLog::where([['sumup_id','=',$sumup_id]])->count();
-
- return json(['code' => 0, 'msg' => $msg,'count'=>$count,'res'=>$res]);
- }
- /**
- * 精品课程点赞
- */
- public function thumbings($sumup_id)
- {
- $root_id = $this->rootId;
- $employee_id = $this->employeeId;
-
- $sumup = TrainSumup::where(['id'=>$sumup_id])->field('id')->findOrEmpty();
- if($sumup->isEmpty()) return json(['code' => 1, 'msg' => '点赞失败']);
-
- $log = TrainThumbLog::where([
- ['sumup_id','=',$sumup_id],
- ['employee_id','=',$employee_id],
- ['root_id','=',$root_id]
- ])->findOrEmpty();
- if (!$log->isEmpty()) {
- TrainThumbLog::where(['sumup_id' => $sumup_id, 'employee_id' => $employee_id, 'root_id'=>$root_id])->delete();
- TrainSumup::where(['id'=>$sumup_id])->dec('thumb')->update();
- $msg = '取消点赞';
- $res = 0;
- } else {
- TrainThumbLog::insert(['sumup_id' => $sumup_id, 'employee_id' => $employee_id, 'root_id'=>$root_id]);
- TrainSumup::where(['id'=>$sumup_id])->inc('thumb')->update();
- $msg = '成功点赞';
- $res = 1;
- }
- //点赞数量
- $count = TrainThumbLog::where([['sumup_id','=',$sumup_id]])->count();
-
- return json(['code' => 0, 'msg' => $msg,'count'=>$count,'res'=>$res]);
- }
- /*
- * 精品课程列表
- */
- public function premiumCourseList()
- {
- $param = Request::only(['page'=>1,'limit'=>10,'cate'=>'','keyword'=>'','type'=>1]);
- $order = $param['type']==1 ? 'id desc' : 'view desc,id desc';
- $page = $param['page'];
- $limit = $param['limit'];
- $cate = $param['cate'];
- $keyword = $param['keyword'];
- // $eid = $this->employeeId;
- $condition = [
- ['root_id','=',0],
- ['del','=',0],
- ['publish','=',1]
- ];
- if ($cate) $condition[] = ['cate','=',$cate];
- if ($keyword) $condition[] = ['title','like','%'.trim($keyword).'%'];
- $data = TrainClass::with(['lecturer'=>function($query){
- $query->bind(['lecturer_name'=>'name']);
- }])->field('id,title,cover,course_id,category,view,train_employee,addtime,type,from_type,lecturer_id');
- $data = $data->where($condition)->page((int)$page , (int)$limit)->order($order)->select();
- $column_class_id = array_column($data->toArray() , 'id');
- $doneLog = TrainDoneLog::where([['from','=',0],['class_id','in',$column_class_id] , ['done_percent','>',0]])->group('class_id')->column('count(class_id)','class_id');
- foreach ($data as $key => $value) {
- $data[$key]['study_count'] = isset($doneLog[$value['id']]) ? $doneLog[$value['id']] : 0;//学习人次
- $data[$key]['lectruer_company_name'] = $value['lecturer_id'] ? $value['lecturer']->company->name : '';
- }
- $count = TrainClass::where($condition)->count();
- return json(['code' => 0, 'data' => $data,'count'=>$count]);
- }
- /*
- * 课件完成人员列表
- */
- public function getCourseEmployee()
- {
- $param = Request::only(['page'=>1,'limit'=>10,'course_id'=>0,'class_id'=>0,'org_id'=>0,'keyword'=>'','start_date'=>'','end_date'=>'']);
- $where = [
- ['id','=',$param['class_id']],
- ['root_id','in',[$this->rootId,0]]
- ];
- $check = TrainClass::where($where)->field('id')->findOrEmpty();
- if($check->isEmpty()) return json(['code' => 0, 'data' => [],'count'=>0]);
- $where1 = [['class_id','=',$param['class_id']],['from','=',0]];
- if($param['start_date']){
- $where1[] = ['addtime','>=',$param['start_date']];
- }
- if($param['end_date']){
- $where1[] = ['addtime','<=',$param['end_date'].' 23:59:59'];
- }
- $emp = TrainDoneLog::where($where1)->column('addtime','employee_id');
- if (empty($emp)) return json(['code' => 0, 'data' => [],'count'=>0]);
- $emp_ids = array_keys($emp);
- $query = [['id','in',$emp_ids]];
- if($param['keyword']) $query[] = ['name','like','%'.$param['keyword'].'%'];
- if($param['org_id']) $query[] = ['org_id','=',$param['org_id']];
- $data = Employee::with(['org'=>function($query){
- $query->bind(['org_name'=>'name']);
- },'user'=>function($query){
- $query->bind(['headimg'=>'headimgurl']);
- }])->where($query)->field('id,name,org_id,uid')->page((int)$param['page'],(int)$param['limit'])->select()->toArray();
-
- $count = Employee::where($query)->count();
- foreach ($data as $key => $value) {
- $data[$key]['addtime'] = isset($emp[$value['id']]) ? $this->date_switch($emp[$value['id']]) : '';
- }
- return json(['code' => 0, 'data' => $data,'count'=>$count]);
- }
- /*
- * 员工完成课件
- */
- public function doneLogging()
- {
- $param = request()->param(['course_id','class_id','study_time']);
- $employee_id = $this->employeeId;
- $root_id = $this->rootId;
- $from = 0;
- $token['org_id'] = Employee::where('id',$this->employeeId)->value('org_id');
-
- //查询课件是否存在
- $class = TrainClass::where(['root_id'=>$root_id , 'del'=>0 , 'publish'=>1 , 'id'=>$param['class_id']])->find();
- $c = explode(',',$class['course_id']);
- if(empty($class) || !in_array($param['course_id'] , $c)){
- return json(['code' => 1, 'msg' => '课件内容不存在']);
- }
-
- //判断是否已完成该课件
- $doneLog = TrainDoneLog::where(['employee_id'=>$employee_id,'from'=>$from,'root_id'=>$root_id,'class_id'=>$param['class_id']])->find();
- $log_course_id = !empty($doneLog['course_id']) ? explode(',',$doneLog['course_id']) : [];
- if(!empty($doneLog) && !empty($doneLog['done_percent']) && in_array($param['course_id'] , $log_course_id)){
- return json(['code' => 1, 'msg' => '该课件已完成,无需重复操作']);
- }
-
- //判断是否设置课件评论字数
- $comment_word_num = TrainCourse::where(['root_id'=>$root_id , 'id'=>$param['course_id']])->value('comment_word_num');
- if($comment_word_num > 0)
- {
- $where[] = ['employee_id','=',$employee_id];
- $where[] = ['course_id','=',$param['course_id']];
- $where[] = ['class_id','=',$param['class_id']];
- $where[] = ['','EXP',Db::raw('char_length(content) >= '.$comment_word_num.'')];
- $sumupcount = TrainSumup::where($where)->count();
- if($sumupcount == 0) return json(['code' => 1, 'msg' => '课件评论字数不达标']);
- }
-
- $course_id = explode(',',$class['course_id']);
- if(!empty($doneLog))
- {
- $have = explode(',',$doneLog['course_id']);
- $checkCourse = [$param['course_id']];
- $newCourseArr = array_filter(array_merge($have,$checkCourse));
-
- $GDP = floor((count($newCourseArr) / count($course_id))*100);
- $doneLog->done_percent = $GDP;
- $doneLog->course_id = implode(',',$newCourseArr);
- if($doneLog->save()){
- if ($GDP==100) {//累计勋章
- Console::call('medal',['class',(string)$employee_id,(string)$root_id]);
- //增加课程完成赠送学分
- $this->add_studylog($employee_id,$class,$root_id);
- }
- //完成单个课件增加学习时长
- if(!empty($param['study_time'])){
- $this->add_studytime($employee_id,$param['class_id'],$param['course_id'],$param['study_time'],$root_id);
- }
-
- return json(['code' => 0, 'msg' => '完成本节学习']);
- }else{
- return json(['code' => 1, 'msg' => '未能完成本节学习']);
- }
- }else{
- $GDP = floor((1 / count($course_id))*100);
- if(TrainDoneLog::insert([
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'org_id' => $token['org_id'],
- 'class_id' => $param['class_id'],
- 'done_percent' => $GDP,
- 'course_id' => $param['course_id'],
- 'from'=>$from
- ])){
- if ($GDP==100) {//累计勋章
- Console::call('medal',['class',(string)$employee_id,(string)$root_id]);
- //增加课程完成赠送学分
- $this->add_studylog($employee_id,$class,$root_id);
- }
- //完成单个课件增加学习时长
- if(!empty($param['study_time'])){
- $this->add_studytime($employee_id,$param['class_id'],$param['course_id'],$param['study_time'],$root_id);
- }
- return json(['code' => 0, 'msg' => '完成本节学习']);
- }else{
- return json(['code' => 1, 'msg' => '未能完成本节学习']);
- }
- }
- }
- /**
- * 课件浏览详情+1
- */
- public function coureseView($course_id,$time=0)
- {
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $token['uid'] = $this->uid;
- $token['org_id'] = Employee::where('id',$this->employeeId)->value('org_id');
- $save['con_id'] = $course_id;
- $save['time'] = $time;
- $save['root_id'] = $token['root_org'];
- $save['employee_id'] = $token['employee_id'];
- $save['uid'] = $token['uid'];
- $save['org_id'] = $token['org_id'];
- $save['type'] = 'traincourse';
- TrainCourseView::insertGetId($save);
- TrainCourse::where(['id'=> $course_id,'root_id'=>$token['root_org']])->inc('view')->update();
- return json(['code' => 0, 'data' =>'成功浏览']);
- }
- //部门列表
- public function all_org(){
- $token['root_org'] = $this->rootId;
- $res = Org::where([['path', 'like', $token['root_org'] . '-%'],['status','=',1]])->field('id,name,pid')->select()->toArray();
- $res = $this->tree($res,0,[]);
- return json(['code' => 0, 'data' => $res]);
- }
- //部门人员树
- public function tree($data,$pid = 0, $persons)
- {
- $new_arr = [];
- foreach($data as $k => $v){
- if($v['pid'] == $pid) {
- $persions = isset($persons[$v['id']])?$persons[$v['id']]:[];
- $children = $this->tree($data, $v['id'], $persons);
- $v['children'] = array_merge_recursive($children,$persions);
- if (empty($v['children'])) $v['disabled']=true;
- $new_arr[] =$v;
- }
- }
- return $new_arr;
- }
- /**
- * 课程浏览 +1
- */
- public function classView(){
- $class_id = input('class_id', '', 'intval');
- $course_id = input('course_id', '', 'intval');
- $time = input('time', 0, 'intval');
- $token['root_org'] = $this->rootId;
- $token['employee_id'] = $this->employeeId;
- $token['org_id'] = Employee::where('id',$token['employee_id'])->value('org_id');
- $token['uid'] = $this->uid;
- $save['course_id'] = $course_id;
- $save['class_id'] = $class_id;
- $save['time'] = $time;
- $save['root_id'] = $token['root_org'];
- $save['employee_id'] = $token['employee_id'];
- $save['uid'] = $token['uid'];
- $save['org_id'] = $token['org_id'];
- $save['type'] = 'traincourse';
- TrainClassView::insertGetId($save);
- TrainCourse::where(['id'=> $course_id,'root_id'=>$token['root_org']])->inc('view')->update();
- return json(['code' => 0, 'data' =>'成功浏览']);
- }
- }
|