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 <<<<<<< HEAD:framework/main/classes/points/class_UserPoints.php
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/points/class_UserPoints.php
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates an instance of this points class
58 * @param $userInstance An instance of a user class
59 * @return $pointsInstance An instance of this class
61 public static final function createUserPoints (ManageableAccount $userInstance) {
63 $pointsInstance = new UserPoints();
66 $pointsInstance->setUserInstance($userInstance);
68 // Get a critieria instance
69 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
71 // Add search criteria
72 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
73 $searchInstance->setLimit(1);
75 // Get a wrapper instance
76 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
79 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
81 // Advance to first entry by default
82 $resultInstance->next();
84 // Set it in this instance
85 $pointsInstance->setResultInstance($resultInstance);
88 return $pointsInstance;
94 * @param $amount Amount of points to store
97 public final function setAmount ($amount) {
98 $this->amount = (float) $amount;
104 * @return $amount Amount of points to store
106 public final function getAmount () {
107 return $this->amount;
111 * Checks whether the user has the required amount of points left for the specified action
113 * @param $action The action or configuration entry plus prefix the user wants to perform
114 * @return $hasRequired Whether the user has the required points
115 * @todo Finish loading part of points
117 public function ifUserHasRequiredPoints ($action) {
118 // Default is that everyone is poor... ;-)
119 $hasRequired = false;
121 // Get the required points entry
122 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
125 $this->getResultInstance()->rewind();
127 // Do we have an entry?
128 if ($this->getResultInstance()->next()) {
130 $currEntry = $this->getResultInstance()->current();
132 // Has he enought points?
133 $hasRequired = ($currEntry['points'] >= $requiredPoints);
141 * "Books" the given points amount on the current user's account
143 * @param $amount Amount of points we shall book
146 public function bookPointsDirectly ($amount) {
148 $this->getResultInstance()->rewind();
150 // Do we have an entry?
151 if ($this->getResultInstance()->next()) {
153 $entry = $this->getResultInstance()->current();
156 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
158 // Now get another criteria
159 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
161 // And add our both entries
162 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
164 // Add the search criteria for searching for the right entry
165 $updateInstance->setSearchInstance($searchInstance);
167 // Set wrapper class name
168 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
170 // Remember the update in database result
171 $this->getResultInstance()->add2UpdateQueue($updateInstance);
173 // Set the amount in class
174 $this->setAmount($amount);
176 // Create the new entry
177 $wrapperInstance->insertUserPoints($this);
182 * Adds registration elements to a given dataset instance
184 * @param $criteriaInstance An instance of a StoreableCriteria class
186 * @todo $requestInstance is currently unused
188 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
190 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
193 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());