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