3 namespace CoreFramework\User\Point;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Registerable;
11 * A class for handling user points which can be real or Internet currency
13 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
32 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this points class
51 * @param $userInstance An instance of a user class
52 * @return $pointsInstance An instance of this class
54 public static final function createUserPoints (ManageableAccount $userInstance) {
56 $pointsInstance = new UserPoints();
59 $pointsInstance->setUserInstance($userInstance);
61 // Get a critieria instance
62 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
64 // Add search criteria
65 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
66 $searchInstance->setLimit(1);
68 // Get a wrapper instance
69 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
72 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
74 // Advance to first entry by default
75 $resultInstance->next();
77 // Set it in this instance
78 $pointsInstance->setResultInstance($resultInstance);
81 return $pointsInstance;
87 * @param $amount Amount of points to store
90 public final function setAmount ($amount) {
91 $this->amount = (float) $amount;
97 * @return $amount Amount of points to store
99 public final function getAmount () {
100 return $this->amount;
104 * Checks whether the user has the required amount of points left for the specified action
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
110 public function ifUserHasRequiredPoints ($action) {
111 // Default is that everyone is poor... ;-)
112 $hasRequired = FALSE;
114 // Get the required points entry
115 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
118 $this->getResultInstance()->rewind();
120 // Do we have an entry?
121 if ($this->getResultInstance()->next()) {
123 $currEntry = $this->getResultInstance()->current();
125 // Has he enought points?
126 $hasRequired = ($currEntry['points'] >= $requiredPoints);
134 * "Books" the given points amount on the current user's account
136 * @param $amount Amount of points we shall book
139 public function bookPointsDirectly ($amount) {
141 $this->getResultInstance()->rewind();
143 // Do we have an entry?
144 if ($this->getResultInstance()->next()) {
146 $entry = $this->getResultInstance()->current();
149 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
151 // Now get another criteria
152 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
154 // And add our both entries
155 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
157 // Add the search criteria for searching for the right entry
158 $updateInstance->setSearchInstance($searchInstance);
160 // Set wrapper class name
161 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
163 // Remember the update in database result
164 $this->getResultInstance()->add2UpdateQueue($updateInstance);
166 // Set the amount in class
167 $this->setAmount($amount);
169 // Create the new entry
170 $wrapperInstance->insertUserPoints($this);
175 * Adds registration elements to a given dataset instance
177 * @param $criteriaInstance An instance of a StoreableCriteria class
178 * @param $requestInstance An instance of a Requestable class
181 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
183 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
186 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());