/**
  * Loads metadata into the database
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $stopwatch = new Stopwatch();
     $stopwatch->start('dummyOrganisationGeneration');
     $this->addDummyOrganisationunits();
     // Dummy organisationunit levels
     $this->addDummyOrganisationunitLevels();
     $this->addDummyOrganisationunitNames();
     // Keep Array of distinct parent and lognames existing
     $distinctLongnameAndParent = array();
     // Populate dummy organisationunits
     foreach ($this->organisationunits as $organisationunitKey => $humanResourceOrganisationunit) {
         $organisationunit = new Organisationunit();
         $organisationunit->setCode($humanResourceOrganisationunit['shortname']);
         $organisationunit->setShortname($humanResourceOrganisationunit['shortname']);
         $organisationunit->setLongname($humanResourceOrganisationunit['longname']);
         if (isset($humanResourceOrganisationunit['description']) && !empty($humanResourceOrganisationunit['description'])) {
             $organisationunit->setDescription($humanResourceOrganisationunit['description']);
         }
         if (isset($humanResourceOrganisationunit['parent']) && !empty($humanResourceOrganisationunit['parent'])) {
             $parentReference = strtolower(str_replace(' ', '', $humanResourceOrganisationunit['parent'])) . '-organisationunit';
             $parentOrganisationunit = $manager->merge($this->getReference($parentReference));
             $organisationunit->setParent($parentOrganisationunit);
             $distinctLongnameAndParent[] = array('longname' => $humanResourceOrganisationunit['shortname'], 'parent' => $parentOrganisationunit->getId());
             $organisationunitReference = strtolower(str_replace(' ', '', $humanResourceOrganisationunit['shortname'])) . '-organisationunit';
             $this->addReference($organisationunitReference, $organisationunit);
             $manager->persist($organisationunit);
             // Keep reference index for senquential generation of organisation unit structure
             $this->addToIndexedOrganisationunit($organisationunitReference);
         } else {
             $organisationunitReference = strtolower(str_replace(' ', '', $humanResourceOrganisationunit['shortname'])) . '-organisationunit';
             $this->addReference($organisationunitReference, $organisationunit);
             $manager->persist($organisationunit);
             // Keep reference index for senquential generation of organisation unit structure
             $this->addToIndexedOrganisationunit($organisationunitReference);
         }
         unset($organisationunit);
         // Randomly populate dispensaries, health centres & hospitals under municipal & district councils
         if (strpos($humanResourceOrganisationunit['longname'], 'District Council') > 0 || strpos($humanResourceOrganisationunit['longname'], 'Municipal Council') > 0 || strpos($humanResourceOrganisationunit['longname'], 'City Council') > 0 || strpos($humanResourceOrganisationunit['longname'], 'Town Council') > 0) {
             $dispensaryCount = rand($this->minDispensaryCount, $this->maxDispensaryCount);
             $healthCentreCount = rand($this->minHealthCentreCount, $this->maxHealthCentreCount);
             $hospitalCount = rand($this->minHospitalCount, $this->maxHospitalCount);
             $parentReference = strtolower(str_replace(' ', '', $humanResourceOrganisationunit['shortname'])) . '-organisationunit';
             $parentOrganisationunit = $manager->merge($this->getReference($parentReference));
             // Populate Dispensaries
             for ($dispensaryIncr = 0; $dispensaryIncr < $dispensaryCount; $dispensaryIncr++) {
                 $dispensary = new Organisationunit();
                 //Kip picking dispensaries randomly until unique reference is found
                 do {
                     $dispensaryKey = array_rand($this->organisationunitNames, 1);
                     $dispensaryName = $this->organisationunitNames[$dispensaryKey] . " Dispensary";
                     $dispensaryShortname = substr(strtolower(str_replace(' ', '', str_replace(' Dispensary', '', $dispensaryName))), 0, 12) . substr($parentOrganisationunit->getShortname(), 0, 5) . 'dsp';
                     $dispensaryReference = strtolower(str_replace(' ', '', $dispensaryShortname . substr($parentOrganisationunit->getShortname(), 0, 5))) . '-organisationunit';
                     $parentorgunitreference = array('longname' => $dispensaryName, 'parent' => $parentOrganisationunit->getId());
                 } while ($this->hasReference($dispensaryReference) || in_array($parentorgunitreference, $distinctLongnameAndParent));
                 $dispensary->setCode($dispensaryShortname);
                 $dispensary->setShortname($dispensaryShortname);
                 $dispensary->setLongname($dispensaryName);
                 $dispensary->setParent($parentOrganisationunit);
                 $dispensary->setActive(true);
                 $this->addReference($dispensaryReference, $dispensary);
                 $distinctLongnameAndParent[] = array('longname' => $dispensaryName, 'parent' => $parentOrganisationunit->getId());
                 $manager->persist($dispensary);
                 // Populate expected completeness figures for public and private
                 // Enter record for public and private form
                 $formNames = array('Public Employee Form', 'Private Employee Form');
                 $form = $manager->getRepository('HrisFormBundle:Form')->findOneBy(array('name' => $formNames[array_rand($formNames, 1)]));
                 $organisationunitCompleteness = new OrganisationunitCompleteness();
                 $organisationunitCompleteness->setOrganisationunit($dispensary);
                 $organisationunitCompleteness->setForm($form);
                 $expectations = array(2, 3, 4);
                 $organisationunitCompleteness->setExpectation($expectations[array_rand($expectations, 1)]);
                 $manager->persist($organisationunitCompleteness);
                 // Keep reference index for senquential generation of organisation unit structure
                 $this->addToIndexedOrganisationunit($dispensaryReference);
                 $dispensary = NULL;
                 $dispensaryReference = NULL;
             }
             // Populate Health Centre
             for ($healthCentreIncr = 0; $healthCentreIncr < $healthCentreCount; $healthCentreIncr++) {
                 $healthCentre = new Organisationunit();
                 //Kip picking health centres randomly until unique reference is found
                 do {
                     $healthCentreKey = array_rand($this->organisationunitNames, 1);
                     $healthCentreName = $this->organisationunitNames[$healthCentreKey] . " Health Centre";
                     $healthCentreShortname = substr(strtolower(str_replace(' ', '', str_replace(' Health Centre', '', $healthCentreName))), 0, 12) . substr($parentOrganisationunit->getShortname(), 0, 5) . 'htc';
                     $healthCentreReference = strtolower(str_replace(' ', '', $healthCentreShortname . substr($parentOrganisationunit->getShortname(), 0, 5))) . '-organisationunit';
                     $parentorgunitreference = array('longname' => $healthCentreName, 'parent' => $parentOrganisationunit->getId());
                 } while ($this->hasReference($healthCentreReference) || in_array($parentorgunitreference, $distinctLongnameAndParent));
                 $healthCentre->setCode($healthCentreShortname);
                 $healthCentre->setShortname($healthCentreShortname);
                 $healthCentre->setLongname($healthCentreName);
                 $healthCentre->setParent($parentOrganisationunit);
                 $healthCentre->setActive(true);
                 $this->addReference($healthCentreReference, $healthCentre);
                 $distinctLongnameAndParent[] = array('longname' => $healthCentreName, 'parent' => $parentOrganisationunit->getId());
                 $manager->persist($healthCentre);
                 // Populate expected completeness figures for public and private
                 // Enter record for public and private form
                 $formNames = array('Public Employee Form', 'Private Employee Form');
                 $form = $manager->getRepository('HrisFormBundle:Form')->findOneBy(array('name' => $formNames[array_rand($formNames, 1)]));
                 $organisationunitCompleteness = new OrganisationunitCompleteness();
                 $organisationunitCompleteness->setOrganisationunit($healthCentre);
                 $organisationunitCompleteness->setForm($form);
                 $organisationunitCompleteness->setExpectation(array_rand(array(2, 3, 4), 1));
                 $manager->persist($organisationunitCompleteness);
                 // Keep reference index for senquential generation of organisation unit structure
                 $this->addToIndexedOrganisationunit($healthCentreReference);
                 $healthCentre = NULL;
                 $healthCentreReference = NULL;
             }
             // Populate Hosptial
             for ($hospitalIncr = 0; $hospitalIncr < $hospitalCount; $hospitalIncr++) {
                 $hospital = new Organisationunit();
                 //Kip picking hospitals randomly until unique reference is found
                 do {
                     $hospitalKey = array_rand($this->organisationunitNames, 1);
                     $hospitalName = $this->organisationunitNames[$hospitalKey] . " Hospital";
                     $hospitalName = str_replace(' 	', ' ', str_replace('   ', ' ', str_replace('  ', ' ', str_replace('\\t', ' ', $hospitalName))));
                     $hospitalShortname = substr(strtolower(str_replace(' ', '', str_replace(' Hospital', '', $hospitalName))), 0, 12) . substr($parentOrganisationunit->getShortname(), 0, 5) . 'hsp';
                     $hospitalReference = strtolower(str_replace(' ', '', $hospitalShortname . substr($parentOrganisationunit->getShortname(), 0, 5))) . '-organisationunit';
                     $parentorgunitreference = array('longname' => $hospitalName, 'parent' => $parentOrganisationunit->getId());
                 } while ($this->hasReference($hospitalReference) || in_array($parentorgunitreference, $distinctLongnameAndParent));
                 $hospital->setCode($hospitalShortname);
                 $hospital->setShortname($hospitalShortname);
                 $hospital->setLongname($hospitalName);
                 $hospital->setParent($parentOrganisationunit);
                 $hospital->setActive(true);
                 $this->addReference($hospitalReference, $hospital);
                 $distinctLongnameAndParent[] = array('longname' => $hospitalName, 'parent' => $parentOrganisationunit->getId());
                 $manager->persist($hospital);
                 // Populate expected completeness figures for public and private
                 // Enter record for public and private form
                 $formNames = array('Public Employee Form', 'Private Employee Form');
                 $form = $manager->getRepository('HrisFormBundle:Form')->findOneBy(array('name' => $formNames[array_rand($formNames, 1)]));
                 $organisationunitCompleteness = new OrganisationunitCompleteness();
                 $organisationunitCompleteness->setOrganisationunit($hospital);
                 $organisationunitCompleteness->setForm($form);
                 $organisationunitCompleteness->setExpectation(array_rand(array(2, 3, 4), 1));
                 $manager->persist($organisationunitCompleteness);
                 // Keep reference index for senquential generation of organisation unit structure
                 $this->addToIndexedOrganisationunit($hospitalReference);
                 $hospital = NULL;
                 $hospitalReference = NULL;
             }
         }
         $parentOrganisationunit = NULL;
     }
     /**
      * Generate organisation unit structure and
      * organisationunit levels.
      */
     // Workound parent reference
     $organisationunitLevelReference = strtolower(str_replace(' ', '', 'Level 1')) . '-organisationunitlevel';
     if ($this->hasReference($organisationunitLevelReference)) {
         // Get orgunitlevel from reference
         $organisationunitLevel = $this->getReference($organisationunitLevelReference);
     } else {
         // Persist and it's reference
         $organisationunitLevel = new OrganisationunitLevel();
         $organisationunitLevel->setLevel(1);
         //            $levelName = 'Level '.$organisationunitLevel->getLevel();
         //            if($organisationunitLevel->getLevel()==1) $levelName="Ministry Of Health &SW";
         //            $organisationunitLevel->setName($levelName);
         $organisationunitLevel->setName('Level ' . $organisationunitLevel->getLevel());
         $this->addReference($organisationunitLevelReference, $organisationunitLevel);
         $manager->persist($organisationunitLevel);
     }
     // Generating organisationunit structure
     if (!empty($this->indexedOrganisationunits)) {
         foreach ($this->getIndexedOrganisationunits() as $indexedOrganisationunitKey => $indexedOrganisationunitReference) {
             $organisationunit = $manager->merge($this->getReference($indexedOrganisationunitReference));
             // Populate orgunit structure
             $organisationunitStructure = new OrganisationunitStructure();
             $organisationunitStructure->setOrganisationunit($organisationunit);
             // Figureout level on the structure by parent
             if ($organisationunit->getParent() == NULL) {
                 // Use created default first level for organisationunit structure
                 $organisationunitStructure->setLevel($organisationunitLevel);
                 $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit());
             } else {
                 // Create new orgunit structure based parent structure
                 //Refer to previously created orgunit structure.
                 $parentOrganisationunitStructureReferenceName = strtolower(str_replace(' ', '', $organisationunit->getParent()->getShortname())) . '-organisationunitstructure';
                 $parentOrganisationunitStructureByReference = $manager->merge($this->getReference($parentOrganisationunitStructureReferenceName));
                 // Cross check to see if level is already created for reusability.
                 $currentOrganisationunitLevelname = 'Level ' . ($parentOrganisationunitStructureByReference->getLevel()->getLevel() + 1);
                 if ($this->hasReference(strtolower(str_replace(' ', '', $currentOrganisationunitLevelname)) . '-organisationunitlevel')) {
                     // Reuse existing reference
                     $currentOrganisationunitLevel = $this->getReference(strtolower(str_replace(' ', '', $currentOrganisationunitLevelname)) . '-organisationunitlevel');
                     $organisationunitLevel = $manager->merge($currentOrganisationunitLevel);
                 } else {
                     // Create new Level and reference.
                     $organisationunitLevel = new OrganisationunitLevel();
                     //                        $organisationunitLevel->setLevel($levelName);
                     $organisationunitLevel->setLevel($parentOrganisationunitStructureByReference->getLevel()->getLevel() + 1);
                     $organisationunitLevel->setName('Level ' . $organisationunitLevel->getLevel());
                     //Wild hack to set data entry level
                     if ($organisationunitLevel->getLevel() == 4) {
                         $organisationunitLevel->setDataentrylevel(True);
                         $organisationunitLevel->setDescription("Data Entry Level");
                     }
                     $organisationunitLevelReference = strtolower(str_replace(' ', '', $organisationunitLevel->getName())) . '-organisationunitlevel';
                     $this->addReference($organisationunitLevelReference, $organisationunitLevel);
                     $manager->persist($organisationunitLevel);
                 }
                 // Use reference of created/re-used level
                 $organisationunitStructure->setLevel($organisationunitLevel);
                 unset($organisationunitLevel);
                 /*
                  * Append Level organisation units based on their parent level.
                  */
                 if ($organisationunitStructure->getLevel()->getLevel() == 1) {
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                 } elseif ($organisationunitStructure->getLevel()->getLevel() == 2) {
                     $organisationunitStructure->setLevel2Organisationunit($organisationunitStructure->getOrganisationunit());
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                 } elseif ($organisationunitStructure->getLevel()->getLevel() == 3) {
                     $organisationunitStructure->setLevel3Organisationunit($organisationunitStructure->getOrganisationunit());
                     $organisationunitStructure->setLevel2Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent());
                 } elseif ($organisationunitStructure->getLevel()->getLevel() == 4) {
                     $organisationunitStructure->setLevel4Organisationunit($organisationunitStructure->getOrganisationunit());
                     $organisationunitStructure->setLevel3Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                     $organisationunitStructure->setLevel2Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent());
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent());
                 } elseif ($organisationunitStructure->getLevel()->getLevel() == 5) {
                     $organisationunitStructure->setLevel5Organisationunit($organisationunitStructure->getOrganisationunit());
                     $organisationunitStructure->setLevel4Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                     $organisationunitStructure->setLevel3Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent());
                     $organisationunitStructure->setLevel2Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent());
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent()->getParent());
                 } elseif ($organisationunitStructure->getLevel()->getLevel() == 6) {
                     $organisationunitStructure->setLevel6Organisationunit($organisationunitStructure->getOrganisationunit());
                     $organisationunitStructure->setLevel5Organisationunit($organisationunitStructure->getOrganisationunit()->getParent());
                     $organisationunitStructure->setLevel4Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent());
                     $organisationunitStructure->setLevel3Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent());
                     $organisationunitStructure->setLevel2Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent()->getParent()->getParent());
                     $organisationunitStructure->setLevel1Organisationunit($organisationunitStructure->getOrganisationunit()->getParent()->getParent()->getParent()->getParent()->getParent()->getParent());
                 }
             }
             $organisationunitStructureReference = strtolower(str_replace(' ', '', $organisationunit->getShortname())) . '-organisationunitstructure';
             $this->addReference($organisationunitStructureReference, $organisationunitStructure);
             $manager->persist($organisationunitStructure);
             unset($organisationunitStructure);
         }
     }
     // Once organisatinounits are in database, assign admin to ministry
     // and district user to one of the districts
     //admin user
     $adminUserByReference = $manager->merge($this->getReference('admin-user'));
     $mohswByReference = $manager->merge($this->getReference('mohsw-organisationunit'));
     $adminUserByReference->setOrganisationunit($mohswByReference);
     $manager->persist($adminUserByReference);
     //district user
     $districtUserByReference = $manager->merge($this->getReference('district-user'));
     $arushadcByReference = $manager->merge($this->getReference('arushadc-organisationunit'));
     $districtUserByReference->setOrganisationunit($arushadcByReference);
     $manager->persist($districtUserByReference);
     //hospital user
     $hospitalUserByReference = $manager->merge($this->getReference('hospital-user'));
     $bugandorefhspByReference = $manager->merge($this->getReference('bugandorefhsp-organisationunit'));
     $hospitalUserByReference->setOrganisationunit($bugandorefhspByReference);
     $manager->persist($hospitalUserByReference);
     $manager->flush();
     /*
      * Check Clock for time spent
      */
     $dummyOrganisationGenerationTime = $stopwatch->stop('dummyOrganisationGeneration');
     $duration = $dummyOrganisationGenerationTime->getDuration() / 1000;
     unset($stopwatch);
     if ($duration < 60) {
         $durationMessage = round($duration, 2) . ' seconds';
     } elseif ($duration >= 60 && $duration < 3600) {
         $durationMessage = round($duration / 60, 2) . ' minutes';
     } elseif ($duration >= 3600 && $duration < 216000) {
         $durationMessage = round($duration / 3600, 2) . ' hours';
     } else {
         $durationMessage = round($duration / 86400, 2) . ' hours';
     }
     //echo "Dummy Organisationunit generation complete in ". $durationMessage .".\n\n";
 }
Example #2
0
 /**
  * Importing Legacy Organisation Units.
  *
  */
 public function LegacyUpdateOrganisationUnitsAction($organisationUnits)
 {
     global $refOrganisationUnit;
     $em = $this->getDoctrine()->getManager();
     $organisationUnits = json_decode($organisationUnits, true);
     foreach ($organisationUnits as $key => $organisationUnit) {
         $parent = null;
         $parent = $em->getRepository('HrisOrganisationunitBundle:Organisationunit')->findOneby(array('longname' => $organisationUnit['longname']));
         $orgunitObjectCheck = $em->getRepository('HrisOrganisationunitBundle:Organisationunit')->findOneby(array('longname' => $organisationUnit[0]['longname'], 'parent' => $parent));
         if ($orgunitObjectCheck != NULL) {
             $refOrganisationUnit[$organisationUnit[0]['id']] = $orgunitObjectCheck->getUid();
             // print 'this record Exists '.$organisationUnit[0]['longname']." Parent: ".$organisationUnit['longname'].'<br>';
         } else {
             $orgunitObject = new Organisationunit();
             $orgunitObject->setUid(uniqid());
             if ($parent != NULL) {
                 $orgunitObject->setParent($parent);
                 //print '<br>Parent Exists with LongName: '. $parent->getLongname();
             } else {
                 $parent = NULL;
                 $orgunitObject->setParent($parent);
             }
             $orgunitObject->setShortname($organisationUnit[0]['shortname']);
             $orgunitObject->setLongname($organisationUnit[0]['longname']);
             $orgunitObject->setCode($organisationUnit[0]['code']);
             $em->persist($orgunitObject);
             $em->flush();
             $refOrganisationUnit[$organisationUnit[0]['id']] = $orgunitObject->getUid();
         }
     }
     $em->flush();
     return new Response('success');
 }