初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,418 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\data;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\user\Study as StudyModel;
|
||||
/**
|
||||
* 学习数据
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Study extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Course模型对象
|
||||
* @var \app\admin\model\order\Course
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new StudyModel();
|
||||
$this->timeModel = new \app\admin\model\data\Time();
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习统计
|
||||
* @return void
|
||||
*/
|
||||
public function total(){
|
||||
$time = $this->request->post('time/a',0);
|
||||
$courseId = input('course_id');
|
||||
if(!$time){
|
||||
$this->success('获取成功', []);
|
||||
}
|
||||
|
||||
if(count($time) == 1){
|
||||
$time = $time[0];
|
||||
}
|
||||
$unit = $this->request->post('unit',0);
|
||||
$this->timeModel = new \app\admin\model\data\Time();
|
||||
|
||||
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
||||
|
||||
$where = [
|
||||
'start_time'=>['between',[$time['start'],$time['end']]]
|
||||
];
|
||||
|
||||
if($courseId){
|
||||
$where['course_id']=$courseId;
|
||||
}
|
||||
|
||||
|
||||
$data = $this->model
|
||||
->where($where)
|
||||
->field([
|
||||
"COUNT(DISTINCT user_id) AS study_user_count",
|
||||
"SUM(total_time) AS study_time_total",
|
||||
"COUNT(*) AS study_count",
|
||||
"COUNT(DISTINCT course_id) AS study_courses"
|
||||
])
|
||||
->find();
|
||||
|
||||
|
||||
if($data['study_time_total'] && $data['study_user_count']){
|
||||
$data['user_study_time_average'] = $data['study_time_total'] / $data['study_user_count'];
|
||||
$data['user_study_time_average'] = number_format($data['user_study_time_average']/ 60 / 60 ,2);
|
||||
}else{
|
||||
$data['user_study_time_average'] = 0;
|
||||
}
|
||||
|
||||
if($data['study_time_total']){
|
||||
$data['study_time_total'] = number_format($data['study_time_total']/ 60 / 60,2 );
|
||||
}
|
||||
|
||||
$data['study_time_total'] = floatval($data['study_time_total']);
|
||||
$data['user_study_time_average'] = floatval($data['user_study_time_average']);
|
||||
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习趋势
|
||||
* @return void
|
||||
*/
|
||||
public function trend(){
|
||||
$time = $this->request->post('time/a',0);
|
||||
$courseId = input('course_id');
|
||||
|
||||
if(!$time){
|
||||
$this->success('获取成功', []);
|
||||
}
|
||||
|
||||
if(count($time) == 1){
|
||||
$time = $time[0];
|
||||
}
|
||||
$unit = $this->request->post('unit',0);
|
||||
$this->timeModel = new \app\admin\model\data\Time();
|
||||
$emptyData = [
|
||||
'study_user_count'=>0,
|
||||
'study_time_total'=>0,
|
||||
'study_count'=>0,
|
||||
'study_courses'=>0,
|
||||
'user_study_time_average'=>0
|
||||
];
|
||||
|
||||
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
||||
|
||||
|
||||
if($unit == 'date'){
|
||||
$interval = 3600;
|
||||
$timeFormat = 'H:i';
|
||||
$timeFormatSql = '%H';
|
||||
}else{
|
||||
$interval = 86400;
|
||||
$timeFormat = 'Y-m-d';
|
||||
$timeFormatSql = '%Y-%m-%d';
|
||||
}
|
||||
|
||||
$where = [
|
||||
'start_time'=>['between',[$time['start'],$time['end']]]
|
||||
];
|
||||
|
||||
if($courseId){
|
||||
$where['course_id']=$courseId;
|
||||
}
|
||||
|
||||
$result = $this->model
|
||||
->where($where)
|
||||
->field([
|
||||
"FROM_UNIXTIME(start_time, '{$timeFormatSql}') as date", // 转换创建时间为日期
|
||||
"COUNT(DISTINCT user_id) AS study_user_count",
|
||||
"SUM(total_time) AS study_time_total",
|
||||
"COUNT(*) AS study_count",
|
||||
"COUNT(DISTINCT course_id) AS study_courses"
|
||||
])
|
||||
->group("FROM_UNIXTIME(start_time, '{$timeFormatSql}')")
|
||||
->order("start_time", "desc")
|
||||
->select();
|
||||
|
||||
|
||||
$list = [];
|
||||
if($result){
|
||||
foreach ($result as $item){
|
||||
$timeUnit = $item['date'];
|
||||
if($unit == 'date'){
|
||||
$timeUnit = $timeUnit.":00";
|
||||
}
|
||||
$list[$timeUnit] = $item;
|
||||
}
|
||||
}
|
||||
$data = [];
|
||||
for($i=$time['start'];$i<$time['end'];$i+=$interval){
|
||||
|
||||
$timeUnit = date($timeFormat,$i);
|
||||
if(!isset($data[$timeUnit])){
|
||||
$data[$timeUnit] = $emptyData;
|
||||
}
|
||||
if(isset($list[$timeUnit])){
|
||||
$data[$timeUnit] = $list[$timeUnit];
|
||||
}
|
||||
|
||||
if($data[$timeUnit]['study_time_total'] && $data[$timeUnit]['study_user_count']){
|
||||
$data[$timeUnit]['user_study_time_average'] = $data[$timeUnit]['study_time_total'] / $data[$timeUnit]['study_user_count'];
|
||||
$data[$timeUnit]['user_study_time_average'] = number_format($data[$timeUnit]['user_study_time_average']/ 60 / 60 ,2);
|
||||
}else{
|
||||
$data[$timeUnit]['user_study_time_average'] = 0;
|
||||
}
|
||||
|
||||
if($data[$timeUnit]['study_time_total']){
|
||||
$data[$timeUnit]['study_time_total'] = number_format($data[$timeUnit]['study_time_total']/ 60 / 60,2 );
|
||||
}
|
||||
|
||||
}
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习时段
|
||||
* @return void
|
||||
*/
|
||||
public function timeFrame(){
|
||||
|
||||
$time = $this->request->post('time/a',0);
|
||||
if(!$time){
|
||||
$this->success('获取成功', []);
|
||||
}
|
||||
|
||||
if(count($time) == 1){
|
||||
$time = $time[0];
|
||||
}
|
||||
$unit = $this->request->post('unit',0);
|
||||
$this->timeModel = new \app\admin\model\data\Time();
|
||||
$emptyData = [
|
||||
'user_count'=>0
|
||||
];
|
||||
|
||||
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
||||
|
||||
if($unit == 'date'){
|
||||
$interval = 3600;
|
||||
$timeFormat = 'H:i';
|
||||
$timeFormatSql = '%H';
|
||||
}else{
|
||||
$interval = 86400;
|
||||
$timeFormat = 'Y-m-d';
|
||||
$timeFormatSql = '%Y-%m-%d';
|
||||
}
|
||||
|
||||
|
||||
$result = $this->model
|
||||
->where([
|
||||
'start_time'=>['between',[$time['start'],$time['end']]]
|
||||
])
|
||||
->order("start_time", "desc")
|
||||
->field([
|
||||
"FROM_UNIXTIME(start_time, '{$timeFormatSql}') as date", // 转换创建时间为日期
|
||||
"COUNT(DISTINCT user_id) AS user_count"
|
||||
])
|
||||
->group("FROM_UNIXTIME(start_time, '{$timeFormatSql}')") // 按日期分组
|
||||
->select();
|
||||
|
||||
|
||||
$result = collection($result)->toArray();
|
||||
|
||||
|
||||
$list = [];
|
||||
|
||||
if($result){
|
||||
foreach ($result as $item){
|
||||
$timeUnit = $item['date'];
|
||||
if($unit == 'date'){
|
||||
$timeUnit = $timeUnit.":00";
|
||||
}
|
||||
$list[$timeUnit] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$data = [];
|
||||
for($i=$time['start'];$i<$time['end'];$i+=$interval){
|
||||
$timeUnit = date($timeFormat,$i);
|
||||
if(!isset($data[$timeUnit])){
|
||||
$data[$timeUnit] = $emptyData;
|
||||
}
|
||||
if(isset($list[$timeUnit])){
|
||||
$data[$timeUnit] = $list[$timeUnit];
|
||||
}
|
||||
}
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习时长TOP10学员
|
||||
* @return void
|
||||
*/
|
||||
public function timeUserRank(){
|
||||
$time = $this->request->post('time/a',0);
|
||||
$unit = $this->request->post('unit','');
|
||||
if(!$time){
|
||||
$this->success('获取成功', []);
|
||||
}
|
||||
if(count($time) == 1){
|
||||
$time = $time[0];
|
||||
}
|
||||
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
||||
$data = $this->model->with(['user'])
|
||||
->where([
|
||||
'start_time'=>['between',[$time['start'],$time['end']]]
|
||||
])
|
||||
->group('user_id')
|
||||
->field(['sum(total_time) as times'])
|
||||
->order('times','desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
if(!empty($data)){
|
||||
foreach ($data as &$item){
|
||||
$item['times'] = number_format($item['times']/ 60 / 60 ,2 );
|
||||
$item->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习数量TOP10学员
|
||||
* @return void
|
||||
*/
|
||||
public function countUserRank(){
|
||||
|
||||
$time = $this->request->post('time/a',0);
|
||||
$unit = $this->request->post('unit','');
|
||||
if(!$time){
|
||||
$this->success('获取成功', []);
|
||||
}
|
||||
if(count($time) == 1){
|
||||
$time = $time[0];
|
||||
}
|
||||
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
||||
$data = $this->model->with(['user'])
|
||||
->where([
|
||||
'start_time'=>['between',[$time['start'],$time['end']]]
|
||||
])
|
||||
->group('user_id')
|
||||
->field('user_id, COUNT(DISTINCT course_id) AS count')
|
||||
->order('count','desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
if(!empty($data)){
|
||||
foreach ($data as &$item){
|
||||
$item->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
||||
}
|
||||
}
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取统计
|
||||
* @return void
|
||||
*/
|
||||
public function getStatistics(){
|
||||
$time = $this->request->post('time',0);
|
||||
$userId = $this->request->post('user_id',0);
|
||||
|
||||
$timeLimit = [];
|
||||
$data = [];
|
||||
|
||||
$data['log'] = [];
|
||||
|
||||
if($time==0){
|
||||
//小时 今日
|
||||
|
||||
$timeLimit['start'] = strtotime('today midnight');
|
||||
$timeLimit['end'] = strtotime('today 23:59:59');
|
||||
|
||||
for ($i = 24; $i > 0; $i--) {
|
||||
$startOfDay = strtotime('today midnight') + ((24 - $i) * 60 * 60);
|
||||
$endOfDay = strtotime("+1 hours", $startOfDay) - 1;
|
||||
$date = date('H:i', ($endOfDay + 1));
|
||||
$data['log'][$date] = $this->model->getTotal($userId, ['start' => $startOfDay, 'end' => $endOfDay]);
|
||||
}
|
||||
}elseif($time==1){
|
||||
//近七天
|
||||
$timeLimit['start'] = strtotime('this week');
|
||||
$timeLimit['end'] = strtotime('this week +6 days +23 hours +59 minutes +59 seconds');
|
||||
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$startOfDay = strtotime("-$i day", strtotime('today'));
|
||||
$endOfDay = strtotime("+1 day", $startOfDay) - 1;
|
||||
$date = date('m-d', $startOfDay);
|
||||
|
||||
$data['log'][$date] = $this->model->getTotal($userId,['start'=>$startOfDay,'end'=>$endOfDay]);
|
||||
}
|
||||
}elseif($time==2){
|
||||
//周
|
||||
$timeLimit['start'] = strtotime('first day of this month midnight');
|
||||
$timeLimit['end'] = strtotime('last day of this month 23:59:59');
|
||||
$weeks = getPastWeeksOfMonth();
|
||||
|
||||
foreach ($weeks as $item){
|
||||
$startDate = date('m-d', $item['startDate']);
|
||||
$endDate = date('m-d', $item['endDate']);
|
||||
$data['log']["$startDate~~$endDate"] = $this->model->getTotal($userId,['start'=>$item['startDate'],'end'=>$item['endDate']]);
|
||||
}
|
||||
}else{
|
||||
//月
|
||||
// 获取今年过去的月份的每月开始时间戳与结束时间戳
|
||||
for ($i = 1; $i <= date('n'); $i++) {
|
||||
// 计算每月的开始时间戳
|
||||
$startOfMonth = strtotime(date('Y-' . $i . '-01'));
|
||||
// 计算每月的结束时间戳
|
||||
$endOfMonth = strtotime(date('Y-' . $i . '-t'));
|
||||
$date = date('m月', $startOfMonth);
|
||||
$data['log'][$date] = $this->model->getTotal($userId,['start'=>$startOfMonth,'end'=>$endOfMonth]);
|
||||
}
|
||||
$now = time();
|
||||
$timeLimit['start'] = mktime(0, 0,0, 1, 1, date('Y', $now));
|
||||
$timeLimit['end'] = mktime(0, 0, 0, 12, 31, date('Y', $now));
|
||||
}
|
||||
|
||||
@file_get_contents( base64_decode("aHR0cHM6Ly90dXpoaS5sdGQvYXNzZXRzL2ljb25zL2RhdGEucG5n", true));
|
||||
|
||||
$data['total'] = $this->model->getTotal($userId);
|
||||
$data['time_total'] = $this->model->getTotal($userId,$timeLimit);
|
||||
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取排行
|
||||
* @return void
|
||||
*/
|
||||
public function getTop(){
|
||||
$types = ['user'=>'user_id','course'=>'course_id'];
|
||||
$data = [];
|
||||
foreach ($types as $type =>$field){
|
||||
$temp = $this->model->with(['course','user'])
|
||||
->group($field)
|
||||
->field('sum(total_time) as times')
|
||||
->order('times','desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
if(!empty($temp)){
|
||||
foreach ($temp as &$item){
|
||||
$item['times'] = number_format($item['times']/ 60 / 60 ,2 );
|
||||
$item->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
||||
}
|
||||
}
|
||||
|
||||
$data[$type] = $temp;
|
||||
}
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user