Control modules based on how many articles the user has published

(0 votes, average 0 out of 5)

Your Joomla site may have a number of authors on it who are submitting articles. You would like to be able to display modules based on how many articles the logged-in user has published/submitted, e.g. show a module saying “You have reached GOLD level by submitting over 100 articles!”. The following recipes show how it’s done.

Don’t forget to customise the module IDs that you return from these recipes, to match the module IDs on your site!

Count the number of “published” articles by the user that are active right now:

if ($user->id > 0) {
 $nullDate = $db->Quote($db->getNullDate());
 $my_id = $db->getEscaped((int)$user->id);
 
 $query = "SELECT  count(*) as count"
  . " FROM #__content WHERE created_by = '$my_id' AND state = 1"
  . " AND ( publish_up = " .  $nullDate
  . " OR publish_up <= CURRENT_TIMESTAMP )"
  . " AND ( publish_down = " .  $nullDate
  . " OR publish_down >= CURRENT_TIMESTAMP  )" ;
 $db->setQuery( $query, 0, 1 );
 $row = $db->loadObject();
 
 $count = $row->count;
 if ($count > 100) return 101; /* show if more than 100 articles */
 if ($count > 75) return 102; /* 76-100 articles */
 if ($count > 50) return 103; /* 51-75 articles */
 /* add other count-related rules as necessary */
}

Count the number of articles by the user irrespective of status (i.e. they may not be “published” or may have expired or start date may not yet be reached):

if ($user->id > 0) {
 $my_id = $db->getEscaped((int)$user->id);
 $query = "SELECT  count(*) as count"
  . " FROM #__content WHERE created_by = '$my_id' " ;
 $db->setQuery( $query, 0, 1 );
 $row = $db->loadObject();
 
 $count = $row->count;
 if ($count > 100) return 101; /* show if more than 100 articles */
 if ($count > 75) return 102; /* 76-100 articles */
 if ($count > 50) return 103; /* 51-75 articles */
 /* add other count-related rules as necessary */
}