I started over on one issue: http://drupal.org/node/667992#comment-3118928

This is an attempt to add functionality to Referral Sources (see subject). I apologize in advance for not knowing (yet) how to make patch files.

I propose a new function that will generate a formatted table. I started with referralsources_statistics_selections() to make the code for referralsources_user_selections(). An additional permission "view referral source individual responses" could help support this functionality, but I just reused "view referral source statistics" for now. Lastly, there is an addition to referralsources_user() to display the content in the user's account page.

Here are the code additions/alterations:

<?php

/**
 * Create a table of referral sources for this user.
 * @param int $uid The UID of the user to reference.
 * @param int $rstid The type of referral source to reference.
 * @return string A formatted table of Referral Source responses.
 */
function referralsources_user_selections($uid = NULL, $rstid = NULL) {
  $output = '';
  $rows = array();

  // Build referral source selections table
  $header = array(
      array('data' => t('From'), 'field' => 'rstid'),
      array('data' => t('Response'), 'field' => 'title'),
      array('data' => t('Other response'), 'field' => 'other'),
  );

  $query = "SELECT rs.rid, r.title, rs.rstid, rs.other, rs.order_id "
          ."FROM {referralsources_submissions} rs LEFT JOIN {referralsources} r ON rs.rid = r.rid "
          ."WHERE TRUE ". (!empty($rstid) ? "AND rstid = '%s' " : '')
          . (!empty($uid) ? "AND uid = '%s' " : '')
          . tablesort_sql($header);

  $args = array();
  if (!empty($rstid)) $args[] = $rstid;
  if (!empty($uid)) $args[] = $uid;

  $result = db_query($query, $args);
  while ($data = db_fetch_object($result)) {
    $row = array();
    if (empty($data->rid)) $data->rid = 0;
    
    //TODO:  Replace text strings with CONSTANTS?
    $data->rstid = check_plain($data->rstid); //Scrub it!
    if ($data->rstid == 'referralsources_user_register') $data->rstid = t("User Registration");
    if ($data->rstid == 'referralsources_webform') $data->rstid = t("Webform"); //TODO: Link to the user's Webform submission?
    if ($data->rstid == 'referralsources_uc_order') {
      //Link to the admin order page if the user has access:
      $data->rstid = user_access('view all orders') ? l(t("UC Order"), "admin/store/orders/".$data->order_id) : t("UC Order #").$data->order_id;
    }
    if ($data->rid == 0) $data->title = t("Other");
    
    $row[] = $data->rstid;//Scrubed above
    $row[] = check_plain($data->title);
    $row[] = check_plain($data->other);
    $rows[] = array('data' => $row);
  }

  //$output .= '<h3>'. t("Referral Source responses:") .'</h3>';
  if (count($rows) > 0) {
    // Rows, display table of referral source selections
    $output .= theme('table', $header, $rows);
  }
  else {
    // No rows, display message instead of empty table
    $output .= '<p>'. t('There are no responses on record.') .'</p>';
  }

  return $output;
}

/**
 * Implementation of hook_user();
 */
function referralsources_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'register':
      if (variable_get('referralsource_register_enable', REFERRALSOURCE_REGISTER_ENABLE_DEFAULT)) {
        $form = array();
        $form['referralsource'] = array(
          '#type' => 'fieldset',
          '#title' => check_plain(variable_get('referralsource_fieldset_title', REFERRALSOURCE_FIELDSET_TITLE_DEFAULT)),
          '#description' => check_plain(variable_get('referralsource_fieldset_description', REFERRALSOURCE_FIELDSET_DESCRIPTION_DEFAULT)),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
          '#weight' => variable_get('referralsource_register_weight', REFERRALSOURCE_REGISTER_WEIGHT_DEFAULT),
        );
        $form['referralsource']['referralsources_select'] = array(
          '#type' => 'referralsources_select',
        );
        return $form;
      }
      break;

    case 'insert':
      if (!empty($edit['referralsources_select'])) {
        db_query("INSERT INTO {referralsources_submissions} (rstid, uid, rid, other, created_datetime) VALUES ('%s', %d, %d, '%s', '%s')",
          REFERRALSOURCE_SUBMISSION_TYPE_USER_REGISTER, $edit['uid'], $edit['referralsources_select']['referralsources_rid'], $edit['referralsources_select']['referralsources_other'], date('c')
        );
      }
      break;
    
    //chadhester:  Proposed addition 20100622-1720
    case 'view':
      if (user_access("view referral source statistics")) {
        //Create a table of a users referral sources selections
        $refsrcs = referralsources_user_selections($account->uid);
        $account->content['ReferralSources'] = array(
          '#type' => 'user_profile_category',
          '#attributes' => array('class' => 'user-member'),
          '#weight' => -49,
          '#title' => t('Referral Sources'),
        );
        
        $account->content['ReferralSources']['Submissions'] = array(
          '#type' => 'user_profile_item',
          '#title' => '',
          '#value' => $refsrcs,
        );
        
      }
      break;
    //chadhester:  end addition
  }
}


?>

I'm sure this can be optimized, but this is at least a start. Right now I have not modified the referral sources module. Instead, I use this logic from my own, separate module.

I would be more than happy to help develop this feature more with any suggestions.

Comments

spacebunny’s picture

Thanks for this! I found it useful. I hope it gets into the next release.