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_goals WHERE id=".$this->id;
$ret = query_row($q);
}
if ( $ret) {
$this->id = $ret->id;
$this->setCreator($ret->creator);
$this->setTrainingGoal($ret->training_goal);
$this->setBodyPart($ret->body_part);
$this->setInitialValue($ret->initial_value);
$this->setCurrentValue($ret->current_value);
$this->setFinalGoal($ret->final_goal);
}
}
function getId() {
return $this->id;
}
function create() {
$this->setCreator(get_logged_in_user()->getId());
$q = "INSERT INTO " . DB_PREFIX . "user_goals (creator, training_goal, body_part, initial_value, current_value, final_goal) values (".$this->getCreator().", ".$this->getTrainingGoal().", '".$this->getBodyPart()."','".$this->getInitialValue()."', '".$this->getInitialValue()."', '".$this->getFinalGoal()."')";
$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 updateCurrentValue($current_value) {
$this->setCurrentValue($current_value);
$update_query = "UPDATE " . DB_PREFIX . "user_goals SET current_value='".$this->getCurrentValue()."' WHERE id=".$this->getId();
$update = query_update($update_query);
return $update;
}
function isBuildValuesEmpty($data) {
if (empty($data["initial_value"]) && empty($data["final_goal"])) {
return true;
}
return false;
}
function build($data) {
if ($this->isBuildValuesEmpty($data)) {
return false;
}
$this->setBodyPart($data["body_part"]);
$this->setInitialValue($data["initial_value"]);
$this->setFinalGoal($data["final_goal"]);
if ($this->getId() > 0) {
return $this->save();
} else {
return $this->create();
}
}
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 getTrainingGoal() {
return $this->training_goal;
}
function setTrainingGoal($training_goal) {
$this->training_goal = $training_goal;
}
function getBodyPart() {
return $this->body_part;
}
function setBodyPart($body_part) {
$this->body_part = $body_part;
}
function getInitialValue() {
return $this->initial_value;
}
function setInitialValue($initial_value) {
$this->initial_value = $initial_value;
}
function getCurrentValue() {
return $this->current_value;
}
function setCurrentValue($current_value) {
$this->current_value = $current_value;
}
function getFinalGoal() {
return $this->final_goal;
}
function setFinalGoal($final_goal) {
$this->final_goal = $final_goal;
}
function getPercent() {
$length = $this->getDifference();
if ($length == 0)
return 0;
$accompliced = $this->getAccompliced();
$percent = ($accompliced / $length) * 100;
if ($percent > 100)
return 100;
if ($percent < 0)
return 0;
return $percent;
}
function getDifference() {
if ($this->getInitialValue() > $this->getFinalGoal()) {
return $this->getInitialValue() - $this->getFinalGoal();
}
return $this->getFinalGoal() - $this->getInitialValue();
}
function getAccompliced() {
if ($this->getInitialValue() > $this->getFinalGoal()) {
return $this->getInitialValue() - $this->getCurrentValue();
}
return $this->getCurrentValue() - $this->getInitialValue();
}
}
?>