b2evolution PHP Cross Reference Blogging Systems

Source: /inc/cron/jobs/_unread_message_reminder.job.php - 138 lines - 6604 bytes - Text - Print

Description: This file implements the unread messages email reminder cron job

   1  <?php
   2  /**

   3   * This file implements the unread messages email reminder cron job

   4   *

   5   * @version $Id: $

   6   */
   7  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
   8  
   9  global $DB, $UserSettings, $Settings;
  10  
  11  global $servertimenow, $htsrv_url, $unread_message_reminder_delay, $unread_messsage_reminder_threshold;
  12  
  13  // New unread messages reminder may be sent to a user if it has at least one unread message which is older then the given threshold date

  14  $threshold_date = date2mysql( $servertimenow - $unread_messsage_reminder_threshold );
  15  // New unread messages reminder should be sent to a user if the last one was sent at least x days ago, where x depends from the configuration

  16  // Get the minimum delay value from the configuration array

  17  $minimum_delay = array_shift( array_values( $unread_message_reminder_delay ) );
  18  // Get the datetime which corresponds to the minimum delay value.

  19  // If the last unread message reminder for a specific user is after this datetime, then notification must not be send to that user now.

  20  $reminder_threshold = date2mysql( $servertimenow - ( $minimum_delay * 86400 ) );
  21  
  22  if (empty($UserSettings))
  23  { // initialize UserSettings
  24      load_class( 'users/model/_usersettings.class.php', 'UserSettings' );
  25      $UserSettings = new UserSettings();
  26  }
  27  
  28  if (empty($Settings))
  29  { // initialize GeneralSettings
  30      load_class( 'settings/model/_generalsettings.class.php', 'GeneralSettings' );
  31      $Settings = new GeneralSettings();
  32  }
  33  
  34  // set condition to check if user wants to recive unread messages reminders

  35  $notify_condition = '( notif_setting.uset_value IS NOT NULL AND notif_setting.uset_value <> '.$DB->quote( '0' ).' )';
  36  if( $Settings->get( 'def_notify_unread_messages' ) )
  37  { // by default everybody should be notified, so notify those users who didn't explicitly set to not receive notifications
  38      $notify_condition .= ' OR notif_setting.uset_value IS NULL';
  39  }
  40  
  41  // Find user who have unread messages created more than x ( = configured threshold value ) hours ago, and have not been reminded in the last y ( = configured reminder delay for this user ) hours

  42  // We needs to send reminder for these users, but the message must include all unread threads even if the last unread messages was created less then x hours

  43  $query = 'SELECT DISTINCT user_ID, last_sent.uset_value
  44          FROM T_users
  45              INNER JOIN T_messaging__threadstatus ON tsta_user_ID = user_ID
  46              INNER JOIN T_messaging__message ON tsta_first_unread_msg_ID = msg_ID
  47              LEFT JOIN T_users__usersettings last_sent ON last_sent.uset_user_ID = user_ID AND last_sent.uset_name = "last_unread_messages_reminder"
  48              LEFT JOIN T_users__usersettings notif_setting ON notif_setting.uset_user_ID = user_ID AND notif_setting.uset_name = "notify_unread_messages"
  49          WHERE ( msg_datetime < '.$DB->quote( $threshold_date ).' )
  50              AND ( last_sent.uset_value IS NULL OR last_sent.uset_value < '.$DB->quote( $reminder_threshold ).' )
  51              AND ( '.$notify_condition.' )
  52              AND ( LENGTH(TRIM(user_email)) > 0 )
  53              AND ( user_email NOT IN ( SELECT emblk_address FROM T_email__blocked WHERE '.get_mail_blocked_condition().' ) )';
  54  $users_to_remind = $DB->get_assoc( $query, 0, 'Find users who need to be reminded' );
  55  
  56  if( empty( $users_to_remind ) )
  57  { // There is no user to remind
  58      $result_message = T_( 'It was not necessary to send any reminder!' );
  59      return 1;
  60  }
  61  
  62  // load all required users into the cache

  63  $UserCache = & get_UserCache();
  64  $users_to_remind_ids = array_keys( $users_to_remind );
  65  $UserCache->load_list( $users_to_remind_ids );
  66  
  67  // Filter out those users which wasn't logged in since many days and shouldn't get reminder email in every three days

  68  $index = -1;
  69  foreach( $users_to_remind as $user_ID => $last_reminder_ts )
  70  {
  71      $index++;
  72      if( empty( $last_reminder_ts ) )
  73      { // Reminder was not sent yet, we don't need to check anything else, because in this case reminder must be sent
  74          continue;
  75      }
  76      $notify_User = $UserCache->get_by_ID( $user_ID );
  77      // Set timestamps

  78      $last_reminder_ts = strtotime( $last_reminder_ts );
  79      $lastseen_ts = strtotime( $notify_User->get( 'lastseen_ts' ) );
  80      // Set days

  81      $days_since_lastseen = floor(( $servertimenow - $lastseen_ts )/(60*60*24));
  82      $days_since_reminder = floor(( $servertimenow - $last_reminder_ts )/(60*60*24));
  83      // Go through reminder delay values defined in the advanced config file, and pick the corresponding delay for this User

  84      $dont_send_reminder = true;
  85      foreach( $unread_message_reminder_delay as $lastseen => $delay )
  86      {
  87          if( $days_since_lastseen < $lastseen )
  88          { // We have found the correct delay value, we must check if we should send reminder
  89              $dont_send_reminder = false;
  90              break;
  91          }
  92      }
  93      if( $dont_send_reminder || ( $days_since_reminder < $delay ) )
  94      { // Don't send a new reminder, because the user was not logged in since a very long time or we have already sent a reminder in the last x days ( x = delay value )
  95          unset( $users_to_remind_ids[$index] );
  96      }
  97  }
  98  
  99  if( empty( $users_to_remind_ids ) )
 100  { // There is no user to remind after we have filtered out those ussers who haven't logged in since a long time
 101      $result_message = T_( 'It was not necessary to send any reminder!' );
 102      return 1;
 103  }
 104  
 105  // Set TRUE to use gender settings from back office

 106  global $is_admin_page;
 107  $is_admin_page = true;
 108  
 109  // Get all those user threads and their recipients where the corresponding users have unread messages

 110  $unread_threads = get_users_unread_threads( $users_to_remind_ids, NULL, 'array', 'html' );
 111  
 112  // Get unread thread urls

 113  list( $threads_link ) = get_messages_link_to();
 114  
 115  $reminder_sent = 0;
 116  foreach( $users_to_remind_ids as $user_ID )
 117  {
 118      // send reminder email

 119      $email_template_params = array(
 120              'unread_threads' => $unread_threads[$user_ID],
 121              'threads_link' => $threads_link,
 122          );
 123      $notify_User = $UserCache->get_by_ID( $user_ID );
 124      // Change locale here to localize the email subject and content

 125      locale_temp_switch( $notify_User->get( 'locale' ) );
 126      if( send_mail_to_User( $user_ID, T_( 'You have unread messages!' ), 'private_messages_unread_reminder', $email_template_params ) )
 127      { // Update users last unread message reminder timestamp
 128          $UserSettings->set( 'last_unread_messages_reminder', date2mysql( $servertimenow ), $user_ID );
 129          // save UserSettings after each email, because the cron task mail fail and users won't be updated!

 130          $UserSettings->dbupdate();
 131          $reminder_sent++;
 132      }
 133      locale_restore_previous();
 134  }
 135  
 136  $result_message = sprintf( T_( '%d reminder emails were sent!' ), $reminder_sent );
 137  return 1; /* ok */
 138  ?>

title

Description

title

Description

title

Description

title

title

Body