src/CorporateTrainingBundle/Controller/Admin/AnalysisController.php line 479

Open in your IDE?
  1. <?php
  2. namespace CorporateTrainingBundle\Controller\Admin;
  3. use AppBundle\Common\ArrayToolkit;
  4. use AppBundle\Common\ChangelogToolkit;
  5. use AppBundle\Common\CurlToolkit;
  6. use AppBundle\Controller\Admin\BaseController;
  7. use Biz\CloudPlatform\CloudAPIFactory;
  8. use Biz\Role\Util\PermissionBuilder;
  9. use Biz\System\Service\CacheService;
  10. use CorporateTrainingBundle\Biz\Focus\Service\FocusService;
  11. use CorporateTrainingBundle\Biz\OfflineActivity\Service\OfflineActivityService;
  12. use CorporateTrainingBundle\Biz\ProjectPlan\Service\ProjectPlanService;
  13. use CorporateTrainingBundle\System;
  14. use ESCloud\SDK\Service\PlatformNewsService;
  15. use ExamPlugin\Biz\Exam\Service\ExamService;
  16. use OfflineCourseProPlugin\Biz\OfflineClass\Service\OfflineClassService;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class AnalysisController extends BaseController
  19. {
  20.     private $homePageQuickEntranceCodes = [
  21.         'admin_train_teach_manage_my_teaching_courses',
  22.         'admin_train_teach_manage_my_teaching_offline_courses',
  23.         'admin_train_exam_manage_list',
  24.         'admin_project_plan_manage',
  25.         'admin_role_manage',
  26.         'admin_block',
  27.         'admin_setting_theme',
  28.         'admin_setting_mobile_settings',
  29.     ];
  30.     public function indexAction(Request $request)
  31.     {
  32.         $currentUser $this->getCurrentUser();
  33.         $quickEntrances $this->getUserQuickEntrances();
  34.         $userProfile $this->getUserService()->getUserProfile($currentUser['id']);
  35.         $homePageQuickEntranceCodes $this->homePageQuickEntranceCodes;
  36.         if ($userProfile['quick_entrance']) {
  37.             $homePageQuickEntranceCodes $userProfile['quick_entrance'];
  38.         }
  39.         $quickEntrances $this->getHomePageQuickEntrances($quickEntrances$homePageQuickEntranceCodes);
  40.         return $this->render('admin/default/corporate-training-admin-index.html.twig', [
  41.             'quickEntrances' => $quickEntrances,
  42.             'showChatGroupQrCode' => 'none' == $this->getSchoolLevelKey(),
  43.         ]);
  44.     }
  45.     public function chooseQuickEntrancesAction(Request $request)
  46.     {
  47.         $currentUser $this->getCurrentUser();
  48.         if ('POST' == $request->getMethod()) {
  49.             $params $request->request->all();
  50.             $params['data'] = json_decode($params['data'], true);
  51.             if (empty($params['data'])) {
  52.                 $params['data'] = [-1];
  53.             }
  54.             $updatedUserProfile $this->getUserService()->updateUserProfile($currentUser['id'], ['quick_entrance' => $params['data']]);
  55.             $quickEntrances $this->getUserQuickEntrances();
  56.             $quickEntrances $this->getHomePageQuickEntrances($quickEntrances$updatedUserProfile['quick_entrance']);
  57.             return $this->render('admin/default/quick-entrance.html.twig', [
  58.                 'quickEntrances' => $quickEntrances,
  59.             ]);
  60.         }
  61.         $userProfile $this->getUserService()->getUserProfile($currentUser['id']);
  62.         $quickEntrances $this->getUserQuickEntrances();
  63.         $homePageQuickEntranceCodes $this->homePageQuickEntranceCodes;
  64.         if ($userProfile['quick_entrance']) {
  65.             $homePageQuickEntranceCodes $userProfile['quick_entrance'];
  66.         }
  67.         foreach ($quickEntrances as &$item) {
  68.             if (!empty($item['data'])) {
  69.                 foreach ($item['data'] as &$entrance) {
  70.                     if (in_array($entrance['code'], $homePageQuickEntranceCodes)) {
  71.                         $entrance['checked'] = true;
  72.                     } else {
  73.                         $entrance['checked'] = false;
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         return $this->render('admin/default/quick-entrance-model.html.twig', [
  79.             'quickEntrances' => $quickEntrances,
  80.         ]);
  81.     }
  82.     protected function getHomePageQuickEntrances($quickEntrances$homePageQuickEntranceCodes)
  83.     {
  84.         foreach ($quickEntrances as &$item) {
  85.             if (!empty($item['data'])) {
  86.                 $item['data'] = array_filter(
  87.                     $item['data'],
  88.                     function ($entrance) use ($homePageQuickEntranceCodes) {
  89.                         return in_array($entrance['code'], $homePageQuickEntranceCodestrue);
  90.                     }
  91.                 );
  92.             }
  93.         }
  94.         return $quickEntrances;
  95.     }
  96.     protected function getUserQuickEntrances()
  97.     {
  98.         $currentUser $this->getCurrentUser();
  99.         $tree PermissionBuilder::instance()->findAllPermissionTree(true);
  100.         $allPermissions $tree->toArray();
  101.         $modules $allPermissions['children'][0]['children'];
  102.         $quickEntrances = [];
  103.         foreach ($modules as $module) {
  104.             $quickEntrances[$module['code']]['data'] = $this->getQuickEntrancesArray($module);
  105.             $quickEntrances[$module['code']]['name'] = $this->trans($module['name'], [], 'menu');
  106.         }
  107.         return $quickEntrances;
  108.     }
  109.     private function getQuickEntrancesArray($module)
  110.     {
  111.         $currentUser $this->getCurrentUser();
  112.         if (isset($module['quick_entrance']) && $module['quick_entrance'] && $currentUser->hasPermission($module['code'])) {
  113.             $module['name'] = $this->trans($module['name'], [], 'menu');
  114.             $moduleQuickEntrances = [$module];
  115.         } else {
  116.             $moduleQuickEntrances = [];
  117.         }
  118.         if (isset($module['children'])) {
  119.             foreach ($module['children'] as $child) {
  120.                 $moduleQuickEntrances array_merge($moduleQuickEntrances$this->getQuickEntrancesArray($child));
  121.             }
  122.         }
  123.         return $moduleQuickEntrances;
  124.     }
  125.     public function systemStatusAction(Request $request)
  126.     {
  127.         list($mainAppUpgrade$upgradeAppCount) = $this->checkCloudAppUpgradeInfo();
  128.         list($android$ios) = $this->getMobileAppVersion();
  129.         $mobileSetting $this->getSettingService()->get('mobile', []);
  130.         $mailStatus $this->isMailEnabled();
  131.         $eduCloudStatus $this->getEduCloudStatus();
  132.         return $this->render('admin/default/corporate-training-system-status.html.twig', [
  133.             'upgradeAppCount' => $upgradeAppCount,
  134.             'mainAppUpgrade' => $mainAppUpgrade,
  135.             'android' => $android,
  136.             'ios' => $ios,
  137.             'mobileSetting' => $mobileSetting,
  138.             'mailServiceStatus' => $mailStatus,
  139.             'eduCloudStatus' => $eduCloudStatus,
  140.         ]);
  141.     }
  142.     public function systemDataOverviewAction()
  143.     {
  144.         list($allUsersCount$newUsersCount) = $this->getUsersNumberData();
  145.         list($allCoursesCount$newCoursesCount) = $this->getCoursesData();
  146.         list($allTeachersCount$newTeachersCount) = $this->getTeachersData();
  147.         list($allProjectCount$newProjectCount) = $this->getProjectData();
  148.         list($allExamCount$newExamCount) = $this->getExamData();
  149.         list($allActivityCount$newActivityCount) = $this->getActivityData();
  150.         $data = [
  151.             'all' => [
  152.                 'usersCount' => $allUsersCount,
  153.                 'coursesCount' => $allCoursesCount,
  154.                 'teachersCount' => $allTeachersCount,
  155.                 'projectCount' => $allProjectCount,
  156.                 'examCount' => $allExamCount,
  157.                 'activityCount' => $allActivityCount,
  158.             ],
  159.             'new' => [
  160.                 'usersCount' => $newUsersCount,
  161.                 'coursesCount' => $newCoursesCount,
  162.                 'teachersCount' => $newTeachersCount,
  163.                 'projectCount' => $newProjectCount,
  164.                 'examCount' => $newExamCount,
  165.                 'activityCount' => $newActivityCount,
  166.             ],
  167.         ];
  168.         return $this->createJsonResponse($data);
  169.     }
  170.     public function businessAdviceAction()
  171.     {
  172.         $advice = [];
  173.         if (!$this->isWithoutNetwork() && $this->isSdkAvailable()) {
  174.             try {
  175.                 $advice $this->getPlatformNewsSdkService()->getAdvice(4'training');
  176.             } catch (\Exception $e) {
  177.                 $advice = [];
  178.             }
  179.         }
  180.         return $this->render('admin/default/admin-business-advice.html.twig', [
  181.             'advice' => $advice,
  182.         ]);
  183.     }
  184.     public function announcementsAction()
  185.     {
  186.         $result = [];
  187.         if (!$this->isWithoutNetwork() && $this->isSdkAvailable()) {
  188.             try {
  189.                 $result $this->getPlatformNewsSdkService()->getAnnouncements(1'training');
  190.             } catch (\Exception $e) {
  191.                 $result = [];
  192.             }
  193.         }
  194.         return $this->render('admin/default/admin-announcements-right.html.twig', [
  195.             'announcement' => empty($result['details']) ? [] : array_pop($result['details']),
  196.             'showChatGroupQrCode' => 'none' == $this->getSchoolLevelKey(),
  197.         ]);
  198.     }
  199.     protected function isWithoutNetwork()
  200.     {
  201.         $developer $this->getSettingService()->get('developer');
  202.         return empty($developer['without_network']) ? false : (bool) $developer['without_network'];
  203.     }
  204.     protected function isSdkAvailable()
  205.     {
  206.         $storage $this->getSettingService()->get('storage', []);
  207.         if (empty($storage['cloud_access_key']) || empty($storage['cloud_secret_key'])) {
  208.             return false;
  209.         }
  210.         return true;
  211.     }
  212.     private function isMailEnabled()
  213.     {
  214.         $cloudEmail $this->getSettingService()->get('cloud_email_crm', []);
  215.         if (!empty($cloudEmail) && 'enable' === $cloudEmail['status']) {
  216.             return true;
  217.         }
  218.         $mailer $this->getSettingService()->get('mailer', []);
  219.         if (!empty($mailer) && $mailer['enabled']) {
  220.             return true;
  221.         }
  222.         return false;
  223.     }
  224.     private function getUsersNumberData()
  225.     {
  226.         $defaultConditions = [
  227.             'locked' => 0,
  228.             'noType' => 'system',
  229.         ];
  230.         $defaultConditions $this->fillOrgCode($defaultConditions);
  231.         $allUsersCount $this->getUserService()->countUsers($defaultConditions);
  232.         $defaultConditions['startTime'] = strtotime(date('Y').'-01-01 00:00:00');
  233.         $newUsersCount $this->getUserService()->countUsers($defaultConditions);
  234.         return [$allUsersCount$newUsersCount];
  235.     }
  236.     private function getCoursesData()
  237.     {
  238.         $defaultConditions = ['orgIds' => $this->prepareOrgIds([])];
  239.         $allOnlineCoursesCount $this->getCourseSetService()->countCourseSets($defaultConditions);
  240.         $projectPlans $this->getProjectPlanService()->searchProjectPlans($defaultConditions, [], 0PHP_INT_MAX, ['id']);
  241.         $defaultConditions['startTime'] = strtotime(date('Y').'-01-01 00:00:00');
  242.         $newOnlineCoursesCount $this->getCourseSetService()->countCourseSets($defaultConditions);
  243.         $defaultConditions = ['targetType' => 'projectPlan''targetIds' => array_column($projectPlans'id') ?: [-1]];
  244.         $allOfflineCoursesCount $this->getOfflineCourseService()->countOfflineCourses($defaultConditions);
  245.         $defaultConditions['startTime'] = strtotime(date('Y').'-01-01 00:00:00');
  246.         $newOfflineCoursesCount $this->getOfflineCourseService()->countOfflineCourses($defaultConditions);
  247.         if ($this->isPluginInstalled('OfflineCoursePro')) {
  248.             $offlineClasses $this->getOfflineClassService()->searchClasses(['orgIds' => $this->prepareOrgIds([])], [], 0PHP_INT_MAX, ['id']);
  249.             $defaultConditions = ['targetType' => 'offlineClass''targetIds' => array_column($offlineClasses'id') ?: [-1]];
  250.             $allOfflineCoursesCount += $this->getOfflineCourseService()->countOfflineCourses($defaultConditions);
  251.             $defaultConditions['startTime'] = strtotime(date('Y').'-01-01 00:00:00');
  252.             $newOfflineCoursesCount += $this->getOfflineCourseService()->countOfflineCourses($defaultConditions);
  253.         }
  254.         return [$allOnlineCoursesCount $allOfflineCoursesCount$newOnlineCoursesCount $newOfflineCoursesCount];
  255.     }
  256.     private function getExamData()
  257.     {
  258.         if (!$this->isPluginInstalled('exam')) {
  259.             return [00];
  260.         }
  261.         $conditions = ['orgIds' => $this->prepareOrgIds([])];
  262.         $allExamCount $this->getExamService()->countExams($conditions);
  263.         $conditions['createdTime_GE'] = strtotime(date('Y').'-01-01 00:00:00');
  264.         $newExamCount $this->getExamService()->countExams($conditions);
  265.         return [$allExamCount$newExamCount];
  266.     }
  267.     private function getActivityData()
  268.     {
  269.         $conditions = ['orgIds' => $this->prepareOrgIds([])];
  270.         $allActivityCount $this->getOfflineActivityService()->countOfflineActivities($conditions);
  271.         $conditions['createdTime_GE'] = strtotime(date('Y').'-01-01 00:00:00');
  272.         $newActivityCount $this->getOfflineActivityService()->countOfflineActivities($conditions);
  273.         return [$allActivityCount$newActivityCount];
  274.     }
  275.     private function getTeachersData()
  276.     {
  277.         $defaultConditions = [
  278.             'locked' => 0,
  279.             'noType' => 'system',
  280.             'roles' => 'ROLE_TEACHER',
  281.         ];
  282.         $defaultConditions $this->fillOrgCode($defaultConditions);
  283.         $allTeachersCount $this->getUserService()->countUsers($defaultConditions);
  284.         $defaultConditions['startTime'] = strtotime(date('Y').'-01-01 00:00:00');
  285.         $newTeachersCount $this->getUserService()->countUsers($defaultConditions);
  286.         return [$allTeachersCount$newTeachersCount];
  287.     }
  288.     private function getProjectData()
  289.     {
  290.         $defaultConditions = ['orgIds' => $this->prepareOrgIds([])];
  291.         $allOfflineClassCount 0;
  292.         $newOfflineClassCount 0;
  293.         $allProjectPlansCount $this->getProjectPlanService()->countProjectPlans($defaultConditions);
  294.         $defaultConditions['createdTime_GE'] = strtotime(date('Y').'-01-01 00:00:00');
  295.         $newProjectPlansCount $this->getProjectPlanService()->countProjectPlans($defaultConditions);
  296.         if ($this->isPluginInstalled('OfflineCoursePro')) {
  297.             $allOfflineClassCount $this->getOfflineClassService()->countClasses(['orgIds' => $this->prepareOrgIds([])]);
  298.             $newOfflineClassCount $this->getOfflineClassService()->countClasses($defaultConditions);
  299.         }
  300.         return [$allProjectPlansCount $allOfflineClassCount$newProjectPlansCount $newOfflineClassCount];
  301.     }
  302.     /**
  303.      * @return \CorporateTrainingBundle\Biz\OfflineCourse\Service\Impl\OfflineCourseServiceImpl
  304.      */
  305.     protected function getOfflineCourseService()
  306.     {
  307.         return $this->createService('CorporateTrainingBundle:OfflineCourse:OfflineCourseService');
  308.     }
  309.     /**
  310.      * @return OfflineActivityService
  311.      */
  312.     protected function getOfflineActivityService()
  313.     {
  314.         return $this->createService('OfflineActivity:OfflineActivityService');
  315.     }
  316.     /**
  317.      * @return ExamService
  318.      */
  319.     protected function getExamService()
  320.     {
  321.         return $this->createService('ExamPlugin:Exam:ExamService');
  322.     }
  323.     private function checkCloudAppUpgradeInfo()
  324.     {
  325.         $apps $this->getAppService()->checkAppUpgrades();
  326.         $upgradeAppCount count($apps);
  327.         if (!is_null($apps)) {
  328.             $indexApps ArrayToolkit::index($apps'code');
  329.         }
  330.         $mainAppUpgrade = empty($indexApps['TRAININGMAIN']) ? [] : $indexApps['TRAININGMAIN'];
  331.         if ($mainAppUpgrade) {
  332.             $upgradeAppCount $upgradeAppCount 1;
  333.         }
  334.         return [$mainAppUpgrade$upgradeAppCount];
  335.     }
  336.     private function getMobileAppVersion()
  337.     {
  338.         return [$this->getMobileVersion('android'), $this->getMobileVersion('iphone')];
  339.     }
  340.     private function getEduCloudStatus()
  341.     {
  342.         try {
  343.             $api CloudAPIFactory::create('root');
  344.             $overview $api->get("/cloud/{$api->getAccessKey()}/overview");
  345.             if (!$overview['enabled']) {
  346.                 return false;
  347.             }
  348.             if (!($overview['accessCloud'])) {
  349.                 return false;
  350.             }
  351.             return true;
  352.         } catch (\Exception $e) {
  353.             return false;
  354.         }
  355.     }
  356.     private function getMobileVersion($system)
  357.     {
  358.         $mobileVersion CurlToolkit::request('GET'"http://www.edusoho.com/version/edusoho-training-{$system}", []);
  359.         if (empty($mobileVersion)) {
  360.             return;
  361.         }
  362.         return $mobileVersion;
  363.     }
  364.     /**
  365.      * @return FocusService
  366.      */
  367.     protected function getFocusService()
  368.     {
  369.         return $this->createService('CorporateTrainingBundle:Focus:FocusService');
  370.     }
  371.     protected function getAppService()
  372.     {
  373.         return $this->createService('CloudPlatform:AppService');
  374.     }
  375.     protected function getSettingService()
  376.     {
  377.         return $this->createService('System:SettingService');
  378.     }
  379.     /**
  380.      * @return ProjectPlanService
  381.      */
  382.     protected function getProjectPlanService()
  383.     {
  384.         return $this->createService('CorporateTrainingBundle:ProjectPlan:ProjectPlanService');
  385.     }
  386.     /**
  387.      * 管理后台-首页-更新日志
  388.      *
  389.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  390.      */
  391.     public function changelogAction(Request $request)
  392.     {
  393.         $rootDir $this->container->getParameter('kernel.root_dir');
  394.         $changelogPath dirname($rootDir).'/src/CorporateTrainingBundle/CHANGELOG';
  395.         $changelog explode(PHP_EOL.PHP_EOLfile_get_contents($changelogPath));
  396.         $currentChangeLog ChangelogToolkit::parseSingleChangelog($changelog[0]);
  397.         $currentChangeLog['jumpUrl'] = $this->generateUrl('changelog_list');
  398.         return $this->createJsonResponse($currentChangeLog);
  399.     }
  400.     /**
  401.      * 管理后台-首页-帮助中心
  402.      *
  403.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  404.      */
  405.     public function helpCenterAction(Request $request)
  406.     {
  407.         $url $request->query->get('url''');
  408.         $cacheName md5('admin_help_center_'.$url);
  409.         $helpCenterInfo $this->getCacheService()->get($cacheName);
  410.         if (null == $helpCenterInfo) {
  411.             $ctHelpCenterUri 'https://ct.edusoho.com/faq/api/hotlist?';
  412.             $params = [
  413.                 'version' => System::CT_VERSION,
  414.                 'url' => $url,
  415.             ];
  416.             if ('/admin/' == $url || '/admin' == $url) {
  417.                 unset($params['url']);
  418.             }
  419.             $ctHelpCenterUrl $ctHelpCenterUri.http_build_query($params);
  420.             $helpCenterInfo CurlToolkit::request('GET'$ctHelpCenterUrl, [], []);
  421.             if (!empty($helpCenterInfo)) {
  422.                 $this->getCacheService()->clear($cacheName);
  423.                 $this->getCacheService()->set($cacheName$helpCenterInfotime() + rand(37) * 24 60 60);
  424.             }
  425.         }
  426.         return $this->createJsonResponse($helpCenterInfo);
  427.     }
  428.     protected function getSchoolLevelKey()
  429.     {
  430.         $settings $this->getSettingService()->get('storage', []);
  431.         if (empty($settings['cloud_access_key']) || empty($settings['cloud_secret_key'])) {
  432.             return 'none';
  433.         }
  434.         $info = [];
  435.         try {
  436.             $info CloudAPIFactory::create('root')->get('/me');
  437.         } catch (\RuntimeException $e) {
  438.             $info['error'] = 'error';
  439.         }
  440.         if (empty($info['userLevel'])) {
  441.             return 'none';
  442.         }
  443.         if (in_array($info['userLevel'], ['none''license''custom''saas'])) {
  444.             return $info['userLevel'];
  445.         }
  446.         return 'none';
  447.     }
  448.     /**
  449.      * @return OfflineClassService
  450.      */
  451.     protected function getOfflineClassService()
  452.     {
  453.         return $this->createService('OfflineCourseProPlugin:OfflineClass:OfflineClassService');
  454.     }
  455.     protected function getCourseSetService()
  456.     {
  457.         return $this->createService('Course:CourseSetService');
  458.     }
  459.     
  460.     /**
  461.      * @return CacheService
  462.      */
  463.     protected function getCacheService()
  464.     {
  465.         return $this->createService('System:CacheService');
  466.     }
  467.     /**
  468.      * @return PlatformNewsService
  469.      */
  470.     protected function getPlatformNewsSdkService()
  471.     {
  472.         $biz $this->getBiz();
  473.         return $biz['ESCloudSdk.platformNews'];
  474.     }
  475. }