3 * A special member class for Ship-Simu
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class ShipSimuBaseUser extends BaseUser implements Registerable, Updateable {
26 * Protected constructor
28 * @param $className Name of the class
31 protected function __construct ($className) {
32 // Call parent constructor
33 parent::__construct($className);
37 * Initializes the bank instance
39 * @return $bankInstance A bank instance wether just created or from registry
41 protected function initBankInstance () {
46 // Get a money bank instance from registry
47 $bankInstance = Registry::getRegistry()->getInstance('money_bank');
48 } catch (NullPointerException $e) {
49 // Instance not found in registry
50 // @TODO We should log this exception later
54 if (is_null($bankInstance)) {
55 // Then create a new one
56 $bankInstance = ObjectFactory::createObjectByConfiguredName('bank_class', array($this));
58 // Store it in registry
59 Registry::getRegistry()->addInstance('money_bank', $bankInstance);
62 // Return the instance
67 * Initializes the government instance
69 * @return $governmentInstance A government instance
71 protected function initGovernmentInstance () {
73 $governmentInstance = null;
76 // First get a government instance from registry
77 $governmentInstance = Registry::getRegistry()->getInstance('government');
78 } catch (NullPointerException $e) {
79 // Instance not found in registry
80 // @TODO We should log this exception later
84 if (is_null($governmentInstance)) {
85 // Then create a new one
86 $governmentInstance = ObjectFactory::createObjectByConfiguredName('government_class', array($this));
88 // Store it in registry
89 Registry::getRegistry()->addInstance('government', $governmentInstance);
92 // Return the prepared instance
93 return $governmentInstance;
97 * Checks wether the user has reached maximum allowed companies to found
99 * @return $reached Wether the user has reached maximum allowed companies to found
101 public function ifUserCreatedMaximumAllowedCompanies () {
102 // Get max allowed companies to found
103 $maxFound = $this->getConfigInstance()->getConfigEntry('max_allowed_companies_found');
105 // Now get a search criteria and set the user's name as criteria
106 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
107 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
108 $searchInstance->setLimit($maxFound);
110 // Get a company wrapper
111 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
113 // Do the count-select by criteria
114 $totalRows = $wrapperInstance->doSelectCountByCriteria($searchInstance);
116 // Does the user have reached maximum?
117 $reached = ($totalRows >= $maxFound);
124 * Checks wether the user has the required amount of points left for the specified action
126 * @param $action The action or configuration entry plus prefix the user wants to perform
127 * @return $hasRequired Wether the user has the required points
129 public function ifUserHasRequiredPoints ($action) {
130 // Default is that everyone is poor... ;-)
131 $hasRequired = false;
134 $pointsInstance = null;
137 // Get a points instance from registry
138 $pointsInstance = Registry::getRegistry()->getInstance('points');
139 } catch (NullPointerException $e) {
140 // Instance not found in registry
141 // @TODO We should log this exception later
144 // Is there an instance?
145 if (is_null($pointsInstance)) {
147 $pointsInstance = ObjectFactory::createObjectByConfiguredName('user_points_class', array($this));
149 // And store it in registry
150 Registry::getRegistry()->addInstance('points', $pointsInstance);
153 // Just forward this request to the user points class
154 $hasRequired = $pointsInstance->ifUserHasRequiredPoints($action);
161 * Determines if government can still pay a "virtual training course" in general
163 * @return $ifGovHelps Wether if government helps the user with a virtual training course
165 public function ifGovernmentPaysTraining () {
166 // By default they want to help.
169 // Initialize government instance
170 $governmentInstance = $this->initGovernmentInstance();
172 // Then ask the government if they want to pay a "startup help" to the user
173 $ifGovHelps = ($governmentInstance->ifGovernmentAlreadyPayedTraining());
175 // Return result here
180 * Determines if government can still pay a "startup help" to the user
182 * @return $ifGovHelps Wether if government helps the user with some startup money
184 public function ifGovernmentPaysStartupHelp () {
185 // By default they want to help.
188 // Initialize government instance
189 $governmentInstance = $this->initGovernmentInstance();
191 // Then ask the government if they want to pay a "startup help" to the user
192 $ifGovHelps = ($governmentInstance->ifGovernmentPayedMaxmimumStartupHelp());
194 // Return result here
199 * Checks wether the user can take points from the money bank
201 * @return $bankLends Wether the money bank is able to lend money
202 * @todo Need to implement MoneyBank::ifBankLendsMoreMoney()
204 public function ifUserAllowedTakeCreditsFromMoneyBank () {
205 // Per default the money bank cannot pay
208 // Initialize bank instance
209 $bankInstance->initBankInstance();
211 // Does the money bank lend more money?
212 $bankLends = ($bankInstance->ifBankLendsMoreMoney());
219 * Checks wether the user has maximum credits with the money bank. This
220 * should be done seperately from checking if the user is allowed to take
221 * credits from the bank.
223 * @return $hasMaxCredits Wether the user has maximum credits with the bank
224 * @todo Need to check the bank if they can lend more money
226 public function ifUserHasMaximumCreditsWithMoneyBank () {
227 // For default he can still get money
228 $hasMaxCredits = false;
230 // Initialize the bank instance
231 $bankInstance = $this->initBankInstance();
233 // Now check if the user has maximum credits
234 $hasMaxCredits = ($bankInstance->ifUserHasMaxCredits());
237 return $hasMaxCredits;
241 * Checks wether the money bank has opened
243 * @return $hasOpened Wether the money bank has opened
245 public function ifMoneyBankHasOpened () {
246 // Default is not opened
249 // Initialize the bank instance
250 $bankInstance = $this->initBankInstance();
252 // Has this bank opened?
253 $hasOpened = ($bankInstance->ifMoneyBankHasOpened());