src/CorporateTrainingBundle/Controller/LearnRankController.php line 15

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Controller;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Controller\Admin\BaseController;
  5. use CorporateTrainingBundle\Biz\DataStatistics\Service\DataStatisticsService;
  6. use CorporateTrainingBundle\Biz\User\Service\UserOrgService;
  7. use CorporateTrainingBundle\Biz\User\Service\UserService;
  8. use CorporateTrainingBundle\Common\DateToolkit;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class LearnRankController extends BaseController
  11. {
  12.     public function userLearnRankListAction(Request $request$orgIds ''$sourceFrom '')
  13.     {
  14.         $fields['type'] = $request->query->get('type''week');
  15.         if ('total' != $fields['type']) {
  16.             list($fields['startDateTime'], $fields['endDateTime']) = $this->generateStartDateAndEndDate($fields['type']);
  17.         }
  18.         $sourceFrom $request->query->get('sourceFrom'$sourceFrom);
  19.         if ($sourceFrom) {
  20.             $conditions 'total' != $fields['type'] ? [
  21.                 'startDateTime' => strtotime($fields['startDateTime']),
  22.                 'endDateTime' => strtotime($fields['endDateTime']),
  23.             ] : [];
  24.             $conditions 'admin' === $sourceFrom $this->fillOrgCode($conditions) : $conditions;
  25.         } else {
  26.             $conditions $this->prepareSearchConditions($fields);
  27.         }
  28.         $ranks $this->getDataStatisticsService()->statisticsPersonOnlineLearnTimeRankingList($conditions);
  29.         $user $this->getCurrentUser();
  30.         $userRank $this->getDataStatisticsService()->statisticsPersonOnlineLearnTimeRankingNum($conditions$user['id']);
  31.         $learnTime $this->getDataStatisticsService()->statisticsOnlineCourseLearnTime(array_merge($conditions, ['userIds' => [$user['id']]]));
  32.         return $this->render('data-report/widgets/user-learn-rank-list.html.twig', [
  33.             'studyRanks' => ['personStudyRanks' => $ranks'sourceFrom' => $sourceFrom],
  34.             'userRank' => $userRank,
  35.             'learnTime' => $learnTime,
  36.         ]);
  37.     }
  38.     public function orgLearnRankListAction(Request $request$sourceFrom '')
  39.     {
  40.         $fields $request->query->all();
  41.         $fields['type'] = empty($fields['type']) ? 'week' $fields['type'];
  42.         if ('total' != $fields['type']) {
  43.             list($fields['startDateTime'], $fields['endDateTime']) = $this->generateStartDateAndEndDate($fields['type']);
  44.         }
  45.         $conditions $this->prepareSearchConditions($fields);
  46.         unset($conditions['userIds']);
  47.         $sourceFrom $request->query->get('sourceFrom'$sourceFrom);
  48.         $conditions 'admin' === $sourceFrom $this->fillOrgCode($conditions) : $conditions;
  49.         $ranks $this->getDataStatisticsService()->statisticsOrgLearnTimeRankingList($conditions);
  50.         $orgUsers $this->getUserService()->statisticsOrgUserNumGroupByOrgId();
  51.         $orgUsers ArrayToolkit::index($orgUsers'orgId');
  52.         if ('admin' == $sourceFrom) {
  53.             $ranks $this->buildAdminOrgLearnRankListData($ranks$orgUsers);
  54.         }
  55.         return $this->render('data-report/widgets/org-learn-rank-list.html.twig', [
  56.             'studyRanks' => ['orgStudyRanks' => $ranks'orgUsers' => $orgUsers'sourceFrom' => $sourceFrom],
  57.         ]);
  58.     }
  59.     protected function buildAdminOrgLearnRankListData($ranks$orgUsers)
  60.     {
  61.         foreach ($ranks as &$rank) {
  62.             if (!empty($orgUsers[$rank['orgId']]) && !empty($orgUsers[$rank['orgId']]['count'])) {
  63.                 $rank['totalLearnTime'] = $rank['totalLearnTime'] / $orgUsers[$rank['orgId']]['count'];
  64.                 $rank['totalLearnTime'] = round($rank['totalLearnTime'], 1);
  65.             }
  66.         }
  67.         if (!empty($ranks)) {
  68.             foreach ($ranks as $key => &$array) {
  69.                 if ($array['totalLearnTime'] > 0) {
  70.                     $key_arrays[] = $array['totalLearnTime'];
  71.                 } else {
  72.                     unset($ranks[$key]);
  73.                 }
  74.             }
  75.             if (!empty($key_arrays)) {
  76.                 array_multisort($key_arraysSORT_DESCSORT_NUMERIC$ranks);
  77.             }
  78.         }
  79.         return $ranks;
  80.     }
  81.     protected function generateStartDateAndEndDate($timeType)
  82.     {
  83.         if ('lastMonth' == $timeType) {
  84.             return DateToolkit::generateStartDateAndEndDate('month', -1);
  85.         }
  86.         return DateToolkit::generateStartDateAndEndDate($timeType);
  87.     }
  88.     protected function prepareSearchConditions($conditions)
  89.     {
  90.         if (!empty($conditions['startDateTime'])) {
  91.             $conditions['startDateTime'] = strtotime($conditions['startDateTime']);
  92.         }
  93.         if (!empty($conditions['endDateTime'])) {
  94.             $conditions['endDateTime'] = strtotime($conditions['endDateTime']);
  95.         }
  96.         if (isset($conditions['orgIds']) && empty($conditions['orgIds'])) {
  97.             $userIds = -1;
  98.         } else {
  99.             $orgIds $this->prepareOrgIds($conditions);
  100.             $userIds $this->getUserOrgService()->findUserIdsByConditions(['orgIds' => $orgIds]);
  101.             $userIds = empty($userIds) ? -$userIds;
  102.         }
  103.         $keywordUserIds = [];
  104.         if (isset($conditions['keywordType']) && !empty($conditions['keyword'])) {
  105.             $users $this->getUserService()->searchUsers(
  106.                 [
  107.                     $conditions['keywordType'] => $conditions['keyword'],
  108.                 ],
  109.                 ['id' => 'DESC'],
  110.                 0,
  111.                 PHP_INT_MAX,
  112.                 ['id']
  113.             );
  114.             $keywordUserIds = empty($users) ? [-1] : ArrayToolkit::column($users'id');
  115.             unset($conditions['keywordType']);
  116.             unset($conditions['keyword']);
  117.         }
  118.         if (-== $keywordUserIds || -== $userIds) {
  119.             $conditions['userIds'] = [-1];
  120.         } elseif (empty($keywordUserIds)) {
  121.             $conditions['userIds'] = $userIds;
  122.         } else {
  123.             $conditions['userIds'] = array_intersect($userIds$keywordUserIds);
  124.         }
  125.         unset($conditions['orgIds']);
  126.         return $conditions;
  127.     }
  128.     /**
  129.      * @return DataStatisticsService
  130.      */
  131.     protected function getDataStatisticsService()
  132.     {
  133.         return $this->createService('CorporateTrainingBundle:DataStatistics:DataStatisticsService');
  134.     }
  135.     /**
  136.      * @return UserService
  137.      */
  138.     protected function getUserService()
  139.     {
  140.         return $this->createService('CorporateTrainingBundle:User:UserService');
  141.     }
  142.     /**
  143.      * @return UserOrgService
  144.      */
  145.     protected function getUserOrgService()
  146.     {
  147.         return $this->createService('CorporateTrainingBundle:User:UserOrgService');
  148.     }
  149. }