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