Deprecated call removed
[shipsimu.git] / application / ship-simu / main / personell / class_SimulatorPersonell.php
1 <?php
2 /**
3  * The general simulator personell class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class SimulatorPersonell extends BasePersonell {
25         // Personell list
26         private $personellList = null;
27
28         // A cache for lists
29         private $cacheList = null;
30
31         // A string for cached conditions
32         private $cacheCond = null;
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Magic wake-up method called when unserialize() is called. This is
46          * neccessary because in this case a personell does not need to know the
47          * min/max ages range and system classes. This would anyway use more RAM
48          * what is not required.
49          *
50          * @return      void
51          */
52         public function __wakeup () {
53                 // Tidy up a little
54                 $this->removePersonellList();
55                 $this->removeMinMaxAge();
56                 $this->removeCache();
57         }
58
59         /**
60          * Generate a specified amount of personell and return the prepared instance
61          *
62          * @param               $amountPersonell                Number of personell we shall
63          *                                                              generate
64          * @return      $personellInstance              An instance of this object with a
65          *                                                              list of personells
66          */
67         public final static function createSimulatorPersonell ($amountPersonell) {
68                 // Make sure only integer can pass
69                 $amountPersonell = (int) $amountPersonell;
70
71                 // Get a new instance
72                 $personellInstance = new SimulatorPersonell();
73
74                 // Debug message
75                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
76                         __CLASS__,
77                         __LINE__,
78                         $amountPersonell
79                 ));
80
81                 // Initialize the personell list
82                 $personellInstance->createPersonellList();
83
84                 // Create requested amount of personell
85                 for ($idx = 0; $idx < $amountPersonell; $idx++) {
86                         $personellInstance->addRandomPersonell();
87                 }
88
89                 // Debug message
90                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
91                         __CLASS__,
92                         __LINE__,
93                         $amountPersonell
94                 ));
95
96                 // Tidy up a little
97                 $personellInstance->removeGender();
98                 $personellInstance->removeNames();
99                 $personellInstance->removeBirthday();
100                 $personellInstance->removeSalary();
101                 $personellInstance->removeEmployed();
102                 $personellInstance->removeMarried();
103                 //$personellInstance->removeCache();
104
105                 // Instanz zurueckgeben
106                 return $personellInstance;
107         }
108
109         /**
110          * Create a SimulatorPersonell object by loading the specified personell
111          * list from an existing database backend
112          *
113          * @param       $idNumber                       The ID number (only right part) of the list
114          * @return      $personellInstance      An instance of this class
115          * @throws      InvalidIDFormatException        If the given id number
116          *                                                                              $idNumber is invalid
117          * @throws      MissingSimulatorIdException             If an ID number was not found
118          * @deprecated
119          */
120         public final static function createSimulatorPersonellByID ($idNumber) {
121                 // Get instance
122                 $personellInstance = new SimulatorPersonell(false);
123                 $personellInstance->makeDeprecated();
124         }
125
126         // Create personell list
127         public function createPersonellList () {
128                 // Is the list already created?
129                 if ($this->personelllList instanceof FrameworkArrayObject) {
130                         // Throw an exception
131                         throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
132                 } // END - if
133
134                 // Initialize the array
135                 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
136         }
137
138         // Remove the personell list
139         private final function removePersonellList () {
140                 unset($this->personellList);
141         }
142
143         // Add new personell object to our list
144         public function addRandomPersonell () {
145                 // Gender list...
146                 $genders = array("M", "F");
147
148                 // Create new personell members
149                 $personellInstance = new SimulatorPersonell();
150
151                 // Set a randomized gender
152                 $personellInstance->setGender($genders[mt_rand(0, 1)]);
153
154                 // Set a randomized birthday (maximum age required, see const MAX_AGE)
155                 $personellInstance->createBirthday();
156
157                 // Married? Same values means: married
158                 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
159
160                 // Tidy up a little
161                 $personellInstance->removePersonellList();
162                 $personellInstance->removeMinMaxAge();
163                 $personellInstance->removeCache();
164
165                 // Add new member to the list
166                 $this->personellList->append($personellInstance);
167         }
168
169         /**
170          * Get a specifyable list of our people, null or empty string will be ignored!
171          *
172          * @return      $cacheList      A list of cached personells
173          */
174         function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
175                 // Serialize the conditions for checking if we can take the cache
176                 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
177
178                 // The same (last) conditions?
179                 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
180                         if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
181                                 __CLASS__,
182                                 __LINE__
183                         ));
184
185                         // Return cached list
186                         return $this->cacheList;
187                 }
188
189                 // Output debug message
190                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
191                         __CLASS__,
192                         __LINE__
193                 ));
194
195                 // Remember the conditions
196                 $this->setCacheCond($serialized);
197
198                 // Create cached list
199                 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
200
201                 // Search all unemployed personells
202                 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
203                         // Element holen
204                         $el = $idx->current();
205
206                         // Check currenylt all single conditions (combined conditions are not yet supported)
207                         if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
208                                 // Add this one (employed status asked)
209                                 $this->cacheList->append($el);
210                         } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
211                                 // Add this one (marrital status asked)
212                                 $this->cacheList->append($el);
213                         } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
214                                 // Add this one (specified gender)
215                                 $this->cacheList->append($el);
216                         }
217                 }
218
219                 // Return the completed list
220                 return $this->cacheList;
221         }
222
223         /**
224          * Get amount of unemployed personell
225          *
226          * @return      $count  Amount of unemployed personell
227          */
228         public final function getAllUnemployed () {
229                 // Get a temporary list
230                 $list = $this->getSpecialPersonellList(false);
231
232                 // Anzahl zurueckliefern
233                 return $list->count();
234         }
235
236         /**
237          * Remove cache things
238          *
239          * @return      void
240          */
241         private function removeCache () {
242                 // Remove cache data
243                 unset($this->cacheList);
244                 unset($this->cacheCond);
245         }
246
247         /**
248          * Setter for cache list
249          *
250          * @param               $cacheList      The new cache list to set or null for initialization/reset
251          * @return      void
252          */
253         private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
254                 $this->cacheList = $cacheList;
255         }
256
257         /**
258          * Setter for cache conditions
259          *
260          * @param               $cacheCond      The new cache conditions to set
261          * @return      void
262          */
263         private final function setCacheCond ($cacheCond) {
264                 $this->cacheCond = (string) $cacheCond;
265         }
266
267         /**
268          * Reset cache list
269          *
270          * @return      void
271          */
272         public function resetCache () {
273                 $this->setAllCacheList(null);
274                 $this->setCacheCond("");
275         }
276
277         /**
278          * Getter for surname. If no surname is set then default surnames are set
279          * for male and female personells.
280          *
281          * @return      $surname        The personell' surname
282          */
283         public final function getSurname () {
284                 $surname = parent::getSurname();
285
286                 // Make sure every one has a surname...
287                 if (empty($surname)) {
288                         if ($this->isMale()) {
289                                 // Typical male name
290                                 $surname = "John";
291                         } else {
292                                 // Typical female name
293                                 $surname = "Jennifer";
294                         }
295
296                         // Set typical family name
297                         parent::setFamily("Smith");
298                 } // END - if
299
300                 // Return surname
301                 return $surname;
302         }
303
304         /**
305          * Getter for personell list
306          *
307          * @return      $personellList          The list of all personells
308          */
309         public final function getPersonellList () {
310                 return $this->personellList;
311         }
312
313         /**
314          * Loads the mostly pre-cached personell list
315          *
316          * @param       $idNumber       The ID number we shall use for looking up
317          *                                              the right data.
318          * @return      void
319          * @deprecated
320          */
321         public function loadPersonellList ($idNumber) {
322                 $this->makeDeprecated();
323         }
324 }
325
326 // [EOF]
327 ?>