企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 学习统计功能(新版) # ## 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; } ~~~