other_module_api_functions.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. } else {
  31. if ($val_array['exact']) { $operator='='; }
  32. else { $operator='~'; }
  33. $where[] = $key.$operator."'".$val_array['value']."'";
  34. }
  35. }
  36. $previous_db = tripal_db_set_active('chado');
  37. $result = db_fetch_object(db_query(
  38. "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  39. $tablename,
  40. implode(' AND ',$where)
  41. ));
  42. tripal_db_set_active($previous_db);
  43. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  44. if ($result->count > 0) {
  45. return $result->max_rank;
  46. } else {
  47. return -1;
  48. }
  49. }