PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/boinc/boinc/html/inc/uotd.inc

http://decs.googlecode.com/
PHP | 181 lines | 138 code | 17 blank | 26 comment | 29 complexity | 489e2f4f78db03c2f0f7bbafceeb3184 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. <?php
  2. require_once('../inc/email.inc');
  3. require_once('../inc/profile.inc');
  4. define('UOTD_THRESHOLD', 7);
  5. // email sysadmin if # of UOTD candidates falls below this
  6. function uotd_thumbnail($profile, $user) {
  7. if ($profile->has_picture) {
  8. return "<a href=".URL_BASE."view_profile.php?userid=$user->id><img border=0 vspace=4 hspace=8 align=left src=".URL_BASE.IMAGE_URL.$user->id."_sm.jpg></a>";
  9. } else {
  10. return "";
  11. }
  12. }
  13. // show UOTD in a small box
  14. //
  15. function show_uotd() {
  16. $profile = get_current_uotd();
  17. if ($profile) {
  18. $user = lookup_user_id($profile->userid);
  19. echo uotd_thumbnail($profile, $user);
  20. echo user_links($user)."<br>";
  21. echo sub_sentence(strip_tags(output_transform($profile->response1)), ' ', 150, true);
  22. }
  23. }
  24. // return the last UOTD profile, or null
  25. //
  26. function get_current_uotd() {
  27. $profiles = BoincProfile::enum(null, "ORDER BY uotd_time DESC LIMIT 1");
  28. if (sizeof($profiles)) {
  29. return $profiles[0];
  30. }
  31. return null;
  32. }
  33. // Select a (possibly new) UOTD
  34. //
  35. function select_uotd() {
  36. echo date("F d Y", time()).": Starting\n";
  37. $current_uotd = get_current_uotd();
  38. if ($current_uotd) {
  39. $assigned = getdate($current_uotd->uotd_time);
  40. $now = getdate(time());
  41. if ($assigned['mday'] == $now['mday']) {
  42. $user = lookup_user_id($current_uotd->userid);
  43. echo "Already have UOTD for today\n";
  44. generate_uotd_gadget($current_uotd, $user);
  45. exit();
  46. }
  47. }
  48. // get a list of profiles that have been 'approved' for UOTD,
  49. // using a project-specific query if supplied in project.inc
  50. //
  51. if (function_exists('uotd_candidates_query')) {
  52. $query = uotd_candidates_query();
  53. } else {
  54. $query = default_uotd_candidates_query();
  55. }
  56. $result = mysql_query($query);
  57. // If the number of approved profiles dips below a threshold,
  58. // email the sys admin every time we pick a new one.
  59. //
  60. if ($result && mysql_num_rows($result) < UOTD_THRESHOLD) {
  61. $u = new BoincUser;
  62. $u->email_addr = UOTD_ADMIN_EMAIL;
  63. $u->name = "UOTD admin";
  64. send_email($u,
  65. PROJECT . ": User of the Day pool is running low!",
  66. "The pool of approved candidates for User of the Day has".
  67. " reached your assigned threshold: there are now only " . mysql_num_rows($result) . " approved users.\n\n".
  68. "To approve more candidates for User of the Day,".
  69. " go to the " . PROJECT . " administration page and click \"Unrated profile\""
  70. );
  71. }
  72. if ($result && mysql_num_rows($result) == 0) {
  73. // If all verified profiles have been selected as UOTD,
  74. // reshow the one that was shown least recently.
  75. //
  76. $result = mysql_query("SELECT * FROM profile,user WHERE profile.userid=user.id AND verification=1 ORDER BY uotd_time ASC LIMIT 1");
  77. }
  78. if (!$result || mysql_num_rows($result) == 0) {
  79. // No valid users of the day - do something.
  80. echo "No screened users found\n";
  81. exit();
  82. }
  83. $profile = mysql_fetch_object($result);
  84. $user = lookup_user_id($profile->userid);
  85. $sql = "UPDATE profile SET uotd_time = ".time()." WHERE userid=$user->id";
  86. mysql_query($sql);
  87. send_email($user,
  88. "You're the " . PROJECT . " user of the day!",
  89. "Congratulations!\n\nYou've been chosen as the "
  90. . PROJECT . " user of the day!
  91. Your profile will be featured on the " . PROJECT . " website for the next 24 hours."
  92. );
  93. echo "Chose user $user->id as UOTD\n";
  94. $profile->uotd_time = time();
  95. generate_uotd_gadget($profile, $user);
  96. }
  97. // This is the default policy for choosing the UOTD on any BOINC project.
  98. // To override this with your own policy, create a similar function in
  99. // your own project.inc called uotd_candidates_query()
  100. //
  101. function default_uotd_candidates_query(){
  102. if (profile_screening()) {
  103. $query = "SELECT * FROM profile,user WHERE profile.userid=user.id ";
  104. $query .= " AND verification=1 ";
  105. $query .= " AND expavg_credit>1 ";
  106. $query .= " AND uotd_time IS NULL ";
  107. $query .= "ORDER BY RAND()";
  108. } else {
  109. $query = "SELECT * FROM profile,user WHERE profile.userid=user.id ";
  110. $query .= "AND expavg_credit>1 ";
  111. $query .= "AND uotd_time IS NULL ";
  112. $query .= "ORDER BY RAND()";
  113. }
  114. return $query;
  115. }
  116. // get a list of profiles that have been 'approved' for UOTD,
  117. // using a project-specific query if supplied in project.inc
  118. //
  119. function count_uotd_candidates(){
  120. $n = -1; // negative value returned on error
  121. if (function_exists('uotd_candidates_query')) {
  122. $query = uotd_candidates_query();
  123. } else {
  124. $query = default_uotd_candidates_query();
  125. }
  126. $result = mysql_query($query);
  127. if($result) {
  128. $n = mysql_num_rows($result);
  129. }
  130. mysql_free_result($result);
  131. return $n;
  132. }
  133. // iGoogle gadget - generate the gadget content page
  134. //
  135. function generate_uotd_gadget($profile, $user) {
  136. $x = "<font size='2'>\n";
  137. $gadget = PROFILE_PATH."uotd_gadget.html";
  138. if( $h = fopen($gadget, "w") ){
  139. $age = time()-$profile->uotd_time;
  140. echo "age: $age";
  141. if($age <= 86400+3600) { // allow for slop
  142. $x .= uotd_thumbnail($profile, $user);
  143. $x .= user_links($user);
  144. $x .= "&nbsp;&nbsp;".
  145. sub_sentence(strip_tags(output_transform($profile->response1)), ' ',
  146. 250, true);
  147. }
  148. else {
  149. $x .= "<font color='fuscia'>
  150. There is no User of the Day today.
  151. Only volunteers who have created a Profile
  152. (with a picture), and have recent credit,
  153. are eligible to be chosen as User of the Day.
  154. We have run out of these, so there isn't a
  155. User of the Day.
  156. </font>";
  157. }
  158. $x .= "\n</font>\n";
  159. fwrite($h, $x);
  160. fclose($h);
  161. }
  162. }
  163. ?>