You are here

function antispam_notify_moderators in AntiSpam 6

Same name and namespace in other branches
  1. 7 antispam.module \antispam_notify_moderators()

Notify moderators of new/updated content, only content needing approval or nothing at all.

Parameters

string Content type; can be either 'node' or 'comment'.:

object Content object.:

boolean TRUE if content is in published status.:

boolean TRUE if content has been marked as spam.:

3 calls to antispam_notify_moderators()
antispam_comment in ./antispam.module
Implementation of hook_comment().
antispam_nodeapi in ./antispam.module
Implementation of hook_nodeapi().
_antispam_comment_form_submit in ./antispam.module
Comment form submit callback; check for spam.

File

./antispam.module, line 1256

Code

function antispam_notify_moderators($content_type, $content, $is_published, $is_spam) {
  global $user, $base_url;

  // Proceed only if e-mail notifications are enabled.
  if (!variable_get('antispam_email_enabled', 0)) {
    return;
  }

  // Make sure we have an object.
  $content = (object) $content;

  // Compute the related moderator permission.
  if ($content_type == 'comment') {
    $moderator_permission = 'moderate spam in comments';
    $administer_permission = 'administer comments';
  }
  else {
    $moderator_types = antispam_get_moderator_types($account);
    $moderator_permission = 'moderate spam in nodes of type ' . $moderator_types[$content->type];
    $administer_permission = 'administer nodes';
  }

  // Obtain list of moderators of the specified content type.
  $sql = 'SELECT u.uid, u.name, u.mail, m.email_for' . ' FROM {permission} p' . ' INNER JOIN {users_roles} r ON r.rid = p.rid' . ' INNER JOIN {users} u ON u.uid = r.uid OR u.uid = 1' . ' LEFT JOIN {antispam_moderator} m ON m.uid = u.uid' . ' WHERE p.perm LIKE \'%%%s%%\'' . ' OR p.perm LIKE \'%%%s%%\'';
  $result = db_query($sql, $moderator_permission, $administer_permission);
  $moderators = array();
  while ($u = db_fetch_object($result)) {

    // if a moderator submit a new node/comment, then exclude him/her from the list
    if ($u->uid != $user->uid) {
      $moderators[$u->uid] = array(
        'name' => $u->name,
        'email_to' => $u->mail,
        'email_for' => !is_null($u->email_for) ? $u->email_for : 'approval',
      );
    }
  }

  // check to see if we need to add the admin to the notification moderator list
  $sql = 'SELECT u.uid, u.name, u.mail, m.email_for' . ' FROM {users} u' . ' LEFT JOIN {antispam_moderator} m ON m.uid = u.uid' . ' WHERE u.uid = 1';
  $result = db_query($sql, $moderator_permission, $administer_permission);
  if ($u = db_fetch_object($result)) {

    // if the admin submit a new node/comment, then exclude from the list
    if ($u->uid != $user->uid) {
      $moderators[$u->uid] = array(
        'name' => $u->name,
        'email_to' => $u->mail,
        'email_for' => !is_null($u->email_for) ? $u->email_for : 'approval',
      );
    }
  }

  // Extract unique email addresses and ignore those who have requested to not get e-mail notifications.
  $unique_emails = array();
  foreach ($moderators as $uid => $moderator) {
    if ($moderator['email_for'] == 'all' || $moderator['email_for'] == 'approval' && !$is_published) {
      if (!isset($unique_emails[$moderator['email_to']])) {
        $unique_emails[$moderator['email_to']] = $uid;
      }
    }
  }
  if (count($unique_emails) <= 0) {
    return;
  }

  // If this is about a comment, try to load the node.
  // Also, prepare arguments for notification message.
  $site_name = variable_get('site_name', t('Drupal'));
  if ($content_type == 'comment') {
    if (!($node = antispam_content_load('node', $content->nid))) {
      watchdog('antispam', 'An error has ocurred while trying to notify moderators about a comment. The associated node could not be loaded.', array(), WATCHDOG_NOTICE, l(t('view'), 'node/' . $content->nid, array(
        'fragment' => 'comment-' . $content->cid,
      )));
      return;
    }
    $message_args = array(
      '@title-label' => t('Subject'),
      '@content-title' => $content->subject,
      '@content-type' => t('comment'),
      '!content-link' => url('node/' . $content->nid, array(
        'fragment' => 'comment-' . $content->cid,
        'absolute' => TRUE,
      )),
    );
  }
  else {
    $message_args = array(
      '@title-label' => t('Title'),
      '@content-title' => $content->title,
      '@content-type' => $moderator_types[$content->type],
      '!content-link' => url('node/' . $content->nid, array(
        'absolute' => TRUE,
      )),
    );
  }
  $message_args['@content-status'] = ($is_published ? t('published') : t('not published')) . ($is_spam ? ' (' . t('marked as spam') . ')' : '');
  $message_args['@site-name'] = $site_name;
  $message_args['!site-link'] = $base_url . base_path();
  $message_args['@type'] = $moderator_types[$content->type];
  $message_title = t('[@site-name] moderator notification - Posted @content-type \'@content-title\'', $message_args);
  $message_body = t(<<<EOT
Hello @user-name,

You can use the following information to review this @content-type:

@title-label: @content-title
URL: !content-link
Status: @content-status

Please, do not reply to this e-mail. It is an automated notification you are receiving because you are a moderator at @site-name. If you no longer wish to receive such notifications, you can change your moderator settings in your user profile.

Thank you

!site-link
EOT
, $message_args);

  // Log the notification to watchdog.
  watchdog('antispam', 'Trying to notify the following recipients: %emails', array(
    '%emails' => implode(', ', array_keys($unique_emails)),
  ));

  // Send e-mails.
  foreach ($unique_emails as $email_to => $uid) {
    $message = array(
      'id' => 'antispam_moderator_notification',
      'to' => $email_to,
      'subject' => $message_title,
      'body' => str_replace('@user-name', check_plain($moderators[$uid]['name']), $message_body),
      'headers' => array(),
    );
    $ret = drupal_mail_send($message);
    if ($ret == FALSE) {
      watchdog('antispam', 'Failed to send a moderator notification email to: %emails', array(
        '%emails' => implode(', ', array_keys($unique_emails)),
      ));
    }
  }
}