/**
  * Creates a new direct transfer offer and notifications.
  * 
  * @param WebSoccer $websoccer Application context
  * @param DbConnection $db DB connection.
  * @param int $playerId ID of player to transfer.
  * @param int $senderUserId ID of user who made the offer.
  * @param int $senderClubId ID of user's team.
  * @param int $receiverUserId ID of player's manager.
  * @param int $receiverClubId ID of player's team.
  * @param int $offerAmount amount to offer.
  * @param string $offerMessage optional message from user.
  * @param int $offerPlayerId1 optional ID of an exchange player.
  * @param int $offerPlayerId2 another optional ID of an exchange player.
  */
 public static function createTransferOffer(WebSoccer $websoccer, DbConnection $db, $playerId, $senderUserId, $senderClubId, $receiverUserId, $receiverClubId, $offerAmount, $offerMessage, $offerPlayerId1 = null, $offerPlayerId2 = null)
 {
     $columns = array("player_id" => $playerId, "sender_user_id" => $senderUserId, "sender_club_id" => $senderClubId, "receiver_club_id" => $receiverClubId, "submitted_date" => $websoccer->getNowAsTimestamp(), "offer_amount" => $offerAmount, "offer_message" => $offerMessage, "offer_player1" => $offerPlayerId1, "offer_player2" => $offerPlayerId2);
     $db->queryInsert($columns, $websoccer->getConfig("db_prefix") . "_transfer_offer");
     $sender = UsersDataService::getUserById($websoccer, $db, $senderUserId);
     // create notification
     NotificationsDataService::createNotification($websoccer, $db, $receiverUserId, "transferoffer_notification_offerreceived", array("sendername" => $sender["nick"]), NOTIFICATION_TYPE, NOTIFICATION_TARGETPAGE, null, $receiverClubId);
 }
 /**
  * Deletes user's absence report and gives back his teams.
  * Deputy gets notification.
  * 
  * @param WebSoccer $websoccer Application context.
  * @param DbConnection $db DB connection.
  * @param int $userId ID of user who returned.
  */
 public static function confirmComeback(WebSoccer $websoccer, DbConnection $db, $userId)
 {
     $absence = self::getCurrentAbsenceOfUser($websoccer, $db, $userId);
     if (!$absence) {
         return;
     }
     // give back teams
     $db->queryUpdate(array('user_id' => $userId, 'user_id_actual' => NULL), $websoccer->getConfig('db_prefix') . '_verein', 'user_id_actual = %d', $userId);
     // delete absence(s)
     $db->queryDelete($websoccer->getConfig('db_prefix') . '_userabsence', 'user_id', $userId);
     // notify deputy
     if ($absence['deputy_id']) {
         $user = UsersDataService::getUserById($websoccer, $db, $userId);
         NotificationsDataService::createNotification($websoccer, $db, $absence['deputy_id'], 'absence_comeback_notification', array('user' => $user['nick']), 'absence', 'user');
     }
 }
 /**
  * (non-PHPdoc)
  * @see IModel::getTemplateParameters()
  */
 public function getTemplateParameters()
 {
     $userId = (int) $this->_websoccer->getRequestParameter('id');
     if ($userId < 1) {
         $userId = $this->_websoccer->getUser()->id;
     }
     $user = UsersDataService::getUserById($this->_websoccer, $this->_db, $userId);
     if (!isset($user['id'])) {
         throw new Exception($this->_i18n->getMessage(MSG_KEY_ERROR_PAGENOTFOUND));
     }
     // get teams of user
     $fromTable = $this->_websoccer->getConfig('db_prefix') . '_verein';
     $whereCondition = 'user_id = %d AND status = \'1\' AND nationalteam != \'1\' ORDER BY name ASC';
     $result = $this->_db->querySelect('id,name', $fromTable, $whereCondition, $userId);
     $teams = array();
     while ($team = $result->fetch_array()) {
         $teams[] = $team;
     }
     $result->free();
     // get national team of user
     if ($this->_websoccer->getConfig('nationalteams_enabled')) {
         $columns = 'id,name';
         $fromTable = $this->_websoccer->getConfig('db_prefix') . '_verein';
         $whereCondition = 'user_id = %d AND nationalteam = \'1\'';
         $result = $this->_db->querySelect($columns, $fromTable, $whereCondition, $userId, 1);
         $nationalteam = $result->fetch_array();
         $result->free();
         if (isset($nationalteam['id'])) {
             $user['nationalteam'] = $nationalteam;
         }
     }
     // badges
     $result = $this->_db->querySelect('name, description, level, date_rewarded, event', $this->_websoccer->getConfig('db_prefix') . '_badge INNER JOIN ' . $this->_websoccer->getConfig('db_prefix') . '_badge_user ON id = badge_id', 'user_id = %d ORDER BY level DESC, date_rewarded ASC', $userId);
     $badges = array();
     while ($badge = $result->fetch_array()) {
         if (!isset($badges[$badge['event']])) {
             $badges[$badge['event']] = $badge;
         }
     }
     $result->free();
     return array('user' => $user, 'userteams' => $teams, 'absence' => AbsencesDataService::getCurrentAbsenceOfUser($this->_websoccer, $this->_db, $userId), 'badges' => $badges);
 }
 public static function computeUserInactivity(WebSoccer $websoccer, DbConnection $db, $userId)
 {
     $inactivity = self::getUserInactivity($websoccer, $db, $userId);
     $now = $websoccer->getNowAsTimestamp();
     $checkBoundary = $now - 24 * 3600;
     $updatecolumns = array();
     $user = UsersDataService::getUserById($websoccer, $db, $userId);
     // compute login-activity
     if ($inactivity["login_check"] < $checkBoundary) {
         $inactiveDays = round(($now - $user["lastonline"]) / (24 * 3600));
         $updatecolumns["login"] = min(100, round($inactiveDays * INACTIVITY_PER_DAY_LOGIN));
         $updatecolumns["login_check"] = $now;
         // update tactics activity
         $formationTable = $websoccer->getConfig("db_prefix") . "_aufstellung AS F";
         $formationTable .= " INNER JOIN " . $websoccer->getConfig("db_prefix") . "_verein AS T ON T.id = F.verein_id";
         $result = $db->querySelect("F.datum AS date", $formationTable, "T.user_id = %d", $userId);
         $formation = $result->fetch_array();
         $result->free();
         if ($formation) {
             $inactiveDays = round(($now - $formation["date"]) / (24 * 3600));
             $updatecolumns["aufstellung"] = min(100, round($inactiveDays * INACTIVITY_PER_DAY_TACTICS));
         }
     }
     // compute transfers-activity (check user's bids)
     if ($inactivity["transfer_check"] < $checkBoundary) {
         $bid = TransfermarketDataService::getLatestBidOfUser($websoccer, $db, $userId);
         $transferBenchmark = $user["registration_date"];
         if ($bid) {
             $transferBenchmark = $bid["date"];
         }
         $inactiveDays = round(($now - $transferBenchmark) / (24 * 3600));
         $updatecolumns["transfer"] = min(100, round($inactiveDays * INACTIVITY_PER_DAY_TRANSFERS));
         $updatecolumns["transfer_check"] = $now;
     }
     // update
     if (count($updatecolumns)) {
         $fromTable = $websoccer->getConfig("db_prefix") . "_user_inactivity";
         $db->queryUpdate($updatecolumns, $fromTable, "id = %d", $inactivity["id"]);
     }
 }
 /**
  * Debits specified amount from user's premium account (TAKING money).
  * Throws Exception if balance is not enough
  * 
  * @param WebSoccer $websoccer Application context.
  * @param DbConnection $db DB connection.
  * @param int $userId ID of user.
  * @param int $amount Positive amount to debit. If 0, no statement will be created.
  * @param string $subject action ID which triggered the statement.
  * @param array $data (otpional) Array of subject data.
  * @throws Exception if amount is negative, if premium credit is not enough or team could not be found.
  */
 public static function debitAmount(WebSoccer $websoccer, DbConnection $db, $userId, $amount, $subject, $data = null)
 {
     if ($amount == 0) {
         return;
     }
     $user = UsersDataService::getUserById($websoccer, $db, $userId);
     if (!isset($user['premium_balance'])) {
         throw new Exception('user not found: ' . $userId);
     }
     if ($amount < 0) {
         throw new Exception('amount illegal: ' . $amount);
     }
     // is balance enough?
     if ($user['premium_balance'] < $amount) {
         $i18n = I18n::getInstance($websoccer->getConfig('supported_languages'));
         throw new Exception($i18n->getMessage('premium_balance_notenough'));
     }
     $amount = 0 - $amount;
     self::createTransaction($websoccer, $db, $user, $userId, $amount, $subject, $data);
 }