| 1 |
<?php
|
| 2 |
// $Id: pivots_block.module,v 1.18 2009/09/08 20:13:24 danithaca Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file provides the pivots_blocks that generates module recommendations.
|
| 7 |
*
|
| 8 |
* Current limitations/issues:
|
| 9 |
* 1. Recommendation data is stored externally on master-other.drupal.org database.
|
| 10 |
* The reason to use external database is to give flexibility to developers to tweak the algorithm.
|
| 11 |
* 2. "More" is disabled temporarily to make the code clearer, will be added later if needed.
|
| 12 |
*
|
| 13 |
* This module was developed with support from the National Science
|
| 14 |
* Foundatio under award IIS-0812042. Any opinions, findings, and conclusions
|
| 15 |
* or recommendations expressed or embodied in this software are those of the
|
| 16 |
* author(s) and do not necessarily reflect the views of the National Science
|
| 17 |
* Foundation.
|
| 18 |
*/
|
| 19 |
|
| 20 |
define('PID_CONVERSATION', 4151); // conversation pivot algorithm ID that displays related conversations
|
| 21 |
define('PID_DOUBLE', 5001); // double pivot algorithm ID that displays related projects in different algorithms.
|
| 22 |
define('LIMIT_CONVERSATION', 5);
|
| 23 |
define('LIMIT_DOUBLE', 5);
|
| 24 |
define('LIMIT_MAX', 100); // maximum items to display in the block or on a page.
|
| 25 |
|
| 26 |
function pivots_block_output() {
|
| 27 |
if ($node = project_get_project_from_menu()) {
|
| 28 |
|
| 29 |
//HACK: randomize algorithm rotation
|
| 30 |
global $_pivots_block_pid_double; // double pivot ID defaults to conversation pivots
|
| 31 |
switch (rand(0,1)) {
|
| 32 |
case 0:
|
| 33 |
$_pivots_block_pid_double = 5001; // conversation pivots
|
| 34 |
break;
|
| 35 |
case 1:
|
| 36 |
$_pivots_block_pid_double = 5002; // solr10
|
| 37 |
break;
|
| 38 |
}
|
| 39 |
|
| 40 |
$output = _pivots_block_display_on_project($node->nid);
|
| 41 |
return $output;
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
function _pivots_block_display_on_project($node_id) {
|
| 46 |
global $_pivots_block_pid_double;
|
| 47 |
$output = '';
|
| 48 |
$output .= _pivots_block_content($node_id, PID_CONVERSATION, t("Related discussions"), LIMIT_CONVERSATION);
|
| 49 |
$output .= _pivots_block_content($node_id, $_pivots_block_pid_double, t("Related projects"), LIMIT_DOUBLE);
|
| 50 |
return $output;
|
| 51 |
}
|
| 52 |
|
| 53 |
function _pivots_block_display_on_forum($node_id) {
|
| 54 |
$output = '';
|
| 55 |
$output = _pivots_block_content($node_id, PID_CONVERSATION, t("Projects mentioned in the discussion"), LIMIT_CONVERSATION);
|
| 56 |
return $output;
|
| 57 |
}
|
| 58 |
|
| 59 |
function _pivots_block_content($node_id, $pivot_id, $title, $limit) {
|
| 60 |
$output = '';
|
| 61 |
$ga_event = "$node_id";
|
| 62 |
$items = _pivots_block_generate_items($node_id, $pivot_id, $limit);
|
| 63 |
if (!empty($items)) {
|
| 64 |
foreach ($items as $position => $item) {
|
| 65 |
$items[$position] = l($item['title'], "node/{$item['nid']}", array( 'attributes' => array(
|
| 66 |
"onClick" => "javascript:pageTracker._trackEvent('PivotsClick_${pivot_id}', '${node_id}_{$item['nid']}');"
|
| 67 |
)));
|
| 68 |
$ga_event .= "_{$item['nid']}";
|
| 69 |
}
|
| 70 |
$output = theme('item_list', $items, $title);
|
| 71 |
}
|
| 72 |
$GLOBALS['conf']['googleanalytics_codesnippet_after'] .= "pageTracker._trackEvent('PivotsPageview_{$pivot_id}', '{$ga_event}');";
|
| 73 |
return $output;
|
| 74 |
}
|
| 75 |
|
| 76 |
function _pivots_block_generate_items($node_id, $pivot_id, $limit) {
|
| 77 |
if ($limit <= 0) {
|
| 78 |
$limit = LIMIT_MAX;
|
| 79 |
}
|
| 80 |
|
| 81 |
db_set_active('pivots'); // NOTE: here we activate the pivots database.
|
| 82 |
// if there's database failure, we just pretend nothing happens whatsoever. pivots_block returns nothing in this case.
|
| 83 |
$matches = @db_query("SELECT DISTINCT dest_id FROM {pivots_match} WHERE pivot_id=%d AND src_id=%d
|
| 84 |
AND dest_id<>%d ORDER BY score DESC", $pivot_id, $node_id, $node_id);
|
| 85 |
db_set_active(); // NOTE: change back to use the default database
|
| 86 |
|
| 87 |
$count = 0;
|
| 88 |
$items = array();
|
| 89 |
while (($match = @db_fetch_array($matches)) && $count < $limit) {
|
| 90 |
$dest_id = $match['dest_id'];
|
| 91 |
$result = db_query("SELECT title FROM {node} WHERE nid=%d AND status=1", $dest_id);
|
| 92 |
// there might be cases that the node was deleted, or set to unpublished between pivots database refresh
|
| 93 |
// so here we only count the valid node.
|
| 94 |
$title = db_result($result);
|
| 95 |
if ($title) {
|
| 96 |
$items[] = array('nid' => $dest_id, 'title' => $title, 'pid' => $pivot_id);
|
| 97 |
$count++;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
return $items;
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* Implementation of hook_block()
|
| 105 |
*/
|
| 106 |
function pivots_block_block($op = 'list', $delta = 0, $edit = array()) {
|
| 107 |
switch ($op) {
|
| 108 |
case 'list':
|
| 109 |
$blocks[0]['info'] = t('pivots_block: Recommendations');
|
| 110 |
return $blocks;
|
| 111 |
|
| 112 |
case 'view':
|
| 113 |
$block['subject'] = t('Recommendations');
|
| 114 |
$block['content'] = pivots_block_output();
|
| 115 |
return $block;
|
| 116 |
}
|
| 117 |
}
|