A criterion of this type can only be created using an ID. A criterion of this type is only excludable. This is disabled for AdX when it is contained within Operators: ADD, SET.
public static function GetRecommendedValues(User $user) { $age = Carbon::Parse($user->bdate)->diffInYears(); $ageRange = AgeRange::where('min_age', '<=', $age)->where('max_age', '>=', $age)->first(); $gender = $user->gender == 0 ? 'M' : 'F'; $results = RecommendedValue::where('age_range', $ageRange->id)->where('sex', $gender)->select('nutrient_id', 'daily_value', 'upper_limit')->get(); return new RecommendedValues($results->toArray(), $user->daily_calories); }
public function __construct($name) { parent::__construct($name); }
/** * Returns all available presentations * * @return array<string> */ public function getSearchPresentationClass($presentation, $fieldName, $configs = array()) { $event = new OW_Event('base.questions_field_get_label', array('presentation' => $presentation, 'fieldName' => $fieldName, 'configs' => $configs, 'type' => 'edit')); OW::getEventManager()->trigger($event); $label = $event->getData(); $class = null; $event = new OW_Event('base.questions_field_init', array('type' => 'search', 'presentation' => $presentation, 'fieldName' => $fieldName, 'configs' => $configs)); OW::getEventManager()->trigger($event); $class = $event->getData(); if (empty($class)) { switch ($presentation) { case self::QUESTION_PRESENTATION_TEXT: case self::QUESTION_PRESENTATION_TEXTAREA: $class = new TextField($fieldName); break; case self::QUESTION_PRESENTATION_CHECKBOX: $class = new CheckboxField($fieldName); break; case self::QUESTION_PRESENTATION_RADIO: case self::QUESTION_PRESENTATION_SELECT: case self::QUESTION_PRESENTATION_MULTICHECKBOX: $class = new CheckboxGroup($fieldName); break; case self::QUESTION_PRESENTATION_BIRTHDATE: case self::QUESTION_PRESENTATION_AGE: $class = new AgeRange($fieldName); if (!empty($configs) && mb_strlen(trim($configs)) > 0) { $configsList = json_decode($configs, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinYear($value['from']); $class->setMaxYear($value['to']); } } } $class->addValidator(new DateValidator($class->getMinYear(), $class->getMaxYear())); break; case self::QUESTION_PRESENTATION_RANGE: $class = new Range($fieldName); if (empty($this->birthdayConfig)) { $birthday = $this->findQuestionByName("birthdate"); if (!empty($birthday)) { $this->birthdayConfig = $birthday->custom; } } $rangeValidator = new RangeValidator(); if (!empty($this->birthdayConfig) && mb_strlen(trim($this->birthdayConfig)) > 0) { $configsList = json_decode($this->birthdayConfig, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinValue(date("Y") - $value['to']); $class->setMaxValue(date("Y") - $value['from']); $rangeValidator->setMinValue(date("Y") - $value['to']); $rangeValidator->setMaxValue(date("Y") - $value['from']); } } } $class->addValidator($rangeValidator); break; case self::QUESTION_PRESENTATION_DATE: $class = new DateRange($fieldName); if (!empty($configs) && mb_strlen(trim($configs)) > 0) { $configsList = json_decode($configs, true); foreach ($configsList as $name => $value) { if ($name = 'year_range' && isset($value['from']) && isset($value['to'])) { $class->setMinYear($value['from']); $class->setMaxYear($value['to']); } } } $class->addValidator(new DateValidator($class->getMinYear(), $class->getMaxYear())); break; case self::QUESTION_PRESENTATION_URL: $class = new TextField($fieldName); $class->addValidator(new UrlValidator()); break; } $value = $this->getQuestionConfig($configs, 'year_range'); if (!empty($value['from']) && !empty($value['to'])) { $class->setMinYear($value['from']); $class->setMaxYear($value['to']); } } if (!empty($label)) { $class->setLabel($label); } return $class; }
public function getFoodSuggestion() { $age = Carbon::Parse($this->bdate)->diffInYears(); $ageRange = AgeRange::where('min_age', '<=', $age)->where('max_age', '>=', $age)->first(); $gender = $this->gender == 1 ? 'F' : 'M'; if ($this->getFoodHistory()->first() == null) { $foodSuggestion = \DB::select(\DB::raw(' SELECT foods.*, SUM(fn.amount_in_food / rem_nutr.remaining_val) / (2000 / foods.calories) as score FROM foods INNER JOIN food_nutrient AS fn ON foods.id = fn.food_id INNER JOIN (SELECT users.id, daily_value AS remaining_val, nutrient_id FROM users INNER JOIN recommended_values WHERE users.id = ' . $this->id . ' AND recommended_values.age_range = ' . $ageRange->id . ' AND recommended_values.sex = \'' . $gender . '\' GROUP BY nutrient_id) AS rem_nutr ON rem_nutr.nutrient_id = fn.nutrient_id and foods.id not in (select food_id from food_restriction as fr inner join restriction_user as ru on ru.restriction_id = fr.restriction_id where user_id = ' . $this->id . ') GROUP BY foods.id order by score DESC, foods.id, fn.nutrient_id;')); } else { $foodSuggestion = \DB::select(\DB::raw(' SELECT foods.*, SUM(fn.amount_in_food / rem_nutr.remaining_val) / (2000 / foods.calories) as score FROM foods INNER JOIN food_nutrient AS fn ON foods.id = fn.food_id INNER JOIN (SELECT users.id, fn.nutrient_id, nutr.daily_value - SUM((quantity * amount_in_food / 100)) AS remaining_val FROM users INNER JOIN user_history AS uh ON users.id = uh.user_id INNER JOIN food_nutrient AS fn ON fn.food_id = uh.food_id INNER JOIN (SELECT nutrient_id, daily_value FROM recommended_values WHERE age_range = ' . $ageRange->id . ' AND sex = \'' . $gender . '\') AS nutr ON nutr.nutrient_id = fn.nutrient_id WHERE timestamp > DATE_SUB(NOW(), INTERVAL 24 HOUR) AND users.id = ' . $this->id . ' GROUP BY nutrient_id) AS rem_nutr ON rem_nutr.nutrient_id = fn.nutrient_id and foods.id not in (select food_id from food_restriction as fr inner join restriction_user as ru on ru.restriction_id = fr.restriction_id where user_id = ' . $this->id . ') GROUP BY foods.id order by score DESC, foods.id, fn.nutrient_id;')); } $random = rand(0, 200); $foodReturn = Food::where('name', $foodSuggestion[$random]->name)->first(); return $foodReturn; }