move code up one directory
[atutor.git] / mods / _core / users / lib / pref_tab_functions.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16
17 function get_tabs() {
18         global $_config;
19
20         //these are the _AT(x) variable names and their include file
21         /* tabs[tab_id] = array(tab_name, file_name, accesskey) */
22         $tabs[0] = array('atutor_settings', 'atutor_settings.inc.php', 'n');
23         $tabs[1] = array('display_settings', 'display_settings.inc.php', 'p');
24         
25         if(!$_config['just_social']){
26                 $tabs[2] = array('content_settings', 'content_settings.inc.php', 'g');
27                 $tabs[3] = array('tool_settings', 'tool_settings.inc.php', 'r');
28                 $tabs[4] = array('control_settings', 'control_settings.inc.php', 'a');  
29         }
30         return $tabs;
31 }
32
33 function output_tabs($current_tab, $changes) {
34         global $_base_path;
35         $tabs = get_tabs();
36         $num_tabs = count($tabs);
37 ?>
38 <div class="etabbed-list-container">
39         <ul class="etabbed-list" >
40         
41                 
42                 <?php for ($i=0; $i < $num_tabs; $i++): 
43                         if ($current_tab == $i):?>
44                                 <li class="prefs_tab_selected">
45                                         <?php if ($changes[$i]): ?>
46                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
47                                         <?php echo '<input type="submit" name="button_'.$i.'" value="'._AT($tabs[$i][0]).'" title="'._AT($tabs[$i][0]).' - alt '.$tabs[$i][2].'" class="prefs_buttontab" accesskey="'.$tabs[$i][2].'" onmouseover="this.style.cursor=\'pointer\';" '.$clickEvent.' />'; ?>
48                                         <?php endif; ?>
49                                         <?php echo _AT($tabs[$i][0]); ?>
50                                 </li>
51                         
52                         <?php else: ?>
53                                 <li class="prefs_tab" >
54                                         <?php if ($changes[$i]): ?>
55                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
56                                         <?php endif; ?>
57
58                                         <?php echo '<input type="submit" name="button_'.$i.'" value="'._AT($tabs[$i][0]).'" title="'._AT($tabs[$i][0]).' - alt '.$tabs[$i][2].'" class="prefs_buttontab" accesskey="'.$tabs[$i][2].'" onmouseover="this.style.cursor=\'pointer\';" '.$clickEvent.' />'; ?>
59                                 </li>
60                                 
61                         <?php endif; ?>
62                 <?php endfor; ?>
63                 
64         
65         </ul>
66 </div>  
67 <?php }
68
69 // returns given $languges in html <option> tag
70 function output_language_options($languages, $selected_lang)
71 {
72         foreach ($languages as $codes)
73         {
74                 $language = current($codes);
75                 
76                 $lang_code = $language->getCode();
77                 $lang_native_name = $language->getNativeName();
78                 $lang_english_name = $language->getEnglishName()
79 ?>
80                 <option value="<?php echo $lang_code ?>" <?php if ($selected_lang == $lang_code) echo 'selected="selected"'; ?>><?php echo $lang_english_name . ' - '. $lang_native_name; ?></option>
81 <?php
82         }
83 }
84 /**
85  * Assigns posted preferences to a $temp_prefs array
86  * 
87  * @return array of preferences
88  */
89 function assignPostVars() {
90 global $addslashes;
91         
92         $temp_prefs = array();
93     foreach($_SESSION['prefs'] as $pref_name => $value) {
94         $temp_prefs[$pref_name] = $value;
95     }
96                 
97         /* custom prefs */
98         // atutor settings (tab 0)
99         if (isset($_POST['numbering'])) $temp_prefs['PREF_NUMBERING'] = intval($_POST['numbering']);    
100         if (isset($_POST['theme'])) $temp_prefs['PREF_THEME'] = $addslashes($_POST['theme']);
101         if (isset($_POST['mobile_theme'])) $temp_prefs['PREF_MOBILE_THEME'] = $addslashes($_POST['mobile_theme']);
102         if (isset($_POST['time_zone'])) $temp_prefs['PREF_TIMEZONE'] = $addslashes($_POST['time_zone']);
103         if (isset($_POST['use_jump_direct'])) $temp_prefs['PREF_JUMP_REDIRECT'] = intval($_POST['use_jump_redirect']);
104         if (isset($_POST['form_focus'])) $temp_prefs['PREF_FORM_FOCUS'] = intval($_POST['form_focus']);
105         if (isset($_POST['content_editor'])) $temp_prefs['PREF_CONTENT_EDITOR'] = intval($_POST['content_editor']);
106         if (isset($_POST['show_guide']))$temp_prefs['PREF_SHOW_GUIDE'] = intval($_POST['show_guide']);
107
108         // display settings (tab 1)
109         if (isset($_POST['fontface'])) $temp_prefs['PREF_FONT_FACE'] = $addslashes($_POST['fontface']);
110         if (isset($_POST['font_times'])) $temp_prefs['PREF_FONT_TIMES'] = $addslashes($_POST['font_times']);
111         if (isset($_POST['fg'])) $temp_prefs['PREF_FG_COLOUR'] = $addslashes($_POST['fg']);
112         if (isset($_POST['bg'])) $temp_prefs['PREF_BG_COLOUR'] = $addslashes($_POST['bg']);
113         if (isset($_POST['hl'])) $temp_prefs['PREF_HL_COLOUR'] = $addslashes($_POST['hl']);
114         
115         // content settings (tab 2)
116         if (isset($_POST['use_alternative_to_text'])) $temp_prefs['PREF_USE_ALTERNATIVE_TO_TEXT'] = intval($_POST['use_alternative_to_text']);
117         if (isset($_POST['preferred_alt_to_text'])) $temp_prefs['PREF_ALT_TO_TEXT'] = $addslashes($_POST['preferred_alt_to_text']);
118         if (isset($_POST['alt_to_text_append_or_replace'])) $temp_prefs['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] = $addslashes($_POST['alt_to_text_append_or_replace']);
119         if (isset($_POST['alt_text_prefer_lang'])) $temp_prefs['PREF_ALT_TEXT_PREFER_LANG'] = $addslashes($_POST['alt_text_prefer_lang']);
120         if (isset($_POST['use_alternative_to_audio'])) $temp_prefs['PREF_USE_ALTERNATIVE_TO_AUDIO'] = intval($_POST['use_alternative_to_audio']);
121         if (isset($_POST['preferred_alt_to_audio'])) $temp_prefs['PREF_ALT_TO_AUDIO'] = $addslashes($_POST['preferred_alt_to_audio']);
122         if (isset($_POST['alt_to_audio_append_or_replace'])) $temp_prefs['PREF_ALT_TO_AUDIO_APPEND_OR_REPLACE'] = $addslashes($_POST['alt_to_audio_append_or_replace']);
123         if (isset($_POST['alt_audio_prefer_lang'])) $temp_prefs['PREF_ALT_AUDIO_PREFER_LANG'] = $addslashes($_POST['alt_audio_prefer_lang']);
124         if (isset($_POST['use_alternative_to_visual'])) $temp_prefs['PREF_USE_ALTERNATIVE_TO_VISUAL'] = intval($_POST['use_alternative_to_visual']);
125         if (isset($_POST['preferred_alt_to_visual'])) $temp_prefs['PREF_ALT_TO_VISUAL'] = $addslashes($_POST['preferred_alt_to_visual']);
126         if (isset($_POST['alt_to_visual_append_or_replace'])) $temp_prefs['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE'] = $addslashes($_POST['alt_to_visual_append_or_replace']);
127         if (isset($_POST['alt_visual_prefer_lang'])) $temp_prefs['PREF_ALT_VISUAL_PREFER_LANG'] = $addslashes($_POST['alt_visual_prefer_lang']);
128
129         // tool settings (tab 3)
130         if (isset($_POST['dictionary_val'])) $temp_prefs['PREF_DICTIONARY'] = intval($_POST['dictionary_val']);
131         if (isset($_POST['thesaurus_val'])) $temp_prefs['PREF_THESAURUS'] = intval($_POST['thesaurus_val']);
132         if (isset($_POST['note_taking_val'])) $temp_prefs['PREF_NOTE_TAKING'] = intval($_POST['note_taking_val']);
133         if (isset($_POST['calculator_val'])) $temp_prefs['PREF_CALCULATOR'] = intval($_POST['calculator_val']);
134         if (isset($_POST['abacus_val'])) $temp_prefs['PREF_ABACUS'] = intval($_POST['abacus_val']);
135         if (isset($_POST['atlas_val'])) $temp_prefs['PREF_ATLAS'] = intval($_POST['atlas_val']);
136         if (isset($_POST['encyclopedia_val'])) $temp_prefs['PREF_ENCYCLOPEDIA'] = intval($_POST['encyclopedia_val']);   
137
138         // control settings (tab 4)
139         if (isset($_POST['show_contents'])) $temp_prefs['PREF_SHOW_CONTENTS'] = intval($_POST['show_contents']);
140         if (isset($_POST['show_next_previous_buttons'])) $temp_prefs['PREF_SHOW_NEXT_PREVIOUS_BUTTONS'] = intval($_POST['show_next_previous_buttons']);
141         if (isset($_POST['show_bread_crumbs'])) $temp_prefs['PREF_SHOW_BREAD_CRUMBS'] = intval($_POST['show_bread_crumbs']);
142                 
143         return $temp_prefs;
144 }
145
146 /**
147  * Assigns default preferences to a preferences array
148  *  
149  * @return an array of preferences
150  */
151 function assignDefaultPrefs() {
152         global $db, $_config_defaults;      
153         $sql    = "SELECT value FROM ".TABLE_PREFIX."config WHERE name='pref_defaults'";
154         $result = mysql_query($sql, $db);
155         
156         if (mysql_num_rows($result) > 0)
157         {
158                 $row_defaults = mysql_fetch_assoc($result);
159                 $default = $row_defaults["value"];
160                 
161                 $temp_prefs = unserialize($default);
162                             
163                 // Many new preferences are introduced in 1.6.2 that are missing in old admin 
164                 // default preference string. Solve this case by completing settings on new
165                 // preferences with $_config_defaults
166                 foreach (unserialize($_config_defaults['pref_defaults']) as $name => $value) {
167                         if (!isset($temp_prefs[$name])) $temp_prefs[$name] = $value;
168                 }
169         }
170         else {
171                 $temp_prefs = unserialize($_config_defaults['pref_defaults']);
172         }
173         return $temp_prefs;
174 }
175
176 /**
177  * gets the default preference for inbox notification (disabled usually)
178  * 
179  * @return the value of the inbox notification preference
180  */
181 function assignDefaultMnot() {
182 global $db, $_config_defaults;
183         $sql    = "SELECT value FROM ".TABLE_PREFIX."config WHERE name='pref_inbox_notify'";
184         $result = mysql_query($sql, $db);
185         if (mysql_num_rows($result) > 0)
186         {
187             $row_notify = mysql_fetch_assoc($result);
188             $mnot = $row_notify["value"];
189         }
190         else
191             $mnot = $_config_defaults['pref_inbox_notify'];
192         return $mnot;
193 }
194
195 /**
196  * gets the default preference for auto login preference (disabled usually)
197  * 
198  * @return the value of the auto login preference
199  */
200 function assignDefaultAutologin() {
201 global $db, $_config_defaults;;
202         $sql    = "SELECT value FROM ".TABLE_PREFIX."config WHERE name='pref_is_auto_login'";
203         $result = mysql_query($sql, $db);
204         if (mysql_num_rows($result) > 0)
205         {
206             $row_is_auto_login = mysql_fetch_assoc($result);
207             $auto_login = $row_is_auto_login["value"];
208         }
209         else
210             $auto_login = $_config_defaults['pref_is_auto_login'];
211         return $auto_login;
212
213 }
214
215 /**
216  *  Either sets the auto login cookies or expires them depending on the input
217  *  
218  * @param string $toDo - 'enable' if the autologin cookies are to be set, and
219  * 'disable' if the auto login cookies are to be expired.
220  * 
221  * @return string - either 'enable' if the cookies were set, or 'disable' otherwise.
222  */
223 function setAutoLoginCookie($toDo) {
224     global $db;
225
226     //set default values for disabled auto login cookies
227     $parts = parse_url(AT_BASE_HREF);
228     $path = $parts['path'];
229         $time = time() - 172800;
230         $password = ""; 
231         $login = "";
232         $is_auto_login = 'disable';
233         
234         //if enable auto login, set actual cookie values
235         if ($toDo == 'enable') {
236                 $time = time() + 172800;
237                 $sql    = "SELECT password, last_login FROM ".TABLE_PREFIX."members WHERE member_id=$_SESSION[member_id]";
238                 $result = mysql_query($sql, $db);
239                 $row    = mysql_fetch_assoc($result);
240                 $password = $row["password"];
241                 $last_login = $row["last_login"];
242                 $login = $_SESSION['login'];    
243         }
244         #4775: password now store with salt
245         $saltedPassword = hash('sha512', $password . hash('sha512', $last_login));
246         //set cookies and boolean value indicating cookies have been set.iesf
247         $is_cookie_login_set = ATutor.setcookie('ATLogin', $login, $time, $path);
248         $is_cookie_pass_set = ATutor.setcookie('ATPass',  $saltedPassword, $time, $path);
249         if ($is_cookie_login_set && $is_cookie_pass_set) $is_auto_login = $toDo;
250         return $is_auto_login;
251 }
252
253 /**
254  * Sets $is_auto_login to 'enable' if both auto login cookies are set, otherwise
255  * returns 'disable'
256  * 
257  * @return string
258  */
259 function checkAutoLoginCookie() {
260     $is_auto_login = 'disable';
261     if (isset($_COOKIE['ATLogin']) && isset($_COOKIE['ATPass'])) {
262         $is_auto_login = 'enable';
263     }
264     return $is_auto_login;
265 }
266
267 ?>