--- userpoints_nodelimit.module	Sat Jun 07 12:22:27 2008
+++ userpoints_nodelimit_new.module	Sat Jun 07 12:23:35 2008
@@ -18,3 +18,3 @@
     case 'admin/modules#description':
-      return t('Enables a limit on node creation based on user points');
+      return t('Enables a limit on node creation based on user points.');
   }
@@ -23,20 +23,93 @@
 /**
- * Implementation of hook_form_alter().
+ * Implementation of hook_menu().
  */
-function userpoints_nodelimit_form_alter($form_id, &$form) { 
-  if (isset($form['type']) &&  $form['type']['#value'] .'_node_form' == $form_id) {
-    $node = $form['#node'];      
-    if (!$node->nid && !user_access('administer nodes') && userpoints_nodelimit_check($node->type) == FALSE) {
-      drupal_set_message(t("You don't have enough points to create more nodes of this type."), 'error');
-      //destory the form
-      $form = NULL;
-      //return a simple hidden field so that function doesn't fail
-      $form['dummy'] = array(
-        '#type' => 'hidden',
-        '#default_value' => '',
-      );
-     }
+function userpoints_nodelimit_menu($may_cache) {
+  global $user;
+  
+  $items = array();
+  
+  if ($may_cache) {
+    foreach (node_get_types() as $type) {
+      if (!user_access('administer nodes') && !userpoints_nodelimit_check()) {
+        if (function_exists($type->module .'_form')) {
+          $items[] = array(
+            'path'               => 'node/add/'. str_replace('_', '-', $type->type),
+            'title'              => drupal_ucfirst($type->name),
+            'callback'           => 'drupal_get_form',
+            'callback arguments' => array(
+              'userpoints_nodelimit_creation_forbidden'
+            ),
+            'access'             => node_access('create', $type->type),
+          );
+        }
+      }
+    }
   }
+  
+  return $items;
 }
 
+/**
++ * Implementation of hook_userpoints().
++ */
+function userpoints_nodelimit_userpoints($op, $points = 0, $uid = 0, $operation = NULL, $description = NULL) {
+  $translation = userpoints_translation();
+  
+  switch ($op) {
+    case 'setting':
+      $form = array();
+      
+      $form['userpoints_nodelimit'] = array(
+        '#type'          => 'fieldset',
+        '#title'         => t('Userpoints Nodelimit settings'),
+        '#collapsible'   => TRUE,
+        '#collapsed'     => TRUE,
+      );
+      $form['userpoints_nodelimit']['description'] = array(
+        '#value'         => t(
+          'This module uses the settings of <em>!userpoints_basic</em><br />.
+          It assumes the !points assigned for the creation of a node are negative;
+          this works, i.e., when the user gets !points he can use to contribute by creating nodes until he has sufficient !points.',
+          array('!userpoints_basic' => 'userpoints_basic.module') + $translation
+        ),
+        '#prefix'        => '<div>', 
+        '#suffix'        => '</div>', 
+      );
+      $form['userpoints_nodelimit']['userpoints_nodelimit_points_limit'] = array(
+        '#type'          => 'textfield',
+        '#title'         => t('Node creation !points limit', $translation),
+        '#default_value' => variable_get('userpoints_nodelimit_points_limit', 0),
+        '#size'          => 3,
+        '#maxlength'     => 3,
+        '#description'   => t('The !points required to create a new node.', $translation),
+      );
+      
+      return $form;
+      break;
+   }
+}
+
+/***********************************************************************
+ * Menu callbacks
+ **********************************************************************/
+
+function userpoints_nodelimit_creation_forbidden() {
+  $form = array();
+  
+  $form['userpoints_nodelimit']['warning'] = array(
+    '#value' => '<div class="messages error"><ul><li>'.
+      t(
+        "You don't have enough !points to create more nodes of this type.",
+        userpoints_translation()
+      ) .
+      '</li></ul></div>',
+  );
+  
+  return $form;
+}
+
+/***********************************************************************
+ * Module pubblic functions
+ **********************************************************************/
+
 /** 
@@ -44,15 +117,9 @@
  */
-function userpoints_nodelimit_check($nodetype) {
+function userpoints_nodelimit_check() {
   global $user;
-
+  
   $current_points = userpoints_get_current_points($user->uid);
-
-  $node_points = variable_get(USERPOINTS_POST . $nodetype, 0);
-
-  if (($current_points + $node_points) < 0) {
-    return FALSE;
-  }
-  else {
-    return TRUE;
-  }
+  $limit          = (integer)variable_get('userpoints_nodelimit_points_limit', 0);
+  
+  return ($current_points >= $limit);
 }
