<?php
/*	mod_userrank: Creates different ranks of users based on their number of posts.
	author: Chris Eaton (tridus@hiredgoons.ca)
	version: 1.1
	license: You are free to use, modify, or redistribute this code so long as I am given credit for the original development somewhere.
			 This code comes with absolutly no warranty.

			 It would be nice (but not required) of you to email me if you find this module useful. :-)
			 
   version 1.2: (10/28/2004)
   - added function mod_userrank_profile (Soeren Fuhrmann, sf@golem.de)
*/
if(!defined("PHORUM")) return;

function mod_userrank($messages) {
	GLOBAL $PHORUM;
    foreach ($messages as $messageid => $message) {
        $userposts = 0;
        // figure out just where we have to get the users post count from
        // anonymous
        if ($message["user_id"] == 0) {
            $messages["$messageid"]["mod_userrank"] = $PHORUM["mod_userrank"]["anonymous_rank"];
            continue;
        }
        // user with a custom rank
        elseif (isset($PHORUM["mod_userrank"]["custom_user_ranks"][$message["user_id"]])) {
            $messages["$messageid"]["mod_userrank"] = $PHORUM["mod_userrank"]["custom_user_ranks"][$message["user_id"]];
            continue;
        }
        // 30 day sliding average
        elseif ($PHORUM["mod_userrank"]["enable_sliding_average"]) {
            // number of seconds in 30 days: 2592000
            $result = mysql_query("SELECT count(*) FROM {$PHORUM['message_table']} WHERE user_id = $message[user_id] AND datestamp >= " . (time() - 2592000));
//            echo mysql_error();
            if ($result) {
               $row = mysql_fetch_row($result);
               $userposts = $row[0];
            }
        }
        // users post count
        else {
            $user = phorum_user_get($message["user_id"], false);
            $userposts = $user["posts"];
        }
        if (isset($PHORUM["mod_userrank"]["user_ranks"])) {
            foreach ($PHORUM["mod_userrank"]["user_ranks"] as $posts => $label) {
                if ($posts <= $userposts) {
                    $messages["$messageid"]["mod_userrank"] = $label;
                } else {
                    break;
                }
            }
        }
    }
    return $messages;
}


// function adds userrank to user profiles
// To make use of this function, add {PROFILE->mod_userrank}
// in templates cc_start.tpl and profile.tpl
function mod_userrank_profile($user) { // hooks on profile
   $PHORUM = $GLOBALS['PHORUM'];

   if (isset($PHORUM["mod_userrank"]["user_ranks"])) {
      $user["mod_userrank"] = $PHORUM["mod_userrank"]["anonymous_rank"];
      foreach ($PHORUM["mod_userrank"]["user_ranks"] as $posts => $label) {
         if ($posts <= $user["posts"]) {
            $user["mod_userrank"] = $label;
         } else {
            break;
         }
      }
   }

   return $user;
}

?>