6799c9225870680ad7318462557b3bb67a41ffed
[atutor.git] / mods / wiki / plugins / module / uservars_gui.php
1 <?php
2
3 /*
4 Interfaces to user variables, 
5
6 */
7
8 $ewiki_user_gui_fields = array(
9   "First Name" => "", "Middle Name" => "","Last Name" => "","Title" => "",
10   "Company" => "", "E-Mail Address" => "", "Phone Number" => "",
11   "Address" => "", "City" => "", "State" => "","Zip Code" => "",
12   "Country" => ""  
13   );
14 $ewiki_t["en"]["USERDATAUPDATESUCCESS"] = "Your user data has been successfully updated.";
15
16 /*
17   Add the pages you would like to use to your config file.
18 */
19  $ewiki_plugins["page"]["AdminFullUser"]="ewiki_page_uservars_full";
20  
21  //The current search function has a bug in it, some web browsers (Mozilla at least)
22  //automatically convert "." to "_" in control names, use at your own risk.
23  //$ewiki_plugins["page"]["AdminSearchAccounts"]="ewiki_page_uservars_search";
24  $ewiki_plugins["page"]["UserInfo"]="ewiki_page_uservars_user";
25
26
27 /**admin fulluser gui for viewing all info about a specified user.
28  *
29  * @param string id
30  * @param mixed data
31  * @param string action
32  * @return string page output response
33  */
34 function ewiki_page_uservars_search($id, $data, $action)
35 {
36   global $ewiki_errmsg;
37   
38   ob_start();
39   
40   $success=TRUE; //used for displaying update status
41   $editing=FALSE; //used for displaying update status
42   
43   echo ewiki_make_title($id, "Search User Data", 2); 
44     
45   if(isset($_REQUEST['search_fieldname'])){ //set fieldname variable
46     $fieldname=$_REQUEST['search_fieldname'];
47   }
48   if(strlen($_REQUEST['search_fieldvalue'])){ //set fieldvalue variable
49     $fieldvalue=$_REQUEST['search_fieldvalue'];
50   }
51   
52   if(isset($_REQUEST['submit_clearuservars'])){ //check if the user clicked the "Delete Selected" button
53     echo("Deleting user variables.... ");
54     $editing=TRUE; //we are performing an edit
55     foreach($_REQUEST as $request_key => $request_value){
56       if(strstr($request_key, "chk_")){ //find all checkboxes
57         $varname=substr($request_key,4); //strip chk_ from the beginning of the value
58         $username=$request_value;
59         if(!ewiki_clear_uservar($varname, $username)){ //clear the selected value
60          $success=FALSE; //there was an error
61         }
62       }
63     }
64   }  
65   
66   if(isset($_REQUEST['submit_changeaccount'])){ //check if we are modifying an existing field
67     $editing=TRUE; //we are performing an edit 
68     foreach($_REQUEST as $request_key => $request_value){ 
69       if(strstr($request_key, "text_")){ //find all text boxes
70         $username=substr($request_key,5); //remove prefix text_ from value 
71         if(!ewiki_set_uservar($fieldname, $request_value, $username)){ //set the desired information
72           $success=FALSE;
73         }
74       }
75     }
76   }
77   
78   if($editing){ //check to see if we performed an edit 
79     echo ($success ? ewiki_t("USERDATASUCCESS") : $ewiki_errmsg);  //if we did and there was an error display it, otherwise display success
80   }
81   
82   if(!empty($fieldname)){
83     $userdata = ewiki_search_uservar($fieldname, $fieldvalue); //get data for the given fieldname/fieldvalue combination
84   }
85     
86   ?>
87   <form method="post" action="">
88     <table>
89       <tr><td>Field Name</td><td><input type="text" name="search_fieldname"></td></tr>
90       <tr><td>Value</td><td><input type="text" name="search_fieldvalue"></td></tr>
91     </table>
92     <input value="Search" type="submit" name="submit_searchaccount" />
93   </form>
94
95   <?
96   
97   if(is_array($userdata)){
98     echo '<h4>Query Results</h4><form action="" method="post"><table border=1>';
99     echo '<tr><th>Select</th><th>UserID</th><th>'.$fieldname.'</th></tr>';
100     foreach($userdata as $username => $value){
101       ?>
102       <tr><td><input type="checkbox" name="chk_<?echo $fieldname?>" value="<?echo $username?>"></td><th><?echo $username?></th>
103       <td><input type="text" name="text_<?echo $username?>" value="<?echo $value?>"></td></tr>
104       <?
105     }
106     ?>
107     </table>
108     <input value="<?echo $fieldname?>" type="hidden" name="search_fieldname">
109     <input value="<?echo $fieldvalue?>" type="hidden" name="search_fieldvalue">
110     <input value="Submit Changes" type="submit" name="submit_changeaccount" />
111     <input value="Delete Selected" type="submit" name="submit_clearuservars" />
112     </form>
113     <?
114   }
115
116   $o = ob_get_contents();
117   ob_end_clean();
118   return $o;  
119 }
120
121 /**admin fulluser gui for viewing all info about a specified user.
122  *
123  * @param string id
124  * @param mixed data
125  * @param string action
126  * @return string page output response
127  */
128 function ewiki_page_uservars_full($id, $data, $action)
129 {
130   global $ewiki_errmsg;
131   $success=TRUE;
132   $editing=FALSE;
133   
134   ob_start();
135   
136   // resolve username if none submitted use current username
137   if(isset($_REQUEST['accountname_text'])){
138     $user=$_REQUEST['accountname_text'];
139   } else {
140     $user =$GLOBALS['ewiki_auth_user'];
141   }
142   
143   echo ewiki_make_title($id, "User info for $user", 2); 
144   
145   if(!empty($user)){
146     // check to see if we are clearing fields
147     if(isset($_REQUEST['submit_clearuservars'])){ //check if we are clearing values
148       $editing=TRUE; //we are editing
149       foreach($_REQUEST as $request_key => $request_value){
150         if(strstr($request_key, "chk_") && $request_value ="on"){ //only use chk_ inputs
151           $varname=substr($request_key,4); //remove the chk_ prefix
152           if(!ewiki_clear_uservar($varname, $user)){ //clear the selected values
153             $success=FALSE; //there was an error
154           }
155         }
156       }
157     } elseif(isset($_REQUEST['submit_changeaccount'])){ //check to see if we are editing fields
158       $editing=TRUE;
159       foreach($_REQUEST as $request_key => $request_value){
160         if(strstr($request_key, "text_")){ //only work on fields with the text_ prefix
161           $varname=substr($request_key,5); //remove the text_ prefix
162           if(!ewiki_set_uservar($varname, $request_value, $user)){ //set appropriate data
163             $success=FALSE;
164           }
165         }
166       } //check to see if we are adding/editing a group of fields
167     } elseif(isset($_REQUEST['submit_batchfields']) && !empty($_REQUEST['batch_fieldnames']) && !empty($_REQUEST['batch_fieldvalues'])){ 
168       $editing=TRUE;
169       $newfields = explode("\n", $_REQUEST['batch_fieldnames']); //split up the batch
170       $newvalues = explode("\n", $_REQUEST['batch_fieldvalues']); //split up the batch
171       foreach($newfields as $key => $field){
172         $field=trim($field);
173         $value=(isset($newvalues[$key])?trim($newvalues[$key]):"");
174         if(strlen($field)){ //cannot add blank fieldnames
175           if(!ewiki_set_uservar($field, $value, $user)){
176             $success=FALSE;
177           }
178         }
179       } //check if we are adding multiple users
180     } elseif(isset($_REQUEST['submit_batchusers']) && !empty($_REQUEST['batch_usernames']) && !empty($_REQUEST['text_fieldname'])){ 
181       $newusers=explode("\n", $_REQUEST['batch_usernames']);
182       $field=trim($_REQUEST['text_fieldname']);
183       $value=trim($_REQUEST['text_fieldvalue']);
184       foreach($newusers as $username){
185         $username=trim($username);
186         if(!ewiki_set_uservar($field, $value, $username)){
187           $success=FALSE;
188         }
189       }
190     } elseif(isset($_REQUEST['submit_bulkset']) && strlen($_REQUEST['bulk_items'])){
191       $editing=TRUE;
192       $bulk=explode("\n", $_REQUEST['bulk_items']);
193       foreach($bulk as $items){
194         $values=explode(",", $items);
195         $username=trim($values[0]);
196         $varname=trim($values[1]);
197         $value=trim($values[2]);
198         if(strlen($varname)){
199           if(!ewiki_set_uservar($varname, $value, $username)){
200             $success=FALSE;
201           }
202         }
203       }
204     } elseif(isset($_REQUEST['submit_accountaddfield']) && !empty($_REQUEST['new_accountfield']) && !empty($_REQUEST['new_accountfield'])){
205       $editing=TRUE; 
206       if(!ewiki_set_uservar($_REQUEST['new_accountfield'], $_REQUEST['new_accountfieldvalue'], $user)){
207         $success=FALSE;
208       }
209     }
210   }
211   
212   if($editing){
213     echo ($success ? ewiki_t("USERDATASUCCESS") : $ewiki_errmsg);
214   }
215   
216   $account_uservars=ewiki_getall_uservar($user);
217
218   ?>
219   <form method="post" action="">
220     <p>User Name <input type="text" name="accountname_text"><input value="View Info" type="submit" name="submit_viewaccount" /></p>
221   </form>
222   <h3>User Information</h3>
223   <p><form method="post" action=""><table border="1" cellpadding="1">
224   <?
225   if(!empty($account_uservars)){
226       foreach($account_uservars as $field => $value){
227       echo '<tr><td><input type="checkbox" name="chk_'.$field.'"></td><th>'.$field.'</th>'.
228            '<td><input name="text_'.$field.'" type="text" value="'.$value.'"></td></tr>';
229       }
230   }?>
231         </table>
232     <input value="Submit Changes" type="submit" name="submit_changeaccount" />
233     <input value="Delete Selected" type="submit" name="submit_clearuservars" />
234     <input value="<?echo $user?>" type="hidden" name="accountname_text">
235   </form>
236   <h3>Add/Edit a Field</h3>
237   <form method="post" action="">
238     <input value="<?echo $user?>" type="hidden" name="accountname_text">
239     <table>
240       <tr><td>Field Name</td><td><input type="text" name="new_accountfield"></td></tr>
241       <tr><td>Value</td><td><input type="text" name="new_accountfieldvalue"></td></tr>
242     </table>
243     <input type="submit" value="Add Field" name="submit_accountaddfield" />
244   </form>
245   <h3>Add/Edit Multiple Fields</h3>
246   <p>On the left insert one field name per line.  On the right insert
247   a corresponding field value for the field.  These will be processed
248   as a batch and each field will be created with its corresponding
249   value.</p>
250   <form method="post" action="">
251     <input value="<?echo $user?>" type="hidden" name="accountname_text">
252     <table>
253       <tr><td><textarea name="batch_fieldnames" rows="10" cols="25"></textarea></td>
254       <td><textarea name="batch_fieldvalues" rows="10" cols="25"></textarea></td></tr>
255     </table>
256     <input type="submit" name="submit_batchfields" value="Add Fields">
257   </form>
258   <h3>Add Field to Multiple Users</h3>
259   <p>In the box below, insert one username per line.  Then insert a field
260   name and an optional value for that field.  That field with that value
261   will be added to all of the users in the box.</p>
262   <form method="post" action="">
263     <input value="<?echo $user?>" type="hidden" name="accountname_text">
264     <table>
265     <tr><td colspan=2>User Names:</td></tr><tr><td colspan=2><textarea name="batch_usernames" rows="10"  cols="24"></textarea></td></tr>
266       <tr><td>Field Name</td><td><input type="text" name="text_fieldname"></td></tr><tr><td>Field Value</td><td><input type="text" name="text_fieldvalue"></td></tr>
267     </table>
268     <input type="submit" name="submit_batchusers" value="Add Fields">
269   </form>
270   <h3>Bulk Set</h3>
271   <p>In the box below insert username,fieldname,value. Only one per line.</p>
272   <form method="post" action="">
273     <input value="<?echo $user?>" type="hidden" name="accountname_text">
274     <table>
275     <tr><td><textarea name="bulk_items" rows="10"  cols="50"></textarea></td></tr>
276     </table>
277     <input type="submit" name="submit_bulkset" value="Submit Bulk">
278   </form>
279 <?
280   $o = ob_get_contents();
281   ob_end_clean();
282   return $o;  
283 }
284
285 /**user gui for viewing predetermined user fields for current user only.
286  *
287  * @param string id
288  * @param mixed data
289  * @param string action
290  * @return string page output response
291  */
292 function ewiki_page_uservars_user($id, $data, $action)
293 {
294   global $ewiki_errmsg, $ewiki_user_gui_fields;
295   
296   $success=TRUE;
297   
298   //set user name to currently logged in user
299   $user =$GLOBALS['ewiki_auth_user'];
300   
301   $o = ewiki_make_title($id, "User info for $user", 2); 
302     
303   //check to see if we are editing fields and that we have a username
304   if(isset($_REQUEST['submit_changeaccount']) && !empty($user)){
305     $editing=TRUE;
306     foreach($_REQUEST as $request_key => $request_value){
307       if(strstr($request_key, "text_")){ //only use the text_ inputs
308         $varname=substr($request_key,5); //remove the text_ prefix
309         if(!ewiki_set_uservar($varname, $request_value, $user)){ //set the appropriate info
310           $success=FALSE; //something failed
311         } 
312       }
313     }
314     $o .= ($success ? ewiki_t("USERDATAUPDATESUCCESS") : $ewiki_errmsg);
315   }
316   
317   $account_uservars=ewiki_getall_uservar($user); //get user info
318   
319
320   if(!empty($account_uservars)){ //if data exists for this user
321     $o .= '<p><form method="post" action=""><table border="1" cellpadding="1">';
322     foreach($account_uservars as $field => $value){ 
323       if(isset($ewiki_user_gui_fields[$field])){ //check if this is a field we want the user to be able to edit 
324         $o .= '<tr><th>'.$field.'</th><td><input name="text_'.$field.'" type="text" value="'.$value.'"></td></tr>';
325       }
326     }
327     $o .= '</table>'.
328       '<input value="Submit Changes" type="submit" name="submit_changeaccount" />'.
329       '</form>';
330   } else {
331     $o .= ewiki_t("USERDATANODATA");
332   }
333
334   return $o;
335
336 }
337 ?>