db.inc

This file contains mostly db shortcuts.

Functions & methods

NameDescription
db_affected_rows
db_fetch_array
db_fetch_object
db_get_user_idLook up the user_id based on the the user_name. Returns FALSE if it cannot find it.
db_get_user_id_from_cwid
db_get_user_settingReturn a specific setting's value, based on the var_name given.
db_get_user_settingsReturn the array from the user_settings table.
db_insert_id
db_num_rows
db_query
db_resultReturns back the first result from a resource_handler.
db_set_user_setting
db_table_existsReturns TRUE if the table specified exists or not.
fp_get_faculty_nameReturns the faculty member's name based on the ID provided.
fp_get_permissions_for_role
fp_get_student_name
fp_get_system_settings
fp_load_userReturns back a user object for this user_id. If the user is not found in the users table, it will return NULL. If the user_id requested is 0, the anonymous user object is returned.
fp_rebuild_modules_listRe-query the modules table and re-add to our global array.
variable_getGet a variable from the database. We will first look in our GLOBALS array, to see that it hasn't already been retrieved this page load.
variable_setSet a variable value, so we can retrieve it later on.
watchdogAdd a log entry to the watchdog (log) table.

File

includes/db.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains mostly db shortcuts.
  5. */
  6. /**
  7. * Add a log entry to the watchdog (log) table.
  8. *
  9. * This is adapted from Drupal 6's watchdog system.
  10. *
  11. * @param string $type
  12. * Generally, this is the name of the module, or some other short text
  13. * which can be used to categorize this log entry. Ex: "system" or "routines".
  14. * @param string $message
  15. * This is the actual log message itself. It can be any length, and contain replacement
  16. * patterns (very similar to the t() function.) Ex: "The user @user has logged in."
  17. * @param array $variables
  18. * If replacement patterns exist in the $message, this is where they are defined, similar
  19. * to the t() function. Ex: array("@user" => $user->name) *
  20. * @param int $severity
  21. * One of several constant values, denoting the severity level.
  22. * Available values:
  23. * - WATCHDOG_DEBUG (for development)
  24. * - WATCHDOG_NOTICE (default)
  25. * - WATCHDOG_ALERT (a step above "notice")
  26. * - WATCHDOG_ERROR (highest level of severity)
  27. * @param string $extra_data
  28. * Any extra bit of text you want to add on. Must be 255 characters or less. Good for adding
  29. * extra codes and such which can be queried for later more easily.
  30. */
  31. function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $extra_data = "") {
  32. global $user;
  33. // TODO: Have a setting where, we do not actually log certain severity levels, like notice or debug
  34. // (to save space)
  35. $user_id = $user->id;
  36. $cwid = $user->cwid;
  37. $user_name = $user->name;
  38. $is_student = (int) $user->is_student;
  39. $is_faculty = (int) $user->is_faculty;
  40. $is_mobile = (int) fp_screen_is_mobile();
  41. $ip = $_SERVER["REMOTE_ADDR"];
  42. $location = $_SERVER["REQUEST_URI"];
  43. $referer = $_SERVER['HTTP_REFERER'];
  44. $ser_variables = "";
  45. if (count($variables) > 0) {
  46. $ser_variables = serialize($variables);
  47. }
  48. db_query("INSERT INTO watchdog
  49. (user_id, user_name, cwid, type, message, variables, severity, extra_data, location, referer, ip, is_mobile, is_student, is_faculty, timestamp)
  50. VALUES
  51. ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')
  52. ", $user_id, $user_name, $cwid, $type, $message, $ser_variables, $severity, $extra_data, $location, $referer, $ip, $is_mobule, $is_student, $is_faculty, time());
  53. }
  54. /**
  55. * Returns the faculty member's name based on the ID provided.
  56. */
  57. function fp_get_faculty_name($cwid) {
  58. $db = get_global_database_handler();
  59. $name = $db->get_faculty_name($cwid);
  60. if (!$name) $name = t("Unknown Advisor");
  61. return $name;
  62. }
  63. /**
  64. * Returns back a user object for this user_id.
  65. * If the user is not found in the users table, it will return NULL.
  66. * If the user_id requested is 0, the anonymous user object is returned.
  67. */
  68. function fp_load_user($user_id) {
  69. $rtn = new stdClass();
  70. if ($user_id == 0) {
  71. // Return the anonymous user.
  72. $rtn->id = 0;
  73. $rtn->name = t("Anonymous");
  74. $rtn->roles = array(1 => "anonymous user");
  75. $rtn->permissions = fp_get_permissions_for_role(1);
  76. return $rtn;
  77. }
  78. $res = db_query("SELECT * FROM users WHERE user_id = '?' ", $user_id);
  79. if (db_num_rows($res) == 0) return NULL;
  80. $cur = db_fetch_object($res);
  81. $rtn->id = $cur->user_id;
  82. $rtn->name = $cur->user_name;
  83. $rtn->f_name = $cur->f_name;
  84. $rtn->l_name = $cur->l_name;
  85. $rtn->email = $cur->email;
  86. $rtn->cwid = $cur->cwid;
  87. $rtn->is_student = (bool) $cur->is_student;
  88. $rtn->is_faculty = (bool) $cur->is_faculty;
  89. $rtn->roles = array();
  90. $rtn->permissions = array();
  91. // Load the user's roles and
  92. $res = db_query("SELECT * FROM user_roles a,
  93. roles c
  94. WHERE a.user_id = '?'
  95. AND a.rid = c.rid", $user_id);
  96. while($cur = db_fetch_array($res)) {
  97. $rtn->roles[$cur["rid"]] = $cur["name"];
  98. }
  99. // Let's make sure we get the authenticated user role as well, #2.
  100. $rtn->roles[2] = "authenticated user";
  101. // Go through each role and add in the permissions for each role.
  102. foreach ($rtn->roles as $rid => $val) {
  103. $perms = fp_get_permissions_for_role($rid);
  104. // Merge the arrays while KEEPING the original's key. So don't
  105. // use array_merge, use the + operator.
  106. $rtn->permissions = $rtn->permissions + $perms;
  107. }
  108. return $rtn;
  109. }
  110. /**
  111. * Look up the user_id based on the the user_name. Returns FALSE if it cannot find it.
  112. *
  113. * @param unknown_type $user_name
  114. */
  115. function db_get_user_id($user_name) {
  116. $user_id = db_result(db_query("SELECT user_id FROM users WHERE user_name = '?' ", $user_name));
  117. if ($user_id) {
  118. return $user_id;
  119. }
  120. return FALSE;
  121. }
  122. function db_get_user_id_from_cwid($cwid, $type = "faculty") {
  123. $type_line = " is_faculty='1' ";
  124. if ($type == "student") {
  125. $type_line = " is_student='1' ";
  126. }
  127. $user_id = db_result(db_query("SELECT user_id FROM users WHERE cwid = '?' AND $type_line ", $cwid));
  128. if ($user_id) {
  129. return $user_id;
  130. }
  131. return FALSE;
  132. }
  133. function fp_get_student_name($cwid) {
  134. if ($cwid == 0) {
  135. return t("Anonymous");
  136. }
  137. $db = get_global_database_handler();
  138. $name = $db->get_student_name($cwid);
  139. if (!$name) $name = t("Unknown Student");
  140. return $name;
  141. }
  142. function fp_get_permissions_for_role($rid) {
  143. $rtn = array();
  144. $res = db_query("SELECT * FROM role_permissions
  145. WHERE rid = '?' ", $rid);
  146. while($cur = db_fetch_array($res)) {
  147. $rtn[$cur["pid"]] = $cur["perm"];
  148. }
  149. return $rtn;
  150. }
  151. /**
  152. * Returns back the first result from a resource_handler.
  153. */
  154. function db_result($resource_handler) {
  155. $cur = db_fetch_array($resource_handler);
  156. return $cur[0];
  157. }
  158. function db_insert_id() {
  159. $db = get_global_database_handler();
  160. return $db->db_insert_id();
  161. }
  162. /**
  163. * Return the array from the user_settings table.
  164. *
  165. * @param unknown_type $user_id
  166. */
  167. function db_get_user_settings($user_id) {
  168. $db = get_global_database_handler();
  169. return $db->get_user_settings($user_id);
  170. }
  171. /**
  172. * Return a specific setting's value, based on the var_name given.
  173. *
  174. * @param unknown_type $user_id
  175. * @param unknown_type $var_name
  176. */
  177. function db_get_user_setting($user_id, $var_name = "", $default_value = "") {
  178. $settings = db_get_user_settings($user_id);
  179. $val = $settings[$var_name];
  180. if (!$val) {
  181. $val = $default_value;
  182. }
  183. return $val;
  184. }
  185. function db_set_user_setting($user_id, $var_name, $value) {
  186. $settings = db_get_user_settings($user_id);
  187. $settings[$var_name] = $value;
  188. $ser = serialize($settings);
  189. db_query("DELETE FROM user_settings WHERE user_id = '?' ", $user_id);
  190. db_query("INSERT INTO user_settings (user_id, settings, posted)
  191. VALUES ('?', '?', '?') ", $user_id, $ser, time());
  192. }
  193. function db_query($query) {
  194. // Capture arguments to this function, to pass along to our $db object.
  195. $args = func_get_args();
  196. array_shift($args);
  197. $db = get_global_database_handler();
  198. $res = $db->db_query($query, $args);
  199. return $res;
  200. }
  201. function db_fetch_array($result_handler) {
  202. $db = get_global_database_handler();
  203. return $db->db_fetch_array($result_handler);
  204. }
  205. function db_fetch_object($result_handler) {
  206. $db = get_global_database_handler();
  207. return $db->db_fetch_object($result_handler);
  208. }
  209. function db_num_rows($result_handler) {
  210. $db = get_global_database_handler();
  211. return $db->db_num_rows($result_handler);
  212. }
  213. function db_affected_rows() {
  214. $db = get_global_database_handler();
  215. return $db->db_affected_rows();
  216. }
  217. /**
  218. * Returns TRUE if the table specified exists or not.
  219. */
  220. function db_table_exists($table_name) {
  221. $res = db_query("SHOW TABLES LIKE '?' ", $table_name);
  222. $cur = db_fetch_array($res);
  223. if ($cur[0] == $table_name) {
  224. return TRUE;
  225. }
  226. return FALSE;
  227. }
  228. /**
  229. * Get a variable from the database. We will first look in our GLOBALS array,
  230. * to see that it hasn't already been retrieved this page load.
  231. *
  232. * @param unknown_type $name
  233. * @param unknown_type $default_value
  234. * @return unknown
  235. */
  236. function variable_get($name, $default_value = "") {
  237. $val = null;
  238. // First, check in our GLOBALS array, like a cache...
  239. if (isset($GLOBALS["fp_system_settings"][$name])) {
  240. $val = $GLOBALS["fp_system_settings"][$name];
  241. }
  242. else {
  243. // Not found-- look in the database for it.
  244. $res = db_query("SELECT value FROM variables
  245. WHERE name = '?' ", $name);
  246. $cur = db_fetch_array($res);
  247. $val = unserialize($cur["value"]);
  248. if ($val === "BOOLEAN_FALSE_PLACEHOLDER") {
  249. $val = FALSE;
  250. }
  251. // Save back to our cache...
  252. $GLOBALS["fp_system_settings"][$name] = $val;
  253. }
  254. if (!$val) {
  255. $val = $default_value;
  256. }
  257. // We must have this down here again, just in case what got stored in the GLOBALS
  258. // array was this placeholder. This can happen, because the settings file doesn't do
  259. // this check when assembling this variable on page load. It's something that needs
  260. // to be fixed.
  261. if ($val === "BOOLEAN_FALSE_PLACEHOLDER") {
  262. $val = FALSE;
  263. }
  264. return $val;
  265. }
  266. /**
  267. * Set a variable value, so we can retrieve it later on.
  268. *
  269. * This will write to our variables database table, as well as store in a cache
  270. * array for quick look-up later.
  271. *
  272. * @param unknown_type $name
  273. * @param unknown_type $value
  274. */
  275. function variable_set($name, $value) {
  276. // Save back to our "cache" GLOBALS array:
  277. $GLOBALS["fp_system_settings"][$name] = $value;
  278. // Boolean FALSE presents unusual problems when we try to tell if it got unserialized correctly.
  279. // We will convert it to a placeholder so we can positively store it.
  280. if ($value === FALSE) {
  281. $value = "BOOLEAN_FALSE_PLACEHOLDER";
  282. }
  283. db_query("REPLACE INTO variables (name, value)
  284. VALUES ('?', '?') ", $name, serialize($value));
  285. }
  286. /**
  287. * Re-query the modules table and re-add to our global array.
  288. */
  289. function fp_rebuild_modules_list($reinclude = TRUE) {
  290. unset($GLOBALS["fp_system_settings"]["modules"]);
  291. $res = db_query("SELECT * FROM modules WHERE enabled = 1
  292. ORDER BY weight");
  293. while ($cur = db_fetch_array($res)) {
  294. $GLOBALS["fp_system_settings"]["modules"][$cur["name"]] = $cur;
  295. if ($reinclude) {
  296. include_module($cur["name"], FALSE);
  297. }
  298. }
  299. }
  300. function fp_get_system_settings($force_rebuild = FALSE) {
  301. if ($force_rebuild == FALSE && isset($GLOBALS["fp_system_settings"])) {
  302. return $GLOBALS["fp_system_settings"];
  303. }
  304. // Get all of our settings from the variables table.
  305. $res = db_query("SELECT * FROM variables");
  306. while ($cur = db_fetch_array($res)) {
  307. $name = $cur["name"];
  308. $val = unserialize($cur["value"]);
  309. if ($val == "BOOLEAN_FALSE_PLACEHOLDER") {
  310. $val = FALSE;
  311. }
  312. $settings[$name] = $val;
  313. $GLOBALS["fp_system_settings"][$name] = $val;
  314. }
  315. // Make sure some important settings have _something_ set, or else it could cause
  316. // problems for some modules.
  317. if ($settings["current_catalog_year"] == "") {
  318. $settings["current_catalog_year"] = 2006;
  319. }
  320. if ($settings["earliest_catalog_year"] == "") {
  321. $settings["earliest_catalog_year"] = 2006;
  322. }
  323. $GLOBALS["fp_system_variables"] = $settings;
  324. return $settings;
  325. }