other_module_api_functions.inc 2.0 KB

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