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

Diff of /contributions/modules/interests/interests.module

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

revision 1.4, Mon Jun 11 23:45:42 2007 UTC revision 1.5, Sat Nov 3 04:10:36 2007 UTC
# Line 14  function interests_menu($may_cache) { Line 14  function interests_menu($may_cache) {
14        'callback' => 'interests_browse',        'callback' => 'interests_browse',
15        'access' => user_access('access interests'),        'access' => user_access('access interests'),
16        'type' => MENU_NORMAL_ITEM);        'type' => MENU_NORMAL_ITEM);
17              $items[] = array('path' => 'interests/add',
18          'title' => t('Add Interest'),
19          'callback' => 'interests_add_node',
20          'access' => user_access('access interests'),
21          'type' => MENU_CALLBACK);
22          $items[] = array('path' => 'interests/edit',          $items[] = array('path' => 'interests/edit',
23                          'title' => t('Change My Interests'),                          'title' => t('Change My Interests'),
24                          'callback' => 'drupal_get_form',                          'callback' => 'drupal_get_form',
# Line 26  function interests_menu($may_cache) { Line 31  function interests_menu($may_cache) {
31        'access' => user_access('admin interests'),        'access' => user_access('admin interests'),
32        'callback' => 'interests_user_overview',        'callback' => 'interests_user_overview',
33                          'callback arguments' => array(arg(1)));                          'callback arguments' => array(arg(1)));
34              $items[] = array('path' => 'admin/content/interests',
35          'title' => t('Interests'),
36          'description' => t('Set which vocabularies can be interests.'),
37          'access' => user_access('admin interests'),
38          'callback' => 'drupal_get_form',
39                            'callback arguments' => 'interests_vocabs_form',
40                            );
41      $items[] = array('path' => 'admin/user/interests/add',      $items[] = array('path' => 'admin/user/interests/add',
42        'title' => t('Add Interests to user'),        'title' => t('Add Interests to user'),
43        'callback' => 'drupal_get_form',        'callback' => 'drupal_get_form',
44        'callback arguments' => array('interests_interests_form'),        'callback arguments' => array('interests_interests_form'),
45        'access' => user_access('admin interests'),        'access' => user_access('admin interests'),
46        'type' => MENU_CALLBACK);        'type' => MENU_CALLBACK,
47                            );
48      $items[] = array('path' => 'admin/user/interests/edit',      $items[] = array('path' => 'admin/user/interests/edit',
49        'title' => t('Edit Interests'),        'title' => t('Edit Interests'),
50        'callback' => 'drupal_get_form',        'callback' => 'drupal_get_form',
# Line 44  function interests_menu($may_cache) { Line 57  function interests_menu($may_cache) {
57        'callback arguments' => array('interests_interests_delete'),        'callback arguments' => array('interests_interests_delete'),
58        'access' => user_access('admin interests'),        'access' => user_access('admin interests'),
59        'type' => MENU_CALLBACK);        'type' => MENU_CALLBACK);
 /*    $items[] = array('path' => 'profile/autocomplete', 'title' => t('Profile autocomplete'),  
       'callback' => 'profile_autocomplete',  
       'access' => 1,  
       'type' => MENU_CALLBACK);*/  
60    }    }
61          elseif(!$may_cache){          elseif (!$may_cache){
62            if(isset($user->uid) && arg(0) == 'node' && is_numeric(arg(1))){            if (isset($user->uid) && arg(0) == 'node' && is_numeric(arg(1))){
63                          $terms = taxonomy_node_get_terms(arg(1));                          $terms = taxonomy_node_get_terms(arg(1));
64                          if($terms){                          if ($terms){
65                                  foreach($terms as $term){                                  foreach ($terms as $term){
66                                          _interests_dynamic_add($term->tid);                                          _interests_dynamic_add($term->tid);
67                                  }                                  }
68                          }                          }
# Line 62  function interests_menu($may_cache) { Line 71  function interests_menu($may_cache) {
71    
72    return $items;    return $items;
73  }  }
74    
75  /**  /**
76   * Implementation of hook_perm().   * Implementation of hook_perm().
77   */   */
# Line 69  function interests_perm() { Line 79  function interests_perm() {
79          return array('access interests', 'edit interests', 'administer interests');          return array('access interests', 'edit interests', 'administer interests');
80  }  }
81    
82  function interests_interests_form_submit($form_id, $form_values){  function interests_link($type, $node = NULL, $teaser = FALSE){
83            $links = array();
84    
85          $currentResult = db_query("SELECT * FROM {interests} WHERE uid = '%s'", $form_values['uid']);          if ($type == 'node'){
86          while($currentRow = db_fetch_array($currentResult)){                  if (!$teaser){
87                  $currentList[] = array('tid' => $currentRow['tid'], 'uid' => $currentRow['uid']);                          if (user_access('access interests')){
88          }                                  $links['interests_add_interests'] = array(
89                                            'title' => t('set as interest'),
90          if($currentList){                                          'href' => 'interests/add/'.$node->nid,
91                  foreach($currentList as $i => $currentTerm){                                  );
                         if(array_key_exists($currentTerm['tid'], $form_values['interest_terms'])){  
                                 unset($form_values['interest_terms'][$currentTerm['tid']]);  
                                 unset($currentList[$i]);  
92                          }                          }
93                  }                  }
94          }          }
95          foreach($form_values['interest_terms'] as $tid){          return $links;
                 $insertResult = db_query("INSERT INTO {interests} values ('%s', '%s', '1')", $form_values['uid'], $tid);  
         }  
         if($currentList){  
                 foreach($currentList AS $current){  
                         $deleteResult = db_query("DELETE FROM {interests} WHERE uid ='%s' AND tid='%s'", $form_values['uid'], $current['tid']);  
                 }  
         }  
   
96  }  }
97    
98  function interests_interests_form($arg = NULL) {  function interests_interests_form($arg = NULL) {
# Line 100  function interests_interests_form($arg = Line 100  function interests_interests_form($arg =
100    
101          $output = l(t('Back to user list'), 'admin/user/interests');          $output = l(t('Back to user list'), 'admin/user/interests');
102    
103          if(is_numeric(arg(4))){          if (is_numeric(arg(4))){
104                  $default_values = _interests_tid_user_list(arg(4));                  $default_values = _interests_tid_user_list(arg(4));
105                  $uid = arg(4);                  $uid = arg(4);
106                  $result = db_query("SELECT uid, name from {users} WHERE uid = '%s'", arg(4));                  $result = db_query("SELECT uid, name from {users} WHERE uid = '%s'", arg(4));
107                  while($row = db_fetch_object($result)){                  while ($row = db_fetch_object($result)){
108                          $uname = $row->name;                          $uname = $row->name;
109                  }                  }
110          }          }
# Line 114  function interests_interests_form($arg = Line 114  function interests_interests_form($arg =
114                  $uname = $user->name;                  $uname = $user->name;
115          }          }
116    
117          $vocabs = taxonomy_get_vocabularies();          $vocabs = _interests_get_vocabs();
118          foreach($vocabs AS $i => $voc){  
119                  $termlist[$voc->name] = _interests_tid_list(array('vocabulary' => $i));          if (!empty($vocabs)){
120                    foreach ($vocabs AS $i => $voc){
121                            $termlist[$voc->name] = _interests_tid_list($vocabs);
122                    }
123              $form['interest_terms'] = array(
124                            '#type' => 'select',
125                            '#title' => t('Interests for '.$uname),
126                            '#multiple' => TRUE,
127                            '#default_value' => $default_values,
128                            '#options' => $termlist
129              );
130                    $form['uid'] = array(
131                            '#type' => 'hidden',
132                            '#value' => $uid
133                    );
134                    $form['submit'] = array(
135                            '#type' => 'submit',
136                            '#value' => 'Change'
137                    );
138            }
139            else{
140                    $form['novocabs'] = array(
141                            '#value' => t('No vocabularies have been setup yet.'),
142                    );
143          }          }
   $form['interest_terms'] = array(  
                 '#type' => 'select',  
                 '#title' => t('Interests for '.$uname),  
                 '#multiple' => 'TRUE',  
                 '#default_value' => $default_values,  
                 '#options' => $termlist  
   );  
         $form['uid'] = array(  
                 '#type' => 'hidden',  
                 '#value' => $uid  
         );  
         $form['submit'] = array(  
                 '#type' => 'submit',  
                 '#value' => 'Change'  
         );  
144          return $form;          return $form;
145  }  }
146    
147    function interests_interests_form_submit($form_id, $form_values){
148            $currentResult = db_query("SELECT * FROM {interests} WHERE uid = '%s'", $form_values['uid']);
149            while ($currentRow = db_fetch_array($currentResult)){
150                    $currentList[] = array('tid' => $currentRow['tid'], 'uid' => $currentRow['uid']);
151            }
152    
153            if ($currentList){
154                    foreach ($currentList as $i => $currentTerm){
155                            if (array_key_exists($currentTerm['tid'], $form_values['interest_terms'])){
156                                    unset($form_values['interest_terms'][$currentTerm['tid']]);
157                                    unset($currentList[$i]);
158                            }
159                    }
160            }
161            foreach ($form_values['interest_terms'] as $tid){
162                    $insertResult = db_query("INSERT INTO {interests} values ('%s', '%s', '1')", $form_values['uid'], $tid);
163            }
164            if ($currentList){
165                    foreach ($currentList AS $current){
166                            $deleteResult = db_query("DELETE FROM {interests} WHERE uid ='%s' AND tid='%s'", $form_values['uid'], $current['tid']);
167                    }
168            }
169    
170            return 'interests';
171    }
172    
173  function interests_user_overview(){  function interests_user_overview(){
174    
175          $result = db_query('SELECT DISTINCT u.uid, u.name FROM {users} u WHERE u.uid != 0 ORDER BY u.name asc');          $result = db_query('SELECT DISTINCT u.uid, u.name FROM {users} u WHERE u.uid != 0 ORDER BY u.name asc');
176          while($row = db_fetch_object($result)){          while ($row = db_fetch_object($result)){
177                  $tableRow[] = array($row->name, l('Edit Interests', 'admin/user/interests/add/'.$row->uid));                  $tableRow[] = array($row->name, l('Edit Interests', 'admin/user/interests/add/'.$row->uid));
178          }          }
179          $output = theme('table', array('Username', 'Edit'), $tableRow);          $output = theme('table', array('Username', 'Edit'), $tableRow);
# Line 147  function interests_user_overview(){ Line 181  function interests_user_overview(){
181          return $output;          return $output;
182  }  }
183    
184    function _interests_get_vocabs(){
185            static $return = array();
186    
187            $result = db_query("SELECT * FROM {interests_vocabs}");
188            while ($row = db_fetch_object($result)){
189                    $return[$row->vid] = $row->vid;
190            }
191            return $return;
192    }
193    
194    function interests_vocabs_form(){
195    
196            $vocabs = taxonomy_get_vocabularies();
197            if (!empty($vocabs)){
198    
199                    foreach ($vocabs AS $vocab){
200                            $options[$vocab->vid] = $vocab->name;
201                    }
202                    $default_options = _interests_get_vocabs();
203    
204                    $form['vocabularies'] = array(
205                            '#type' => 'select',
206                            '#options' => $options,
207                            '#title' => t('Vocabularies'),
208                            '#default_value' => $default_options,
209                            '#multiple' => true,
210                    );
211                    $form['submit'] = array(
212                            '#type' => 'submit',
213                            '#value' => 'Submit',
214                    );
215            }
216            else{
217                    $form['novocabs'] = array(
218                            '#value' => t('There are no taxonomy vocabularies yet.'),
219                    );
220            }
221    
222            return $form;
223    }
224    
225    function interests_vocabs_form_submit($form_id, &$form_values){
226    
227            $currentVocabs = _interests_get_vocabs();
228    
229            foreach ($form_values['vocabularies'] AS $i => $vocab){
230                    if (!isset($currentVocabs[$i])){
231                            db_query("INSERT INTO {interests_vocabs} VALUES (%s)", $vocab);
232                            unset($currentVocabs[$i]);
233                    }
234                    else{
235                            unset($currentVocabs[$i]);
236                    }
237            }
238            if (!empty($currentVocabs)){
239                    foreach ($currentVocabs AS $x => $delVocab){
240                            db_query("DELETE FROM {interests_vocabs} WHERE vid = '%s'", $delVocab);
241                    }
242            }
243    }
244    
245    function interests_add_node(){
246            $nid = arg(2);
247    
248            $terms = taxonomy_node_get_terms($nid);
249      if ($terms){
250                    foreach ($terms as $term){
251                            _interests_dynamic_add($term->tid);
252                    }
253            }
254    
255            drupal_goto('node/'.$nid);
256    }
257    
258  function _interests_dynamic_add($tid){  function _interests_dynamic_add($tid){
259          global $user;          global $user;
260          $result = db_query("SELECT * FROM {interests} WHERE uid = '%s' AND tid = '%s'", $user->uid, $tid);          $result = db_query("SELECT * FROM {interests} WHERE uid = '%s' AND tid = '%s'", $user->uid, $tid);
261          if(db_num_rows($result) != 0){          if (db_num_rows($result) != 0){
262                  while($row = db_fetch_array($result)){                  while ($row = db_fetch_array($result)){
263                          $counter = $row['counter'] + 1;                          $counter = $row['counter'] + 1;
264                          db_query("UPDATE {interests} SET counter = '%s' WHERE uid = '%s' AND tid = '%s'", $counter, $user->uid, $tid);                          db_query("UPDATE {interests} SET counter = '%s' WHERE uid = '%s' AND tid = '%s'", $counter, $user->uid, $tid);
265                  }                  }
# Line 164  function _interests_dynamic_add($tid){ Line 272  function _interests_dynamic_add($tid){
272    
273  function _interests_tid_user_list($uid = NULL){  function _interests_tid_user_list($uid = NULL){
274          $result = db_query("SELECT tid FROM {interests} WHERE uid='%s'", $uid);          $result = db_query("SELECT tid FROM {interests} WHERE uid='%s'", $uid);
275    while($row = db_fetch_array($result)){    while ($row = db_fetch_array($result)){
276                  $rows[] = $row['tid'];                  $rows[] = $row['tid'];
277          }          }
278          return $rows;          return $rows;
279  }  }
280    
281  function _interests_tid_list($filterinfo) {  function _interests_tid_list($vocabularies) {
   $tids = array();  
   if ($filterinfo['vocabulary']) {  
     $where = "WHERE td.vid = $filterinfo[vocabulary]";  
   }  
   $result = db_query("SELECT DISTINCT(td.tid), td.name, td.weight, v.name as vocabname, v.weight FROM {term_data} td LEFT JOIN {vocabulary} v ON v.vid = td.vid $where ORDER BY v.weight, v.name, td.weight, td.name");  
   while ($obj = db_fetch_object($result)) {  
     if ($filterinfo['vocabulary']) {  
       $tids[$obj->tid] = "$obj->name";  
     }  
     else {  
       $tids[$obj->tid] = "$obj->vocabname: $obj->name";  
     }  
   }  
282    
283      $tids = array();
284            foreach ($vocabularies as $vocab){
285              $result = db_query("SELECT DISTINCT(td.tid), td.name, td.weight, v.name as vocabname, v.weight FROM {term_data} td LEFT JOIN {vocabulary} v ON v.vid = td.vid WHERE td.vid = '%s' ORDER BY v.weight, v.name, td.weight, td.name", $vocab);
286              while ($obj = db_fetch_object($result)) {
287                $tids[$obj->tid] = $obj->vocabname.": ".$obj->name;
288              }
289            }
290    return $tids;    return $tids;
291  }  }
292    
# Line 195  function interests_browse() { Line 297  function interests_browse() {
297    
298          //Do the selection of users where at least one tag matches, order by number of matches - Limited to 10          //Do the selection of users where at least one tag matches, order by number of matches - Limited to 10
299    
         $output .= "<h2>".t('Users with similar interests')."</h2>";  
300    
301          $myTags = db_query("SELECT i.tid FROM {interests} i WHERE i.uid = '%s'", $user->uid);          $myTags = db_query("SELECT i.tid FROM {interests} i WHERE i.uid = '%s'", $user->uid);
302          while($row = db_fetch_array($myTags)){          if (db_num_rows($myTags) >= 1){
303                  $like .= "i.tid = '".$row['tid']."'  OR ";                  while ($row = db_fetch_array($myTags)){
304          }                          $like .= "i.tid = '".$row['tid']."'  OR ";
         $like = rtrim($like, ' OR ');  
         if(isset($like)) {  
                 $resultUsers = db_query("SELECT i.uid, i.tid, u.name, t.name AS tname FROM {interests} i, {users} u, {term_data} t WHERE ($like) AND i.uid != '%s' AND i.uid != '0' AND u.uid = i.uid AND i.tid = t.tid GROUP BY i.uid ORDER BY i.counter desc LIMIT 10", $user->uid);  
                 while($row = db_fetch_array($resultUsers)){  
                         $tableRow[] = array('username' => $row['name'],  
                                                                                                 'blog' => l(t('View Blog'), 'blog/'.$row['uid']),  
                                                                                                 'profile' => l(t('View Profile'), 'user/'.$row['uid']));  
305                  }                  }
306                  $output .= theme('table', array('Username', 'Blog', 'Profile'), $tableRow);                  $like = rtrim($like, ' OR ');
307          }                  if (isset($like)) {
308          else{                          $resultUsers = db_query("SELECT i.uid, i.tid, u.name, t.name AS tname FROM {interests} i, {users} u, {term_data} t WHERE ($like) AND i.uid != '%s' AND i.uid != '0' AND u.uid = i.uid AND i.tid = t.tid GROUP BY i.uid ORDER BY i.counter desc LIMIT 10", $user->uid);
309                  $output .= "<p>".t("You haven't specified any interests yet.")."</p>n";                          if (db_num_rows($resultUsers) >=1){
310          }                      $output .= "<h2>".t('Users with similar interests')."</h2>";
311                                    while ($row = db_fetch_array($resultUsers)){
312          //Do the selection of nodes where tags match                                          $tableRow[] = array('username' => $row['name'],
313                                                                                                            'blog' => l(t('View Blog'), 'blog/'.$row['uid']),
314          $resultTypes = db_query("SELECT nt.type, nt.name FROM {node_type} nt");                                                                                                          'profile' => l(t('View Profile'), 'user/'.$row['uid']));
315          while($row = db_fetch_array($resultTypes)){                                  }
316                  $result = db_query("SELECT i.counter, t.tid, t.name, n.nid, n.title FROM {interests} i, {term_data} t, {term_node} tn, {node} n WHERE n.nid = tn.nid AND tn.tid = t.tid AND i.tid = t.tid AND i.uid = '%s' AND n.type = '%s' GROUP BY n.nid ORDER BY i.counter desc, n.changed desc LIMIT 5", $user->uid, $row['type']);                                  $output .= theme('table', array('Username', 'Blog', 'Profile'), $tableRow);
                 if(db_num_rows($result)){  
                         $sectionTitle = $row['name'];  
                         $output .= "<h2>$sectionTitle</h2>";  
                         $output .= "<ul>";  
   
                         while($rowNodes = db_fetch_array($result)){  
                                 $output .= "<li>".l($rowNodes['title'], 'node/'.$rowNodes['nid'])."</li>";  
317                          }                          }
318                    }
319                    else{
320                            $output .= "<p>".t("You haven't specified any interests yet.")."</p>n";
321                    }
322    
323                          $output .= "</ul>";                  //Do the selection of nodes where tags match
324                    $resultTypes = db_query("SELECT nt.type, nt.name FROM {node_type} nt");
325                    while ($row = db_fetch_array($resultTypes)){
326                            $result = db_query("SELECT i.counter, t.tid, t.name, n.nid, n.title FROM {interests} i, {term_data} t, {term_node} tn, {node} n WHERE n.nid = tn.nid AND tn.tid = t.tid AND i.tid = t.tid AND i.uid = '%s' AND n.type = '%s' GROUP BY n.nid ORDER BY i.counter desc, n.changed desc LIMIT 5", $user->uid, $row['type']);
327                            if (db_num_rows($result)){
328                                    $sectionTitle = $row['name'];
329                                    $output .= "<h2>$sectionTitle</h2>";
330                                    $output .= "<ul>";
331    
332                                    while ($rowNodes = db_fetch_array($result)){
333                                            $output .= "<li>".l($rowNodes['title'], 'node/'.$rowNodes['nid'])."</li>";
334                                    }
335    
336                                    $output .= "</ul>";
337                            }
338                  }                  }
339      }
340            else{
341                    $output .= t("<br />You haven't specified any interests yet.");
342          }          }
   
343          return $output;          return $output;
344    
345  }  }
# Line 240  function interests_browse() { Line 347  function interests_browse() {
347  function _interests_user_suggestions(){  function _interests_user_suggestions(){
348          global $user;          global $user;
349    
350          if($user->uid == 0){          if ($user->uid == 0){
351                  $output = t("This site uses interest tagging for authenticated users");                  $output = t("This site uses interest tagging for authenticated users");
352          }          }
353          else{          else{

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2