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