Index: quota_by_role.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/quota_by_role/quota_by_role.module,v
retrieving revision 1.2.4.3
diff -u -r1.2.4.3 quota_by_role.module
--- quota_by_role.module	17 Jan 2008 11:26:11 -0000	1.2.4.3
+++ quota_by_role.module	23 Feb 2008 17:53:36 -0000
@@ -12,7 +12,7 @@
 
                        <strong>Notes:</strong><ul>
                                <li>When a new role is added, there will be no quota set up for it. You need to come to this settings form to add a new quota.</li>
-                               <li>Quotas are determined based on the weight. Processing starts at the lowest weight and is stopped at the first match. It is a good idea to have groups that admins are in at the lowest weight.</li>
+                               <li>Quotas are determined based on the weight. Processing starts at the lowest weight and is stopped at the group first match. It is a good idea to have groups that admins are in at the lowest weight.</li>
                                <li>User 1 will not be included in quota checks.</li>
 ") .'</p><br />';
   }
@@ -70,7 +70,7 @@
   if (isset($_GET['delete'])) {
     $rid = $_GET['delete'];
     db_query("DELETE FROM {quota_by_role_rules} WHERE qid = $rid");
-    drupal_set_message('Quota deleted.');
+    drupal_set_message(t('Quota deleted.'));
   }  
 
   // Fetch all roles from system
@@ -100,15 +100,15 @@
       case 999999999    : $per = 'forever'; break;
     }
     
-    $img = '<a href="?delete='.$r->qid.'"><img src="/misc/watchdog-error.png" alt="delete quota" title="delete quota" height="18" width="18"></a>';
-    $rows[] = array($r->name, $r->limit_to, 'submissions', $per, $r->weight, $img);
+    $img = '<a href="?delete='.$r->qid.'"><img src="/misc/watchdog-error.png" alt="'.t('delete quota').'" title="'.t('delete quota').'" height="18" width="18"></a>';
+    $rows[] = array($r->name, $r->limit_to, t('submissions'), t($per), $r->weight, $img);
   }
   // We add a hidden element to the form, in order to use the #suffix component to add a table
   // Create table showing the current defined quotas
   $form = array();
   $form['quota_by_role_hidden1'] = array(
     '#type' => 'hidden',
-    '#prefix' => '<h2>Current defined quotas:</h2>',
+    '#prefix' => '<h2>'.t('Current defined quotas:').'</h2>',
     '#suffix' => theme('table', $header, $rows),
   );
 
@@ -119,7 +119,7 @@
   $role = array(
     '#parents' => array('',''),
     '#type' => 'select',
-    '#title'=> 'Select Role',
+    '#title'=> t('Select Role'),
     '#name' => $role_varname, 
     '#options' => $roles,
     '#required' => TRUE,
@@ -129,7 +129,7 @@
   $limit = array(
     '#parents' => array('',''),
     '#type' => 'textfield',
-    '#title' => 'Limit to',
+    '#title' => t('Limit to'),
     '#name' => $limit_varname,
     '#size' => '5',
     '#value' => variable_get($textfield_varname, 10),  
@@ -141,7 +141,7 @@
   $submissions_per = array(
     '#parents' => array('',''),
     '#type' => 'select',
-    '#title' => 'Per',
+    '#title' => t('Per'),
     '#name' => $submissions_per_varname, 
     '#value' => variable_get($select_varname, t('hour')), 
     '#default_value' => variable_get($select_varname, t('hour')),
@@ -159,7 +159,7 @@
   $weight = array(
     '#parents' => array('',''),
     '#type' => 'select',
-    '#title' => 'Weight',
+    '#title' => t('Weight'),
     '#name' => $weight_varname, 
     '#options' => _generate_weights(),
     '#required' => TRUE,
@@ -171,7 +171,7 @@
   // Use a second hidden element in the form to show our second table using the #suffix component 
   $form['quota_by_role_hidden2'] = array(
     '#type' => 'hidden', 
-    '#prefix' => '<br /><hr><h2>Add new quota:</h2>',
+    '#prefix' => '<br /><hr><h2>'.t('Add new quota:').'</h2>',
     '#suffix' => theme('table', array('','','','',''), $rows) . '<hr>',
   );
 
@@ -202,7 +202,7 @@
 
   // Insert newly created quota into the database
   if(db_query("INSERT INTO {quota_by_role_rules} (rid,limit_to,per,weight,created) VALUES ('".$new_quota['select_role']."', '".$new_quota['textfield_limit']."', '".$per."', '".$new_quota['select_weight']."', '".time()."')")) {
-    drupal_set_message('New quota successfully created.');
+    drupal_set_message(t('New quota successfully created.'));
   }
 }
 
@@ -234,51 +234,51 @@
 // This determines if a user may post new content/comments or not
 function _quota_by_role_check_post($user, $table) {
   $overquota = FALSE;
-
-  // Retrieve list of all the roles this user belongs to
-  $user_roles = $user->roles;
+  $breakout = FALSE;
 
   // Retrieve list of all the quota_by_role
   $sql_result_object = db_query("SELECT q.*, r.name FROM {quota_by_role_rules q, role r} WHERE q.rid = r.rid ORDER BY q.weight ASC");
   while($r = db_fetch_object($sql_result_object)) {
-   
-    // Now we need to do a node create count for every role in the quota_by_role array
-    $sql_result_object_2 = _quota_by_role_fetch_count('node', $r->per, $user->uid);
-    $r_2[] = db_fetch_array($sql_result_object_2);  // Count of nodes created
-    $sql_result_object_3 = _quota_by_role_fetch_count('comments', $r->per, $user->uid);
-    $r_3[] = db_fetch_array($sql_result_object_3);  // Count of comments created
-    $total_count = $r_2[0]['cnt'] + $r_3[0]['cnt']; // Add them together
+    
+    // is this user even in the role given?
+    if (in_array($r->name, $user->roles)) {
+      
+      // stop after the first group match.
+      $breakout = TRUE;
+
+      // Now we need to do a node create count for every role in the quota_by_role array
+      $nodes_posted = _quota_by_role_fetch_count('node', $r->per, $user->uid); // Count of nodes created
+      $comments_posted = _quota_by_role_fetch_count('comments', $r->per, $user->uid); // Count of comments created
+      $total_count = $nodes_posted + $comments_posted; // Add them together
        
       // Compare the count of the above query with quota limit
       // If quota exceeded, then print error message and dissallow the post
-      if ($total_count >= $r->limit_to && _quota_by_role_value_in_array($user_roles, $r->name)) { 
+      if ($total_count >= $r->limit_to) { 
 
         switch($r->per) {
-          case 60           : $per = 'minute'; break;
-          case 60*60        : $per = 'hour';   break;
-          case 60*60*24     : $per = 'day';    break;
-          case 60*60*24*7   : $per = 'week';   break;
-          case 60*60*24*30  : $per = 'month';  break;
-          case 999999999    : $per = 'forever'; break;
+          case 60           : $per = t('minute'); break;
+          case 60*60        : $per = t('hour');   break;
+          case 60*60*24     : $per = t('day');    break;
+          case 60*60*24*7   : $per = t('week');   break;
+          case 60*60*24*30  : $per = t('month');  break;
+          case 999999999    : $per = t('forever'); break;
         }
 
-        if ($per == 'forever')
+        if ($per == t('forever'))
           $message = t('Sorry, but you posting quota has been exceeded. You are unable to create any new content.');
         else
-          $message = t('Sorry, but your posting quota has been exceeded. Please try again in 1 $per.');
+          $message = t('Sorry, but your posting quota has been exceeded. Please try again in 1 @per.', array('@per' => $per));
 
         form_set_error('', $message);
         watchdog('quota_by_role', $message);                
 
         $overquota = TRUE;
       }
-      else {
-        // Otherwise the post is allowed
-        // drupal_set_message(t("You are still free to post."));
-      }
- 
+    
+    }
+    
     // No need to continue if we have found a quota limiting this user
-    if ($overquota)
+    if ($breakout)
       break;
   }  
 
@@ -288,20 +288,9 @@
 // Fetches the count of content type
 function _quota_by_role_fetch_count($table, $per, $uid) {
   switch ($table) {
-    case 'node' : return db_query("SELECT COUNT(*) AS cnt FROM {node t} WHERE t.created >= %d AND t.uid = %d", time() - $per, $uid); break;
-    case 'comments' : return db_query("SELECT COUNT(*) AS cnt FROM {comments t} WHERE t.timestamp >= %d AND t.uid = %d", time() - $per, $uid); break;
-  }
-} 
-
-// This function checks whether $to_check exists in $array
-function _quota_by_role_value_in_array($array, $to_check) {
-
-  foreach ($array as $key => $value) {
-    if ($value == $to_check)
-      return true;
+    case 'node' : return db_result(db_query("SELECT COUNT(*) AS cnt FROM {node t} WHERE t.created >= %d AND t.uid = %d", time() - $per, $uid)); break;
+    case 'comments' : return db_result(db_query("SELECT COUNT(*) AS cnt FROM {comments t} WHERE t.timestamp >= %d AND t.uid = %d", time() - $per, $uid)); break;
   }
-
-  return false;
 }
 
 ?>

