Index: project_issue.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.module,v
retrieving revision 1.88.2.5
diff -u -r1.88.2.5 project_issue.module
--- project_issue.module	15 May 2008 23:44:52 -0000	1.88.2.5
+++ project_issue.module	26 Jun 2008 04:40:43 -0000
@@ -1,12 +1,17 @@
 <?php
 // $Id: project_issue.module,v 1.88.2.5 2008/05/15 23:44:52 thehunmonkgroup Exp $
-// $Name:  $
+// $Name: DRUPAL-5--2 $
 
 // issue nodes      -> project_issues
 // issue comments   -> project_issue_comments
 
 /// How many issues should be displayed per page by default.
 define('PROJECT_ISSUES_PER_PAGE', 20);
+/**
+ * The project issue state.
+ */
+define('PROJECT_ISSUE_STATE_FIXED', 2);
+define('PROJECT_ISSUE_STATE_CLOSED', 7);
 
 if (function_exists('drupal_get_path')) {
   $path = drupal_get_path('module', 'project_issue');
@@ -205,7 +210,7 @@
 
   // Determine the auto-close username from the auto-close setting.
   $auto_close_username = '';
-  $uid = variable_get('project_issue_auto_close_user', 'anon');
+  $uid = variable_get('project_issue_auto_comment_user', 'anon');
   $anon = variable_get('anonymous', t('Anonymous'));
   if ($uid) {
     if ($uid == 'anon') {
@@ -216,7 +221,7 @@
     }
   }
 
-  $form['project_issue_auto_close_user'] = array(
+  $form['project_issue_auto_comment_user'] = array(
     '#title' => t('Auto-close user'),
     '#type' => 'textfield',
     '#default_value' => $auto_close_username,
@@ -307,93 +312,155 @@
 function project_issue_auto_close() {
   global $user;
 
-  if ($uid = variable_get('project_issue_auto_close_user', 'anon')) {
-    // If a user exists for auto-closing comments, load them into
+  if ($auto_user = project_issue_get_auto_comment_user()) {
+    // If a user exists for auto-comments, load them into
     // the global user object temporarily. We use session_save_session()
     // to provide safe user impersonation.
-    $auto_close = TRUE;
     $original_user = $user;
     session_save_session(FALSE);
+    $user = $auto_user;
+
+    $sid = PROJECT_ISSUE_STATE_CLOSED;
+    $comment = theme('project_issue_auto_close_message');
+    $result = db_query('SELECT p.nid FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid WHERE n.status = 1 AND p.sid = 2 AND n.changed < %d', time() - 14 * 24 * 60 * 60);
+    $clear_cache = FALSE;
+    while ($issue = db_fetch_object($result)) {
+      $clear_cache = $clear_cache || project_issue_insert_auto_comment($issue->nid, $sid, $comment);
+    }
+
+    if ($clear_cache) {
+      // Clear cache so anonymous users can see the new post.
+      cache_clear_all();
+    }
+
+    // Reload the original user.
+    $user = $original_user;
+    session_save_session(TRUE);
+  }
+}
+
+/**
+ * Automatically comment an issue.
+ *
+ * @param integer $nid The node ID to auto comment.
+ * @param integer $sid The project issue status ID.
+ * @param string $message The message text.
+ */
+function project_issue_auto_comment($nid, $sid, $message) {
+  global $user;
+
+  if ($auto_user = project_issue_get_auto_comment_user()) {
+    // If a user exists for auto-comments, 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;
+
+    $clear_cache = project_issue_insert_auto_comment($nid, $sid, $message);
+
+    if ($clear_cache) {
+      // Clear cache so anonymous users can see the new post.
+      cache_clear_all();
+    }
+
+    // Reload the original user.
+    $user = $original_user;
+    session_save_session(TRUE);
+  }
+}
+
+/**
+ * Load and verify the auto-comment user.
+ */
+function project_issue_get_auto_comment_user() {
+  if ($uid = variable_get('project_issue_auto_comment_user', 'anon')) {
     $is_anon = $uid == 'anon';
-    $user = $is_anon ? user_load(array('uid' => 0)) : user_load(array('uid' => $uid));
+    $auto_user = $is_anon ? user_load(array('uid' => 0)) : user_load(array('uid' => $uid));
     // Safety check -- we have to have a valid user here.
-    if(!$user) {
-      watchdog('project_issue', t('Auto-close user failed to load.'), WATCHDOG_ERROR);
-      $auto_close = FALSE;
+    if(!$auto_user) {
+      watchdog('project_issue', t('Auto-comment user failed to load.'), WATCHDOG_ERROR);
+      return FALSE;
     }
     // Safety check -- selected user must still have the correct permissions to auto-close issues.
     if (!user_access('access project issues')) {
-      watchdog('project_issue', t('%name does not have sufficient permissions to auto-close issues.', array('%name' => $is_anon ? variable_get('anonymous', t('Anonymous')) : $user->name)), WATCHDOG_ERROR);
-      $auto_close = FALSE;
+      watchdog('project_issue', t('%name does not have sufficient permissions to auto-comment issues.', array('%name' => $is_anon ? variable_get('anonymous', t('Anonymous')) : $auto_user->name)), WATCHDOG_ERROR);
+      return FALSE;
     }
+    return $auto_user;
+  }
+}
 
-    if ($auto_close) {
-      $result = db_query('SELECT p.nid, p.pid, p.category, p.component, p.priority, p.rid, p.assigned, p.sid, n.title FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid WHERE n.status = 1 AND p.sid = 2 AND n.changed < %d', time() - 14 * 24 * 60 * 60);
-
-      // Set up the persistent {comments} data.
-      $comment['pid'] = 0;
-      $comment['uid'] = $user->uid;
-      // The correct subject number is supplied during the save cycle.
-      $comment['subject'] = 'temp';
-      $comment['comment'] = theme('project_issue_auto_close_message');
-      $comment['format'] = FILTER_FORMAT_DEFAULT;
-      $comment['status'] = COMMENT_PUBLISHED;
-      $comment['name'] = $user->name;
-      $comment['mail'] = '';
-      $comment['homepage'] = '';
-
-      $roles = variable_get('comment_roles', array());
-      $score = 0;
-      foreach (array_intersect(array_keys($roles), array_keys($user->roles)) as $rid) {
-        $score = max($roles[$rid], $score);
-      }
-      $users = serialize(array(0 => $score));
-
-      // Set up the persistent {project_issue_comments} data.
-      // TODO: It's evil to hard-code the status here.
-      $comment['sid'] = 7;
-
-      $clear_cache = FALSE;
-
-      while ($issue = db_fetch_object($result)) {
-        $clear_cache = TRUE;
-
-        $comment['cid'] = db_next_id('{comments}_cid');
-        $comment['timestamp'] = time();
-        $comment['nid'] = $issue->nid;
-        $comment['category'] = $issue->category;
-        $comment['priority'] = $issue->priority;
-        $comment['assigned'] = $issue->assigned;
-        $comment['title'] = $issue->title;
-        $comment['project_info']['pid'] = $issue->pid;
-        $comment['project_info']['rid'] = $issue->rid;
-        $comment['project_info']['component'] = $issue->component;
-
-        // Build vancode
-        $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $comment['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) .'/';
+/**
+ * Insert an automatic comment.
+ *
+ * @param integer $nid The node ID to auto comment.
+ * @param integer $sid The project issue status ID.
+ * @param string $message The message text.
+ * @return boolean TRUE if record was inserted (implying cache needs to be cleared).
+ */
+function project_issue_insert_auto_comment($nid, $sid, $message) {
+  global $user;
 
-        db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $comment['cid'], $comment['nid'], $comment['pid'], $comment['uid'], $comment['subject'], $comment['comment'], $comment['format'], $_SERVER['REMOTE_ADDR'], $comment['timestamp'], $comment['status'], $score, $users, $thread, $comment['name'], $comment['mail'], $comment['homepage']);
+  $result = db_query('SELECT p.nid, p.pid, p.category, p.component, p.priority, p.rid, p.assigned, p.sid, n.title FROM {project_issues} p INNER JOIN {node} n ON n.nid = p.nid WHERE n.nid = %d', $nid);
 
-        _comment_update_node_statistics($comment['nid']);
+  $comment = array();
 
-        // Tell the other modules a new comment has been submitted.
-        comment_invoke_comment($comment, 'insert');
-      }
+  // Set up the persistent {comments} data.
+  $comment['pid'] = 0;
+  $comment['uid'] = $user->uid;
+  // The correct subject number is supplied during the save cycle.
+  $comment['subject'] = 'temp';
+  $comment['comment'] = $message;
+  $comment['format'] = FILTER_FORMAT_DEFAULT;
+  $comment['status'] = COMMENT_PUBLISHED;
+  $comment['name'] = $user->name;
+  $comment['mail'] = '';
+  $comment['homepage'] = '';
+
+  $roles = variable_get('comment_roles', array());
+  $score = 0;
+  foreach (array_intersect(array_keys($roles), array_keys($user->roles)) as $rid) {
+    $score = max($roles[$rid], $score);
+  }
+  $users = serialize(array(0 => $score));
+
+  // Set up the persistent {project_issue_comments} data.
+  $comment['sid'] = $sid;
+
+  $clear_cache = FALSE;
+
+  // TODO Could remove the while loop as query should only return one row.
+  while ($issue = db_fetch_object($result)) {
+    $clear_cache = TRUE;
+
+    $comment['cid'] = db_next_id('{comments}_cid');
+    $comment['timestamp'] = time();
+    $comment['nid'] = $issue->nid;
+    $comment['category'] = $issue->category;
+    $comment['priority'] = $issue->priority;
+    $comment['assigned'] = $issue->assigned;
+    $comment['title'] = $issue->title;
+    $comment['project_info']['pid'] = $issue->pid;
+    $comment['project_info']['rid'] = $issue->rid;
+    $comment['project_info']['component'] = $issue->component;
+
+    // Build vancode
+    $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $comment['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) .'/';
+
+    db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $comment['cid'], $comment['nid'], $comment['pid'], $comment['uid'], $comment['subject'], $comment['comment'], $comment['format'], $_SERVER['REMOTE_ADDR'], $comment['timestamp'], $comment['status'], $score, $users, $thread, $comment['name'], $comment['mail'], $comment['homepage']);
 
-      if ($clear_cache) {
-        // Clear cache so anonymous users can see the new post.
-        cache_clear_all();
-      }
-    }
+    _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');
   }
+
+  return $clear_cache;
 }
 
 /**
