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

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

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


Revision 1.4 - (show annotations) (download) (as text)
Tue Jun 13 04:39:20 2006 UTC (3 years, 5 months ago) by scottreynolds
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +3 -2 lines
File MIME type: text/x-php
Fixed Id error
1 <?php
2 // $Id:$
3 function creTest_help($section) {
4 switch ($section) {
5 case 'admin/modules#description':
6 return t("Provides blocks and an api to display Recommended Nodes");
7 case 'admin/settings/votingapi':
8 return t("Provides several different ways to display Recommended Nodes to users");
9 }
10 }
11
12 function creTest_build($userID,$itemID)
13 {
14 // switch globals on database type!!!
15 // Run my magik! put the values in dev table
16
17 $db_result = db_query("SELECT DISTINCT r.content_id, r2.value-r.value as rating_difference
18 FROM {votingapi_vote r}, {votingapi_vote r2}
19 WHERE r.uid=%d AND r2.content_id=%d AND r2.uid=r.uid", $userID, $itemID);
20 if(!$db_result)
21 {
22 drupal_set_message(t('ERROR $userID, $itemID'));
23 }
24 else
25 {
26 while($row = db_fetch_array($db_result))
27 {
28 $other_node=$row["content_id"];
29 $rating_difference=$row["rating_difference"];
30
31 // check to see if the pair of content ($itemID and $other_node) are already in the dev table
32 if(db_num_rows(db_query("SELECT nid1 from {dev} WHERE nid1=%d AND nid2=%d",$itemID,$other_node)) > 0)
33 {
34 //update the two rows
35 //drupal_set_message(t('updated!'));
36 db_query("UPDATE {dev} SET count=count+1, sum=sum+%d
37 WHERE nid1=%d AND nid2=%d", $rating_difference, $itemID, $other_node);
38 // update the second row ONLY if the two nodes are different
39 if($itemID != $other_node)
40 {
41 db_query("UPDATE {dev} SET count=count+1, sum=sum-%d
42 WHERE nid1=%d AND nid2=%d", $rating_difference, $other_node, $itemID);
43 }
44 }
45 // if this is a new 'pairing' creTestate two rows
46 else
47 {
48 // insert 2 rows only if the nodes are different
49 $insertSQL = "INSERT INTO {dev} VALUES ($itemID, $other_node, 1, $rating_difference)";
50 db_query("INSERT INTO {dev} VALUES (%d,%d, 1, %d)",$itemID,$other_node,$rating_difference);
51
52 if($itemID != $other_node)
53 {
54 $sql="INSERT INTO dev VALUES($other_node, $itemID,1,-$rating_difference)";
55 db_query("INSERT INTO dev VALUES(%d, %d,1,%d)",$other_node,$itemID,-$rating_difference);
56 }
57 }
58 }
59 }
60 }
61
62 function creTest_top_n($uid, $n, $type = NULL, $cat = NULL)
63 {
64
65 // do something special if user not logged in????
66 if($uid == 0)
67 {
68 return;
69 }
70
71 if(isset($type))
72 {
73 //drupal_set_message(t('Type is set to ' . $type));
74 // instead use type to specify vote type? BUT THEN CANT REFERENCE {node}!!!
75 $db_result = db_query("SELECT d.nid1 as 'node', sum(d.sum+d.count*r.value)/sum(d.count) as 'score', n.title
76 FROM {votingapi_vote r}, {dev d}, {node n}
77 WHERE r.uid=%d AND d.nid1<>r.content_id AND d.nid2=r.content_id AND n.nid=d.nid1 AND n.type='%s'
78 GROUP BY d.nid1 ORDER BY score DESC",$uid,$type);
79
80 }
81 else
82 {
83 $db_result = db_query("SELECT d.nid1 as 'node', sum(d.sum+d.count*r.value)/sum(d.count) as 'score', n.title
84 FROM {votingapi_vote r}, {dev d}, {node n}
85 WHERE r.uid=%d AND d.nid1<>r.content_id AND d.nid2=r.content_id AND n.nid=d.nid1
86 GROUP BY d.nid1 ORDER BY score DESC",$uid);
87 }
88
89
90 $basicSQL = db_query("SELECT sum(value) as 'sum', sum(uid / %d) as 'count'
91 FROM {votingapi_vote}
92 WHERE uid=%d", $uid,$uid);
93 $sqlObj = db_fetch_object($basicSQL);
94 $avg = $sqlObj->sum / $sqlObj->count;
95
96
97 $count = 0;
98 $nodeObj = db_fetch_object($db_result);
99 while($nodeObj != NULL)
100 {
101 if($count >= $n)
102 {
103 break;
104 }
105 // do a check to see if the node was rated by the uid
106 // replace with votingapi_get_user_votes($content_type,$content_id,$uid)
107 $nid = $nodeObj->node;
108 $user_check_result = db_query("SELECT uid, content_id
109 FROM {votingapi_vote}
110 WHERE uid = %d AND content_id = %d", $uid,$nid);
111 if(db_num_rows($user_check_result) == 0)
112 {
113 $return_value.= l($nodeObj->title,"node/".$nodeObj->node) /*." score = ".$nodeObj->score*/ ."<br>";
114 $count++;
115 }
116 $nodeObj = db_fetch_object($db_result);
117 }
118 if(isset($return_value))
119 {
120 return $return_value . "avg = $avg";
121 }
122 else
123 {
124 // do something special when user has rated all nodes
125 // return top rated nodes?
126 //return "No nodes";
127 }
128 }
129
130 function creTest_similar($n)
131 {
132 $nid = arg(1);
133 if($nid <= 0)
134 {
135 // May want to do something here if it's not a node
136 // highest rated nodes?
137 return;
138 }
139
140 $sql_result = db_query("SELECT d.nid2 as 'node', (d.sum / d.count) AS 'average', n.title
141 FROM {dev d}, {node n}
142 WHERE nid1=%d AND n.nid=d.nid2 AND n.nid <> %d
143 ORDER BY (sum/count) DESC LIMIT %d",$nid,$nid,$n);
144 while($node = db_fetch_object($sql_result))
145 {
146 $return_value .= l($node->title, "node/".$node->node) . "<br>";
147 }
148 return $return_value;
149
150 }
151
152 function creTest_votingapi_insert(&$vote)
153 {
154 // run my magik!
155 // $vote obj has all the fields for a vote record! extract uid, nid
156 creTest_build($vote->uid,$vote->content_id);
157 //drupal_set_message(t('function Called'));
158 return;
159 }
160
161 function creTest_votingapi_update(&$vote)
162 {
163 // when a vote is updated need to just adjust sum NOT COUNT!!
164 // for all records in dev where nid1 or nid2 equals vote->content_id
165 // and the adjustment to sum MUST BE THE RATING_DIFFERENCE
166 }
167
168 function creTest_votingapi_delete(&$vote)
169 {
170 // TODO: modifiy the dev table by subratcting from count
171 // and subtracting vote value from sum
172 // OVER ALL REFERENCES TO THE CONTENT_ID
173 // THINK !!! reverse of build!!
174 }
175
176 function creTest_block($op = 'list', $delta = 0, $edit = array()) {
177 // LOCAL VARIABLES!! FIX THIS TO THE ACTUAL USER AND n
178 global $user;
179 $uid = $user->uid;
180 $name = $user->name;
181
182 // The $op parameter determines what piece of information is being requested.
183 switch ($op) {
184 case 'list':
185 // If $op is "list", we just need to return a list of block descriptions.
186 // This is used to provide a list of possible blocks to the administrator,
187 // end users will not see these descriptions.
188 $blocks[0]['info'] = t('General Content Recomendation Engine block');
189 $blocks[1]['info'] = t('Recommended nodes of a given type');
190 $blocks[2]['info']= t('Similarly Rated Nodes');
191 return $blocks;
192 case 'configure':
193 // If $op is "configure", we need to provide the administrator with a
194 // configuration form. The $delta parameter tells us which block is being
195 // configured. In this example, we'll allow the administrator to customize
196 // the text of the first block.
197 $form = array();
198 if ($delta == 0) {
199 // All we need to provide is a text field, Drupal will take care of
200 // the other block configuration options and the save button.
201 $form['creTest_general_title'] = array(
202 '#type' => 'textfield',
203 '#title' => t('Title'),
204 '#size' => 20,
205 '#description' => t('This string will the title of the General Recommendation Block'),
206 '#default_value' =>
207 variable_get('creTest_general_title', t('Recommended Nodes')),
208 );
209
210 // now add the value for n
211 $form['n_value'] = array(
212 '#type' => 'textfield',
213 '#title' => t('Maximum number of nodes to recommend'),
214 '#size' => 2,
215 '#description' => t('This sets the max number of recommended nodes presented within the general block'),
216 '#default_value' => 2,
217 );
218 }
219 else if ($delta == 1)
220 {
221 $form['creTest_type_title'] = array(
222 '#type' => 'textfield',
223 '#title' => t('Title of the block'),
224 '#size' => 20,
225 '#description' => t('This is the title of the type block. Type applies to node types...not vocab'),
226 '#default_value' => variable_get('creTest_type_title', 'Other Recomendations'),
227 );
228
229 // this is the type of node
230 $form['creTest_type_type'] = array(
231 '#type' => 'textfield',
232 '#title' => t('Node type to recommend'),
233 '#size' => 20,
234 '#description' => t('This is the type of nodes to recommend within this block'),
235 '#default_value' => variable_get('creTest_type_type','story'),
236 );
237
238 $form['creTest_type_n_value'] = array(
239 '#type' => 'textfield',
240 '#title' => t('Maximum number of nodes to recommend'),
241 '#size' => 2,
242 '#description' => t('This sets the max number of recommendations within this node category'),
243 '#default_value' => variable_get('creTest_type_n_value', 3),
244 );
245 }
246 else if ($delta == 2)
247 {
248 $form['creTest_similar_title'] = array(
249 '#type' => 'textfield',
250 '#title' => t('Title for the similar rated block'),
251 '#size' => 20,
252 '#description' => t('This sets the title for this block'),
253 '#default_value' => variable_get('creTest_similar_title', t('Similarly Rated Nodes')),
254 );
255
256 // n_value for this block
257 $form['creTest_similar_n_value'] = array(
258 '#type' => 'textfield',
259 '#title' => t('Maximum number of nodes to recommend'),
260 '#size' => 2,
261 '#description' => t('This sets the max number of nodes to recommend'),
262 '#default_value' => variable_get('creTest_similar_n_value', 4),
263 );
264 }
265 return $form;
266 case 'save':
267 // If $op is "save", we need to save settings from the configuration form.
268 // Since the first block is the only one that allows configuration, we
269 // need to check $delta to make sure we only save it.
270 if ($delta == 0) {
271 // Have Drupal save the string to the database.
272 variable_set('creTest_general_title', $edit['creTest_general_title']);
273 variable_set('creTest_n_value', $edit['n_value']);
274 }
275 else if ($delta == 1)
276 {
277 variable_set('creTest_type_title', $edit['creTest_type_title']);
278 variable_set('creTest_type_n_value', $edit['creTest_type_n_value']);
279 variable_set('creTest_type_type', $edit['creTest_type_type']);
280 }
281 else if ($delta == 2)
282 {
283 variable_set('creTest_similar_title', $edit['creTest_similar_title']);
284 variable_set('creTest_similar_n_value', $edit['creTest_similar_n_value']);
285 }
286 return;
287 case 'view': default:
288 // If $op is "view", then we need to generate the block for display
289 // purposes. The $delta parameter tells us which block is being requested.
290 switch ($delta) {
291 case 0:
292 //Personalized recommendations
293 $n=variable_get('creTest_n_value',1);
294 $block['subject'] = t(variable_get('creTest_general_title', t('Recommended Nodes')));
295 $block['content'] = creTest_top_n($uid, $n);
296 break;
297 case 1: // call for the block that displays a specfic type
298 $n=variable_get('creTest_type_n_value',2);
299 $block['subject'] = t(variable_get('creTest_type_title', t('Other Recomendations')));
300 $block['content'] = t(creTest_top_n($uid,$n, variable_get('creTest_type_type','page')));
301 break;
302 case 2: // call for the general similar block
303 $n = variable_get('creTest_similar_n_value', 3);
304 $block['subject'] = t(variable_get('creTest_similar_title', t('Those that like this also liked')));
305 $block['content'] = t(creTest_similar($n));
306 break;
307 }
308 return $block;
309 }
310 }
311
312 function creTest_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
313 {
314 // do I need this??
315 if($op == 'delete')
316 {
317 // remove all references in dev table where both $nid1 == &$node->nid and $nid2 == &$node->nid
318 db_query("DELETE FROM {dev} WHERE nid1=%d OR nid2=%d",$node->nid,$node->nid);
319
320 }
321 }

  ViewVC Help
Powered by ViewVC 1.1.2