/**
  * Globally toggle access to an account, either in or outbound
  * @param int $accountId ID of account to modify
  * @param string $direction Specify inbound/outbound access to update
  * @return string JSON response
  * @throws MoodletxtAJAXException
  * @version 2012101601
  * @since 2011061301
  */
 private function toggleAccountAccess($accountId, $direction = 'outbound')
 {
     $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);
     }
     // Update account settings within DB
     if ($direction == self::$DIRECTION_IDENTIFIER_OUTBOUND) {
         $txttoolsAccount->setOutboundEnabled(!$txttoolsAccount->isOutboundEnabled());
     } else {
         if ($direction == self::$DIRECTION_IDENTIFIER_INBOUND) {
             $txttoolsAccount->setInboundEnabled(!$txttoolsAccount->isInboundEnabled());
         }
     }
     $this->accountDAO->saveTxttoolsAccount($txttoolsAccount);
     return $this->buildResponse($txttoolsAccount);
 }
        } 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();
    }
}
echo $output->header();
if (count($connErrors) > 0 || count($formErrors) > 0) {
    $errorText = get_string('errorconnadmin', 'block_moodletxt');
    foreach ($formErrors as $formError) {
        $errorText .= html_writer::empty_tag('br') . $formError;
    }