DatabaseHandler.php

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

File

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

Classes

Namesort descending Description
DatabaseHandler