db.inc

  1. 6.x includes/db.inc
  2. 4.x includes/db.inc
  3. 5.x includes/db.inc

This file contains mostly db shortcuts.

File

includes/db.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains mostly db shortcuts.
  5. */
  6. /**
  7. * This method will return a globally-set DatabaseHandler object,
  8. * creating it if it does not already exist. This is for efficiency
  9. * reasons, so every module or method does not need to keep creating
  10. * databasehandler objects (and re-connecting to the database).
  11. *
  12. */
  13. function get_global_database_handler() {
  14. $db = @$GLOBALS["fp_global_database_handler"];
  15. if (!is_object($db) || !is_object($db->pdo)) {
  16. // Something isn't right, or it wasn't set correctly. Create a new connection.
  17. $GLOBALS["fp_global_database_handler"] = new DatabaseHandler();
  18. }
  19. return $GLOBALS["fp_global_database_handler"];
  20. }
  21. /**
  22. * Add a log entry to the watchdog (log) table.
  23. *
  24. * This is adapted from Drupal 6's watchdog system.
  25. *
  26. * @param string $type
  27. * Generally, this is the name of the module, or some other short text
  28. * which can be used to categorize this log entry. Ex: "system" or "routines".
  29. * @param string $message
  30. * This is the actual log message itself. It can be any length, and contain replacement
  31. * patterns (very similar to the t() function.) Ex: "The user @user has logged in."
  32. * @param array $variables
  33. * If replacement patterns exist in the $message, this is where they are defined, similar
  34. * to the t() function. Ex: array("@user" => $user->name) *
  35. * @param int $severity
  36. * One of several constant values, denoting the severity level.
  37. * Available values:
  38. * - WATCHDOG_DEBUG (for development)
  39. * - WATCHDOG_NOTICE (default)
  40. * - WATCHDOG_ALERT (a step above "notice")
  41. * - WATCHDOG_ERROR (highest level of severity)
  42. * @param string $extra_data
  43. * Any extra bit of text you want to add on. Must be 255 characters or less. Good for adding
  44. * extra codes and such which can be queried for later more easily.
  45. */
  46. function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $extra_data = "") {
  47. global $user;
  48. $user_id = 0;
  49. $cwid = 0;
  50. $user_name = "";
  51. $is_student = 0;
  52. $is_faculty = 0;
  53. $school_id = 0;
  54. if (is_object($user)) {
  55. $user_id = @$user->id;
  56. $cwid = @$user->cwid;
  57. $user_name = @$user->name;
  58. $is_student = (int) @$user->is_student;
  59. $is_faculty = (int) @$user->is_faculty;
  60. $school_id = (int) @$user->school_id;
  61. }
  62. $is_mobile = 0; //TODO: not used anymore! //(int) fp_screen_is_mobile();
  63. $ip = @$_SERVER["REMOTE_ADDR"];
  64. $location = @$_SERVER["REQUEST_URI"];
  65. $referer = @$_SERVER['HTTP_REFERER'];
  66. $ser_variables = "";
  67. if (is_array($variables) && count($variables) > 0) {
  68. $ser_variables = serialize($variables);
  69. }
  70. db_query("INSERT INTO watchdog
  71. (user_id, user_name, cwid, type, message, variables, severity, extra_data, location, referer, ip, is_mobile, is_student, is_faculty, timestamp, school_id)
  72. VALUES
  73. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  74. ", $user_id, $user_name, $cwid, $type, $message, $ser_variables, $severity, $extra_data, $location, $referer, $ip, $is_mobile, $is_student, $is_faculty, time(), $school_id);
  75. }
  76. /**
  77. * Figure out the maximum number of times this course can be repeated for credit, based on what is stored
  78. * in the course catalog.
  79. *
  80. * We will do this by directly querying the database, for speed reasons.
  81. *
  82. */
  83. function fp_get_max_catalog_repeats_for_course($subject_id, $course_num, $catalog_year, $bool_draft = TRUE, $school_id = 0) {
  84. $table_name = "courses";
  85. if ($bool_draft) {
  86. $table_name = "draft_courses";
  87. }
  88. $res = db_query("SELECT * FROM $table_name
  89. WHERE subject_id = ?
  90. AND course_num = ?
  91. AND catalog_year = ?
  92. AND school_id = ? ", $subject_id, $course_num, $catalog_year, $school_id);
  93. $cur = db_fetch_array($res);
  94. $min_hours = $cur['min_hours'];
  95. $max_hours = $cur['max_hours'];
  96. $repeat_hours = $cur['repeat_hours'];
  97. if ($repeat_hours <= $min_hours) {
  98. // Meaning, this course cannot be repeated for anything. So, just return 1, meaning, it can be taken only once.
  99. return 1;
  100. }
  101. // Okay, so what we want to do is figure out, if this is a 3 hour course, and it can be repeated for 9 hours, that means it
  102. // can be taken (repeats) 3 times.
  103. // We will use the min_hours for this calculation. If zero, then change it to 1.
  104. if ($min_hours <= 0) $min_hours = 1;
  105. // Use intval so we don't have decimals. Whole attempts only.
  106. $repeats = intval($repeat_hours / $min_hours);
  107. return $repeats;
  108. } // fp_get_max_catalog_repeats_for_course
  109. /**
  110. * Code adapted from Drupal 6: https://api.drupal.org/api/drupal/includes%21pager.inc/function/pager_query/6.x
  111. *
  112. * This helps allow us to create a paginated query (where we have Prev and Next links for the results).
  113. *
  114. * NOTE!!!!!!!!! To render the pager, call the function theme_pager() after getting the results of this query
  115. *
  116. * Description from the Drupal API page:
  117. *
  118. * Use this function when doing select queries you wish to be able to page. The pager uses LIMIT-based queries to fetch only the records
  119. * required to render a certain page. However, it has to learn the total number of records returned by the query to compute the number
  120. * of pages (the number of records / records per page). This is done by inserting "COUNT(*)" in the original query.
  121. * For example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC" would be
  122. * rewritten to read "SELECT COUNT(*) FROM node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the query is accomplished
  123. * using a regular expression.
  124. *
  125. * Unfortunately, the rewrite rule does not always work as intended for queries that already have a "COUNT(*)" or a "GROUP BY" clause,
  126. * and possibly for other complex queries. In those cases, you can optionally pass a query that will be used to count the records.
  127. *
  128. * For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node GROUP BY TYPE", pager_query()
  129. * would invoke the incorrect query "SELECT COUNT(*) FROM node GROUP BY TYPE". So instead,
  130. * you should pass "SELECT COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter.
  131. *
  132. * $query: The SQL query that needs paging.
  133. * $limit: The number of query results to display per page.
  134. * $element: An optional integer to distinguish between multiple pagers on one page.
  135. * $count_query: An SQL query used to count matching records.
  136. *
  137. *
  138. */
  139. function pager_query($query, $args = array(), $limit = 10, $element = 0, $count_query = NULL, $count_query_select_from = NULL) {
  140. global $pager_page_array, $pager_total, $pager_total_items;
  141. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : '';
  142. // Construct a count query if none was given.
  143. if (!isset($count_query)) {
  144. $select_from_replace = 'SELECT COUNT(*) ';
  145. if ($count_query_select_from) {
  146. $select_from_replace = $count_query_select_from;
  147. }
  148. $count_query = preg_replace(array(
  149. '/SELECT.*?FROM /As',
  150. '/ORDER BY .*/',
  151. '/GROUP BY .*/',
  152. ), array(
  153. $select_from_replace . " FROM ",
  154. '',
  155. '',
  156. ), $query);
  157. }
  158. // Convert comma-separated $page to an array, used by other functions.
  159. $pager_page_array = explode(',', $page);
  160. // We calculate the total of pages as ceil(items / limit).
  161. $pager_total_items[$element] = db_result(db_query($count_query, $args));
  162. $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
  163. $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));
  164. return db_query_range($query, $args, $pager_page_array[$element] * $limit, $limit);
  165. }
  166. /**
  167. * Adds a simple LIMIT statement to a query. Adapted from Drupal 6: https://api.drupal.org/api/drupal/includes%21database.mysql.inc/function/db_query_range/6.x
  168. */
  169. function db_query_range($query, $args = array(), $from, $count = 10) {
  170. $query .= ' LIMIT ' . (int) $from . ', ' . (int) $count;
  171. return db_query($query, $args);
  172. }
  173. /**
  174. * Returns the major code for a given degree_id
  175. */
  176. function fp_get_degree_major_code($degree_id, $bool_reset_cache = FALSE) {
  177. if ($bool_reset_cache) {
  178. unset($GLOBALS['fp_temp_degree_major_codes']);
  179. }
  180. // We will cache in a GLOBALS variable, to save lookup time.
  181. if (isset($GLOBALS['fp_temp_degree_major_codes'][$degree_id])) {
  182. return $GLOBALS['fp_temp_degree_major_codes'][$degree_id];
  183. }
  184. $major_code = '';
  185. $res = db_query("SELECT major_code FROM degrees WHERE degree_id = ?", $degree_id);
  186. $cur = db_fetch_array($res);
  187. if ($cur) {
  188. $major_code = trim($cur['major_code']);
  189. }
  190. if ($major_code) {
  191. $GLOBALS['fp_temp_degree_major_codes'][$degree_id] = $major_code;
  192. }
  193. return $major_code;
  194. }
  195. function fp_get_degree_advising_weight($degree_id, $bool_reset_cache = FALSE) {
  196. if ($bool_reset_cache) {
  197. unset($GLOBALS['fp_temp_degree_advising_weights']);
  198. }
  199. // We will cache in a GLOBALS variable, to save lookup time.
  200. if (isset($GLOBALS['fp_temp_degree_advising_weights'][$degree_id])) {
  201. return $GLOBALS['fp_temp_degree_advising_weights'][$degree_id];
  202. }
  203. $advising_weight = 0;
  204. $res = db_query("SELECT advising_weight FROM degrees WHERE degree_id = ?", $degree_id);
  205. $cur = db_fetch_array($res);
  206. if ($cur) {
  207. $advising_weight = intval(trim($cur['advising_weight']));
  208. }
  209. if ($advising_weight) {
  210. $GLOBALS['fp_temp_degree_advising_weights'][$degree_id] = $advising_weight;
  211. }
  212. return $advising_weight;
  213. }
  214. /**
  215. * Quick method to look up title for a degree.
  216. */
  217. function fp_get_degree_title($degree_id, $bool_include_track_title = FALSE, $bool_include_html = TRUE, $bool_use_draft = FALSE, $bool_include_major_code = FALSE) {
  218. // TODO: Check cache.
  219. $dtitle = "";
  220. $track_title = "";
  221. // Still no title? Try to load ANY degree title with this degree's
  222. // major_code.
  223. $table_name = "degrees";
  224. if ($bool_use_draft) {$table_name = "draft_$table_name";}
  225. $res = db_query("SELECT * FROM $table_name
  226. WHERE degree_id = ?
  227. ORDER BY catalog_year DESC LIMIT 1", $degree_id);
  228. $cur = db_fetch_array($res);
  229. if (!$cur) return '';
  230. $dtitle = $cur["title"];
  231. $major_code = $cur['major_code'];
  232. $o_major_code = $cur['major_code'];
  233. $catalog_year = $cur['catalog_year'];
  234. if ($bool_include_track_title) {
  235. // Get track title
  236. if (strstr($major_code, "_"))
  237. {
  238. // This means that there is a track. Get all the information
  239. // you can about it.
  240. $temp = explode("_", $major_code);
  241. $track_code = trim($temp[1]);
  242. $major_code = trim($temp[0]);
  243. // The major_code might now have a | at the very end. If so,
  244. // get rid of it.
  245. if (substr($major_code, strlen($major_code)-1, 1) == "|")
  246. {
  247. $major_code = str_replace("|","",$major_code);
  248. }
  249. // Now, look up information on the track.
  250. $table_name = "degree_tracks";
  251. if ($bool_use_draft) {$table_name = "draft_$table_name";}
  252. $res = db_query("SELECT track_title FROM $table_name
  253. WHERE major_code = '?'
  254. AND track_code = '?'
  255. AND catalog_year = '?' ", $major_code, $track_code, $catalog_year);
  256. $cur = db_fetch_array($res);
  257. $track_title = $cur["track_title"];
  258. }
  259. if ($track_title != "") {
  260. if ($bool_include_html) {
  261. $dtitle .= "<span class='level-3-raquo'>&raquo;</span>";
  262. }
  263. $dtitle .= $track_title;
  264. }
  265. }
  266. if ($bool_include_major_code) {
  267. $dtitle .= " [$major_code]";
  268. }
  269. return $dtitle;
  270. }
  271. /**
  272. * Returns the faculty member's name based on the ID provided.
  273. */
  274. function fp_get_faculty_name($cwid) {
  275. // Already cached?
  276. if (isset($GLOBALS['fp_cache_get_faculty_name'][$cwid])) {
  277. return $GLOBALS['fp_cache_get_faculty_name'][$cwid];
  278. }
  279. $db = get_global_database_handler();
  280. $name = $db->get_faculty_name($cwid, FALSE);
  281. if (!$name) $name = t("Unknown Advisor");
  282. $GLOBALS['fp_cache_get_faculty_name'][$cwid] = $name;
  283. return $name;
  284. }
  285. /**
  286. * Returns back a user object for this user_id.
  287. * If the user is not found in the users table, it will return NULL.
  288. * If the user_id requested is 0, the anonymous user object is returned.
  289. */
  290. function fp_load_user($user_id) {
  291. $rtn = new stdClass();
  292. if ($user_id <= 0) {
  293. // Return the anonymous user.
  294. $rtn->id = 0;
  295. $rtn->name = t("Anonymous");
  296. $rtn->roles = array(1 => "anonymous user");
  297. $rtn->permissions = fp_get_permissions_for_role(1);
  298. $rtn->email = '';
  299. $rtn->school_id = 0;
  300. return $rtn;
  301. }
  302. $res = db_query("SELECT * FROM users WHERE user_id = ? ", $user_id);
  303. if (db_num_rows($res) == 0) return NULL;
  304. $cur = db_fetch_object($res);
  305. $rtn->id = $cur->user_id;
  306. $rtn->name = $cur->user_name;
  307. $rtn->f_name = $cur->f_name;
  308. $rtn->l_name = $cur->l_name;
  309. $rtn->school_id = intval($cur->school_id);
  310. $rtn->email = trim(strtolower($cur->email));
  311. $rtn->cwid = $cur->cwid;
  312. $rtn->is_student = (bool) $cur->is_student;
  313. $rtn->is_faculty = (bool) $cur->is_faculty;
  314. $rtn->is_disabled = (bool) $cur->is_disabled;
  315. $rtn->last_login = (int) $cur->last_login;
  316. $rtn->roles = array();
  317. $rtn->permissions = array();
  318. // Load the user's roles and
  319. $res = db_query("SELECT * FROM user_roles a,
  320. roles c
  321. WHERE a.user_id = ?
  322. AND a.rid = c.rid ", array($user_id));
  323. while($cur = db_fetch_array($res)) {
  324. if (!isset($rtn->roles[$cur['rid']])) {
  325. $rtn->roles[$cur["rid"]] = $cur["name"];
  326. }
  327. }
  328. // Let's make sure we get the authenticated user role as well, #2.
  329. $rtn->roles[2] = "authenticated user";
  330. // Go through each role and add in the permissions for each role.
  331. foreach ($rtn->roles as $rid => $val) {
  332. $perms = fp_get_permissions_for_role($rid);
  333. // Merge the arrays while KEEPING the original's key. So don't
  334. // use array_merge, use the + operator.
  335. $rtn->permissions = $rtn->permissions + $perms;
  336. }
  337. // Load settings from the user_settings table (if any exist)
  338. $rtn->settings = array();
  339. $res = db_query("SELECT * FROM user_settings WHERE user_id = ?", array($user_id));
  340. while ($cur = db_fetch_array($res)) {
  341. $rtn->settings[$cur['name']] = $cur['value'];
  342. }
  343. // Set the user image url...
  344. // See if there are any other modules which might be overriding how we look up user images.
  345. $image_url = @$cur->settings['image_url'];
  346. $type = 'student';
  347. if ($rtn->is_faculty) $type = 'faculty';
  348. $modules = invoke_hook("get_user_image_url", array($rtn->cwid, $type));
  349. // Although there might be several modules which will return an image url, we will only use the last one (even if it's blank or FALSE, meaning no picture).
  350. foreach ($modules as $val) {
  351. $image_url = trim($val);
  352. }
  353. $rtn->settings['image_url'] = $image_url;
  354. // load attributes from user_attributes table (if any exist)
  355. $rtn->attributes = array();
  356. $res = db_query("SELECT * FROM user_attributes WHERE user_id = ?", array($user_id));
  357. while ($cur = db_fetch_array($res)) {
  358. $rtn->attributes[$cur['name']] = $cur['value'];
  359. }
  360. return $rtn;
  361. }
  362. /**
  363. * Returns back the timezone for this user, if selected. If not, we return back the system timezone.
  364. */
  365. function fp_get_user_timezone($account = NULL) {
  366. global $user;
  367. if (is_numeric($account)) {
  368. $account = fp_load_user($account);
  369. }
  370. if ($account == NULL) {
  371. $account = $user;
  372. }
  373. $utz = trim(@$account->settings['timezone']);
  374. if ($utz) return $utz;
  375. // Else...
  376. $system_timezone = variable_get('system_timezone', 'America/Chicago');
  377. return $system_timezone;
  378. }
  379. /**
  380. * Look up the user_id based on the the user_name. Returns FALSE if it cannot find it.
  381. *
  382. * @param unknown_type $user_name
  383. */
  384. function db_get_user_id($user_name) {
  385. $user_id = db_result(db_query("SELECT user_id FROM users WHERE user_name = '?' ", $user_name));
  386. if ($user_id) {
  387. return $user_id;
  388. }
  389. return FALSE;
  390. }
  391. function db_get_cwid_from_user_id($user_id) {
  392. return db_result(db_query("SELECT cwid FROM users WHERE user_id = ?", array($user_id)));
  393. }
  394. function db_get_user_id_from_cwid($cwid, $type = "faculty") {
  395. $type_line = " is_faculty='1' ";
  396. if ($type == "student") {
  397. $type_line = " is_student='1' ";
  398. }
  399. $user_id = db_result(db_query("SELECT user_id FROM users WHERE cwid = ?
  400. AND $type_line ", $cwid));
  401. if ($user_id) {
  402. return $user_id;
  403. }
  404. return FALSE;
  405. }
  406. function db_get_user_id_from_email($email, $type = "faculty") {
  407. $type_line = " is_faculty='1' ";
  408. if ($type == "student") {
  409. $type_line = " is_student='1' ";
  410. }
  411. // Force email to be lowercase
  412. $email = trim(strtolower($email));
  413. $user_id = db_result(db_query("SELECT user_id FROM users WHERE email = ? AND $type_line ", $email));
  414. if ($user_id) {
  415. return $user_id;
  416. }
  417. return FALSE;
  418. }
  419. function db_get_user_id_from_user_name($user_name, $type = "faculty") {
  420. $type_line = " is_faculty='1' ";
  421. if ($type == "student") {
  422. $type_line = " is_student='1' ";
  423. }
  424. // Force to be lowercase
  425. $user_name = trim(strtolower($user_name));
  426. $user_id = db_result(db_query("SELECT user_id FROM users WHERE user_name = ? AND $type_line ", $user_name));
  427. if ($user_id) {
  428. return $user_id;
  429. }
  430. return FALSE;
  431. }
  432. /**
  433. Return back the codes or records for a student's degrees, based on what is in the
  434. student_degrees table (thanks to system.module), as well as what we get from hooks.
  435. */
  436. function fp_get_student_majors($student_cwid, $bool_return_as_csv = FALSE, $bool_return_as_full_record = FALSE, $perform_join_with_degrees = TRUE, $bool_skip_directives = TRUE, $bool_check_for_allow_dynamic = TRUE) {
  437. $params = array($student_cwid, $bool_return_as_full_record, $perform_join_with_degrees, $bool_skip_directives, $bool_check_for_allow_dynamic);
  438. // Execute hook for this too.
  439. $arr = invoke_hook("fp_get_student_majors", $params);
  440. // Results will appear in an array, with each module as the index.
  441. // Ex:
  442. // system
  443. // MAJOR1 => MAJOR1
  444. // MAJOR2 => MAJOR2
  445. // custom_module
  446. // MAJOR1 => MAJOR1
  447. // XYZ => XYZ
  448. $new_arr = array();
  449. $csv = "";
  450. // Go through our modules, combine the identical ones, then prepare as a possible CSV.
  451. foreach ($arr as $module => $results) {
  452. foreach ($results as $k => $v) {
  453. $new_arr[$k] = $v;
  454. }
  455. }
  456. $rtn = $new_arr;
  457. // Returning as a CSV?
  458. if ($bool_return_as_csv) {
  459. foreach ($new_arr as $k => $v) {
  460. $csv .= $k . ",";
  461. }
  462. $csv = rtrim($csv, ",");
  463. $rtn = $csv;
  464. }
  465. return $rtn;
  466. }
  467. function fp_get_student_name($cwid, $bool_include_cwid = FALSE ) {
  468. if (is_numeric($cwid) && ($cwid === 0 || $cwid === '0')) {
  469. return t("Anonymous");
  470. }
  471. // Already cached?
  472. if (isset($GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)])) {
  473. return $GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)];
  474. }
  475. $db = get_global_database_handler();
  476. $name = $db->get_student_name($cwid, $bool_include_cwid);
  477. if (!$name) $name = t("Unknown Student");
  478. $GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)] = $name;
  479. return $name;
  480. }
  481. function fp_get_student_email($cwid) {
  482. $email = db_result(db_query("SELECT email FROM users
  483. WHERE cwid = ?
  484. AND is_student = 1", array($cwid)));
  485. if (trim($email)) {
  486. return trim(strtolower($email));
  487. }
  488. // else, email wasn't set.
  489. return FALSE;
  490. }
  491. function fp_get_faculty_email($cwid) {
  492. $email = db_result(db_query("SELECT email FROM users
  493. WHERE cwid = ?
  494. AND is_faculty = 1", array($cwid)));
  495. if (trim($email)) {
  496. return trim(strtolower($email));
  497. }
  498. // else, email wasn't set.
  499. return FALSE;
  500. }
  501. function fp_get_permissions_for_role($rid) {
  502. $rtn = array();
  503. $res = db_query("SELECT pid, perm FROM role_permissions
  504. WHERE rid = ?
  505. GROUP BY perm
  506. ORDER BY perm", array($rid));
  507. while($cur = db_fetch_array($res)) {
  508. if (!in_array($cur['perm'], $rtn)) {
  509. $rtn[$cur["pid"]] = $cur["perm"];
  510. }
  511. }
  512. return $rtn;
  513. }
  514. /**
  515. * Returns back the first result from a resource_handler.
  516. */
  517. function db_result($res) {
  518. if (is_object($res)) {
  519. return $res->fetchColumn(); // fetches the first value returned.
  520. }
  521. return NULL;
  522. }
  523. function db_insert_id() {
  524. $db = get_global_database_handler();
  525. return $db->db_insert_id();
  526. }
  527. /**
  528. * Return the array from the user_settings table.
  529. *
  530. * @param unknown_type $user_id
  531. */
  532. function db_get_user_settings($user_id) {
  533. $db = get_global_database_handler();
  534. return $db->get_user_settings($user_id);
  535. }
  536. function db_get_school_id_for_student_id($student_id) {
  537. $db = get_global_database_handler();
  538. return $db->get_school_id_for_student_id($student_id);
  539. }
  540. function db_get_school_id_for_user_id($user_id) {
  541. $db = get_global_database_handler();
  542. return $db->get_school_id_for_user_id($user_id);
  543. }
  544. /**
  545. * Return a specific setting's value, based on the var_name given.
  546. *
  547. * @param unknown_type $user_id
  548. * @param unknown_type $var_name
  549. */
  550. function db_get_user_setting($user_id, $var_name = "", $default_value = "") {
  551. $settings = db_get_user_settings($user_id);
  552. $val = @$settings[$var_name];
  553. if (!$val) {
  554. $val = $default_value;
  555. }
  556. return $val;
  557. }
  558. function db_set_user_setting($user_id, $var_name, $value) {
  559. $settings = db_get_user_settings($user_id);
  560. $settings[$var_name] = $value;
  561. $ser = serialize($settings);
  562. db_query("DELETE FROM user_settings WHERE user_id = '?' ", $user_id);
  563. db_query("INSERT INTO user_settings (user_id, settings, posted)
  564. VALUES ('?', '?', '?') ", $user_id, $ser, time());
  565. }
  566. function db_query($query) {
  567. // Capture arguments to this function, to pass along to our $db object.
  568. $args = func_get_args();
  569. array_shift($args);
  570. $db = get_global_database_handler();
  571. $res = $db->db_query($query, $args);
  572. return $res;
  573. }
  574. function db_fetch_array($result_handler) {
  575. $db = get_global_database_handler();
  576. return $db->db_fetch_array($result_handler);
  577. }
  578. function db_fetch_object($result_handler) {
  579. $db = get_global_database_handler();
  580. return $db->db_fetch_object($result_handler);
  581. }
  582. function db_num_rows($result_handler) {
  583. $db = get_global_database_handler();
  584. return $db->db_num_rows($result_handler);
  585. }
  586. function db_affected_rows($result_handler) {
  587. $db = get_global_database_handler();
  588. return $db->db_affected_rows($result_handler);
  589. }
  590. /**
  591. * Returns TRUE if the table specified exists or not.
  592. */
  593. function db_table_exists($table_name) {
  594. $res = db_query("SHOW TABLES LIKE ? ", $table_name);
  595. $cur = db_fetch_array($res);
  596. if ($cur[0] == $table_name) {
  597. return TRUE;
  598. }
  599. return FALSE;
  600. }
  601. /**
  602. * Returns TRUE or FALSE if the variable exists in the variables table.
  603. */
  604. function variable_exists($name) {
  605. if (isset($GLOBALS["fp_system_settings"][$name])) {
  606. return TRUE;
  607. }
  608. $count = intval(db_result(db_query("SELECT COUNT(name) as my_count
  609. FROM variables
  610. WHERE name = ?", array($name))));
  611. if ($count > 0) return TRUE;
  612. // else....
  613. return FALSE;
  614. }
  615. /**
  616. * Get a variable from the database. We will first look in our GLOBALS array,
  617. * to see that it hasn't already been retrieved this page load.
  618. *
  619. * @param string $name
  620. * @param mixed $default_value
  621. * @return mixed
  622. */
  623. function variable_get($name, $default_value = "") {
  624. $val = null;
  625. // First, check in our GLOBALS array, like a cache...
  626. if (isset($GLOBALS["fp_system_settings"][$name])) {
  627. $val = $GLOBALS["fp_system_settings"][$name];
  628. }
  629. else {
  630. // Not found-- look in the database for it.
  631. $res = db_query("SELECT value FROM variables
  632. WHERE name = ? ", array($name));
  633. $cur = db_fetch_array($res);
  634. @$val = unserialize($cur["value"]);
  635. // Save back to our cache...
  636. $GLOBALS["fp_system_settings"][$name] = $val;
  637. }
  638. if (!$val) {
  639. $val = $default_value;
  640. }
  641. // We must have this down here again, just in case what got stored in the GLOBALS
  642. // array was this placeholder. This can happen, because the settings file doesn't do
  643. // this check when assembling this variable on page load. It's something that needs
  644. // to be fixed.
  645. if ($val === "BOOLEAN_FALSE_PLACEHOLDER") {
  646. $val = FALSE;
  647. }
  648. if ($val === "NULL_PLACEHOLDER") {
  649. $val = NULL;
  650. }
  651. return $val;
  652. }
  653. /**
  654. * Get a value for a particular school.
  655. * if bool_ignore_override is TRUE, then we won't worry if it's being overwritten or not.
  656. */
  657. function variable_get_for_school($name, $default_value = "", $school_id = 0, $bool_ignore_override = FALSE) {
  658. $school_id = intval($school_id);
  659. // If this is the default school, OR, we are using NOT overriding the default vals for this school, then use variable_get normally
  660. if ($school_id === 0 || ($bool_ignore_override == FALSE && variable_get("school_override__$name~~school_" . $school_id, '') !== 'yes')) {
  661. return variable_get($name, $default_value); // for default school, just work normally.
  662. }
  663. if ($bool_ignore_override == TRUE && !variable_exists("school_override__$name~~school_" . $school_id)) {
  664. return variable_get($name, $default_value); // Can't find the override anyway, so get the default.
  665. }
  666. // If we are here, we need to get using a fieldname suffix with the school id.
  667. return variable_get($name . "~~school_" . $school_id, $default_value);
  668. }
  669. /**
  670. * Set a variable value, so we can retrieve it later on.
  671. *
  672. * This will write to our variables database table, as well as store in a cache
  673. * array for quick look-up later.
  674. *
  675. * @param unknown_type $name
  676. * @param unknown_type $value
  677. */
  678. function variable_set($name, $value) {
  679. // Save back to our "cache" GLOBALS array:
  680. $GLOBALS["fp_system_settings"][$name] = $value;
  681. // Boolean FALSE presents unusual problems when we try to tell if it got unserialized correctly.
  682. // We will convert it to a placeholder so we can positively store it.
  683. if ($value === FALSE) {
  684. $value = "BOOLEAN_FALSE_PLACEHOLDER";
  685. }
  686. // Same for NULL value
  687. if ($value === NULL) {
  688. $value = "NULL_PLACEHOLDER";
  689. }
  690. db_query("DELETE FROM variables WHERE name = ?", $name);
  691. db_query("INSERT INTO variables (name, value)
  692. VALUES (?, ?) ", $name, serialize($value));
  693. }
  694. /**
  695. * Remove a variable entirely from the table
  696. */
  697. function variable_delete($name) {
  698. unset($GLOBALS["fp_system_settings"][$name]);
  699. db_query("DELETE FROM variables WHERE name = ?", $name);
  700. }
  701. /**
  702. * Re-query the modules table and re-add to our global array.
  703. */
  704. function fp_rebuild_modules_list($reinclude = TRUE) {
  705. unset($GLOBALS["fp_system_settings"]["modules"]);
  706. $res = db_query("SELECT * FROM modules WHERE enabled = 1
  707. ORDER BY weight");
  708. while ($cur = db_fetch_array($res)) {
  709. $GLOBALS["fp_system_settings"]["modules"][$cur["name"]] = $cur;
  710. if ($reinclude) {
  711. include_module($cur["name"], FALSE);
  712. }
  713. }
  714. }
  715. function z__fp_get_system_settings($force_rebuild = FALSE) {
  716. depricated_message("Using fp_get_system_settings() is deprecated.");
  717. if ($force_rebuild == FALSE && isset($GLOBALS["fp_system_settings"])) {
  718. return $GLOBALS["fp_system_settings"];
  719. }
  720. // Get all of our settings from the variables table.
  721. $res = db_query("SELECT * FROM variables");
  722. while ($cur = db_fetch_array($res)) {
  723. $name = $cur["name"];
  724. $val = unserialize($cur["value"]);
  725. if ($val == "BOOLEAN_FALSE_PLACEHOLDER") {
  726. $val = FALSE;
  727. }
  728. $settings[$name] = $val;
  729. $GLOBALS["fp_system_settings"][$name] = $val;
  730. }
  731. // Make sure some important settings have _something_ set, or else it could cause
  732. // problems for some modules.
  733. if ($settings["current_catalog_year"] == "") {
  734. $settings["current_catalog_year"] = 2006;
  735. }
  736. if ($settings["earliest_catalog_year"] == "") {
  737. $settings["earliest_catalog_year"] = 2006;
  738. }
  739. $GLOBALS["fp_system_variables"] = $settings;
  740. return $settings;
  741. }

Functions

Namesort descending Description
db_affected_rows
db_fetch_array
db_fetch_object
db_get_cwid_from_user_id
db_get_school_id_for_student_id
db_get_school_id_for_user_id
db_get_user_id Look 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_id_from_email
db_get_user_id_from_user_name
db_get_user_setting Return a specific setting's value, based on the var_name given.
db_get_user_settings Return the array from the user_settings table.
db_insert_id
db_num_rows
db_query
db_query_range Adds a simple LIMIT statement to a query. Adapted from Drupal 6: https://api.drupal.org/api/drupal/includes%21database.mysql.inc/function...
db_result Returns back the first result from a resource_handler.
db_set_user_setting
db_table_exists Returns TRUE if the table specified exists or not.
fp_get_degree_advising_weight
fp_get_degree_major_code Returns the major code for a given degree_id
fp_get_degree_title Quick method to look up title for a degree.
fp_get_faculty_email
fp_get_faculty_name Returns the faculty member's name based on the ID provided.
fp_get_max_catalog_repeats_for_course Figure out the maximum number of times this course can be repeated for credit, based on what is stored in the course catalog.
fp_get_permissions_for_role
fp_get_student_email
fp_get_student_majors Return back the codes or records for a student's degrees, based on what is in the student_degrees table (thanks to system.module), as well as what we get from hooks.
fp_get_student_name
fp_get_user_timezone Returns back the timezone for this user, if selected. If not, we return back the system timezone.
fp_load_user Returns 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_list Re-query the modules table and re-add to our global array.
get_global_database_handler This method will return a globally-set DatabaseHandler object, creating it if it does not already exist. This is for efficiency reasons, so every module or method does not need to keep creating databasehandler objects (and re-connecting to the database).
pager_query Code adapted from Drupal 6: https://api.drupal.org/api/drupal/includes%21pager.inc/function/pager_qu...
variable_delete Remove a variable entirely from the table
variable_exists Returns TRUE or FALSE if the variable exists in the variables table.
variable_get Get 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_get_for_school Get a value for a particular school. if bool_ignore_override is TRUE, then we won't worry if it's being overwritten or not.
variable_set Set a variable value, so we can retrieve it later on.
watchdog Add a log entry to the watchdog (log) table.
z__fp_get_system_settings