Hello there,

I have implemented a series of modules that use audio.module and most of them need an easy way to query audio nodes that follow specific criteria. In order to do that, I've implemented the following function:

/*
 * returns list with audio nodes that match specified criteria
 *
 * @param $query_info
 *   array with the query criteria for the audio entries:
 *     ['status'] :: 1 = published, 0 = unpublished, NULL = any
 *     ['promote'] :: 1 = promoted to the front page, 0 = not promoted, NULL = any
 *     ['uid'] :: user id of the desired entries author, or NULL for any
 *     ['name'] :: username of the desired entries author, or NULL for any. Note that this option
 *       cannot not be used in conjunction with the 'uid' one.
 *     ['categories_or'] :: array with category ids to be ORed; or NULL for all
 *     ['categories_and'] :: array with category ids to be ANDed; or NULL for all
 *     ['unread_entries_only'] :: TRUE, return only the entries that haven't been read by the user
 *     ['read_entries_only'] :: TRUE, return only the entries that have already been read by the user.
 *       Note that this option cannot be used in conjunction with 'unread_entries_only'
 *     ['date_from'] :: time stamp of the oldest entry desired.  If NULL, return entries up to 30 days old.
 *     ['date_to'] :: time stamp of the newest entry desired.  If NULL, assume no time limit.
 *     ['gid'] :: (optional) with the id of the group of where the selected entries have been published to 
 *     ['limit'] :: (optional) the maximum number of nodes to be returned
 *
 * @return
 *   array with the audio nodes that obey the specified criteria. Note that audio nodes
 *   that haven't been read by the user will have an extra element ['unread'] set to TRUE.
 */
function audio_api_get_node_list($query_info) {

  global $user;

  //
  // Prepare the arguments for the query
  //

  $query_status = empty($query_info['status']) ? '' : 'AND n.status=' . $query_info['status'];
  
  $query_promote = empty($query_info['promote']) ? '' : 'AND n.promote=' . $query_info['promote'];

  $query_user = empty($query_info['uid']) ? '': 'AND n.uid=' . $query_info['uid'];
  if (empty($query_user)) {
    $join_name = (!$query_info['name']) ? '': ' INNER JOIN {users} u ON n.uid=u.uid ';
    $query_name = (!$query_info['name']) ? '': "AND u.name='" . $query_info['name'] . "'";
  }
  else {
    $join_name = '';
    $query_name = '';
  }

  $tids_or_tmp = $query_info['categories_or'];
  $query_tids_or = '';
  if (!empty($tids_or_tmp) && !in_array(NULL, $tids_or_tmp)) { // make sure none of the array elements is NULL
    $query_tids_or = 'AND ( t.tid=' . implode(' OR t.tid=', $tids_or_tmp);
    $query_tids_or .= ')';
  }

  $tids_and_tmp = $query_info['categories_and'];
  $query_tids_and = '';
  if (!empty($tids_and_tmp) && !in_array(NULL, $tids_and_tmp)) { // make sure none of the array elements is NULL
    $query_tids_and = 'AND ( t.tid=' . implode(' AND t.tid=', $tids_and_tmp);
    $query_tids_and .= ')';
  }

  $join_categories = '';
  if (!empty($query_tids_or) || !empty($query_tids_and)) { // if at least a category is being queried...
    $join_categories = ' INNER JOIN {term_node} t ON n.nid=t.nid ';
  }

  $query_date_from = ($query_info['date_from'])? $query_date_from = "AND n.changed >= " . $query_info['date_from']:'';

  $date_to = (!$query_info['date_to']) ? '' : $query_info['date_to'];
  $query_date_to = ($date_to) ? "AND n.changed <= $date_to" : '';
  
  $limit = empty($query_info['limit'])?'':("LIMIT 0, " . $query_info['limit']);
  
  $args = (!$query_info['gid']) ? NULL : array('og_nid' => $query_info['gid']);

  //
  // Do the query
  //

  $query = "SELECT DISTINCT n.nid FROM {node} n $join_name $join_categories "
            . " WHERE n.type='audio' "
            . " $query_status $query_promote $query_user $query_name $query_tids_or $query_tids_and $query_date_from $query_date_to "
            . " ORDER BY n.sticky DESC, n.changed DESC $limit";
  $query = db_rewrite_sql($query, 'n', 'nid', $args);

  $result = db_query($query);

  $unread_entries_only = (isset($query_info['unread_entries_only'])) ? $query_info['unread_entries_only'] : FALSE;

  $read_entries_only = FALSE;
  if (!$unread_entries_only) {
    $read_entries_only = (isset($query_info['read_entries_only'])) ? $query_info['read_entries_only'] : FALSE;
  }

  $audio_nodes = array();
  
  while ($node = db_fetch_object($result)) {
    $node_tmp = node_load(array('nid' => $node->nid));

    // if user has permission to view the entry...
    if (node_access('view', $node_tmp)) {

      // check if the user has already read the entry
      $mark = node_mark($node_tmp->nid, $node_tmp->changed);

      // if node not already read...
      if ($mark != MARK_READ) {
        if ($read_entries_only == FALSE) {
          $node_tmp->unread = TRUE;
          $audio_nodes[] = $node_tmp;
        }
      }
      // if node already read...
      else {
        if ($unread_entries_only == FALSE) {
          $node_tmp->unread = FALSE;
          $audio_nodes[] = $node_tmp;
        }
      }
    }
  }

  return $audio_nodes;
}

Would it be possible to incorporate that function to audio.module?

Thanks in advance,

Leo