Spicing up the Forum module for Moodle 1.9 with a word and post count.

--- Advertisement ---

Connected 2023
Connected Conference OpenLMS

Totara LMS supports the work of Frankie Kam as he explores the many possibilities of Moodle for fun and to improve his college’s course site.


Good things come in threes. For example Love, Hope and Charity. What else?  Nasi Lemak, Roti Canai and Char Kuey Teow (yummy Malaysian food).  In this post, I would like to introduce you to food, err…to three ways in which I spiced up my Moodle 1.9 forum activity.

The forum module is, in my humble opinion, the workhorse of Moodle. At least for it is! But seriously, which teacher wouldn’t be proud of her students when a forum is full of verbose posts and discussions? Good forum discussions breed and promote critical thinking and writing skills. So what more would you that isn’t already provided by Moodle’s forum activity?

Learning Outcomes

At the end of this post, you will know how to add a post count and a word count to your forum. You will also know how to add a context colour rectangle to better link forum post hierarchies. See the image below.

Do you like what you see? If your answer is yes, then read on! You can setup your Moodle forum likewise.

Note: the code below is using Moodle 1.9.15.

I. “Add a post count to each user” hack by John White

In this 2009 post in, John White gave the code that displays a post count next to the user’s name in a post’s header section.

Open your (moodle)/mod/forum/lib.php file with your text editor.

Search for…

function forum_print_discussion_header(

Scroll down to find the below code.

// User name
 $fullname = fullname($post, has_capability
('moodle/site:viewfullnames', $modcontext));
 echo '';
 echo ''
 echo "\n";

Add the code in red.

// User name
 $fullname = fullname($post,
has_capability('moodle/site:viewfullnames', $modcontext));
 echo '';
 echo ''
 echo "</td>\n";

Right ABOVE this code paste in…

$sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'forum_posts 
AS p, ' .$CFG->prefix.'forum_discussions 
AND d.course='.$forum->course.' 
AND p.userid='.$postuser->id;

$postcount = ($forum->assessed > 0) ? ' (' 
.count_records_sql($sql).' posts)' : ''; 
//If you want to count all posts - assessed and not assessed, 
//then comment the line above and uncomment the line below! 
//$postcount = ' ('.count_records_sql($sql).' posts)' : '';

Next, look for the function: function forum_print_post( Do the same thing, which is to add the code in red.

echo '
‘; $fullname = fullname($postuser, $cm->cache->caps[‘moodle/site:viewfullnames’]); $by = new object(); $by->name = ”. $fullname
 $by->date = userdate($post->modified);
 print_string('bynameondate', 'forum', $by);
 echo '';

Paste the same sql block immediately ABOVE this one as well.

In both the View and Discussion pages, if the forum is set to be ‘assessed’ (that is: it uses ratings aggregation of any kind), then this results in something like…

The number that appears within the round brackets is the user’s postings count for all forums within the same course. The count shows after the user’s name.

 So that’s the first way to spice up the forum.

II. Show the word count on forum posts

How would you like to have the forum post display a word count? With the code below, you can have your Moodle forum display a word count that is as accurate as Ms Word’s word count.

Now how useful  could this be? Well, if the teacher gave a forum exercise that included a word count, the students could refer to this value in a Moodle forum, instead of using a web-based  word count or by copying and pasting into Ms. Word!
Refer to:

How to add the word count. Firstly, look for this code in /mod/forum/lib.php which should be located inside the forum_print_post() function.

 $by = new object();
 $by->name = 'wwwroot.'/user/view.php?id='.
 $by->date = userdate($post->modified);
 // This, I presume, is to localize string display.
 //Unfortunately it's unstructured text, so I can't insert
 // the necessary <abbr> element with the date.
 // Instead that's done in a hidden tag above.
 print_string('bynameondate', 'forum', $by);
 $options = new object();
 $options->para = false;
 $options->trusttext = true;

Next, add the below red colour code immediately after the $options->trusttext = true;  code line:

$tempMsg = $post->message; 
$tempMsg = str_replace(",","",$tempMsg); 
$tempMsg = str_replace("'","",$tempMsg); 
$tempMsg = str_replace(". "," ",$tempMsg); 
$tempMsg = str_replace(".","",$tempMsg); 
$tempMsg = str_replace(", "," ",$tempMsg); 
$tempMsg = str_replace("’","",$tempMsg); 
$tempMsg = str_replace("`","",$tempMsg); 
$tempMsg = str_replace("]","",$tempMsg); 
$tempMsg = str_replace("[","",$tempMsg); 
$tempMsg = str_replace("(","",$tempMsg); 
$tempMsg = str_replace(")","",$tempMsg); 
$tempMsg = str_replace("-","",$tempMsg); 
$tempMsg = str_replace("
“,” “,$tempMsg);
$tempMsg = str_replace(""," ",$tempMsg); 
$tempMsg = str_replace(" "," ",$tempMsg); 
$tempMsg = str_replace(""," ",$tempMsg); 
$tempMsg = str_replace("

“,” “,$tempMsg);

$tempMsg = str_replace("
"," ",$tempMsg); 
$tempMsg = str_replace("__"," ",$tempMsg); 
$tempMsg = str_replace("_"," ",$tempMsg); 
$tempMsg = str_replace("""," ",$tempMsg); 
$tempMsg = str_replace("<"," ",$tempMsg); 
$tempMsg = str_replace(">"," ",$tempMsg); 
$tempMsg = str_replace("&"," ",$tempMsg); 
$tempMsg = strip_tags($tempMsg); 
echo ' ('.get_string('numwords', '', 
$post->format, $options, $course->id))).')';

The result is a word count that is not too far off from the result of doing a word count with Microsoft Word’s word count feature. Give it a try.

III. Add context and improving readability of Moodle forums with Aaron Fulton’s Hyperforum


Hyper Forum is an addon that improves the readability of the Moodle forums. The purpose of this tool is to allow the users to be able to better follow complex threads. This is achieved by colour coding each post according the threads it is related to. Hyper Forum adds two coloured blocks onto each post. The first block indicates the parent of the current post, and the second indicates which posts are children (replies to) the current post. Each colour is also a link, and clicking on the link will hide all unrelated posts enabling easy reading of a particular thread.

How to install Hyperforum.

From Download the hyperforum.js file from the downloads page and save it into your current theme directory.

Moodle 1.9 or lower

Open the header.html file of your current theme and locate this line:

javascript"); ?>

Insert this line just after the line above:

 . current_theme() ?>/hyperforum.js"  type="text/javascript"></script>

Moodle 2.0 higher

Open the layout/general.php file of your current theme and locate this line:

<?php echo $OUTPUT->standard_head_html() ?>

Insert this line just after the line above:

 . current_theme() ?> /hyperforum.js"  type="text/javascript"></script>

That’s it! Your forums should now be more readable! You might be saying to yourself, “The colours are too bright, and they clash with my theme’s colours”. Well, if you don’t like the colour scheme of the rectangles, you can always customise the colours by editing hyperforum.js by substituting the color names with colour hexadecimal codes. For example, here’s the original code inside hyperforum.js that is responsible for setting the colours of the rectangles.


You can customise the colours to your Moodle site’s theme colours. For example, using this code (which uses colours that are from my Moodle site’s pallete – range of colours)…

Array("","#00CC00", "#006F2E", "#002553", "#6F002E", "#B90009");

…gives me this:

Do you notice that the context-rectangles have lost the gaudy-looking red, yellow and purple hues? Well, have fun customizing the colours to match your site’s theme!

That’s all for this post. I hope you that you have found it useful in making your Moodle forums more interesting and slightly more immersive.

Until the next time, happy Moodling,
Frankie Kam
Melaka, Malaysia
[email protected]

4 Responses

  1. FIRST AND FOREMOST, PLEASE BACKUP your (moodle)/mod/forum/lib.php file! It is fairly large (250+ Kb), and depending on your Internet speed, we wouldn’t want to take chances of you losing the original code. So at the risk of repeating myself, “Please backup your file before making any changes to it!”.

  2. I changed the sql query used to count the posts so that only posts in a single forum are counted and nott all forum posts in a course.
    My query looks like this:

    $sql = ‘SELECT COUNT(*) FROM ‘.$CFG->prefix.’forum_posts AS p, ‘.$CFG->prefix.’forum_discussions AS d
    WHERE AND d.course=’.$forum->course.’ AND p.userid=’.$postuser->id.’ AND’.$forum->id;

    as opposed to the original:

    $sql = ‘SELECT COUNT(*) FROM ‘.$CFG->prefix.’forum_posts AS p, ‘.$CFG->prefix.’forum_discussions AS d
    WHERE AND d.course=’.$forum->course.’ AND p.userid=’.$postuser->id;

    Hope this helps somebody

  3. Revision to my post above
    In order to post correct post count for each individual discussion for Simple and Standard forums I made the following changes t the SQL block given in the article:

    In the function function forum_print_post( paste the following code

    $sql = ‘SELECT COUNT(*) FROM ‘.$CFG->prefix.’forum_posts AS p, ‘.$CFG->prefix.’forum_discussions AS d WHERE AND d.course=’.$forum->course.’ AND p.userid=’.$postuser->id.’ AND’.$forum->id.’ AND p.discussion=’.$post->discussion;

    For the function forum_print_discussion_header( use the following

    $sql = ‘SELECT COUNT(*) FROM ‘.$CFG->prefix.’forum_posts AS p, ‘.$CFG->prefix.’forum_discussions AS d WHERE AND d.course=’.$forum->course.’ AND p.userid=’.$postuser->id.’ AND’.$forum->id;

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dive Deeper On

The Latest

The eLearn Podcast

Connected Conference OpenLMS

--- Advertisement ---

Post Pages - Sidebar 4 - CourseMerchant

--- Advertisement ---

Post Pages - Sidebar 7 - Titus Learning

--- Advertisement ---

Post Pages - Sidebar 5 - Edwiser (RemUI)

Subscribe to our newsletter

Education technology has the power to change lives. 

To get the latest news, information and resources about online learning from around the world by clicking on the button below.