function engagements_handle_sms_stop

6.x engagements.module engagements_handle_sms_stop($user_id = 0, $cwid = '', $phone_number = '', $from_fp_phone_number = '')

This function is called by engagements_handle_incoming_sms, when we receive 'STOP' from the user. We must add them to our "sms_do_not_txt" table, and send a reply letting them know how to re-subscribe in the future.

1 call to engagements_handle_sms_stop()
engagements_handle_incoming_sms in modules/engagements/engagements.module
This catches incoming sms messages from POST, but can also be used by our "sms_get_all_messages" function, but it is also used by the sms_get_all_messages to save/update information.

File

modules/engagements/engagements.module, line 1451
This is the primary module file for the engagements module.

Code

function engagements_handle_sms_stop($user_id = 0, $cwid = '', $phone_number = '', $from_fp_phone_number = '') {

  if ($user_id === 0) {
    // Try to figure out what the user_id is based on the phone number.
    $test = db_result(db_query("SELECT user_id FROM user_attributes WHERE name = 'mobile_phone' AND value = ?", array($from_fp_phone_number)));
    $test = intval($test);
    if ($test > 0) {
      $user_id = $test;
    }
  }

  $phone_number = engagements_convert_to_valid_phone_number($phone_number);
  if (!$phone_number) {
    // watchdog that there was an error because phone number not valid.
    watchdog('engagements_sms', "User tried to opt-out of sms, but invalid phone number: $user_id, $cwid, $phone_number", array(), WATCHDOG_ERROR);
    return FALSE;
  }

  if ($user_id) {
    // Get current notification method for this user.
    $prev_notification_method = user_get_setting($user_id, 'default_notification_method', 'email');
    // Set their new defailt_notification_method to just email.
    user_set_setting($user_id, 'default_notification_method', 'email');
    // also set an opt-out setting value here, so the user can change it from within flightpath.
    user_set_setting($user_id, "sms_opt_out__" . $phone_number, 'yes');
  }

  // Save everything to do_not_txt table.  (First, delete anything already there for this phone_number, which is the most important to keep track of)
  db_query('DELETE FROM sms_do_not_txt WHERE phone_number = ?', array($phone_number));
  db_query("INSERT INTO sms_do_not_txt (user_id, cwid, phone_number, prev_notification_method, updated)
                    VALUES (?, ?, ?, ?, ?)", array($user_id, $cwid, $phone_number, $prev_notification_method, time()));

  // Send final sms back to number telling them how to re-enable.
  $msg = t("You have opted-out of receiving text messages from @flightpath. To re-subscribe, text UNSTOP or SUBSCRIBE.\n\nYou may also log into @flightpath and edit your notification settings.", 
  array("@flightpath" => variable_get("system_name", "FlightPath")));

  engagements_send_sms_to_number($phone_number, $msg, '', $from_fp_phone_number, FALSE);


  watchdog('engagements_sms', "User opted-out of sms: $user_id, $cwid, $phone_number");
}