id = $id; } $this->load($this->id); } function load($id = NULL) { if (is_numeric($id)) { $this->id = $id; } $ret = false; if (is_numeric($this->id)) { $q = "SELECT * FROM " . DB_PREFIX . "user_trainings WHERE id=".$this->id; $ret = query_row($q); } if ( $ret) { $this->id = $ret->id; $this->title = $ret->title; $mysql_training_date = $ret->training_date; $php_training_date = strtotime($mysql_training_date); $training_date = date("d.m.Y", $php_training_date); $this->training_date = $training_date; $this->setBeginningTimes($ret->beginning); $this->setEndingTimes($ret->ending); $this->setSleepingTimes($ret->sleeping_time_in_min); $this->warmup_device = $ret->warmup_device; $this->setWarmupDurationTimes($ret->warmup_duration_in_sec); $this->cardio_device = $ret->cardio_device; $this->setCardioDurationTimes($ret->cardio_duration_in_sec); $this->setRating($ret->rating); $this->setNotes($ret->notes); $this->setExercises(); $this->setNutritions(); $this->setTrainingNutrition(); $this->setSeriesCount($ret->series_count); } } function getURL() { return WWW_ROOT."trainings/training/".$this->id."/view"; } function getId() { return $this->id; } function create() { $this->setCreator(get_logged_in_user()->getId()); $training_datetime = strtotime($this->getTrainingDate()); $training_date_for_mysql = $mysqldate = date( 'Y-m-d', $training_datetime); $q = "INSERT INTO " . DB_PREFIX . "user_trainings (creator, title, training_date, beginning, ending, sleeping_time_in_min, warmup_device, warmup_duration_in_sec, cardio_device, cardio_duration_in_sec, rating, notes, series_count) values (".$this->getCreator().", '".$this->getTitle()."', '".$training_date_for_mysql."', '".$this->getBeginningTime()."', '".$this->getEndingTime()."', '".$this->getSleepingTimeInMinutes()."', '".$this->getWarmupDevice()."', '". $this->getWarmupDurationInSeconds() ."', '".$this->getCardioDevice()."', '". $this->getCardioDurationInSeconds() ."', '".$this->getRating()."', '".$this->getNotes()."', ". $this->getSeriesCount().")"; $uid = query_insert($q); if ($uid) return $uid; return false; } function save() { $training_datetime = strtotime($this->getTrainingDate()); $training_date_for_mysql = $mysqldate = date( 'Y-m-d', $training_datetime); $update_query = "UPDATE " . DB_PREFIX . "user_trainings SET"; $update_query .= " title='".$this->getTitle()."',"; $update_query .= " training_date='".$training_date_for_mysql."',"; $update_query .= " beginning='".$this->getBeginningTime()."',"; $update_query .= " ending='".$this->getEndingTime()."',"; $update_query .= " sleeping_time_in_min=".$this->getSleepingTimeInMinutes().","; $update_query .= " warmup_device='".$this->getWarmupDevice()."',"; $update_query .= " modified=NOW(),"; $update_query .= " warmup_duration_in_sec=".$this->getWarmupDurationInSeconds().","; $update_query .= " cardio_device='".$this->getCardioDevice()."',"; $update_query .= " cardio_duration_in_sec=".$this->getCardioDurationInSeconds().","; $update_query .= " rating='".$this->getRating()."',"; $update_query .= " notes='".$this->getNotes()."',"; $update_query .= " series_count=".$this->getSeriesCount(); $update_query .= " WHERE id=".$this->getId(); $update = query_update($update_query); return $this->getId(); } function saveExercises($id, $exercises) { foreach($exercises as $exercise_data) { if (empty($exercise_data["id"])) { $exercise = new Exercise(); $exercise->setTraining($id); } else { $fitness = new Fitness(); $exercise_id = $exercise_data["id"]; $exercise = $fitness->loadType("exercise", $exercise_id); } $exercise->build($exercise_data); } } function saveNutritions($id, $nutritions) { foreach ($nutritions as $nutrition_data) { if (empty($nutrition_data["id"])) { $nutrition = new Nutrition(); $nutrition->setTraining($id); $nutrition->build($nutrition_data); } else { $fitness = new Fitness(); $nutrition_id = $nutrition_data["id"]; $nutrition = $fitness->loadType("nutrition", $nutrition_id); $nutrition->build($nutrition_data); } } } function build($data) { $this->setTitle($data["title"]); $this->setTrainingDate($data["training_date"]); $this->setBeginningHH($data["beginning_hh"]); $this->setBeginningMM($data["beginning_mm"]); $this->setEndingHH($data["ending_hh"]); $this->setEndingMM($data["ending_mm"]); $this->setSleepingTimeHH($data["sleeping_time_hh"]); $this->setSleepingTimeMM($data["sleeping_time_mm"]); $this->setWarmupDevice($data["warmup_device"]); $this->setWarmupDurationMM($data["warmup_duration_mm"]); $this->setWarmupDurationSS($data["warmup_duration_ss"]); $this->setCardioDevice($data["cardio_device"]); $this->setCardioDurationMM($data["cardio_duration_mm"]); $this->setCardioDurationSS($data["cardio_duration_ss"]); if (isset($data["rating"])) $this->setRating($data["rating"]); $this->setNotes($data["notes"]); $this->setSeriesCount($data["series_count"]); if ($this->getId() > 0) { $this->saveNutritions($this->getId(), $data["nutritions"]); $this->saveExercises($this->getId(), $data["exercises"]); if (count($data["exercises"])==1 && $data["exercises"][1]["exercise"] == "") { $this->setSeriesCount(1); } return $this->save(); } else { $id = $this->create(); $this->saveNutritions($id, $data["nutritions"]); $this->saveExercises($id, $data["exercises"]); return $id; } } function delete() { global $TeKe; if (is_numeric($this->getId()) && $this->getId() > 0) { if ($TeKe->is_logged_in() && ($TeKe->is_admin() || get_logged_in_user()->getId() == $this->getCreator())) { return query("DELETE FROM " . DB_PREFIX . "user_trainings WHERE id=".$this->getId()); } } return false; } function getCreator() { return $this->creator; } function setCreator($creator) { $this->creator = $creator; } function getTitle() { return $this->title; } function setTitle($title) { $this->title = $title; } function getTrainingDate() { if (!$this->training_date) { return date("d.m.Y"); } return $this->training_date; } function setTrainingDate($training_date) { $this->training_date = $training_date; } function getBeginningHH() { return $this->beginning_hh; } function setBeginningHH($beginning_hh) { $this->beginning_hh = $beginning_hh; } function getBeginningMM() { return $this->beginning_mm; } function setBeginningMM($beginning_mm) { $this->beginning_mm = $beginning_mm; } function getBeginningTime() { $beginning_time = "{$this->getBeginningHH()}:{$this->getBeginningMM()}"; return $beginning_time; } function setBeginningTimes($beginning_time) { $beginning_times = explode(":", $beginning_time); $this->setBeginningHH($beginning_times[0]); $this->setBeginningMM($beginning_times[1]); } function getEndingHH() { return $this->ending_hh; } function setEndingHH($ending_hh) { $this->ending_hh = $ending_hh; } function getEndingMM() { return $this->ending_mm; } function setEndingMM($ending_mm) { $this->ending_mm = $ending_mm; } function getEndingTime() { $ending_time = "{$this->getEndingHH()}:{$this->getEndingMM()}"; return $ending_time; } function setEndingTimes($ending_time) { $ending_times = explode(":", $ending_time); $this->setEndingHH($ending_times[0]); $this->setEndingMM($ending_times[1]); } function getSleepingTimeHH() { return $this->sleeping_time_hh; } function setSleepingTimeHH($sleeping_time_hh) { $this->sleeping_time_hh = $sleeping_time_hh; } function getSleepingTimeMM() { return $this->sleeping_time_mm; } function setSleepingTimeMM($sleeping_time_mm) { $this->sleeping_time_mm = $sleeping_time_mm; } function getSleepingTimeInMinutes() { $sleep_in_min = 0; $sleep_in_min += $this->getSleepingTimeHH()*60; $sleep_in_min += $this->getSleepingTimeMM(); return $sleep_in_min; } function setSleepingTimes($sleep_in_min) { $hours = intval(($sleep_in_min / 60) % 60); $minutes = intval($sleep_in_min % 60); $this->setSleepingTimeHH($hours); $this->setSleepingTimeMM($minutes); } function getWarmupDevice() { return $this->warmup_device; } function setWarmupDevice($warmup_device) { $this->warmup_device = $warmup_device; } function getWarmupDurationMM() { return $this->warmup_duration_mm; } function setWarmupDurationMM($warmup_duration_mm) { $this->warmup_duration_mm = $warmup_duration_mm; } function getWarmupDurationSS() { return $this->warmup_duration_ss; } function setWarmupDurationSS($warmup_duration_ss) { $this->warmup_duration_ss = $warmup_duration_ss; } function getWarmupDurationInSeconds() { $duration_in_sec = 0; $duration_in_sec += $this->getWarmupDurationMM()*60; $duration_in_sec += $this->getWarmupDurationSS(); return $duration_in_sec; } function setWarmupDurationTimes($duration_in_sec) { $minutes = intval(($duration_in_sec / 60) % 60); $seconds = intval($duration_in_sec % 60); $this->setWarmupDurationMM($minutes); $this->setWarmupDurationSS($seconds); } function getCardioDevice() { return $this->cardio_device; } function setCardioDevice($cardio_device) { $this->cardio_device = $cardio_device; } function getCardioDurationMM() { return $this->cardio_duration_mm; } function setCardioDurationMM($cardio_duration_mm) { $this->cardio_duration_mm = $cardio_duration_mm; } function getCardioDurationSS() { return $this->cardio_duration_ss; } function setCardioDurationSS($cardio_duration_ss) { $this->cardio_duration_ss = $cardio_duration_ss; } function getCardioDurationInSeconds() { $duration_in_sec = 0; $duration_in_sec += $this->getCardioDurationMM()*60; $duration_in_sec += $this->getCardioDurationSS(); return $duration_in_sec; } function setCardioDurationTimes($duration_in_sec) { $minutes = intval(($duration_in_sec / 60) % 60); $seconds = intval($duration_in_sec % 60); $this->setCardioDurationMM($minutes); $this->setCardioDurationSS($seconds); } function getRating() { return $this->rating; } function setRating($rating) { $this->rating = $rating; } function getNotes() { return $this->notes; } function setNotes($notes) { $this->notes = $notes; } function getSeriesCount() { return $this->series_count; } function setSeriesCount($series_count) { $this->series_count = $series_count; } function getExercises() { if (empty($this->exercises)) { $exercise = new Exercise(); return array("1"=>array("id" => "", "muscle_group"=>"", "exercise"=>"", "series" => $exercise->getSeries())); } return $this->exercises; } function getTrainingExercises() { return $this->exercises; } function setExercises() { $res = query_rows("SELECT * FROM ". DB_PREFIX . "user_exercises WHERE training=".$this->getId()); $exercises = array(); foreach ($res as $exercise) { $Fitness = new Fitness(); $loaded_exercise = $Fitness->loadType("exercise", $exercise->id); $exercise_data = array(); $exercise_data["id"] = $exercise->id; $exercise_data["muscle_group"] = $exercise->muscle_group; $exercise_data["exercise"] = $exercise->exercise; $exercise_data["series"] = $loaded_exercise->getSeries(); $exercises[$exercise->id] = $exercise_data; } $this->exercises = $exercises; } function getNutritions() { if (empty($this->nutritions)) { return array("1"=>array("id"=>"", "food"=>"", "nutrition_time_hh"=>"hh", "nutrition_time_mm"=>"mm", "kcal"=>"", "proteins"=>"", "carbohydrates"=>"", "fat"=>"")); } return $this->nutritions; } function getTrainingNutritions() { return $this->nutritions; } function setNutritions() { $res = query_rows("SELECT * FROM ". DB_PREFIX . "user_nutritions WHERE training=".$this->getId()); //var_dump($res);exit; $nutritions = array(); foreach ($res as $nutrition) { $nutrition_data = array(); $nutrition_data["id"] = $nutrition->id; $nutrition_data["food"] = $nutrition->food; $nutrition_times = explode(":", $nutrition->nutrition_time); $nutrition_data["nutrition_time_hh"] = $nutrition_times[0]; $nutrition_data["nutrition_time_mm"] = $nutrition_times[1]; $nutrition_data["kcal"] = $nutrition->kcal; $nutrition_data["proteins"] = $nutrition->proteins; $nutrition_data["carbohydrates"] = $nutrition->carbohydrates; $nutrition_data["fat"] = $nutrition->fat; $nutritions[$nutrition->id] = $nutrition_data; } $this->nutritions = $nutritions; } function setTrainingNutrition() { $nutritions = $this->getNutritions(); $kcals = 0; $proteins = 0; $carbohydrates = 0; $fat = 0; foreach($nutritions as $nutrition) { $kcals += $nutrition["kcal"]; $proteins += $nutrition["proteins"]; $carbohydrates += $nutrition["carbohydrates"]; $fat += $nutrition["fat"]; } $this->training_kcals = $kcals; $this->training_proteins = $proteins; $this->training_carbohydrates = $carbohydrates; $this->training_fat = $fat; } function getTrainingKCals() { return $this->training_kcals; } function getTrainingProteins() { return $this->training_proteins; } function getTrainingCarbohydrates() { return $this->training_carbohydrates; } function getTrainingFat() { return $this->training_fat; } } ?>