other_module_api_functions.inc 2.0 KB

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