/[drupal]/contributions/sandbox/scottreynolds/modules/node_recommendation.module
ViewVC logotype

Contents of /contributions/sandbox/scottreynolds/modules/node_recommendation.module

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


Revision 1.1 - (show annotations) (download) (as text)
Tue Jul 11 23:22:10 2006 UTC (3 years, 4 months ago) by scottreynolds
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
cre speed improvements. Added page callback to node_recommendation.
Intial work on views addition
1 <?php
2 // $Id:$
3
4 if (module_exist('views')) {
5 include_once('node_recommendation_views.inc');
6 //drupal_set_message(t('really good'));
7 }
8
9 function node_recommendation_help($section) {
10 switch ($section) {
11 case 'admin/modules#description':
12 return t("Provides blocks and an api to display Recommended Nodes");
13 case 'admin/settings/node_recommendation':
14 return t("These settings change the behaviors of the page view for recommendations");
15 case 'recommendations':
16 // Here is some help text for a custom page.
17 return t(variable_get('node_recommendation_page_help_txt', 'These are your personal recommendations'));
18 }
19 }
20
21 function node_recommendation_settings() {
22 $form['node_recommendation_page_title'] = array(
23 '#type' => 'textfield',
24 '#title' => t('Recommeded Node page title'),
25 '#description' => t('This sets the title displayed at the top of the recommendations page'),
26 '#default_value' => variable_get('node_recommendation_page_title', 'Your recommendations'),
27 );
28 $form['node_recommendation_path_to_page'] = array(
29 '#type' => 'textfield',
30 '#title' => t('URL path to Recommendation Page'),
31 '#description' => t('The path to the page itself. EXAMPLE: <i>recommendations</i> would result in a url ?q=recommendations or /recommendations for clean urls'),
32 '#default_value' => variable_get('node_recommendation_path_to_page', 'recommendations'),
33 );
34 $form['node_recommendation_page_n'] = array(
35 '#type' => 'textfield',
36 '#title' => t('N-Value for page display'),
37 '#description' => t('Max number of recommendations to display on the page'),
38 '#size' => 3,
39 '#default_value' => variable_get('node_recommendation_page_n', 20),
40 );
41 $form['node_recommendation_page_help_txt'] = array(
42 '#type' => 'textfield',
43 '#title' => t('Help Text'),
44 '#description' => t('Help text displayed below the title. Set to " " if you dont want any text'),
45 '#default_value' => variable_get('node_recommendation_page_help_txt', 'These are your personal recommendations'),
46 );
47
48 return $form;
49
50 }
51
52 /*
53 * implementation of hook_block()
54 */
55
56 function node_recommendation_block($op = 'list', $delta = 0, $edit = array()) {
57 // get the user name and id
58 global $user;
59 $uid = $user->uid;
60 $name = $user->name;
61
62 // The $op parameter determines what piece of information is being requested.
63 switch ($op) {
64 case 'list':
65 // If $op is "list", we just need to return a list of block descriptions.
66 // This is used to provide a list of possible blocks to the administrator,
67 // end users will not see these descriptions.
68 $blocks[0]['info'] = t('General node Recomendation Engine block');
69 $blocks[1]['info']= t('Similarly Rated Nodes');
70 return $blocks;
71 case 'configure':
72 // If $op is "configure", we need to provide the administrator with a
73 // configuration form. The $delta parameter tells us which block is being
74 // configured. In this example, we'll allow the administrator to customize
75 // the text of the first block.
76 $form = array();
77 if ($delta == 0) {
78 // All we need to provide is a text field, Drupal will take care of
79 // the other block configuration options and the save button.
80 $form['node_recommendation_general_title'] = array(
81 '#type' => 'textfield',
82 '#title' => t('Title'),
83 '#size' => 20,
84 '#description' => t('This string will the title of the General Recommendation Block'),
85 '#default_value' =>
86 variable_get('node_recommendation_general_title', t('Recommended Nodes')),
87 );
88
89 // now add the value for n
90 $form['n_value'] = array(
91 '#type' => 'textfield',
92 '#title' => t('Maximum number of nodes to recommend'),
93 '#size' => 2,
94 '#description' => t('This sets the max number of recommended nodes presented within the general block'),
95 '#default_value' => variable_get('cre_n_value', 2)
96 );
97 }
98 else if ($delta == 1)
99 {
100 $form['node_recommendation_similar_title'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Title for the similar rated block'),
103 '#size' => 20,
104 '#description' => t('This sets the title for this block'),
105 '#default_value' => variable_get('node_recommendation_similar_title', t('Similarly Rated Nodes')),
106 );
107
108 // n_value for this block
109 $form['node_recommendation_similar_n_value'] = array(
110 '#type' => 'textfield',
111 '#title' => t('Maximum number of nodes to recommend'),
112 '#size' => 2,
113 '#description' => t('This sets the max number of nodes to recommend'),
114 '#default_value' => variable_get('node_recommendation_similar_n_value', 4),
115 );
116 }
117 return $form;
118 case 'save':
119 // If $op is "save", we need to save settings from the configuration form.
120 // Since the first block is the only one that allows configuration, we
121 // need to check $delta to make sure we only save it.
122 if ($delta == 0) {
123 // Have Drupal save the string to the database.
124 variable_set('node_recommendation_general_title', $edit['node_recommendation_general_title']);
125 variable_set('cre_n_value', $edit['n_value']);
126 }
127 else if ($delta == 1)
128 {
129 variable_set('node_recommendation_similar_title', $edit['node_recommendation_similar_title']);
130 variable_set('node_recommendation_similar_n_value', $edit['node_recommendation_similar_n_value']);
131 }
132 return;
133 case 'view': default:
134 // If $op is "view", then we need to generate the block for display
135 // purposes. The $delta parameter tells us which block is being requested.
136 switch ($delta) {
137 case 0:
138 //Personalized recommendations
139 $n=variable_get('cre_n_value',2);
140 $content = node_recommendation_get_top_n($uid, $n);
141 if ($content == NULL) {
142 return;
143 }
144 $block['subject'] = t(variable_get('node_recommendation_general_title', t('Recommended Nodes')));
145 $block['content'] = theme('item_list', $content );
146 break;
147 case 1: // call for the general similar block
148 $n = variable_get('node_recommendation_similar_n_value', 3);
149 $content = node_recommendation_get_similar($n);
150 if ($content == NULL) {
151 return;
152 }
153 $block['subject'] = t(variable_get('node_recommendation_similar_title', t('Those that like this also liked')));
154 $block['content'] = theme('item_list', $content);
155
156 break;
157 }
158 return $block;
159 }
160 }
161
162 /*
163 * implementation of node's hook_nodeapi()
164 */
165
166 function node_recommendation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
167 {
168 if($op == 'delete') {
169 // remove all references in cre_similarity_matrix table where both $nid1 == &$node->nid and $nid2 == &$node->nid
170 db_query("DELETE FROM {cre_similarity_matrix} WHERE nid1=%d OR nid2=%d",$node->nid,$node->nid);
171 }
172 }
173
174 /*
175 * Formats and prints the top n nodes
176 *
177 * @param $uid
178 * uid for the personalized recommendations
179 *
180 * @param $n
181 * specifies the number of recommendations to return
182 */
183
184 function node_recommendation_get_top_n($uid,$n) {
185
186 // do something special if user not logged in????
187 if($uid == 0) {
188 return;
189 }
190
191 //fetch nids
192 $recommendations = cre_top($uid, $n);
193
194 if ($recommendations == NULL) {
195 drupal_set_message('ERROR: no recommendations for user: $uid');
196 return;
197 }
198
199 foreach($recommendations as $nodeobj) {
200 // format and return the html
201 $node = db_fetch_object(db_query("SELECT title, nid FROM {node} WHERE nid=%d",$nodeobj->content_id));
202 $return_value[]= l($node->title,"node/".$node->nid) /*." score = ".$nodeobj->score*/ ."<br>";
203 }
204
205 return $return_value;
206 }
207
208 /*
209 * formats the nodes that were rated similar according to the currently
210 * viewed node
211 *
212 * @param $n
213 * Specifies the number of recommendatios to print
214 */
215 function node_recommendation_get_similar($n) {
216 if (arg(0) == 'node') {
217 $nid = arg(1);
218 if($nid <= 0) {
219 // May want to do something here if it's not a node
220 // highest rated nodes?
221 return;
222 }
223 }
224 else {
225 return;
226 }
227
228 // now get the similar nodes
229 $related_nodes = cre_similar($n,$nid,'node','vote');
230
231 if ($related_nodes == NULL) {
232 // no one has rated this node yet
233 // therefore, there are no 'similars'
234 // optionally display some msg (Be the first to rate this node something like that )
235 return;
236 }
237
238 foreach($related_nodes as $recommended) {
239 // format the output html
240 $node = db_fetch_object(db_query("SELECT title, nid FROM {node} WHERE nid=%d",$recommended->content_id));
241 $return_value[] = l($node->title, "node/".$node->nid);
242 }
243
244 return $return_value;
245 }
246
247
248 /**
249 * Implementation of hook_perm().
250 *
251 * Since the access to our new custom pages will be granted based on
252 * special permissions, we need to define what those permissions are here.
253 * This ensures that they are available to enable on the user role
254 * administration pages.
255 */
256 function node_recommendation_perm() {
257 return array('access recommendations');
258 }
259
260 /**
261 * Implementation of hook_menu().
262 *
263 * You must implement hook_menu() to emit items to place in the main menu.
264 * This is a required step for modules wishing to display their own pages,
265 * because the process of creating the links also tells Drupal what
266 * callback function to use for a given URL. The menu items returned
267 * here provide this information to the menu system.
268 *
269 */
270 function node_recommendation_menu($may_cache) {
271 $items = array();
272
273 // The $may_cache parameter is used to divide menu items into two parts. Those
274 // returned when $may_cache is true must be consistently applicable for the
275 // current user at all times; the others may change or be defined at only
276 // certain paths. Most modules will have excusively cacheable menu items.
277 if ($may_cache) {
278 // This is the minimum information you can provide for a menu item.
279 $items[] = array('path' => variable_get('node_recommendation_path_to_page', 'recommendations'),
280 'title' => t(variable_get('node_recommendation_page_title', 'Your recommendations')), // make a setting!
281 'callback' => 'node_recommendation_recommendations',
282 'access' => user_access('access recommendations'));
283 }
284
285 return $items;
286 }
287
288 /**
289 * A simple page callback.
290 *
291 *
292 */
293 function node_recommendation_recommendations() {
294 global $user;
295 $n = variable_get('node_recommendation_page_n', 20);
296 if ($user->uid == 0) {
297 // return top rated nodes
298 drupal_set_message(t('good'));
299 $top_nodes = _node_recommendation_get_top_rate_nodes();
300 $count = 0;
301 foreach($top_nodes as $node) {
302 if ($count >= $n) {
303 break;
304 }
305 $content[] = l($node->title, 'node/'.$node->nid);
306 $count++;
307 }
308 }
309 else {
310 drupal_set_message(t('generating recommendations'));
311 $content = node_recommendation_get_top_n($user->uid, $n);
312 }
313 drupal_set_message(t('???'));
314 return theme('item_list', $content);
315 }
316
317 /*
318 * gets the highest rated items
319 */
320 function _node_recommendation_get_top_rate_nodes() {
321 $sql = "SELECT c.content_id as nid, c.value as avg_vote, n.title
322 FROM {votingapi_cache} c, {node} n
323 WHERE c.content_id = n.nid AND c.function = 'average'
324 ORDER BY avg_vote";
325 $result = db_query($sql);
326 while ($cached = db_fetch_object($result)) {
327 $voting_results[] = $cached;
328 }
329 return $voting_results;
330 }

  ViewVC Help
Powered by ViewVC 1.1.2