DatabaseHandler.php

  1. 7.x classes/DatabaseHandler.php
  2. 6.x classes/DatabaseHandler.php
  3. 5.x custom/classes/DatabaseHandler.php

File

classes/DatabaseHandler.php
View source
  1. <?php
  2. class DatabaseHandler extends stdClass
  3. {
  4. public $pdo;
  5. function __construct()
  6. {
  7. $this->pdo = $GLOBALS['pdo']; // set in our settings.php file.
  8. /////////////////// NOTE /////////////////////
  9. // The actual PDO connection happens in custom/settings.php.
  10. }
  11. /**
  12. * This is a PHP "magic" function. Called during a serialize command.
  13. * Basically, we aren't trying to save any local variables.
  14. * In fact, we will get a fatal exception if we try to serialize our PDO connection.
  15. */
  16. function __sleep() {
  17. return array();
  18. }
  19. /**
  20. * This function is called when this objectis unserialized. We want to reconnect to the database, so we'll call our constructor.
  21. */
  22. function __wakeup() {
  23. $this->__construct();
  24. }
  25. /**
  26. * Sets the maintenance mode. $val should be either 0 (off) or 1 (on)
  27. *
  28. * @param integer $val
  29. */
  30. function set_maintenance_mode($val)
  31. {
  32. // Convenience function for setting maintenance mode. 0 = off, 1 = on.
  33. $this->set_settings_variable("maintenance_mode", $val);
  34. }
  35. function get_substitution_details($sub_id)
  36. {
  37. // Simply returns an associative array containing
  38. // the details of a substitution. The subID specified
  39. // is the actual id of the row of the database in
  40. // flightpath.student_substitutions.
  41. $rtn_array = array();
  42. $res = $this->db_query("SELECT * FROM student_substitutions
  43. WHERE id = '?' ", $sub_id);
  44. if ($this->db_num_rows($res) > 0)
  45. {
  46. $cur = $this->db_fetch_array($res);
  47. $rtn_array["faculty_id"] = $cur["faculty_id"];
  48. $rtn_array["remarks"] = trim($cur["sub_remarks"]);
  49. $rtn_array["sub_hours"] = $cur["sub_hours"];
  50. $rtn_array["required_course_id"] = $cur["required_course_id"];
  51. $rtn_array["required_group_id"] = $cur["required_group_id"];
  52. $rtn_array["posted"] = $cur["posted"];
  53. $rtn_array["required_degree_id"] = $cur["required_degree_id"];
  54. $rtn_array["db_record"] = $cur;
  55. }
  56. return $rtn_array;
  57. }
  58. function get_developmental_requirements($student_cwid)
  59. {
  60. // returns an array which states whether or not the student
  61. // requires any developmental requirements.
  62. $rtn_array = array();
  63. $res = $this->db_query("SELECT * FROM student_developmentals
  64. WHERE student_id = ?
  65. ", $student_cwid);
  66. while($cur = $this->db_fetch_array($res)) {
  67. $rtn_array[] = $cur["requirement"];
  68. }
  69. return $rtn_array;
  70. }
  71. /**
  72. * This is a simple helper function which "escapes" the question marks (?) in
  73. * the string, by changing them to "??". This makes it suitable for use
  74. * within db_query(), but not necessary if used as an argument. Ex:
  75. * db_query("INSERT ... '" . $db->escape_question_marks($xml) . "' "); is good.
  76. * db_query("INSERT ... '?' ", $xml); is good. This function not needed.
  77. *
  78. * @param unknown_type $str
  79. */
  80. function escape_question_marks($str) {
  81. $rtn = str_replace("?", "??", $str);
  82. return $rtn;
  83. }
  84. /**
  85. * This function is used to perform a database query. It uses PDO execute, which will
  86. * take automatically replace ? with variables you supply as the arguments to this function,
  87. * or as an array to this function. Either will work.
  88. * Do this by using ?, or naming the variable like :name or :age.
  89. *
  90. * For example:
  91. * $result = $db->db_query("SELECT * FROM table WHERE name = ? and age = ? ", $name, $temp_age);
  92. * or
  93. * $result = $db->db_query("SELECT * FROM table WHERE name = ? AND age = ? ", array($name, $temp_age));
  94. * or
  95. * $result = $db->db_query("SELECT * FROM table WHERE name = :name ", array(":name" => $name));
  96. *
  97. * @param unknown_type $sql_query
  98. * @return unknown
  99. */
  100. function db_query($sql_query, $args = array()) {
  101. // If there were any arguments to this function, then we must first apply
  102. // replacement patterns.
  103. $args = func_get_args();
  104. array_shift($args);
  105. if (isset($args[0]) && is_array($args[0])) {
  106. // If the first argument was an array, it means we passed an array of values instead
  107. // of passing them directly. So use them directly as our args.
  108. $args = $args[0];
  109. // If we were supplied an array, then we need to see if the NEW args[0] is an array... If it is, grab the first element AGAIN.
  110. if (isset($args[0]) && is_array($args[0])) {
  111. $args = $args[0];
  112. }
  113. }
  114. // We need to make sure that arguments are passed without being contained in single quotes ('?'). Should be just ?
  115. $sql_query = str_replace("'?'", "?", $sql_query);
  116. // If $c (number of replacements performed) does not match the number of replacements
  117. // specified, warn the user.
  118. /*
  119. * Don't do this anymore, as it might throw off queries that don't use ?'s, but instead use :var as the replacements.
  120. *
  121. if (substr_count($sql_query, "?") != count($args)) {
  122. fpm("<br><b>WARNING:</b> Replacement count does not match what was supplied to query: $sql_query<br><br>");
  123. }
  124. */
  125. //////////////////////////////////////////////
  126. // Run the sqlQuery and return the result set.
  127. if (!isset($this->pdo) || $this->pdo == NULL) fpm(debug_backtrace());
  128. try {
  129. $result = $this->pdo->prepare($sql_query);
  130. $result->execute($args);
  131. $_SESSION["fp_last_insert_id"] = $this->pdo->lastInsertId(); // capture last insert id, in case we ask for it later.
  132. return $result;
  133. }
  134. catch (Exception $ex) {
  135. // Some error happened!
  136. $this->db_error($ex);
  137. }
  138. } // db_query
  139. /**
  140. * Draw out the error onto the screen.
  141. *
  142. */
  143. function db_error(Exception $ex)
  144. {
  145. global $user;
  146. $arr = $ex->getTrace();
  147. $when_ts = convert_time(time());
  148. $when_english = format_date($when_ts);
  149. $message = $ex->getMessage();
  150. // If the message involves a complaint about the sql_mode, point the user to a
  151. // help page about setting the sql_mode.
  152. if (stristr($message, "sql_mode=")) {
  153. $message .= "<br><br><b>" . t("It appears this error is being caused because of your server's sql_mode setting.") . "</b> ";
  154. $message .= t("To set your sql_mode for MySQL, please see the following help page: <a href='http://getflightpath.com/node/1161' target='_blank'>http://getflightpath.com/node/1161</a>");
  155. }
  156. $file = $arr[2]["file"];
  157. if (strlen($file) > 50) {
  158. $file = "..." . substr($file, strlen($file) - 50);
  159. }
  160. $file_and_line = "Line " . $arr[2]["line"] . ": " . $file;
  161. @$query_and_args = print_r($arr[2]['args'], TRUE);
  162. // If we are on production, email someone!
  163. if (variable_get("notify_mysql_error_email_address",'') != "")
  164. {
  165. $server = @$_SERVER["SERVER_NAME"] . " - " . $GLOBALS['fp_system_settings']['base_url']; // intentionally use the GLOBALS here, since it comes from settings.php file.
  166. $email_msg = t("A MYSQL error has occured in FlightPath.") . "
  167. User: $user->name ($user->id)
  168. Server: $server
  169. Timestamp: $when_ts ($when_english)
  170. *** Error: ***
  171. $message
  172. /-----------------------------------/
  173. *** Location: ***
  174. $file_and_line
  175. /-----------------------------------/
  176. *** Query/Args: ***
  177. $query_and_args
  178. /-----------------------------------/
  179. *** Limited Backtrace: ***
  180. " . print_r($arr, true) . "
  181. ";
  182. fp_mail(variable_get("notify_mysql_error_email_address",''), "FlightPath MYSQL Error Reported on $server", $email_msg);
  183. }
  184. fpm(t("A MySQL error has occured:") . " $message<br><br>" . t("Location:") . " $file_and_line<br><br>" . t("The backtrace:"));
  185. fpm($arr);
  186. if (@$GLOBALS["fp_die_mysql_errors"] == TRUE) {
  187. print "\n<br>The script has stopped executing because of a MySQL error:
  188. $message<br>
  189. Location: $file_and_line<br>\n
  190. Please fix the error and try again.<br>\n";
  191. print "<br><br>Timestamp: $when_ts ($when_english)
  192. <br><br>Program backtrace:
  193. <pre>" . print_r($arr, true) . "</pre>";
  194. die;
  195. }
  196. // Also, check to see if the mysql_err is because of a lost connection, as in, the
  197. // server went down. In that case, we should also terminate immediately, rather
  198. // than risk spamming an email recipient with error emails.
  199. if (stristr($message, "Lost connection to MySQL server")
  200. || stristr($message, "MySQL server has gone away")) {
  201. print "<h2 style='font-family: Arial, sans serif;'>Database Connection Error</h2>
  202. <br>
  203. <div style='font-size: 1.2em; font-family: Arial, sans serif; padding-left: 30px;
  204. padding-right: 30px;'>
  205. Sorry, but it appears the database is currently unavailable. This may
  206. simply be part of scheduled maintenance to the database server. Please
  207. try again in a few minutes. If the problem persists for longer
  208. than an hour, contact your technical support
  209. staff.
  210. </div>
  211. ";
  212. // DEV: Comment out when not needed.
  213. // print "<pre>" . print_r($arr, TRUE) . "</pre>";
  214. die;
  215. }
  216. } // db_error
  217. function request_new_group_id()
  218. {
  219. // Return a valid new group_id...
  220. for ($t = 0; $t < 1000; $t++)
  221. {
  222. $id = mt_rand(1, 2147483640); // A few less than the max for a signed int in mysql.
  223. // Check for collisions...
  224. $res4 = $this->db_query("SELECT * FROM draft_group_requirements
  225. WHERE group_id = $id LIMIT 1");
  226. if ($this->db_num_rows($res4) == 0)
  227. { // Was not in the table already, so use it!
  228. return $id;
  229. }
  230. }
  231. return false;
  232. }
  233. /**
  234. * Generates a new advising session token and makes sure it is unique before returning it.
  235. */
  236. function request_new_advising_session_token() {
  237. for ($t = 0; $t < 1000; $t++) { // try up to 1000 times
  238. $test_token = hash('sha256', mt_rand(0, 99999) . microtime() . mt_rand(0,99999));
  239. // check for collisions
  240. $res = $this->db_query("SELECT advising_session_id
  241. FROM advising_sessions
  242. WHERE advising_session_token = ?", array($test_token));
  243. if ($this->db_num_rows($res) == 0) {
  244. // Was not in the table, so we can use it.
  245. return $test_token;
  246. }
  247. }
  248. return FALSE; // some kind of problem-- we never found an available token!
  249. }
  250. function request_new_course_id()
  251. {
  252. // Return a valid new course_id...
  253. for ($t = 0; $t < 1000; $t++)
  254. {
  255. $id = mt_rand(1, 2147483640); // A few less than the max for a signed int in mysql.
  256. // Check for collisions...
  257. $res4 = $this->db_query("SELECT * FROM draft_courses
  258. WHERE course_id = $id LIMIT 1");
  259. if ($this->db_num_rows($res4) == 0)
  260. { // Was not in the table already, so use it!
  261. return $id;
  262. }
  263. }
  264. return false;
  265. }
  266. function load_course_descriptive_data($course = null, $course_id = 0)
  267. {
  268. $school_id = 0;
  269. if ($course == NULL) {
  270. $school_id = $this->get_school_id_for_course_id($course_id);
  271. }
  272. else {
  273. $school_id = $this->get_school_id_for_course_id($course->course_id);
  274. }
  275. $current_catalog_year = variable_get_for_school("current_catalog_year", "2006", $school_id);
  276. $catalog_year = $current_catalog_year; // currentCatalogYear.
  277. if ($course != null)
  278. {
  279. $course_id = $course->course_id;
  280. $catalog_year = $course->catalog_year;
  281. }
  282. $array_valid_names = array();
  283. if ($course_id != 0)
  284. {
  285. $res = $this->db_query("SELECT * FROM courses
  286. WHERE course_id = '?'
  287. AND catalog_year = '?'
  288. AND catalog_year <= '?'
  289. AND delete_flag = '0'
  290. AND exclude = '0' ", $course_id, $catalog_year, $current_catalog_year);
  291. $cur = $this->db_fetch_array($res);
  292. if ($this->db_num_rows($res) < 1)
  293. {
  294. // No results found, so instead pick the most recent
  295. // catalog year that is not excluded (keeping below the
  296. // current catalog year from the settings)
  297. //$this2 = new DatabaseHandler();
  298. $res2 = $this->db_query("SELECT * FROM courses
  299. WHERE `course_id`='?'
  300. AND `subject_id`!=''
  301. AND `delete_flag` = '0'
  302. AND `exclude`='0'
  303. AND `catalog_year` <= '?'
  304. ORDER BY `catalog_year` DESC LIMIT 1", $course_id, $current_catalog_year);
  305. $cur = $this->db_fetch_array($res2);
  306. if ($this->db_num_rows($res2) < 1)
  307. {
  308. // Meaning, there were no results found that didn't have
  309. // the exclude flag set. So, as a last-ditch effort,
  310. // go ahead and try to retrieve any course, even if it has
  311. // been excluded. (keeping below the
  312. // current catalog year from the settings)
  313. //$this3 = new DatabaseHandler();
  314. //
  315. $res3 = $this->db_query("SELECT * FROM courses
  316. WHERE course_id = '?'
  317. AND subject_id != ''
  318. AND delete_flag = '0'
  319. AND catalog_year <= '?'
  320. ORDER BY catalog_year DESC LIMIT 1", $course_id, $current_catalog_year);
  321. $cur = $this->db_fetch_array($res3);
  322. }
  323. }
  324. $title = $cur["title"];
  325. $description = trim($cur["description"]);
  326. $subject_id = trim(strtoupper($cur["subject_id"]));
  327. $course_num = trim(strtoupper($cur["course_num"]));
  328. $cache_catalog_year = $cur['catalog_year'];
  329. $min_hours = $cur["min_hours"];
  330. $max_hours = $cur["max_hours"];
  331. $repeat_hours = $cur["repeat_hours"];
  332. if ($repeat_hours*1 == 0)
  333. {
  334. $repeat_hours = $max_hours;
  335. }
  336. $db_exclude = $cur["exclude"];
  337. $db_school_id = $cur['school_id'];
  338. $data_entry_comment = $cur["data_entry_comment"];
  339. // Now, lets get a list of all the valid names for this course.
  340. // In other words, all the non-excluded names. For most
  341. // courses, this will just be one name. But for cross-listed
  342. // courses, this will be 2 or more (probably just 2 though).
  343. // Example: MATH 373 and CSCI 373 are both valid names for that course.
  344. $res = $this->db_query("SELECT * FROM courses
  345. WHERE course_id = '?'
  346. AND exclude = 0
  347. AND delete_flag = 0 ", $course_id);
  348. while($cur = $this->db_fetch_array($res))
  349. {
  350. $si = $cur["subject_id"];
  351. $cn = $cur["course_num"];
  352. if (in_array("$si~$cn", $array_valid_names))
  353. {
  354. continue;
  355. }
  356. $array_valid_names[] = "$si~$cn";
  357. }
  358. }
  359. if ($description == "")
  360. {
  361. $description = "There is no course description available at this time.";
  362. }
  363. if ($title == "")
  364. {
  365. $title = "$subject_id $course_num";
  366. }
  367. // Now, to reduce the number of database calls in the future, save this
  368. // to our GLOBALS cache...
  369. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["subject_id"] = $subject_id;
  370. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["course_num"] = $course_num;
  371. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["title"] = $title;
  372. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["description"] = $description;
  373. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["min_hours"] = $min_hours;
  374. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["max_hours"] = $max_hours;
  375. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["repeat_hours"] = $repeat_hours;
  376. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["db_exclude"] = $db_exclude;
  377. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["school_id"] = $db_school_id;
  378. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["array_valid_names"] = $array_valid_names;
  379. $cache_catalog_year = 0;
  380. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["subject_id"] = $subject_id;
  381. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["course_num"] = $course_num;
  382. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["title"] = $title;
  383. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["description"] = $description;
  384. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["min_hours"] = $min_hours;
  385. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["max_hours"] = $max_hours;
  386. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["repeat_hours"] = $repeat_hours;
  387. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["db_exclude"] = $db_exclude;
  388. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["school_id"] = $db_school_id;
  389. $GLOBALS["fp_course_inventory"][$course_id][$cache_catalog_year]["array_valid_names"] = $array_valid_names;
  390. $GLOBALS["cache_course_inventory"] = true; // rebuild this cache before it closes.
  391. // Should we put all this into our course object?
  392. }
  393. /**
  394. Note that the $course object need not be a Course object, but rather a simple stdClass() with certain properties set.
  395. Specifically, the values we see being input into draft_courses in this function.
  396. */
  397. function duplicate_course_for_year($course = NULL, $catalog_year = 0, $bool_delete_existing = TRUE)
  398. {
  399. // Duplicate the course for the given catalog_year.
  400. // If it already exists for that catalog_year, delete it from the
  401. // table.
  402. // In other words, copy all course data from some valid year into this
  403. // new year.
  404. $c = $course;
  405. $course_id = $c->course_id;
  406. $min_hours = $c->min_hours;
  407. $max_hours = $c->max_hours;
  408. if (@$c->bool_ghost_min_hour) {
  409. $min_hours = 0;
  410. }
  411. if (@$c->bool_ghost_hour) {
  412. $max_hours = 0;
  413. }
  414. if ($bool_delete_existing) {
  415. $res = $this->db_query("DELETE FROM draft_courses
  416. WHERE
  417. course_id = ?
  418. AND catalog_year = ?
  419. AND subject_id = ?
  420. AND course_num = ?
  421. AND school_id = ? ", $course_id, $catalog_year, $c->subject_id, $c->course_num, $c->school_id);
  422. }
  423. $res2 = $this->db_query("INSERT INTO draft_courses(course_id,
  424. subject_id, course_num, catalog_year,
  425. title, description, min_hours, max_hours,
  426. repeat_hours, exclude, school_id) values (
  427. ?,?,?,?,?,?,?,?,?,?,?)
  428. ", $course_id, $c->subject_id,$c->course_num,$catalog_year,$c->title,$c->description,$min_hours,$max_hours,
  429. $c->repeat_hours,$c->db_exclude,$c->school_id);
  430. }
  431. function update_course_requirement_from_name($subject_id, $course_num, $new_course_id, $school_id = 0)
  432. {
  433. // This will convert all instances of subject_id/course_num
  434. // to use the newCourseID. It looks through the requirements tables
  435. // that may have listed it as a requirement. We will
  436. // look specifically at the data_entry_value to do some of them.
  437. // ************ IMPORTANT ****************
  438. // This is used only by dataentry. It is intentionally
  439. // not doing the draft tables!
  440. $res = $this->db_query("UPDATE degree_requirements
  441. set `course_id`= ?
  442. where `data_entry_value`= ?
  443. ", $new_course_id, "$subject_id~$course_num") ;
  444. $res = $this->db_query("UPDATE group_requirements
  445. SET `course_id`='?'
  446. WHERE `data_entry_value`= ?
  447. ", $new_course_id, "$subject_id~$course_num") ;
  448. // Also update substitutions....
  449. $res = $this->db_query("UPDATE student_substitutions
  450. SET `sub_course_id`='?'
  451. WHERE `sub_entry_value`= ?
  452. ", $new_course_id, "$subject_id~$course_num") ;
  453. $res = $this->db_query("UPDATE student_substitutions
  454. SET `required_course_id`='?'
  455. WHERE `required_entry_value`= ?
  456. ", $new_course_id, "$subject_id~$course_num") ;
  457. // Also the advising histories....
  458. $res = $this->db_query("UPDATE advised_courses
  459. SET `course_id`='?'
  460. WHERE `entry_value`= ?
  461. ", $new_course_id, "$subject_id~$course_num") ;
  462. }
  463. function add_draft_instruction($text)
  464. {
  465. // Adds a new "instruction" to the draft_instructions table.
  466. // Simple insert.
  467. $res = $this->db_query("INSERT INTO draft_instructions
  468. (instruction) VALUES ('?') ", $text);
  469. }
  470. function update_course_id($from_course_id, $to_course_id, $bool_draft = false)
  471. {
  472. // This will convert *all* instances of "fromCourseID"
  473. // across every table that it is used, to toCourseID.
  474. // Use this function when you want to change a course's
  475. // course_id in the database.
  476. $table_array = array("advised_courses",
  477. "courses",
  478. "degree_requirements",
  479. "group_requirements",
  480. "student_unassign_group");
  481. if ($bool_draft)
  482. { // only do the draft tables...
  483. $table_array = array(
  484. "draft_courses",
  485. "draft_degree_requirements",
  486. "draft_group_requirements",
  487. );
  488. }
  489. // Do the tables where it's named "course_id"...
  490. foreach($table_array as $table_name)
  491. {
  492. $res = $this->db_query("UPDATE $table_name
  493. SET course_id = '?'
  494. WHERE course_id = '?' ", $to_course_id, $from_course_id);
  495. }
  496. $res = $this->db_query("update student_substitutions
  497. set `required_course_id`='?'
  498. where `required_course_id`='?' ", $to_course_id, $from_course_id);
  499. $res = $this->db_query("update student_substitutions
  500. set `sub_course_id`='?'
  501. where `sub_course_id`='?'
  502. and `sub_transfer_flag`='0' ", $to_course_id, $from_course_id);
  503. $res = $this->db_query("update transfer_eqv_per_student
  504. set `local_course_id`='?'
  505. where `local_course_id`='?' ", $to_course_id, $from_course_id);
  506. }
  507. /**
  508. * Given an advising_session_id, create a duplicate of it as a new session_id (and return the new session_id).
  509. *
  510. * All the values can be left blank to mean "keep what is in there". If they have values supplied in the arguments to this function,
  511. * then the new values will be used.
  512. */
  513. function duplicate_advising_session($advising_session_id, $faculty_id = "", $student_id = "", $term_id = "", $degree_id = "", $is_whatif = "", $is_draft = "") {
  514. $now = time();
  515. // First, get the details of this particular advising session....
  516. $res = db_query("SELECT * FROM advising_sessions WHERE advising_session_id = ?", $advising_session_id);
  517. $cur = db_fetch_array($res);
  518. // Get our values....
  519. $db_student_id = ($student_id == "") ? $cur["student_id"] : $student_id;
  520. $db_faculty_id = ($faculty_id == "") ? $cur["faculty_id"] : $faculty_id;
  521. $db_term_id = ($term_id == "") ? $cur["term_id"] : $term_id;
  522. $db_degree_id = ($degree_id == "") ? $cur["degree_id"] : $degree_id;
  523. $db_major_code_csv = $cur["major_code_csv"];
  524. $db_catalog_year = $cur["catalog_year"];
  525. $db_posted = $now;
  526. $db_is_whatif = ($is_whatif == "") ? $cur["is_whatif"] : $is_whatif;
  527. $db_is_draft = ($is_draft == "") ? $cur["is_draft"] : $is_draft;
  528. $db_is_empty = $cur["is_empty"];
  529. $db_delete_flag = $cur['delete_flag'];
  530. // Okay, let's INSERT this record, and capture the new advising_session_id...
  531. $res = db_query("INSERT INTO advising_sessions
  532. (student_id, faculty_id, term_id, degree_id, major_code_csv, catalog_year, posted, is_whatif, is_draft, is_empty, delete_flag)
  533. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  534. ", $db_student_id, $db_faculty_id, $db_term_id, $db_degree_id, $db_major_code_csv, $db_catalog_year, $db_posted, $db_is_whatif, $db_is_draft, $db_is_empty, $db_delete_flag);
  535. $new_asid = db_insert_id();
  536. // Okay, now pull out the advised_courses, and insert again under the new_asid...
  537. $res = db_query("SELECT * FROM advised_courses WHERE advising_session_id = ?", $advising_session_id);
  538. while ($cur = db_fetch_array($res)) {
  539. db_query("INSERT INTO advised_courses (advising_session_id, course_id, entry_value, semester_num, group_id, var_hours, term_id, degree_id)
  540. VALUES (?, ?, ?, ?, ?, ?, ?, ?)", $new_asid, $cur["course_id"], $cur["entry_value"], $cur["semester_num"], $cur["group_id"], $cur["var_hours"], $cur["term_id"], $cur["degree_id"]);
  541. }
  542. // Finished!
  543. return $new_asid;
  544. }
  545. function get_advising_session_id($faculty_id = "", $student_id = "", $term_id = "", $degree_id = "", $bool_what_if = false, $bool_draft = true, $bool_load_any_active_if_faculty_id_not_found = TRUE)
  546. {
  547. $is_what_if = "0";
  548. $is_draft = "0";
  549. $draft_line = " and `is_draft`='0' ";
  550. $faculty_line = " and `faculty_id`='$faculty_id' ";
  551. $advising_session_id = 0; // init
  552. if ($faculty_id == 0 || $faculty_id == "")
  553. { // If no faculty is specified, just get the first one to come up.
  554. $faculty_line = "";
  555. }
  556. if ($bool_what_if == true){$is_what_if = "1";}
  557. if ($bool_draft == true)
  558. {
  559. $is_draft = "1";
  560. $draft_line = "";
  561. // If we are told to pull up draft, we can safely
  562. // assume we just want the most recent save, whether it
  563. // is saved as a draft or not.
  564. }
  565. $query = "select * from advising_sessions
  566. where
  567. student_id = ?
  568. $faculty_line
  569. and term_id = ?
  570. and degree_id = ?
  571. and is_whatif = ?
  572. AND delete_flag = 0
  573. $draft_line
  574. order by `posted` desc limit 1";
  575. $result = $this->db_query($query, array($student_id, $term_id, $degree_id, $is_what_if)) ;
  576. if ($this->db_num_rows($result) > 0)
  577. {
  578. $cur = $this->db_fetch_array($result);
  579. $advising_session_id = $cur["advising_session_id"];
  580. return $advising_session_id;
  581. }
  582. if (intval($advising_session_id) < 1 && $bool_load_any_active_if_faculty_id_not_found) {
  583. // Meaning, we couldn't find a record for the supplied faculty_id. Let's just load the most recent active one, regardless
  584. // of faculty_id. Meaning, we need to make sure that is_draft = 0
  585. $query = "select * from advising_sessions
  586. where
  587. student_id = ?
  588. and term_id = ?
  589. and degree_id = ?
  590. and is_whatif = ?
  591. and is_draft = 0
  592. AND delete_flag = 0
  593. order by `posted` desc limit 1";
  594. $result = $this->db_query($query, array($student_id, $term_id, $degree_id, $is_what_if)) ;
  595. if ($this->db_num_rows($result) > 0) {
  596. $cur = $this->db_fetch_array($result);
  597. $advising_session_id = $cur["advising_session_id"];
  598. return $advising_session_id;
  599. }
  600. }
  601. return 0;
  602. }
  603. /**
  604. * Returns the group_id for the given group name, or FALSE
  605. */
  606. function get_group_name($group_id) {
  607. $temp = explode("_", $group_id);
  608. $group_id = fp_trim(@$temp[0]);
  609. // If it's already in our static cache, just return that.
  610. static $group_name_cache = array();
  611. if (isset($group_name_cache[$group_id])) {
  612. return $group_name_cache[$group_id];
  613. }
  614. $res7 = $this->db_query("SELECT group_name FROM `groups`
  615. WHERE group_id = ?
  616. AND delete_flag = 0
  617. LIMIT 1 ", $group_id) ;
  618. if ($this->db_num_rows($res7) > 0)
  619. {
  620. $cur7 = $this->db_fetch_array($res7);
  621. // Save to our cache before returning.
  622. $group_name_cache[$group_id] = $cur7['group_name'];
  623. return $cur7['group_name'];
  624. }
  625. return FALSE;
  626. }
  627. function get_group_id($group_name, $catalog_year, $school_id = 0) {
  628. if ($catalog_year < variable_get_for_school("earliest_catalog_year", 2006, $school_id))
  629. {
  630. $catalog_year = variable_get_for_school("earliest_catalog_year", 2006, $school_id);
  631. }
  632. // If it's already in our static cache, just return that.
  633. static $group_id_cache = array();
  634. if (isset($group_id_cache[$group_name][$school_id][$catalog_year])) {
  635. return $group_id_cache[$group_name][$school_id][$catalog_year];
  636. }
  637. $res7 = $this->db_query("SELECT group_id FROM `groups`
  638. WHERE group_name = ?
  639. AND catalog_year = ?
  640. AND school_id = ?
  641. AND delete_flag = 0
  642. LIMIT 1 ", $group_name, $catalog_year, $school_id) ;
  643. if ($this->db_num_rows($res7) > 0)
  644. {
  645. $cur7 = $this->db_fetch_array($res7);
  646. // Save to our cache
  647. $group_id_cache[$group_name][$school_id][$catalog_year] = $cur7['group_id'];
  648. return $cur7['group_id'];
  649. }
  650. return false;
  651. }
  652. function request_new_degree_id()
  653. {
  654. // Return a valid new id...
  655. for ($t = 0; $t < 1000; $t++)
  656. {
  657. $id = mt_rand(1, 2147483640); // A few less than the max for a signed int in mysql.
  658. // Check for collisions...
  659. $res4 = $this->db_query("SELECT * FROM draft_degrees
  660. WHERE `degree_id`='?' limit 1", $id);
  661. if ($this->db_num_rows($res4) == 0)
  662. { // Was not in the table already, so use it!
  663. return $id;
  664. }
  665. }
  666. return false;
  667. }
  668. function get_institution_name($institution_id, $school_id = 0)
  669. {
  670. // Return the name of the institution...
  671. $res = $this->db_query("SELECT * FROM transfer_institutions
  672. where institution_id = ?
  673. AND school_id = ?", $institution_id, $school_id);
  674. $cur = $this->db_fetch_array($res);
  675. if ($cur) {
  676. return fp_trim(@$cur['name']);
  677. }
  678. return ''; // nothing found, so return blank
  679. }
  680. /**
  681. * Retrieve a value from the variables table.
  682. *
  683. * @param string $name
  684. */
  685. function get_variable($name, $default_value = "") {
  686. $res = $this->db_query("SELECT value FROM variables
  687. WHERE name = ? ", $name);
  688. $cur = $this->db_fetch_array($res);
  689. $val = $cur["value"];
  690. if ($val == "") {
  691. $val = $default_value;
  692. }
  693. return $val;
  694. }
  695. /**
  696. * Sets a variable's value in the variables table.
  697. *
  698. * @param unknown_type $name
  699. * @param unknown_type $value
  700. */
  701. function set_variable($name, $value) {
  702. $res2 = $this->db_query("REPLACE INTO variables (name, value)
  703. VALUES (?, ?) ", $name, $value);
  704. }
  705. function get_school_id_for_transfer_course_id($transfer_course_id) {
  706. return intval(db_result(db_query("SELECT school_id FROM transfer_courses WHERE transfer_course_id = ?", array($transfer_course_id))));
  707. }
  708. function get_school_id_for_user_id($user_id) {
  709. return intval(db_result(db_query("SELECT school_id FROM users WHERE user_id = ?", array($user_id))));
  710. }
  711. function get_school_id_for_student_id($cwid) {
  712. // Save to cache for quick lookup
  713. if (isset($GLOBALS['cache_school_id_for_student_id'][$cwid])) {
  714. return $GLOBALS['cache_school_id_for_student_id'][$cwid];
  715. }
  716. $rtn = intval(db_result(db_query("SELECT school_id FROM users WHERE cwid = ? AND is_student = 1", array($cwid))));
  717. $GLOBALS['cache_school_id_for_student_id'][$cwid] = $rtn;
  718. return $rtn;
  719. }
  720. function get_school_id_for_faculty_id($cwid) {
  721. return intval(db_result(db_query("SELECT school_id FROM users WHERE cwid = ? AND is_faculty = 1", array($cwid))));
  722. }
  723. function get_school_id_for_course_id($course_id, $bool_use_draft = FALSE) {
  724. // Always override if the global variable is set.
  725. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  726. $bool_use_draft = true;
  727. }
  728. $table_name = "courses";
  729. if ($bool_use_draft){$table_name = "draft_$table_name";}
  730. // Use GLOBALS cache to make this faster.
  731. if (isset($GLOBALS['fp_school_id_for_course_id'][$table_name][$course_id])) {
  732. return $GLOBALS['fp_school_id_for_course_id'][$table_name][$course_id];
  733. }
  734. $val = intval(db_result(db_query("SELECT school_id FROM $table_name WHERE course_id = ?", array($course_id))));
  735. $GLOBALS['fp_school_id_for_course_id'][$table_name][$course_id] = $val;
  736. return $val;
  737. }
  738. function get_school_id_for_degree_id($degree_id, $bool_use_draft = FALSE) {
  739. // Always override if the global variable is set.
  740. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  741. $bool_use_draft = true;
  742. }
  743. $table_name = "degrees";
  744. if ($bool_use_draft){$table_name = "draft_$table_name";}
  745. return intval(db_result(db_query("SELECT school_id FROM $table_name WHERE degree_id = ?", array($degree_id))));
  746. }
  747. function get_school_id_for_group_id($group_id, $bool_use_draft = FALSE) {
  748. // Always override if the global variable is set.
  749. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  750. $bool_use_draft = true;
  751. }
  752. $table_name = "groups";
  753. if ($bool_use_draft){$table_name = "draft_$table_name";}
  754. return intval(db_result(db_query("SELECT school_id FROM $table_name WHERE group_id = ? ", array($group_id))));
  755. }
  756. /**
  757. * Returns an object from db query for a row we find with matching course_id, from the most recent catalog year.
  758. */
  759. function get_course_db_row($course_id) {
  760. $res = db_query("SELECT * FROM courses
  761. WHERE course_id = ?
  762. AND exclude != 1
  763. AND delete_flag != 1
  764. ORDER BY `catalog_year` DESC", array($course_id));
  765. $cur = db_fetch_object($res);
  766. if (!$cur) {
  767. // Couldn't find it, so lift the exclude requirement.
  768. $res = db_query("SELECT * FROM courses
  769. WHERE course_id = ?
  770. AND delete_flag != 1
  771. ORDER BY `catalog_year` DESC", array($course_id));
  772. $cur = db_fetch_object($res);
  773. }
  774. return $cur;
  775. }
  776. function get_course_id($subject_id, $course_num, $catalog_year = "", $bool_use_draft = FALSE, $school_id = 0, $bool_check_allow_default_school = FALSE)
  777. {
  778. // If we were not sent a valid course name, return FALSE right away.
  779. if (!$subject_id && !$course_num) return FALSE;
  780. if (!$course_num) $course_num = ''; // Make sure it isn't FALSE or NULL
  781. // Ignore the colon, if there is one.
  782. if (strpos($course_num, ":") !== FALSE)
  783. {
  784. //$course_num = substr($course_num,0,-2);
  785. $temp = explode(":", $course_num);
  786. $course_num = trim($temp[0]);
  787. }
  788. $params = array();
  789. $school_line = " AND school_id = :school_id ";
  790. // Should we ALSO check the default school, in addition to whatever we specified? Don't bother if what we specified was the default school.
  791. if ($bool_check_allow_default_school && module_enabled('schools') && variable_get('schools_allow_courses_from_default_school', 'yes') === 'yes' && $school_id != 0) {
  792. $school_line = " AND (school_id = :school_id OR school_id = 0) ";
  793. }
  794. // Always override if the global variable is set.
  795. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  796. $bool_use_draft = true;
  797. }
  798. $catalog_line = "";
  799. if ($catalog_year != "")
  800. {
  801. $catalog_year = intval($catalog_year);
  802. $catalog_line = "and catalog_year = '$catalog_year' ";
  803. }
  804. $table_name = "courses";
  805. if ($bool_use_draft){$table_name = "draft_$table_name";}
  806. $params[':subject_id'] = $subject_id;
  807. $params[':course_num'] = $course_num;
  808. $params[':school_id'] = intval($school_id);
  809. $res7 = $this->db_query("SELECT course_id FROM $table_name
  810. WHERE subject_id = :subject_id
  811. AND course_num = :course_num
  812. $school_line
  813. $catalog_line
  814. ORDER BY catalog_year DESC LIMIT 1 ", $params) ;
  815. if ($this->db_num_rows($res7) > 0)
  816. {
  817. $cur7 = $this->db_fetch_array($res7);
  818. return intval($cur7["course_id"]);
  819. }
  820. return FALSE;
  821. }
  822. function get_student_settings($student_cwid) {
  823. // This returns an array (from the xml) of a student's
  824. // settings in the student_settings table. It will
  825. // return FALSE if the student was not in the table.
  826. $res = $this->db_query("SELECT settings FROM student_settings
  827. WHERE student_id = ?
  828. ", $student_cwid) ;
  829. if ($this->db_num_rows($res) < 1)
  830. {
  831. return false;
  832. }
  833. $cur = $this->db_fetch_array($res);
  834. if (!$rtn = unserialize($cur["settings"])) {
  835. $rtn = array();
  836. }
  837. return $rtn;
  838. }
  839. function get_student_cumulative_hours($student_cwid) {
  840. // Let's perform our queries.
  841. $res = $this->db_query("SELECT cumulative_hours FROM students
  842. WHERE cwid = ?
  843. ", $student_cwid);
  844. $cur = $this->db_fetch_array($res);
  845. return $cur["cumulative_hours"];
  846. }
  847. function get_student_gpa($student_cwid) {
  848. // Let's perform our queries.
  849. $res = $this->db_query("SELECT gpa FROM students
  850. WHERE cwid = ?
  851. ", $student_cwid);
  852. $cur = $this->db_fetch_array($res);
  853. return $cur["gpa"];
  854. }
  855. function get_student_catalog_year($student_cwid) {
  856. if (isset($GLOBALS['db_get_student_catalog_year'][$student_cwid])) {
  857. return $GLOBALS['db_get_student_catalog_year'][$student_cwid];
  858. }
  859. $catalog = 0;
  860. // Let's perform our queries.
  861. $res = $this->db_query("SELECT catalog_year FROM students
  862. WHERE cwid = ?
  863. ", $student_cwid);
  864. $cur = $this->db_fetch_array($res);
  865. if ($cur) {
  866. $catalog = intval($cur["catalog_year"]);
  867. }
  868. $GLOBALS['db_get_student_catalog_year'][$student_cwid] = $catalog;
  869. return $catalog;
  870. }
  871. /**
  872. * Returns whatever is in the Rank field for this student.
  873. * Ex: JR, SR, FR, etc.
  874. *
  875. * @param unknown_type $student_id
  876. * @return unknown
  877. */
  878. function get_student_rank($student_cwid) {
  879. // Let's perform our queries.
  880. $res = $this->db_query("SELECT rank_code FROM students
  881. WHERE cwid = ?
  882. ", $student_cwid);
  883. $cur = $this->db_fetch_array($res);
  884. $rank = $cur["rank_code"];
  885. return trim($rank);
  886. }
  887. /**
  888. * Returns the student's first and last name, put together.
  889. * Ex: John Smith.
  890. *
  891. * @param int $student_id
  892. * @return string
  893. */
  894. function get_student_name($cwid, $bool_include_cwid = FALSE) {
  895. // Let's perform our queries.
  896. $res = $this->db_query("SELECT f_name, l_name FROM users
  897. WHERE cwid = ?
  898. AND is_student = 1 ", $cwid);
  899. $cur = $this->db_fetch_array($res);
  900. if ($cur) {
  901. $name = $cur["f_name"] . " " . $cur["l_name"];
  902. }
  903. else {
  904. $name = t("Unknown Student");
  905. }
  906. // Force into pretty capitalization.
  907. // turns JOHN SMITH into John Smith
  908. $name = trim(ucwords(strtolower($name)));
  909. if ($bool_include_cwid) {
  910. $name .= " ($cwid)";
  911. }
  912. return $name;
  913. }
  914. /**
  915. * Returns the faculty's first and last name, put together.
  916. * Ex: John Smith or John W Smith.
  917. *
  918. * @param int $faculty_id
  919. * @return string
  920. */
  921. function get_faculty_name($cwid, $bool_include_cwid = FALSE) {
  922. $name = '';
  923. // Let's perform our queries.
  924. $res = $this->db_query("SELECT f_name, l_name FROM users
  925. WHERE cwid = ?
  926. AND is_faculty = 1 ", $cwid);
  927. $cur = $this->db_fetch_array($res);
  928. if ($cur) {
  929. $name = $cur["f_name"] . " " . $cur["l_name"];
  930. // Force into pretty capitalization.
  931. // turns JOHN SMITH into John Smith
  932. $name = trim(ucwords(strtolower($name)));
  933. if ($bool_include_cwid) {
  934. $name .= " ($cwid)";
  935. }
  936. }
  937. return $name;
  938. }
  939. /**
  940. * Looks in our extra tables to find out what major code, if any, has been assigned
  941. * to this faculty member.
  942. *
  943. */
  944. function get_faculty_major_code_csv($faculty_cwid) {
  945. // Let's pull the needed variables out of our settings, so we know what
  946. // to query, because this is a non-FlightPath table.
  947. $res = $this->db_query("SELECT major_code_csv FROM faculty WHERE cwid = ? ", $faculty_cwid);
  948. $cur = $this->db_fetch_array($res);
  949. return @$cur["major_code_csv"];
  950. }
  951. /**
  952. * Returns an array (or CSV string) of major_codes from the student_degrees table for this student.
  953. *
  954. * If bool_check_for_allow_dynaic is TRUE, it means that, if the student has more than one degree returned, we will make sure that they all
  955. * have allow_dynamic = TRUE. If they do not, we will use the first is_editable degree we find ONLY. We do this because that means the student
  956. * had a situation like we see in FlightPath 4x, where only one degree may be selected at a time, and the is_editiable degree is the track/option they
  957. * selected.
  958. *
  959. *
  960. */
  961. function get_student_majors_from_db($student_cwid, $bool_return_as_full_record = FALSE, $perform_join_with_degrees = TRUE, $bool_skip_directives = TRUE, $bool_check_for_allow_dynamic = TRUE) {
  962. // Looks in the student_degrees table and returns an array of major codes.
  963. $rtn = array();
  964. // Keep track of degrees which have is_editable set to 1.
  965. $is_editable_true = array();
  966. $is_editable_false = array();
  967. if ($perform_join_with_degrees) {
  968. $catalog_year = $this->get_student_catalog_year($student_cwid);
  969. $res = $this->db_query("SELECT * FROM student_degrees a, degrees b
  970. WHERE student_id = ?
  971. AND a.major_code = b.major_code
  972. AND b.catalog_year = ?
  973. ORDER BY b.advising_weight, b.major_code
  974. ", $student_cwid, $catalog_year);
  975. }
  976. else {
  977. // No need to join with degrees table...
  978. $res = $this->db_query("SELECT * FROM student_degrees a
  979. WHERE student_id = ?
  980. ORDER BY major_code
  981. ", $student_cwid);
  982. }
  983. while ($cur = $this->db_fetch_array($res)) {
  984. if ($bool_skip_directives && strstr($cur["major_code"], "~")) continue;
  985. if ($bool_return_as_full_record) {
  986. $rtn[$cur["major_code"]] = $cur;
  987. }
  988. else {
  989. $rtn[$cur["major_code"]] = $cur["major_code"];
  990. }
  991. if ($bool_check_for_allow_dynamic && !isset($cur['allow_dynamic']) && isset($cur['degree_id'])) {
  992. $cur['allow_dynamic'] = $this->get_degree_allow_dynamic($cur['degree_id']);
  993. }
  994. if ($cur['is_editable'] == 1) {
  995. $is_editable_true[] = $cur;
  996. }
  997. else {
  998. $is_editable_false[] = $cur;
  999. }
  1000. }
  1001. if ($bool_check_for_allow_dynamic && count($rtn) > 1) {
  1002. // This means that we have more than one degree selected, and we have been asked to make sure that if any of the degrees have allow_dynamic = 0, then we will
  1003. // only select the is_editable degree.
  1004. foreach ($is_editable_false as $major) {
  1005. if (isset($major['allow_dynamic']) && $major['allow_dynamic'] == 0) {
  1006. // Meaning, allow dynamic is NOT allowed. So, if we have ANYTHING in is_editable_true, then use THAT, else, use THIS.
  1007. if (count($is_editable_true) > 0) {
  1008. // Only get out 1 major.
  1009. $x = $is_editable_true[0];
  1010. $new_rtn[$x['major_code']] = $rtn[$x['major_code']];
  1011. $rtn = $new_rtn;
  1012. }
  1013. else {
  1014. $x = $major;
  1015. $new_rtn[$x['major_code']] = $rtn[$x['major_code']];
  1016. $rtn = $new_rtn;
  1017. }
  1018. }
  1019. }
  1020. } // if bool_check_for_allow_dynamic
  1021. return $rtn;
  1022. }
  1023. function get_flightpath_settings()
  1024. {
  1025. // Returns an array of everything in the flightpath_settings table.
  1026. $rtn_array = array();
  1027. $res = $this->db_query("SELECT * FROM flightpath_settings ") ;
  1028. while($cur = $this->db_fetch_array($res))
  1029. {
  1030. $rtn_array[$cur["variable_name"]] = trim($cur["value"]);
  1031. }
  1032. return $rtn_array;
  1033. }
  1034. function get_degrees_in_catalog_year($catalog_year, $bool_include_tracks = false, $bool_use_draft = false, $bool_undergrad_only = TRUE, $only_level_nums = array(1,2), $school_id = 0)
  1035. {
  1036. // Returns an array of all the degrees from a particular year
  1037. // which are entered into FlightPath.
  1038. $undergrad_line = $degree_class_line = "";
  1039. $table_name = "degrees";
  1040. if ($bool_use_draft){$table_name = "draft_$table_name";}
  1041. // change this to be whatever the graduate code actually is.
  1042. if ($bool_undergrad_only) $undergrad_line = " AND degree_level <> 'GR' ";
  1043. $degree_class_line = "";
  1044. if (count($only_level_nums) > 0) {
  1045. $classes = fp_get_degree_classifications();
  1046. foreach ($only_level_nums as $num) {
  1047. foreach ($classes["levels"][$num] as $machine_name => $val) {
  1048. $degree_class_line .= " degree_class = '" . addslashes($machine_name) . "' OR";
  1049. }
  1050. }
  1051. // Remove training "OR" from degree_class_line
  1052. $degree_class_line = substr($degree_class_line, 0, strlen($degree_class_line) - 2);
  1053. }
  1054. if ($degree_class_line != "") {
  1055. $degree_class_line = "AND ($degree_class_line)";
  1056. }
  1057. $rtn_array = array();
  1058. $res = $this->db_query("SELECT `id`, degree_id, major_code, title, degree_class , school_id, catalog_year
  1059. FROM $table_name
  1060. WHERE exclude = '0'
  1061. AND catalog_year = ?
  1062. AND school_id = ?
  1063. $undergrad_line
  1064. $degree_class_line
  1065. ORDER BY title, major_code ", $catalog_year, $school_id);
  1066. if ($this->db_num_rows($res) < 1)
  1067. {
  1068. return false;
  1069. }
  1070. while ($cur = $this->db_fetch_array($res))
  1071. {
  1072. $degree_id = $cur["degree_id"];
  1073. $major = trim($cur["major_code"]);
  1074. $title = trim($cur["title"]);
  1075. $track_code = "";
  1076. $major_code = $major;
  1077. // The major may have a track specified. If so, take out
  1078. // the track and make it seperate.
  1079. if (strstr($major, "_"))
  1080. {
  1081. $temp = explode("_", $major);
  1082. $major_code = trim($temp[0]);
  1083. $track_code = trim($temp[1]);
  1084. // The major_code might now have a | at the very end. If so,
  1085. // get rid of it.
  1086. if (substr($major_code, strlen($major_code)-1, 1) == "|")
  1087. {
  1088. $major_code = str_replace("|","",$major_code);
  1089. }
  1090. }
  1091. // Leave the track in if requested.
  1092. if ($bool_include_tracks == true)
  1093. {
  1094. // Set it back to what we got from the db.
  1095. $major_code = $major;
  1096. $temp_degree = $this->get_degree_plan($major, $catalog_year, true);
  1097. if ($temp_degree->track_code != "")
  1098. {
  1099. $title .= " - " . $temp_degree->track_title;
  1100. }
  1101. }
  1102. $rtn_array[$major_code]["title"] = $title;
  1103. $rtn_array[$major_code]["degree_id"] = $degree_id;
  1104. $rtn_array[$major_code]["degree_class"] = trim(strtoupper($cur["degree_class"]));
  1105. $rtn_array[$major_code]["school_id"] = intval($cur['school_id']);
  1106. $rtn_array[$major_code]["catalog_year"] = $cur['catalog_year'];
  1107. $rtn_array[$major_code]["db_id"] = $cur['id'];
  1108. }
  1109. return $rtn_array;
  1110. }
  1111. function get_degree_tracks($major_code, $catalog_year, $school_id = 0)
  1112. {
  1113. // Will return an array of all the tracks that a particular major
  1114. // has. Must match the major_code in degree_tracks table.
  1115. // Returns FALSE if there are none.
  1116. $rtn_array = array();
  1117. static $degree_tracks_data_cache = array();
  1118. if (isset($degree_tracks_data_cache[$catalog_year][$major_code])) {
  1119. return $degree_tracks_data_cache[$catalog_year][$major_code];
  1120. }
  1121. $res = $this->db_query("SELECT * FROM degree_tracks
  1122. WHERE major_code = ?
  1123. AND catalog_year = ?
  1124. AND school_id = ?", $major_code, $catalog_year, $school_id);
  1125. if ($this->db_num_rows($res) < 1)
  1126. {
  1127. $degree_tracks_data_cache[$catalog_year][$major_code] = false;
  1128. return FALSE;
  1129. }
  1130. while($cur = $this->db_fetch_array($res))
  1131. {
  1132. extract($cur, 3, "db");
  1133. $rtn_array[] = $db_track_code;
  1134. }
  1135. $degree_tracks_data_cache[$catalog_year][$major_code] = $rtn_array;
  1136. return $rtn_array;
  1137. }
  1138. function get_degree_plan($major_and_track_code, $catalog_year = "", $bool_minimal = false, $school_id = 0)
  1139. {
  1140. // Returns a degreePlan object from the supplied information.
  1141. // If catalog_year is blank, use whatever the current catalog year is, loaded from our settings table.
  1142. if ($catalog_year == "") {
  1143. $catalog_year = variable_get_for_school("current_catalog_year", "2006", $school_id);
  1144. }
  1145. $degree_id = $this->get_degree_id(trim($major_and_track_code), $catalog_year, FALSE, $school_id);
  1146. $dp = new DegreePlan($degree_id,null,$bool_minimal);
  1147. if ($dp->major_code == "")
  1148. {
  1149. $dp->major_code = trim($major_and_track_code);
  1150. }
  1151. return $dp;
  1152. }
  1153. /**
  1154. * Returns the value of a degree's allow_dynamic field in the database.
  1155. *
  1156. * Returns boolean FALSE if it cannot find the degree.
  1157. *
  1158. * @param unknown_type $degree_id
  1159. * @param unknown_type $bool_use_draft
  1160. */
  1161. function get_degree_allow_dynamic($degree_id, $bool_use_draft = FALSE) {
  1162. $table_name = "degrees";
  1163. if ($bool_use_draft){$table_name = "draft_$table_name";}
  1164. $res7 = $this->db_query("SELECT allow_dynamic FROM $table_name
  1165. WHERE degree_id = ?
  1166. ", $degree_id) ;
  1167. if ($this->db_num_rows($res7) > 0)
  1168. {
  1169. $cur7 = $this->db_fetch_array($res7);
  1170. return $cur7["allow_dynamic"];
  1171. }
  1172. return false;
  1173. }
  1174. function get_degree_id($major_and_track_code, $catalog_year, $bool_use_draft = FALSE, $school_id = 0)
  1175. {
  1176. // This function expects the major_code and track_code (if it exists)
  1177. // to be joined using |_. Example:
  1178. // GSBA|_123 or KIND|EXCP_231.
  1179. // In other words, all in one.
  1180. // Always override if the global variable is set.
  1181. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  1182. $bool_use_draft = true;
  1183. }
  1184. if ($catalog_year < variable_get_for_school("earliest_catalog_year", 2006, $school_id))
  1185. { // Lowest possible year.
  1186. $catalog_year = variable_get_for_school("earliest_catalog_year", 2006, $school_id);
  1187. }
  1188. $table_name = "degrees";
  1189. if ($bool_use_draft){$table_name = "draft_$table_name";}
  1190. $res7 = $this->db_query("SELECT degree_id FROM $table_name
  1191. WHERE major_code = ?
  1192. AND catalog_year = ?
  1193. AND school_id = ?
  1194. LIMIT 1 ", trim($major_and_track_code), $catalog_year, $school_id) ;
  1195. if ($this->db_num_rows($res7) > 0)
  1196. {
  1197. $cur7 = $this->db_fetch_array($res7);
  1198. return $cur7["degree_id"];
  1199. }
  1200. return false;
  1201. }
  1202. // Returns a simple array of all degree_id's which match this major code, any catalog year.
  1203. function get_degree_ids($major_code, $school_id = 0) {
  1204. $rtn = array();
  1205. $bool_use_draft = FALSE;
  1206. // Always override if the global variable is set.
  1207. if (@$GLOBALS["fp_advising"]["bool_use_draft"] == true) {
  1208. $bool_use_draft = true;
  1209. }
  1210. $table_name = "degrees";
  1211. if ($bool_use_draft){$table_name = "draft_$table_name";}
  1212. $res7 = $this->db_query("SELECT degree_id FROM $table_name
  1213. WHERE major_code = ?
  1214. AND school_id = ?
  1215. ", trim($major_code), $school_id) ;
  1216. while ($cur7 = $this->db_fetch_array($res7)) {
  1217. $rtn[$cur7["degree_id"]] = $cur7["degree_id"];
  1218. }
  1219. return $rtn;
  1220. } // get_degree_ids
  1221. function db_fetch_array($result) {
  1222. if (!is_object($result)) return FALSE;
  1223. return $result->fetch(PDO::FETCH_ASSOC);
  1224. }
  1225. function db_fetch_object($result) {
  1226. if (!is_object($result)) return FALSE;
  1227. return $result->fetch(PDO::FETCH_OBJ);
  1228. }
  1229. function db_num_rows($result) {
  1230. if (!is_object($result)) return FALSE;
  1231. return $result->rowCount();
  1232. }
  1233. function db_affected_rows($result) {
  1234. return db_num_rows($result);
  1235. }
  1236. function db_insert_id() {
  1237. //fpm($this->pdo->lastInsertId());
  1238. //return $this->pdo->lastInsertId();
  1239. return $_SESSION["fp_last_insert_id"];
  1240. }
  1241. function db_close() {
  1242. return $this->pdo = NULL; // this is all you need to do to close a PDO connection.
  1243. }
  1244. /////////////////////////////////////////////
  1245. /////////////////////////////////////////////
  1246. /////////////////////////////////////////////
  1247. }

Classes

Namesort descending Description
DatabaseHandler