Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.57 KB | None | 0 0
  1. <?php
  2. /**
  3. * @param $item_id
  4. * @param string $item_type
  5. * @param int $user_id
  6. * @param string $reaction
  7. * Adds reaction to database
  8. */
  9. function da_reactions_insert_user_reactions($item_id, $item_type = 'post', $user_id = null, $reaction = 'like') {
  10. if (is_null($user_id)) {
  11. $current_user = wp_get_current_user();
  12. $user_id = $current_user->ID;
  13. }
  14. global $wpdb;
  15. $table_name = $wpdb->prefix . 'da_reactions';
  16. $wpdb->insert($table_name, array(
  17. "resource_id" => $item_id,
  18. "resource_type" => $item_type,
  19. "user_id" => $user_id,
  20. "emotion" => $reaction
  21. ), array("%d", "%s", "%s", "%s"));
  22. }
  23. /**
  24. * @param $item_id
  25. * @param string $item_type
  26. * @param int $user_id
  27. * @param string $reaction
  28. * Adds reaction to database
  29. */
  30. function da_reactions_insert_user_vote($item_id, $item_type = 'post', $user_id = null, $vote = 'si') {
  31. if (is_null($user_id)) {
  32. $current_user = wp_get_current_user();
  33. $user_id = $current_user->ID;
  34. }
  35. global $wpdb;
  36. $table_name = $wpdb->prefix . 'da_polls';
  37. $wpdb->insert($table_name, array(
  38. "resource_id" => $item_id,
  39. "resource_type" => $item_type,
  40. "user_id" => $user_id,
  41. "value" => $vote
  42. ), array("%d", "%s", "%s", "%s"));
  43. }
  44.  
  45. /**
  46. * @param $item_id
  47. * @param string $item_type
  48. * @param int $user_id
  49. * Deletes reaction from database
  50. */
  51. function da_reactions_delete_user_reactions($item_id, $item_type = 'post', $user_id = null) {
  52. if (is_null($user_id)) {
  53. $current_user = wp_get_current_user();
  54. $user_id = $current_user->ID;
  55. }
  56. global $wpdb;
  57. $table_name = $wpdb->prefix . 'da_reactions';
  58. $wpdb->delete($table_name, array(
  59. "resource_id" => $item_id,
  60. "resource_type" => $item_type,
  61. "user_id" => $user_id
  62. ));
  63. }
  64.  
  65. /**
  66. * @param $item_id
  67. * @param string $item_type
  68. * @param int $user_id
  69. * Deletes vote from database
  70. */
  71. function da_reactions_delete_user_votes($item_id, $item_type = 'post', $user_id = null) {
  72. if (is_null($user_id)) {
  73. $current_user = wp_get_current_user();
  74. $user_id = $current_user->ID;
  75. }
  76. global $wpdb;
  77. $table_name = $wpdb->prefix . 'da_polls';
  78. $wpdb->delete($table_name, array(
  79. "resource_id" => $item_id,
  80. "resource_type" => $item_type,
  81. "user_id" => $user_id
  82. ));
  83. }
  84.  
  85. /**
  86. * @param int $item_id
  87. * @param string $item_type
  88. * @param int|null $user_id
  89. * @return array|null|object|void
  90. * Counts reactions for specific item and user
  91. */
  92. function da_reactions_count_user_reaction($item_id, $item_type = 'post', $user_id = null) {
  93. if (is_null($user_id)) {
  94. $current_user = wp_get_current_user();
  95. $user_id = $current_user->ID;
  96. }
  97. global $wpdb;
  98. $table_name = $wpdb->prefix . 'da_reactions';
  99.  
  100. $qry = "SELECT COUNT(*) AS total
  101. FROM $table_name
  102. WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
  103.  
  104. return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
  105. }
  106.  
  107. /**
  108. * @param int $item_id
  109. * @param string $item_type
  110. * @param int|null $user_id
  111. * @return array|null|object|void
  112. * Counts reactions for specific item and user
  113. */
  114. function da_reactions_count_user_total_reaction($user_id = null, $item_type = 'post') {
  115. if (is_null($user_id)) {
  116. $current_user = wp_get_current_user();
  117. $user_id = $current_user->ID;
  118. }
  119. global $wpdb;
  120. $table_name = $wpdb->prefix . 'da_reactions';
  121.  
  122. $qry = "SELECT COUNT(*)
  123. FROM $table_name
  124. WHERE resource_type=%s AND user_id=%s";
  125.  
  126. return $wpdb->get_var($wpdb->prepare($qry, $item_type, $user_id));
  127. }
  128.  
  129. /**
  130. * @param int $item_id
  131. * @param string $item_type
  132. * @param int|null $user_id
  133. * @return array|null|object|void
  134. * Counts votes for specific item and user
  135. */
  136. function da_reactions_count_user_total_poll_votes($user_id = null, $item_type = 'post') {
  137. if (is_null($user_id)) {
  138. $current_user = wp_get_current_user();
  139. $user_id = $current_user->ID;
  140. }
  141. global $wpdb;
  142. $table_name = $wpdb->prefix . 'da_polls';
  143.  
  144. $qry = "SELECT COUNT(*)
  145. FROM $table_name
  146. WHERE resource_type=%s AND user_id=%s";
  147.  
  148. return $wpdb->get_var($wpdb->prepare($qry, $item_type, $user_id));
  149. }
  150.  
  151. /**
  152. * @param int $item_id
  153. * @return array|null|object|void
  154. * Counts all reactions for specific post
  155. */
  156. function da_reactions_count_post_reactions($item_id) {
  157. global $wpdb;
  158. $table_name = $wpdb->prefix . 'da_reactions';
  159.  
  160. $qry = "SELECT COUNT(*) AS total
  161. FROM $table_name
  162. WHERE resource_id=%d AND resource_type=%s";
  163.  
  164. return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post'));
  165. }
  166.  
  167. /**
  168. * @param int $item_id
  169. * @return array|null|object|void
  170. * Counts all reactions for specific comment
  171. */
  172. function da_reactions_count_comments_reactions($item_id) {
  173. global $wpdb;
  174. $table_name = $wpdb->prefix . 'da_reactions';
  175.  
  176. $qry = "SELECT COUNT(*) AS total
  177. FROM $table_name
  178. WHERE resource_id=%d AND resource_type=%s";
  179.  
  180. return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'comment'));
  181. }
  182.  
  183. /**
  184. * @param $item_id
  185. * @param $reaction
  186. * @return array|null|object|void
  187. * Counts specific reaction for specific post
  188. */
  189. function da_reactions_count_post_reaction($item_id, $reaction) {
  190. global $wpdb;
  191. $table_name = $wpdb->prefix . 'da_reactions';
  192.  
  193. $qry = "SELECT COUNT(*) AS total
  194. FROM $table_name
  195. WHERE resource_id=%d AND resource_type=%s AND emotion=%s";
  196.  
  197. return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post', $reaction));
  198. }
  199.  
  200. /**
  201. * @param $item_id
  202. * @param $reaction
  203. * @return array|null|object|void
  204. * Counts specific reaction for specific post
  205. */
  206. function da_reactions_count_post_votes($item_id, $value = null) {
  207. global $wpdb;
  208. $table_name = $wpdb->prefix . 'da_polls';
  209.  
  210. $qry = "SELECT COUNT(*) AS total
  211. FROM $table_name
  212. WHERE resource_id=%d AND resource_type=%s AND value=%s";
  213.  
  214. return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'post', $value));
  215. }
  216.  
  217. /**
  218. * @param $item_id
  219. * @param $reaction
  220. * @return array|null|object|void
  221. * Counts specific reaction for specific post
  222. */
  223. function da_reactions_count_post_total_votes($item_id) {
  224. global $wpdb;
  225. $table_name = $wpdb->prefix . 'da_polls';
  226.  
  227. $qry = "SELECT COUNT(*)
  228. FROM $table_name
  229. WHERE resource_id=%d AND resource_type=%s";
  230.  
  231. return $wpdb->get_var($wpdb->prepare($qry, $item_id, 'post'));
  232. }
  233.  
  234. /**
  235. * @param $item_id
  236. * @param $reaction
  237. * @return array|null|object|void
  238. * Counts specific reaction for specific comment
  239. */
  240. function da_reactions_count_comments_reaction($item_id, $reaction) {
  241. global $wpdb;
  242. $table_name = $wpdb->prefix . 'da_reactions';
  243.  
  244. $qry = "SELECT COUNT(*) AS total
  245. FROM $table_name
  246. WHERE resource_id=%d AND resource_type=%s AND emotion=%s";
  247.  
  248. // var_dump($qry);
  249.  
  250. return $wpdb->get_row($wpdb->prepare($qry, $item_id, 'comment', $reaction));
  251. }
  252.  
  253. /**
  254. * @param int $item_id
  255. * @param string $item_type
  256. * @param int|null $user_id
  257. * @return array|null|object|void
  258. * Get user reaction on specific item
  259. */
  260. function da_reactions_get_user_reaction($item_id, $item_type = 'post', $user_id = null) {
  261. if (is_null($user_id)) {
  262. $current_user = wp_get_current_user();
  263. $user_id = $current_user->ID;
  264. }
  265.  
  266. global $wpdb;
  267. $table_name = $wpdb->prefix . 'da_reactions';
  268.  
  269. $qry = "SELECT *
  270. FROM $table_name
  271. WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
  272.  
  273. return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
  274. }
  275.  
  276. /**
  277. * @param int $item_id
  278. * @param string $item_type
  279. * @param int|null $user_id
  280. * @return array|null|object|void
  281. * Get user vote on specific item
  282. */
  283. function da_reactions_get_user_poll_vote($item_id, $item_type = 'post', $user_id = null) {
  284. if (is_null($user_id)) {
  285. $current_user = wp_get_current_user();
  286. $user_id = $current_user->ID;
  287. }
  288.  
  289. global $wpdb;
  290. $table_name = $wpdb->prefix . 'da_polls';
  291.  
  292. $qry = "SELECT *
  293. FROM $table_name
  294. WHERE resource_id=%d AND resource_type=%s AND user_id=%s";
  295.  
  296. return $wpdb->get_row($wpdb->prepare($qry, $item_id, $item_type, $user_id));
  297. }
  298.  
  299. function da_reactions_get_user_statistics()
  300. {
  301. global $wpdb;
  302. $prefix = $wpdb->prefix;
  303.  
  304. $qry = "select wtot.user_id,
  305. wtot.user_display,
  306. COALESCE(sum(wtot.flg_emotion), 0) as user_comment_reactions
  307. from (
  308.  
  309. select *
  310. from (( select wu.ID as user_id,
  311. wu.display_name as user_display,
  312. COALESCE(wc.comment_ID, 0) as comment_id
  313. from ".$prefix."users wu
  314. left join ".$prefix."comments wc on wc.user_id = wu.ID
  315.  
  316. join ".$prefix."usermeta wum on wum.user_id = wu.ID
  317. WHERE wum.meta_key = 'wp_user_level'
  318. AND wum.meta_value = 0
  319. ) ww
  320.  
  321. left join (select wr.resource_id,
  322. wr.resource_type,
  323. wr.emotion,
  324. 1 as flg_emotion
  325. from ".$prefix."da_reactions wr
  326. where wr.resource_type = %s) rr
  327. on rr.resource_id = ww.comment_id
  328. )
  329. ) wtot
  330. group by wtot.user_id,
  331. wtot.user_display
  332. order by user_comment_reactions desc";
  333.  
  334. return $wpdb->get_results($wpdb->prepare($qry, 'comment'));
  335. }
  336.  
  337. function da_reactions_get_saved_user_statistics() {
  338. return get_option('da_reactions_users_statistics', null);
  339. }
  340.  
  341. function da_reactions_get_saved_posts_statistics() {
  342. return get_option('da_reactions_groups_statistics', null);
  343. }
  344.  
  345. function da_reactions_generate_groups_statistics() {
  346. //da_reactions_badge_check_playmaker(4);
  347. da_reactions_badge_check_medal();
  348. $user_stats = da_reactions_generate_users_statistics();
  349.  
  350. foreach($user_stats as $u) {
  351. if ($u->group->group_id != 1) {
  352. if (!isset($group_stats[$u->group->group_id])) {
  353. $group_stats[$u->group->group_id] = array(
  354. 'group_id' => $u->group->group_id,
  355. 'name' => $u->group->name,
  356. 'description' => $u->group->description,
  357. 'group_users' => 0,
  358. 'group_score' => 0,
  359. 'best_player' => array(
  360. 'id' => '',
  361. 'user_display' => ''
  362. )
  363. );
  364.  
  365. /**
  366. * Wave 2:
  367. * Calcola il best player per ogni gruppo
  368. */
  369. $group_stats[$u->group->group_id]['best_player']['id'] = $u->user_id;
  370. $group_stats[$u->group->group_id]['best_player']['user_display'] = $u->user_display;
  371. }
  372. $group_stats[$u->group->group_id]['group_users'] += 1;
  373. $group_stats[$u->group->group_id]['group_score'] += $u->user_score;
  374. }
  375. }
  376.  
  377. $multiplier = (int)get_option('da_reactions_group_score_multiplier', 1);
  378. $use_average = (bool)get_option('da_reactions_group_use_average', false);
  379. foreach ($group_stats as $i => $g) {
  380. $group_stats[$i]['group_final_score'] = $group_stats[$i]['group_score'] * $multiplier / ($use_average ? $group_stats[$i]['group_users'] : 1);
  381. }
  382.  
  383. return $group_stats;
  384. }
  385.  
  386. function da_reactions_sort_by_user_score($a, $b) {
  387. return $a->user_score < $b->user_score;
  388. }
  389.  
  390. function da_reactions_generate_users_statistics() {
  391. $users_stats = da_reactions_get_user_statistics();
  392.  
  393. for ($i = 0; $i < count($users_stats); $i++) {
  394. $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
  395. $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
  396. $comments_score = da_reactions_count_user_comments($users_stats[$i]->user_id) * 2; /// Ogni commento valido scritto da questo utente * 2
  397. $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
  398. $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
  399. $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
  400.  
  401. $users_stats[$i]->user_score = $poll_answers_score + $reactions_on_post_score + $comments_score + $reactions_on_comment_score + $reaction_received_score + $social_share_score;
  402.  
  403. $users_stats[$i]->group = da_reactions_get_user_group_detail_by_id($users_stats[$i]->user_id);
  404. }
  405.  
  406. usort($users_stats, 'da_reactions_sort_by_user_score');
  407.  
  408. return $users_stats;
  409. }
  410.  
  411. function da_reactions_get_user_count_by_group($group_id) {
  412. global $wpdb;
  413. $prefix = $wpdb->prefix;
  414. $qry = "SELECT count(*) AS total FROM `".$prefix."groups_user_group` WHERE `group_id` = %d";
  415.  
  416. return $wpdb->get_var($wpdb->prepare($qry, $group_id));
  417. }
  418.  
  419. function da_reactions_count_user_comments($user_id) {
  420. global $wpdb;
  421. $count = $wpdb->get_var(
  422. 'SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. '
  423. WHERE user_id = "' . $user_id . '"
  424. AND comment_approved = "1"
  425. AND comment_type IN ("comment", "")'
  426. );
  427.  
  428. return (int)$count;
  429. }
  430.  
  431. function da_reactions_count_user_total_shares($user_id) {
  432. global $wpdb;
  433. $prefix = $wpdb->prefix;
  434.  
  435. $qry = "SELECT count(*) FROM `".$prefix."da_share` WHERE `user_id` = %d";
  436.  
  437. return $wpdb->get_var($wpdb->prepare($qry, $user_id));
  438. }
  439.  
  440. function da_reactions_get_user_shared($item_id, $social_name, $user_id) {
  441. global $wpdb;
  442. $prefix = $wpdb->prefix;
  443. $qry = "SELECT count(*) FROM `".$prefix."da_share` WHERE `resource_id` = %d AND `social` = %s AND `user_id` = %d";
  444.  
  445. return $wpdb->get_var($wpdb->prepare($qry, $item_id, $social_name, $user_id));
  446. }
  447.  
  448. function da_reactions_insert_user_share($item_id, $item_url = '', $user_id = null, $social_name = '') {
  449. if (is_null($user_id)) {
  450. $current_user = wp_get_current_user();
  451. $user_id = $current_user->ID;
  452. }
  453. global $wpdb;
  454. $table_name = $wpdb->prefix . 'da_share';
  455. $wpdb->insert($table_name, array(
  456. "resource_id" => $item_id,
  457. "resource_url" => $item_url,
  458. "social" => $social_name,
  459. "user_id" => $user_id
  460. ), array("%d", "%s", "%s", "%s"));
  461. }
  462.  
  463.  
  464. /**
  465. * @param $user_id
  466. * @return null|string
  467. * Restituisce il gruppo in base all'utente
  468. */
  469. function da_reactions_get_user_group_detail_by_id( $user_id) {
  470. global $wpdb;
  471. $prefix = $wpdb->prefix;
  472.  
  473. $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";
  474.  
  475. return $wpdb->get_results($qry)[0];
  476. }
  477.  
  478. /**
  479. * Restituisce i badge guadagnati da un utente
  480. */
  481. function da_reactions_get_user_badges ( $user_id, $badge_descr = false ) {
  482. global $wpdb;
  483. $prefix = $wpdb->prefix;
  484.  
  485. if ($badge_descr) {
  486. $qry = "select ba.badge_descr
  487. from " . $prefix . "da_badge_user dbu
  488. left join " . $prefix . "da_badge ba
  489. on dbu.badge_id = ba.id
  490. where dbu.user_id = %s and ba.badge_descr like %s";
  491.  
  492. return $wpdb->get_results($wpdb->prepare($qry, $user_id, '%' . $badge_descr . '%'));
  493. }
  494. else {
  495. $qry = "select ba.badge_descr
  496. from " . $prefix . "da_badge_user dbu
  497. left join " . $prefix . "da_badge ba
  498. on dbu.badge_id = ba.id
  499. where dbu.user_id = %s";
  500.  
  501. return $wpdb->get_results($wpdb->prepare($qry, $user_id));
  502. }
  503. }
  504.  
  505. /**
  506. * Restituisce le threshold di uno specifico badge
  507. */
  508. function da_reactions_get_badge_threshold ( $badge_descr ) {
  509. global $wpdb;
  510. $prefix = $wpdb->prefix;
  511.  
  512. $qry = "select ba.badge_threshold
  513. from ".$prefix."da_badge ba
  514. where ba.badge_descr like %s";
  515.  
  516. return $wpdb->get_results($wpdb->prepare($qry, '%'.$badge_descr.'%'));
  517. }
  518.  
  519. /**
  520. * Inserimento badge generico
  521. */
  522. function da_reactions_insert_user_badge ( $badge_id, $user_id ) {
  523. global $wpdb;
  524. $table_name = $wpdb->prefix . 'da_badge_user';
  525. $wpdb->insert($table_name, array(
  526. "badge_id" => $badge_id,
  527. "user_id" => $user_id
  528. ), array("%d", "%s"));
  529. }
  530.  
  531. /**
  532. * Verifica se un utente generico ha guadagnato il playmaker
  533. */
  534. function da_reactions_badge_check_playmaker( $user_id ){
  535.  
  536. $like_name = 'playmaker';
  537. /**
  538. * Step 1: si verifica se ha già qualche badge
  539. */
  540. $playmaker_badges = da_reactions_get_user_badges($user_id, $like_name);
  541.  
  542. if(count($playmaker_badges)) {
  543. return; //Tutti i badge
  544. }
  545.  
  546. /**
  547. * Step 2: Se manca uno dei tre badge aggiorna le statistiche per effettuare verifica
  548. */
  549.  
  550. $users_statistics = da_reactions_generate_users_statistics();
  551. $score = 0;
  552.  
  553. foreach ($users_statistics as $statistic) {
  554. if ($statistic->user_id == $user_id) {
  555. $score = $statistic->user_score;
  556. break;
  557. }
  558. }
  559.  
  560. /**
  561. * Step 3: Se lo score supera le threshold e il record non esiste già inseriamo
  562. */
  563. $score = '77';
  564.  
  565. $thr = da_reactions_get_badge_threshold($like_name);
  566.  
  567. if ($score < $thr[0]->badge_threshold) {
  568. return; //Nessun badge
  569. }
  570. else if ($score > $thr[0]->badge_threshold && $score < $thr[1]->badge_threshold) {
  571. da_reactions_insert_user_badge(2, $user_id); //playmaker_silver
  572. }
  573. else {
  574. da_reactions_insert_user_badge(3, $user_id); //playmaker_gold
  575. }
  576.  
  577. da_reactions_get_user_badges($user_id);
  578. }
  579.  
  580. /**
  581. * Verifica se un utente generico ha guadagnato la guardia
  582. */
  583. function da_reactions_badge_check_guard( $user_id ){
  584.  
  585. }
  586.  
  587. /**
  588. * Verifica se un utente generico ha guadagnato l'ala
  589. */
  590. function da_reactions_badge_check_wing( $user_id ){
  591.  
  592. }
  593.  
  594. /**
  595. * Verifica se un utente generico ha guadagnato il pivot
  596. */
  597. function da_reactions_badge_check_pivot( $user_id ){
  598.  
  599. }
  600.  
  601. /**
  602. * Verifica se un utente generico ha guadagnato la coppa
  603. */
  604. function da_reactions_badge_check_cup( $user_id ){
  605.  
  606. }
  607.  
  608. /**
  609. * Assegna le medaglie
  610. */
  611. function da_reactions_badge_check_medal(){
  612. $users_statistics = da_reactions_generate_users_statistics();
  613.  
  614. $badges_id = [18, 17, 16]; //Medaglia d'oro, argento e bronzo
  615.  
  616. $i = 1;
  617. foreach ($users_statistics as $statistic) {
  618. $podio[] = $statistic->user_id;
  619. if ($i === 3) {
  620. break;
  621. }
  622. else {
  623. $i++;
  624. }
  625. }
  626.  
  627. if (count($podio) > 0) {
  628. global $wpdb;
  629. $prefix = $wpdb->prefix;
  630.  
  631. $qry = "delete
  632. from ".$prefix."da_badge_user
  633. where badge_id = %d OR badge_id = %d OR badge_id = %d";
  634.  
  635. $wpdb->get_row($wpdb->prepare($qry, 16, 17, 18));
  636. }
  637.  
  638. foreach ($badges_id as $key => $b) {
  639. da_reactions_insert_user_badge($b, $podio[$key]);
  640. }
  641. }
  642.  
  643. /**
  644. * Verifica se un utente generico ha guadagnato il pallone
  645. */
  646. function da_reactions_badge_check_ball( $user_id ){
  647.  
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement