Browse Source

Admin: Add branch_sync.unique_id to elements reported to chamilo.org. Unique ID is generated upon install and is supposed to uniquely represent each portal, which will improve tracking and reduce redundancy of information in our statistics in the long-term future

Yannick Warnier 6 years ago
parent
commit
82ebde4843

+ 16 - 0
main/inc/ajax/admin.ajax.php

@@ -1,5 +1,10 @@
 <?php
 /* For licensing terms, see /license.txt */
+
+use \Chamilo\CoreBundle\Entity\Repository\BranchSyncRepository;
+use \Chamilo\CoreBundle\Entity\BranchSync;
+use \Doctrine\Common\Collections\Criteria;
+
 /**
  * Responses to AJAX calls.
  */
@@ -158,6 +163,16 @@ function check_system_version()
             $packager = 'chamilo';
         }
 
+        $uniqueId = '';
+        $entityManager = Database::getManager();
+        /** @var BranchSyncRepository $branch */
+        $repository = $entityManager->getRepository('ChamiloCoreBundle:BranchSync');
+        /** @var BranchSync $branch */
+        $branch = $repository->getTopBranch();
+        if (is_a($branch, '\Chamilo\CoreBundle\Entity\BranchSync')) {
+            $uniqueId = $branch->getUniqueId();
+        }
+
         $data = [
             'url' => api_get_path(WEB_PATH),
             'campus' => api_get_setting('siteName'),
@@ -179,6 +194,7 @@ function check_system_version()
             // the default config file (main/install/configuration.dist.php)
             // or in the installed config file. The default value is 'chamilo'
             'packager' => $packager,
+            'unique_id' => $uniqueId,
         ];
 
         $version = null;

+ 25 - 0
src/Chamilo/CoreBundle/Entity/Repository/BranchSyncRepository.php

@@ -37,4 +37,29 @@ class BranchSyncRepository extends NestedTreeRepository
 
         return $q->execute();
     }
+
+    /**
+     * Gets the first branch with parent_id = NULL
+     * @return mixed
+     */
+    public function getTopBranch()
+    {
+        $qb = $this->createQueryBuilder('a');
+
+        //Selecting user info
+        $qb->select('DISTINCT b');
+
+        $qb->from('Chamilo\CoreBundle\Entity\BranchSync', 'b');
+        $qb->where('b.parentId IS NULL');
+        $qb->add('orderBy', 'b.id ASC');
+        $qb->setMaxResults(1);
+        $q = $qb->getQuery()->getResult();
+        if (empty($q)) {
+            return null;
+        } else {
+            foreach ($q as $result) {
+                return $result;
+            }
+        }
+    }
 }