other_module_api_functions.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. ///////////////////////////////////////////////////////////////////////////
  7. // Module: tripal_core
  8. ///////////////////////////////////////////////////////////////////////////
  9. /*************************************************************************
  10. * Purpose: Get max rank for a given set of criteria
  11. * This function was developed with the many property tables in chado in mind
  12. *
  13. * @params tablename: the name of the chado table you want to select the max rank from
  14. * this table must contain a rank column of type integer
  15. * @params where_options: array(
  16. * <column_name> => array(
  17. * 'type' => <type of column: INT/STRING>,
  18. * 'value' => <the value you want to filter on>,
  19. * 'exact' => <if TRUE use =; if FALSE use ~>,
  20. * )
  21. * )
  22. * where options should include the id and type for that table to correctly
  23. * group a set of records together where the only difference are the value and rank
  24. * @return the maximum rank
  25. *
  26. */
  27. function get_max_chado_rank($tablename, $where_options) {
  28. $where= array();
  29. //generate the where clause from supplied options
  30. // the key is the column name
  31. foreach ($where_options as $key => $val_array) {
  32. if (preg_match('/INT/', $val_array['type'])) {
  33. $where[] = $key . "=" . $val_array['value'];
  34. }
  35. else {
  36. if ($val_array['exact']) {
  37. $operator='='; }
  38. else { $operator='~'; }
  39. $where[] = $key . $operator . "'" . $val_array['value'] . "'";
  40. }
  41. }
  42. $result = db_fetch_object(chado_query(
  43. "SELECT max(rank) as max_rank, count(rank) as count FROM {%s} WHERE %s",
  44. $tablename,
  45. implode(' AND ', $where)
  46. ));
  47. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  48. if ($result->count > 0) {
  49. return $result->max_rank;
  50. }
  51. else {
  52. return -1;
  53. }
  54. }