Example #1
0
 /**
  * Convert weight and height if necessary
  * 
  * @param \Xali\Bundle\SurvivorBundle\Entity\Survivor $survivor
  * @param \Xali\Bundle\SurvivorBundle\Converter\Converter $converter
  * @author Anthony Bocci <*****@*****.**>
  */
 private function convertWeightAndHeight($survivor, $converter)
 {
     //If weight unit is kilogrammes
     if ($survivor->getWeightUnit() == "kg") {
         $survivor->setWeight($converter->fromKgToLb($survivor->getWeight()));
     }
     //If height unit is centimeters
     if ($survivor->getHeightUnit() == "cm") {
         $survivor->setHeight($converter->fromCmToInch($survivor->getHeight()));
     }
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     /**
      * Coefficient between kilogrammes and pounds
      * 
      * @var float
      */
     $WEIGHTCOEFFICIENT = 2.20458554;
     /**
      * Coefficient between centimeters and inches
      * 
      * @var float
      */
     $HEIGHTCOEFFICIENT = 2.54;
     $firstnamesMale = array('John', 'Luke', 'Mike', 'Anakin', 'Bruce', 'Clark', 'Peet', 'Jack', 'Harry', 'Peter', 'Ron', 'Chuck', 'Cordel', 'Aladdin');
     $firstnamesFemale = array('Hermione', 'Sarah', 'Jasmine', 'Taslima', 'Alix', 'Padme', 'Rachel', 'Rose', 'Jane');
     $lastnamesMale = array('Connor', 'Skywalker', 'Lee', 'Wayne', 'Kent', 'Sparrow', 'Potter', 'Parker', 'Weasley', 'Norris', 'Walker');
     $lastnamesFemale = array('Granger', 'Connor', 'Amidala', 'Kent', 'Parker', 'Potter');
     $firstnames = array($firstnamesMale, $firstnamesFemale);
     $lastnames = array($lastnamesMale, $lastnamesFemale);
     $genders = array('m', 'f');
     $survivorsNumber = 50000;
     $camps = $manager->getRepository('XaliCampBundle:Camp')->findAll();
     $eyesColors = array('brown', 'green', 'blue', 'grey', 'black', 'other');
     $hairColors = array('blond', 'blue', 'brown', 'pink', 'purple', 'red', 'other');
     for ($i = 0; $i < $survivorsNumber; $i++) {
         $maleOrFemale = rand(0, count($genders) - 1);
         $survivor = new Survivor();
         $survivor->setGender($genders[$maleOrFemale]);
         //Today minus a number of days between 3 and 10 years
         $daysNumber = rand(1095, 146000);
         $birthday = new \DateTime();
         $birthday->sub(new \DateInterval('P' . $daysNumber . 'D'));
         $survivor->setBirthday($birthday);
         $camp = $camps[rand(0, count($camps) - 1)];
         //Each 15 times, survivor has no camp
         if ($i % 15 != 0) {
             $survivor->setCamp($camp);
         } else {
             $survivor->setCamp(null);
         }
         $firstname = $firstnames[$maleOrFemale][rand(0, count($firstnames[$maleOrFemale]) - 1)];
         $lastname = $lastnames[$maleOrFemale][rand(0, count($lastnames[$maleOrFemale]) - 1)];
         $survivor->setFirstname($firstname);
         $survivor->setLastname($lastname);
         $survivor->setHairColor($hairColors[rand(0, count($hairColors) - 1)]);
         $survivor->setEyesColor($eyesColors[rand(0, count($eyesColors) - 1)]);
         //Set height and weight according to age
         $years = $daysNumber / 365;
         if ($years <= 6) {
             $heightInCm = rand(60, 110);
         } elseif ($years <= 12) {
             $heightInCm = rand(110, 150);
         } else {
             $heightInCm = rand(150, 200);
         }
         $genderCoeff = $survivor->getGender() == 'f' ? 2.5 : 4;
         //Set coherent weight according to height
         $weightInKg = $heightInCm - 100 - ($heightInCm - 150) / $genderCoeff;
         $weight = $weightInKg * $WEIGHTCOEFFICIENT;
         $height = $heightInCm / $HEIGHTCOEFFICIENT;
         $survivor->setWeight($weight);
         $survivor->setHeight($height);
         $manager->persist($survivor);
     }
     $manager->flush();
 }
Example #3
0
 public function search(Survivor $survivor)
 {
     $queryBuilder = $this->createQueryBuilder('s');
     $queryBuilder->leftJoin('s.camp', 'c')->addSelect('c')->where('s.firstname = :firstname')->setParameter('firstname', ucfirst($survivor->getFirstname()))->andWhere('s.lastname = :lastname')->setParameter('lastname', ucfirst($survivor->getLastname()));
     if (!empty($survivor->getBirthday())) {
         $queryBuilder->andWhere('s.birthday = :birthday')->setParameter('birthday', $survivor->getBirthday());
     }
     if (!empty($survivor->getEyesColor())) {
         $queryBuilder->andWhere('s.eyesColor = :eyescolor')->setParameter('eyescolor', $survivor->getEyesColor());
     }
     if (!empty($survivor->getWeight())) {
         //Convert weight if necessary
         if ($survivor->getWeightUnit() == "kg") {
             $converter = new Converter();
             $weight = $converter->fromKgToLb($survivor->getWeight());
             $survivor->setWeight($weight);
         }
         $queryBuilder->andWhere($queryBuilder->expr()->between(':weight', 's.weight - 5', 's.weight + 5'))->setParameter('weight', $survivor->getWeight());
     }
     if (!empty($survivor->getHeight())) {
         //Convert height if necessary
         if ($survivor->getHeightUnit() == "cm") {
             $converter = new Converter();
             $height = $converter->fromCmToInch($survivor->getHeight());
             $survivor->setHeight($height);
         }
         $queryBuilder->andWhere($queryBuilder->expr()->between(':height', 's.height - 5', 's.height + 5'))->setParameter('height', $survivor->getHeight());
     }
     if (!empty($survivor->getHairColor())) {
         $queryBuilder->andWhere('s.hairColor = :haircolor')->setParameter('haircolor', $survivor->getHairColor());
     }
     return $queryBuilder->getQuery()->getResult();
 }
Example #4
0
 /**
  * Check if the survivor belong to the camp
  * Note: check survivor validity (for update for example check if the
  * given id is different to 0) above in controller
  * 
  * @param \Xali\Bundle\SurvivorBundle\Entity\Survivor $survivor
  * @param \Xali\Bundle\CampBundle\Entity\Camp $camp
  * @return boolean
  */
 public function survivorBelongsToCamp($survivor, $camp)
 {
     //If survivor or camp is invalid
     if (!$survivor instanceof Survivor || !$camp instanceof Camp) {
         return false;
     }
     /*
      * If survivor has a camp and survivor belong to $camp
      */
     return $survivor->getCamp() != null && $survivor->getCamp()->getId() == $camp->getId();
 }