db.inc

  1. 6.x includes/db.inc
  2. 7.x includes/db.inc
  3. 4.x includes/db.inc
  4. 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. $rtn->is_student = FALSE;
  301. $rtn->is_faculty = FALSE;
  302. $rtn->is_disabled = FALSE;
  303. return $rtn;
  304. }
  305. $res = db_query("SELECT * FROM users WHERE user_id = ? ", $user_id);
  306. if (db_num_rows($res) == 0) return NULL;
  307. $cur = db_fetch_object($res);
  308. $rtn->id = $cur->user_id;
  309. $rtn->name = $cur->user_name;
  310. $rtn->f_name = $cur->f_name;
  311. $rtn->l_name = $cur->l_name;
  312. $rtn->school_id = intval($cur->school_id);
  313. $rtn->email = trim(strtolower($cur->email));
  314. $rtn->cwid = $cur->cwid;
  315. $rtn->is_student = (bool) $cur->is_student;
  316. $rtn->is_faculty = (bool) $cur->is_faculty;
  317. $rtn->is_disabled = (bool) $cur->is_disabled;
  318. $rtn->last_login = (int) $cur->last_login;
  319. $rtn->roles = array();
  320. $rtn->permissions = array();
  321. // Load the user's roles and
  322. $res = db_query("SELECT * FROM user_roles a,
  323. roles c
  324. WHERE a.user_id = ?
  325. AND a.rid = c.rid ", array($user_id));
  326. while($cur = db_fetch_array($res)) {
  327. if (!isset($rtn->roles[$cur['rid']])) {
  328. $rtn->roles[$cur["rid"]] = $cur["name"];
  329. }
  330. }
  331. // Let's make sure we get the authenticated user role as well, #2.
  332. $rtn->roles[2] = "authenticated user";
  333. // Go through each role and add in the permissions for each role.
  334. foreach ($rtn->roles as $rid => $val) {
  335. $perms = fp_get_permissions_for_role($rid);
  336. // Merge the arrays while KEEPING the original's key. So don't
  337. // use array_merge, use the + operator.
  338. $rtn->permissions = $rtn->permissions + $perms;
  339. }
  340. // Load settings from the user_settings table (if any exist)
  341. $rtn->settings = array();
  342. $res = db_query("SELECT * FROM user_settings WHERE user_id = ?", array($user_id));
  343. while ($cur = db_fetch_array($res)) {
  344. $rtn->settings[$cur['name']] = $cur['value'];
  345. }
  346. // Set the user image url...
  347. // See if there are any other modules which might be overriding how we look up user images.
  348. $image_url = @$cur->settings['image_url'];
  349. $type = 'student';
  350. if ($rtn->is_faculty) $type = 'faculty';
  351. $modules = invoke_hook("get_user_image_url", array($rtn->cwid, $type));
  352. // 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).
  353. foreach ($modules as $val) {
  354. $image_url = trim($val);
  355. }
  356. $rtn->settings['image_url'] = $image_url;
  357. // load attributes from user_attributes table (if any exist)
  358. $rtn->attributes = array();
  359. $res = db_query("SELECT * FROM user_attributes WHERE user_id = ?", array($user_id));
  360. while ($cur = db_fetch_array($res)) {
  361. $rtn->attributes[$cur['name']] = $cur['value'];
  362. }
  363. return $rtn;
  364. }
  365. /**
  366. * Returns back the timezone for this user, if selected. If not, we return back the system timezone.
  367. */
  368. function fp_get_user_timezone($account = NULL) {
  369. global $user;
  370. if (is_numeric($account)) {
  371. $account = fp_load_user($account);
  372. }
  373. if ($account == NULL) {
  374. $account = $user;
  375. }
  376. $utz = NULL;
  377. if (isset($account->settings['timezone'])) {
  378. $utz = trim(@$account->settings['timezone']);
  379. }
  380. if ($utz) return $utz;
  381. // Else...
  382. $system_timezone = variable_get('system_timezone', 'America/Chicago');
  383. return $system_timezone;
  384. }
  385. /**
  386. * Look up the user_id based on the the user_name. Returns FALSE if it cannot find it.
  387. *
  388. * @param unknown_type $user_name
  389. */
  390. function db_get_user_id($user_name) {
  391. $user_id = db_result(db_query("SELECT user_id FROM users WHERE user_name = '?' ", $user_name));
  392. if ($user_id) {
  393. return $user_id;
  394. }
  395. return FALSE;
  396. }
  397. function db_get_cwid_from_user_id($user_id) {
  398. return db_result(db_query("SELECT cwid FROM users WHERE user_id = ?", array($user_id)));
  399. }
  400. function db_get_user_id_from_cwid($cwid, $type = "faculty") {
  401. $type_line = " is_faculty='1' ";
  402. if ($type == "student") {
  403. $type_line = " is_student='1' ";
  404. }
  405. $user_id = db_result(db_query("SELECT user_id FROM users WHERE cwid = ?
  406. AND $type_line ", $cwid));
  407. if ($user_id) {
  408. return $user_id;
  409. }
  410. return FALSE;
  411. }
  412. function db_get_user_id_from_email($email, $type = "faculty") {
  413. $type_line = " is_faculty='1' ";
  414. if ($type == "student") {
  415. $type_line = " is_student='1' ";
  416. }
  417. // Force email to be lowercase
  418. $email = trim(strtolower($email));
  419. $user_id = db_result(db_query("SELECT user_id FROM users WHERE email = ? AND $type_line ", $email));
  420. if ($user_id) {
  421. return $user_id;
  422. }
  423. return FALSE;
  424. }
  425. function db_get_user_id_from_user_name($user_name, $type = "faculty") {
  426. $type_line = " is_faculty='1' ";
  427. if ($type == "student") {
  428. $type_line = " is_student='1' ";
  429. }
  430. // Force to be lowercase
  431. $user_name = trim(strtolower($user_name));
  432. $user_id = db_result(db_query("SELECT user_id FROM users WHERE user_name = ? AND $type_line ", $user_name));
  433. if ($user_id) {
  434. return $user_id;
  435. }
  436. return FALSE;
  437. }
  438. /**
  439. Return back the codes or records for a student's degrees, based on what is in the
  440. student_degrees table (thanks to system.module), as well as what we get from hooks.
  441. */
  442. 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) {
  443. $params = array($student_cwid, $bool_return_as_full_record, $perform_join_with_degrees, $bool_skip_directives, $bool_check_for_allow_dynamic);
  444. // Execute hook for this too.
  445. $arr = invoke_hook("fp_get_student_majors", $params);
  446. // Results will appear in an array, with each module as the index.
  447. // Ex:
  448. // system
  449. // MAJOR1 => MAJOR1
  450. // MAJOR2 => MAJOR2
  451. // custom_module
  452. // MAJOR1 => MAJOR1
  453. // XYZ => XYZ
  454. $new_arr = array();
  455. $csv = "";
  456. // Go through our modules, combine the identical ones, then prepare as a possible CSV.
  457. foreach ($arr as $module => $results) {
  458. foreach ($results as $k => $v) {
  459. $new_arr[$k] = $v;
  460. }
  461. }
  462. $rtn = $new_arr;
  463. // Returning as a CSV?
  464. if ($bool_return_as_csv) {
  465. foreach ($new_arr as $k => $v) {
  466. $csv .= $k . ",";
  467. }
  468. $csv = rtrim($csv, ",");
  469. $rtn = $csv;
  470. }
  471. return $rtn;
  472. }
  473. function fp_get_student_name($cwid, $bool_include_cwid = FALSE ) {
  474. if (is_numeric($cwid) && ($cwid === 0 || $cwid === '0')) {
  475. return t("Anonymous");
  476. }
  477. // Already cached?
  478. if (isset($GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)])) {
  479. return $GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)];
  480. }
  481. $db = get_global_database_handler();
  482. $name = $db->get_student_name($cwid, $bool_include_cwid);
  483. if (!$name) $name = t("Unknown Student");
  484. $GLOBALS['fp_cache_get_student_name'][$cwid][intval($bool_include_cwid)] = $name;
  485. return $name;
  486. }
  487. function fp_get_student_email($cwid) {
  488. $email = db_result(db_query("SELECT email FROM users
  489. WHERE cwid = ?
  490. AND is_student = 1", array($cwid)));
  491. if (trim($email)) {
  492. return trim(strtolower($email));
  493. }
  494. // else, email wasn't set.
  495. return FALSE;
  496. }
  497. function fp_get_faculty_email($cwid) {
  498. $email = db_result(db_query("SELECT email FROM users
  499. WHERE cwid = ?
  500. AND is_faculty = 1", array($cwid)));
  501. if (trim($email)) {
  502. return trim(strtolower($email));
  503. }
  504. // else, email wasn't set.
  505. return FALSE;
  506. }
  507. function fp_get_permissions_for_role($rid) {
  508. $rtn = array();
  509. $res = db_query("SELECT pid, perm FROM role_permissions
  510. WHERE rid = ?
  511. GROUP BY perm
  512. ORDER BY perm", array($rid));
  513. while($cur = db_fetch_array($res)) {
  514. if (!in_array($cur['perm'], $rtn)) {
  515. $rtn[$cur["pid"]] = $cur["perm"];
  516. }
  517. }
  518. return $rtn;
  519. }
  520. /**
  521. * Returns back the first result from a resource_handler.
  522. */
  523. function db_result($res) {
  524. if (is_object($res)) {
  525. return $res->fetchColumn(); // fetches the first value returned.
  526. }
  527. return NULL;
  528. }
  529. function db_insert_id() {
  530. $db = get_global_database_handler();
  531. return $db->db_insert_id();
  532. }
  533. /**
  534. * Return the array from the user_settings table.
  535. *
  536. * @param unknown_type $user_id
  537. */
  538. function db_get_user_settings($user_id) {
  539. $db = get_global_database_handler();
  540. return $db->get_user_settings($user_id);
  541. }
  542. function db_get_school_id_for_student_id($student_id) {
  543. $db = get_global_database_handler();
  544. return $db->get_school_id_for_student_id($student_id);
  545. }
  546. function db_get_school_id_for_user_id($user_id) {
  547. $db = get_global_database_handler();
  548. return $db->get_school_id_for_user_id($user_id);
  549. }
  550. /**
  551. * Return a specific setting's value, based on the var_name given.
  552. *
  553. * @param unknown_type $user_id
  554. * @param unknown_type $var_name
  555. */
  556. function db_get_user_setting($user_id, $var_name = "", $default_value = "") {
  557. $settings = db_get_user_settings($user_id);
  558. $val = @$settings[$var_name];
  559. if (!$val) {
  560. $val = $default_value;
  561. }
  562. return $val;
  563. }
  564. function db_set_user_setting($user_id, $var_name, $value) {
  565. $settings = db_get_user_settings($user_id);
  566. $settings[$var_name] = $value;
  567. $ser = serialize($settings);
  568. db_query("DELETE FROM user_settings WHERE user_id = '?' ", $user_id);
  569. db_query("INSERT INTO user_settings (user_id, settings, posted)
  570. VALUES ('?', '?', '?') ", $user_id, $ser, time());
  571. }
  572. function db_query($query) {
  573. // Capture arguments to this function, to pass along to our $db object.
  574. $args = func_get_args();
  575. array_shift($args);
  576. $db = get_global_database_handler();
  577. $res = $db->db_query($query, $args);
  578. return $res;
  579. }
  580. function db_fetch_array($result_handler) {
  581. $db = get_global_database_handler();
  582. return $db->db_fetch_array($result_handler);
  583. }
  584. function db_fetch_object($result_handler) {
  585. $db = get_global_database_handler();
  586. return $db->db_fetch_object($result_handler);
  587. }
  588. function db_num_rows($result_handler) {
  589. $db = get_global_database_handler();
  590. return $db->db_num_rows($result_handler);
  591. }
  592. function db_affected_rows($result_handler) {
  593. $db = get_global_database_handler();
  594. return $db->db_affected_rows($result_handler);
  595. }
  596. /**
  597. * Returns TRUE if the table specified exists or not.
  598. */
  599. function db_table_exists($table_name) {
  600. $res = db_query("SHOW TABLES LIKE ? ", $table_name);
  601. $cur = db_fetch_array($res);
  602. if ($cur[0] == $table_name) {
  603. return TRUE;
  604. }
  605. return FALSE;
  606. }
  607. /**
  608. * Returns TRUE or FALSE if the variable exists in the variables table.
  609. */
  610. function variable_exists($name) {
  611. if (isset($GLOBALS["fp_system_settings"][$name])) {
  612. return TRUE;
  613. }
  614. $count = intval(db_result(db_query("SELECT COUNT(name) as my_count
  615. FROM variables
  616. WHERE name = ?", array($name))));
  617. if ($count > 0) return TRUE;
  618. // else....
  619. return FALSE;
  620. }
  621. /**
  622. * Get a variable from the database. We will first look in our GLOBALS array,
  623. * to see that it hasn't already been retrieved this page load.
  624. *
  625. * @param string $name
  626. * @param mixed $default_value
  627. * @return mixed
  628. */
  629. function variable_get($name, $default_value = "") {
  630. $val = null;
  631. // First, check in our GLOBALS array, like a cache...
  632. if (isset($GLOBALS["fp_system_settings"][$name])) {
  633. $val = $GLOBALS["fp_system_settings"][$name];
  634. }
  635. else {
  636. // Not found-- look in the database for it.
  637. $res = db_query("SELECT value FROM variables
  638. WHERE name = ? ", array($name));
  639. $cur = db_fetch_array($res);
  640. @$val = unserialize($cur["value"]);
  641. // Save back to our cache...
  642. $GLOBALS["fp_system_settings"][$name] = $val;
  643. }
  644. if (!$val) {
  645. $val = $default_value;
  646. }
  647. // We must have this down here again, just in case what got stored in the GLOBALS
  648. // array was this placeholder. This can happen, because the settings file doesn't do
  649. // this check when assembling this variable on page load. It's something that needs
  650. // to be fixed.
  651. if ($val === "BOOLEAN_FALSE_PLACEHOLDER") {
  652. $val = FALSE;
  653. }
  654. if ($val === "NULL_PLACEHOLDER") {
  655. $val = NULL;
  656. }
  657. return $val;
  658. }
  659. /**
  660. * Get a value for a particular school.
  661. * if bool_ignore_override is TRUE, then we won't worry if it's being overwritten or not.
  662. */
  663. function variable_get_for_school($name, $default_value = "", $school_id = 0, $bool_ignore_override = FALSE) {
  664. $school_id = intval($school_id);
  665. // If this is the default school, OR, we are using NOT overriding the default vals for this school, then use variable_get normally
  666. if ($school_id === 0 || ($bool_ignore_override == FALSE && variable_get("school_override__$name~~school_" . $school_id, '') !== 'yes')) {
  667. return variable_get($name, $default_value); // for default school, just work normally.
  668. }
  669. if ($bool_ignore_override == TRUE && !variable_exists("school_override__$name~~school_" . $school_id)) {
  670. return variable_get($name, $default_value); // Can't find the override anyway, so get the default.
  671. }
  672. // If we are here, we need to get using a fieldname suffix with the school id.
  673. return variable_get($name . "~~school_" . $school_id, $default_value);
  674. }
  675. /**
  676. * Set a variable value, so we can retrieve it later on.
  677. *
  678. * This will write to our variables database table, as well as store in a cache
  679. * array for quick look-up later.
  680. *
  681. * @param unknown_type $name
  682. * @param unknown_type $value
  683. */
  684. function variable_set($name, $value) {
  685. // Save back to our "cache" GLOBALS array:
  686. $GLOBALS["fp_system_settings"][$name] = $value;
  687. // Boolean FALSE presents unusual problems when we try to tell if it got unserialized correctly.
  688. // We will convert it to a placeholder so we can positively store it.
  689. if ($value === FALSE) {
  690. $value = "BOOLEAN_FALSE_PLACEHOLDER";
  691. }
  692. // Same for NULL value
  693. if ($value === NULL) {
  694. $value = "NULL_PLACEHOLDER";
  695. }
  696. db_query("DELETE FROM variables WHERE name = ?", $name);
  697. db_query("INSERT INTO variables (name, value)
  698. VALUES (?, ?) ", $name, serialize($value));
  699. }
  700. /**
  701. * Remove a variable entirely from the table
  702. */
  703. function variable_delete($name) {
  704. unset($GLOBALS["fp_system_settings"][$name]);
  705. db_query("DELETE FROM variables WHERE name = ?", $name);
  706. }
  707. /**
  708. * Re-query the modules table and re-add to our global array.
  709. */
  710. function fp_rebuild_modules_list($reinclude = TRUE) {
  711. unset($GLOBALS["fp_system_settings"]["modules"]);
  712. $res = db_query("SELECT * FROM modules WHERE enabled = 1
  713. ORDER BY weight");
  714. while ($cur = db_fetch_array($res)) {
  715. $GLOBALS["fp_system_settings"]["modules"][$cur["name"]] = $cur;
  716. if ($reinclude) {
  717. include_module($cur["name"], FALSE);
  718. }
  719. }
  720. }
  721. function z__fp_get_system_settings($force_rebuild = FALSE) {
  722. depricated_message("Using fp_get_system_settings() is deprecated.");
  723. if ($force_rebuild == FALSE && isset($GLOBALS["fp_system_settings"])) {
  724. return $GLOBALS["fp_system_settings"];
  725. }
  726. // Get all of our settings from the variables table.
  727. $res = db_query("SELECT * FROM variables");
  728. while ($cur = db_fetch_array($res)) {
  729. $name = $cur["name"];
  730. $val = unserialize($cur["value"]);
  731. if ($val == "BOOLEAN_FALSE_PLACEHOLDER") {
  732. $val = FALSE;
  733. }
  734. $settings[$name] = $val;
  735. $GLOBALS["fp_system_settings"][$name] = $val;
  736. }
  737. // Make sure some important settings have _something_ set, or else it could cause
  738. // problems for some modules.
  739. if ($settings["current_catalog_year"] == "") {
  740. $settings["current_catalog_year"] = 2006;
  741. }
  742. if ($settings["earliest_catalog_year"] == "") {
  743. $settings["earliest_catalog_year"] = 2006;
  744. }
  745. $GLOBALS["fp_system_variables"] = $settings;
  746. return $settings;
  747. }

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