function engagements_handle_incoming_sms

6.x engagements.module engagements_handle_incoming_sms($vars = array())

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.

TODO: Have a way to handle incoming calls, too, so we can charge correctly.

2 calls to engagements_handle_incoming_sms()
engagements_mass_sms_perform_batch_operation in modules/engagements/engagements.module
engagements_sms_get_all_messages in modules/engagements/engagements.module
Retrieve all messages, update the ones which don't have prices associated with them yet.
1 string reference to 'engagements_handle_incoming_sms'
engagements_menu in modules/engagements/engagements.module
Implement hook_menu

File

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

Code

function engagements_handle_incoming_sms($vars = array()) 
 {

  // If we did not supply a set of variables, use the POST superglobal
  if (count($vars) === 0) {
    $vars = $_POST;
  }

  watchdog("engagements_sms", "Received from vars (possibly POST): <pre>" . print_r($vars, TRUE) . '</pre>', array(), WATCHDOG_DEBUG);

  $message_sid = $vars ['MessageSid'];
  $from_mobile = engagements_convert_to_valid_phone_number($vars ['From']);
  $to_mobile = engagements_convert_to_valid_phone_number($vars ['To']);
  $system_name = variable_get("system_name", "FlightPath");

  $body = $vars ['Body'];

  $num_media = intval($vars ['NumMedia']);
  $num_segments = intval($vars ['NumSegments']);
  $price = 0; // We know this is an SMS, so this is the price per segment inbound (SignalWire does not charge us for basic SMS inbound).
  if ($num_media > 0) {
    $price = 0.01; // Contained at least one piece of media, so price increases.  This is SignalWire's price.
  }
  $direction = "inbound";
  $fp_price = engagements_get_fp_price($price, $direction, $num_segments);

  $date_sent_ts = time(); // the time we received it.
  if (isset($vars ['date_sent_ts'])) {
    $date_sent_ts = $vars ['date_sent_ts'];
  }

  if (isset($vars ['direction'])) {
    $direction = $vars ['direction'];
  }

  $content = NULL;

  $link = $alert_link = "";

  // Figure out what user this came from/to!
  $from_cwid = $from_user_id = $to_user_id = "";
  $user_id = db_result(db_query("SELECT user_id FROM user_attributes WHERE `name` = 'mobile_phone' AND `value` = ?", array($from_mobile)));
  if ($user_id) {
    $from_cwid = db_get_cwid_from_user_id($user_id);
    $from_user_id = $user_id;
  }

  $to_cwid = "";
  $user_id = db_result(db_query("SELECT user_id FROM user_attributes WHERE `name` = 'mobile_phone' AND `value` = ?", array($to_mobile)));
  if ($user_id) {
    $to_cwid = db_get_cwid_from_user_id($user_id);
    $to_user_id = $user_id;
  }


  $dir = "received";
  $fac_cwid = $to_cwid;
  $stu_cwid = $from_cwid;
  if (strstr($direction, 'outbound')) {
    $dir = 'sent';
    $fac_cwid = $from_cwid;
    $stu_cwid = $to_cwid;
  }

  // Did we receive STOP?
  if ($dir == 'received') {
    if (trim(strtoupper($body)) === 'STOP') {
      engagements_handle_sms_stop($from_user_id, $from_cwid, $from_mobile, $to_mobile);
    }
    // UNSTOP or SUBSCRIBE?
    if (trim(strtoupper($body)) === 'UNSTOP' || trim(strtoupper($body)) === 'SUBSCRIBE') {
      engagements_handle_sms_unstop($from_user_id, $from_cwid, $from_mobile, $to_mobile);
    }

    // We do not return.  Go ahead and let it create an engagement below, so the
    // advisor can see that this happened.

  }


  if (trim($stu_cwid) != "") {
    // create an engagement content node for this student!

    $content = new stdClass();
    $content->type = 'engagement';
    $content->cid = "new";
    $content->published = 1;
    $content->delete_flag = 0;
    $content->title = "Engagement: txt_msg re: student $stu_cwid"; // required

    $content->field__activity_datetime ['value'] = date('Y-m-d H:i:s', $date_sent_ts);
    $content->field__faculty_id ['value'] = $fac_cwid;
    $content->field__student_id ['value'] = $stu_cwid;
    $content->field__engagement_type ['value'] = 'txt_msg';
    $content->field__direction ['value'] = $dir;
    $content->field__from_sms_phone ['value'] = $from_mobile;
    $content->field__to_sms_phone ['value'] = $to_mobile;

    content_save($content);
    $cid = $content->cid;

    $media_filenames = "";

    // Handle retreiving and saving media.
    if ($num_media > 0) { // Do we have any media?
      for ($t = 0; $t < $num_media; $t++) {
        $media_url = $vars ["MediaUrl$t"];
        $media_type = $vars ["MediaContentType$t"];

        $ext = engagements_get_file_extension_from_mime_type($media_type);
        if (!$ext || $ext == "html" || $ext == "htm") {
          continue; // skip if its not a type we care about.
        }

        // Create a new random filename to save it as.
        $filename = 'sms__' . $from_mobile . '_' . time() . '_' . mt_rand(1, 9999) . '.' . $ext;
        $tmp_name = sha1($filename . mt_rand(1, 9999) . time() . mt_rand(1, 9999));

        $file_contents = file_get_contents($media_url);

        // Save to our temporary location on the server.
        $test = file_put_contents(sys_get_temp_dir() . '/' . $tmp_name, $file_contents);
        if (!$test) {
          watchdog('engagements_sms', 'Unable to write media file at temp location: ' . sys_get_temp_dir(), array(), 'error');
          fpm('Unable to write media file at temp location:' . sys_get_temp_dir() . '.  Permissions issue?');
          continue;
        }


        $file = array(
          'name' => $filename,
          'type' => $media_type,
          'tmp_name' => $tmp_name,
        );

        // This function will handle encryption of files automatically.
        $fid = content_add_new_uploaded_file($file, $cid, FALSE);
        $media_filenames .= $filename . ',';
      } // for t (media)

      $media_filenames = rtrim($media_filenames, ',');
    } // if num media > 0




    // if we have media, add to the end of body.
    if ($media_filenames) {
      $body .= "~~MEDIA~~$media_filenames~~END_MEDIA~~";
    }

    $content->field__engagement_msg ['value'] = $body;
    $content->field__external_msg_id ['value'] = $message_sid;

    $content->field__visibility ['value'] = 'public';

    content_save($content);

  } // We have a student to attach this to.



  if ($dir != "sent") { // meaning, we RECEIVED this message.
    $already_notified_users = array();

    $student_name = fp_get_student_name($stu_cwid, TRUE);
    $base_url = $GLOBALS ['fp_system_settings']['base_url'];
    $link = $base_url . "/engagements?current_student_id=$stu_cwid";

    $phones = engagements_get_from_phones();
    $desc = "";
    $to = $to_mobile;
    $pretty_to_number = engagements_convert_to_pretty_phone_number($to);
    $desc = @$phones ['lines'][$to]['description'];
    if (!$desc) {
      $desc = @$phones ['lines'][$from_mobile]['description'];
    }
    if ($desc) {
      $desc = " $desc";
    }

    if (trim($stu_cwid) != "") {
      // Create a new "activity record" that the student has sent a txt message    
      $acontent = new stdClass();
      $acontent->type = 'activity_record';
      $acontent->cid = "new";
      $acontent->published = 1;
      $acontent->delete_flag = 0;
      $acontent->title = t('Student sent a text message to ') . $pretty_to_number . " / " . $desc;
      $acontent->field__student_id ['value'] = $stu_cwid;
      $acontent->field__activity_type ['value'] = 'comment';
      content_save($acontent);

      // Notify the advisor(s).      

      $tmsg = "$student_name has submitted a text message to $system_name 
                    (To: $pretty_to_number / $desc).
                    <br><br>\n\n
                    To view, visit the student's Engagements tab by logging in and following this link:<br>\n 
                    <a href='$link'>$link</a>";

      $advisors = advise_get_advisors_for_student($stu_cwid);
      foreach ($advisors as $c => $afaculty_id) {
        $account_user_id = db_get_user_id_from_cwid($afaculty_id, 'faculty');
        if ($account_user_id) {
          notify_send_notification_to_user($account_user_id, $tmsg, $content->cid, 'engagement');
          $already_notified_users [] = $account_user_id;
        }
      } // foreach advisors

    }


    /*
 *    TODO:  Only do this if we have a setting set?  
 *    TODO:  Note: don't send the full message, but instead send link to an alert that we create for the recipient?  We'd have to do this in the foreach loop below.
 * 
      if (trim($stu_cwid) == "") {
        // Meaning, we couldn't find this student in our system!
        // TODO:  Create an alert for the recipient which does show the full txt message.  The notification provides a link to the alert.  We'd do this in
        // TODO:  the foreach loop below.
        $tmsg .= "A text message has been received for $system_name. (To: $pretty_to_number / $desc).<br><br> No student could be found which is
                  associated with the sending number ($from_mobile).<br><br>Message body:<br>---------<br>
                  $body<br><br>Message SID:$message_sid."; 
        
      }
  */



    // Notify other users who might have been assigned to receive notifications 
    $to_be_notified = engagements_get_users_to_be_notified_for_sms_on_number($to_mobile);
    foreach ($to_be_notified as $account_user_id) {
      if (in_array($account_user_id, $already_notified_users)) {
        continue;
      }

      if (trim($stu_cwid) == "") {
        // Meaning, we couldn't find this student in our system!
        // TODO:  Create an alert for the recipient which does show the full txt message.  The notification provides a link to the alert.
        $alert = new stdClass();
        $alert->type = 'alert';
        $alert->cid = "new";
        $alert->published = 1;
        $alert->delete_flag = 0;
        $alert->title = t('Unrecognized sender sent a text message to ') . $pretty_to_number . " / " . $desc;
        $alert->field__alert_msg ['value'] = $body;
        $alert->field__alert_status ['value'] = 'open';
        $alert->field__visibility ['value'] = 'faculty';
        $alert->field__target_faculty_id ['value'] = db_get_cwid_from_user_id($account_user_id);
        $alert->field__student_id ['value'] = "N/A";
        $alert->field__exclude_advisor ['value'] = 1;

        content_save($alert);

        $alert_link = $base_url . "/content/$alert->cid?content_crumbs=alerts";

        $tmsg = "A text message has been received for $system_name. (To: $pretty_to_number / $desc).<br><br> No student could be found which is
                  associated with the sending number ($from_mobile).<br><br>Message SID: $message_sid.
                  <br><br>To view the full message, view the Alert by logging in and clicking here: 
                  <a href='$alert_link'>$alert_link</a>";

        notify_send_notification_to_user($account_user_id, $tmsg, $alert->cid, 'alert');
      }
      else {
        // We DO have a student this came from.  Let this user know normally.
        $tmsg = "$student_name has submitted a text message to $system_name (To: $pretty_to_number / $desc).
                  <br><br>\n\n
                  To view, visit the student's Engagements tab by logging in and following this link:<br>\n 
                  <a href='$link'>$link</a>";
        notify_send_notification_to_user($account_user_id, $tmsg, $content->cid, 'engagement');
      }
    } //foreach to_be_notified


  } // dir != sent  (we received this text)








  // Write our data to our sms_history table.
  db_query(
  "INSERT INTO sms_history (`message_sid`, sw_type, `body`, `from_number`, `to_number`, `sw_price`, `fp_price`, `from_cwid`, `to_cwid`, `updated`, `direction`, 
                              `media_filenames`, `date_sent`, price_processed, num_segments, delivery_status, err_code, err_message, err_friendly_message)
              VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ", 
  array($message_sid, 'sms', $body, $from_mobile, $to_mobile, $price, $fp_price, $from_cwid, $to_cwid, time(), $direction,
    $media_filenames, $date_sent_ts, 1, $num_segments, @$vars ['display_status'], @$vars ['err_code'], @$vars ['err_message'], @$vars ['err_friendly_message'])
  );
}