Commit c67f6098 authored by Derek Wright's avatar Derek Wright
Browse files

[#397458] by dww: Consolidate project notifications into user defaults.

Since there never used to be a way to say "Send me e-mail notifications
for my own issues for all projects" many users have had to select this
option individually on a large number of projects. If a user is subscribed
to 50 or more projects at this notification level, remove all of those
per-project settings and just set their per-user global default.
parent 0e022780
Loading
Loading
Loading
Loading
+42 −3
Changes for project_issue.install: 42 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -746,6 +746,45 @@ function project_issue_update_6009() {
  return $ret;
}

/**
 * Migrate users with notifications for >= 50 projects to a global setting.
 *
 * Since there never used to be a way to say "Send me e-mail notifications
 * for my own issues for all projects" many users have had to select this
 * option individually on a large number of projects. If a user is subscribed
 * to 50 or more projects at this notification level, remove all of those
 * per-project settings and just set their per-user global default.
 */
function project_issue_update_6010() {
  $ret = array();

  // Find all the users with 50 or more projects configured to send e-mail for
  // "own issues".
  $uids = array();
  $result = db_query("SELECT uid, COUNT(nid) AS project_count FROM {project_issue_notification_project} WHERE level = %d GROUP BY uid HAVING project_count >= 50", PROJECT_ISSUE_NOTIFICATION_OWN);
  while ($uid = db_result($result)) {
    $uids[] = $uid;
  }

  if (!empty($uids)) {
    // Define a per-user default for all of them.
    foreach ($uids as $uid) {
      $query = db_query("INSERT INTO {project_issue_notification_global} (uid, level) VALUES (%d, %d)", $uid, PROJECT_ISSUE_NOTIFICATION_OWN);
    }

    // Remove all the per-project settings for them.
    $placeholders = db_placeholders($uids);
    $query = db_query("DELETE FROM {project_issue_notification_project} WHERE level = " . PROJECT_ISSUE_NOTIFICATION_OWN . " AND uid IN (" . $placeholders . ")", $uids);

    $ret[] = array(
      'success' => TRUE,
      'query' => format_plural(count($uids), 'Converted per-project notification settings for @count users to per-user global defaults.', 'Converted per-project notification settings for 1 user a per-user global default.'),
    );
  }

  return $ret;
}

/**
 * Add primary key to {project_issue_notification_project}.
 *
@@ -753,7 +792,7 @@ function project_issue_update_6009() {
 * there are a high number of rows in the table. The test table had 2.34
 * million rows, and the query took over three hours on reasonable hardware.
 */
function project_issue_update_6010() {
function project_issue_update_6011() {
  $ret = array();
  db_add_primary_key($ret, 'project_issue_notification_project', array('uid', 'nid'));
  return $ret;
@@ -765,7 +804,7 @@ function project_issue_update_6010() {
 * WARNING: This update can take a *very* long time (potentially hours) if
 * there are a high number of rows in the table.
 */
function project_issue_update_6011() {
function project_issue_update_6012() {
  $ret = array();
  db_add_index($ret, 'project_issue_notification_project', 'nid_level', array('nid', 'level'));
  return $ret;
@@ -774,7 +813,7 @@ function project_issue_update_6011() {
/**
 * Drop unneccessary indexes from {project_issue_notification_project}.
 */
function project_issue_update_6012() {
function project_issue_update_6013() {
  $ret = array();
  db_drop_index($ret, 'project_issue_notification_project', 'project_subscriptions_nid_uid_level');
  return $ret;