6 * @link http://www.open-emr.org
7 * @author Brady Miller <brady.g.miller@gmail.com>
8 * @copyright Copyright (c) 2010 Brady Miller <brady.g.miller@gmail.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 // Set effective user - If no user id is provided, then use the currently logged in user
14 function effectiveUser($user)
16 return (is_null($user) ? $_SESSION['authUserID'] : $user);
20 * Return user setting(s) from the 'users' table
22 * @param string $label - Setting key
23 * @param int $user - user id number from users table
24 * @param int $defaultUser - user id to check as alternative/default
25 * @return Effective user setting for $label (NULL if does not exist)
27 function getUserSetting($label, $user = null, $defaultUser = 0)
30 $user = effectiveUser($user);
32 // Collect entry for specified user or 0 (global default user)
33 $res = sqlQuery("SELECT setting_value FROM user_settings
34 WHERE (setting_user=? OR setting_user=?) AND setting_label=?
35 ORDER BY setting_user DESC", array($user, $defaultUser, $label));
37 // If no entries exist, then return NULL.
38 return (isset($res['setting_value']) ? $res['setting_value'] : null);
42 * Check if effective user setting matches given value
44 * @param string $label - Setting key
45 * @param string $value - Setting value
46 * @param int $user - user id number from users table
47 * @return boolean - true if setting exist and false if does not exist
49 function checkUserSetting($label, $value, $user = null)
52 $user = effectiveUser($user);
54 $curval = getUserSetting($label, $user);
55 if (is_null($curval)) {
58 return ($curval === $value);
65 * @param string $label - Setting key
66 * @param string $value - Setting value
67 * @param int $user - user id number from users table
68 * @param boolean $createDefault - If no current global default value, create one.
69 * @param boolean $overwrite - If this is set to true, then overwrite the current setting
71 function setUserSetting($label, $value, $user = null, $createDefault = true, $overwrite = true)
74 $user = effectiveUser($user);
76 $cur_value = getUserSetting($label, $user, $user);
78 // Check for a custom settings
79 if (is_null($cur_value)) {
80 sqlStatement("INSERT INTO user_settings(setting_user, setting_label, setting_value) " .
81 "VALUES (?,?,?)", array($user, $label, $value));
82 } elseif (($cur_value !== $value) && $overwrite) {
83 sqlStatement("UPDATE user_settings SET setting_value=? " .
84 "WHERE setting_user=? AND setting_label=?", array($value, $user, $label));
87 // Call self to create default token
88 // (Note this is only done if a default token does not yet exist, thus set overwrite to FALSE))
90 setUserSetting($label, $value, 0, false, false);
94 //This will remove the selected user setting from the 'user_settings' table.
95 // $label is used to determine which setting to remove
96 // $user is the user id number from users table
97 function removeUserSetting($label, $user = null)
100 $user = effectiveUser($user);
102 // mdsupport - DELETE has implicit select, no need to check and delete
103 sqlQuery("DELETE FROM user_settings " .
104 "WHERE setting_user=? AND setting_label=?", array($user, $label));
107 function getUserIDInfo($id)
109 return sqlQuery("SELECT fname, lname, username FROM users where id=?", array($id));
113 * Function to retain current user's choices from prior sessions
114 * @param string $uspfx - Caller specified prefix to be used in settings key, typically script name
115 * @param string $postvar - $_POST variable name containing current value
116 * @param string $label - Caller specified constant added to $uspfx to create settings key
117 * @param string $initval - Initial value to be saved in case user setting does not exist
118 * @return Prior setting (if found) or initial value to be used in script
120 function prevSetting($uspfx, $postvar, $label, $initval)
123 $setting_key = $uspfx.$label;
125 if (isset($_POST[$postvar])) {
126 // If script provides current value, store it for future use.
127 $pset = $_POST[$postvar];
128 if ($pset != getUserSetting($setting_key)) {
129 setUserSetting($setting_key, $_POST[$postvar]);
132 // Script requires prior value
133 $pset = getUserSetting($setting_key);
134 if (is_null($pset)) {
135 setUserSetting($setting_key, $initval);
136 $pset = getUserSetting($setting_key);
144 * Function to set the state of expandable forms as per user choice, user default or global default
145 * @return the current state of the file after updating table user_settings
148 function collectAndOrganizeExpandSetting($filenames = array())
150 $current_filename = $filenames[0];
151 $global_value = $GLOBALS['expand_form'];
153 if (getUserSetting($current_filename) > -1) {
154 $current_state = getUserSetting($current_filename);
155 } elseif ($global_value) {
156 $current_state = $global_value;
161 if (count($filenames)) {
162 foreach ($filenames as $filename) {
163 setUserSetting($filename, $current_state);
167 return $current_state;