/[drupal]/contributions/modules/referral/referral.module
ViewVC logotype

Contents of /contributions/modules/referral/referral.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.18 - (show annotations) (download) (as text)
Wed Nov 26 20:04:44 2008 UTC (11 months, 4 weeks ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +59 -91 lines
File MIME type: text/x-php
#338760 by rcourtna, add a hook_referral() so other modules can act on
referrals.
The userpoints stuff has been removed, and will be in its own module.
1 <?php
2
3 // $Id: referral.module,v 1.16.2.5 2008/11/26 20:03:41 kbahey Exp $
4
5 /**
6 * @file
7 *
8 */
9
10 // Copyright 2005 Khalid Baheyeldin http://2bits.com
11
12 // ported to Drupal 6 by andyceo http://andyceo.ruware.com/
13
14 define('REFERRAL_PERM_USE', 'use referral');
15 define('REFERRAL_PERM_ADMIN', 'administer referral');
16 define('REFERRAL_UID', 'referral_uid');
17 define('REFERRAL_TIMSTAMP', 'referral_timestamp');
18 define('REFERRAL_IP', 'referral_ip_address');
19 define('REFERRAL_REFERER', 'referral_referer');
20 define('REFERRAL_PAGE_COUNT', 50);
21 define('REFERRAL_BLOCK_COUNT', 5);
22 define('REFERRAL_DATE_FORMAT', 'Y-m-d H:i:s');
23 define('REFERRAL_HEX_START', 7);
24 define('REFERRAL_HEX_LENGTH', 4);
25 define('REFERRAL_DISPLAY_MODE', 'referral_display_mode');
26 define('REFERRAL_COOKIE', 'referral_data');
27 define('REFERRAL_GOTO_PATH', 'referral_goto_path');
28
29
30 function referral_help($path, $arg) {
31 switch ($path) {
32 case 'admin/settings/referral':
33 $output = t('Track users referring others to your site');
34 break;
35 }
36 return $output;
37 }
38
39 function referral_perm() {
40 return array(REFERRAL_PERM_USE, REFERRAL_PERM_ADMIN);
41 }
42
43 function referral_menu() {
44 $items = array();
45
46 $items['admin/settings/referral'] = array(
47 'title' => t('User Referral'),
48 'description' => t('Settings for the User Referral module.'),
49 'page callback' => 'drupal_get_form',
50 'page arguments' => array('referral_settings'),
51 'access arguments' => array('administer site configuration'),
52 'type' => MENU_NORMAL_ITEM,
53 );
54
55 $items['referral'] = array(
56 'page callback' => 'referral_get',
57 'access callback' => TRUE,
58 'type' => MENU_CALLBACK,
59 );
60
61 $items['referral/view'] = array(
62 'page callback' => 'referral_view',
63 'title' => t('Your Referrals'),
64 'access arguments' => array(REFERRAL_PERM_USE),
65 'type' => MENU_SUGGESTED_ITEM,
66 );
67
68 $items['admin/reports/referral'] = array(
69 'title' => t('Referrals summary'),
70 'page callback' => 'referral_admin_view_summary',
71 'access arguments' => array(REFERRAL_PERM_ADMIN),
72 'type' => MENU_NORMAL_ITEM,
73 );
74
75 $items['admin/reports/referral/summary'] = array(
76 'title' => t('Referrals summary'),
77 'page callback' => 'referral_admin_view_summary',
78 'access arguments' => array(REFERRAL_PERM_ADMIN),
79 'weight' => 1,
80 'type' => MENU_NORMAL_ITEM,
81 );
82
83 $items['admin/reports/referral/details'] = array(
84 'page callback' => 'referral_admin_view_details',
85 'access arguments' => array(REFERRAL_PERM_ADMIN),
86 'weight' => 2,
87 'type' => MENU_CALLBACK,
88 );
89
90 $items['admin/reports/referral/unflagged'] = array(
91 'title' => t('Unflagged referrals'),
92 'page callback' => 'referral_admin_view_unflagged',
93 'access arguments' => array(REFERRAL_PERM_ADMIN),
94 'weight' => 3,
95 'type' => MENU_NORMAL_ITEM,
96 );
97
98 $items['admin/reports/referral/roles'] = array(
99 'title' => t('Referral roles'),
100 'page callback' => 'referral_admin_view_roles',
101 'access arguments' => array(REFERRAL_PERM_ADMIN),
102 'type' => MENU_CALLBACK,
103 );
104
105 $items['admin/referral/flag'] = array(
106 'page callback' => 'referral_admin_flag',
107 'access arguments' => array(REFERRAL_PERM_ADMIN),
108 'type' => MENU_CALLBACK,
109 );
110 return $items;
111 }
112
113 function referral_settings() {
114 $options = array(
115 0 => t('User page'),
116 1 => t('Referrals page'),
117 );
118
119 $form[REFERRAL_DISPLAY_MODE] = array(
120 '#type' => 'radios',
121 '#title' => t('Referral link type'),
122 '#default_value' => variable_get(REFERRAL_DISPLAY_MODE, 0),
123 '#options' => $options,
124 '#description' => t('Select the way you want the referral link to be shown.'),
125 );
126
127 $form[REFERRAL_GOTO_PATH] = array(
128 '#type' => 'textfield',
129 '#title' => t('Referral goto path'),
130 '#default_value' => variable_get(REFERRAL_GOTO_PATH, 'user/register'),
131 '#description' => t('The path to redirect to after visiting the referral link.'),
132 );
133
134 return system_settings_form($form);
135 }
136
137 function referral_get() {
138 global $user;
139
140 if (!$user->uid) {
141 // User has not logged in, or registered yet
142 $uid = _referral_ref2uid(arg(1));
143 if (($uid) && is_numeric($uid)) {
144 // A referring User ID is set and it is numeric
145 $cookie = array(
146 'uid' => $uid,
147 'timstamp' => time(),
148 'ip' => ip_address(),
149 'referer' => $_SERVER['HTTP_REFERER'],
150 );
151
152 setcookie(REFERRAL_COOKIE, serialize($cookie), time() + 86400, '/');
153 }
154 drupal_goto(variable_get(REFERRAL_GOTO_PATH, 'user/register'));
155 }
156 drupal_goto();
157 }
158
159 function referral_get_user($uid) {
160 $referral_uid = FALSE;
161 $result = db_query('SELECT referral_uid FROM {referral} WHERE uid = %d', $uid);
162 while ($data = db_fetch_object($result)) {
163 $referral_uid = $data->referral_uid;
164 }
165 return $referral_uid;
166 }
167
168 function _referral_user_save($uid) {
169 if (!isset($_COOKIE[REFERRAL_COOKIE])) {
170 return;
171 }
172
173 $cookie = unserialize($_COOKIE[REFERRAL_COOKIE]);
174 db_query("INSERT INTO {referral} (uid, referral_uid, created, host, http_referer) VALUES (%d, %d, %d, '%s', '%s')",
175 $uid,
176 $cookie['uid'],
177 $cookie['timstamp'],
178 $cookie['ip'],
179 $cookie['referer']);
180
181 if (db_affected_rows()) {
182 module_invoke_all('referral', $uid, $cookie['uid']);
183 }
184 else {
185 watchdog('referral', 'INSERT of referral data failed.', array(), WATCHDOG_ERROR);
186 }
187 }
188
189 function _referral_user_delete($uid) {
190 db_query('DELETE FROM {referral} WHERE uid = %d OR referral_uid = %d', $uid);
191 }
192
193 function referral_user($op, $array = NULL, $arg_user) {
194 global $user;
195 switch ($op) {
196 case 'insert':
197 // The user is being created, save the complete referral data
198 _referral_user_save($arg_user->uid);
199 break;
200
201 case 'delete':
202 // The user is being deleted
203 _referral_user_delete($arg_user->uid);
204 break;
205
206 case 'view':
207 $referrals = array();
208 $link = "referral/" . _referral_uid2ref($arg_user->uid);
209
210 if (!variable_get(REFERRAL_DISPLAY_MODE, 0)) {
211 if (user_access(REFERRAL_PERM_USE)) {
212 if ($user->uid == $arg_user->uid) {
213 // User is viewing own page, show referrals
214 $referrals[] = array(
215 '#title' => t('Your referral link'),
216 '#value' => url($link, array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE)),
217 '#type' => 'user_profile_item',
218 );
219 $referrals[] = array(
220 '#title' => t('Referrals'),
221 '#value' => l(t('View users you have referred'), 'referral/view'),
222 '#type' => 'user_profile_item',
223 );
224 }
225 }
226 }
227
228 if (!$user->uid) {
229 $referrals[] = array(
230 '#title' => t('Referral link'),
231 '#value' => l(t('Register to this site using my referral link'), $link),
232 '#type' => 'user_profile_item',
233 );
234 }
235
236 if ($referrals) {
237 $referrals['#type'] = 'user_profile_category';
238 $referrals['#title'] = t('Referrals');
239 $referrals['#weight'] = 10;
240 $arg_user->content['Referrals'] = $referrals;
241 // return array('Referrals' => $referrals); // obsolete from drupal 5
242 }
243 break;
244 }
245 }
246
247 function referral_block($op = 'list', $delta = 0) {
248 $title[0] = t('Top referring users');
249 switch ($op) {
250 case 'list':
251 $block[0]['info'] = $title[0];
252 return $block;
253
254 case 'view':
255 switch ($delta) {
256 case 0:
257 $block['subject'] = $title[0];
258 $block['content'] = referral_block_content();
259 break;
260 }
261 return $block;
262 }
263 }
264
265 function referral_block_content() {
266 $header = array();
267 $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS num_referrals
268 FROM {referral} r INNER JOIN {users} u ON u.uid = r.referral_uid
269 INNER JOIN {users} u2 ON u2.uid = r.uid
270 WHERE u2.status = 1
271 GROUP BY r.referral_uid
272 ORDER BY num_referrals DESC
273 LIMIT %d';
274 $result = db_query($sql, REFERRAL_BLOCK_COUNT);
275 while ($data = db_fetch_object($result)) {
276 $rows[] = array(
277 array('data' => l($data->name, "user/$data->referral_uid")),
278 array('data' => $data->num_referrals),
279 );
280 }
281 if (!$rows) {
282 $rows[] = array(array('data' => t('No data.')));
283 }
284 return theme('table', $header, $rows);
285 }
286
287 function referral_admin_flag() {
288 $uid = (int)arg(3);
289 db_query('UPDATE {referral}
290 SET flag = 1, flag_timestamp = %d
291 WHERE referral_uid = %d', time(), $uid);
292 $num = db_affected_rows();
293 drupal_set_message(t('Flagged %num user(s).', array('%num' => $num)));
294 drupal_goto('admin/reports/referral/unflagged');
295 }
296
297 function referral_view() {
298 global $user;
299 //$node = node_prepare($node, $teaser);
300
301 if (variable_get(REFERRAL_DISPLAY_MODE, 0)) {
302 $output .= '<div class="referral_link">';
303 $output .= t('Your referral link: ');
304 $output .= url('referral/'. _referral_uid2ref($user->uid), array('query' => NULL, 'fragment' => NULL, 'absolute' => TRUE));
305 $output .= '</div>';
306 }
307
308 $header = array(
309 array('data' => t('User'), 'field' => 'u.name'),
310 array('data' => t('Roles')),
311 array('data' => t('Flag'), 'field' => 'r.flag'),
312 array('data' => t('Time'), 'field' => 'r.created', 'sort' => 'desc'),
313 );
314
315 $sql = 'SELECT u.uid, u.name, r.created, r.flag
316 FROM {referral} r INNER JOIN {users} u USING(uid)
317 WHERE referral_uid = %d
318 AND u.status = 1
319 ORDER BY r.created DESC';
320
321 $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, NULL, $user->uid);
322 while ($data = db_fetch_object($result)) {
323 $rows[] = array(
324 array('data' => l($data->name, "user/$data->uid")),
325 array('data' => implode(',', _referral_get_user_roles($data->uid))),
326 array('data' => ($data->flag ? 'Yes' : 'No')),
327 array('data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT)),
328 );
329 }
330 if (!$rows) {
331 $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
332 }
333 $pager = theme('pager', NULL, REFERRAL_PAGE_COUNT, 0);
334 if (!empty($pager)) {
335 $rows[] = array(array('data' => $pager, 'colspan' => '4'));
336 }
337 print theme('page', $output . theme('table', $header, $rows), t('Referrals Report'));
338 /* $node->content['referral'] = array(
339 '#value' => $output . theme('table', $header, $rows),
340 '#title' => t('Referrals Report')
341 );
342 return $node; */
343 }
344
345 function referral_admin_view_details() {
346 $uid = (int)arg(4);
347 $ref_user = user_load(array('uid' => $uid));
348 $header = array(
349 array('data' => t('User'), 'field' => 'u.name'),
350 array('data' => t('Flag'), 'field' => 'r.flag'),
351 array('data' => t('Roles')),
352 array('data' => t('Time'), 'field' => 'r.created', 'sort' => 'desc'),
353 array('data' => t('IP Address'), 'field' => 'r.host'),
354 array('data' => t('Referrer'), 'field' => 'r.http_referer'),
355 );
356
357 $sql = 'SELECT u.uid, r.flag, u.name, r.created, r.host, r.http_referer
358 FROM {referral} r INNER JOIN {users} u USING(uid)
359 WHERE r.referral_uid = %d
360 AND u.status = 1' . tablesort_sql($header);
361 $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, NULL, $uid);
362 while ($data = db_fetch_object($result)) {
363 $referer = check_plain(_referral_column_width($data->http_referer));
364 $rows[] = array(
365 array('data' => l($data->name, "user/$data->uid")),
366 array('data' => ($data->flag ? 'Yes' : 'No')),
367 array('data' => implode(',', _referral_get_user_roles($data->uid))),
368 array('data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT)),
369 array('data' => l($data->host, "http://whois.domaintools.com/$data->host")),
370 array('data' => l($referer, $data->http_referer)),
371 );
372 }
373 if (!$rows) {
374 $rows[] = array(array('data' => t('No data.'), 'colspan' => '6'));
375 }
376 $pager = theme('pager', NULL, REFERRAL_PAGE_COUNT, 0);
377 if (!empty($pager)) {
378 $rows[] = array(array('data' => $pager, 'colspan' => '6'));
379 }
380 print theme('page', theme('table', $header, $rows), t('Referrals Report'));
381 }
382
383 function referral_admin_view_unflagged() {
384
385 $header = array(
386 array('data' => t('Referring User'), 'field' => 'u.name'),
387 array('data' => t('# Unflagged'), 'field' => 'cnt'),
388 array('data' => t('Last'), 'field' => 'last', 'sort' => 'desc'),
389 array('data' => t('Operations')),
390 );
391
392 $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS cnt, MAX(r.created) AS last
393 FROM {referral} r INNER JOIN {users} u ON(r.referral_uid = u.uid)
394 WHERE r.flag = 0
395 AND r.uid IN
396 ( SELECT u.uid FROM {users} u WHERE status = 1 )
397 GROUP BY r.referral_uid' . tablesort_sql($header);
398 $result = pager_query($sql, REFERRAL_PAGE_COUNT);
399 while ($data = db_fetch_object($result)) {
400 $rows[] = array(
401 array('data' => l($data->name, "user/$data->referral_uid")),
402 array('data' => $data->cnt),
403 array('data' => format_date($data->last, 'custom', REFERRAL_DATE_FORMAT)),
404 array('data' => l(t('details'), "admin/reports/referral/roles/$data->referral_uid") .' | '.
405 l(t('flag'), "admin/referral/flag/$data->referral_uid")),
406 );
407 }
408 if (!$rows) {
409 $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
410 }
411 $pager = theme('pager', NULL, REFERRAL_PAGE_COUNT, 0);
412 if (!empty($pager)) {
413 $rows[] = array(array('data' => $pager, 'colspan' => '4'));
414 }
415 print theme('page', theme('table', $header, $rows), t('Referrals Report'));
416 }
417
418 function referral_admin_view_roles() {
419 $uid = (int)arg(4);
420
421 $header = array(
422 array('data' => t('User'), 'field' => 'u.name'),
423 array('data' => t('Roles')),
424 array('data' => t('Time'), 'field' => 'r.created', 'sort' => 'desc'),
425 array('data' => t('IP Address'), 'field' => 'r.host'),
426 array('data' => t('Referrer'), 'field' => 'r.http_referer'),
427 );
428
429 $sql = 'SELECT u.uid, u.name, r.created, r.host, r.http_referer
430 FROM {referral} r INNER JOIN {users} u USING(uid)
431 WHERE r.referral_uid = %d
432 AND r.flag = 0
433 AND r.uid IN
434 ( SELECT u.uid FROM {users} u WHERE status = 1 )' . tablesort_sql($header);
435 $result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, NULL, $uid);
436 while ($data = db_fetch_object($result)) {
437 $referer = check_plain(_referral_column_width($data->http_referer));
438 $rows[] = array(
439 array('data' => l($data->name, "user/$data->uid")),
440 array('data' => implode(',', _referral_get_user_roles($data->uid))),
441 array('data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT)),
442 array('data' => l($data->host, "http://whois.domaintools.com/$data->host")),
443 array('data' => l($referer, $data->http_referer)),
444 );
445 }
446 if (!$rows) {
447 $rows[] = array(array('data' => t('No data.'), 'colspan' => '5'));
448 }
449 $pager = theme('pager', NULL, REFERRAL_PAGE_COUNT, 0);
450 if (!empty($pager)) {
451 $rows[] = array(array('data' => $pager, 'colspan' => '5'));
452 }
453 $user = user_load(array('uid' => $uid));
454 $output = t('Unflagged referral users with roles for: @user', array('@user' => l($user->name, "user/$user->uid")));
455 $output .= theme('table', $header, $rows);
456 print theme('page', $output, t('Roles Report'));
457 }
458
459 function referral_admin_view_summary() {
460 $header = array(
461 array('data' => t('Referring User'), 'field' => 'r.referral_uid'),
462 array('data' => t('# Referred'), 'field' => 'num_referrals', 'sort' => 'desc'),
463 array('data' => t('Last Referral'), 'field' => 'r.created'),
464 array('data' => t('Details')),
465 );
466
467 $sql = 'SELECT r.referral_uid, u.name, COUNT(*) AS num_referrals, MAX(r.created) AS last
468 FROM {referral} r INNER JOIN {users} u ON u.uid = r.referral_uid
469 INNER JOIN {users} u2 ON u2.uid = r.uid
470 WHERE u2.status = 1
471 GROUP BY r.referral_uid' . tablesort_sql($header);
472 $result = pager_query($sql, REFERRAL_PAGE_COUNT);
473 while ($data = db_fetch_object($result)) {
474 $rows[] = array(
475 array('data' => l($data->name, "user/$data->referral_uid")),
476 array('data' => $data->num_referrals),
477 array('data' => format_date($data->last, 'custom', REFERRAL_DATE_FORMAT)),
478 array('data' => l(t('details'), "admin/reports/referral/details/$data->referral_uid")),
479 );
480 }
481 if (!$rows) {
482 $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
483 }
484 $pager = theme('pager', NULL, REFERRAL_PAGE_COUNT, 0);
485 if (!empty($pager)) {
486 $rows[] = array(array('data' => $pager, 'colspan' => '4'));
487 }
488 print theme('page', theme('table', $header, $rows), t('Referrals Report'));
489 }
490
491 function _referral_column_width($column, $width = 30) {
492 return (strlen($column) > $width ? substr($column, 0, $width) .'...' : $column);
493 }
494
495 function _referral_ref2uid($ref) {
496 if ($ref) {
497 if (is_numeric('0x'. $ref)) {
498 $uid = hexdec($ref) - _referral_hex_seed();
499 return $uid;
500 }
501 }
502 return FALSE;
503 }
504
505 function _referral_uid2ref($uid) {
506 if ($uid) {
507 if (is_numeric($uid)) {
508 $ref = dechex(_referral_hex_seed() + $uid);
509 return $ref;
510 }
511 }
512 return FALSE;
513 }
514
515 function _referral_hex_seed() {
516 global $base_url;
517 $seed = hexdec(_referral_asc2hex(substr($base_url, REFERRAL_HEX_START, REFERRAL_HEX_LENGTH)));
518 return $seed;
519 }
520
521 function _referral_asc2hex($asc_str) {
522 for ($i=0; $i<strlen($asc_str); $i++) {
523 $hex_str .= sprintf("%02x", ord(substr($asc_str, $i, 1)));
524 }
525 return $hex_str;
526 }
527
528 function _referral_get_user_roles($uid) {
529 $data = array();
530 $result = db_query('SELECT r.name FROM {role} r INNER JOIN {users_roles} ur USING (rid)
531 WHERE ur.uid = %d', $uid);
532 while ($row = db_fetch_object($result)) {
533 $data[] = $row->name;
534 }
535 return $data;
536 }
537

  ViewVC Help
Powered by ViewVC 1.1.2