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\Request\Requestable;
12 use Org\Mxchange\CoreFramework\User\ManageableAccount;
15 * A class for handling user points which can be real or Internet currency
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this points class
55 * @param $userInstance An instance of a user class
56 * @return $pointsInstance An instance of this class
58 public static final function createUserPoints (ManageableAccount $userInstance) {
60 $pointsInstance = new UserPoints();
63 $pointsInstance->setUserInstance($userInstance);
65 // Get a critieria instance
66 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
68 // Add search criteria
69 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
70 $searchInstance->setLimit(1);
72 // Get a wrapper instance
73 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
76 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
78 // Advance to first entry by default
79 $resultInstance->next();
81 // Set it in this instance
82 $pointsInstance->setResultInstance($resultInstance);
85 return $pointsInstance;
91 * @param $amount Amount of points to store
94 public final function setAmount ($amount) {
95 $this->amount = (float) $amount;
101 * @return $amount Amount of points to store
103 public final function getAmount () {
104 return $this->amount;
108 * Checks whether the user has the required amount of points left for the specified action
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
114 public function ifUserHasRequiredPoints ($action) {
115 // Default is that everyone is poor... ;-)
116 $hasRequired = false;
118 // Get the required points entry
119 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
122 $this->getResultInstance()->rewind();
124 // Do we have an entry?
125 if ($this->getResultInstance()->next()) {
127 $currEntry = $this->getResultInstance()->current();
129 // Has he enought points?
130 $hasRequired = ($currEntry['points'] >= $requiredPoints);
138 * "Books" the given points amount on the current user's account
140 * @param $amount Amount of points we shall book
143 public function bookPointsDirectly ($amount) {
145 $this->getResultInstance()->rewind();
147 // Do we have an entry?
148 if ($this->getResultInstance()->next()) {
150 $entry = $this->getResultInstance()->current();
153 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
155 // Now get another criteria
156 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
158 // And add our both entries
159 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
161 // Add the search criteria for searching for the right entry
162 $updateInstance->setSearchInstance($searchInstance);
164 // Set wrapper class name
165 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
167 // Remember the update in database result
168 $this->getResultInstance()->add2UpdateQueue($updateInstance);
170 // Set the amount in class
171 $this->setAmount($amount);
173 // Create the new entry
174 $wrapperInstance->insertUserPoints($this);
179 * Adds registration elements to a given dataset instance
181 * @param $criteriaInstance An instance of a StoreableCriteria class
182 * @param $requestInstance An instance of a Requestable class
184 * @todo $requestInstance is currently unused
186 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
188 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
191 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());