? LICENSE.txt ? user_titles_6_port_2.patch ? user_titles_6_port_3.patch Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/README.txt,v retrieving revision 1.4 diff -u -p -r1.4 README.txt --- README.txt 19 Nov 2007 00:51:51 -0000 1.4 +++ README.txt 11 Nov 2008 06:23:00 -0000 @@ -3,37 +3,8 @@ because you need to modify your theme in you like. The function you need to call to get the data is -user_titles_get_user_title(). You may call this function either in -your template.php or in your node.tpl.php and/or your comment.tpl.php. It -is recommended that you place this in your template.php. - -In template.php, in the function _phptemplate_variables, place this -piece of code under case 'node': and a similar one under case 'comment' (see -below): - - if (module_exists('user_titles')) { - $vars['user_title'] = user_titles_get_user_title($vars['node']->uid); - } - -Note that $vars may be named something else, such as $variables in your -implementation. Note the variable in the function definition. - -If you do not have a _phptemplate_variables() in your template.php -file, you may create one: - -function _phptemplate_variables($hook, $vars = array()) { - switch ($hook) { - case 'node': - if (module_exists('user_titles')) { - $vars['user_title'] = user_titles_get_user_title($vars['node']->uid); - } - case 'comment': - if (module_exists('user_titles')) { - $vars['user_title'] = user_titles_get_user_title($vars['comment']->uid); - } - } - return $vars; -} +user_titles_get_user_title(). This module will automatically populate the +$user_title variable into your nodes and comments. In your node.tpl.php and/or your comment.tpl.php, choose where you would like the user title to appear, and place this code: @@ -42,11 +13,6 @@ the user title to appear, and place this
-There are other places you may wish a user title to appear, such as the user -profile page. Please see the drupal.org theming handbook for more information -on theming those; the additions you make will be materially the same as -these. - NOTE: The settings UI for this currently requires javascript and doesn't degrade. -Patches welcome. +Patches welcome. \ No newline at end of file Index: user_titles.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/user_titles.info,v retrieving revision 1.2 diff -u -p -r1.2 user_titles.info --- user_titles.info 20 Jul 2007 18:18:45 -0000 1.2 +++ user_titles.info 11 Nov 2008 06:23:01 -0000 @@ -1,4 +1,7 @@ -; $Id: user_titles.info,v 1.2 2007/07/20 18:18:45 merlinofchaos Exp $ +; $Id$ name = "User titles" -description = "Allow users to have titles based upon how many posts they have." \ No newline at end of file +description = "Allow users to have titles based upon how many posts they have." +version = "6.x-0.1" +core = "6.x" + Index: user_titles.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/user_titles.install,v retrieving revision 1.3 diff -u -p -r1.3 user_titles.install --- user_titles.install 27 Jul 2007 00:19:56 -0000 1.3 +++ user_titles.install 11 Nov 2008 06:23:01 -0000 @@ -1,37 +1,29 @@ array( + 'tid' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10'), + 'title' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE), + 'value' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10')), + 'primary key' => array('tid'), + ); + + $schema['user_titles_posts'] = array( + 'fields' => array( + 'uid' => array('type' => 'int', 'not null' => TRUE, 'disp-width' => '10'), + 'posts' => array('type' => 'int', 'not null' => FALSE, 'disp-width' => '11')), + 'primary key' => array('uid'), + ); + + return $schema; } -function user_titles_update_1() { - $ret = array(); - $ret[] = update_sql("RENAME TABLE {user_titles} TO {user_titles_posts}"); - $ret[] = update_sql(" - CREATE TABLE {user_titles} ( - tid int(10) NOT NULL PRIMARY KEY, - title varchar(255) NOT NULL, - value int(10) NOT NULL - ) /*!40100 DEFAULT CHARACTER SET utf8 */ - "); - - return $ret; +function user_titles_install() { + drupal_install_schema('user_titles'); } function user_titles_uninstall() { - db_query("DROP TABLE {user_titles}"); - db_query("DROP TABLE {user_titles_posts}"); + drupal_uninstall_schema('user_titles'); } Index: user_titles.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/user_titles.module,v retrieving revision 1.13 diff -u -p -r1.13 user_titles.module --- user_titles.module 19 Nov 2007 00:46:36 -0000 1.13 +++ user_titles.module 11 Nov 2008 06:23:01 -0000 @@ -1,5 +1,5 @@ t('User titles'), - 'description' => t('Configure user titles and number of posts required'), - 'path' => 'admin/user/user-titles', - 'callback' => 'drupal_get_form', - 'callback arguments' => array('user_titles_settings_form'), - 'access' => user_access('administer user titles'), - 'type' => MENU_NORMAL_ITEM, - ); - return $items; - } +function user_titles_menu() { + $items['admin/user/user-titles'] = array( + 'title' => 'User titles', + 'description' => 'Configure user titles and number of posts required', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('user_titles_settings_form'), + 'access arguments' => array('administer user titles'), + ); + return $items; } /** @@ -69,7 +65,8 @@ function user_titles_settings_form() { // Build the form bit so we can get the actual number of titles already in // the form, which may have been changed thanks to javascript. - $form['num_titles'] = form_builder('user_titles_settings_form', $form['num_titles']); + $form_state = array(); + $form['num_titles'] = form_builder('user_titles_settings_form', $form['num_titles'], $form_state); $actual_titles = $form['num_titles']['#value']; $form['titles'] = array('#tree' => TRUE); @@ -116,6 +113,14 @@ function user_titles_settings_form() { return $form; } +function user_titles_theme() { + return array( + 'user_titles_settings_form' => array( + 'arguments' => array('form' => NULL), + ), + ); +} + /** * Theme the user title settings form. * @@ -137,7 +142,7 @@ function theme_user_titles_settings_form $output = '
'; - foreach (element_children($form['titles']) as $key) { + foreach (element_children($form['titles']) as $key) { // set a reference so that the drupal_render gets remembered. unset($elem); $elem = &$form['titles'][$key]; @@ -170,9 +175,9 @@ function theme_user_titles_settings_form /** * Validate the user titles setting form */ -function user_titles_settings_form_validate($form_id, $form_values, $form) { +function user_titles_settings_form_validate($form, &$form_state) { $used_posts = array(); - foreach ($form_values['titles'] as $id => $title) { + foreach ($form_state['values']['titles'] as $id => $title) { // if no title and no value, this is a deletion and we'll skip valiation on it. if (empty($title['value']) && empty($title['title'])) { continue; @@ -194,7 +199,7 @@ function user_titles_settings_form_valid */ function user_titles_settings_form_submit($form_id, $form_values) { $titles = array(); - foreach ($form_values['titles'] as $id => $title) { + foreach ($form_values['values']['titles'] as $id => $title) { if (empty($title['value']) && empty($title['title'])) { continue; } @@ -202,13 +207,14 @@ function user_titles_settings_form_submi } user_titles_set_titles($titles); - // If they change the allowed types, wipe the existing counts so each one will be fresh. + // If they change the allowed types, rebuild the existing counts so each one will be fresh. $types = user_titles_get_allowed_types(); - if ($types != $form_values['types']) { - db_query("TRUNCATE {user_titles_posts}"); + + if ($types != array_keys(array_filter($form_values['values']['types']))) { + user_titles_set_allowed_types(array_keys(array_filter($form_values['values']['types']))); + user_titles_rebuild_counts(); } - user_titles_set_allowed_types(array_keys(array_filter($form_values['types']))); drupal_set_message(t('The configuration has been updated.')); } @@ -216,12 +222,12 @@ function user_titles_settings_form_submi * Implementation of hook_nodeapi */ function user_titles_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { - if ($op == 'insert' || $op == 'delete') { - $allowed_types = user_titles_get_allowed_types(); - if (in_array($node->type, $allowed_types)) { - $inc = ($op == 'insert') ? 1 : -1; - db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + $inc); - } + switch ($op) { + case 'insert': + $allowed_types = user_titles_get_allowed_types(); + if (in_array($node->type, $allowed_types)) { + db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + 1); + } } } @@ -229,11 +235,12 @@ function user_titles_nodeapi(&$node, $op * Implementation of hook_comment. */ function user_titles_comment($a1, $op) { - if ($op == 'insert' || $op == 'delete') { - $a1 = (object)$a1; // sometimes an array is passed in - $uid = $a1->uid; - $inc = ($op == 'insert') ? 1 : -1; - db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, user_titles_get_posts($uid) + $inc); + if ($op == 'insert') { + $node = node_load($a1['nid']); + $allowed_types = user_titles_get_allowed_types(); + if (in_array($node->type, $allowed_types)) { + db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $a1['uid'], user_titles_get_posts($a1['uid']) + 1); + } } } @@ -319,10 +326,21 @@ function user_titles_update_post_count($ $allowed_types = user_titles_get_allowed_types(); if ($allowed_types) { $types = implode("','", $allowed_types); - $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ('$types')", $uid)); + /* + $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ('%s')", $uid, $types)); if (module_exists('comment')) { $count += db_result(db_query("SELECT COUNT(*) FROM {comments} WHERE uid = %d", $uid)); } + */ + if (module_exists('comment')) { + $count = db_result(db_query("SELECT (COUNT(DISTINCT n.nid) + COUNT(DISTINCT c.cid)) FROM {node} n INNER JOIN {comments} c ON c.nid = n.nid WHERE n.type IN ('%s') AND (n.uid = %d OR c.uid = %d) GROUP BY n.uid, c.uid ORDER BY n.uid, c.uid", + $types, $uid, $uid)); + } + else { + $count = db_result(db_query($user_node_count_query = "SELECT COUNT(DISTINCT n.nid) FROM {node} n WHERE n.type IN ('%s') AND n.uid = %d GROUP BY n.uid ORDER BY n.uid", + $types, $uid)); + } + } else { $count = 0; @@ -352,14 +370,12 @@ function user_titles_set_allowed_types($ */ function user_titles_get_titles() { $query = db_query("SELECT title, value, tid FROM {user_titles} ORDER BY value DESC"); - if(db_num_rows($query)) { - while($r = db_fetch_array($query)) { - $result[] = $r; - } - } - else { - $result = array(); + + $result = array(); + while ($r = db_fetch_array($query)) { + $result[] = $r; } + return $result; } @@ -368,9 +384,10 @@ function user_titles_get_titles() { */ function user_titles_set_titles($titles) { db_query("TRUNCATE {user_titles}"); - foreach($titles as $title) { - if($title['tid'] == 'new') { - $title['tid'] = db_next_id("{user_titles}_tid"); + foreach ($titles as $title) { + if ($title['tid'] == 'new') { + $title['tid'] = db_result(db_query('SELECT MAX(tid) AS tid FROM {user_titles}')) + 1; + //$title['tid'] = db_last_insert_id("user_titles", "tid"); } db_query("INSERT INTO {user_titles} (title, value, tid) VALUES ('%s', %d, %d)", $title['title'], $title['value'], $title['tid']); } @@ -400,11 +417,11 @@ function user_titles_user($op, $edit, &$ } $form['user_titles']['current_title'] = array( - '#value' => '
' . t('Current user title:') . ' ' . filter_xss_admin($title) . '
', + '#value' => '
'. t('Current user title:') .' '. filter_xss_admin($title) .'
', ); if (isset($title_info['title']) && empty($title_info['tid'])) { - $default_title_info = user_titles_get_title(user_titles_get_posts($user->uid)); + $default_title_info = user_titles_get_title(user_titles_get_posts($uid)); if (!isset($default_title_info['title'])) { $default_title = t('No title set'); } @@ -412,7 +429,7 @@ function user_titles_user($op, $edit, &$ $default_title = $default_title_info['title']; } $form['user_titles']['default_title'] = array( - '#value' => '
' . t('Default user title:') . ' ' . filter_xss_admin($default_title) . '
', + '#value' => '
'. t('Default user title:') .' '. filter_xss_admin($default_title) .'
', ); } @@ -435,3 +452,51 @@ function user_titles_user($op, $edit, &$ } } } + +function user_titles_rebuild_counts() { + db_query('TRUNCATE TABLE {user_titles_posts}'); + + $types = user_titles_get_allowed_types(); + foreach ($types as $id => $type) { + $types[$id] = "'". $type ."'"; + } + $types_clause = implode(',', $types); + + if ($types_clause != '') { + $user_counts = array(); + + if (module_exists('comment')) { + $user_comment_count_query = "SELECT c.uid, COUNT(c.cid) AS user_content_count FROM {node} n INNER JOIN {comments} c ON c.nid = n.nid WHERE n.type IN ($types_clause) GROUP BY c.uid ORDER BY c.uid"; + + $result = db_query($user_comment_count_query); + while ($row = db_fetch_array($result)) { + $user_counts[$row['uid']] += $row['user_content_count']; + } + } + + $user_node_count_query = "SELECT n.uid, COUNT(n.nid) AS user_content_count FROM {node} n WHERE n.type IN ($types_clause) GROUP BY n.uid ORDER BY n.uid"; + + $result = db_query($user_node_count_query); + while ($row = db_fetch_array($result)) { + $user_counts[$row['uid']] += $row['user_content_count']; + } + + foreach ($user_counts as $uid => $user_content_count) { + $user_node_count_insert_query = db_query("INSERT INTO {user_titles_posts}(uid,posts) VALUES(%d,%d)", + $uid, $user_content_count); + } + + drupal_set_message(t("Users' post counts have been rebuilt.")); + } + else { + drupal_set_message(t("Users' post counts were cleared. Select one or more content types to allow your users to advance.")); + } +} + +function user_titles_preprocess_node(&$variables) { + $variables['user_title'] = user_titles_get_user_title($variables['node']->uid); +} + +function user_titles_preprocess_comment(&$variables) { + $variables['user_title'] = user_titles_get_user_title($variables['comment']->uid); +}