Index: includes/cron.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/includes/cron.inc,v
retrieving revision 1.5
diff -u -F^f -u -p -r1.5 cron.inc
--- includes/cron.inc	18 Feb 2009 17:29:19 -0000	1.5
+++ includes/cron.inc	21 Mar 2009 17:51:16 -0000
@@ -47,7 +47,7 @@ function project_issue_auto_close() {
   $comment = theme('project_issue_auto_close_message', $auto_close_days);
   $result = db_query('SELECT pi.nid FROM {project_issues} pi INNER JOIN {node} n ON n.nid = pi.nid WHERE pi.sid = %d AND n.changed < %d', PROJECT_ISSUE_STATE_FIXED, time() - $seconds);
   while ($issue = db_fetch_object($result)) {
-    project_issue_add_followup(array(
+    project_issue_add_auto_followup(array(
       'nid' => $issue->nid,
       'sid' => PROJECT_ISSUE_STATE_CLOSED,
       'comment' => $comment,
Index: project_issue.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.module,v
retrieving revision 1.148
diff -u -F^f -u -p -r1.148 project_issue.module
--- project_issue.module	20 Mar 2009 02:33:44 -0000	1.148
+++ project_issue.module	21 Mar 2009 17:51:16 -0000
@@ -627,14 +627,18 @@ function theme_project_issue_auto_close_
 }
 
 /**
- * Add a followup to a project issue.
+ * Add a followup to a project issue using the auto-followup user.
  *
  * @param $changes
  *   An associative array specifying what should change in the issue. Every key
  *   corresponds to a database field and the value is what it should be changed
- *   to. nid is a required key and it specifies the issue being changed.
- *   'comment' is also a required key and it contains the text of the followup
- *   changing the issue.
+ *   to. Required keys are:
+ *     - nid: Specifies the issue being changed.
+ *     - comment: Contains the text of the followup changing the issue.
+ *
+ *   uid and name are optional keys -- if not specified then the values from
+ *   'Auto-followup user' in the project issue settings are used.
+ *
  *   You can specify the following fields of the comment table: uid, subject,
  *   hostname, timestamp, score, status, format, thread, users, name, mail,
  *   homepage. You can also specify the following fields from project_issues
@@ -649,73 +653,101 @@ function theme_project_issue_auto_close_
  *      'comment' => t('This issue was automatically closed after 2 weeks of no activity.'),
  *     );
  */
-function project_issue_add_followup($changes) {
-  global $user;
-
+function project_issue_add_auto_followup($changes) {
+  // If a user exists for followups, load them into changes and proceed.
   if ($auto_user = _project_issue_followup_get_user()) {
-    // If a user exists for followups, load them into
-    // the global user object temporarily. We use session_save_session()
-    // to provide safe user impersonation.
-    $original_user = $user;
-    session_save_session(FALSE);
-    $user = $auto_user;
-
-    $result = db_query('SELECT pi.nid, pi.rid, pi.component, pi.category, pi.priority, pi.assigned, pi.sid, pi.pid, n.title FROM {project_issues} pi INNER JOIN {node} n ON n.nid = pi.nid WHERE n.nid = %d', $changes['nid']);
-
-    if ($issue = db_fetch_object($result)) {
-      // Build vancode
-      $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $changes['nid']));
-      // Strip the "/" from the end of the thread.
-      $max = rtrim($max, '/');
-      // Finally, build the thread field for this new comment.
-      $thread = int2vancode(vancode2int($max) + 1) .'/';
-
-      // These two are not allowed to be set in changes.
-      unset($changes['cid'], $changes['pid']);
-      $comment = $changes + array(
-        'pid' => 0,
-        'uid' => $user->uid,
-        // The correct subject (#number) is supplied during the save cycle.
-        'subject' => '--project followup subject--',
-        'hostname' => ip_address(),
-        'timestamp' => time(),
-        'status' => COMMENT_PUBLISHED,
-        'format' => FILTER_FORMAT_DEFAULT,
-        'thread' => $thread,
-        'name' => $user->name,
-        'mail' => '',
-        'homepage' => '',
-        'category' => $issue->category,
-        'priority' => $issue->priority,
-        'assigned' => $issue->assigned,
-        'sid' => $issue->sid,
-        'title' => $issue->title,
-      );
+    $defaults = array('uid', 'name');
+    foreach ($defaults as $key) {
+      $changes[$key] = isset($changes[$key]) ? $changes[$key] : $auto_user->$key;
+    }
+    project_issue_add_followup($changes);
+  }
+  else {
+    return FALSE;
+  }
+}
 
-      if (!isset($comment['project_info'])) {
-        $comment['project_info'] = array();
-      }
-      $comment['project_info'] += array(
-        'pid' => $issue->pid,
-        'rid' => $issue->rid,
-        'component' => $issue->component,
-        'assigned' => $issue->assigned,
-      );
+/**
+ * Saves a comment to the database.
+ *
+ * TODO: Ideally this should die as soon as core's comment_save() becomes more
+ *       abstracted.
+ *
+ * @param $changes
+ *   An associative array specifying what should change in the issue. Every key
+ *   corresponds to a database field and the value is what it should be changed
+ *   to.  Required keys are:
+ *     - nid: Specifies the issue being changed.
+ *     - uid: Specifies the user ID of the user adding the followup.
+ *     - comment: Contains the text of the followup changing the issue.
+ *
+ *   You can specify the following fields of the comment table: subject,
+ *   hostname, timestamp, score, status, format, thread, users, name, mail,
+ *   homepage. You can also specify the following fields from project_issues
+ *   table: category, priority, assigned, sid, title. There is a special,
+ *   optional key called 'project_info', its value is another associative
+ *   array with the following fields from project_issues: pid, rid, component.
+ *   Example: To change the issue status and set the comment text for the
+ *   issue with nid = 100, this array might look like:
+ *     array(
+ *      'nid' => 100,
+ *      'sid' => 4,
+ *      'comment' => t('This issue was automatically closed after 2 weeks of no activity.'),
+ *     );
+ */
+function project_issue_add_followup($changes) {
+  $user = user_load(array('uid' => $changes['uid']));
+  $result = db_query('SELECT pi.nid, pi.rid, pi.component, pi.category, pi.priority, pi.assigned, pi.sid, pi.pid, n.title FROM {project_issues} pi INNER JOIN {node} n ON n.nid = pi.nid WHERE n.nid = %d', $changes['nid']);
 
-      db_query("INSERT INTO {comments} (pid, nid, uid, subject, comment, hostname, timestamp, status, format, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s')", $comment['pid'], $comment['nid'], $comment['uid'], $comment['subject'], $comment['comment'], $comment['hostname'], $comment['timestamp'], $comment['status'], $comment['format'], $comment['thread'], $comment['name'], $comment['mail'], $comment['homepage']);
+  if ($issue = db_fetch_object($result)) {
+    // Build vancode
+    $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $changes['nid']));
+    // Strip the "/" from the end of the thread.
+    $max = rtrim($max, '/');
+    // Finally, build the thread field for this new comment.
+    $thread = int2vancode(vancode2int($max) + 1) .'/';
+
+    // These two are not allowed to be set in changes.
+    unset($changes['cid'], $changes['pid']);
+    $comment = $changes + array(
+      'pid' => 0,
+      'uid' => $user->uid,
+      // The correct subject (#number) is supplied during the save cycle.
+      'subject' => '--project followup subject--',
+      'hostname' => ip_address(),
+      'timestamp' => time(),
+      'status' => COMMENT_PUBLISHED,
+      'format' => FILTER_FORMAT_DEFAULT,
+      'thread' => $thread,
+      'name' => $user->name,
+      'mail' => '',
+      'homepage' => '',
+      'category' => $issue->category,
+      'priority' => $issue->priority,
+      'assigned' => $issue->assigned,
+      'sid' => $issue->sid,
+      'title' => $issue->title,
+    );
 
-      $comment['cid'] = db_last_insert_id('comments', 'cid');
+    if (!isset($comment['project_info'])) {
+      $comment['project_info'] = array();
+    }
+    $comment['project_info'] += array(
+      'pid' => $issue->pid,
+      'rid' => $issue->rid,
+      'component' => $issue->component,
+      'assigned' => $issue->assigned,
+    );
 
-      _comment_update_node_statistics($comment['nid']);
+    db_query("INSERT INTO {comments} (pid, nid, uid, subject, comment, hostname, timestamp, status, format, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s')", $comment['pid'], $comment['nid'], $comment['uid'], $comment['subject'], $comment['comment'], $comment['hostname'], $comment['timestamp'], $comment['status'], $comment['format'], $comment['thread'], $comment['name'], $comment['mail'], $comment['homepage']);
 
-      // Tell the other modules a new comment has been submitted.
-      comment_invoke_comment($comment, 'insert');
-      cache_clear_all();
-    }
+    $comment['cid'] = db_last_insert_id('comments', 'cid');
+
+    _comment_update_node_statistics($comment['nid']);
 
-    // Load the original user back in.
-    $user = $original_user;
-    session_save_session(TRUE);
+    // Tell the other modules a new comment has been submitted.
+    comment_invoke_comment($comment, 'insert');
+    cache_clear_all();
   }
 }
 
