Index: server/pift_server.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue_file_test/server/pift_server.install,v
retrieving revision 1.6
diff -u -p -r1.6 pift_server.install
--- server/pift_server.install	25 Oct 2008 14:14:42 -0000	1.6
+++ server/pift_server.install	2 Nov 2008 14:46:56 -0000
@@ -67,9 +67,6 @@ function pift_server_install() {
 
 function pift_server_uninstall() {
   $vars = array(
-    'pift_server_last_issue_file_processed',
-    'pift_server_last_followup_file_processed',
-    'pift_server_reset_send_queue',
     'pift_server_sites',
     'pift_server_send_frequency',
     'pift_server_last_sent',
@@ -101,4 +98,14 @@ function pift_server_uninstall() {
   }
 
   drupal_set_message(t('The Project issue file test server module was uninstalled successfully.'));
-}
\ No newline at end of file
+}
+
+/**
+ * Remove variables used for cron-based adding of files to {pift_data}.
+ */
+function pift_server_update_1() {
+  variable_del('pift_server_last_issue_file_processed');
+  variable_del('pift_server_last_followup_file_processed');
+
+  return array();
+}
Index: server/pift_server.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue_file_test/server/pift_server.module,v
retrieving revision 1.22
diff -u -p -r1.22 pift_server.module
--- server/pift_server.module	26 Oct 2008 00:51:36 -0000	1.22
+++ server/pift_server.module	2 Nov 2008 14:46:58 -0000
@@ -37,7 +37,6 @@ function pift_server_cron() {
     $frequency = $send * 60;
     // Time to send again?
     if ($time > ($last_sent + $frequency)) {
-      pift_server_add_new_files();
       pift_server_retest_check();
       pift_server_send_file_data();
       // Update last sent time.
@@ -339,51 +338,11 @@ function pift_server_menu($may_cache) {
 }
 
 /**
- * Gets the most recently processed issue and comment ID.
- *
- * @return
- *   An associative array of information, as follows:
- *     'issue'    => The most recently processed issue nid.
- *     'followup' => The most recently processed comment cid.
- */
-function pift_server_get_last() {
-
-  $last_issue = variable_get('pift_server_last_issue_file_processed', 0);
-  $last_followup = variable_get('pift_server_last_followup_file_processed', 0);
-
-  return array('issue' => $last_issue, 'followup' => $last_followup);
-}
-
-/**
- * Gets the newest issue and comment ID.
- *
- * @return
- *   An associative array of information, as follows:
- *     'issue'    => The most recent issue nid.
- *     'followup' => The most recent comment cid.
- */
-function pift_server_get_newest() {
-
-  $newest_issue = db_result(db_query("SELECT MAX(fid) FROM {files}"));
-  // No result, set to zero.
-  $newest_issue = $newest_issue === FALSE ? 0 : $newest_issue;
-
-  $newest_followup_file = db_result(db_query("SELECT MAX(fid) FROM {comment_upload_files}"));
-  // No result, set to zero.
-  $newest_followup_file = $newest_followup_file === FALSE ? 0 : $newest_followup_file;
-
-  return array('issue' => $newest_issue, 'followup' => $newest_followup_file);
-}
-
-/**
  * Sets the last processed issue/followup to the most
  * recently posted issue/followup, sets the last sent
  * time to the current time, and empties the send queue.
  */
 function pift_server_reset_send_queue() {
-  $newest = pift_server_get_newest();
-  variable_set('pift_server_last_issue_file_processed', $newest['issue']);
-  variable_set('pift_server_last_followup_file_processed', $newest['followup']);
   variable_set('pift_server_last_sent', time());
   // Remove all untested files.
   // TODO: Do we really want to do this??
@@ -391,39 +350,29 @@ function pift_server_reset_send_queue() 
 }
 
 /**
- * Adds newly submitted issues/folowups to the send queue.
+ * Adds issue attachments matching PIFT_FILE_REGEX to the send queue.
+ *
+ * @param $files
+ *   An array of file objects.
  */
-function pift_server_add_new_files() {
-  global $base_url;
-
-  // Pull both the last items processed, and the newest items submitted.
-  // This is the range that we can enter into the queue during this run.
-  $last = pift_server_get_last();
-  $newest = pift_server_get_newest();
-  $data['issues'] = array();
-  $data['followups'] = array();
-
-  // Pull all new issue data. Limit the results to 5 times the PIFT_SEND_LIMIT for consistency.
-  $issue_files = db_query_range("SELECT n.nid, n.uid, f.fid, f.filepath FROM {node} n INNER JOIN {files} f ON n.nid = f.nid WHERE n.type = 'project_issue' AND f.fid > %d AND f.fid <= %d AND f.filepath <> '' ORDER BY n.nid, f.fid", $last['issue'], $newest['issue'], 0, PIFT_SEND_LIMIT * 5);
-
-  while ($file = db_fetch_object($issue_files)) {
-    // Update last processed here, in case the server dies during the run.
-    variable_set('pift_server_last_issue_file_processed', $file->fid);
-
+function pift_server_add_issue_files($files) {
+  foreach ($files as $file) {
     // Put the file data into the send queue.
     if (preg_match(PIFT_FILE_REGEX, basename($file->filepath)) && file_exists($file->filepath)) {
       $ftid = db_next_id('{pift_data}_ftid');
       db_query("INSERT INTO {pift_data} (ftid, fid, nid, cid, uid, display_data, status, timestamp) VALUES (%d, %d, %d, %d, %d, '%s', %d, %d)", $ftid, $file->fid, $file->nid, 0, $file->uid, '', PIFT_UNTESTED, 0);
     }
   }
+}
 
-  // Pull all new followup file data. Limit the results to 5 times the PIFT_SEND_LIMIT for consistency.
-  $followup_files = db_query_range("SELECT n.nid, c.cid, c.uid, cu.fid, cu.filepath FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid INNER JOIN {comment_upload_files} cu ON c.cid = cu.cid WHERE n.type = 'project_issue' AND cu.fid > %d AND cu.fid <= %d AND cu.filepath <> '' ORDER BY c.cid, cu.fid", $last['followup'], $newest['followup'], 0, PIFT_SEND_LIMIT * 5);
-
-  while ($file = db_fetch_object($followup_files)) {
-    // Update last processed here, in case the server dies during the run.
-    variable_set('pift_server_last_followup_file_processed', $file->fid);
-
+/**
+ * Add followup attachments matching PIFT_FILE_REGEX to the send queue.
+ *
+ * @param $files
+ *   An array of file objects.
+ */
+function pift_server_add_followup_files($files) {
+  foreach ($files as $files) {
     // Put the file data into the send queue.
     if (preg_match(PIFT_FILE_REGEX, basename($file->filepath)) && file_exists($file->filepath)) {
       $ftid = db_next_id('{pift_data}_ftid');
@@ -433,6 +382,28 @@ function pift_server_add_new_files() {
 }
 
 /**
+ * Get attachments for an issue followup.
+ *
+ * @param cid
+ *  A comment cid.
+ *
+ * @return
+ *  An array of file objects.
+ */
+function pift_server_get_followup_files($cid) {
+  $files = array();
+  $result = db_query("SELECT n.nid, c.cid, c.uid, cu.fid, cu.filepath FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid INNER JOIN {comment_upload_files} cu ON c.cid = cu.cid WHERE cid = %d ORDER BY cu.fid", $cid);
+
+  while ($file = db_fetch_object($result)) {
+    $files[] = $file;
+  }
+
+  return $files;
+}
+
+
+
+/**
  * Marks previously passed files for re-testing, if:
  *   1. The file's last test is older than the retest interval.
  *   2. The file is part of a project that's being tested.
@@ -871,6 +842,11 @@ function pift_server_nodeapi(&$node, $op
         break;
     }
   }
+  // Add attachments for this node to the send queue.
+  if ($op == 'insert') {
+    $files = upload_load($node);
+    pift_server_add_issue_files($files);
+  }
 }
 
 /**
@@ -934,6 +910,11 @@ function pift_server_comment(&$comment, 
         $comment->comment .= $cids[$comment->cid];
       }
       break;
+    case 'insert':
+      // Add attachments to this comment to the send queue.
+      $files = pift_server_get_followup_files($comment['cid']);
+      pift_server_add_followup_files($files);
+      break;
     case 'update':
       // If files were deleted, remove related data in the test results table.
       if (is_array($comment['files'])) {
