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