/** * save rate to poll * * @param <type> $_user_id The user identifier * @param <type> $_post_id The post identifier * @param integer $_rate The rate * * @return boolean ( description_of_the_return_value ) */ public static function rate($_user_id, $_post_id, $_rate) { $is_rate = self::is_rate($_user_id, $_post_id); if ($is_rate) { return true; } if (intval($_rate) < 0) { return false; } if (intval($_rate) > 5) { $_rate = 5; } $args = ['comment_content' => $_rate, 'comment_type' => 'rate', 'comment_status' => 'approved', 'user_id' => $_user_id, 'post_id' => $_post_id]; // insert comments $result = self::insert($args); if ($result) { $total_rate = self::get_total_rate($_post_id); if (!$total_rate) { // insert new value $first_meta = ['total' => ['count' => 1, 'sum' => $_rate, 'avg' => round($_rate / 1, 1)], "rate{$_rate}" => ['count' => 1, 'sum' => $_rate, 'avg' => round($_rate / 1, 1)]]; $first_meta = json_encode($first_meta, JSON_UNESCAPED_UNICODE); $arg = ['post_id' => $_post_id, 'option_cat' => "poll_{$_post_id}", 'option_key' => 'comment', 'option_value' => 'rate', 'option_meta' => $first_meta]; return \lib\db\options::insert($arg); } else { $option_id = $total_rate['id']; $meta = json_decode($total_rate['meta'], true); if (!is_array($meta)) { return false; } foreach ($meta as $key => $value) { if ($key == 'total' || $key == "rate{$_rate}") { $meta[$key]['count'] = $meta[$key]['count'] + 1; $meta[$key]['sum'] = $meta[$key]['sum'] + $_rate; $meta[$key]['avg'] = round(floatval($meta[$key]['sum']) / floatval($meta[$key]['count']), 1); } } if (!isset($meta["rate{$_rate}"])) { $meta["rate{$_rate}"] = ['count' => 1, 'sum' => $_rate, 'avg' => round($_rate / 1, 1)]; } return \lib\db\options::update(['option_meta' => json_encode($meta, JSON_UNESCAPED_UNICODE)], $option_id); } } }
/** * Sets the user language. * * @param <type> $_language The language * * @return <type> ( description_of_the_return_value ) */ public static function set_language($_language, $_options = []) { $default_options = ["update_on_duplicate" => true, "user_id" => self::$user_id]; $_options = array_merge($default_options, $_options); // set user id if ($_options['user_id'] == null) { return false; } $arg = ['user_id' => $_options['user_id'], 'option_cat' => 'user_detail_' . $_options['user_id'], 'option_key' => 'language', 'option_value' => $_language]; $result = \lib\db\options::insert($arg); if (!$result && $_options['update_on_duplicate']) { $where = ['user_id' => $_options['user_id'], 'option_cat' => 'user_detail_' . $_options['user_id'], 'option_key' => 'language']; $result = \lib\db\options::update_on_error($arg, $where); } return $result; }