You are here

referral.module in User Referral 5

Same filename and directory in other branches
  1. 6 referral.module
  2. 7 referral.module

File

referral.module
View source
<?php

// Copyright 2005 Khalid Baheyeldin http://2bits.com
define('REFERRAL_PERM_USE', 'use referral');
define('REFERRAL_PERM_ADMIN', 'administer referral');
define('REFERRAL_UID', 'referral_uid');
define('REFERRAL_TIMESTAMP', 'referral_timestamp');
define('REFERRAL_IP', 'referral_ip_address');
define('REFERRAL_REFERER', 'referral_referer');
define('REFERRAL_PAGE_COUNT', 50);
define('REFERRAL_BLOCK_COUNT', 5);
define('REFERRAL_DATE_FORMAT', 'Y-m-d H:i:s');
define('REFERRAL_HEX_START', 7);
define('REFERRAL_HEX_LENGTH', 4);
define('REFERRAL_DISPLAY_MODE', 'referral_display_mode');
define('REFERRAL_USERPOINTS', 'referral_userpoints');
define('REFERRAL_COOKIE', 'referral_data');
function referral_help($section) {
  switch ($section) {
    case 'admin/settings/referral':
      $output = t('Track users referring others to your site.');
      break;
  }
  return $output;
}
function referral_perm() {
  return array(
    REFERRAL_PERM_USE,
    REFERRAL_PERM_ADMIN,
  );
}
function referral_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/referral',
      'title' => t('User Referral'),
      'description' => t('Settings for the User Referral module.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'referral_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'referral',
      'callback' => 'referral_get',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'referral/view',
      'callback' => 'referral_view',
      'title' => t('Your Referrals'),
      'access' => user_access(REFERRAL_PERM_USE),
      'type' => MENU_SUGGESTED_ITEM,
    );
    $items[] = array(
      'path' => 'admin/logs/referral',
      'title' => t('Referral'),
      'callback' => 'referral_admin_view_summary',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/logs/referral/summary',
      'title' => t('Summary'),
      'callback' => 'referral_admin_view_summary',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'weight' => 1,
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/logs/referral/details',
      'callback' => 'referral_admin_view_details',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'weight' => 2,
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/logs/referral/unflagged',
      'title' => t('Unflagged'),
      'callback' => 'referral_admin_view_unflagged',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'weight' => 3,
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/logs/referral/roles',
      'title' => t('Roles'),
      'callback' => 'referral_admin_view_roles',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/referral/flag',
      'callback' => 'referral_admin_flag',
      'access' => user_access(REFERRAL_PERM_ADMIN),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
function referral_settings() {
  $options = array(
    0 => t('User page'),
    1 => t('Referrals page'),
  );
  $form[REFERRAL_DISPLAY_MODE] = array(
    '#type' => 'radios',
    '#title' => t('Referral link type'),
    '#default_value' => variable_get(REFERRAL_DISPLAY_MODE, 0),
    '#options' => $options,
    '#description' => t('Select the way you want the referral link to be shown.'),
  );
  return system_settings_form($form);
}
function referral_get() {
  global $user;
  if (!$user->uid) {

    // User has not logged in, or registered yet
    $uid = _referral_ref2uid(arg(1));
    if ($uid && is_numeric($uid)) {

      // A referring User ID is set and it is numeric
      referral_set_cookie($uid);
    }
    drupal_goto('user/register');
  }
  drupal_goto();
}
function referral_get_user($uid) {
  $referral_uid = false;
  $result = db_query('SELECT referral_uid FROM {referral} WHERE uid = %d', $uid);
  while ($data = db_fetch_object($result)) {
    $referral_uid = $data->referral_uid;
  }
  return $referral_uid;
}
function referral_userpoints($op, $points = 0, $uid = 0, $event = '') {
  switch ($op) {
    case 'setting':
      $group = 'referral';
      $form[$group] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('!Points for referral', userpoints_translation()),
      );
      $form[$group][REFERRAL_USERPOINTS] = array(
        '#type' => 'textfield',
        '#title' => t('!Points for referring a user', userpoints_translation()),
        '#default_value' => variable_get(REFERRAL_USERPOINTS, 0),
        '#size' => 5,
        '#maxlength' => 5,
      );
      return $form;
  }
}
function _referral_user_save($uid) {
  if (!isset($_COOKIE[REFERRAL_COOKIE])) {
    return;
  }
  $cookie = unserialize($_COOKIE[REFERRAL_COOKIE]);
  db_query("INSERT INTO {referral} (uid, referral_uid, created, host, http_referer) VALUES (%d,  %d, %d, '%s', '%s')", $uid, $cookie['uid'], $cookie['timestamp'], $cookie['ip'], $cookie['referer']);
  if (db_affected_rows()) {
    if (module_exists('userpoints')) {
      $points = variable_get(REFERRAL_USERPOINTS, 1);

      // Build the userpoints params array
      $params = array(
        'points' => $points,
        'uid' => $cookie['uid'],
        'operation' => 'referral',
        'entity_id' => $cookie['uid'],
        'entity_type' => 'user',
        'reference' => 'referral',
      );

      // Award the points to the user
      userpoints_userpointsapi($params);

      // Check to see if the role needs to be elevated

      //if (function_exists('userpoints_role_userpoints')) {

      //userpoints_role_userpoints('points after', 0, $cookie[$uid], '');

      //}
    }
  }
  else {
    watchdog('referral', t('INSERT of referral data failed.'), WATCHDOG_ERROR);
  }
}
function _referral_user_delete($uid) {
  db_query('DELETE FROM {referral} WHERE uid = %d OR referral_uid = %d', $uid);
}
function referral_user($op, $array = null, $arg_user) {
  global $user;
  switch ($op) {
    case 'insert':

      // The user is being created, save the complete referral data
      _referral_user_save($arg_user->uid);
      break;
    case 'delete':

      // The user is being deleted
      _referral_user_delete($arg_user->uid);
      break;
    case 'view':
      $referrals = array();
      $link = "referral/" . _referral_uid2ref($arg_user->uid);
      if (!variable_get(REFERRAL_DISPLAY_MODE, 0)) {
        if (user_access(REFERRAL_PERM_USE)) {
          if ($user->uid == $arg_user->uid) {

            // User is viewing own page, show referrals
            $referrals[] = array(
              'title' => t('Your referral link'),
              'value' => url($link, NULL, NULL, TRUE),
            );
            $referrals[] = array(
              'title' => t('Referrals'),
              'value' => l(t('View users you have referred'), 'referral/view'),
            );
          }
        }
      }
      if (!$user->uid) {
        $referrals[] = array(
          'title' => t('Referral link'),
          'value' => l(t('Register to this site using my referral link'), $link),
        );
        referral_set_cookie(arg(1));
      }
      if ($referrals) {
        return array(
          'Referrals' => $referrals,
        );
      }
      break;
  }
}
function referral_set_cookie($uid) {
  $cookie = array(
    'uid' => $uid,
    'timestamp' => time(),
    'ip' => $_SERVER['REMOTE_ADDR'],
    'referer' => $_SERVER['HTTP_REFERER'],
  );
  setcookie(REFERRAL_COOKIE, serialize($cookie), time() + 86400, '/');
}
function referral_block($op = 'list', $delta = 0) {
  $title[0] = t('Top referring users');
  switch ($op) {
    case 'list':
      $block[0]['info'] = $title[0];
      return $block;
    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = $title[0];
          $block['content'] = referral_block_content();
          break;
      }
      return $block;
  }
}
function referral_block_content() {
  $header = array();
  $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS num_referrals
    FROM {referral} r INNER JOIN {users} u ON u.uid = r.referral_uid
    INNER JOIN {users} u2 ON u2.uid = r.uid
    WHERE u2.status = 1
    GROUP BY r.referral_uid
    ORDER BY num_referrals DESC
    LIMIT %d';
  $result = db_query($sql, REFERRAL_BLOCK_COUNT);
  while ($data = db_fetch_object($result)) {
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->referral_uid}"),
      ),
      array(
        'data' => $data->num_referrals,
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
      ),
    );
  }
  return theme('table', $header, $rows);
}
function referral_admin_flag() {
  $uid = (int) arg(3);
  db_query('UPDATE {referral}
    SET flag = 1, flag_timestamp = %d
    WHERE referral_uid = %d', time(), $uid);
  $num = db_affected_rows();
  drupal_set_message(t('Flagged %num user(s).', array(
    '%num' => $num,
  )));
  drupal_goto('admin/logs/referral/unflagged');
}
function referral_view() {
  global $user;

  //$node = node_prepare($node, $teaser);
  if (variable_get(REFERRAL_DISPLAY_MODE, 0)) {
    $output .= '<div class="referral_link">';
    $output .= t('Your referral link: ');
    $output .= url('referral/' . _referral_uid2ref($user->uid), NULL, NULL, TRUE);
    $output .= '</div>';
  }
  $header = array(
    array(
      'data' => t('User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Roles'),
    ),
    array(
      'data' => t('Flag'),
      'field' => 'r.flag',
    ),
    array(
      'data' => t('Time'),
      'field' => 'r.created',
      'sort' => 'desc',
    ),
  );
  $sql = 'SELECT u.uid, u.name, r.created, r.flag
    FROM {referral} r INNER JOIN {users} u USING(uid)
    WHERE referral_uid = %d
    AND u.status = 1
    ORDER BY r.created DESC';
  $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, null, $user->uid);
  while ($data = db_fetch_object($result)) {
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->uid}"),
      ),
      array(
        'data' => implode(',', _referral_get_user_roles($data->uid)),
      ),
      array(
        'data' => $data->flag ? 'Yes' : 'No',
      ),
      array(
        'data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
        'colspan' => '4',
      ),
    );
  }
  $pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty($pager)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '4',
      ),
    );
  }
  print theme('page', $output . theme('table', $header, $rows), t('Referrals Report'));

  /* $node->content['referral'] = array(
  	'#value' => $output . theme('table', $header, $rows),
  	'#title' => t('Referrals Report')
    );
    return $node; */
}
function referral_admin_view_details() {
  $uid = (int) arg(4);
  $ref_user = user_load(array(
    'uid' => $uid,
  ));
  $header = array(
    array(
      'data' => t('User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Flag'),
      'field' => 'r.flag',
    ),
    array(
      'data' => t('Roles'),
    ),
    array(
      'data' => t('Time'),
      'field' => 'r.created',
      'sort' => 'desc',
    ),
    array(
      'data' => t('IP Address'),
      'field' => 'r.host',
    ),
    array(
      'data' => t('Referrer'),
      'field' => 'r.http_referer',
    ),
  );
  $sql = 'SELECT u.uid, r.flag, u.name, r.created, r.host, r.http_referer
    FROM {referral} r INNER JOIN {users} u USING(uid)
    WHERE r.referral_uid = %d
    AND u.status = 1' . tablesort_sql($header);
  $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, NULL, $uid);
  while ($data = db_fetch_object($result)) {
    $referer = check_plain(_referral_column_width($data->http_referer));
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->uid}"),
      ),
      array(
        'data' => $data->flag ? 'Yes' : 'No',
      ),
      array(
        'data' => implode(',', _referral_get_user_roles($data->uid)),
      ),
      array(
        'data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT),
      ),
      array(
        'data' => l($data->host, "http://whois.domaintools.com/{$data->host}"),
      ),
      array(
        'data' => l($referer, $data->http_referer),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
        'colspan' => '6',
      ),
    );
  }
  $pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty($pager)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '6',
      ),
    );
  }
  print theme('page', theme('table', $header, $rows), t('Referrals Report'));
}
function referral_admin_view_unflagged() {
  $header = array(
    array(
      'data' => t('Referring User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('# Unflagged'),
      'field' => 'cnt',
    ),
    array(
      'data' => t('Last'),
      'field' => 'last',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS cnt, MAX(r.created) AS last
    FROM {referral} r INNER JOIN {users} u ON(r.referral_uid = u.uid)
    WHERE r.flag = 0
    AND r.uid IN
    ( SELECT u.uid FROM {users} u WHERE status = 1 )
    GROUP BY r.referral_uid' . tablesort_sql($header);
  $result = pager_query($sql, REFERRAL_PAGE_COUNT);
  while ($data = db_fetch_object($result)) {
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->referral_uid}"),
      ),
      array(
        'data' => $data->cnt,
      ),
      array(
        'data' => format_date($data->last, 'custom', REFERRAL_DATE_FORMAT),
      ),
      array(
        'data' => l(t('details'), "admin/logs/referral/roles/{$data->referral_uid}") . ' | ' . l(t('flag'), "admin/referral/flag/{$data->referral_uid}"),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
        'colspan' => '4',
      ),
    );
  }
  $pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty($pager)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '4',
      ),
    );
  }
  print theme('page', theme('table', $header, $rows), t('Referrals Report'));
}
function referral_admin_view_roles() {
  $uid = (int) arg(4);
  $header = array(
    array(
      'data' => t('User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Roles'),
    ),
    array(
      'data' => t('Time'),
      'field' => 'r.created',
      'sort' => 'desc',
    ),
    array(
      'data' => t('IP Address'),
      'field' => 'r.host',
    ),
    array(
      'data' => t('Referrer'),
      'field' => 'r.http_referer',
    ),
  );
  $sql = 'SELECT u.uid, u.name, r.created, r.host, r.http_referer
    FROM {referral} r INNER JOIN {users} u USING(uid)
    WHERE r.referral_uid = %d
    AND r.flag = 0
    AND r.uid IN
    ( SELECT u.uid FROM {users} u WHERE status = 1 )' . tablesort_sql($header);
  $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, NULL, $uid);
  while ($data = db_fetch_object($result)) {
    $referer = check_plain(_referral_column_width($data->http_referer));
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->uid}"),
      ),
      array(
        'data' => implode(',', _referral_get_user_roles($data->uid)),
      ),
      array(
        'data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT),
      ),
      array(
        'data' => l($data->host, "http://whois.domaintools.com/{$data->host}"),
      ),
      array(
        'data' => l($referer, $data->http_referer),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
        'colspan' => '5',
      ),
    );
  }
  $pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty($pager)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '5',
      ),
    );
  }
  $user = user_load(array(
    'uid' => $uid,
  ));
  $output = t('Unflagged referral users with roles for: @user', array(
    '@user' => l($user->name, "user/{$user->uid}"),
  ));
  $output .= theme('table', $header, $rows);
  print theme('page', $output, t('Roles Report'));
}
function referral_admin_view_summary() {
  $header = array(
    array(
      'data' => t('Referring User'),
      'field' => 'r.referral_uid',
    ),
    array(
      'data' => t('# Referred'),
      'field' => 'num_referrals',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Last Referral'),
      'field' => 'r.created',
    ),
    array(
      'data' => t('Details'),
    ),
  );
  $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS num_referrals, MAX(r.created) AS last
    FROM {referral} r INNER JOIN {users} u ON u.uid = r.referral_uid
    INNER JOIN {users} u2 ON u2.uid = r.uid
    WHERE u2.status = 1
    GROUP BY r.referral_uid' . tablesort_sql($header);
  $result = pager_query($sql, REFERRAL_PAGE_COUNT);
  while ($data = db_fetch_object($result)) {
    $rows[] = array(
      array(
        'data' => l($data->name, "user/{$data->referral_uid}"),
      ),
      array(
        'data' => $data->num_referrals,
      ),
      array(
        'data' => format_date($data->last, 'custom', REFERRAL_DATE_FORMAT),
      ),
      array(
        'data' => l(t('details'), "admin/logs/referral/details/{$data->referral_uid}"),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No data.'),
        'colspan' => '4',
      ),
    );
  }
  $pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty($pager)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '4',
      ),
    );
  }
  print theme('page', theme('table', $header, $rows), t('Referrals Report'));
}
function _referral_column_width($column, $width = 30) {
  return strlen($column) > $width ? substr($column, 0, $width) . '...' : $column;
}
function _referral_ref2uid($ref) {
  if ($ref) {
    if (is_numeric('0x' . $ref)) {
      $uid = hexdec($ref) - _referral_hex_seed();
      return $uid;
    }
  }
  return false;
}
function _referral_uid2ref($uid) {
  if ($uid) {
    if (is_numeric($uid)) {
      $ref = dechex(_referral_hex_seed() + $uid);
      return $ref;
    }
  }
  return false;
}
function _referral_hex_seed() {
  global $base_url;
  $seed = hexdec(_referral_asc2hex(substr($base_url, REFERRAL_HEX_START, REFERRAL_HEX_LENGTH)));
  return $seed;
}
function _referral_asc2hex($asc_str) {
  for ($i = 0; $i < strlen($asc_str); $i++) {
    $hex_str .= sprintf("%02x", ord(substr($asc_str, $i, 1)));
  }
  return $hex_str;
}
function _referral_get_user_roles($uid) {
  $data = array();
  $result = db_query('SELECT r.name FROM {role} r INNER JOIN {users_roles} ur USING (rid)
    WHERE ur.uid = %d', $uid);
  while ($row = db_fetch_object($result)) {
    $data[] = $row->name;
  }
  return $data;
}