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