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