<?php
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook("datahandler_post_insert_thread", "add_thread_count_plus");
$plugins->add_hook("class_moderation_delete_thread_start", "add_thread_count_minus");
$plugins->add_hook("admin_tools_recount_rebuild_output_list", "add_thread_count_showreset");
$plugins->add_hook("admin_tools_recount_rebuild_start", "add_thread_count_dothereset");
function add_thread_count_info()
{
return array(
"name" => "Thread count",
"description" => "Count user threads",
"website" => "http://mybboard.net",
"author" => "Santiago Dimattía",
"authorsite" => "http://teleportz.com.ar",
"version" => "1.0",
"guid" => "",
"compatibility" => "*"
);
}
function add_thread_count_install()
{
global $db;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `threadnum` INT(10) NOT NULL DEFAULT '0';");
}
function add_thread_count_uninstall()
{
global $db;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` DROP `threadnum`;");
}
function add_thread_count_is_installed()
{
global $db;
if($db->field_exists('threadnum', 'users'))
{
return TRUE;
}
return FALSE;
}
function add_thread_count_plus($thread)
{
global $mybb, $db;
if($thread->data['uid'] == $mybb->user['uid'])
{
$uid = $mybb->user['uid'];
$value = $mybb->user['threadnum'];
}
else
{
$q = $db->simple_select('users', 'threadnum', 'uid = ' . $thread['username'], array('limit' => 1));
$value = $db->fetch_field('threadnum', $q);
$uid = $thread->data['uid'];
}
$new_value = ++$value;
$db->update_query('users', array('threadnum' => $new_value), 'uid = ' . $uid);
}
function add_thread_count_minus($tid)
{
global $mybb, $db, $thread;
$q = $db->query('SELECT t.uid, u.threadnum FROM ' . TABLE_PREFIX . 'threads t JOIN ' . TABLE_PREFIX . 'users u ON t.uid = u.uid WHERE t.tid = ' . $tid . ' LIMIT 1');
$result = $db->fetch_array($q);
$threadnum = $result['threadnum'] - 1;
$uid = $result['uid'];
$db->update_query('users', array('threadnum' => $threadnum), 'uid = ' . $uid);
}
function plugin_recount_thread_count()
{
global $db, $mybb, $lang;
$query = $db->simple_select("users", "COUNT(uid) as num_users");
$num_users = $db->fetch_field($query, 'num_users');
$page = intval($mybb->input['page']); if($page == 0) : $page = 1; endif;
$per_page = intval($mybb->input['userthreads']);
$start = ($page-1) * $per_page;
$end = $start + $per_page;
$query = $db->simple_select("forums", "fid", "usepostcounts = 0");
while($forum = $db->fetch_array($query))
{
$fids[] = $forum['fid'];
}
if(is_array($fids))
{
$fids = implode(',', $fids);
}
if($fids)
{
$fids = " AND t.fid NOT IN($fids)";
}
else
{
$fids = "";
}
$query = $db->simple_select("users", "uid", '', array('order_by' => 'uid', 'order_dir' => 'asc', 'limit_start' => $start, 'limit' => $per_page));
while($user = $db->fetch_array($query))
{
$query2 = $db->query("
SELECT COUNT(tid) AS thread_count
FROM ".TABLE_PREFIX."threads t
WHERE uid='{$user['uid']}' AND t.visible > 0{$fids}
");
$num_threads = $db->fetch_field($query2, "thread_count");
$db->update_query("users", array("threadnum" => intval($num_threads)), "uid='{$user['uid']}'");
}
check_proceed($num_users, $end, ++$page, $per_page, "userthreads", "do_recountuserthreads", "Los temas de los usuarios se han recontado correctamente.");
}
function add_thread_count_showreset()
{
global $form, $form_container, $lang;
$form_container->output_cell("<label>Recontar temas de los usuarios</label><div class=\"description\">Cuando ejecutas esta función, el contador de temas de los usuarios se actualizarán para reflejar el actual valor basándose en los temas de la base de datos.</div>");
$form_container->output_cell($form->generate_text_box("userthreads", 500, array('style' => 'width: 150px;')));
$form_container->output_cell($form->generate_submit_button($lang->go, array("name" => "do_recountuserthreads")));
$form_container->construct_row();
}
function add_thread_count_dothereset()
{
global $mybb;
if($mybb->request_method == "post" && isset($mybb->input['do_recountuserthreads']))
{
plugin_recount_thread_count();
}
}
Diferencia con el de Edson:
- No realiza consultas adicionales cuando ves un post (El de Edson realiza 1 consulta extra por cada post cargado)
- No hace nada automaticamente.
Para usar, primero debés ir a "Recount & Rebuild" y ejecutar la tarea para que cuente todos los threads.
Despues agregar en los templates:
- En los threads: $post['threadnum']
- En el perfil: $memprofile['threadnum']
- Etc (El conteo funcionará en cualquier parte del foro, ya que se "anexa" al perfil del usuario. Esto funciona: $mybb->user['threadnum'].
(Traducido por abdonroda)
Si mañana ando de humor lo subo a MyBB :p