Continued:
[core.git] / inc / main / classes / database / frontend / class_UserPointsDatabaseWrapper.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Database\Wrapper\Points;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Registerable;
8
9 /**
10  * A database wrapper for user points classes
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
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
17  *
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.
22  *
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.
27  *
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/>.
30  */
31 class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper, Registerable {
32         /**
33          * Constants for database table names
34          */
35         const DB_TABLE_USER_POINTS = 'user_points';
36
37         /**
38          * Name of the user->points column
39          */
40         const DB_COLUMN_POINTS_UID = 'points_uid';
41
42         /**
43          * Name of the points column
44          */
45         const DB_COLUMN_POINTS = 'points';
46
47         /**
48          * Protected constructor
49          *
50          * @return      void
51          */
52         protected function __construct () {
53                 // Call parent constructor
54                 parent::__construct(__CLASS__);
55         }
56
57         /**
58          * Creates an instance of this database wrapper by a provided user class
59          *
60          * @return      $wrapperInstance        An instance of the created wrapper class
61          */
62         public static final function createUserPointsDatabaseWrapper () {
63                 // Get a new instance
64                 $wrapperInstance = new UserPointsDatabaseWrapper();
65
66                 // Set (primary!) table name
67                 $wrapperInstance->setTableName(self::DB_TABLE_USER_POINTS);
68
69                 // Return the instance
70                 return $wrapperInstance;
71         }
72
73         /**
74          * Inserts the given points for the given user in the database
75          *
76          * @param       $pointsInstance         An instance of a user class
77          * @return      void
78          */
79         public function insertUserPoints (BookablePoints $pointsInstance) {
80                 // Generate a data set for the request
81                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
82
83                 // Set the primary key
84                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
85
86                 // Add registration elements to the dataset
87                 $pointsInstance->addElementsToDataSet($dataSetInstance, NULL);
88
89                 // "Insert" this request instance completely into the database
90                 $this->queryInsertDataSet($dataSetInstance);
91         }
92
93         /**
94          * Updates an user database entry with given result
95          *
96          * @param       $resultInstance         An instance of a Updateable database result
97          * @return      void
98          */
99         public function doUpdateByResult (UpdateableResult $resultInstance) {
100                 // Generate a data set object
101                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
102
103                 // Add all update criteria to the database set
104                 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
105
106                 // Add seach criteria
107                 $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance());
108
109                 // Set the primary key
110                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
111
112                 // "Update" this request with the database
113                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
114         }
115
116 }