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