/** * Returns all filters on a given txttools account * @global moodle_database $DB Moodle database controller * @param TxttoolsAccount $txttoolsAccount Account to search against * @return TxttoolsAccount Updated account object * @version 2012042301 * @since 2011070401 */ public function getFiltersForAccount(TxttoolsAccount $txttoolsAccount) { global $DB; $filters = $DB->get_records('block_moodletxt_filter', array('account' => $txttoolsAccount->getId())); // Iterate over filters and build beans foreach ($filters as $filter) { $filterObj = new MoodletxtInboundFilter($filter->account, $filter->type, $filter->value, $filter->id); $users = $DB->get_records_sql(self::$FETCH_INBOXES_SQL, array('filterid' => $filterObj->getId())); // Get links between filters and user inboxes foreach ($users as $user) { $filterObj->addDestinationUser(new MoodletxtBiteSizedUser($user->id, $user->username, $user->firstname, $user->lastname)); } $txttoolsAccount->addInboundFilter($filterObj); } return $txttoolsAccount; }
/** * Special-case method used by cron and other automated update routines * to find all messages requiring status updates on a given txttools account * @global moodle_database $DB Moodle database manager * @param TxttoolsAccount $txttoolsAccount Account to find messages for * @return MoodletxtInboundMessage[] Messages requiring update * @version 2013081601 * @since 2012041701 */ public function getAllNonFinalisedSMSMessagesForAccount(TxttoolsAccount $txttoolsAccount) { global $DB; /* * This used to be done in a single query, but thanks * to a lack of potential database optimisation and * a lot of outer joins, it was pretty slow, so let's * do things the Moodletxt 2 way! */ $sql = 'SELECT sent.ticketnumber FROM {block_moodletxt_sent} as sent INNER JOIN {block_moodletxt_outbox} messages ON sent.messageid = messages.id INNER JOIN {block_moodletxt_status} status ON status.ticketnumber = sent.ticketnumber WHERE messages.txttoolsaccount = :accountid AND ( status.status = :failurestatus1 OR status.status = :failurestatus2 OR status.status = :failurestatus3 OR status.status = :deliveredstatus )'; $params = array('accountid' => $txttoolsAccount->getId(), 'failurestatus1' => MoodletxtOutboundSMSStatus::$STATUS_FAILED_INSUFFICIENT_CREDITS, 'failurestatus2' => MoodletxtOutboundSMSStatus::$STATUS_FAILED_AT_NETWORK, 'failurestatus3' => MoodletxtOutboundSMSStatus::$STATUS_FAILED_UNKNOWN_ERROR, 'deliveredstatus' => MoodletxtOutboundSMSStatus::$STATUS_DELIVERED_TO_HANDSET); $completedMessageTickets = $DB->get_fieldset_sql($sql, $params); $returnedMessages = array(); // get_in_or_equal() doesn't accept empty arrays if (count($completedMessageTickets) > 0) { list($inFrag, $params2) = $DB->get_in_or_equal($completedMessageTickets, SQL_PARAMS_NAMED, 'param', false); $sql = 'SELECT sent.*, messages.messagetext FROM {block_moodletxt_sent} sent INNER JOIN {block_moodletxt_outbox} messages ON sent.messageid = messages.id WHERE sent.ticketnumber ' . $inFrag; $sentMessages = $DB->get_records_sql($sql, $params2); foreach ($sentMessages as $sentMessage) { $returnedMessages[$sentMessage->id] = $this->convertSMSStandardClassToBeans($sentMessage); } } return $returnedMessages; }
/** * Saves a txttools account to the database * @global moodle_database $DB Moodle database controller * @param TxttoolsAccount $txttoolsAccount txttools account * @param boolean $updateRestrictions Whether included restrictions need saving * @version 2012042301 * @since 2011042601 */ public function saveTxttoolsAccount(TxttoolsAccount $txttoolsAccount, $updateRestrictions = false) { global $DB; $insertClass = new object(); $action = 'insert'; // Check for existing account if ($txttoolsAccount->getId() > 0) { // Account already exists - update DB values with new ones $insertClass = $this->convertBeanToStandardClass($txttoolsAccount, $DB->get_record('block_moodletxt_accounts', array('id' => $txttoolsAccount->getId()))); $action = 'update'; } else { $insertClass = $this->convertBeanToStandardClass($txttoolsAccount); } // Do database update/insert if ($action == 'update') { $DB->update_record('block_moodletxt_accounts', $insertClass); } else { $DB->insert_record('block_moodletxt_accounts', $insertClass); } if ($updateRestrictions) { // Clear existing allowed users and set new ones $DB->delete_records('block_moodletxt_restrict', array('txttoolsaccount' => $txttoolsAccount->getId())); foreach ($txttoolsAccount->getAllowedUsers() as $allowedUser) { $insertClass = new object(); $insertClass->moodleuser = $allowedUser->getId(); $insertClass->txttoolsaccount = $txttoolsAccount->getId(); $DB->insert_record('block_moodletxt_restrict', $insertClass); } } }
/** * Increment's a user's outbound message stats for a given day * @TODO Expand these stats in 3.1 * @param TxttoolsAccount $txttoolsAccount Account message was sent through * @param object $user Moodle user object * @param int $numberSent Number of messages sent * @version 2012101101 * @since 2012101101 */ public function incrementUserOutboundStats(TxttoolsAccount $txttoolsAccount, object $user, $numberSent) { $this->incrementUserOutboundStatsById($txttoolsAccount->getId(), $user->id, $numberSent); }
/** * Build JSON response structure for an updated account * @param TxttoolsAccount $txttoolsAccount Account to build from * @return string Constructed JSON * @version 2012100801 * @since 2011061301 */ private function buildResponse(TxttoolsAccount $txttoolsAccount) { // Copy template down $response = $this->responseTemplate; $response['accountID'] = $txttoolsAccount->getId(); $response['username'] = $txttoolsAccount->getUsername(); $response['description'] = $txttoolsAccount->getDescription(); $response['creditsUsed'] = $txttoolsAccount->getCreditsUsed(); $response['creditsRemaining'] = $txttoolsAccount->getCreditsRemaining(); $response['updateTimeString'] = $txttoolsAccount->getLastUpdateFormatted(); $response['allowOutbound'] = $txttoolsAccount->isOutboundEnabled() ? 1 : 0; $response['allowInbound'] = $txttoolsAccount->isInboundEnabled() ? 1 : 0; $response['billingType'] = $txttoolsAccount->getBillingType(); // Include account restrictions if specified foreach ($txttoolsAccount->getAllowedUsers() as $allowedUser) { $response['allowedUsers'][$allowedUser->getId()] = array('firstName' => $allowedUser->getFirstName(), 'lastName' => $allowedUser->getLastName(), 'username' => $allowedUser->getUsername()); } // Include inbound filters if specified foreach ($txttoolsAccount->getInboundFilters() as $inboundFilter) { $response['inboundFilters'][$inboundFilter->getId()] = array('type' => $inboundFilter->getFilterType(), 'operand' => (string) $inboundFilter->getOperand(), 'inboxes' => array()); foreach ($inboundFilter->getDestinationUsers() as $biteSizedUser) { // No I'm not kidding. Fortunately these won't be included often $response['inboundFilters'][$inboundFilter->getId()]['users'][$biteSizedUser->getId()] = array('firstName' => $biteSizedUser->getFirstName(), 'lastName' => $biteSizedUser->getLastName(), 'username' => $biteSizedUser->getUsername()); } } return json_encode($response); }
/** * @param array $blacklistedNumbers * @param TxttoolsAccount $account * @version 2015062901 * @since 2015062901 */ private function syncDbWithResponse($blacklistedNumbers, $account) { $blacklistEntries = array(); foreach ($blacklistedNumbers as $number => $status) { $blacklistEntry = new BlacklistEntry(); $blacklistEntry->setPhoneNumber($number); $blacklistEntry->setIsBlacklisted(BlacklistDAO::$BLACKLISTED); $blacklistEntry->setAccountId($account->getId()); $blacklistEntries[] = $blacklistEntry; } $this->blacklistDAO->removeAllBlacklistEntriesForAccount($account->getId()); $this->blacklistDAO->saveBlacklistEntries($blacklistEntries); }