/** * (non-PHPdoc) * @see IModel::getTemplateParameters() */ public function getTemplateParameters() { $teamId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db); // last training executions $lastExecution = TrainingDataService::getLatestTrainingExecutionTime($this->_websoccer, $this->_db, $teamId); $unitsCount = TrainingDataService::countRemainingTrainingUnits($this->_websoccer, $this->_db, $teamId); $paginator = null; $trainers = null; // has valid training unit? $training_unit = TrainingDataService::getValidTrainingUnit($this->_websoccer, $this->_db, $teamId); if (!isset($training_unit["id"])) { // get trainers $count = TrainingDataService::countTrainers($this->_websoccer, $this->_db); $eps = $this->_websoccer->getConfig("entries_per_page"); $paginator = new Paginator($count, $eps, $this->_websoccer); if ($count > 0) { $trainers = TrainingDataService::getTrainers($this->_websoccer, $this->_db, $paginator->getFirstIndex(), $eps); } } else { $training_unit["trainer"] = TrainingDataService::getTrainerById($this->_websoccer, $this->_db, $training_unit["trainer_id"]); } // collect effects of executed training unit $trainingEffects = array(); $contextParameters = $this->_websoccer->getContextParameters(); if (isset($contextParameters["trainingEffects"])) { $trainingEffects = $contextParameters["trainingEffects"]; } return array("unitsCount" => $unitsCount, "lastExecution" => $lastExecution, "training_unit" => $training_unit, "trainers" => $trainers, "paginator" => $paginator, "trainingEffects" => $trainingEffects); }
/** * (non-PHPdoc) * @see IActionController::executeAction() */ public function executeAction($parameters) { $user = $this->_websoccer->getUser(); $teamId = $user->getClubId($this->_websoccer, $this->_db); if ($teamId < 1) { throw new Exception($this->_i18n->getMessage("feature_requires_team")); } if (TrainingDataService::countRemainingTrainingUnits($this->_websoccer, $this->_db, $teamId)) { throw new Exception($this->_i18n->getMessage("training_choose_trainer_err_existing_units")); } // trainer info $trainer = TrainingDataService::getTrainerById($this->_websoccer, $this->_db, $parameters["id"]); if (!isset($trainer["id"])) { throw new Exception("invalid ID"); } // can team afford it? $numberOfUnits = (int) $parameters["units"]; $totalCosts = $numberOfUnits * $trainer["salary"]; $teamInfo = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $teamId); if ($teamInfo["team_budget"] <= $totalCosts) { throw new Exception($this->_i18n->getMessage("training_choose_trainer_err_too_expensive")); } // try to debit premium fee if ($trainer['premiumfee']) { PremiumDataService::debitAmount($this->_websoccer, $this->_db, $user->id, $trainer['premiumfee'], "choose-trainer"); } // debit money BankAccountDataService::debitAmount($this->_websoccer, $this->_db, $teamId, $totalCosts, "training_trainer_salary_subject", $trainer["name"]); // create new units $columns["team_id"] = $teamId; $columns["trainer_id"] = $trainer["id"]; $fromTable = $this->_websoccer->getConfig("db_prefix") . "_training_unit"; for ($unitNo = 1; $unitNo <= $numberOfUnits; $unitNo++) { $this->_db->queryInsert($columns, $fromTable); } // success message $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("saved_message_title"), "")); // redirect to training overview return "training"; }