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

Classes

Namesort descending Description
DatabaseHandler