3 namespace Org\Mxchange\CoreFramework\User\Points;
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;
14 * A class for handling user points which can be real or Internet currency
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this points class
54 * @param $userInstance An instance of a user class
55 * @return $pointsInstance An instance of this class
57 public static final function createUserPoints (ManageableAccount $userInstance) {
59 $pointsInstance = new UserPoints();
62 $pointsInstance->setUserInstance($userInstance);
64 // Get a critieria instance
65 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
67 // Add search criteria
68 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
69 $searchInstance->setLimit(1);
71 // Get a wrapper instance
72 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
75 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
77 // Advance to first entry by default
78 $resultInstance->next();
80 // Set it in this instance
81 $pointsInstance->setResultInstance($resultInstance);
84 return $pointsInstance;
90 * @param $amount Amount of points to store
93 public final function setAmount ($amount) {
94 $this->amount = (float) $amount;
100 * @return $amount Amount of points to store
102 public final function getAmount () {
103 return $this->amount;
107 * Checks whether the user has the required amount of points left for the specified action
109 * @param $action The action or configuration entry plus prefix the user wants to perform
110 * @return $hasRequired Whether the user has the required points
111 * @todo Finish loading part of points
113 public function ifUserHasRequiredPoints ($action) {
114 // Default is that everyone is poor... ;-)
115 $hasRequired = false;
117 // Get the required points entry
118 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
121 $this->getResultInstance()->rewind();
123 // Do we have an entry?
124 if ($this->getResultInstance()->next()) {
126 $currEntry = $this->getResultInstance()->current();
128 // Has he enought points?
129 $hasRequired = ($currEntry['points'] >= $requiredPoints);
137 * "Books" the given points amount on the current user's account
139 * @param $amount Amount of points we shall book
142 public function bookPointsDirectly ($amount) {
144 $this->getResultInstance()->rewind();
146 // Do we have an entry?
147 if ($this->getResultInstance()->next()) {
149 $entry = $this->getResultInstance()->current();
152 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
154 // Now get another criteria
155 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
157 // And add our both entries
158 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
160 // Add the search criteria for searching for the right entry
161 $updateInstance->setSearchInstance($searchInstance);
163 // Set wrapper class name
164 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
166 // Remember the update in database result
167 $this->getResultInstance()->add2UpdateQueue($updateInstance);
169 // Set the amount in class
170 $this->setAmount($amount);
172 // Create the new entry
173 $wrapperInstance->insertUserPoints($this);
178 * Adds registration elements to a given dataset instance
180 * @param $criteriaInstance An instance of a StoreableCriteria class
182 * @todo $requestInstance is currently unused
184 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
186 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
189 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());