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.

dostandinggamma.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?PHP
  2. require_once('inc/database.php');
  3. class quote {
  4. public $id;
  5. public $owner;
  6. public $good = array();
  7. public $neutral = array();
  8. public $bad = array();
  9. }
  10. $quotes = array();
  11. $users = array();
  12. // Read the quotes into an array
  13. $sql = 'SELECT quote_id, user_id FROM quotes';
  14. $res = mysql_query($sql);
  15. while ($row = mysql_fetch_assoc($res)) {
  16. if (!isset($users[($row['user_id'])])) {
  17. $users[($row['user_id'])] = 0;
  18. }
  19. $quotes[($row['quote_id'])] = new quote;
  20. $quotes[($row['quote_id'])]->id = $row['quote_id'];
  21. $quotes[($row['quote_id'])]->owner = $row['user_id'];
  22. }
  23. // And read the ratings in
  24. $sql = 'SELECT user_id, quote_id, rating_change FROM ratings';
  25. $res = mysql_query($sql);
  26. while ($row = mysql_fetch_assoc($res)) {
  27. $q =& $quotes[($row['quote_id'])];
  28. $u = $row['user_id'];
  29. if (!isset($users[$u])) { $users[$u] = 0; }
  30. if ($row['rating_change'] > 0) {
  31. $q->good[] = $u;
  32. } elseif ($row['rating_change'] < 0) {
  33. $q->bad[] = $u;
  34. } else {
  35. $q->neutral[] = $u;
  36. }
  37. }
  38. define('USERS', count($users));
  39. define('QUOTES', count($quotes));
  40. // First pass: standings based on rating agreement
  41. foreach ($quotes as $quote) {
  42. $num = count($quote->good) + count($quote->bad);
  43. if ($num == 0) { continue; }
  44. $off = (1/QUOTES) * ($num/USERS);
  45. $bad = $bad / $num;
  46. foreach ($quote->bad as $uid) {
  47. $users[$uid] += $off * $bad;
  48. }
  49. foreach ($quote->good as $uid) {
  50. $users[$uid] += $off * (1 - $bad);
  51. }
  52. foreach ($quote->neutral as $uid) {
  53. $users[$uid] += $off * 0.5;
  54. }
  55. }
  56. $fstanding = 0;
  57. foreach ($users as $stand) { $fstanding += $stand; }
  58. define('FSTANDING', $fstanding);
  59. // Second pass: quote ratings
  60. foreach ($quotes as $quote) {
  61. $score = 0;
  62. $off = 10/FSTANDING;
  63. foreach ($quote->bad as $uid) {
  64. $score -= $off * $users[$uid];
  65. }
  66. foreach ($quote->good as $uid) {
  67. $score += $off * $users[$uid];
  68. }
  69. echo "Quote: ".$quote->id.' scores '.$score.'<br>';
  70. }
  71. echo "<hr>";
  72. // Third pass: user standings
  73. ?>