Limit the posts of a user per x-days


Hi.Recently a client and friend of mine asked me if we could somehow 'limit' the posts of a user per week or per day or per x-days . I told him that this was possible not only for a certain node content type but for all of the content types that a user could use ( or had permissions for ) . My friend told me that he would like to limit the posts only for a certain content type and it would be nice to have the user post a new node every two days . I told him that this is ok and we hang up the phone . So after a while this is how i solve it ;) . First of all you must build a 'custom module' to do this . It's almost 100% guarantee that a 'homemade' drupal module is needed when you are building an awesome website ;) . So how do we build a custom module? For a start we create a folder e.g "limits" inside the sites/all/modules folder of our drupal installation next we add 2 files inside this folder . The and the limits.module file. The file will look like this

; $Id$
name = Content Type Limiter
description = Adding some custom form overrides for limiting the total posts of a node per day etc.
core = 6.x
The limits.module file will look like this
function limits_form_alter(&$form, $form_state, $form_id) { 
if (
$form_id == 'photo_node_form') {
$form['#validate'] = array('_limits_photo_node_form_validate');
_limits_photo_node_form_validate($form,&$form_state) {
$result = db_query_range("SELECT * from {node} WHERE  type='photo' and uid=%d order by created desc",$this_user,0,1);
$data = db_fetch_array($result);
$period=$data['created']+172800; // the period of a post limit
$now=time(); // the time function returns the now unix timestamp
$diff=abs($now-$period); // if diff is larger than 0 then the user can post another node
form_set_error('title','Sorry but you have succeded the maximum limit of 1 photo per 2 days . Please try again in aprox. '.round($diff/3600).' hours');
And that's it . :D What did we do and what do we need to know ? Well for a start if you read once more time the limits.module file then you'll understand that the name of the type that we want to control is named 'photo' . How do we know this ? First of all the $form_id value is photo_node_form plus the sql query 'searches' for nodes with type='photo' . If you dont know or dont remember the name of yours node-type then you can find it either from the phpmyadmin node table of your drupal database ( just look for the field - type of the node table ) or by entering the admin pages and the content type pages clicking edit at the content type you wish to change ( e.g for the 'page' node type the url will be http://localhost/nsp/admin/content/node-type/page and the name of the node-type is located at the 'Type: *' field. How do i allow the user to post once photo per 2 days ? One minute has 60 seconds . I hour has 60 minutes ( 3600 seconds ) . I day ( 24 hours ) has 86400 seconds and two days have ( 48 hours ) 172800 seconds . So basically if i know the latest post date ( created date not changed ) the current day ( current unix timestamp ) in seconds plus the fixed value of 172800 seconds ( 2 days ) i can calculate when did every user posted for the last time in order to allow another post afer a period of 2 days . And that's it :) . I added a small user friendly bit of code at the form-set-error function by notifing the user when he will be able to add a new post ( after how many hours actually ) by calculating : round($diff/3600) .

( Update :03.02.2012) This is somehow ,deprecated.You can now use the stable version of the node limit module.

this is an arrow pointing back to the top of the page