/** * Used to determine if difference between positive and negative ratings changes, and makes appropriate changes to the rating given to user. * * @param $to_user_id * @param $from_user_id * @param $current_score * @param $new_score * @param $action */ private function setUserRating($to_user_id, $from_user_id, $current_score, $new_score, $sql_ary, $action) { $to_user_id = (int) $to_user_id; $from_user_id = (int) $from_user_id; $current_score = (int) $current_score; $new_score = (int) $new_score; $difference = $this->countFeedbackType($to_user_id, $from_user_id); $insert = true; switch ($action) { case 'add': $new_difference = $difference + $new_score; $insert = $this->db->sql_query('INSERT INTO ' . $this->tables['feedback'] . ' ' . $this->db->sql_build_array('INSERT', $sql_ary)); $next_id = $this->db->sql_nextid(); break; case 'revert': $new_difference = $difference + $current_score; break; case 'edit': $new_difference = $difference - $current_score + $new_score; break; case 'delete': $new_difference = $difference - $current_score; break; } if ($this->normalize($difference) != $this->normalize($new_difference) && $insert) { $this->removeUserRating($to_user_id, $this->normalize($difference)); $this->giveUserRating($to_user_id, $this->normalize($new_difference)); } else { if ($this->get_users_feedback_count($to_user_id) == 1 && $this->normalize($new_difference) == 0 && $insert) { $this->giveUserRating($to_user_id, self::RATE_NEUTRAL); } } return $next_id; }
/** * {@inheritDoc} */ public function add($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = array()) { if (!$this->is_enabled($mode)) { return false; } if ($log_time === false) { $log_time = time(); } $sql_ary = array('user_id' => !empty($user_id) ? $user_id : ANONYMOUS, 'log_ip' => !empty($log_ip) ? $log_ip : '', 'log_time' => $log_time, 'log_operation' => $log_operation); switch ($mode) { case 'admin': $sql_ary += array('log_type' => LOG_ADMIN, 'log_data' => !empty($additional_data) ? serialize($additional_data) : ''); break; case 'mod': $forum_id = isset($additional_data['forum_id']) ? (int) $additional_data['forum_id'] : 0; unset($additional_data['forum_id']); $topic_id = isset($additional_data['topic_id']) ? (int) $additional_data['topic_id'] : 0; unset($additional_data['topic_id']); $post_id = isset($additional_data['post_id']) ? (int) $additional_data['post_id'] : 0; unset($additional_data['post_id']); $sql_ary += array('log_type' => LOG_MOD, 'forum_id' => $forum_id, 'topic_id' => $topic_id, 'post_id' => $post_id, 'log_data' => !empty($additional_data) ? serialize($additional_data) : ''); break; case 'user': $reportee_id = (int) $additional_data['reportee_id']; unset($additional_data['reportee_id']); $sql_ary += array('log_type' => LOG_USERS, 'reportee_id' => $reportee_id, 'log_data' => !empty($additional_data) ? serialize($additional_data) : ''); break; case 'critical': $sql_ary += array('log_type' => LOG_CRITICAL, 'log_data' => !empty($additional_data) ? serialize($additional_data) : ''); break; } /** * Allows to modify log data before we add it to the database * * NOTE: if sql_ary does not contain a log_type value, the entry will * not be stored in the database. So ensure to set it, if needed. * * @event core.add_log * @var string mode Mode of the entry we log * @var int user_id ID of the user who triggered the log * @var string log_ip IP of the user who triggered the log * @var string log_operation Language key of the log operation * @var int log_time Timestamp, when the log was added * @var array additional_data Array with additional log data * @var array sql_ary Array with log data we insert into the * database. If sql_ary[log_type] is not set, * we won't add the entry to the database. * @since 3.1.0-a1 */ $vars = array('mode', 'user_id', 'log_ip', 'log_operation', 'log_time', 'additional_data', 'sql_ary'); extract($this->dispatcher->trigger_event('core.add_log', compact($vars))); // We didn't find a log_type, so we don't save it in the database. if (!isset($sql_ary['log_type'])) { return false; } $this->db->sql_query('INSERT INTO ' . $this->log_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary)); return $this->db->sql_nextid(); }
/** * The main function for recording reputation vote. * * @param array $data Reputation data * @access public * @return null */ public function store_reputation($data) { $data['reputation_time'] = time(); $fields = array('user_id_from' => 'integer', 'user_id_to' => 'integer', 'reputation_time' => 'integer', 'reputation_type' => 'string', 'reputation_item_id' => 'integer', 'reputation_points' => 'integer', 'reputation_comment' => 'string'); foreach ($fields as $field => $type) { if (!isset($data[$field])) { throw new \pico\reputation\exception\invalid_argument(array($field, 'FIELD_MISSING')); } $value = $data[$field]; settype($value, $type); $data[$field] = $value; } // Get reputation type id $data['reputation_type_id'] = $this->get_reputation_type_id($data['reputation_type']); // Unset reputation type - it is not stored in DB unset($data['reputation_type']); $validate_unsigned = array('user_id_from', 'user_id_to', 'reputation_time', 'reputation_type_id', 'reputation_item_id'); foreach ($validate_unsigned as $field) { if ($data[$field] < 0) { throw new \pico\reputation\exception\out_of_bounds($field); } } // Save reputation vote $sql = 'INSERT INTO ' . $this->reputations_table . ' ' . $this->db->sql_build_array('INSERT', $data); $this->db->sql_query($sql); unset($this->reputation_id); $this->reputation_id = $this->db->sql_nextid(); // Update post reputation if ($data['reputation_type_id'] == $this->get_reputation_type_id('post')) { $sql = 'UPDATE ' . POSTS_TABLE . "\n\t\t\t\tSET post_reputation = post_reputation + {$data['reputation_points']}\n\t\t\t\tWHERE post_id = {$data['reputation_item_id']}"; $this->db->sql_query($sql); } // Update user reputation $sql = 'UPDATE ' . USERS_TABLE . "\n\t\t\tSET user_reputation = user_reputation + {$data['reputation_points']}\n\t\t\tWHERE user_id = {$data['user_id_to']}"; $this->db->sql_query($sql); // Check max/min user points if ($this->config['rs_max_point'] || $this->config['rs_min_point']) { $this->check_max_min($data['user_id_to']); } }