3 namespace CoreFramework\User\Point;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A class for handling user points which can be real or Internet currency
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this points class
49 * @param $userInstance An instance of a user class
50 * @return $pointsInstance An instance of this class
52 public static final function createUserPoints (ManageableAccount $userInstance) {
54 $pointsInstance = new UserPoints();
57 $pointsInstance->setUserInstance($userInstance);
59 // Get a critieria instance
60 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
62 // Add search criteria
63 $searchInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
64 $searchInstance->setLimit(1);
66 // Get a wrapper instance
67 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('user_points_db_wrapper_class');
70 $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance);
72 // Advance to first entry by default
73 $resultInstance->next();
75 // Set it in this instance
76 $pointsInstance->setResultInstance($resultInstance);
79 return $pointsInstance;
85 * @param $amount Amount of points to store
88 public final function setAmount ($amount) {
89 $this->amount = (float) $amount;
95 * @return $amount Amount of points to store
97 public final function getAmount () {
102 * Checks whether the user has the required amount of points left for the specified action
104 * @param $action The action or configuration entry plus prefix the user wants to perform
105 * @return $hasRequired Whether the user has the required points
106 * @todo Finish loading part of points
108 public function ifUserHasRequiredPoints ($action) {
109 // Default is that everyone is poor... ;-)
110 $hasRequired = FALSE;
112 // Get the required points entry
113 $requiredPoints = $this->getConfigInstance()->getConfigEntry($action . '_action_points');
116 $this->getResultInstance()->rewind();
118 // Do we have an entry?
119 if ($this->getResultInstance()->next()) {
121 $currEntry = $this->getResultInstance()->current();
123 // Has he enought points?
124 $hasRequired = ($currEntry['points'] >= $requiredPoints);
132 * "Books" the given points amount on the current user's account
134 * @param $amount Amount of points we shall book
137 public function bookPointsDirectly ($amount) {
139 $this->getResultInstance()->rewind();
141 // Do we have an entry?
142 if ($this->getResultInstance()->next()) {
144 $entry = $this->getResultInstance()->current();
147 $amount += $entry[UserPointsDatabaseWrapper::DB_COLUMN_POINTS];
149 // Now get another criteria
150 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
152 // And add our both entries
153 $updateInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $amount);
155 // Add the search criteria for searching for the right entry
156 $updateInstance->setSearchInstance($searchInstance);
158 // Set wrapper class name
159 $updateInstance->setWrapperConfigEntry('user_points_db_wrapper_class');
161 // Remember the update in database result
162 $this->getResultInstance()->add2UpdateQueue($updateInstance);
164 // Set the amount in class
165 $this->setAmount($amount);
167 // Create the new entry
168 $wrapperInstance->insertUserPoints($this);
173 * Adds registration elements to a given dataset instance
175 * @param $criteriaInstance An instance of a StoreableCriteria class
176 * @param $requestInstance An instance of a Requestable class
179 public function addElementsToDataSet (StoreableCriteria $criteriaInstance, Requestable $requestInstance = NULL) {
181 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
184 $criteriaInstance->addCriteria(UserPointsDatabaseWrapper::DB_COLUMN_POINTS, $this->getAmount());