function engagements_mass_sms_perform_batch_operation
Search API
7.x engagements.module | engagements_mass_sms_perform_batch_operation(&$batch, $recipients, $message, $from_number) |
6.x engagements.module | engagements_mass_sms_perform_batch_operation(&$batch, $recipients, $message, $from_number) |
File
- modules/
engagements/ engagements.module, line 653 - This is the primary module file for the engagements module.
Code
function engagements_mass_sms_perform_batch_operation(&$batch, $recipients, $message, $from_number)
{
// if this is our first time through, let's init our values.
if (!isset($batch ["results"]["total"])) {
// Our first time through. Let's start up.
$batch ['recipients'] = array();
$batch ['not_found'] = array();
$batch ['rejected'] = array();
$batch ['date_sent_ts'] = time();
$batch ['date_sent'] = format_date(convert_time(time()));
$batch ['csv'] = "";
$batch ['message'] = $message;
$safe_message = str_replace('"', "'", $message);
$num_segments = ceil(strlen($message) / 160);
// Create headers for the csv
$batch ['csv'] .= "From Number,To Entered Value,Recipient Mobile Number,Recipient Cwid,Recipient User Type,SMS Status,Message,Num Segments,MessageSid,Date Sent\n";
// Second row states the message, so we don't have to repeat it each row.
$batch ['csv'] .= "000,Sending msg:,0,0,0,0,\"$safe_message\",$num_segments,0,0\n";
$batch ['csv'] .= "--,--,--,--,--,--,--,--,--,--\n";
$lines = explode("\n", $recipients);
$batch ["results"] = array();
$batch ["results"]["total"] = count($lines);
$batch ["results"]["current"] = 0;
$batch ["results"]["finished"] = FALSE;
} // if.... (this is our first time through)
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Actually go through our lines of recipients, try to find their number, and send!
////////////////////////////////////////////
$current = $batch ["results"]["current"];
$total = $batch ["results"]["total"];
$limit = 1; // How many numbers to process this time through
$c = 0; // count of records.
$lines = explode("\n", $recipients);
for ($t = $current; $t < count($lines); $t++) {
if ($c >= $limit) {
break;
}
$line = trim($lines [$t]);
if (!$line) {
$c++;
continue;
}
// Whatever this is, let's try to get a valid phone number out of it.
$num = $cwid = $user_type = $user_name = "";
$user_type = 'student';
// First, we were given a CWID?
$test = db_get_user_id_from_cwid($line, 'student');
if (!$test) {
$test = db_get_user_id_from_cwid($line, 'faculty');
$user_type = 'faculty';
}
if (!$test) {
$user_type = '';
// Okay, it was NOT a CWID. Is this an email address?
if (strstr($line, '@')) {
$test = db_get_user_id_from_email($line, 'student');
$user_type = 'student';
if (!$test) {
$test = db_get_user_id_from_email($line, 'faculty');
$user_type = 'faculty';
}
}
}
if (!$test) {
$user_type = '';
// Okay, this was not an email address either. Perhaps this is simply a phone number all by itself?
$num = engagements_convert_to_valid_phone_number($line);
if (!$num) {
// Okay, we are going to have to give up-- this was not a valid number, nor anything else we might try
// to search for.
$batch ['not_found'][] = $line;
// add to csv
$batch ['csv'] .= "\"$from_number\",\"$line\",\"\",\"\",\"\",\"user/number not found\",\"\",\"\",\"\",\"\"\n";
$c++;
continue;
}
else {
// Figure out what user_id this phone number belongs to.
// TODO: This bit of code means it can only be sent to current users, and not non-users. Perhaps that should be a setting?
$test = db_result(db_query("SELECT user_id FROM user_attributes WHERE `name` = 'mobile_phone' AND `value` = ? LIMIT 1", array($num)));
}
}
if ($test) {
$user_type = 'student';
// This means that we found a user_id. Let's get their mobile_phone number from their user attributes.
$num = engagements_convert_to_valid_phone_number(user_get_attribute($test, 'mobile_phone', ''));
$test_account = fp_load_user($test);
if ($test_account && !$test_account->is_student) {
$user_type = 'faculty';
}
// Let's also get their CWID.
$cwid = db_get_cwid_from_user_id($test);
// And let's get their name.
$user_name = $test_account->l_name . ", " . $test_account->f_name;
}
if (!$num || $num == "") {
// Okay, we are going to have to give up-- this was not a valid number
$batch ['not_found'][] = $line;
$batch ['csv'] .= "\"$from_number\",\"$line\",\"$cwid\",\"$user_type\",\"\",\"user has no valid mobile number\",\"\",\"\",\"\",\"\"\n";
$c++;
continue;
}
// If we made it here, then we have a valid recipient! Let's put it into an array called details.
$details = array(
'num' => $num,
'user_id' => $test,
'cwid' => $cwid,
'user_type' => $user_type,
'user_name' => $user_name,
'line' => $line,
);
// Has the user opted-out of receiving our txt messages?
if (!engagements_can_send_sms_to_number($num)) {
$batch ['opt_out'][] = $line;
$batch ['csv'] .= "\"$from_number\",\"$line\",\"$cwid\",\"$user_type\",\"\",\"user opted-out of receiving text\",\"\",\"\",\"\",\"\"\n";
$c++;
continue;
}
// Send our message to this number, record it as an engagement as well.
$num_segments = ceil(strlen($message) / 160);
$status = "sent";
$external_msg_id = engagements_send_sms_to_number($num, $message, $details ['cwid'], $from_number);
if (!$external_msg_id) {
$batch ['rejected'][] = $num;
$status = "rejected";
}
else {
// We can create an engagement for this SMS. We will make use of our engagements_handle_incoming_sms function.
$vars = array(
'MessageSid' => $external_msg_id,
'From' => $from_number,
'To' => $num,
'Body' => $message,
'NumMedia' => 0,
'NumSegements' => $num_segments,
'direction' => 'outbound-api',
'date_sent_ts' => $batch ['date_sent_ts'],
);
engagements_handle_incoming_sms($vars);
}
$safe_message_sid = (string) $external_msg_id;
$batch ['csv'] .= "\"$from_number\",\"{$details ['line']}\",\"$num\",\"{$details ['cwid']}\",\"{$details ['user_type']}\",\"$status\",\"\",\"$num_segments\",\"$safe_message_sid\",\"{$batch ['date_sent']}\"\n";
$c++;
} // foreach
// Update our $batch results variables
$batch ["results"]["current"] = $current + $c;
if ($batch ["results"]["current"] >= $total) {
// We are finished!
$batch ["results"]["finished"] = TRUE;
}
// Add the information to the SESSION so we can call on it later.
$_SESSION ['mass_sms_batch_data'] = $batch;
$_SESSION ["mass_sms_batch_data"]["last_timestamp"] = time();
}