diff --git a/lib/rsslib.php b/lib/rsslib.php
index 8b637a3..2e03987 100644
--- a/lib/rsslib.php
+++ b/lib/rsslib.php
@@ -173,8 +173,16 @@ function rss_get_file_full_name($componentname, $filename) {
     return "$CFG->cachedir/rss/$componentname/$filename.xml";
 }
 
-function rss_get_file_name($instance, $sql) {
-    return $instance->id.'_'.md5($sql);
+function rss_get_file_name($instance, $sql, $params = null) {
+    $param_str = '';
+
+    if (is_array($params)) {
+        foreach ($params as $key => $value) {
+            $param_str .= $key.':'.$value;
+        }
+    }
+
+    return $instance->id.'_'.md5($sql.$param_str);
 }
 
 /**
diff --git a/mod/forum/rsslib.php b/mod/forum/rsslib.php
index 044896c..d1532e7 100644
--- a/mod/forum/rsslib.php
+++ b/mod/forum/rsslib.php
@@ -58,15 +58,15 @@ function forum_rss_get_feed($context, $args) {
     }
 
     //the sql that will retreive the data for the feed and be hashed to get the cache filename
-    $sql = forum_rss_get_sql($forum, $cm);
+    list($sql, $params) = forum_rss_get_sql($forum, $cm);
 
     // Hash the sql to get the cache file name.
     // If the forum is Q and A then we need to cache the files per user. This can
     // have a large impact on performance, so we want to only do it on this type of forum.
     if ($forum->type == 'qanda') {
-        $filename = rss_get_file_name($forum, $sql . $USER->id);
+        $filename = rss_get_file_name($forum, $sql . $USER->id, $params);
     } else {
-        $filename = rss_get_file_name($forum, $sql);
+        $filename = rss_get_file_name($forum, $sql, $params);
     }
 
     $cachedfilepath = rss_get_file_full_name('mod_forum', $filename);
@@ -80,7 +80,7 @@ function forum_rss_get_feed($context, $args) {
     $dontrecheckcutoff = time()-60;
     if ( $dontrecheckcutoff > $cachedfilelastmodified && forum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) {
         //need to regenerate the cached version
-        $result = forum_rss_feed_contents($forum, $sql, $modcontext);
+        $result = forum_rss_feed_contents($forum, $sql, $params, $modcontext);
         if (!empty($result)) {
             $status = rss_save_file('mod_forum',$filename,$result);
         }
@@ -115,81 +115,129 @@ function forum_rss_delete_file($forum) {
 function forum_rss_newstuff($forum, $cm, $time) {
     global $DB;
 
-    $sql = forum_rss_get_sql($forum, $cm, $time);
+    list($sql, $params) = forum_rss_get_sql($forum, $cm, $time);
 
-    $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
+    $recs = $DB->get_records_sql($sql, $params, 0, 1);//limit of 1. If we get even 1 back we have new stuff
     return ($recs && !empty($recs));
 }
 
+
+/**
+ *
+ * @param object $forum
+ * @param object $cm
+ * @param int $time
+ * @return array(string,array)|array(null,null)
+ */
 function forum_rss_get_sql($forum, $cm, $time=0) {
-    $sql = null;
+    $sql     = null;
+    $params  = null;
 
     if (!empty($forum->rsstype)) {
         if ($forum->rsstype == 1) {    //Discussion RSS
-            $sql = forum_rss_feed_discussions_sql($forum, $cm, $time);
+            list($sql, $params) = forum_rss_feed_discussions_sql($forum, $cm, $time);
         } else {                //Post RSS
-            $sql = forum_rss_feed_posts_sql($forum, $cm, $time);
+            list($sql, $params) = forum_rss_feed_posts_sql($forum, $cm, $time);
         }
     }
 
-    return $sql;
+    return array($sql, $params);
 }
 
-function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) {
-    global $CFG, $DB, $USER;
 
-    $timelimit = '';
+/**
+ *
+ * @param object $forum
+ * @param object $cm
+ * @param int $newsince
+ * @return array(string, array())
+ */
+function forum_rss_feed_discussions_sql($forum, $cm, $newsince = 0) {
+    global $CFG, $DB, $USER;
 
-    $modcontext = null;
+    $timelimit_sql = '';
+    $params = array();
 
     $now = round(time(), -2);
-    $params = array($cm->instance);
 
+    $modcontext = null;
     $modcontext = context_module::instance($cm->id);
 
     if (!empty($CFG->forum_enabletimedposts)) { /// Users must fulfill timed posts
         if (!has_capability('mod/forum:viewhiddentimedposts', $modcontext)) {
-            $timelimit = " AND ((d.timestart <= :now1 AND (d.timeend = 0 OR d.timeend > :now2))";
+            $timelimit_sql = " AND ((d.timestart <= :now1 AND (d.timeend = 0 OR d.timeend > :now2))";
             $params['now1'] = $now;
             $params['now2'] = $now;
+
             if (isloggedin()) {
-                $timelimit .= " OR d.userid = :userid";
+                $timelimit_sql .= " OR d.userid = :userid";
                 $params['userid'] = $USER->id;
             }
-            $timelimit .= ")";
+            $timelimit_sql .= ")";
         }
     }
 
     //do we only want new posts?
     if ($newsince) {
-        $newsince = " AND p.modified > '$newsince'";
-    } else {
-        $newsince = '';
+        $newsince_sql = " AND p.modified > :newsince";
+        $params['newsince'] = $newsince;
+    }
+    else {
+        $newsince_sql = '';
     }
 
     //get group enforcing SQL
     $groupmode    = groups_get_activity_groupmode($cm);
     $currentgroup = groups_get_activity_group($cm);
-    $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
 
-    if ($groupmode && $currentgroup) {
-        $params['groupid'] = $currentgroup;
-    }
+    list($group_sql, $group_params)  = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
+    $params = array_merge($params, $group_params);
 
-    $forumsort = "d.timemodified DESC";
-    $postdata = "p.id AS postid, p.subject, p.created as postcreated, p.modified, p.discussion, p.userid, p.message as postmessage, p.messageformat AS postformat, p.messagetrust AS posttrust";
-
-    $sql = "SELECT $postdata, d.id as discussionid, d.name as discussionname, d.timemodified, d.usermodified, d.groupid, d.timestart, d.timeend,
-                   u.firstname as userfirstname, u.lastname as userlastname, u.email, u.picture, u.imagealt
-              FROM {forum_discussions} d
-                   JOIN {forum_posts} p ON p.discussion = d.id
-                   JOIN {user} u ON p.userid = u.id
-             WHERE d.forum = {$forum->id} AND p.parent = 0
-                   $timelimit $groupselect $newsince
-          ORDER BY $forumsort";
-    return $sql;
+
+    $sql = "SELECT p.id AS postid,
+                   p.subject AS subject,
+                   p.created AS postcreated,
+                   p.modified AS modified,
+                   p.discussion AS discussion,
+                   p.userid AS userid,
+                   p.message AS postmessage,
+                   p.messageformat AS postformat,
+                   p.messagetrust AS posttrust,
+                   d.id AS discussionid,
+                   d.name AS discussionname,
+                   d.timemodified AS timemodified,
+                   d.usermodified AS usermodified,
+                   d.groupid AS groupid,
+                   d.timestart AS timestart,
+                   d.timeend AS timeend,
+                   u.firstname AS userfirstname,
+                   u.lastname AS userlastname,
+                   u.email AS email,
+                   u.picture AS picture,
+                   u.imagealt AS imagealt
+            FROM {forum_discussions} d
+                 JOIN {forum_posts} p ON p.discussion = d.id
+                 JOIN {user} u ON p.userid = u.id
+            WHERE d.forum = :forum_id AND
+                  p.parent = 0
+                  {$timelimit_sql}
+                  {$group_sql}
+                  {$newsince_sql}
+            ORDER BY d.timemodified DESC";
+
+    $params['forum_id'] = $forum->id;
+
+    return array($sql, $params);
 }
 
+
+/**
+ *
+ * @param object $forum
+ * @param object $cm
+ * @param int $newsince
+ * @return array(string, array)
+ */
 function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
     $modcontext = context_module::instance($cm->id);
 
@@ -197,17 +245,16 @@ function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
     $groupmode    = groups_get_activity_groupmode($cm);
     $currentgroup = groups_get_activity_group($cm);
 
-    $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
-
-    if ($groupmode && $currentgroup) {
-        $params['groupid'] = $currentgroup;
-    }
+    $params = array();
+    list($group_sql, $params) = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
 
     //do we only want new posts?
     if ($newsince) {
-        $newsince = " AND p.modified > '$newsince'";
-    } else {
-        $newsince = '';
+        $newsince_sql = " AND p.modified > :newsince";
+        $params['newsince'] = $newsince;
+    }
+    else {
+        $newsince_sql = '';
     }
 
     $sql = "SELECT p.id AS postid,
@@ -221,20 +268,31 @@ function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
                  p.created AS postcreated,
                  p.messageformat AS postformat,
                  p.messagetrust AS posttrust
-            FROM {forum_discussions} d,
-               {forum_posts} p,
-               {user} u
-            WHERE d.forum = {$forum->id} AND
-                p.discussion = d.id AND
-                u.id = p.userid $newsince
-                $groupselect
+            FROM {forum_discussions} d
+                 JOIN {forum_posts} p ON p.discussion = d.id
+                 JOIN {user} u ON u.id = p.userid
+            WHERE d.forum = :forum_id
+                 $newsince_sql
+                 $group_sql
             ORDER BY p.created desc";
 
-    return $sql;
+    $params['forum_id'] = $forum->id;
+
+    return array($sql, $params);
 }
 
+
+/**
+ *
+ * @param object $cm
+ * @param int $groupmode
+ * @param int $currentgroup
+ * @param object $modcontext
+ * @return array(string, array)
+ */
 function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=null) {
     $groupselect = '';
+    $params      = array();
 
     if ($groupmode) {
         if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
@@ -253,7 +311,7 @@ function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=nul
         }
     }
 
-    return $groupselect;
+    return array($groupselect, $params);
 }
 
 
@@ -268,13 +326,11 @@ function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=nul
  * @param object $context the context this forum relates to
  * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
  */
-function forum_rss_feed_contents($forum, $sql, $context) {
+function forum_rss_feed_contents($forum, $sql, $params, $context) {
     global $CFG, $DB, $USER;
 
     $status = true;
 
-    $params = array();
-    //$params['forumid'] = $forum->id;
     $recs = $DB->get_recordset_sql($sql, $params, 0, $forum->rssarticles);
 
     //set a flag. Are we displaying discussions or posts?
