5d261360a5313738f161e55d3b84d7da191ba537
[atutor.git] / mods / wiki / plugins / auth-liveuser / pref_liveuser.php
1 <?php
2
3 /**
4  * Copyright (c) 2003, The Burgiss Group, LLC
5  * This source code is part of eWiki LiveUser Plugin.
6  *
7  * eWiki LiveUser Plugin is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * eWiki LiveUser Plugin is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with Wiki LiveUser Plugin; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /**
23  * ewiki: preferences api for liveuser plugin
24  *
25  * @author andy fundinger <afundinger@burgiss.com>
26  * @author alex wan <alex@burgiss.com>
27  * @author jeremy mikola <jmikola@arsjerm.net>
28  *
29  * This interface provides functions to save and retrieve user preferences, as
30  * well as to configure available preference fields.
31  */
32
33 require_once(dirname(__FILE__).'/auth_liveuser.php');
34 require_once(dirname(__FILE__).'/liveuser_aux.php');
35
36 $ewiki_plugins['page']['ChangePrefs'] = 'ewiki_page_liveuser_chprefs';
37
38 /**
39  * changes user preferences based on form input
40  *
41  * @param mixed id
42  * @param mixed data
43  * @return mixed
44  */
45 function ewiki_page_liveuser_chprefs($id, $data)
46
47     global $liveuser, $liveuserDB;
48     
49     // if form was submitted, write 
50     if (isset($_REQUEST['submit_prefs'])) {
51         return ewiki_t('CHPWFORM');
52     }
53         
54     ob_start();
55     
56     echo ewiki_make_title($id, $id, 2);
57     
58     $results = $liveuserDB->getAll('SELECT * FROM '.LW_PREFIX.'_prefs_fields');
59     
60     foreach ($results as $result) {
61         if (isset($_REQUEST[$result['field_name']])) {
62             liveuser_pref_setPref($liveuser->getHandle(), $result['field_name'], $_REQUEST[$result['field_name']]);
63         }
64     }
65
66     echo '<form action="" method="post"><table border="1">';    
67     
68     foreach ($results as $result) {
69         echo '<tr><td>';
70         echo '<label for="'.$result['field_name'].'">'.$result['field_name'].'</label></td><td>';
71         echo '<input id="'.$result['field_name'].'" name="'.$result['field_name'].'" type="text" ';
72         echo ' value='.liveuser_pref_getPref($liveuser->getHandle(), $result['field_name']).'></td></tr>';      
73     }   
74     
75     echo '<tr><td colspan="2"><input type="reset" text="Reset"><input type="submit"></td></tr></table></form>';        
76     
77     $o = ob_get_contents();
78     ob_end_clean();
79     
80     return $o;
81 }
82
83 /**
84  * Checks if the specified preference already exists for the specified user.
85  *
86  * @param string username user handle 
87  * @param string field_name preference field name
88  * @return boolean true if preference exists, false otherwise
89  */
90 function liveuser_pref_checkPref($username, $field_name)
91 {
92     global $liveuserDB;
93     
94     $user_id = liveuser_getPermUserId($username);
95     $field_id = $liveuserDB->getOne('SELECT field_id FROM '.LW_PREFIX.'_prefs_fields WHERE field_name = ?',
96             array($field_name));
97     
98     return (is_numeric($field_id) ? $field_id : false);
99 }
100
101 /**
102  * Fetches the specified preference's value for the specified user.
103  *
104  * @param string username user handle
105  * @param string field_name preference field name
106  * @param boolean useDefault if true, default field value is supplied for non-existent pref, else null is used
107  * @return string preference field value if set, default value or null otherwise
108  */
109 function liveuser_pref_getPref($username, $field_name, $useDefault = true)
110 {
111     global $liveuserDB;
112     
113     $user_id = liveuser_getPermUserId($username);
114     $field_value = $liveuserDB->getOne('
115         SELECT '.LW_PREFIX.'_prefs_data.field_value 
116         FROM '.LW_PREFIX.'_prefs_data, '.LW_PREFIX.'_prefs_fields 
117         WHERE '.LW_PREFIX.'_prefs_data.user_id = ? 
118         AND '.LW_PREFIX.'_prefs_data.field_id = '.LW_PREFIX.'_prefs_fields.field_id 
119         AND '.LW_PREFIX.'_prefs_fields.field_name = ?',
120         array((int)$user_id, $field_name));
121     
122     if (is_null($field_value) && $useDefault) {
123         $field_value = $liveuserDB->getOne('
124             SELECT default_value 
125             FROM '.LW_PREFIX.'_prefs_fields 
126             WHERE field_name = ?', 
127             array($field_name));    
128     }
129     
130     return $field_value;
131 }
132
133 /**
134  * Sets the specified preference's value for the specified user.
135  *
136  * @param string username user handle 
137  * @param string field_name preference field name
138  * @return boolean true if the preference was successfully set, false otherwise
139  */
140 function liveuser_pref_setPref($username, $field_name, $field_value)
141 {
142     global $liveuserDB;
143     
144     $user_id = liveuser_getPermUserId($username);
145     
146     /* attempt to fetch existing field_id for the field_name, or create a new field if necessary */
147     if (($field_id = liveuser_pref_checkField($field_name)) === false &&
148         ($field_id = liveuser_pref_setField($field_name)) === false) {
149         return false;
150     }
151     
152     $pref_id = $liveuserDB->getOne('
153         SELECT pref_id 
154         FROM '.LW_PREFIX.'_prefs_data 
155         WHERE user_id = ? AND field_id = ?',
156         array((int)$user_id, (int)$field_id));
157             
158     if (isset($pref_id) && is_numeric($pref_id)) {
159         return ($liveuserDB->query('
160             UPDATE '.LW_PREFIX.'_prefs_data 
161             SET field_value = ? 
162             WHERE pref_id = ?',
163             array($field_value, (int)$pref_id)) == DB_OK);
164     } else {
165         return ($liveuserDB->query('
166             INSERT INTO '.LW_PREFIX.'_prefs_data (field_value, user_id, field_id) VALUES (?, ?, ?)',
167             array($field_value, (int)$user_id, (int)$field_id)) == DB_OK);
168     }       
169 }
170
171 /**
172  * Checks if the specified preference field already exists.
173  *
174  * @param mixed field_name field_name or id of preference field to check for 
175  * @param boolean publicOnly if true, only check against public fields
176  * @return mixed integer identifier if field exists, false otherwise
177  */ 
178 function liveuser_pref_checkField($field_name, $publicOnly = false)
179 {
180     global $liveuserDB;
181     
182     if (is_numeric($field_name)) {
183         $field_id = $liveuserDB->getOne('
184             SELECT field_id 
185             FROM '.LW_PREFIX.'_prefs_fields 
186             WHERE field_id = ? AND public >= ?',
187             array((int)$field_name, (int)$publicOnly));
188     } else {    
189         $field_id = $liveuserDB->getOne('
190             SELECT field_id 
191             FROM '.LW_PREFIX.'_prefs_fields 
192             WHERE field_name = ? AND public >= ?',
193             array($field_name, (int)$publicOnly));
194     }
195     
196     return (is_numeric($field_id) ? $field_id : false);
197 }
198
199 /**
200  * Removes a field definition and all references in the preferences data table.
201  *
202  * @param mixed field_name field name or id of preference field 
203  * @return boolean true if the field was removed successfully, false otherwise
204  */ 
205 function liveuser_pref_removeField($field_name)
206 {
207     global $liveuserDB;
208     
209     if (($field_id = liveuser_pref_checkField($field_name)) === false) {
210         return false;
211     }
212     
213     if ($liveuserDB->query('DELETE FROM '.LW_PREFIX.'_prefs_data WHERE field_id = ?',
214         array((int)field_id)) != DB_OK) {
215         return false;
216     }
217     
218     if ($liveuserDB->query('DELETE FROM '.LW_PREFIX.'_prefs_fields WHERE field_id = ?',
219         array((int)field_id)) != DB_OK) {
220         return false;
221     }
222 }
223
224 /**
225  * Sets a field (adds or updates) with the specified properties.
226  *
227  * @param mixed field_name field name or id of preference field 
228  * @param boolean public preference field is public if true, private if false
229  * @param string default_value default field value if user preference is not set
230  * @param string possible_values possible_values field value for admin to set and user to choose
231  * @return mixed integer identifier of field if operation was successful, false otherwise
232  */ 
233 function liveuser_pref_setField($field_name, $public = true, $default_value = null, $possible_values = null)
234 {
235     global $liveuserDB;
236     
237     if (($field_id = liveuser_pref_checkField($field_name)) !== false) {
238         return ($liveuserDB->query('
239             UPDATE '.LW_PREFIX.'_prefs_fields 
240             SET public = ?, default_value = ?, possible_values = ?
241             WHERE field_id = ?',
242             array((int)$public, $default_value, ($possible_values ? serialize($possible_values) : null), (int)$field_id)) == DB_OK);
243     } else {
244         if ($liveuserDB->query('
245             INSERT INTO '.LW_PREFIX.'_prefs_fields (field_name, public, default_value, possible_values) 
246             VALUES (?, ?, ?, ?)',
247             array($field_name, (int)$public, $default_value, ($possible_values ? serialize($possible_values) : null))) != DB_OK) {
248             return false;
249         }
250         return liveuser_pref_checkField($field_name);
251     }
252 }
253
254 ?>