WeightReading.php 867 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. class WeightReading extends DBObject {
  3. /**
  4. * @var IUserSettingsRepository
  5. */
  6. private $_userSettingsRepo;
  7. public static function CalculateBMI($weight, $height){
  8. if ($weight==0 || $height==0)
  9. return 0;
  10. return ($weight/$height)/$height;
  11. }
  12. public function __construct(IUserSettingsRepository $userSettingsRepo, $id=0) {
  13. parent::__construct("weight_readings", "reading_id", $id);
  14. $this->_userSettingsRepo=$userSettingsRepo;
  15. }
  16. public function GetParsedDate(){
  17. return date_parse($this->Timestamp);
  18. }
  19. public function Save(User $user=null) {
  20. if ($user==null)
  21. throw new Exception("Please specify user");
  22. $height=$this->_userSettingsRepo->GetSetting($user, "height")->Value;
  23. $height*=0.01;//convert from cm to metres;
  24. $this->Bmi=self::CalculateBMI($this->Weight, $height);
  25. $this->UserId=$user->UserId;
  26. parent::Save();
  27. }
  28. }