Implementing a Moderation System in a Movable Type Weblog

Recently I added a Slashdot like moderation system to my home made weblogging software. The motivation was to add a simple way of offering your opinion on an entry without requiring a comment when you don’t have one. This grabbed the attention of Etan over at http://www.toomuchsexy.org/; who desired an implementation for Movable Type. After some work last night and this morning I believe I have perfected the Movable Type implementation.

What you need:

A Movable Type weblog, obviously.
Access to a MySQL management system, such as phpMyAdmin.
A web server running PHP.

These instructions are based on a MT installation using MySQL and with a .php file extension. If you use a .html extension adding the following line to a .htaccess file in your web root folder will make the .html files be parsed by the PHP parser.

AddType application/x-httpd-php .html

See http://www.akamarketing.com/htaccess.html; and http://www.buildwebsite4u.com/advanced/htaccess-file.shtml; for more information on the .htaccess file.

Step 1: Creating the ratings table in MySQL

In phpMyAdmin select the MySQL database that contains your MT tables.
Choose SQL from the menu at the top and paste the following into the text field and submit it.

CREATE TABLE `ratings` (
`id` bigint(20) NOT NULL auto_increment,
`uid` bigint(20) NOT NULL default ‘0′,
`rate` varchar(4) NOT NULL default ‘0′,
`reason` varchar(50) NOT NULL default ”,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1

That is it for adding things to the MySQL database. Now on to step 2.

Step 2: The PHP Add Script

This step involves creating a php script file that will handle adding user mods to the database created in step 1.

Open a text editor and copy and paste the following code into a new file.

<?php
$usr = “SQLUSERNAME”;
$pwd = “SQLPASSWORD”;
$db = “SQLDBNAME”;
$host = “localhost”;
$connection_id=mysql_connect($host,$usr,$pwd)
mysql_select_db($db);
$original_post_id=$_POST["uid"];
$rating=$_POST["rating"];
$reason=htmlentities(strip_tags($_POST["reason"]));
$SQL=”INSERT INTO `ratings` VALUES (”, ‘$original_post_id’, ‘$rating’, ‘$reason’);”;
if (!mysql_query($SQL, $connection_id)) { echo(mysql_error()); }
mysql_close($connection_id);
header(”Location: “.$_SERVER['HTTP_REFERER']);
?>

Replace SQLUSERNAME with the username used to access the MySQL database.

Replace SQLPASSWORD with the password used to access the MySQL database.

Replace SQLDBNAME with the name of MySQL database that houses the table created in Step 1, should be the same one as your MT database.

Save this file as addrating.php and save it in the same directory as your MovableType weblog. IE your weblog url is http://server.com/mt/index.php save it in http://server.com/mt/.

No special permissions are needed for this file. This step is done, now to Step 3.

Step 3: Creating the Rating Form

This step involves creating a Movable Type template module that contains the form code used to add a moderation to the database.

From the MT weblog administration panel choose “Templates” from the left side menu. scroll down to “Template Modules” and click on “Create a new template module” to create a new module.

Name the module “ratingform” and paste the following code into the “Module Body” field.

<form method=”post” action=”<$MTBlogURL$>addrating.php”>
<p><select id=”rating” size=”1″ name=”rating”>
<option label=”+1″ value=”+1″>+1</option>
<option label=”0″ value=”0″ selected=”selected”>0</option>
<option label=”-1″ value=”-1″>-1</option>
</select>
Reason: <input type=”text” id=”reason” name=”reason” size=”10″ maxlength=”50″ />
<input type=”hidden” name=”uid” value=”<$MTEntryID$>” /><input type=”hidden” name=”func” value=”Rating” />
<input type=”submit” name=”Submit” value=”Rate Me” title=”Send Rating”/></p>
</form>

Save the module and go back to the templates page, there is no need to rebuild your site yet.

Step 4: Creating the “Calculate Rating” Module

This step involves creating a template module that caluculates the total +/- rating for the entry.
Create a new template module and name it “calcrating” and paste the following code into the Module Body.

<?php
$usr = “SQLUSERNAME”;
$pwd = “SQLPASSWORD”;
$db = “SQLDBNAME”;
$host = “localhost”;
$connection_id=mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
$RateSQL=”SELECT `id` FROM `ratings` WHERE `uid` = <$MTEntryID$>”;
$num_of_rates=mysql_num_rows(mysql_query($RateSQL, $connection_id));
if ($num_of_rates != 0 )
{
$Plus1RateSQL=”SELECT `id` FROM `ratings` WHERE `uid` = <$MTEntryID$> AND `rate` = ‘+1′”;
$num_of_plus1rates=mysql_num_rows(mysql_query($Plus1RateSQL, $connection_id));
$Min1RateSQL=”SELECT `id` FROM `ratings` WHERE `uid` = <$MTEntryID$> AND `rate` = ‘-1′”;
$num_of_min1rates=mysql_num_rows(mysql_query($Min1RateSQL, $connection_id));
$cur_rate=($num_of_plus1rates - $num_of_min1rates);
if ($num_of_plus1rates > $num_of_min1rates) {$cur_rate=”+”.$cur_rate;}
}
elseif ($num_of_rates==0) { $cur_rate=0;}
echo $cur_rate;
?>

Replace SQLUSERNAME with the username used to access the MySQL database.

Replace SQLPASSWORD with the password used to access the MySQL database.

Replace SQLDBNAME with the name of MySQL database that houses the table created in Step 1, should be the same one as your MT database.

Save the module and go back to the templates page, there is no need to rebuild your site yet.

Step 5: Creating the “Rating List” Module

This step involves creating a template module that builds a list of the ratings and the reason given for the rating, in other words the moderation.
Create a new template module and name it “ratinglist” and paste the following code into the Module Body.

<?php
$usr = “SQLUSERNAME”;
$pwd = “SQLPASSWORD”;
$db = “SQLDBNAME”;
$host = “localhost”;
$connection_id=mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
$RateSQL=”SELECT * FROM `ratings` WHERE `uid` = <$MTEntryID$> ORDER BY `id` DESC”;
$raw_rate_data=mysql_query($RateSQL, $connection_id);
while ($row=mysql_fetch_array($raw_rate_data))
{
$rate_rate=$row['rate'];
$rate_reason=$row['reason'];
$rate_data.=”<li>$rate_rate: $rate_reason</li>\n”;
}
$rate_data=”<ul>\n”.$rate_data.”</ul>\n”;
echo $rate_data;
?>

Replace SQLUSERNAME with the username used to access the MySQL database.

Replace SQLPASSWORD with the password used to access the MySQL database.

Replace SQLDBNAME with the name of MySQL database that houses the table created in Step 1, should be the same one as your MT database.

Save the module and go back to the templates page, there is no need to rebuild your site yet.

Step 6: Adding It To Your Templates

Now that we’ve gone through and created all these modules it’s time to put them to use. Let’s start with an example usage of the “calcrating” module. A default MT weblog has a byline that displays Author, Time, Comments Count and Trackback Coount. This is a perfect place to add a rating, as would be next to the title.

To get this paste this one line <$MTInclude module=”calcrating”$> into the Main Index template after the </MTEntryIfAllowPings> code. Save the template and rebuild your site. Now in the byline the total rating (+3, -1, etc) will be displayed right along with the other info.

Use <$MTInclude module=”calcrating”$> anytime you want to show the totla rating for an entry.

To display the ratings form just add <$MTInclude module=”ratingform”$> to the individual archive page in a place that makes sense, above or below the comments for example.

To display the list of ratings and reasons add this line <$MTInclude module=”ratinglist”$> to where it makes sense, a side bar, or right above or below the ratings form.

Notes:
If you already use PHP with your MT weblog to interact with the MySQL backend and have a connection file you can use that and adjust the variables used above.

For Step 5 you can easily edit the formatting of the output if you don’t want it as a list. Delete this line $rate_data=”<ul>\n”.$rate_data.”</ul>\n”; and change <li> and </li> to the tags you want to use.

19 Responses to “Implementing a Moderation System in a Movable Type Weblog”

  1. TooMuchSexy.blog Says:
    Adding timestamps and banning to your Movable Type Rating System
    After adding a moderation system to Movable Type I was quickly presented with trolls whose only purpose in life is to boil the blood of random strangers. My solution was to modify James’ Rating System to include IP addresses, IP banning and timestamps….

  2. Etan Says:

    As you can see by my ping, I have written up a guide for adding IP-banning and timestamps to this rating system. Feel free to drop by, read it, and give me any suggestions.

    Thanks!

  3. TooMuchSexy.blog Says:
    Adding timestamps and banning to your Movable Type Rating System
    Introduction After adding a moderation system to Movable Type I was quickly presented with trolls whose only purpose in life is to boil the blood of random strangers. My solution was to modify James’ Rating System to include IP addresses, IP banning an…

  4. James Says:

    This is the author here with a security hole fix.

    In Step 2 change

    $reason=$_POST["reason"];

    to

    $reason=htmlentities(strip_tags($_POST["reason"]));

    Saw that after I submitted it.

  5. Jennifer Says:

    Just edited the post. Thanks! :)

  6. them.ws :: Where Ideas Come To Die Says:
    It Spreads
    Okay, spreading isn’t the right term. I only know of one site using it, but my Mod Rating script has had a modification to

  7. Daphne Says:

    Very cool idea:) I wonder how you’d do one more like Karma… or really, Props - where you caould just make a quice click to add +s but not negatives… like at Xanga.

  8. Etan Says:

    Daphne, you can just modify the rating form and the calculation rating module to have 0,+1,+2 to make this more like eProps.

  9. LibraryPlanet.com Says:
    Blog as Forum
    If you are undecided whether you want a blog or a forum, take a look at what Mark Carey has…

  10. gemini Says:

    do u think u could come up with a hack for b2 with this?

  11. Etan Says:

    It should be relatively simple to modify this to work for b2, unfortunatley I have no access to b2.

    All you would need to do is replace <$MTEntryID$>, <$MTBlogURL$>, etc. with their respective values in b2.

    I don’t know how b2 deals with includes, but you could either use the entire module codes instead of using includes or use b2’s solution for <$MTInclude module=”"$>

  12. Daphne Says:

    Etan - you’re right *lol* that would certainly work. I was actually thinking more of a system like you find in GM - just a click up/down without going to a new page… I think, actually, the easiest way to do that would probably be to modify a mini-inline-poll like Technoerotica (?) had….

    Sorry, I’ve really kind of gotten off-topic.

    You’r script is great work, Etan - I really love the IP tracking addition, too!

  13. Daphne Says:

    Sorry, that credit goes to Etan and James - fantastic work, both of you:)

  14. The Trommetter Times Says:
    Rate My Blog
    This is a message for all the lurkers out there. You know who you are! There are a lot of…

  15. James Says:

    Updated the code slightly see it here: http://them.ws/?p=894

  16. The Trommetter Times Says:
    Rate My Blog
    This is a message for all the lurkers out there. You know who you are! There are a lot of…

  17. Mark Carey Says:

    I also posted these suggesttions on Etan site, but I wanted to make sure everyone see them. I see a lot of potential for this.

    Here are two enhancement suggestions for future versions:

    1) In addition to IP banning, it would be great to enforce a “one rating per IP address” rule to (automatically) prevent people from rating more than once on a particular entry.

    2) Rating comments. In addition to rating entries, it would be great if people could rate the comments themselves. Essentially each comment could be rated individually. Then you could implement a filter thats only shows comments “greater than -2”, etc. Of course this will be even more useful once MT supports vistor registrations, then you could calculate a comment “score” for each person (similar to the “monthly comment count” on this site).

  18. Cassiopaya Says:

    Hey there I set everything up like you said above and now I get this error when submitting a rating:

    Parse error: parse error, unexpected T_STRING in /home/www/htdocs/1daland.net/morsmordre/addrating.php on line 7

    I just changed my DBname, PW and DBUsername…can you tell me what could be wrong?

  19. Lauren Noelle Says:

    Hi. Your site is great. Could I somehow restrict who can rate, like with TypeKey or a member script?