1
hipoonios
Random Comments Block

I want to have a random comment block on my site. Like the recent comments block that is in XOOPS core.

Anyone know how to do this?
I love Xoops!

2
trabis
Re: Random Comments Block
  • 2009/3/7 16:11

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


try edit system/blocks/system_blocks.php

around line 334 replace
$criteria->setSort('com_created');
by this
$criteria->setSort('rand()');

3
hipoonios
Re: Random Comments Block
  • 2009/3/10 20:08

  • hipoonios

  • Friend of XOOPS

  • Posts: 298

  • Since: 2005/9/24


Thanks, it works! But what about if I want to have both a recent and random comment block? I guess it's a bit more complicated.
I love Xoops!

4
trabis
Re: Random Comments Block
  • 2009/3/10 20:44

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Edit system/xoops_version.php and add extra block:
$modversion['blocks'][14]['file'] = "system_blocks.php";
$modversion['blocks'][14]['name'] = _MI_SYSTEM_BNAME11;
$modversion['blocks'][14]['description'] = "Shows most recent comments random";
$modversion['blocks'][14]['show_func'] = "b_system_comments_show_random";
$modversion['blocks'][14]['options'] = "10";
$modversion['blocks'][14]['edit_func'] = "b_system_comments_edit";
$modversion['blocks'][14]['template'] = 'system_block_comments.html';

then open system/blocks/system_blocks.php and add new function
function b_system_comments_show_random($options)
{
    
$block = array();
    include_once 
XOOPS_ROOT_PATH.'/include/comment_constants.php';
    
$comment_handler =& xoops_gethandler('comment');
    
$criteria = new CriteriaCompo(new Criteria('com_status'XOOPS_COMMENT_ACTIVE));
    
$criteria->setLimit(intval($options[0]));
    
$criteria->setSort('rand()');
    
$criteria->setOrder('DESC');

    
// Check modules permissions
    
global $xoopsUser;
    
$moduleperm_handler =& xoops_gethandler('groupperm');
    
$gperm_groupid is_object($xoopsUser) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS);
    
$criteria1 = new CriteriaCompo(new Criteria('gperm_name','module_read','='));
    
$criteria1->add(new Criteria('gperm_groupid''('.implode(','$gperm_groupid).')''IN'));
    
$perms $moduleperm_handler->getObjects($criteria1true);
    
$modIds = array();
    foreach(
$perms as $item) {
        
$modIds[] = $item->getVar('gperm_itemid');
    }
    if(
count($modIds) > ) {
        
$modIds array_unique($modIds);
        
$criteria->add(new Criteria('com_modid''('.implode(','$modIds).')''IN'));
    }
    
// Check modules permissions

    
$comments $comment_handler->getObjects($criteriatrue);
    
$member_handler =& xoops_gethandler('member');
    
$module_handler =& xoops_gethandler('module');
    
$modules $module_handler->getObjects(new Criteria('hascomments'1), true);
    
$comment_config = array();
    foreach (
array_keys($comments) as $i) {
        
$mid $comments[$i]->getVar('com_modid');
        
$com['module'] = '<a href="'.XOOPS_URL.'/modules/'.$modules[$mid]->getVar('dirname').'/">'.$modules[$mid]->getVar('name').'</a>';
        if (!isset(
$comment_config[$mid])) {
            
$comment_config[$mid] = $modules[$mid]->getInfo('comments');
        }
        
$com['id'] = $i;
        
$com['title'] = '<a href="'.XOOPS_URL.'/modules/'.$modules[$mid]->getVar('dirname').'/'.$comment_config[$mid]['pageName'].'?'.$comment_config[$mid]['itemName'].'='.$comments[$i]->getVar('com_itemid').'&amp;com_id='.$i.'&amp;com_rootid='.$comments[$i]->getVar('com_rootid').'&amp;'.htmlspecialchars($comments[$i]->getVar('com_exparams')).'#comment'.$i.'">'.$comments[$i]->getVar('com_title').'</a>';
        
$com['icon'] = htmlspecialchars$comments[$i]->getVar('com_icon'), ENT_QUOTES );
        
$com['icon'] = ($com['icon'] != '') ? $com['icon'] : 'icon1.gif';
        
$com['time'] = formatTimestamp($comments[$i]->getVar('com_created'),'m');
        if (
$comments[$i]->getVar('com_uid') > 0) {
            
$poster =& $member_handler->getUser($comments[$i]->getVar('com_uid'));
            if (
is_object($poster)) {
                
$com['poster'] = '<a href="'.XOOPS_URL.'/userinfo.php?uid='.$comments[$i]->getVar('com_uid').'">'.$poster->getVar('uname').'</a>';
            } else {
                
$com['poster'] = $GLOBALS['xoopsConfig']['anonymous'];
            }
        } else {
            
$com['poster'] = $GLOBALS['xoopsConfig']['anonymous'];
        }
        
$block['comments'][] =& $com;
        unset(
$com);
    }
    return 
$block;
}

Update system module and you will have new block.

This could be done adding extra option in block but it is easier for me this way!

Login

Donat-O-Meter

Stats
Goal: $15.00
Due Date: Sep 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $15.00
Make donations with PayPal!