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

2945


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.

Post Pages - Post Inline - WIRIS

Introduction

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 moodle.org 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 Moodle.org, 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 ''
.$fullname.'';
 echo "\n";

Add the code in red.

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

Right ABOVE this code paste in…


$sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'forum_posts 
AS p, ' .$CFG->prefix.'forum_discussions 
AS d WHERE p.discussion=d.id 
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
.$postcount.'';
 $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: http://tracker.moodle.org/browse/MDL-5875

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='.
 $post->userid.'&course='.$course->id.'">'.$fullname
 .$postcount.'';
 $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', '', 
count_words(format_text($tempMsg, 
$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

From http://moodle.org/mod/data/view.php?d=13&rid=1159


 

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 http://www.webtolife.co.nz/content/hyper-forum-documentatoin 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.

Array("","orange","limegreen","orangered","orchid","lightskyblue","khaki","salmon","peru","darkblue","gold","red","mediumorchid","mediumseagreen","mediumslateblue","palegreen","indianred","lavender","yellow","burlywood","lightsteelblue","lime","magenta","lightpink","darkslateblue","gray","royalblue","yellowgreen","darkorchid","cyan","indigo","lightgrey","crimson","bisque","lightseagreen","blue","darkorange","deeppink","seagreen","lightblue","purple","blanchedalmond","goldenrod","mediumvioletred","steelblue","pink","chartreuse","darkred","darkkhaki","plum","olivedrab","mediumturquoise","maroon","lightsalmon","dodgerblue","firebrick","greenyellow","violet","darkcyan","skyblue","peachpuff","mediumblue","lightcoral","dimgray","wheat","springgreen","slateblue","darkmagenta","deepskyblue","chocolate","palevioletred","silver","darkgoldenrod","darkolivegreen","blueviolet","coral","cornflowerblue","lawngreen","lightgoldenrodyellow","olive","rosybrown","tomato","darkseagreen","darkviolet","paleturquoise","fuchsia","green","brown","mediumspringgreen","mediumpurple","navy","thistle","darkgreen","darksalmon","aqua","saddlebrown","hotpink","navajowhite","cadetblue","sienna","midnightblue","tan","teal","palegoldenrod","lightgreen","darkgray","black","turquoise","moccasin","lightslategray","antiquewhite","darkturquoise","aquamarine","darkslategray","mistyrose","slategray","forestgreen","mediumaquamarine","papayawhip","powderblue","gainsboro");

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]