| 1 |
<?php
|
| 2 |
// $Id:$
|
| 3 |
|
| 4 |
if (module_exist('views')) {
|
| 5 |
include_once('node_recommendation_views.inc');
|
| 6 |
}
|
| 7 |
|
| 8 |
function node_recommedation_help($section) {
|
| 9 |
switch ($section) {
|
| 10 |
case 'admin/modules#description':
|
| 11 |
return t("Provides blocks and an api to display Recommended Nodes");
|
| 12 |
case 'admin/settings/cre':
|
| 13 |
return t("Provides several different ways to display Recommended Nodes to users");
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
/*
|
| 18 |
* implementation of hook_block()
|
| 19 |
*/
|
| 20 |
|
| 21 |
function node_recommedation_block($op = 'list', $delta = 0, $edit = array()) {
|
| 22 |
// LOCAL VARIABLES!! FIX THIS TO THE ACTUAL USER AND n
|
| 23 |
global $user;
|
| 24 |
$uid = $user->uid;
|
| 25 |
$name = $user->name;
|
| 26 |
|
| 27 |
// The $op parameter determines what piece of information is being requested.
|
| 28 |
switch ($op) {
|
| 29 |
case 'list':
|
| 30 |
// If $op is "list", we just need to return a list of block descriptions.
|
| 31 |
// This is used to provide a list of possible blocks to the administrator,
|
| 32 |
// end users will not see these descriptions.
|
| 33 |
$blocks[0]['info'] = t('General node Recomendation Engine block');
|
| 34 |
$blocks[1]['info']= t('Similarly Rated Nodes');
|
| 35 |
return $blocks;
|
| 36 |
case 'configure':
|
| 37 |
// If $op is "configure", we need to provide the administrator with a
|
| 38 |
// configuration form. The $delta parameter tells us which block is being
|
| 39 |
// configured. In this example, we'll allow the administrator to customize
|
| 40 |
// the text of the first block.
|
| 41 |
$form = array();
|
| 42 |
if ($delta == 0) {
|
| 43 |
// All we need to provide is a text field, Drupal will take care of
|
| 44 |
// the other block configuration options and the save button.
|
| 45 |
$form['node_recommedation_general_title'] = array(
|
| 46 |
'#type' => 'textfield',
|
| 47 |
'#title' => t('Title'),
|
| 48 |
'#size' => 20,
|
| 49 |
'#description' => t('This string will the title of the General Recommendation Block'),
|
| 50 |
'#default_value' =>
|
| 51 |
variable_get('node_recommedation_general_title', t('Recommended Nodes')),
|
| 52 |
);
|
| 53 |
|
| 54 |
// now add the value for n
|
| 55 |
$form['n_value'] = array(
|
| 56 |
'#type' => 'textfield',
|
| 57 |
'#title' => t('Maximum number of nodes to recommend'),
|
| 58 |
'#size' => 2,
|
| 59 |
'#description' => t('This sets the max number of recommended nodes presented within the general block'),
|
| 60 |
'#default_value' => 2,
|
| 61 |
);
|
| 62 |
}
|
| 63 |
else if ($delta == 1)
|
| 64 |
{
|
| 65 |
$form['node_recommedation_similar_title'] = array(
|
| 66 |
'#type' => 'textfield',
|
| 67 |
'#title' => t('Title for the similar rated block'),
|
| 68 |
'#size' => 20,
|
| 69 |
'#description' => t('This sets the title for this block'),
|
| 70 |
'#default_value' => variable_get('node_recommedation_similar_title', t('Similarly Rated Nodes')),
|
| 71 |
);
|
| 72 |
|
| 73 |
// n_value for this block
|
| 74 |
$form['node_recommedation_similar_n_value'] = array(
|
| 75 |
'#type' => 'textfield',
|
| 76 |
'#title' => t('Maximum number of nodes to recommend'),
|
| 77 |
'#size' => 2,
|
| 78 |
'#description' => t('This sets the max number of nodes to recommend'),
|
| 79 |
'#default_value' => variable_get('node_recommedation_similar_n_value', 4),
|
| 80 |
);
|
| 81 |
}
|
| 82 |
return $form;
|
| 83 |
case 'save':
|
| 84 |
// If $op is "save", we need to save settings from the configuration form.
|
| 85 |
// Since the first block is the only one that allows configuration, we
|
| 86 |
// need to check $delta to make sure we only save it.
|
| 87 |
if ($delta == 0) {
|
| 88 |
// Have Drupal save the string to the database.
|
| 89 |
variable_set('node_recommedation_general_title', $edit['node_recommedation_general_title']);
|
| 90 |
variable_set('cre_n_value', $edit['n_value']);
|
| 91 |
}
|
| 92 |
else if ($delta == 1)
|
| 93 |
{
|
| 94 |
variable_set('node_recommedation_similar_title', $edit['node_recommedation_similar_title']);
|
| 95 |
variable_set('node_recommedation_similar_n_value', $edit['node_recommedation_similar_n_value']);
|
| 96 |
}
|
| 97 |
return;
|
| 98 |
case 'view': default:
|
| 99 |
// If $op is "view", then we need to generate the block for display
|
| 100 |
// purposes. The $delta parameter tells us which block is being requested.
|
| 101 |
switch ($delta) {
|
| 102 |
case 0:
|
| 103 |
//Personalized recommendations
|
| 104 |
$n=variable_get('cre_n_value',1);
|
| 105 |
$block['subject'] = t(variable_get('node_recommedation_general_title', t('Recommended Nodes')));
|
| 106 |
$block['content'] = node_recommedation_get_top_n($uid, $n);
|
| 107 |
break;
|
| 108 |
case 1: // call for the general similar block
|
| 109 |
$n = variable_get('node_recommedation_similar_n_value', 3);
|
| 110 |
$block['subject'] = t(variable_get('node_recommedation_similar_title', t('Those that like this also liked')));
|
| 111 |
$block['content'] = t(node_recommedation_get_similar($n));
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
return $block;
|
| 115 |
}
|
| 116 |
}
|
| 117 |
|
| 118 |
/*
|
| 119 |
* implementation of node's hook_nodeapi()
|
| 120 |
*/
|
| 121 |
|
| 122 |
function node_recommedation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
|
| 123 |
{
|
| 124 |
if($op == 'delete') {
|
| 125 |
// remove all references in cre_avg_node_diff table where both $nid1 == &$node->nid and $nid2 == &$node->nid
|
| 126 |
db_query("DELETE FROM {cre_similarity_matrix} WHERE nid1=%d OR nid2=%d",$node->nid,$node->nid);
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
/*
|
| 131 |
* Formats and prints the top n nodes
|
| 132 |
*
|
| 133 |
* @param $uid
|
| 134 |
* uid for the personalized recommendations
|
| 135 |
*
|
| 136 |
* @param $n
|
| 137 |
* specifies the number of recommendations to return
|
| 138 |
*/
|
| 139 |
|
| 140 |
function node_recommedation_get_top_n($uid,$n) {
|
| 141 |
|
| 142 |
// do something special if user not logged in????
|
| 143 |
if($uid == 0) {
|
| 144 |
return;
|
| 145 |
}
|
| 146 |
|
| 147 |
//fetch nids
|
| 148 |
$recommedations = cre_top($uid, $n, 'node','vote');
|
| 149 |
|
| 150 |
if ($recommedations == NULL) {
|
| 151 |
return;
|
| 152 |
}
|
| 153 |
|
| 154 |
foreach($recommedations as $nodeobj) {
|
| 155 |
// format and return the html
|
| 156 |
$node = db_fetch_object(db_query("SELECT title, nid FROM {node} WHERE nid=%d",$nodeobj->content_id));
|
| 157 |
$return_value.= l($node->title,"node/".$node->nid) /*." score = ".$nodeobj->score*/ ."<br>";
|
| 158 |
}
|
| 159 |
|
| 160 |
return $return_value;
|
| 161 |
}
|
| 162 |
|
| 163 |
/*
|
| 164 |
* formats the nodes that were rated similar according to the currently
|
| 165 |
* viewed node
|
| 166 |
*
|
| 167 |
* @param $n
|
| 168 |
* Specifies the number of recommendatios to print
|
| 169 |
*/
|
| 170 |
function node_recommedation_get_similar($n) {
|
| 171 |
if (arg(0) == 'node') {
|
| 172 |
$nid = arg(1);
|
| 173 |
if($nid <= 0) {
|
| 174 |
// May want to do something here if it's not a node
|
| 175 |
// highest rated nodes?
|
| 176 |
return;
|
| 177 |
}
|
| 178 |
}
|
| 179 |
else {
|
| 180 |
return;
|
| 181 |
}
|
| 182 |
|
| 183 |
// now get the similar nodes
|
| 184 |
$related_nodes = cre_similar($n,$nid,'node','vote');
|
| 185 |
|
| 186 |
if ($related_nodes == NULL) {
|
| 187 |
// no one has rated this node yet
|
| 188 |
// therefore, there are no 'similars'
|
| 189 |
// optionally display some msg (Be the first to rate this node something like that )
|
| 190 |
return;
|
| 191 |
}
|
| 192 |
|
| 193 |
foreach($related_nodes as $recommended) {
|
| 194 |
// format the output html
|
| 195 |
$node = db_fetch_object(db_query("SELECT title, nid FROM {node} WHERE nid=%d",$recommended->content_id));
|
| 196 |
$return_value .= l($node->title, "node/".$node->nid) . "<br>";
|
| 197 |
}
|
| 198 |
|
| 199 |
return $return_value;
|
| 200 |
}
|