Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @param $item_id
- * @param string $item_type
- * @param int $user_id
- * @param string $reaction
- * Adds reaction to database
- */
- function da_reactions_insert_user_reactions($item_id, $item_type = 'post', $user_id = null, $reaction = 'like') {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $wpdb->insert($table_name, array(
- "resource_id" => $item_id,
- "resource_type" => $item_type,
- "user_id" => $user_id,
- "emotion" => $reaction
- ), array("%d", "%s", "%s", "%s"));
- }
- /**
- * @param $item_id
- * @param string $item_type
- * @param int $user_id
- * @param string $reaction
- * Adds reaction to database
- */
- function da_reactions_insert_user_vote($item_id, $item_type = 'post', $user_id = null, $vote = 'si') {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $wpdb->insert($table_name, array(
- "resource_id" => $item_id,
- "resource_type" => $item_type,
- "user_id" => $user_id,
- "value" => $vote
- ), array("%d", "%s", "%s", "%s"));
- }
- /**
- * @param $item_id
- * @param string $item_type
- * @param int $user_id
- * Deletes reaction from database
- */
- function da_reactions_delete_user_reactions($item_id, $item_type = 'post', $user_id = null) {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $wpdb->delete($table_name, array(
- "resource_id" => $item_id,
- "resource_type" => $item_type,
- "user_id" => $user_id
- ));
- }
- /**
- * @param $item_id
- * @param string $item_type
- * @param int $user_id
- * Deletes vote from database
- */
- function da_reactions_delete_user_votes($item_id, $item_type = 'post', $user_id = null) {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $wpdb->delete($table_name, array(
- "resource_id" => $item_id,
- "resource_type" => $item_type,
- "user_id" => $user_id
- ));
- }
- /**
- * @param int $item_id
- * @param string $item_type
- * @param int|null $user_id
- * @return array|null|object|void
- * Counts reactions for specific item and user
- */
- function da_reactions_count_user_reaction($item_id, $item_type = 'post', $user_id = null) {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
- }
- /**
- * @param int $item_id
- * @param string $item_type
- * @param int|null $user_id
- * @return array|null|object|void
- * Counts reactions for specific item and user
- */
- function da_reactions_count_user_total_reaction($user_id = null, $item_type = 'post') {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*)
- FROM $table_name
- WHERE resource_type=%s AND user_id=%s";
- return $wpdb->get_var($wpdb->prepare($qry, $item_type, $user_id));
- }
- /**
- * @param int $item_id
- * @param string $item_type
- * @param int|null $user_id
- * @return array|null|object|void
- * Counts votes for specific item and user
- */
- function da_reactions_count_user_total_poll_votes($user_id = null, $item_type = 'post') {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $qry = "SELECT COUNT(*)
- FROM $table_name
- WHERE resource_type=%s AND user_id=%s";
- return $wpdb->get_var($wpdb->prepare($qry, $item_type, $user_id));
- }
- /**
- * @param int $item_id
- * @return array|null|object|void
- * Counts all reactions for specific post
- */
- function da_reactions_count_post_reactions($item_id) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post'));
- }
- /**
- * @param int $item_id
- * @return array|null|object|void
- * Counts all reactions for specific comment
- */
- function da_reactions_count_comments_reactions($item_id) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'comment'));
- }
- /**
- * @param $item_id
- * @param $reaction
- * @return array|null|object|void
- * Counts specific reaction for specific post
- */
- function da_reactions_count_post_reaction($item_id, $reaction) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND emotion=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post', $reaction));
- }
- /**
- * @param $item_id
- * @param $reaction
- * @return array|null|object|void
- * Counts specific reaction for specific post
- */
- function da_reactions_count_post_votes($item_id, $value = null) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND value=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post', $value));
- }
- /**
- * @param $item_id
- * @param $reaction
- * @return array|null|object|void
- * Counts specific reaction for specific post
- */
- function da_reactions_count_post_total_votes($item_id) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $qry = "SELECT COUNT(*)
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s";
- return $wpdb->get_var($wpdb->prepare($qry, $item_id, 'post'));
- }
- /**
- * @param $item_id
- * @param $reaction
- * @return array|null|object|void
- * Counts specific reaction for specific comment
- */
- function da_reactions_count_comments_reaction($item_id, $reaction) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT COUNT(*) AS total
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND emotion=%s";
- // var_dump($qry);
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'comment', $reaction));
- }
- /**
- * @param int $item_id
- * @param string $item_type
- * @param int|null $user_id
- * @return array|null|object|void
- * Get user reaction on specific item
- */
- function da_reactions_get_user_reaction($item_id, $item_type = 'post', $user_id = null) {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_reactions';
- $qry = "SELECT *
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
- }
- /**
- * @param int $item_id
- * @param string $item_type
- * @param int|null $user_id
- * @return array|null|object|void
- * Get user vote on specific item
- */
- function da_reactions_get_user_poll_vote($item_id, $item_type = 'post', $user_id = null) {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_polls';
- $qry = "SELECT *
- FROM $table_name
- WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
- return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
- }
- function da_reactions_get_user_statistics()
- {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "select wtot.user_id,
- wtot.user_display,
- COALESCE(sum(wtot.flg_emotion), 0) as user_comment_reactions
- from (
- select *
- from (( select wu.ID as user_id,
- wu.display_name as user_display,
- COALESCE(wc.comment_ID, 0) as comment_id
- from ".$prefix."users wu
- left join ".$prefix."comments wc on wc.user_id = wu.ID
- join ".$prefix."usermeta wum on wum.user_id = wu.ID
- WHERE wum.meta_key = 'wp_user_level'
- AND wum.meta_value = 0
- ) ww
- left join (select wr.resource_id,
- wr.resource_type,
- wr.emotion,
- 1 as flg_emotion
- from ".$prefix."da_reactions wr
- where wr.resource_type = %s) rr
- on rr.resource_id = ww.comment_id
- )
- ) wtot
- group by wtot.user_id,
- wtot.user_display
- order by user_comment_reactions desc";
- return $wpdb->get_results($wpdb->prepare($qry, 'comment'));
- }
- function da_reactions_get_saved_user_statistics() {
- return get_option('da_reactions_users_statistics', null);
- }
- function da_reactions_get_saved_posts_statistics() {
- return get_option('da_reactions_groups_statistics', null);
- }
- function da_reactions_generate_groups_statistics() {
- //da_reactions_badge_check_playmaker(4);
- da_reactions_badge_check_medal();
- $user_stats = da_reactions_generate_users_statistics();
- foreach($user_stats as $u) {
- if ($u->group->group_id != 1) {
- if (!isset($group_stats[$u->group->group_id])) {
- $group_stats[$u->group->group_id] = array(
- 'group_id' => $u->group->group_id,
- 'name' => $u->group->name,
- 'description' => $u->group->description,
- 'group_users' => 0,
- 'group_score' => 0,
- 'best_player' => array(
- 'id' => '',
- 'user_display' => ''
- )
- );
- /**
- * Wave 2:
- * Calcola il best player per ogni gruppo
- */
- $group_stats[$u->group->group_id]['best_player']['id'] = $u->user_id;
- $group_stats[$u->group->group_id]['best_player']['user_display'] = $u->user_display;
- }
- $group_stats[$u->group->group_id]['group_users'] += 1;
- $group_stats[$u->group->group_id]['group_score'] += $u->user_score;
- }
- }
- $multiplier = (int)get_option('da_reactions_group_score_multiplier', 1);
- $use_average = (bool)get_option('da_reactions_group_use_average', false);
- foreach ($group_stats as $i => $g) {
- $group_stats[$i]['group_final_score'] = $group_stats[$i]['group_score'] * $multiplier / ($use_average ? $group_stats[$i]['group_users'] : 1);
- }
- return $group_stats;
- }
- function da_reactions_sort_by_user_score($a, $b) {
- return $a->user_score < $b->user_score;
- }
- function da_reactions_generate_users_statistics() {
- $users_stats = da_reactions_get_user_statistics();
- for ($i = 0; $i < count($users_stats); $i++) {
- $poll_answers_score = da_reactions_count_user_total_poll_votes($users_stats[$i]->user_id); /// Ogni volta che questo utente ha risposto a un sondaggio
- $reactions_on_post_score = da_reactions_count_user_total_reaction($users_stats[$i]->user_id) * 0.5; /// Ogni volta che questo utente ha lasciato una reaction su un post * 0.5
- $comments_score = da_reactions_count_user_comments($users_stats[$i]->user_id) * 2; /// Ogni commento valido scritto da questo utente * 2
- $reactions_on_comment_score = da_reactions_count_user_total_reaction($users_stats[$i]->user_id, 'comment') * 0.5; /// Ogni volta che questo utente ha lasciato una reaction su un commento * 0.5
- $reaction_received_score = (int)$users_stats[$i]->user_comment_reactions * 0.5; /// Ogni volta che qualcuno ha lasciato una reaction su un commento di questo utente * 0.5
- $social_share_score = da_reactions_count_user_total_shares($users_stats[$i]->user_id) * 3; /// Ogni volta che questo utente ha condiviso un post sui social * 3
- $users_stats[$i]->user_score = $poll_answers_score + $reactions_on_post_score + $comments_score + $reactions_on_comment_score + $reaction_received_score + $social_share_score;
- $users_stats[$i]->group = da_reactions_get_user_group_detail_by_id($users_stats[$i]->user_id);
- }
- usort($users_stats, 'da_reactions_sort_by_user_score');
- return $users_stats;
- }
- function da_reactions_get_user_count_by_group($group_id) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "SELECT count(*) AS total FROM `".$prefix."groups_user_group` WHERE `group_id` = %d";
- return $wpdb->get_var($wpdb->prepare($qry, $group_id));
- }
- function da_reactions_count_user_comments($user_id) {
- global $wpdb;
- $count = $wpdb->get_var(
- 'SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. '
- WHERE user_id = "' . $user_id . '"
- AND comment_approved = "1"
- AND comment_type IN ("comment", "")'
- );
- return (int)$count;
- }
- function da_reactions_count_user_total_shares($user_id) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "SELECT count(*) FROM `".$prefix."da_share` WHERE `user_id` = %d";
- return $wpdb->get_var($wpdb->prepare($qry, $user_id));
- }
- function da_reactions_get_user_shared($item_id, $social_name, $user_id) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "SELECT count(*) FROM `".$prefix."da_share` WHERE `resource_id` = %d AND `social` = %s AND `user_id` = %d";
- return $wpdb->get_var($wpdb->prepare($qry, $item_id, $social_name, $user_id));
- }
- function da_reactions_insert_user_share($item_id, $item_url = '', $user_id = null, $social_name = '') {
- if (is_null($user_id)) {
- $current_user = wp_get_current_user();
- $user_id = $current_user->ID;
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_share';
- $wpdb->insert($table_name, array(
- "resource_id" => $item_id,
- "resource_url" => $item_url,
- "social" => $social_name,
- "user_id" => $user_id
- ), array("%d", "%s", "%s", "%s"));
- }
- /**
- * @param $user_id
- * @return null|string
- * Restituisce il gruppo in base all'utente
- */
- function da_reactions_get_user_group_detail_by_id( $user_id) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "SELECT * from " . $prefix . "groups_group gg INNER JOIN " . $prefix . "groups_user_group gug ON gg.group_id = gug.group_id WHERE gug.user_id = " . $user_id . " ORDER BY gg.group_id DESC LIMIT 1";
- return $wpdb->get_results($qry)[0];
- }
- /**
- * Restituisce i badge guadagnati da un utente
- */
- function da_reactions_get_user_badges ( $user_id, $badge_descr = false ) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- if ($badge_descr) {
- $qry = "select ba.badge_descr
- from " . $prefix . "da_badge_user dbu
- left join " . $prefix . "da_badge ba
- on dbu.badge_id = ba.id
- where dbu.user_id = %s and ba.badge_descr like %s";
- return $wpdb->get_results($wpdb->prepare($qry, $user_id, '%' . $badge_descr . '%'));
- }
- else {
- $qry = "select ba.badge_descr
- from " . $prefix . "da_badge_user dbu
- left join " . $prefix . "da_badge ba
- on dbu.badge_id = ba.id
- where dbu.user_id = %s";
- return $wpdb->get_results($wpdb->prepare($qry, $user_id));
- }
- }
- /**
- * Restituisce le threshold di uno specifico badge
- */
- function da_reactions_get_badge_threshold ( $badge_descr ) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "select ba.badge_threshold
- from ".$prefix."da_badge ba
- where ba.badge_descr like %s";
- return $wpdb->get_results($wpdb->prepare($qry, '%'.$badge_descr.'%'));
- }
- /**
- * Inserimento badge generico
- */
- function da_reactions_insert_user_badge ( $badge_id, $user_id ) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'da_badge_user';
- $wpdb->insert($table_name, array(
- "badge_id" => $badge_id,
- "user_id" => $user_id
- ), array("%d", "%s"));
- }
- /**
- * Verifica se un utente generico ha guadagnato il playmaker
- */
- function da_reactions_badge_check_playmaker( $user_id ){
- $like_name = 'playmaker';
- /**
- * Step 1: si verifica se ha già qualche badge
- */
- $playmaker_badges = da_reactions_get_user_badges($user_id, $like_name);
- if(count($playmaker_badges)) {
- return; //Tutti i badge
- }
- /**
- * Step 2: Se manca uno dei tre badge aggiorna le statistiche per effettuare verifica
- */
- $users_statistics = da_reactions_generate_users_statistics();
- $score = 0;
- foreach ($users_statistics as $statistic) {
- if ($statistic->user_id == $user_id) {
- $score = $statistic->user_score;
- break;
- }
- }
- /**
- * Step 3: Se lo score supera le threshold e il record non esiste già inseriamo
- */
- $score = '77';
- $thr = da_reactions_get_badge_threshold($like_name);
- if ($score < $thr[0]->badge_threshold) {
- return; //Nessun badge
- }
- else if ($score > $thr[0]->badge_threshold && $score < $thr[1]->badge_threshold) {
- da_reactions_insert_user_badge(2, $user_id); //playmaker_silver
- }
- else {
- da_reactions_insert_user_badge(3, $user_id); //playmaker_gold
- }
- da_reactions_get_user_badges($user_id);
- }
- /**
- * Verifica se un utente generico ha guadagnato la guardia
- */
- function da_reactions_badge_check_guard( $user_id ){
- }
- /**
- * Verifica se un utente generico ha guadagnato l'ala
- */
- function da_reactions_badge_check_wing( $user_id ){
- }
- /**
- * Verifica se un utente generico ha guadagnato il pivot
- */
- function da_reactions_badge_check_pivot( $user_id ){
- }
- /**
- * Verifica se un utente generico ha guadagnato la coppa
- */
- function da_reactions_badge_check_cup( $user_id ){
- }
- /**
- * Assegna le medaglie
- */
- function da_reactions_badge_check_medal(){
- $users_statistics = da_reactions_generate_users_statistics();
- $badges_id = [18, 17, 16]; //Medaglia d'oro, argento e bronzo
- $i = 1;
- foreach ($users_statistics as $statistic) {
- $podio[] = $statistic->user_id;
- if ($i === 3) {
- break;
- }
- else {
- $i++;
- }
- }
- if (count($podio) > 0) {
- global $wpdb;
- $prefix = $wpdb->prefix;
- $qry = "delete
- from ".$prefix."da_badge_user
- where badge_id = %d OR badge_id = %d OR badge_id = %d";
- $wpdb->get_row($wpdb->prepare($qry, 16, 17, 18));
- }
- foreach ($badges_id as $key => $b) {
- da_reactions_insert_user_badge($b, $podio[$key]);
- }
- }
- /**
- * Verifica se un utente generico ha guadagnato il pallone
- */
- function da_reactions_badge_check_ball( $user_id ){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement