t('UT basic'), 'page callback' => 'drupal_get_form', 'page arguments' => array('ut_basic_settings_form'), 'access arguments' => array('administer user titles'), 'type' => MENU_LOCAL_TASK, 'file' => 'ut_basic.admin.inc', ); return $items; } /** * Implementation of hook_nodeapi(). */ function ut_basic_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { if (variable_get('user_titles_hook_module', 'ut_basic') == 'ut_basic') { if ($op == 'insert' || $op == 'delete') { // Update the users post count $allowed_types = variable_get('ut_basic_node_types', array()); if (in_array($node->type, $allowed_types)) { $inc = ($op == 'insert') ? 1 : -1; db_query("UPDATE {user_titles_posts} SET posts = %d WHERE uid = %d", user_titles_get_posts($node->uid) + $inc, $node->uid); if (!db_affected_rows()) { db_query("INSERT INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + $inc); } } // Update the users title user_titles_set_user_title($a1->uid); } } } /** * Implementation of hook_comment(). */ function ut_basic_comment($a1, $op) { // Only run this if this module is controlling user titles if (variable_get('user_titles_hook_module', 'ut_basic') == 'ut_basic') { if ($op == 'insert' || $op == 'delete') { // Update the users post count $a1 = (object) $a1; // sometimes an array is passed in $node = node_load($a1->nid); $types = variable_get('ut_basic_comment_types', array()); if (in_array($node->type, $types)) { $uid = $a1->uid; $inc = ($op == 'insert') ? 1 : -1; db_query("UPDATE {user_titles_posts} SET posts = %d WHERE uid = %d", user_titles_get_posts($uid) + $inc, $uid); if (!db_affected_rows()) { db_query("INSERT INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, user_titles_get_posts($uid) + $inc); } } // Update the users title user_titles_set_user_title($a1->uid); } } } /** * Implementation of hook_user(). * * Add the 'edit user title' form to the edit user page. */ function ut_basic_user($op, $edit, &$user, $category = NULL) { switch ($op) { case 'delete': db_query('DELETE FROM {user_titles_posts} WHERE uid = %d', $user->uid); break; } } /** * Update the post count for a given user. * * @param $uid * The user id to update. * @param $count * The count to write. If not given, posts will be counted and that data * written. * * @return * The post count for the user. */ function ut_basic_update_post_count($uid, $count = NULL) { if (is_null($count)) { $count = 0; // Fetch count from the database $allowed_node_types = variable_get('ut_basic_node_types', array()); $allowed_comment_types = variable_get('ut_basic_comment_types', array()); if ($allowed_types) { $in = ''; $query_args = array($uid); foreach ($allowed_node_types as $type) { $query_args[] = $type; $in .= $in ? ", '%s'" : "'%s'"; } $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE uid = %d AND type IN ($in)", $query_args)); if (module_exists('comment')) { $in = ''; $query_args = array($uid); foreach ($allowed_node_types as $type) { $query_args[] = $type; $in .= $in ? ", '%s'" : "'%s'"; } $count += db_result(db_query("SELECT COUNT(*) FROM {comments} WHERE uid = %d AND nid IN (SELECT nid FROM {node} WHERE type IN ($in)", $query_args)); } } } db_query("UPDATE {user_titles_posts} SET posts = %d WHERE uid = %d", $count, $uid); if (!db_affected_rows()) { db_query("INSERT INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, $count); } return $count; } /** * Reset post count for all users. */ function ut_basic_update_all_post_counts() { // Just in case. This could take a little while... set_time_limit(600); db_query('TRUNCATE TABLE {user_titles_posts}'); $result = db_query('SELECT uid FROM {users}'); $uids = array(); while ($row = db_fetch_array($result)) { $uids[] = $row['uid']; } foreach ($uids as $uid) { ut_basic_update_post_count($uid); } } /** * Built-in hook implementation that counts nodes */ function ut_basic_user_titles($op, $uid = NULL) { switch ($op) { case 'register': return 'ut_basic'; case 'name': return t('Basic post count'); case 'description': return t('Built-in, see below'); case 'units': return t('Posts'); case 'url': return 'admin/user/user-titles/ut-basic'; case 'get': $res = db_result(db_query("SELECT posts FROM {user_titles_posts} WHERE uid = %d", $uid)); if ($res === FALSE) { $res = ut_basic_update_post_count($uid); } return $res; } }