Thread: Miscellaneous Hacks - DevTracker : Search by usergroup
View Single Post
  #12  
Old 11-29-2007, 11:50 PM
grecostimpy grecostimpy is offline
 
Join Date: Mar 2005
Posts: 93
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Blizzke View Post
Actually, if you like to search by groups you can do it like this:
Code:
if ($vbulletin->GPC['searchuser'])
{
   // username too short
   if (!$vbulletin->GPC['exactname'] AND strlen($vbulletin->GPC['searchuser']) < 3)
   {
      $errors[] = 'searchnametooshort';
   }
   else
   {
      if ( $vbulletin->GPC['searchuser'] == 'devtracker' )
      {
         $aDevGroups = Array ( 1, 2, 3 );
         $q = 'SELECT posts, userid, username FROM `' . TABLE_PREFIX . 'user` where usergroupid IN ( ' . implode ( ',', $aDevGroups ) . ') ';
         foreach ( $aDevGroups as $nDevGroup )
            $q .= ' OR concat( membergroupids,"," ) LIKE "%' . $nDevGroup . ',%"';
      }
      else
      {
         $username = htmlspecialchars_uni($vbulletin->GPC['searchuser']);
         $q = "
            SELECT posts, userid, username
            FROM " . TABLE_PREFIX . "user AS user
            WHERE username " .
               ($vbulletin->GPC['exactname'] ?
                  "= '" . $db->escape_string($username) . "'" :
                  "LIKE('%" . sanitize_word_for_sql($username) . "%')"
               )
         ;
      }

      require_once(DIR . '/includes/functions_bigthree.php');
      $coventry = fetch_coventry();
The green section is what you should add in the search.php file, note that you need to replace the $aDevGroups = Array ( 1, 2, 3 ); with an array containing the id's of the groups you wish to track.

How does it work?
Pretty easy. The query returns all users that either have one of the specified groups as the primary group (usergroupid field) or user with one (or more) of the requested groups as secondary.

Why the concat in the query ?
The secondary grouplist consists of a comma separate list of group id's.
We have to add a comma to the search term because otherwise we could pick up partial groups (ie a user in group 10 will be returned if we are looking for group 1). Solution to that is to always include a comma, so look for "1," instead of simple "1".
This poses a second problem: Should a group be the the last, it will not be detected. (eg "1,2" and we are searching for group "2"). So, the logical thing is to add an extra comma to the end of the membergroupids and then search for the groupid with a comma, which will always return the correct user.

Please note the following:
Obviously this is a "decent query" when used on a large board with a lot of users, so you might want to consider taking the initial query out and caching it's results into a small separate table, that way you would only have to select the rows from that table. You could have a script update that table so that you only have to run that once when you add or remove "devs".
I know this is extremely old, but can someone expand on this? Where would I enter the usergroup id? I changed the numbers in the line $aDevGroups = Array ( 1, 2, 3 ); to my usergroup id's. But it just ended up with me getting a blank white screen anytimeI did ANY type of search.

Help? Thanks!
Reply With Quote
 
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01222 seconds
  • Memory Usage 1,784KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD_SHOWPOST
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • reputationlevel
  • showthread
Included Files:
  • ./showpost.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showpost_start
  • bbcode_fetch_tags
  • bbcode_create
  • postbit_factory
  • showpost_post
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • showpost_complete