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 23:10:50 -0000 @@ -1,12 +1,14 @@ project_issues // issue comments -> project_issue_comments /// How many issues should be displayed per page by default. define('PROJECT_ISSUES_PER_PAGE', 20); +/// Default age in seconds of issues to auto close. +define('PROJECT_ISSUE_AUTO_CLOSE_SECONDS', 14 * 24 * 60 * 60); if (function_exists('drupal_get_path')) { $path = drupal_get_path('module', 'project_issue'); @@ -203,29 +205,33 @@ '#description' => t('All issue e-mails sent via subscriptions will appear from this e-mail address. You can use %project as a placeholder which will be replaced with the %short_project_name setting for the issue\'s current project.', array('%project' => '%project', '%short_project_name' => t('Short project name'))), ); - // Determine the auto-close username from the auto-close setting. - $auto_close_username = ''; - $uid = variable_get('project_issue_auto_close_user', 'anon'); + // Determine the auto-comment username from the auto-comment setting. + $auto_comment_username = ''; $anon = variable_get('anonymous', t('Anonymous')); - if ($uid) { - if ($uid == 'anon') { - $auto_close_username = $anon; - } - elseif ($account = user_load(array('uid' => $uid))) { - $auto_close_username = $account->name; - } + if ($auto_user = project_issue_get_auto_comment_user()) { + $auto_comment_username = $auto_user->name; } - $form['project_issue_auto_close_user'] = array( - '#title' => t('Auto-close user'), + $form['project_issue_auto_comment_user'] = array( + '#title' => t('Auto-comment user'), '#type' => 'textfield', - '#default_value' => $auto_close_username, + '#default_value' => $auto_comment_username, '#maxlength' => 60, - '#description' => t('Enter the user which will auto-close fixed issues -- leave empty to disable auto-closing or set to %anon to use the anonymous user.', array('%anon' => $anon)), - '#validate' => array('project_issue_validate_auto_close_user' => array()), + '#description' => t('Enter the user which will auto-comment issues -- leave empty to disable auto-commenting or set to %anon to use the anonymous user.', array('%anon' => $anon)), + '#validate' => array('project_issue_validate_auto_comment_user' => array()), '#autocomplete_path' => 'user/autocomplete', ); + $form['project_issue_auto_close_seconds'] = array( + '#title' => t('Auto-close seconds'), + '#type' => 'textfield', + '#default_value' => variable_get('project_issue_auto_close_seconds', PROJECT_ISSUE_AUTO_CLOSE_SECONDS), + '#size' => 8, + '#maxlength' => 10, + '#description' => t('When cron is run, all issues marked fixed that were last updated more than the number of seconds shown will be closed by the auto-comment user. A value of 1209600 (14 * 24 * 60 * 60) will close issues unchanged for two weeks.'), +// '#validate' => array('project_issue_validate_auto_close_seconds' => array()), + ); + if (module_exists('mailhandler')) { // TODO: move this stuff to mailhandler.module ? $items = array(t('')); @@ -261,23 +267,25 @@ } /** - * Validates that the auto-close user exists, and has sufficient permissions - * to auto-close issues. + * Validates that the auto-comment user exists, and has sufficient permissions + * to auto-comment issues. */ -function project_issue_validate_auto_close_user($form) { +function project_issue_validate_auto_comment_user($form) { $name = $form['#value']; if ($name) { + $anon = variable_get('anonymous', t('Anonymous')); // Make this check case-insensitive to allow the admin some data entry leeway. - $is_anon = drupal_strtolower($name) == drupal_strtolower(variable_get('anonymous', t('Anonymous'))); + $is_anon = drupal_strtolower($name) == drupal_strtolower($anon); // Load the user. (don't see a constant for uid 0... ) $account = $is_anon ? user_load(array('uid' => 0)) : user_load(array('name' => $name)); if ($account) { if (user_access('access project issues', $account)) { // Transform the username into the more stable user ID. - form_set_value($form, $is_anon ? 'anon' : $account->uid); +// form_set_value($form, $is_anon ? 'anon' : $account->uid); + form_set_value($form, $account->uid); // Is there a reason not to store the actual uid? } else { - form_error($form, t('%name does not have sufficient permissions to auto-close issues.', array('%name' => $is_anon ? variable_get('anonymous', t('Anonymous')) : $name))); + form_error($form, t('%name does not have sufficient permissions to auto-comment issues.', array('%name' => $is_anon ? $anon : $name))); } } else { @@ -302,105 +310,188 @@ } /** - * Automatically closes issues marked as fixed after two weeks. + * Automatically closes issues marked as fixed for a specified number of seconds. */ function project_issue_auto_close() { + // Set query parameters. + $sid_fixed = project_issue_issue_state_value('fixed'); + $seconds = variable_get('project_issue_auto_close_seconds', PROJECT_ISSUE_AUTO_CLOSE_SECONDS); + + $nids = array(); + $result = db_query('SELECT pi.nid FROM {project_issues} pi INNER JOIN {node} n ON n.nid = pi.nid WHERE n.status = 1 AND pi.sid = %d AND n.changed < %d', $sid_fixed, time() - $seconds); + while ($issue = db_fetch_object($result)) { + $nids[] = $issue->nid; + } + + if (!empty($nids)) { + // Set auto-close status and message. + $sid_closed = project_issue_issue_state_value('closed'); + $message = theme('project_issue_auto_close_message'); + + project_issue_auto_comment($nids, $sid_closed, $message); + } +} + +/** + * Retrieve the id associated with a given project issue state name. + * + * @param $name + * The project issue state name. + * @return + * ID corresponding to project issue state name (or zero if not found). + */ +function project_issue_issue_state_value($name) { + $result = db_query('SELECT pis.sid FROM {project_issue_state} pis WHERE pis.name = "%s"', $name); + $state = db_result($result); + return $state ? $state : 0; // How to default this? +} + +/** + * Comment left when cron auto-closes an issue. + */ +function theme_project_issue_auto_close_message() { + return t('Automatically closed -- issue fixed for two weeks with no activity.'); +} + +/** + * Automatically add a comment to an issue. + * + * @param $nids + * The node IDs to auto comment. + * @param $sid + * The project issue status ID to set on each node. + * @param $message + * The message text. + */ +function project_issue_auto_comment($nids, $sid, $message) { 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); - $is_anon = $uid == 'anon'; - $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; - } - // 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; - } - - 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) .'/'; + $user = $auto_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']); - - _comment_update_node_statistics($comment['nid']); - - // Tell the other modules a new comment has been submitted. - comment_invoke_comment($comment, 'insert'); - } + $clear_cache = _project_issue_insert_auto_comment($nids, $sid, $message); - if ($clear_cache) { - // Clear cache so anonymous users can see the new post. - cache_clear_all(); - } + if ($clear_cache) { + // Clear cache so anonymous users can see the new post. + cache_clear_all(); } - // Load the original user back in. + // Reload the original user. $user = $original_user; session_save_session(TRUE); } } /** - * Comment left when cron auto-closes an issue. + * Load and verify the auto-comment user. + * + * @return object $account The account of the auto-comment user. */ -function theme_project_issue_auto_close_message() { - return t('Automatically closed -- issue fixed for two weeks with no activity.'); +function project_issue_get_auto_comment_user() { + $anon = variable_get('anonymous', t('Anonymous')); + $uid = variable_get('project_issue_auto_comment_user', 0); + $account = user_load(array('uid' => $uid)); + // Safety check -- we have to have a valid user here. + if (!$account) { + watchdog('project_issue', t('Auto-comment user failed to load.'), WATCHDOG_ERROR); + return FALSE; + } + $account->name = $uid ? $account->name : $anon; + // Safety check -- selected user must still have the correct permissions to auto-comment issues. + if (!user_access('access project issues', $account)) { + watchdog('project_issue', t('%name does not have sufficient permissions to auto-comment issues.', array('%name' => $account->name)), WATCHDOG_ERROR); + return FALSE; + } + return $account; +} + +/** + * Insert an automatic comment. + * + * @param $nids + * The node IDs to auto comment. + * @param $sid + * The project issue status ID to set on each node. + * @param $message + * The message text. + * @return + * TRUE if record was inserted (implying cache needs to be cleared); otherwise FALSE. + */ +function _project_issue_insert_auto_comment($nids, $sid, $message) { + global $user; + + /* + * We could call comment_save() except that: + * It sets status based on user access value and we want it always to be published. + * $edit['status'] = user_access('post comments without approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED; + * It calls cache_clear_all() each time (which would be inefficient for the cron auto_close). + * It adds an entry to the watchdog log for each comment added. + */ + + $nid_list = is_array($nids) ? implode(',', $nids) : $nids; + $result = db_query('SELECT pi.nid, pi.pid, pi.category, pi.component, pi.priority, pi.rid, pi.assigned, pi.sid, n.title FROM {project_issues} pi INNER JOIN {node} n ON n.nid = pi.nid WHERE n.nid IN (%s)', $nid_list); + + $comment = array(); + + // 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; + + 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']); + + _comment_update_node_statistics($comment['nid']); + + // Tell the other modules a new comment has been submitted. + comment_invoke_comment($comment, 'insert'); + } + + return $clear_cache; } function project_issue_menu($may_cache) { @@ -870,7 +961,7 @@ array('data' => t('Issue links'), 'class' => 'project-issue-links'), ); $default_states = implode(',', project_issue_default_states()); - $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COUNT(ni.nid) AS count, MAX(ni.changed) AS max_issue_changed FROM {node} n LEFT JOIN {project_issues} p ON n.nid = p.pid AND p.sid IN ($default_states) LEFT JOIN {node} ni ON ni.nid = p.nid AND ni.status = 1 WHERE n.type = 'project_project' AND n.status = 1 AND n.uid = %d GROUP BY n.nid, n.title") . tablesort_sql($header), $user->uid); + $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COUNT(ni.nid) AS count, MAX(ni.changed) AS max_issue_changed FROM {node} n LEFT JOIN {project_issues} pi ON n.nid = pi.pid AND pi.sid IN ($default_states) LEFT JOIN {node} ni ON ni.nid = pi.nid AND ni.status = 1 WHERE n.type = 'project_project' AND n.status = 1 AND n.uid = %d GROUP BY n.nid, n.title") . tablesort_sql($header), $user->uid); if (!db_num_rows($result)) { return ($current_user ? t('You have no projects.') : t('This user has no projects.')); Index: project_issue.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.install,v retrieving revision 1.48.2.1 diff -u -r1.48.2.1 project_issue.install --- project_issue.install 13 Apr 2008 21:13:58 -0000 1.48.2.1 +++ project_issue.install 26 Jun 2008 23:10:50 -0000 @@ -1,6 +1,6 @@