changed git call from https to git readonly
[atutor.git] / mods / cpref_switch / ajax_save.php
1 <?php
2 include_once('module.inc.php');
3 include_once(AT_INCLUDE_PATH."vitals.inc.php");
4
5 /**
6  * Performs input validation by confirming that the posted variable is one of the 
7  * expected input variables. Returns a default variable AT_PREF_NONE if the input is invalid.
8  * 
9  * @param $post_var The posted variable
10  * @param $expected An array of expected variables
11  * @return Either the correct posted variable or AT_PREF_NONE if the posted variable is invalid
12  */
13 function check_post_var($post_var, $expected) {
14     $var = AT_PREF_NONE;
15     if (isset($post_var) && in_array($post_var, $expected)) {
16         $var = $post_var;
17     }
18     return $var;
19 }
20
21 /**
22  * Checks if the content preference type changed from the user's current settings.
23  * 
24  * @param $post_var The content preference type posted.
25  * @param $use_pref_name The name of the 'use content' preference (ie. PREF_USE_ALTERNATIVE_TO_TEXT).
26  * @param $alternative_pref The name of the 'alternative content' preference (ie. PREF_ALT_TO_TEXT).
27  * @return A boolean value false if the preference has not changed, true otherwise.
28  */
29 function isPrefChanged($post_var, $use_pref_name, $alternative_pref) {
30     if ($_SESSION['prefs'][$use_pref_name] == 0 && $post_var == AT_PREF_NONE) {
31         return false;
32     }
33     if ($_SESSION['prefs'][$use_pref_name] == 1 && $post_var == $_SESSION['prefs'][$alternative_pref]) {
34         return false;
35     }
36     return true;
37 }
38
39 /**
40  * Changes a temporary array of preferences to reflect the new settings. Returns false if the preferences
41  * have not changed and true otherwise.
42  * 
43  * @param $temp_prefs An array of preferences passed by reference.
44  * @param $post_var The content preference posted.
45  * @param $pref_type The type of content preference alternative to set, either "TEXT", "AUDIO" or "VISUAL".
46  * @return A boolean value false if the preference has not changed, true otherwise.
47  */
48 function changePreference(&$temp_prefs, $post_var, $pref_type) {
49     $pref_changed = false;
50     if (isPrefChanged($post_var, 'PREF_USE_ALTERNATIVE_TO_'.$pref_type, 'PREF_ALT_TO_'.$pref_type)) {
51         $pref_changed = true;
52         if ($post_var == AT_PREF_NONE) {
53             //change the first setting and leave the rest as chosen by the user
54             $temp_prefs['PREF_USE_ALTERNATIVE_TO_'.$pref_type] = 0;
55             $temp_prefs['PREF_ALT_TO_'.$pref_type] = $_SESSION['prefs']['PREF_ALT_TO_'.$pref_type];
56             $temp_prefs['PREF_ALT_TO_'.$pref_type.'_APPEND_OR_REPLACE'] = $_SESSION['prefs']['PREF_ALT_TO_'.$pref_type.'_APPEND_OR_REPLACE'];
57             $temp_prefs['PREF_ALT_'.$pref_type.'_PREFER_LANG'] = $_SESSION['prefs']['PREF_ALT_'.$pref_type.'_PREFER_LANG'];
58         } else {
59             //change first two settings and leave the rest as chosen by the user
60             $temp_prefs['PREF_USE_ALTERNATIVE_TO_'.$pref_type] = 1;
61             $temp_prefs['PREF_ALT_TO_'.$pref_type] = $post_var;
62             $temp_prefs['PREF_ALT_TO_'.$pref_type.'_APPEND_OR_REPLACE'] = $_SESSION['prefs']['PREF_ALT_TO_'.$pref_type.'_APPEND_OR_REPLACE'];
63             $temp_prefs['PREF_ALT_'.$pref_type.'_PREFER_LANG'] = $_SESSION['prefs']['PREF_ALT_'.$pref_type.'_PREFER_LANG'];
64         }
65     }
66     return $pref_changed;
67 }
68
69 //do post variable input validation
70 $alt_to_text = check_post_var($addslashes($_POST[AT_POST_ALT_TO_TEXT]), array(AT_PREF_NONE, AT_PREF_AUDIO, AT_PREF_VISUAL, AT_PREF_SIGN));
71 $alt_to_audio = check_post_var($addslashes($_POST[AT_POST_ALT_TO_AUDIO]), array(AT_PREF_NONE, AT_PREF_TEXT, AT_PREF_VISUAL, AT_PREF_SIGN));
72 $alt_to_visual = check_post_var($addslashes($_POST[AT_POST_ALT_TO_VISUAL]), array(AT_PREF_NONE, AT_PREF_TEXT, AT_PREF_AUDIO, AT_PREF_SIGN));
73
74 //if preferences have changed then change $_SESSION variable
75 //save settings if user is student
76 $temp_prefs = $_SESSION['prefs'];
77 $text_prefs_changed = changePreference($temp_prefs, $alt_to_text, "TEXT");
78 $audio_prefs_changed = changePreference($temp_prefs, $alt_to_audio, "AUDIO");
79 $visual_prefs_changed = changePreference($temp_prefs, $alt_to_visual, "VISUAL");
80
81 $is_preferences_changed = $text_prefs_changed || $audio_prefs_changed || $visual_prefs_changed;
82 if ($is_preferences_changed) {
83     assign_session_prefs($temp_prefs);
84     save_prefs(); 
85 }
86
87 echo $is_preferences_changed;
88 ?>