2004-06-09 21:42:23 +00:00
< ? php
define ( 'UOTD_THRESHOLD' , 7 );
2004-05-11 22:49:23 +00:00
function uotd_thumbnail ( $profile , $user ) {
2004-07-21 22:19:21 +00:00
if ( $profile -> has_picture ) {
2005-01-19 19:08:46 +00:00
return " <a href=view_profile.php?userid= $user->id ><img border=0 vspace=4 hspace=8 align=left src= " . IMAGE_URL . $user -> id . " _sm.jpg></a> " ;
2004-07-21 22:19:21 +00:00
} else {
return " " ;
}
2004-05-11 22:49:23 +00:00
}
function generate_uotd_page ( $profile , $user ) {
$filename = PROFILE_PATH . " uotd.html " ;
$descriptor = fopen ( $filename , " w " );
if ( $profile -> has_picture ) {
fwrite ( $descriptor , uotd_thumbnail ( $profile , $user ));
}
$x = user_links ( $user );
fwrite ( $descriptor , " The " . PROJECT . " User of the Day is $x " );
fclose ( $descriptor );
}
// return the last UOTD profile, or null
//
function get_current_uotd () {
$result = mysql_query ( " SELECT * FROM profile ORDER BY uotd_time DESC LIMIT 1 " );
$p = null ;
if ( mysql_num_rows ( $result ) > 0 ) {
$p = mysql_fetch_object ( $result );
}
mysql_free_result ( $result );
return $p ;
}
// see if it's time to pick a new UOTD.
// Either way, generate the UOTD page
//
function build_uotd_page () {
2004-12-27 21:53:38 +00:00
echo date ( " F d Y " , time ()) . " \n " ;
2004-05-11 22:49:23 +00:00
$current_uotd = get_current_uotd ();
if ( $current_uotd ) {
$assigned = getdate ( $current_uotd -> uotd_time );
$now = getdate ( time ());
if ( $assigned [ 'mday' ] == $now [ 'mday' ]) {
$user = lookup_user_id ( $current_uotd -> userid );
generate_uotd_page ( $current_uotd , $user );
2004-12-27 21:53:38 +00:00
echo " Already have UOTD for today \n " ;
2004-05-11 22:49:23 +00:00
exit ();
}
}
2005-09-11 08:09:39 +00:00
$result = mysql_query ( " SELECT * FROM profile,user WHERE profile.userid=user.id AND total_credit>0 AND verification=1 AND uotd_time IS NULL ORDER BY RAND() " );
2004-05-11 22:49:23 +00:00
// If the number of approved profiles dips below a threshold,
// email the sys admin every time we pick a new one.
//
if ( $result && mysql_num_rows ( $result ) < UOTD_THRESHOLD ) {
mail ( SYS_ADMIN_EMAIL ,
PROJECT . " : User of the Day pool is running low! " ,
" The pool of approved candidates for User of the Day has " .
2004-06-11 18:50:15 +00:00
" reached your assigned threshold: there are now only " . mysql_num_rows ( $result ) . " approved users. \n \n " .
2004-05-11 22:49:23 +00:00
" To approve more candidates for User of the Day, " .
2004-06-11 18:50:15 +00:00
" go to the " . PROJECT . " administration page and click \" Unrated profile \" "
2004-05-11 22:49:23 +00:00
);
}
if ( $result && mysql_num_rows ( $result ) == 0 ) {
2004-12-27 21:53:38 +00:00
// If all verified profiles have been selected as UOTD,
// reshow the one that was shown least recently.
//
2005-09-11 08:09:39 +00:00
$result = mysql_query ( " SELECT * FROM profile,user WHERE profile.userid=user.id AND total_credit>0 AND verification=1 ORDER BY uotd_time ASC LIMIT 1 " );
2004-05-11 22:49:23 +00:00
}
if ( ! $result || mysql_num_rows ( $result ) == 0 ) {
// No valid users of the day - do something.
2004-12-27 21:53:38 +00:00
echo " No screened users found \n " ;
2004-05-11 22:49:23 +00:00
exit ();
}
$profile = mysql_fetch_object ( $result );
$user = lookup_user_id ( $profile -> userid );
generate_uotd_page ( $profile , $user );
$sql = " UPDATE profile SET uotd_time = " . time () . " WHERE userid= $user->id " ;
mysql_query ( $sql );
mail ( $user -> email_addr ,
" You're the " . PROJECT . " user of the day! " ,
" Congratulations! \n \n You've been chosen as the "
. PROJECT . " user of the day!
Your profile will be featured on the " . PROJECT . " website for the next 24 hours . "
);
2004-12-27 21:53:38 +00:00
echo " Chose user $user->id as UOTD \n " ;
2004-05-11 22:49:23 +00:00
}
?>