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 . "training_plans WHERE id=".$this->id;
$ret = query_row($q);
}
if ( $ret) {
$this->id = $ret->id;
$this->title = $ret->title;
$this->setKeywords($ret->keywords);
$this->setDuration($ret->duration);
$this->setFrequency($ret->frequency);
$this->setNotes($ret->notes);
$this->setStatus($ret->status);
$this->setSecurity($ret->security);
$this->setType($ret->type);
}
}
function getURL() {
return WWW_ROOT."trainingplans/trainingplan/".$this->id."/view";
}
function getId() {
return $this->id;
}
function create() {
$this->setCreator(get_logged_in_user()->getId());
$q = "INSERT INTO " . DB_PREFIX . "training_plans (creator, title, keywords, duration, frequency, notes, status, security, type) values (".$this->getCreator().", '".mysql_real_escape_string($this->getTitle())."', '".mysql_real_escape_string($this->getKeywords())."', '".mysql_real_escape_string($this->getDuration())."', '".$this->getFrequency()."', '".mysql_real_escape_string($this->getNotes())."', '".$this->getStatus()."', '".$this->getSecurity()."', '".$this->getType()."')";
$uid = query_insert($q);
if ($uid) return $uid;
return false;
}
function save() {
$update_query = "UPDATE " . DB_PREFIX . "training_plans SET";
$update_query .= " title='".mysql_real_escape_string($this->getTitle())."',";
$update_query .= " keywords='".mysql_real_escape_string($this->getKeywords())."',";
$update_query .= " duration='".mysql_real_escape_string($this->getDuration())."',";
$update_query .= " frequency='".$this->getFrequency()."',";
$update_query .= " notes='".mysql_real_escape_string($this->getNotes())."',";
$update_query .= " status='".$this->getStatus()."',";
$update_query .= " security='".$this->getSecurity()."',";
$update_query .= " type='".$this->getType()."',";
$update_query .= " modified=NOW()";
$update_query .= " WHERE id=".$this->getId();
$update = query_update($update_query);
return $this->getId();
}
function saveDaysData($id, $days) {
foreach($days as $daynr => $day_data) {
$exercises = $day_data["exercises"];
$pos = 0;
foreach($exercises as $exercise_data) {
if (!$this->isExerciseDataEmpty($exercise_data)) {
$pos = $pos + 1;
if (empty($exercise_data["id"])) {
$exercise = new Trainingplan_exercise();
$exercise->setTrainingplan($id);
$exercise->setDay($daynr);
} else {
$fitness = new Fitness();
$exercise_id = $exercise_data["id"];
$exercise = $fitness->loadType("trainingplan_exercise", $exercise_id);
}
$exercise->setPosition($pos);
$exercise->build($exercise_data);
}
}
}
}
function isExerciseDataEmpty($data) {
if (empty($data["exercise"])) {
return true;
}
return false;
}
function build($data) {
$this->setTitle($data["title"]);
$this->setKeywords($data["keywords"]);
$this->setDuration($data["duration"]);
$this->setFrequency($data["frequency"]);
$this->setNotes($data["notes"]);
if ($data["type"] == "mf") {
global $TeKe;
if ($TeKe->is_admin()) {
$this->setType("mf");
$this->setStatus($data["status"]);
$this->setSecurity($data["security"]);
}
}
if ($this->getId() > 0) {
$this->saveDaysData($this->getId(), $data["days"]);
return $this->save();
} else {
$id = $this->create();
$this->saveDaysData($id, $data["days"]);
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 . "training_plans 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 getKeywords() {
return $this->keywords;
}
function setKeywords($keywords) {
$this->keywords = $keywords;
}
function getDuration() {
return $this->duration;
}
function setDuration($duration) {
$this->duration = $duration;
}
function getFrequency() {
return $this->frequency;
}
function setFrequency($frequency) {
$this->frequency = $frequency;
}
function getNotes() {
return $this->notes;
}
function setNotes($notes) {
$this->notes = $notes;
}
function getStatus() {
return $this->status;
}
function setStatus($status) {
$this->status = $status;
}
function getSecurity() {
return $this->security;
}
function setSecurity($security) {
$this->security = $security;
}
function getType() {
return $this->type;
}
function setType($type) {
$this->type = $type;
}
/*ifunction getExercises() {
if (empty($this->exercises)) {
$exercise = new Exercise();
return array("1"=>array("id" => "", "muscle_group"=>"", "exercise"=>"", "series" => $exercise->getSeries()));
}
return $this->exercises;
}*/
function getInitialisedDayExercises() {
$exercises = array("1"=>array("id" => "", "muscle_group"=>"", "exercise"=>"", "series" =>"", "repetitions"=>"", "break_length"=>"", "break_unit"=>""));
return $exercises;
}
function getDayExercises($daynr) {
if ($this->getId()) {
$res = query_rows("SELECT * FROM ". DB_PREFIX . "training_plan_days WHERE training_plan=".$this->getId()." AND day=".$daynr );
$exercises = array();
foreach ($res as $exercise) {
$Fitness = new Fitness();
$loaded_exercise = $Fitness->loadType("trainingplan_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"] = $exercise->series;
$exercise_data["repetitions"] = $exercise->repetitions;
$exercise_data["break_length"] = $exercise->break_length;
$exercise_data["break_unit"] = $exercise->break_unit;
$exercises[$exercise->id] = $exercise_data;
}
$this->exercises = $exercises;
}
if (empty($exercises)) {
return $this->getInitialisedDayExercises();
}
return $exercises;
}
function addActivatedTrainingplanRecord($beginning_date, $ending_date) {
$beginning = date( 'Y-m-d', $beginning_date);
$ending = date( 'Y-m-d', $ending_date);
$user = get_logged_in_user()->getId();
$q = "INSERT INTO " . DB_PREFIX . "activated_training_plans (creator, training_plan, beginning, ending) values (".$user.", ".$this->getId().", '".$beginning."', '".$ending."')";
$uid = query_insert($q);
if ($uid) return $uid;
return false;
}
function isActiveTrainingplanForUser() {
$user = get_logged_in_user()->getId();
$query = "SELECT * FROM ". DB_PREFIX . "activated_training_plans WHERE CURDATE() >= beginning AND CURDATE() <= ending AND creator=" . $user . " AND training_plan=". $this->getId();
$active_trainingplans = query_rows($query);
if (!empty($active_trainingplans)) {
return true;
}
return false;
}
}
?>