示例#1
0
 /**
  * Importing Legacy Record History.
  */
 public function LegacyUpdateRecordsHistoryAction($history)
 {
     $em = $this->getDoctrine()->getManager();
     $recordHistories = json_decode($history, True);
     foreach ($recordHistories as $key => $historyValue) {
         //getting the Object if Exist from the Database
         $field = $em->getRepository('HrisFormBundle:Field')->findOneby(array('name' => $historyValue['field_name']));
         $recordObject = $em->getRepository('HrisRecordsBundle:Record')->findOneby(array('instance' => $historyValue[0]['instance']));
         if (!empty($recordObject)) {
             $startDate = new \DateTime($historyValue[0]['startdate']['date']);
             $historyObjectInstance = $em->getRepository('HrisRecordsBundle:History')->findOneby(array('record' => $recordObject, 'history' => $historyValue[0]['history'], 'startdate' => $startDate));
             if (empty($historyObjectInstance)) {
                 $historyObject = new History();
                 $historyObject->setUid(uniqid());
                 $historyObject->setField($field);
                 $historyObject->setRecord($recordObject);
                 $historyObject->setUsername($historyValue[0]['username']);
                 $historyObject->setReason($historyValue[0]['reason']);
                 $historyObject->setHistory($historyValue[0]['history']);
                 $historyObject->setStartdate($startDate);
                 $em->persist($historyObject);
             }
         }
     }
     $em->flush();
     return new Response('success');
 }
示例#2
0
 public function load(ObjectManager $manager)
 {
     $stopwatch = new Stopwatch();
     $stopwatch->start('dummyRecordGeneration');
     // Populate dummy forms
     $this->addDummyFemaleNames();
     $this->addDummyMaleNmes();
     $this->addDummyCourseNames();
     $this->addDummyCourseLocations();
     $this->addDummySponsors();
     $loadUserData = new LoadUserData();
     $loadUserData->addDummyUsers();
     $dummyUsers = $loadUserData->getUsers();
     $loadFieldData = new LoadFieldData();
     $loadFieldData->addDummyFields();
     $dummyFields = $loadFieldData->getFields();
     $loadFormData = new LoadFormData();
     $loadFormData->addDummyForms();
     $dummyForms = $loadFormData->getForms();
     $organiastionunits = $manager->getRepository('HrisOrganisationunitBundle:Organisationunit')->findAll();
     /*
      * Add data to facilities
      */
     if (!empty($organiastionunits)) {
         foreach ($organiastionunits as $organiastionunitKey => $organisationunit) {
             /*
              * Assign data to dispensary, hospital and health centres only.
              */
             if (preg_match('/dispensary|hospital|health centre|council/i', $organisationunit->getLongname())) {
                 // Initiate record entering
                 // Enter two records for each orgunit
                 for ($recordIncr = 0; $recordIncr < $this->recordsPerOrganisationunit; $recordIncr++) {
                     $record = new Record();
                     $record->setOrganisationunit($organisationunit);
                     // Enter record for public and private form
                     $formNames = array('Public Employee Form', 'Private Employee Form');
                     $formName = $formNames[array_rand($formNames, 1)];
                     if (empty($formName)) {
                         $formName = 'Public Employee Form';
                     }
                     $form = $manager->getRepository('HrisFormBundle:Form')->findOneBy(array('name' => $formName));
                     // Find history fields belonging to this form for population of data
                     $queryBuilder = $manager->createQueryBuilder();
                     $historyFields = $queryBuilder->select('field')->from('HrisFormBundle:Field', 'field')->join('field.formFieldMember', 'formFieldMember')->join('formFieldMember.form', 'form')->where('form.id=:formId')->andWhere('field.hashistory=True')->setParameter('formId', $form->getId())->getQuery()->getResult();
                     $record->setForm($form);
                     $record->setComplete(True);
                     $record->setCorrect(True);
                     $record->setHashistory(False);
                     $record->setHastraining(False);
                     $dummyUserKey = array_rand($dummyUsers, 1);
                     $dummyUsername = $dummyUsers[$dummyUserKey]['username'];
                     $record->setUsername($dummyUsername);
                     // Constructing a Value Array
                     // @todo removing hard-coding of HrisRecordBundle:Record values
                     $value = array();
                     // Fetch all field members belonging to the form and add records
                     // Roll a dice with gender of employee to pick name
                     $genders = array('Male', 'Female');
                     $gender_picked = array_rand($genders, 1);
                     $formFieldMembers = $manager->getRepository('HrisFormBundle:FormFieldMember')->findBy(array('form' => $form));
                     foreach ($formFieldMembers as $formFieldMemberKey => $formFieldMember) {
                         /**
                          * Made dynamic, on which field column is used as key, i.e. uid, name or id.
                          */
                         // Translates to $formFieldMember->getField()->getUid()
                         // or $formFieldMember->getField()->getUid() depending on value of $recordKeyName
                         $recordKeyName = ucfirst(Record::getFieldKey());
                         $valueKey = call_user_func_array(array($formFieldMember->getField(), "get{$recordKeyName}"), array());
                         if ($formFieldMember->getField()->getName() == "Firstname" || $formFieldMember->getField()->getName() == "Middlename" || $formFieldMember->getField()->getName() == "Surname" || $formFieldMember->getField()->getName() == "NextofKin") {
                             // Deal with names
                             if ($gender_picked == "Female" && ($formFieldMember->getField()->getName() == "Firstname" || $formFieldMember->getField()->getName() == "NextofKin")) {
                                 $value[$valueKey] = $this->femaleNames[array_rand($this->femaleNames, 1)];
                             } else {
                                 $value[$valueKey] = $this->maleNames[array_rand($this->maleNames, 1)];
                             }
                             if ($formFieldMember->getField()->getName() == "NextofKin") {
                                 $value[$valueKey] .= ' ' . $this->maleNames[array_rand($this->maleNames, 1)];
                             }
                             //@todo remove hard-coding of instance
                             // used later for instance formulation
                             if ($formFieldMember->getField()->getName() == "Firstname") {
                                 $firstName = $value[$valueKey];
                             }
                             if ($formFieldMember->getField()->getName() == "Middlename") {
                                 $middleName = $value[$valueKey];
                             }
                             if ($formFieldMember->getField()->getName() == "Surname") {
                                 $surname = $value[$valueKey];
                             }
                         } else {
                             if ($formFieldMember->getField()->getInputType()->getName() == "Select") {
                                 // Deal with select
                                 /**
                                  * Made dynamic, on which field column is used as key, i.e. uid, name or id.
                                  */
                                 // Translates to $fieldOptions[0]->getUid()
                                 // or $fieldOptions[0]->getValue() depending on value of $recordKeyName
                                 // $fieldOptionKey = ucfirst($record->getFieldOptionKey());
                                 //$valueKey = call_user_func_array(array($fieldOptions[0], "get${fieldOptionKey}"),array());
                                 $fieldOptionKey = ucfirst(Record::getFieldOptionKey());
                                 $fieldOptions = $manager->getRepository('HrisFormBundle:FieldOption')->findBy(array('field' => $formFieldMember->getField()));
                                 // For case of gender choose match name with gender
                                 if ($formFieldMember->getField()->getName() == "Sex") {
                                     // Made FieldOption key to store in record value array dynamic.
                                     if ($fieldOptions[0]->getValue() == $gender_picked) {
                                         $value[$valueKey] = call_user_func_array(array($fieldOptions[0], "get{$fieldOptionKey}"), array());
                                     } else {
                                         $value[$valueKey] = call_user_func_array(array($fieldOptions[1], "get{$fieldOptionKey}"), array());
                                     }
                                 } else {
                                     // Made fieldOption key to store in record value array dynamic
                                     $value[$valueKey] = call_user_func_array(array($fieldOptions[array_rand($fieldOptions, 1)], "get{$fieldOptionKey}"), array());
                                 }
                             } else {
                                 if ($formFieldMember->getField()->getInputType()->getName() == "Date") {
                                     // Deal with dates
                                     // If birth date pick 20 - 55 date range
                                     // If employment data set it to birth date range+18
                                     // If confirmation date, set it to employment date+1
                                     // If promotion date, set it to confirmation+3
                                     $beginDateStart = 50;
                                     $beginDateStop = 75;
                                     $endDateStart = 40;
                                     $endDateStop = 50;
                                     if ($formFieldMember->getField()->getName() == "DateOfBirth") {
                                         $beginDateStart = 50;
                                         $beginDateStop = 75;
                                         $endDateStart = 40;
                                         $endDateStop = 50;
                                     } elseif ($formFieldMember->getField()->getName() == "DateofFirstAppointment") {
                                         $beginDateStart -= 36;
                                         $beginDateStop -= 36;
                                         $endDateStart -= 36;
                                         $endDateStop -= 36;
                                     } elseif ($formFieldMember->getField()->getName() == "DateofConfirmation") {
                                         $beginDateStart -= 37;
                                         $beginDateStop -= 37;
                                         $endDateStart -= 37;
                                         $endDateStop -= 37;
                                     } elseif ($formFieldMember->getField()->getName() == "DateofLastPromotion") {
                                         $beginDateStart -= 40;
                                         $beginDateStop -= 40;
                                         $endDateStart -= 40;
                                         $endDateStop -= 40;
                                     }
                                     $value[$valueKey] = new \DateTime($this->getRandDate(array($beginDateStart, $beginDateStop), array($endDateStart, $endDateStop)));
                                     //@todo remove hard-coding of instance
                                     if ($formFieldMember->getField()->getName() == "DateOfBirth") {
                                         $dateOfBirth = $value[$valueKey];
                                     }
                                 } else {
                                     if ($formFieldMember->getField()->getInputType()->getName() == "Text") {
                                         // Deal with numbers
                                         if ($formFieldMember->getField()->getName() == "NumberofChildrenDependants") {
                                             $value[$valueKey] = rand(0, 10);
                                         } elseif ($formFieldMember->getField()->getName() == "CheckNumber") {
                                             $value[$valueKey] = rand(9999999, 9999999999);
                                         } elseif ($formFieldMember->getField()->getName() == "EmployersFileNumber") {
                                             $value[$valueKey] = "FN/" . rand(100, 100000);
                                         } elseif ($formFieldMember->getField()->getName() == "RegistrationNumber") {
                                             $value[$valueKey] = "RB/" . rand(10, 10000);
                                         } elseif ($formFieldMember->getField()->getName() == "MonthlyBasicSalary") {
                                             $value[$valueKey] = rand(100, 1500) . '000';
                                         } else {
                                             $value[$valueKey] = $this->maleNames[array_rand($this->maleNames, 1)] . " Street";
                                         }
                                     } else {
                                         if ($formFieldMember->getField()->getInputType()->getName() == "TextArea") {
                                             // Deal with domicile, contact
                                             if ($formFieldMember->getField()->getName() == "ContactsofEmployee" || $formFieldMember->getField()->getName() == "ContactsofNextofKin") {
                                                 $value[$valueKey] = "+255" . rand(6, 7) . rand(53, 69) . rand(01, 998) . rand(01, 998);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     $instance = md5($firstName . $middleName . $surname . $dateOfBirth->format('Y-m-d'));
                     $record->setInstance($instance);
                     $record->setValue($value);
                     //@todo check for uniqueness of instance and unique fields
                     $recordReference = strtolower(str_replace(' ', '', $record->getInstance())) . '-record';
                     $this->addReference($recordReference, $record);
                     $manager->persist($record);
                     // Randomly on flip of a coin assign history & training data
                     $outcomes = array(True, False);
                     if ($outcomes[array_rand($outcomes, 1)]) {
                         // Assign randomly between 2 to 4 histories per record
                         $numberofHistoriesToAssign = array(1, 2);
                         for ($incr = 0; $incr < $numberofHistoriesToAssign[array_rand($numberofHistoriesToAssign, 1)]; $incr++) {
                             $history = new History();
                             $history->setRecord($record);
                             $history->setUsername($record->getUsername());
                             //Calculate start date ranging starting form now-2yrs back and and stopping between 3-5 years back
                             $beginDateStart = 3;
                             $beginDateStop = 5;
                             $endDateStart = 0;
                             $endDateStop = 2;
                             $startDate = new \DateTime($this->getRandDate(array($beginDateStart, $beginDateStop), array($endDateStart, $endDateStop)));
                             $history->setStartdate($startDate);
                             $historyField = $historyFields[array_rand($historyFields, 1)];
                             //echo get_class($historyField);exit;
                             // If history field is Combo assign combo if text assign text
                             if ($historyField->getInputType() == "Select") {
                                 $historyFieldOptions = $historyField->getFieldOption();
                                 $historyFieldOptions = $historyFieldOptions->getValues();
                                 $selectedHistoryOption = $historyFieldOptions[array_rand($historyFieldOptions, 1)];
                                 $historyValue = $selectedHistoryOption->getValue();
                             } elseif ($historyField->getInputType() == "Date") {
                                 //Calculate start date ranging starting form 1-3yrs back and and stopping between 5-8 years back
                                 $beginDateStart = 5;
                                 $beginDateStop = 8;
                                 $endDateStart = 1;
                                 $endDateStop = 3;
                                 $historyDateObject = new \DateTime($this->getRandDate(array($beginDateStart, $beginDateStop), array($endDateStart, $endDateStop)));
                                 $historyValue = $historyDateObject->format('Y-m-d');
                             } else {
                                 // Deal with string history fields
                                 if ($historyField->getName() == "Firstname" || $historyField->getName() == "Middlename" || $historyField->getName() == "Surname" || $historyField->getName() == "NextofKin") {
                                     // Deal with names
                                     if ($gender_picked == "Female" && ($historyField->getName() == "Firstname" || $historyField->getName() == "NextofKin")) {
                                         $historyValue = $this->femaleNames[array_rand($this->femaleNames, 1)];
                                     } else {
                                         $historyValue = $this->maleNames[array_rand($this->maleNames, 1)];
                                     }
                                     if ($historyField->getName() == "NextofKin") {
                                         $historyValue .= ' ' . $this->maleNames[array_rand($this->maleNames, 1)];
                                     }
                                 } else {
                                     if ($historyField->getInputType()->getName() == "Text") {
                                         // Deal with numbers
                                         if ($historyField->getName() == "NumberofChildrenDependants") {
                                             $historyValue = rand(0, 10);
                                         } elseif ($historyField->getName() == "CheckNumber") {
                                             $historyValue = rand(9999999, 9999999999);
                                         } elseif ($historyField->getName() == "EmployersFileNumber") {
                                             $historyValue = "FN/" . rand(100, 100000);
                                         } elseif ($historyField->getName() == "RegistrationNumber") {
                                             $historyValue = "RB/" . rand(10, 10000);
                                         } elseif ($historyField->getName() == "MonthlyBasicSalary") {
                                             $historyValue = rand(100, 1500) . '000';
                                         } else {
                                             $historyValue = $this->maleNames[array_rand($this->maleNames, 1)] . " Street";
                                         }
                                     } else {
                                         if ($historyField->getInputType()->getName() == "TextArea") {
                                             // Deal with domicile, contact
                                             if ($historyField->getName() == "ContactsofEmployee" || $historyField->getName() == "ContactsofNextofKin") {
                                                 $historyValue = "+255" . rand(6, 7) . rand(53, 69) . rand(01, 998) . rand(01, 998);
                                             }
                                         }
                                     }
                                 }
                             }
                             $reason = $historyField->getCaption() . " changed.";
                             $history->setField($historyField);
                             $history->setHistory($historyValue);
                             $history->setReason($reason);
                             $manager->persist($history);
                             unset($history);
                         }
                         $record->setHashistory(True);
                         $manager->persist($record);
                     }
                     if ($outcomes[array_rand($outcomes, 1)]) {
                         // Assign randomly between 2 to 4 trainings per record
                         $numberofTrainingsToAssign = array(1, 2);
                         for ($incr = 0; $incr < $numberofTrainingsToAssign[array_rand($numberofTrainingsToAssign, 1)]; $incr++) {
                             $training = new Training();
                             $training->setRecord($record);
                             $training->setCoursename($this->courseNames[array_rand($this->courseNames, 1)]);
                             $training->setCourselocation($this->courseLocations[array_rand($this->courseLocations, 1)]);
                             $training->setSponsor($this->sponsor[array_rand($this->sponsor, 1)]);
                             //Calculate start date ranging starting form 9-10yrs back and and stopping between 10-12 years back
                             $beginDateStart = 10;
                             $beginDateStop = 12;
                             $endDateStart = 9;
                             $endDateStop = 10;
                             $startDate = new \DateTime($this->getRandDate(array($beginDateStart, $beginDateStop), array($endDateStart, $endDateStop)));
                             //Calculate end date ranging starting form 11-13yrs back and and stopping between 13-15 years back
                             $beginDateStart = 13;
                             $beginDateStop = 15;
                             $endDateStart = 11;
                             $endDateStop = 13;
                             $endDate = new \DateTime($this->getRandDate(array($beginDateStart, $beginDateStop), array($endDateStart, $endDateStop)));
                             $training->setStartdate($startDate);
                             $training->setEnddate($endDate);
                             $training->setUsername($record->getUsername());
                             $manager->persist($training);
                             unset($training);
                         }
                         $record->setHastraining(True);
                         $manager->persist($record);
                     }
                     unset($record);
                 }
             }
         }
     }
     $manager->flush();
     /*
      * Check Clock for time spent
      */
     $dummyRecordGenerationTime = $stopwatch->stop('dummyRecordGeneration');
     $duration = $dummyRecordGenerationTime->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 "\tDummy Records generation complete in " . $durationMessage . ".\n\n";
 }