WeightReading.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class WeightReading extends DBObject implements JsonSerializable {
  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. public function jsonSerialize() {
  29. $output=new stdClass();
  30. $output->id=(int)$this->ReadingId;
  31. $output->date=date("c", strtotime($this->Timestamp));
  32. $output->weight=(float)$this->Weight;
  33. $output->bmi=(float)$this->Bmi;
  34. $output->fat=(float)$this->Fat;
  35. return $output;
  36. }
  37. }