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