I wrote a quick mod for something I needed on a site, figured I would share it here.
This mod will display a list of related stories to the one you are currently viewing. The relevance is done based on tags (the more tags in common the more relevant) and is limited to 10 stories. It ain't pretty and it is not in a module but it works.
1. Add the template declarations to story.php in the root directory of you pligg install
PHP Code:
// for show who voted
$main_smarty->assign('user_url', getmyurl('userblank', ""));
$main_smarty->assign('voter', who_voted($id, 'small'));
// for show related stories
$main_smarty->assign('related_title_url', getmyurl('storytitle', ""));
$main_smarty->assign('related_story', related_stories($id, $link->tags, $link->category));
2. Add the needed function in html1.php located in /libs
PHP Code:
function related_stories($storyid, $related_tags, $category){
// this returns similar stories based on tags in common and in the same category
global $db;
$related_tags="'".str_replace(", ","', '",$related_tags)."'"; // This gives us the proper string structure for IN SQL statement
// Select 10 stories that share tags with the current story and order them by number of tags they share
$sql = "SELECT ".table_links.".link_title, ".table_links.".link_title_url, COUNT( ".table_tags.".tag_link_id ) AS relevance, ".table_tags.".tag_link_id FROM ".table_tags.", ".table_links." WHERE ".table_tags.".tag_words IN ( ".$related_tags." ) AND ".table_tags.".tag_link_id = ".table_links.".link_id AND ".table_links.".link_status = 'published' AND NOT ".table_links.".link_id = ".$storyid;
//comment the following line out if you want this to work accross categories
$sql.= " AND ".table_links.".link_category = ".$category;
$sql.= " GROUP BY ".table_tags.".tag_link_id, ".table_links.".link_title, ".table_links.".link_title_url ORDER BY relevance DESC LIMIT 10";
$related_stories = mysql_query($sql);
$related_story = array();
while ($rows = mysql_fetch_array ($related_stories, MYSQL_ASSOC)) array_push ($related_story, $rows);
return $related_story;
}
3. Make it show up in your templates. The following is based on the future pligg template so it will need adaptation.
First of you need to add a new declaration to lang.conf located in /libs
Code:
PLIGG_Visual_Story_RelatedStory = "Related Stories"
Code:
{config_load file="/libs/lang.conf"}
<div id="comments">
<h2>{#PLIGG_Visual_Story_RelatedStory#}</h2>
<div class = "whovotedwrapper" id = "relatedstories">
<ol
{section name=nr loop=$related_story}
><li><a href = "{$related_title_url}{$related_story[nr].link_title_url}">{$related_story[nr].link_title}</a><br/></li
{/section}
></ol><br/>
</div>
</div>
{php}
?>
<style type="text/css">
/* allow room for 3 columns */
div#idwhovotedwrapper ol
{
width: 35em;
list-style-type: none;
}
/* float & allow room for the widest item */
div#idwhovotedwrapper ol li
{
float: left;
width: 10em;
}
/* stop the float */
div#idwhovotedwrapper br
{
clear: left;
}
/* separate the list from subsequent markup */
div#idwhovotedwrapper div.whovotedwrapper
{
margin-bottom: 1em;
}
</style>
<?php
{/php}
Code:
{include file=$the_template."/story_related_stories.tpl"}
That's it, hope someone finds it useful.
Bert






Works like a charm now. All of the stories were on the upcoming page before.
Linear Mode




