Continued:
[core.git] / inc / main / classes / points / class_UserPoints.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\User\Point;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
9 use CoreFramework\Request\Requestable;
10
11 /**
12  * A class for handling user points which can be real or Internet currency
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
34         /**
35          * Amount of points
36          */
37         private $amount = 0;
38
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of this points class
51          *
52          * @param       $userInstance           An instance of a user class
53          * @return      $pointsInstance         An instance of this class
54          */
55         public static final function createUserPoints (ManageableAccount $userInstance) {
56                 // Get a new instance
57                 $pointsInstance = new UserPoints();
58
59                 // Set user instance
60                 $pointsInstance->setUserInstance($userInstance);
61
62                 // Get a critieria instance
63                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
64
65                 // Add search criteria
66                 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
67                 $searchInstance->setLimit(1);
68
69                 // Get a wrapper instance
70                 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
71
72                 // Get result back
73                 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
74
75                 // Advance to first entry by default
76                 $resultInstance->next();
77
78                 // Set it in this instance
79                 $pointsInstance->setResultInstance($resultInstance);
80
81                 // Return instance
82                 return $pointsInstance;
83         }
84
85         /**
86          * Setter for amount
87          *
88          * @param       $amount         Amount of points to store
89          * @return      void
90          */
91         public final function setAmount ($amount) {
92                 $this->amount = (float) $amount;
93         }
94
95         /**
96          * Getter for amount
97          *
98          * @return      $amount         Amount of points to store
99          */
100         public final function getAmount () {
101                 return $this->amount;
102         }
103
104         /**
105          * Checks whether the user has the required amount of points left for the specified action
106          *
107          * @param       $action                 The action or configuration entry plus prefix the user wants to perform
108          * @return      $hasRequired    Whether the user has the required points
109          * @todo        Finish loading part of points
110          */
111         public function ifUserHasRequiredPoints ($action) {
112                 // Default is that everyone is poor... ;-)
113                 $hasRequired = FALSE;
114
115                 // Get the required points entry
116                 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
117
118                 // Rewind always
119                 $this->getResultInstance()->rewind();
120
121                 // Do we have an entry?
122                 if ($this->getResultInstance()->next()) {
123                         // Get the entry
124                         $currEntry = $this->getResultInstance()->current();
125
126                         // Has he enought points?
127                         $hasRequired = ($currEntry['points'] >= $requiredPoints);
128                 } // END - if
129
130                 // Return the result
131                 return $hasRequired;
132         }
133
134         /**
135          * "Books" the given points amount on the current user's account
136          *
137          * @param       $amount         Amount of points we shall book
138          * @return      void
139          */
140         public function bookPointsDirectly ($amount) {
141                 // Rewind always
142                 $this->getResultInstance()->rewind();
143
144                 // Do we have an entry?
145                 if ($this->getResultInstance()->next()) {
146                         // Get the entry
147                         $entry = $this->getResultInstance()->current();
148
149                         // Add the points
150                         $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
151
152                         // Now get another criteria
153                         $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
154
155                         // And add our both entries
156                         $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
157
158                         // Add the search criteria for searching for the right entry
159                         $updateInstance->setSearchInstance($searchInstance);
160
161                         // Set wrapper class name
162                         $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
163
164                         // Remember the update in database result
165                         $this->getResultInstance()->add2UpdateQueue($updateInstance);
166                 } else {
167                         // Set the amount in class
168                         $this->setAmount($amount);
169
170                         // Create the new entry
171                         $wrapperInstance->insertUserPoints($this);
172                 }
173         }
174
175         /**
176          * Adds registration elements to a given dataset instance
177          *
178          * @param       $criteriaInstance       An instance of a StoreableCriteria class
179          * @param       $requestInstance        An instance of a Requestable class
180          * @return      void
181          * @todo        $requestInstance is currently unused
182          */
183         public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
184                 // Add user id
185                 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
186
187                 // Add amount
188                 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());
189         }
190
191 }