7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 // Set effective user - If no user id is provided, then use the currently logged in user
15 function effectiveUser($user)
17 return (is_null($user) ? $_SESSION['authUserID'] : $user);
21 * Return user setting(s) from the 'users' table
23 * @param string $label - Setting key
24 * @param int $user - user id number from users table
25 * @param int $defaultUser - user id to check as alternative/default
26 * @return Effective user setting for $label (NULL if does not exist)
28 function getUserSetting($label, $user = null, $defaultUser = 0)
31 $user = effectiveUser($user);
33 // Collect entry for specified user or 0 (global default user)
34 $res = sqlQuery("SELECT setting_value FROM user_settings
35 WHERE (setting_user=? OR setting_user=?) AND setting_label=?
36 ORDER BY setting_user DESC", array($user, $defaultUser, $label));
38 // If no entries exist, then return NULL.
39 return (isset($res['setting_value']) ? $res['setting_value'] : null);
43 * Check if effective user setting matches given value
45 * @param string $label - Setting key
46 * @param string $value - Setting value
47 * @param int $user - user id number from users table
48 * @return boolean - true if setting exist and false if does not exist
50 function checkUserSetting($label, $value, $user = null)
53 $user = effectiveUser($user);
55 $curval = getUserSetting($label, $user);
56 if (is_null($curval)) {
59 return ($curval === $value);
66 * @param string $label - Setting key
67 * @param string $value - Setting value
68 * @param int $user - user id number from users table
69 * @param boolean $createDefault - If no current global default value, create one.
70 * @param boolean $overwrite - If this is set to true, then overwrite the current setting
72 function setUserSetting($label, $value, $user = null, $createDefault = true, $overwrite = true)
75 $user = effectiveUser($user);
77 $cur_value = getUserSetting($label, $user, $user);
79 // Check for a custom settings
80 if (is_null($cur_value)) {
81 sqlStatement("INSERT INTO user_settings(setting_user, setting_label, setting_value) " .
82 "VALUES (?,?,?)", array($user, $label, $value));
83 } elseif (($cur_value !== $value) && $overwrite) {
84 sqlStatement("UPDATE user_settings SET setting_value=? " .
85 "WHERE setting_user=? AND setting_label=?", array($value, $user, $label));
88 // Call self to create default token
89 // (Note this is only done if a default token does not yet exist, thus set overwrite to FALSE))
91 setUserSetting($label, $value, 0, false, false);
95 //This will remove the selected user setting from the 'user_settings' table.
96 // $label is used to determine which setting to remove
97 // $user is the user id number from users table
98 function removeUserSetting($label, $user = null)
101 $user = effectiveUser($user);
103 // mdsupport - DELETE has implicit select, no need to check and delete
104 sqlQuery("DELETE FROM user_settings " .
105 "WHERE setting_user=? AND setting_label=?", array($user, $label));
108 function getUserIDInfo($id)
110 return sqlQuery("SELECT fname, lname, username FROM users where id=?", array($id));
114 * Function to retain current user's choices from prior sessions
115 * @param string $uspfx - Caller specified prefix to be used in settings key, typically script name
116 * @param string $postvar - $_POST variable name containing current value
117 * @param string $label - Caller specified constant added to $uspfx to create settings key
118 * @param string $initval - Initial value to be saved in case user setting does not exist
119 * @return Prior setting (if found) or initial value to be used in script
121 function prevSetting($uspfx, $postvar, $label, $initval)
124 $setting_key = $uspfx . $label;
126 if (isset($_POST[$postvar])) {
127 // If script provides current value, store it for future use.
128 $pset = $_POST[$postvar];
129 if ($pset != getUserSetting($setting_key)) {
130 setUserSetting($setting_key, $_POST[$postvar]);
133 // Script requires prior value
134 $pset = getUserSetting($setting_key);
135 if (is_null($pset)) {
136 setUserSetting($setting_key, $initval);
137 $pset = getUserSetting($setting_key);
145 * Function to set the state of expandable forms as per user choice, user default or global default
146 * @return the current state of the file after updating table user_settings
149 function collectAndOrganizeExpandSetting($filenames = array())
151 $current_filename = $filenames[0];
152 $global_value = $GLOBALS['expand_form'];
154 if (getUserSetting($current_filename) > -1) {
155 $current_state = getUserSetting($current_filename);
156 } elseif ($global_value) {
157 $current_state = $global_value;
162 if (count($filenames)) {
163 foreach ($filenames as $filename) {
164 setUserSetting($filename, $current_state);
168 return $current_state;