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