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

Diff of /contributions/modules/user_tags/user_tags.module

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

revision 1.15, Mon Feb 12 08:57:52 2007 UTC revision 1.15.4.1, Wed Mar 4 20:27:28 2009 UTC
# Line 1  Line 1 
1  <?php // $id:$  <?php
2    // $Id$
3    
4  /**  /**
5  * Implementation of hook_help().   * Implementation of hook_help().
6   */   */
7  function user_tags_help($section) {  function user_tags_help($path, $arg) {
8    switch ($section) {    switch ($path) {
9      case 'admin/help#user_tags':      case 'admin/help#user_tags':
10      case 'admin/modules#description':      case 'admin/modules#description':
11        return t('Allows users to free tag themselves.');        return t('Allows users to free tag themselves.');
12    }    }
13  }  }
14    
15    
16  /**  /**
17   * Implementation of hook_perm().   * Implementation of hook_perm().
18   */   */
19  function user_tags_perm() {  function user_tags_perm() {
20    return array('administer user tags', 'tag users');    return array('administer user tags', 'tag users', 'view user tags');
21  }  }
22    
23  function user_tags_menu($may_cache) {  
24    /**
25     * Implementation of hook_menu().
26     */
27    function user_tags_menu() {
28    $access = 'tag users';    $access = 'tag users';
29    $items = array();    $items = array();
30    if ($may_cache) {  
31      $items[] = array(    $items['tags'] = array(
32      'path' => 'tag/user',      'title' => 'Tags in use',
33      'title' => t('tag'),      'page callback' => 'user_tags_page_list',
34      'callback' => 'user_tags_page',      'access arguments' => array($access),
     'access' => $access,  
35      'type' => MENU_CALLBACK,      'type' => MENU_CALLBACK,
36      );    );
37      $items[] = array(  
38      'path' => 'admin/settings/user_tags',    $items['tag/user'] = array(
39      'title' => t('User Tags'),      'title' => 'List of tagged users',
40      'callback' => 'drupal_get_form',      'page callback' => 'user_tags_page_users',
41      'callback arguments' => array('user_tags_settings'),      'access arguments' => array($access),
42      'access' => user_access('administer user tags'),      'type' => MENU_CALLBACK,
43      );
44    
45      $items['admin/settings/user_tags'] = array(
46        'title' => 'User Tags',
47        'description' => 'Settings for user tags',
48        'page callback' => 'drupal_get_form',
49        'page arguments' => array('user_tags_settings'),
50        'access arguments' => array('administer user tags'),
51      'type' => MENU_NORMAL_ITEM,      'type' => MENU_NORMAL_ITEM,
52      );    );
53    }  
54    return $items;    return $items;
55  }  }
56    
57    
58    /**
59     * Implementation of hook_settings().
60     */
61  function user_tags_settings() {  function user_tags_settings() {
62    if ($_POST['edit']['user_tags_vocabulary_list']) {    if ($_POST['user_tags_vocabulary_list']) {
63      db_query('DELETE FROM {user_tags_vocabulary}');      db_query('DELETE FROM {user_tags_vocabulary}');
64      foreach ($_POST['edit']['user_tags_vocabulary_list'] as $vid) {      foreach ($_POST['user_tags_vocabulary_list'] as $vid) {
65        db_query("INSERT INTO {user_tags_vocabulary} (vid) VALUES (%d)", $vid);        db_query("INSERT INTO {user_tags_vocabulary} (vid) VALUES (%d)", $vid);
66      }      }
67    }    }
68    
69    $vocab = db_query("SELECT vid, name, description FROM {vocabulary}");    $vocab = db_query("SELECT vid, name, description FROM {vocabulary}");
70    if (db_affected_rows() > 0) {    if (db_affected_rows() > 0) {
71      while($vocab_list = db_fetch_object($vocab)) {      while ($vocab_list = db_fetch_object($vocab)) {
72        $options[$vocab_list->vid] = $vocab_list->name;        $options[$vocab_list->vid] = $vocab_list->name;
       if ($vocab_list->description) {  
         $options[$vocab_list->vid] .=" - ". $vocab_list->description;  
       }  
73      }      }
74      $enabled_vocab = db_query("SELECT vu.vid, v.name FROM {user_tags_vocabulary} vu INNER JOIN {vocabulary} v ON v.vid = vu.vid");      $enabled_vocab = db_query("SELECT vu.vid, v.name FROM {user_tags_vocabulary} vu INNER JOIN {vocabulary} v ON v.vid = vu.vid");
75      while($enabled = db_fetch_object($enabled_vocab)) {      while ($enabled = db_fetch_object($enabled_vocab)) {
76        $match_options[] = $enabled->vid;        $match_options[] = $enabled->vid;
77      }      }
78      $form["user_tags_vocabulary_list"] = array("#type" => "select",      $form["user_tags_vocabulary_list"] = array(
79       "#multiple" => true,        "#type" => 'select',
80          "#multiple" => true,
81        "#required" => true,        "#required" => true,
82        "#title" => t("Vocabularies Available"),        "#title" => t("Vocabularies Available"),
83        "#description" => t("To select multiple items, hold ctrl (command) and click on the items"),        "#description" => t("To select multiple items, hold ctrl (command) and click on the items"),
# Line 71  function user_tags_settings() { Line 85  function user_tags_settings() {
85        "#default_value" => $match_options        "#default_value" => $match_options
86      );      );
87    
88      $form["user_tags_display_options"] = array("#type" => "fieldset",      $form["user_tags_display_options"] = array(
89          "#type" => 'fieldset',
90        "#collapsible" => true,        "#collapsible" => true,
91        "#title" => t("Display Options")        "#title" => t("Display Options")
92      );      );
93    
94      $form["user_tags_display_options"]["user_tags_user_page"] = array("#type" => "checkbox",      $form["user_tags_display_options"]["user_tags_user_page"] = array(
95          "#type" => 'checkbox',
96        "#title" => t("Display tags on user's profile pages (user/username)"),        "#title" => t("Display tags on user's profile pages (user/username)"),
97        "#default_value" => variable_get("user_tag_user_page", true),        "#default_value" => variable_get("user_tags_user_page", true),
98        "#return_value" => true,        "#return_value" => true,
99      );      );
100    
101    } else {    }
102      drupal_set_message(t("You must add a ". l(t("vocabulary"), "admin/taxonomy") ."before using this module."), "error");    else {
103        drupal_set_message(t('You must add a !link before using this module.', array('!link' => l(t('vocabulary'), 'admin/content/taxonomy'))), "error");
104    }    }
105    
106    return system_settings_form($form);    return system_settings_form($form);
107  }  }
108    
109    
110    /**
111     * Implementation of hook_user().
112     */
113  function user_tags_user($op, &$edit, &$account, $category = NULL) {  function user_tags_user($op, &$edit, &$account, $category = NULL) {
114    switch ($op) {    switch ($op) {
115      case 'view':      case 'view':
116        $list = array();        if (user_access('view user tags')) {
117        foreach ($account->taxonomy as $tid => $term) {          return user_tags_view_tags($account);
         $list[] = array('value' => l($term->name, "tag/user/$tid"));  
       }  
       if (!empty($list)) {  
         return array('Tags' => $list);  
118        }        }
119      break;        break;
120    
     case 'delete':  
       return user_tags_delete($account->uid);  
     break;  
   
121      case 'load':      case 'load':
122        $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.uid = %d ORDER BY weight, name', $account->uid);        user_tags_load_tags($account);
123        $account->taxonomy = array();        break;
       while ($term = db_fetch_object($result)) {  
         $account->taxonomy[$term->tid] = $term;  
       }  
       return $account->taxonomy;  
     break;  
   
     case 'categories':  
       return array(array('name' =>'tags',  
                          'title' => 'tags',  
                          'weight' => 10));  
     break;  
   
124    
125      case 'form':      case 'form':
126        if ($category != 'tags') {        return user_tags_form_tags($category, $account);
127          return;  
       }  
   
       if (!isset($account->taxonomy)) {  
            if ($account->uid) {  
              $terms = user_tags_get_terms($account->uid);  
            }  
            else {  
              $terms = array();  
            }  
          }  
          else {  
            $terms = $account->taxonomy;  
          }  
   
         $c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {user_tags_vocabulary} u ON v.vid = u.vid ORDER BY v.weight, v.name", 'v', 'vid'));  
   
         while ($vocabulary = db_fetch_object($c)) {  
            if ($vocabulary->tags) {  
              $typed_terms = array();  
              foreach ($terms as $term) {  
                // Extract terms belonging to the vocabulary in question.  
                if ($term->vid == $vocabulary->vid) {  
   
                  // Commas and quotes in terms are special cases, so encode 'em.  
                  if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {  
                    $term->name = '"'.preg_replace('/"/', '""', $term->name).'"';  
                  }  
                 $typed_terms[] = $term->name;  
                }  
              }  
              $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);  
   
              if ($vocabulary->help) {  
                $help = $vocabulary->help;  
              }  
              else {  
                $help = t('A comma-separated list of terms describing this content.  Example: funny, bungee jumping, "Company, Inc.".');  
              }  
              $form['taxonomy']['freetags'][$vocabulary->vid] = array('#type' => 'textfield',  
                '#title' => $vocabulary->name,  
                '#description' => $help,  
                '#required' => $vocabulary->required,  
                '#default_value' => $typed_string,  
                '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,  
                '#weight' => $vocabulary->weight,  
                '#maxlength' => 100,  
              );  
            }  
            else {  
              // Extract terms belonging to the vocabulary in question.  
              $default_terms = array();  
              foreach ($terms as $term) {  
                if ($term->vid == $vocabulary->vid) {  
                  $default_terms[$term->tid] = $term;  
                }  
              }  
              $form['taxonomy']['tags'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), $vocabulary->help);  
              $form['taxonomy']['tags'][$vocabulary->vid]['#weight'] = $vocabulary->weight;  
              $form['taxonomy']['tags'][$vocabulary->vid]['#required'] = $vocabulary->required;  
            }  
   
          if (isset($form['taxonomy'])) {  
            $form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);  
           }  
         }  
       return $form;  
     break;  
   
128      case 'submit':      case 'submit':
129        if ($category != 'tags') {        user_tags_submit_tags($category, $edit, $account);
130          return;        break;
131        }  
132        user_tags_delete($account->uid);      case 'categories':
133          return array(array('name' => 'tags', 'title' => t('Tags'), 'weight' => 10));
134        if (is_array($edit['taxonomy'])) {  
135          if (isset($edit['taxonomy']['freetags'])) {      case 'delete':
136            foreach ($edit['taxonomy']['freetags'] as $key => $value){        user_tags_delete_tags($account);
137              $vid = $key;        break;
             $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';  
             preg_match_all($regexp, $value, $matches);  
             $typed_terms = $matches[1];  
             foreach ($typed_terms as $typed_term) {  
               $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));  
               $typed_term = trim($typed_term);  
               if ($typed_term == '') continue;  
               $possibilities = taxonomy_get_term_by_name($typed_term);  
               $typed_term_tid = NULL; // tid match if any.  
               foreach ($possibilities as $possibility) {  
                 if ($possibility->vid == $vid) {  
                   $typed_term_tid = $possibility->tid;  
                 }  
               }  
               if (!$typed_term_tid) {  
                 $save_terms = array('vid' => $vid, 'name' => $typed_term);  
                 $status = taxonomy_save_term($save_terms);  
                 $typed_term_tid = $save_terms['tid'];  
               }  
               db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $typed_term_tid, $vid);  
             }  
           }  
         }  
         if (isset($edit['taxonomy']['tags'])) {  
           foreach ($edit['taxonomy']['tags'] as $vid => $term) {  
             if (is_array($term)) {  
               foreach ($term as $tid) {  
                 if ($tid) {  
                   db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $tid, $vid);  
                 }  
               }  
             }  
             else if ($term) {  
               db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $term, $vid);  
             }  
           }  
         }  
       }  
       unset($edit['taxonomy']);  
     break;  
138    }    }
139  }  }
140    
141    
142  /**  /**
143   * Find all terms associated to the given user, ordered by vocabulary and term weight.   * Find all terms associated to the given user, ordered by vocabulary and term weight.
144     *
145     * @param int - $uid : User ID
146     * @param char - $key : Key used for query rewriting - default to 'tid'
147     * @return array - of terms assigned to a user
148   */   */
149  function user_tags_get_terms($uid, $key = 'tid') {  function user_tags_get_terms($uid, $key = 'tid') {
150    static $terms;    static $terms;
# Line 262  function user_tags_get_terms($uid, $key Line 159  function user_tags_get_terms($uid, $key
159    return $terms[$uid];    return $terms[$uid];
160  }  }
161    
162  function user_tags_delete($uid) {  
163    db_query('DELETE FROM {term_user} WHERE uid = %d', $uid);  /**
164     * prints all the vocab in use atm
165     *
166     * @param char $tid : term id
167     * @return char - page listing off all the users with corresponding term
168     */
169    function user_tags_page_list() {
170      //$term = taxonomy_get_term($tid);
171    
172      //print all the tags currently in use.
173      $query = db_query('SELECT DISTINCT( td.name ) as termname, tu.tid, tu.vid as vid FROM {term_data} td INNER JOIN ({term_user} as tu)
174                          ON td.tid = tu.tid ');
175      //get all the vocabs in use
176      //$vocab_query = (db_query('SELECT DISTINCT(tu.vid) as vid, v.name FROM {term_user} tu INNER JOIN {vocabulary} v  ON tu.vid = v.vid GROUP BY vid'));
177      //$count_vocab = db_num_rows($vocab_query);
178    
179      $vocabs = taxonomy_get_vocabularies();
180    
181      while ($term = db_fetch_object($query)) {
182        $vocab[$term->vid][$term->tid] = $term->termname;
183      }
184    
185      $output = '';
186      foreach ($vocab as $vid => $tid) {
187        $rows = array();
188        foreach ($tid as $term_tid => $term_name) {
189          $rows[] = array(l($term_name, "tag/user/$term_tid"));
190        }
191        $output .= theme('table', array(t('Tags')), $rows, array(), $vocabs[$vid]->name);
192      }
193    
194      return $output;
195  }  }
196    
197  function user_tags_page($tid = null) {  
198    global $user;  /**
199     * Generate a table of users and their tags
200     *
201     * @param int $tid : Term ID
202     * @return char : page listing all users and their tags
203     */
204    function user_tags_page_users($tid) {
205    $row = array();    $row = array();
206    $rows = array();    $rows = array();
207    $term = taxonomy_get_term($tid);    $term = taxonomy_get_term($tid);
208      $tmp = array();
209    if (!$tid || $term->tid != $tid) {    if (!$tid || $term->tid != $tid) {
210        drupal_goto('tags');
     //print all the tags currently in use.  
     $query = db_query('SELECT DISTINCT( td.name ) as termname, tu.tid FROM {term_data} td INNER JOIN {term_user} tu  ON td.tid = tu.tid  
                        ORDER BY td.weight, td.name');  
     while ($term = db_fetch_object($query)) {  
       $rows[] = array(l($term->termname, "tag/user/$term->tid"));  
     }  
     return theme('table', array(t('Tags')), $rows);  
211    }    }
212    else {    else {
213      drupal_set_title($term->name);      drupal_set_title($term->name);
# Line 292  function user_tags_page($tid = null) { Line 219  function user_tags_page($tid = null) {
219        if ($term->username != $old->username && !empty($row)) {        if ($term->username != $old->username && !empty($row)) {
220          $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));          $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));
221          unset($row); // Cleaning and reinitializing to avoid duplicates.          unset($row); // Cleaning and reinitializing to avoid duplicates.
222          $row = array();          $row = array();
223        }        }
224        $old = $term;        $old = $term;
225        $row[] = l($term->termname, "tag/user/$term->tid");        $row[] = l($term->termname, "tag/user/$term->tid");
# Line 302  function user_tags_page($tid = null) { Line 229  function user_tags_page($tid = null) {
229    }    }
230  }  }
231    
232    
233  /**  /**
234  * Find all terms associated to the user of a given vocabulary.   * Get all the terms associated with a user but limited to a specified Vocabulary ID
235     *
236     * @param int  $uid : user ID
237     * @param int  $vid : vocabulary ID
238     * @param char $key : term ID
239     * @return array : all the tags belonging to the user with the specified uid
240   */   */
241  function user_tags_get_terms_by_vocabulary($uid, $vid, $key = 'tid') {  function user_tags_get_terms_by_vocabulary($uid, $vid, $key = 'tid') {
242    $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.vid = %d AND tu.uid =%d', $vid, $uid);    $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.vid = %d AND tu.uid =%d', $vid, $uid);
# Line 312  function user_tags_get_terms_by_vocabula Line 245  function user_tags_get_terms_by_vocabula
245      $terms[$term->$key] = $term;      $terms[$term->$key] = $term;
246    }    }
247    return $terms;    return $terms;
248  }  }
249    
250    
251    /**
252     * generate the categories selected for this module
253     * in admin setting, if user has already selected any tags
254     * show them
255     *
256     * @param  char  $category : current page name
257     * @param  array $account  : user details
258     * @return array : taxonomy form
259     */
260    function user_tags_form_tags($category, $account) {
261      if ($category != 'tags') {
262        return;
263      }
264    
265      if (!isset($account->taxonomy)) {
266        if ($account->uid) {
267          $terms = user_tags_get_terms($account->uid);
268        }
269        else {
270          $terms = array();
271        }
272      }
273      else {
274        $terms = $account->taxonomy;
275      }
276    
277      $c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {user_tags_vocabulary} u ON v.vid = u.vid ORDER BY v.weight, v.name", 'v', 'vid'));
278    
279      while ($vocabulary = db_fetch_object($c)) {
280        if ($vocabulary->tags) {
281          $typed_terms = array();
282          foreach ($terms as $term) {
283            // Extract terms belonging to the vocabulary in question.
284            if ($term->vid == $vocabulary->vid) {
285              // Commas and quotes in terms are special cases, so encode 'em.
286              if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {
287                $term->name = '"'. preg_replace('/"/', '""', $term->name) .'"';
288              }
289              $typed_terms[] = $term->name;
290            }
291          }
292    
293          $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
294          if ($vocabulary->help) {
295            $help = $vocabulary->help;
296          }
297          else {
298            $help = t('A comma-separated list of terms describing this content.  Example: funny, bungee jumping, "Company, Inc.".');
299          }
300    
301          $form['taxonomy']['freetags'][$vocabulary->vid] = array('#type' => 'textfield',
302            '#title' => $vocabulary->name,
303            '#description' => $help,
304            '#required' => $vocabulary->required,
305            '#default_value' => $typed_string,
306            '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
307            '#weight' => $vocabulary->weight,
308            '#maxlength' => 100,
309          );
310        }
311        else {
312          // Extract terms belonging to the vocabulary in question.
313          $default_terms = array();
314          foreach ($terms as $term) {
315            if ($term->vid == $vocabulary->vid) {
316              $default_terms[$term->tid] = $term;
317            }
318          }
319    
320          $form['taxonomy']['tags'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), $vocabulary->help);
321          $form['taxonomy']['tags'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
322          $form['taxonomy']['tags'][$vocabulary->vid]['#required'] = $vocabulary->required;
323        }
324        if (isset($form['taxonomy'])) {
325          $form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);
326        }
327      }
328      return $form;
329    }
330    
331    
332    /**
333     * Load tags onto a User Object
334     *
335     * @param object $account : User Object for modifying (passed ByRef)
336     */
337    function user_tags_load_tags(&$account) {
338      $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.uid = %d ORDER BY weight, name', $account->uid);
339      $account->taxonomy = array();
340      while ($term = db_fetch_object($result)) {
341        $account->taxonomy[$term->tid] = $term;
342      }
343    }
344    
345    
346    /**
347     * Function for deleting tags from a user account
348     *
349     * @param object $account : User Object
350     */
351    function user_tags_delete_tags($account) {
352      db_query('DELETE FROM {term_user} WHERE uid = %d', $account->uid);
353    }
354    
355    
356    /**
357     * User submit handling function.
358     *
359     * @param char $category : The selected tab on the user profile page.
360     * @param array $edit : the values returned from the submitted form
361     * @param object $account : The User Object
362     */
363    function user_tags_submit_tags($category, $edit, $account) {
364      if ($category != 'tags') {
365        return;
366      }
367      //delete current tags and replace them with the new submitted ones
368      user_tags_delete_tags($account);
369    
370      if (is_array($edit['taxonomy'])) {
371        if (isset($edit['taxonomy']['freetags'])) {
372          foreach ($edit['taxonomy']['freetags'] as $key => $value) {
373            $vid = $key;
374            $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
375            preg_match_all($regexp, $value, $matches);
376            $typed_terms = $matches[1];
377            foreach ($typed_terms as $typed_term) {
378              $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
379              $typed_term = trim($typed_term);
380              if ($typed_term == '') continue;
381              $possibilities = taxonomy_get_term_by_name($typed_term);
382              $typed_term_tid = NULL; // tid match if any.
383              foreach ($possibilities as $possibility) {
384                if ($possibility->vid == $vid) {
385                  $typed_term_tid = $possibility->tid;
386                }
387              }
388              if (!$typed_term_tid) {
389                $save_terms = array('vid' => $vid, 'name' => $typed_term);
390                $status = taxonomy_save_term($save_terms);
391                $typed_term_tid = $save_terms['tid'];
392              }
393              db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $typed_term_tid, $vid);
394            }
395          }
396        }
397        if (isset($edit['taxonomy']['tags'])) {
398          foreach ($edit['taxonomy']['tags'] as $vid => $term) {
399            if (is_array($term)) {
400              foreach ($term as $tid) {
401                if ($tid) {
402                  db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $tid, $vid);
403                }
404              }
405            }
406            else if ($term) {
407              db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $term, $vid);
408            }
409          }
410        }
411      }
412      unset($edit['taxonomy']);
413    }
414    
415    
416    /**
417     * Return an array of links to user tags.
418     *
419     * @param object $account : User account object (from user_load() function)
420     * @return array : nested array
421     */
422    function user_tags_view_tags($account) {
423      $list = array();
424      foreach ($account->taxonomy as $tid => $term) {
425        $list[$term->vid][] = l($term->name, "tag/user/$tid");
426      }
427    
428      $vocs = taxonomy_get_vocabularies();
429      $account->content['tags'] = array(
430        '#title' => t('Tags'),
431        '#type' => 'user_profile_category'
432      );
433      foreach ($list as $key => $terms) {
434        $account->content['tags']['vid-'.$key] = array(
435          '#title' => $vocs[$key]->name,
436          '#type' => 'user_profile_item',
437          '#value' => implode(', ', $terms)
438        );
439      }
440    }
441    
442    /**
443     * Find users with similar tags. Results are ordered so the closest matching user
444     *   is decided on number of matching terms, falling back to alphabetical on
445     *   username if matching terms are equal.
446     *
447     * @param array $terms : array of term objects with the array keys as term ID's (ie, the result of user_tags_get_terms($uid)
448     * @param int $limit : optional, limit the number of results returned. Default: 5
449     * @return array : Array of output from theme_username.
450     */
451    function user_tags_get_similar_users($terms, $limit = 5) {
452      if (empty($terms)) {
453        return array();
454      }
455    
456      $args = implode(',', array_keys($terms));
457      $result = db_query('SELECT
458                            u.uid AS uid,
459                            COUNT(*) AS cnt
460                          FROM {users} u
461                          LEFT JOIN {term_user} tu ON tu.uid = u.uid AND tu.tid IN (%s)
462                          WHERE u.status = 1 AND tu.tid IS NOT NULL
463                          GROUP BY u.uid
464                          ORDER BY cnt DESC, u.name ASC
465                          LIMIT %d', $args, $limit);
466      if (db_num_rows($result)) {
467        $items = array();
468    
469        while ($item = db_fetch_object($result)) {
470          $items[] = theme('username', user_load(array('uid' => $item->uid)));
471        }
472    
473        return $items;
474      }
475      else {
476        return array();
477      }
478    }
479    

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.15.4.1

  ViewVC Help
Powered by ViewVC 1.1.2