Ejemplo n.º 1
0
/**
 * Receives a message event object from moodletxt+
 * and sens an SMS to the destination user as per their
 * preferences
 * @param object $messageObject Message event object
 * @version 2012062401
 * @since 2012062401
 */
function send_moodletxt_plus_message($messageObject)
{
    $accountDAO = new TxttoolsAccountDAO();
    $userDAO = new MoodletxtMoodleUserDAO();
    $messageDAO = new TxttoolsSentMessageDAO();
    // moodletxt requires a ConnectTxt account to be specified
    // for this message to transition via
    $defaultAccountId = (int) get_config('moodletxt', 'Event_Messaging_Account');
    if ($defaultAccountId > 0) {
        $connectTxtAccount = $accountDAO->getTxttoolsAccountById($defaultAccountId);
        // Check that the specified account has outbound enabled
        if ($connectTxtAccount->isOutboundEnabled()) {
            $sender = $userDAO->getUserById($messageObject->from_id);
            $recipient = $userDAO->getUserById($messageObject->to_id);
            $message = new MoodletxtOutboundMessage($connectTxtAccount, $sender, $messageObject->smallmessage, time(), MoodletxtOutboundMessage::$MESSAGE_CHARGE_TYPE_BULK);
            $message->addMessageRecipient($recipient);
            $message->setEventCreated(true);
            // Send message to ConnectTxt
            try {
                $connector = MoodletxtOutboundControllerFactory::getOutboundController(MoodletxtOutboundControllerFactory::$CONTROLLER_TYPE_XML);
                $message->setSentSMSMessages($connector->sendMessage($message));
                $messageDAO->saveSentMessage($message);
            } catch (MoodletxtRemoteProcessingException $ex) {
                // Die silently, for now
            }
        }
    }
}
Ejemplo n.º 2
0
 /**
  * Builds message and status objects from parsed XML
  * @param SimpleXMLElement $node The node collection to create the object from
  * @return mixed[] Sent messages built from the XML
  * @see SentMessageStatus
  * @version 2012031501
  * @since 2011033101
  */
 private function buildStatusMessage($node)
 {
     $returnObjects = array();
     // Check that all required children for message object
     // build exist within passed data
     if (isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_MESSAGE_TEXT}) && isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_PHONE}) && isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_TICKET}) && isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_UNIQUE_ID})) {
         // Build object and shove it onto the parsed objects array
         $sentMessageObject = new MoodletxtOutboundSMS($this->outboundMessageObject->getId(), (int) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_TICKET}, (string) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_MESSAGE_TEXT});
         $sentMessageObject->setRecipientObject($this->outboundMessageObject->getMessageRecipientByKey((string) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_UNIQUE_ID}));
     }
     // Check if required children for status object exist
     if (isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_TICKET}) && isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_CODE}) && isset($node->{MoodletxtXMLConstants::$RESPONSE_STATUS_MESSAGE})) {
         // Build status object and shove onto array
         $statusObject = new MoodletxtOutboundSMSStatus((int) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_TICKET}, (int) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_CODE}, (string) $node->{MoodletxtXMLConstants::$RESPONSE_STATUS_MESSAGE}, time());
         if (isset($sentMessageObject)) {
             $sentMessageObject->addStatusUpdate($statusObject);
         } else {
             if ($this->getExistingSentMessageByTicketNumber($statusObject->getTicketNumber()) != null) {
                 $this->getExistingSentMessageByTicketNumber($statusObject->getTicketNumber())->addStatusUpdate($statusObject);
             } else {
                 array_push($returnObjects, $statusObject);
             }
         }
     }
     if (isset($sentMessageObject)) {
         array_push($returnObjects, $sentMessageObject);
     }
     return $returnObjects;
 }
Ejemplo n.º 3
0
         $destinations += $userDAO->getUsersInGroup($userGroup, 'phone');
     }
 }
 if (count($moodleUsers) > 0) {
     $destinations += $userDAO->getUsersById($moodleUsers, 'phone');
 }
 // If there are valid destinations, send the message
 if (count($destinations) > 0) {
     $txttoolsAccount = $accountDAO->getTxttoolsAccountById($formData->txttoolsaccount);
     $thisUser = new MoodletxtBiteSizedUser($USER->id, $USER->username, $USER->firstname, $USER->lastname);
     if ($formData->schedule == 'schedule') {
         $scheduletime = usertime(gmmktime($formData->scheduletime['H'], $formData->scheduletime['i'], 0, $formData->scheduletime['M'], $formData->scheduletime['d'], $formData->scheduletime['Y']));
     } else {
         $scheduletime = time();
     }
     $outboundMessage = new MoodletxtOutboundMessage($txttoolsAccount, $thisUser, $formData->messageText, time(), MoodletxtOutboundMessage::$MESSAGE_CHARGE_TYPE_BULK, $scheduletime, $formData->suppressUnicode);
     $outboundMessage->setMessageRecipients($destinations);
     try {
         $connector = MoodletxtOutboundControllerFactory::getOutboundController(MoodletxtOutboundControllerFactory::$CONTROLLER_TYPE_XML);
         $outboundMessage->setSentSMSMessages($connector->sendMessage($outboundMessage));
         $messageDAO->saveSentMessage($outboundMessage);
         $statsDAO->incrementUserOutboundStatsById($txttoolsAccount->getId(), $USER->id, 1);
         // Stub for later expansion
         $sentPageUrl = new moodle_url('/blocks/moodletxt/sent.php', array('course' => $courseId, 'instance' => $instanceId));
         redirect($sentPageUrl, get_string('redirectmessagesent', 'block_moodletxt'));
     } catch (MoodletxtRemoteProcessingException $ex) {
         // Uh-oh, couldn't send! Display error, please
         if (has_capability('block/moodletxt:adminsettings', $blockcontext, $USER->id)) {
             $notifications = $output->notification(get_string('errorconnadmin', 'block_moodletxt'), 'notifyproblem');
             $notifications .= $output->notification($ex->getMessage(), 'notifyproblem');
         } else {
Ejemplo n.º 4
0
 /**
  * Converts an outbound message down to a basic object for writing to the DB
  * @param MoodletxtOutboundMessage $bean Object to convert
  * @param object $existingRecord Existing DB record, if one exists
  * @return object Object for writing to the database
  * @version 2012062401
  * @since 2011081101
  */
 private function convertBeanToStandardClass(MoodletxtOutboundMessage $bean, object $existingRecord = null)
 {
     if ($existingRecord == null) {
         $existingRecord = new object();
     }
     $existingRecord->userid = $bean->getUser()->getId();
     $existingRecord->txttoolsaccount = $bean->getTxttoolsAccount()->getId();
     $existingRecord->messagetext = $bean->getMessageText();
     $existingRecord->timesent = $bean->getTimeSent();
     $existingRecord->scheduledfor = $bean->getScheduledTime();
     $existingRecord->type = $bean->getType();
     $existingRecord->fromevent = $bean->isEventCreated() ? 1 : 0;
     return $existingRecord;
 }
Ejemplo n.º 5
0
 /**
  * Build text message blocks.
  * Method builds the individual <Message> blocks to be included in the message being built.
  * These blocks are cached into an array, then compiled into <Request> blocks later on.
  * @param MoodletxtOutboundMessage $message The message object to base the block on
  * @version 2013080801
  * @since 2010090101
  */
 private function buildTextMessageBlocks(MoodletxtOutboundMessage $message)
 {
     $messageRecipients = $message->getMessageRecipients();
     // Loop over recipients and create message blocks
     foreach ($messageRecipients as $recipientKey => $recipient) {
         if (!$recipient->getRecipientNumber() instanceof MoodletxtPhoneNumber) {
             continue;
         }
         $messageText = trim(stripslashes($message->getMessageText()));
         $messageText = MoodletxtStringHelper::mergeTagsIntoMessageText($messageText, $recipient);
         // Chunk message text into blocks of 160 chars
         $messagechunks = str_split($messageText, 160);
         // Build message blocks
         foreach ($messagechunks as $chunk) {
             // Code written this way so the output is all nice and formatted
             $messageBlock = '
 ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_BLOCK . '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_TEXT . $chunk . MoodletxtXMLConstants::$_REQUEST_MESSAGE_TEXT . '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_PHONE . $recipient->getRecipientNumber()->getPhoneNumber() . MoodletxtXMLConstants::$_REQUEST_MESSAGE_PHONE . '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_TYPE . $message->getType() . MoodletxtXMLConstants::$_REQUEST_MESSAGE_TYPE . '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_UNIQUE_ID . $recipientKey . MoodletxtXMLConstants::$_REQUEST_MESSAGE_UNIQUE_ID;
             // Add scheduled send time if specified
             $messageBlock .= $message->getScheduledTime() > $message->getTimeSent() ? '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_SCHEDULE_DATE . $message->getScheduledTime() . MoodletxtXMLConstants::$_REQUEST_MESSAGE_SCHEDULE_DATE : '';
             // Add UTF-8 suppression if specified
             $messageBlock .= $message->isSuppressUnicode() ? '
     ' . MoodletxtXMLConstants::$REQUEST_MESSAGE_SUPPRESS_UNICODE : '';
             $messageBlock .= '
 ' . MoodletxtXMLConstants::$_REQUEST_MESSAGE_BLOCK;
             $this->addRequestBlock($messageBlock);
         }
     }
 }