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