Quote database webapp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dostandingbeta.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?PHP
  2. require_once('inc/database.php');
  3. $users = array();
  4. $quotes = array();
  5. $sql = 'SELECT COUNT(*), user_id FROM quotes GROUP BY user_id';
  6. $res = mysql_query($sql);
  7. $max = 0;
  8. while ($row = mysql_fetch_array($res)) {
  9. if ($row[0] > $max) { $max = $row[0]; }
  10. $quotes[($row[1])] = $row[0];
  11. }
  12. $sql = 'SELECT u.user_id, r.rating_change, r.quote_id, r.user_id AS rater FROM ratings AS r, quotes AS u WHERE u.quote_id = r.quote_id';
  13. $qr = array();
  14. $res = mysql_query($sql);
  15. while ($row = mysql_fetch_array($res)) {
  16. if (!isset($qr[($row['quote_id'])])) {
  17. $qr[($row['quote_id'])] = array('for'=>array(),'against'=>array());
  18. }
  19. $user =& $users[($row['user_id'])];
  20. if ($row['rating_change'] == 0) {
  21. $user += 0;
  22. } elseif ($row['rating_change'] > 0) {
  23. $user += 1;
  24. $qr[($row['quote_id'])]['for'][] = $row['rater'];
  25. } else {
  26. $user -= 1;
  27. $qr[($row['quote_id'])]['against'][] = $row['rater'];
  28. }
  29. }
  30. foreach ($qr as $quote => $rankings) {
  31. $for = count($rankings['for']);
  32. $against = count($rankings['against']);
  33. $total = $for + $against;
  34. if ($for == 0) {
  35. foreach ($rankings['against'] as $uid) {
  36. $users[$uid] += 0.5;
  37. }
  38. } elseif ($against == 0) {
  39. foreach ($rankings['for'] as $uid) {
  40. $users[$uid] += 0.5;
  41. }
  42. } else {
  43. $forscore = $for/$total - 0.5;
  44. $againstscore = $against/$total - 0.5;
  45. foreach ($rankings['for'] as $uid) {
  46. $users[$uid] += $forscore;
  47. }
  48. foreach ($rankings['against'] as $uid) {
  49. $users[$uid] -= $againstscore;
  50. }
  51. }
  52. }
  53. foreach ($users as $uid => $user) {
  54. #echo $uid.' ==&gt; '.$user.' == &gt; '.(10*$user*(1+$quotes[$uid])/pow($max,2)).'<br>';
  55. }
  56. $nusers = array();
  57. $sql = 'SELECT u.user_id, r.rating_change FROM ratings AS r, quotes AS u WHERE u.quote_id = r.quote_id';
  58. $res = mysql_query($sql);
  59. while ($row = mysql_fetch_array($res)) {
  60. $user =& $nusers[($row['user_id'])];
  61. if ($row['rating_change'] == 0) {
  62. $base = 0;
  63. } elseif ($row['rating_change'] > 0) {
  64. $base = 1;
  65. } else {
  66. $base = -1;
  67. }
  68. $base *= ($users[($row['user_id'])]+10)/10;
  69. $user += $base;
  70. }
  71. $sql = 'SELECT COUNT(*) FROM users';
  72. $res = mysql_query($sql);
  73. $row = mysql_fetch_array($res);
  74. $cusers = $row[0];
  75. foreach ($qr as $quote => $rankings) {
  76. $for = count($rankings['for']);
  77. $against = count($rankings['against']);
  78. $total = $for + $against;
  79. if ($for == 0) {
  80. foreach ($rankings['against'] as $uid) {
  81. $nusers[$uid] += 0.5;
  82. }
  83. } elseif ($against == 0) {
  84. foreach ($rankings['for'] as $uid) {
  85. $nusers[$uid] += 0.5;
  86. }
  87. } else {
  88. $forscore = $for/$total - 0.5;
  89. $againstscore = $against/$total - 0.5;
  90. foreach ($rankings['for'] as $uid) {
  91. $nusers[$uid] += $forscore;
  92. }
  93. foreach ($rankings['against'] as $uid) {
  94. $nusers[$uid] -= $againstscore;
  95. }
  96. }
  97. }
  98. #echo '<hr>';
  99. foreach ($nusers as $uid => $user) {
  100. #echo $uid.' ==&gt; '.(10*$user*(1+$quotes[$uid])/(pow($max,2)*$cusers*2)).'<br>';
  101. $nusers[$uid] = (10*$user*(1+$quotes[$uid])/(pow($max,2)*$cusers*2));
  102. $sql = 'UPDATE users SET user_standing = '.$nusers[$uid].' WHERE user_id = '.$uid;
  103. mysql_query($sql);
  104. }
  105. #echo '<hr>';
  106. $quotes = array();
  107. $sql = 'SELECT quote_id, user_id, rating_change FROM ratings';
  108. $res = mysql_query($sql);
  109. while ($row = mysql_fetch_array($res)) {
  110. if (isset($nusers[($row['user_id'])])) {
  111. $user = $nusers[($row['user_id'])];
  112. } else {
  113. $user = 0;
  114. }
  115. if ($row['rating_change'] == 0) {
  116. $base = 0;
  117. } elseif ($row['rating_change'] > 0) {
  118. $base = 1;
  119. } else {
  120. $base = -1;
  121. }
  122. $base *= ($user+10)/10;
  123. $quotes[($row['quote_id'])] += $base;
  124. }
  125. foreach ($quotes as $qid => $score) {
  126. $sql = 'UPDATE quotes SET quote_rating = '.$score.' WHERE quote_id = '.$qid;
  127. mysql_query($sql);
  128. #echo "$qid ==&gt; $score<br>";
  129. }
  130. ?>