/** * Edits an existing Organisationunit entity. * * @Secure(roles="ROLE_SUPER_USER,ROLE_ORGANISATIONUNIT_UPDATE") * @Route("/{id}", requirements={"id"="\d+"}, name="organisationunit_update") * @Method("PUT") * @Template("HrisOrganisationunitBundle:Organisationunit:edit.html.twig") */ public function updateAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('HrisOrganisationunitBundle:Organisationunit')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Organisationunit entity.'); } $deleteForm = $this->createDeleteForm($id); $editForm = $this->createForm(new OrganisationunitType(), $entity); $editForm->bind($request); $completenessForms = $this->getDoctrine()->getManager()->getRepository('HrisFormBundle:Form')->findAll(); if ($editForm->isValid()) { // Persist completeness figures too $completenessFigures = $request->request->get('hris_organisationunitbundle_organisationunittype_completeness'); //Get rid of current expectations $em->createQueryBuilder('organisationunitCompleteness')->delete('HrisOrganisationunitBundle:OrganisationunitCompleteness', 'organisationunitCompleteness')->where('organisationunitCompleteness.organisationunit= :organisationunitId')->setParameter('organisationunitId', $entity->getId())->getQuery()->getResult(); $em->flush(); foreach ($completenessForms as $completenessFormKey => $completenessForm) { if (isset($completenessFigures[$completenessForm->getUid()]) && !empty($completenessFigures[$completenessForm->getUid()])) { $organisationunitCompleteness = new OrganisationunitCompleteness(); $organisationunitCompleteness->setOrganisationunit($entity); $organisationunitCompleteness->setForm($completenessForm); $organisationunitCompleteness->setExpectation($completenessFigures[$completenessForm->getUid()]); $entity->addOrganisationunitCompletenes($organisationunitCompleteness); unset($organisationunitCompleteness); } } // Persist organisationunit groups $organisationunitGroupsetsContents = $request->request->get('hris_organisationunitbundle_orgnisationunittype_groupsets'); $organisationunitGroupsets = $this->getDoctrine()->getManager()->getRepository('HrisOrganisationunitBundle:OrganisationunitGroupset')->findAll(); $entity->removeAllOrganisationunitGroups(); $em->persist($entity); foreach ($organisationunitGroupsets as $organisationunitGroupsetKey => $organisationunitGroupset) { // Go through groups in the groupset and remove membership foreach ($organisationunitGroupset->getOrganisationunitGroup() as $organisationunitGroupKey => $organisationunitGroup) { $organisationunitGroup->removeOrganisationunit($entity); } $organisationunitGroupId = $organisationunitGroupsetsContents[$organisationunitGroupset->getUid()]; $organisationunitGroup = $this->getDoctrine()->getRepository('HrisOrganisationunitBundle:OrganisationunitGroup')->findOneBy(array('id' => $organisationunitGroupId)); $organisationunitGroup->addOrganisationunit($entity); } $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('organisationunit_show', array('id' => $id))); } else { $completenessForms = $this->getDoctrine()->getManager()->getRepository('HrisFormBundle:Form')->findAll(); //Support addition of group and groupset $organisationunitGroupsets = $this->getDoctrine()->getManager()->getRepository('HrisOrganisationunitBundle:OrganisationunitGroupset')->findAll(); } return array('entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView(), 'completenessForms' => $completenessForms); }
/** * 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"; }
/** * Update organisationunit completeness through ajax. * * @Secure(roles="ROLE_SUPER_USER,ROLE_ORGANISATIONUNITCOMPLETENESS_AJAXUPDATE") * @Route("/ajaxupdate", name="organisationunitcompleteness_ajaxupdate") * @Method("POST") * @Template() */ public function ajaxUpdateAction(Request $request) { $em = $this->getDoctrine()->getManager(); // Persist completeness figures too $completenessValue = $request->request->get('value'); $completenessId = $request->request->get('id'); $idArray = explode('_', $completenessId); $organisationunitId = $idArray[1]; $formId = $idArray[2]; // Check if Stored completeness exist already $queryBuilder = $this->getDoctrine()->getManager()->createQueryBuilder(); $storedCompletenessIds = $queryBuilder->select('organisationunitCompleteness.id')->from('HrisOrganisationunitBundle:Organisationunit', 'organisationunit')->join('organisationunit.organisationunitCompleteness', 'organisationunitCompleteness')->join('organisationunitCompleteness.form', 'form')->join('organisationunit.organisationunitStructure', 'organisationunitStructure')->andWhere($queryBuilder->expr()->in('form.id', $formId))->andWhere('organisationunit.id=:organisationunitId')->andWhere('organisationunit.active=True')->setParameters(array('organisationunitId' => $organisationunitId))->getQuery()->getResult(); if (!empty($storedCompletenessIds)) { // Update existing completeness $storedCompletenessId = $this->array_value_recursive('id', $storedCompletenessIds); $entity = $em->getRepository('HrisOrganisationunitBundle:OrganisationunitCompleteness')->find($storedCompletenessId); $entity->setExpectation($completenessValue); } else { // Insert new completeness $entity = new OrganisationunitCompleteness(); // Organisationunit object $organisationunit = $em->getRepository('HrisOrganisationunitBundle:Organisationunit')->find($organisationunitId); $form = $em->getRepository('HrisFormBundle:Form')->find($formId); $entity->setOrganisationunit($organisationunit); $entity->setForm($form); $entity->setExpectation($completenessValue); } // Persist & flush $em->persist($entity); $em->flush(); return array('completenessValue' => $completenessValue); }