baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / user / PKPUserSettingsDAO.inc.php
blob29443c799b112ea8ce007a8d64f6c5d0f0f9ba17
1 <?php
3 /**
4 * @file classes/user/PKPUserSettingsDAO.inc.php
6 * Copyright (c) 2003-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PKPUserSettingsDAO
10 * @ingroup user
11 * @see PKPUser
13 * @brief Operations for retrieving and modifying user settings.
16 // $Id: PKPUserSettingsDAO.inc.php,v 1.2 2009/08/18 23:44:08 asmecher Exp $
19 class PKPUserSettingsDAO extends DAO {
20 /**
21 * Retrieve a user setting value.
22 * @param $userId int
23 * @param $name
24 * @param $assocType int
25 * @param $assocId int
26 * @return mixed
28 function &getSetting($userId, $name, $assocType = null, $assocId = null) {
29 $result =& $this->retrieve(
30 'SELECT setting_value,
31 setting_type
32 FROM user_settings
33 WHERE user_id = ? AND
34 setting_name = ? AND
35 assoc_type = ? AND
36 assoc_id = ?',
37 array(
38 (int) $userId,
39 $name,
40 (int) $assocType,
41 (int) $assocId
45 if ($result->RecordCount() != 0) {
46 $row =& $result->getRowAssoc(false);
47 $returner = $this->convertFromDB($row['setting_value'], $row['setting_type']);
48 } else {
49 $returner = null;
52 return $returner;
55 /**
56 * Retrieve all users by setting name and value.
57 * @param $name string
58 * @param $value mixed
59 * @param $type string
60 * @param $assocType int
61 * @param $assocId int
62 * @return DAOResultFactory matching Users
64 function &getUsersBySetting($name, $value, $type = null, $assocType = null, $assocId = null) {
65 $userDao =& DAORegistry::getDAO('UserDAO');
67 $value = $this->convertToDB($value, $type);
68 $result =& $this->retrieve(
69 'SELECT u.*
70 FROM users u,
71 user_settings s
72 WHERE u.user_id = s.user_id AND
73 s.setting_name = ? AND
74 s.setting_value = ? AND
75 s.assoc_type = ? AND
76 s.assoc_id = ?',
77 array($name, $value, (int) $assocType, (int) $assocId)
80 $returner = new DAOResultFactory($result, $userDao, '_returnUserFromRow');
81 return $returner;
84 /**
85 * Retrieve all settings for a user by association info.
86 * @param $userId int
87 * @param $assocType int
88 * @param $assocId int
89 * @return array
91 function &getSettingsByAssoc($userId, $assocType = null, $assocId = null) {
92 $userSettings = array();
94 $result =& $this->retrieve(
95 'SELECT setting_name,
96 setting_value,
97 setting_type
98 FROM user_settings
99 WHERE user_id = ? AND
100 assoc_type = ?
101 AND assoc_id = ?',
102 array((int) $userId, (int) $assocType, (int) $assocId)
105 while (!$result->EOF) {
106 $row =& $result->getRowAssoc(false);
107 $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
108 $userSettings[$row['setting_name']] = $value;
109 $result->MoveNext();
111 $result->Close();
112 unset($result);
114 return $userSettings;
118 * Add/update a user setting.
119 * @param $userId int
120 * @param $name string
121 * @param $value mixed
122 * @param $type string data type of the setting. If omitted, type will be guessed
123 * @param $assocType int
124 * @param $assocId int
126 function updateSetting($userId, $name, $value, $type = null, $assocType = null, $assocId = null) {
127 $result = $this->retrieve(
128 'SELECT COUNT(*)
129 FROM user_settings
130 WHERE user_id = ? AND
131 setting_name = ?
132 AND assoc_type = ?
133 AND assoc_id = ?',
134 array((int) $userId, $name, (int) $assocType, (int) $assocId)
137 $value = $this->convertToDB($value, $type);
138 if ($result->fields[0] == 0) {
139 $returner = $this->update(
140 'INSERT INTO user_settings
141 (user_id, setting_name, assoc_type, assoc_id, setting_value, setting_type)
142 VALUES
143 (?, ?, ?, ?, ?, ?)',
144 array(
145 (int) $userId,
146 $name,
147 (int) $assocType,
148 (int) $assocId,
149 $value,
150 $type
153 } else {
154 $returner = $this->update(
155 'UPDATE user_settings
156 SET setting_value = ?,
157 setting_type = ?
158 WHERE user_id = ? AND
159 setting_name = ? AND
160 assoc_type = ?
161 AND assoc_id = ?',
162 array(
163 $value,
164 $type,
165 (int) $userId,
166 $name,
167 (int) $assocType,
168 (int) $assocId
173 $result->Close();
174 unset($result);
176 return $returner;
180 * Delete a user setting.
181 * @param $userId int
182 * @param $name string
183 * @param $assocType int
184 * @param $assocId int
186 function deleteSetting($userId, $name, $assocType = null, $assocId = null) {
187 return $this->update(
188 'DELETE FROM user_settings WHERE user_id = ? AND setting_name = ? AND assoc_type = ? AND assoc_id = ?',
189 array((int) $userId, $name, (int) $assocType, (int) $assocId)
194 * Delete all settings for a user.
195 * @param $userId int
197 function deleteSettings($userId) {
198 return $this->update(
199 'DELETE FROM user_settings WHERE user_id = ?', $userId