# 学习统计功能(新版) #
## 1. 获取mongodb数组
*D:\wamp\www\xue.test\xuetang\protected\modules\croom\controllers\ManageController.php*
~~~
/**
* 学习统计:掌握考点情况数据统计
* @author wangbo 2019-1-21
* @method GET
* @url croom/manage/myExerTagStatistics
* @param
* userID 必填
* end 默认当前日期
* begin 默认30天内
* @return json
*/
public function actionMyExerTagStatistics()
{
$userID = RequestUtils::getNormalRequest('userID'); //学生ID
$end = RequestUtils::getNormalRequest('end', (time() + 3600 * 24) * 1000);
$begin = RequestUtils::getNormalRequest('begin', (time() - 3600 * 24 * 30) * 1000); //默认是30天
$args['begin'] = $begin;
$args['end'] = $end;
$args['uid'] = $userID;
if (!$userID) {
//判断是否是学生
ResponseUtils::json( null, 21, ResponseUtils::$rmsg[ 21 ] );
}
// 获取学生的考点树形数组(一级,三级)
$tagTree = V2StatisticsService::factory()->getTagTree($args);
$exers = V2StatisticsService::factory()->getAnswerExerRecordByUserID($args);
foreach ($exers as $v) {
foreach ($v['tags'] as $tag) {
if ($tag['tid'] == 3 && $tag['level'] == 1) {
$tagTree[$tag['tv']]['totalQuestion']++;
$tagTree[$tag['tv']]['totalScore']+=$v['total'];
$tagTree[$tag['tv']]['myTotalScore']+=$v['score'];
}
if ($tag['tid'] == 3 && $tag['level'] == 3) {
// 在树形数组中找到对应的三级考点
foreach ($tagTree as $k => $item) {
if (in_array($tag['tv'], array_keys($item['subTag']))) {
$tagTree[$k]['subTag'][$tag['tv']]['totalQuestion']++;
$tagTree[$k]['subTag'][$tag['tv']]['totalScore']+=$v['total'];
$tagTree[$k]['subTag'][$tag['tv']]['myTotalScore']+=$v['score'];
}
}
}
}
}
// 计算得分率
foreach ($tagTree as $k => $v) {
if ($v['totalScore'] != 0) {
$tagTree[$k]['rate'] = round(($v['myTotalScore'] / $v['totalScore']) * 100);
}
foreach ($v['subTag'] as $i => $j) {
if ($j['totalScore'] != 0) {
$tagTree[$k]['subTag'][$i]['rate'] = round(($j['myTotalScore'] / $j['totalScore']) * 100);
}
}
}
// 去掉数组索引
$data = StatisticsService::factory()->unsetArrIndex($tagTree);
// 数组按照得分率rate进行从高到低的排序
$data = StatisticsService::factory()->sortDataArr($data);
foreach ($data as $k => $v) {
if ($v['subTag']) {
$data[$k]['subTag'] = StatisticsService::factory()->sortDataArr($v['subTag']);
}
}
if (!$tagTree) {
ResponseUtils::json(null, 1, 'error');
}
ResponseUtils::json(['data' => $data]);
}
~~~
*D:\wamp\www\xue.test\xuetang\protected\service\service2\V2StatisticsService.php*
~~~
/**
* 查找该用户练习记录的考点,组装成二维数组
* @param $args
* @return array
*/
public function getTagTree($args)
{
/** @var EMongoClient $mongo */
$mongo = Yii::app()->mongodb;
$collection = $mongo->selectCollection(WK::XT_EXER_RECORD_COLLECTION);
$res = $this->getAnswerExerRecordByUserID($args);
$arr = []; //三级考点
foreach ($res as $v) {
foreach ($v['tags'] as $index => $item) {
if ($item['tid'] == 3 && $item['level'] == 3) {
if (!in_array($item['tv'], array_keys($arr))) {
$temp = [];
$temp['id'] = $parentID = $item['tv'];
$tag = Tag::model()->findByPk($item['tv']);
$temp['fdName'] = $tag->fdName;
$temp['totalQuestion'] = 0;
$temp['totalScore'] = 0;
$temp['myTotalScore'] = 0;
$arr[$item['tv']] = $temp;
}
}
}
}
$arr2 = []; //一级考点
foreach ($arr as $k => $v) {
$parent = TagService::factory()->getParentTag($v['id']);
if (!in_array($parent->id, array_keys($arr2))) {
$arr2[$parent->id]['id'] = $parent->id;
$arr2[$parent->id]['fdName'] = $parent->fdName;
$arr2[$parent->id]['totalQuestion'] = 0;
$arr2[$parent->id]['totalScore'] = 0;
$arr2[$parent->id]['myTotalScore'] = 0;
}
$arr2[$parent->id]['subTag'][$k] = $v;
}
return $arr2;
}
~~~
~~~
/* *
* 通过MongoDB获取用户的练习记录
* @param $args
* @author wangbo 2019-2-21
*/
public function getAnswerExerRecordByUserID($args)
{
/** @var EMongoClient $mongo */
$mongo = Yii::app()->mongodb;
$collection = $mongo->selectCollection(WK::XT_EXER_RECORD_COLLECTION);
$args['begin'] = $args['begin'] ? new MongoDate($args['begin'] / 1000 + 8 * 60 * 60) : new MongoDate(strtotime('-1 year'));
$args['end'] = $args['end'] ? new MongoDate($args['end'] / 1000 + 8 * 60 * 60) : new MongoDate(time() + 8 * 60 * 60);
$res = $collection->aggregate(array(
array(
'$match' => array(
'uid' => (int)$args['uid'], //必须强制转换为整型,如果是string类型,数据无法匹配
'date' => array(
'$gte' => $args['begin'],
'$lte' => $args['end'],
)
),
),
array(
'$project' => array(
'_id' => 0,
'exid' => 1,
'eid' => 1,
'tags' => 1,
'score' => 1,
'total' => 1,
'scoreRate' => 1,
'date' => 1,
),
),
array(
'$sort' => array(
'date' => -1, // 日期倒序
)
)
));
return (array)$res['result'];
}
~~~
## 2. 操作数组
*D:\wamp\www\xue.test\xuetang\protected\service\StatisticsService.php*
~~~
/**
* 将数组的值,按照数组中的rate值,从高到低进行排序
* @param $data
* @return array
* @author wangbo 2019-2-22
*/
public function sortDataArr($data)
{
$key = [];
//第一步:获取rate数组并排序
foreach ($data as $k => $v) {
if (!in_array($v['rate'], array_values($key))) {
$key[] = $v['rate'];
}
}
rsort($key); //获取到要排序的索引值得排序
//第二步:组装数组
$arr2 = [];
foreach ($key as $v) {
foreach ($data as $i => $j) {
if ($j['rate'] == $v) {
$arr2[] = $j;
}
}
}
return $arr2;
}
/* *
* 去掉数组索引
* @param $tagTree
* @author wangbo 2019-2-22
*/
public function unsetArrIndex($tagTree)
{
$data = [];
foreach ($tagTree as $k => $v) {
$data[] = $v;
}
foreach ($data as $k => $v) {
$temp = [];
foreach ($v['subTag'] as $item) {
$temp[] = $item;
}
$data[$k]['subTag'] = $temp;
}
return $data;
}
~~~
- 说明
- 开发任务
- 星课-真光
- 课表
- Excel Down
- 调课
- 课表修改
- 课表代码分析
- 课堂
- 课堂:应用商店通信管理协议
- 教师账号强制绑定手机或邮箱
- 强制绑定手机和修改密码的规则
- 学堂
- 课程学习:讨论功能
- 后台:课程讨论管理
- 课程直播接口
- 学习统计功能(旧版)
- 学习统计功能(新版)
- 同步课程统计功能
- 同步课程编辑-新增视频
- 第三方接口
- 学科网
- 安徽第三方
- 大赛
- 管控系统
- 日志管理
- 设备日志
- 平板接口
- 渝教
- 教学总结
- 空白目录
- Yii 1.1
- 学堂架构
- Yii 1.1一些方法的解读
- MVCS结构
- 基础使用语法
- 创建1个新模块
- 关联模型
- CDbCriteria
- 学生-课堂记录
- 学生端页面展示
- 教师端页面展示
- 编辑课程文档
- SQL
- 课堂项目运行入口
- 上传资源示意图
- 行为
- PHPStorm
- 源码阅读
- 会诊答卷页面
- 考点练习
- 资源首页
- 同步课程
- 同步课程:章节信息
- 升学复习
- 统计图-范例
- 模块
- 非法词
- 服务层
- MongoDB类
- 学堂作答记录从Mongo新集合获取数据
- MongoYii
- 错题集
- 小技巧
- 完善资料
- 邮件发送
- K12
- JSpang视频课程
- MongoDB
- 创业
- 项目
- 包包