3 namespace CoreFramework\User\Point;
5 // Import framework stuff
6 use CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Object\BaseFrameworkSystem;
9 use CoreFramework\Registry\Registerable;
10 use CoreFramework\Request\Requestable;
13 * A class for handling user points which can be real or Internet currency
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this points class
53 * @param $userInstance An instance of a user class
54 * @return $pointsInstance An instance of this class
56 public static final function createUserPoints (ManageableAccount $userInstance) {
58 $pointsInstance = new UserPoints();
61 $pointsInstance->setUserInstance($userInstance);
63 // Get a critieria instance
64 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
66 // Add search criteria
67 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
68 $searchInstance->setLimit(1);
70 // Get a wrapper instance
71 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
74 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
76 // Advance to first entry by default
77 $resultInstance->next();
79 // Set it in this instance
80 $pointsInstance->setResultInstance($resultInstance);
83 return $pointsInstance;
89 * @param $amount Amount of points to store
92 public final function setAmount ($amount) {
93 $this->amount = (float) $amount;
99 * @return $amount Amount of points to store
101 public final function getAmount () {
102 return $this->amount;
106 * Checks whether the user has the required amount of points left for the specified action
108 * @param $action The action or configuration entry plus prefix the user wants to perform
109 * @return $hasRequired Whether the user has the required points
110 * @todo Finish loading part of points
112 public function ifUserHasRequiredPoints ($action) {
113 // Default is that everyone is poor... ;-)
114 $hasRequired = FALSE;
116 // Get the required points entry
117 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
120 $this->getResultInstance()->rewind();
122 // Do we have an entry?
123 if ($this->getResultInstance()->next()) {
125 $currEntry = $this->getResultInstance()->current();
127 // Has he enought points?
128 $hasRequired = ($currEntry['points'] >= $requiredPoints);
136 * "Books" the given points amount on the current user's account
138 * @param $amount Amount of points we shall book
141 public function bookPointsDirectly ($amount) {
143 $this->getResultInstance()->rewind();
145 // Do we have an entry?
146 if ($this->getResultInstance()->next()) {
148 $entry = $this->getResultInstance()->current();
151 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
153 // Now get another criteria
154 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
156 // And add our both entries
157 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
159 // Add the search criteria for searching for the right entry
160 $updateInstance->setSearchInstance($searchInstance);
162 // Set wrapper class name
163 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
165 // Remember the update in database result
166 $this->getResultInstance()->add2UpdateQueue($updateInstance);
168 // Set the amount in class
169 $this->setAmount($amount);
171 // Create the new entry
172 $wrapperInstance->insertUserPoints($this);
177 * Adds registration elements to a given dataset instance
179 * @param $criteriaInstance An instance of a StoreableCriteria class
180 * @param $requestInstance An instance of a Requestable class
182 * @todo $requestInstance is currently unused
184 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
186 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
189 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());