config = $config; $this->db = $db; $this->dispatcher = $dispatcher; $this->functions = $functions; $this->log = $log; $this->user = $user; $this->valuator = $valuator; } /** * Distribute a user's points. * * @param int $user_id The user identifier * @param double $points The points gained * @param array $logs The logs array * @param null $user_points The current user's points, if available. Has to be null as 0 is a valid current points value. * @return bool Whether the user points were updated or not * @access public */ public function distribute($user_id, $points, $logs, $user_points = null) { // Calculate the new total for this user $total = $this->total($user_id, $points, $user_points); // If logging was successful if ($this->log->add_multi($logs)) { // Update the points for this user return $this->update_points($total, $user_id); } // Points were not updated, return false (logs were invalid) return false; } /** * Update a user's points. * * @param double $points The user points * @param int $user_id The user identifier * @return bool Whether or not the user's row was updated * @access public */ public function update_points($points, $user_id = 0) { $user_id = $user_id ? $user_id : $this->user->data['user_id']; $sql = 'UPDATE ' . $this->functions->table('users') . ' SET user_points = ' . (double) $points . ' WHERE user_id = ' . (int) $user_id; $this->db->sql_query($sql); $success = (bool) $this->db->sql_affectedrows(); /** * Event to perform additional actions after APS Points have been distributed. * * @event phpbbstudio.aps.update_points * @var int user_id The user identifier * @var double points The user points * @var bool success Whether or not the points were updated * @since 1.0.3 */ $vars = ['user_id', 'points', 'success']; extract($this->dispatcher->trigger_event('phpbbstudio.aps.update_points', compact($vars))); return $success; } /** * Approve log entries and distribute the on-hold points for a certain user. * * @param int $user_id The user identifier * @param array $post_ids The post identifiers * @param null $user_points The current user's points, if available. Has to be null as 0 is a valid current points value. * @return void * @access public */ public function approve($user_id, array $post_ids, $user_points = null) { // Get points gained from the log entries $points = $this->log->get_values($user_id, $post_ids, false); // Equate the points gained to a single value $points = $this->functions->equate_array($points); // Calculate the new total for this user $total = $this->total($user_id, $user_points, $points); $this->db->sql_transaction('begin'); // Approve the log entries $this->log->approve($user_id, $post_ids); // Update the points for this user $this->update_points($total, $user_id); $this->db->sql_transaction('commit'); } /** * Disapprove log entries for a certain user. * * @param int $user_id The user identifier * @param array $post_ids The post identifiers * @return void * @access public */ public function disapprove($user_id, array $post_ids) { // Delete the log entries $this->log->delete([ 'log_approved' => (int) false, 'user_id' => (int) $user_id, 'post_id' => [ 'IN' => (array) $post_ids, ], ]); } /** * Calculate the new total (current points + gained points) for a specific user. * * @param int $user_id The user identifier * @param double $points The user's gained points * @param double $user_points The user's current points * @return double The new total for this user * @access public */ public function total($user_id, $points, $user_points = null) { // If the current user's points is null, get it from the database if ($user_points === null) { $user_points = $this->valuator->user((int) $user_id); } // Calculate the new total for this user $total = $this->functions->equate_points($user_points, $points); // Check total boundaries (not higher than X, not lower than X) $total = $this->functions->boundaries($total); return $total; } }