Exemple #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
            }
        }
    }
}
 /**
  * Updates an acount's list of allowed users
  * @param int $accountId ConnectTxt account ID
  * @param array $allowedUsers User IDs the account should be restricted to
  * @return string JSON response
  * @version 2013061701
  * @since 2011062301
  */
 private function updateAccountRestrictions($accountId, array $allowedUsers)
 {
     $txttoolsAccount = $this->accountDAO->getTxttoolsAccountById($accountId);
     // Account must exist within system
     if (!is_object($txttoolsAccount)) {
         throw new MoodletxtAJAXException(get_string('errorinvalidaccountid', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_CODE_BAD_ACCOUNT_ID, null, false, $accountId);
     }
     // Easiest way of doing this, rather than checking
     // which links already exist and modifying, is to clear
     // all existing links and save the given set
     $txttoolsAccount->clearAllowedUsers();
     foreach ($allowedUsers as $allowedUser) {
         try {
             $userObj = $this->userDAO->getUserById($allowedUser);
             $txttoolsAccount->addAllowedUser($userObj);
         } catch (InvalidArgumentException $ex) {
             // Bad user ID - ignore and continue
             continue;
         }
     }
     $this->accountDAO->saveTxttoolsAccount($txttoolsAccount, true);
     return $this->buildResponse($txttoolsAccount);
 }
 /**
  * Move messages from one user's inbox to another
  * @param int $sourceUserId ID of source user for the message(s)
  * @param array(int) $messageIds The ID(s) of message(s) to move across
  * @param int $destinationUserId ID of user to send messages to
  * @return string Encoded JSON response
  * @throws MoodletxtAJAXException 
  * @version 2012051601
  * @since 2012050301
  */
 private function moveMessagesToUser($sourceUserId, array $messageIds, $destinationUserId)
 {
     try {
         $sourceUser = $this->userDAO->getUserById($sourceUserId);
         $destinationUser = $this->userDAO->getUserById($destinationUserId);
         // User is trying to pull a fast one - stop them
         if ($sourceUser === null || $destinationUser === null) {
             throw new MoodletxtAJAXException(get_string('errorinvaliduserid', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_CODE_BAD_USER_ID);
         }
         foreach ($messageIds as $messageId) {
             $message = $this->messageDAO->getMessageById($messageId, true, $sourceUser->getId());
             $this->messageDAO->deleteMessage($message);
             $message->addDestinationUserId($destinationUser->getId());
             $this->messageDAO->saveInboundMessage($message);
         }
     } catch (InvalidArgumentException $ex) {
         // Either the message no longer exists,
         // or the user is attempting to access a message they don't own
         throw new MoodletxtAJAXException(get_string('errorbadmessageid', 'block_moodletxt'), MoodletxtAJAXException::$ERROR_CODE_BAD_MESSAGE_ID);
     }
     $response = $this->responseTemplate;
     $response['userId'] = $sourceUser->getId();
     return json_encode($response);
 }
 /**
  * Fetches content for the block when displayed
  * @global Object $CFG Moodle config object
  * @global Object $USER Moodle user object
  * @return string Block content
  * @version 2012071701
  * @since 2010081801
  */
 public function get_content()
 {
     global $CFG, $USER;
     // If content has already been created, return that
     if ($this->content !== NULL) {
         return $this->content;
     }
     // Get renderer
     $output = $this->page->get_renderer('block_moodletxt');
     // Get some user details
     $user = $this->userDAO->getUserById($USER->id);
     // Initialise content class
     $this->content = new stdClass();
     // Check that specialization has been done
     $this->specialization();
     $userIsAdmin = false;
     $userCanReceive = false;
     // Check for admin
     $userIsAdmin = has_capability('block/moodletxt:adminsettings', $this->context, $USER->id) || has_capability('block/moodletxt:adminusers', $this->context, $USER->id);
     // Check that user has send access
     $checkSend = has_capability('block/moodletxt:sendmessages', $this->context, $USER->id);
     // Check user is allowed to set their signature/templates up
     $checkPrefs = has_capability('block/moodletxt:personalsettings', $this->context, $USER->id);
     $unreadFrag = '';
     if (has_capability('block/moodletxt:receivemessages', $this->context, $USER->id)) {
         $userCanReceive = true;
         $unreadMessages = $this->receivedMessagesDAO->countMessagesInUsersInbox($USER->id, true);
         if ($unreadMessages > 0) {
             $unreadFrag = html_writer::tag('b', '(' . $unreadMessages . ')');
         }
     }
     // Initialise content object
     $this->content->items = array();
     $this->content->icons = array();
     $this->content->footer = '';
     // Add links to block dependent on user permissions/access level
     if ($checkSend) {
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_MESSAGE_COMPOSE, get_string('altcompose', 'block_moodletxt'), array('title' => get_string('imgtitlecompose', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinksend', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/blocks/moodletxt/send.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         array_push($this->content->icons, $output->render($icon));
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_MESSAGES_SENT, get_string('altsentmessages', 'block_moodletxt'), array('title' => get_string('imgtitlesentmessages', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinksent', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/blocks/moodletxt/sent.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         array_push($this->content->icons, $output->render($icon));
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_ADDRESSBOOK, get_string('altaddressbook', 'block_moodletxt'), array('title' => get_string('imgtitleaddressbook', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinkaddressbook', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/blocks/moodletxt/addressbooks.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         array_push($this->content->icons, $output->render($icon));
     }
     if ($userCanReceive) {
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_MESSAGES_INBOX, get_string('altinbox', 'block_moodletxt'), array('title' => get_string('imgtitleinbox', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinkinbox', 'block_moodletxt') . $unreadFrag, array('href' => $CFG->wwwroot . '/blocks/moodletxt/received.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         array_push($this->content->icons, $output->render($icon));
     }
     if ($checkPrefs) {
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_PREFERENCES, get_string('altpreferences', 'block_moodletxt'), array('title' => get_string('imgtitlepreferences', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinkpreferences', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/blocks/moodletxt/preferences.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         array_push($this->content->icons, $output->render($icon));
     }
     if ($userIsAdmin) {
         //            $icon = new moodletxt_icon(moodletxt_icon::$ICON_STATS, get_string('altstats', 'block_moodletxt'), array('title' => get_string('imgtitlestats', 'block_moodletxt')));
         //            array_push($this->content->items, html_writer::tag('a', get_string('blocklinkstats', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/blocks/moodletxt/userstats.php?course=' . $this->page->course->id . '&instance=' . $this->instance->id)));
         //            array_push($this->content->icons, $output->render($icon));
         $icon = new moodletxt_icon(moodletxt_icon::$ICON_SETTINGS, get_string('altsettings', 'block_moodletxt'), array('title' => get_string('imgtitlesettings', 'block_moodletxt')));
         array_push($this->content->items, html_writer::tag('a', get_string('blocklinksettings', 'block_moodletxt'), array('href' => $CFG->wwwroot . '/admin/settings.php?section=blocksettingmoodletxt')));
         array_push($this->content->icons, $output->render($icon));
     }
     // If some form of content has been added, set up block
     if (count($this->content->items) > 0) {
         $this->content->footer = get_string('blockfooter', 'block_moodletxt');
         // Check whether config info has been previously defined
         if (!isset($this->config->title) || empty($this->config->title)) {
             // Set up default configuration
             $this->title = get_string('blocktitle', 'block_moodletxt');
         } else {
             // Use user configuration
             $this->title = $this->config->title;
         }
     }
     return $this->content;
 }
         // if there already are other accounts saved in the DB use the same URL and location for all new ones
     } else {
         $acc = $accountDAO->getUrlAndLocation();
         $location = $acc->location;
         $url = $acc->url;
     }
     if ($accountDAO->accountNameExists((string) $formData->accountName, $url)) {
         $formErrors[] = get_string('erroraccountexists', 'block_moodletxt');
     } else {
         if ($formData->accountCtxtInstance !== TxttoolsAccount::$UK_LOCATION && $formData->accountCtxtInstance !== TxttoolsAccount::$US_LOCATION && ($formData->accountCtxtInstance !== 'URL' && $formData->accountUrl)) {
             $formErrors[] = get_string('errorinvalidlocationorurl', 'block_moodletxt');
         } else {
             $encrypter = new MoodletxtEncryption();
             $key = get_config('moodletxt', 'EK');
             $xmlController = MoodletxtOutboundControllerFactory::getOutboundController(MoodletxtOutboundControllerFactory::$CONTROLLER_TYPE_XML);
             $defaultUser = $userDAO->getUserById($formData->accountDefaultInbox);
             $txttoolsAccount = new TxttoolsAccount((string) $formData->accountName, (string) $formData->accountDescription, $defaultUser, $url, $location);
             $txttoolsAccount->setEncryptedPassword($encrypter->encrypt($key, $formData->accountPassword1, 20));
             if ($location === 'URL') {
                 $location = $xmlController->getServerLocation($txttoolsAccount);
                 $txttoolsAccount->setLocation($location);
             }
             $txttoolsAccount = $xmlController->updateAccountInfo($txttoolsAccount);
             $accountDAO->saveTxttoolsAccount($txttoolsAccount);
             redirect($CFG->wwwroot . '/blocks/moodletxt/settings_accounts.php', get_string('redirectaccountsfound', 'block_moodletxt'));
         }
     }
 } catch (MoodletxtRemoteProcessingException $ex) {
     $connErrors['remoteError'] = $ex->getCode();
 } catch (Exception $ex) {
     $connErrors['connectError'] = $ex->getCode();