update copyright as changes will happen this year
[core.git] / inc / main / classes / database / frontend / class_UserPointsDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for user points classes
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookablePointsWrapper, Registerable {
25         /**
26          * Constants for database table names
27          */
28         const DB_TABLE_USER_POINTS = 'user_points';
29
30         /**
31          * Name of the user->points column
32          */
33         const DB_COLUMN_POINTS_UID = 'points_uid';
34
35         /**
36          * Name of the points column
37          */
38         const DB_COLUMN_POINTS = 'points';
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this database wrapper by a provided user class
52          *
53          * @return      $wrapperInstance        An instance of the created wrapper class
54          */
55         public static final function createUserPointsDatabaseWrapper () {
56                 // Get a new instance
57                 $wrapperInstance = new UserPointsDatabaseWrapper();
58
59                 // Set (primary!) table name
60                 $wrapperInstance->setTableName(self::DB_TABLE_USER_POINTS);
61
62                 // Return the instance
63                 return $wrapperInstance;
64         }
65
66         /**
67          * Inserts the given points for the given user in the database
68          *
69          * @param       $pointsInstance         An instance of a user class
70          * @return      void
71          */
72         public function insertUserPoints (BookablePoints $pointsInstance) {
73                 // Generate a data set for the request
74                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
75
76                 // Set the primary key
77                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
78
79                 // Add registration elements to the dataset
80                 $pointsInstance->addElementsToDataSet($dataSetInstance, NULL);
81
82                 // "Insert" this request instance completely into the database
83                 $this->queryInsertDataSet($dataSetInstance);
84         }
85
86         /**
87          * Updates an user database entry with given result
88          *
89          * @param       $resultInstance         An instance of a Updateable database result
90          * @return      void
91          */
92         public function doUpdateByResult (UpdateableResult $resultInstance) {
93                 // Generate a data set object
94                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER_POINTS));
95
96                 // Add all update criteria to the database set
97                 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
98
99                 // Add seach criteria
100                 $dataSetInstance->setSearchInstance($resultInstance->getUpdateInstance()->getSearchInstance());
101
102                 // Set the primary key
103                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID);
104
105                 // "Update" this request with the database
106                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
107         }
108 }
109
110 // [EOF]
111 ?>