# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/lib/rsslib.php
--- moodle/lib/rsslib.php Base (1.69)
+++ moodle/lib/rsslib.php Locally Modified (Based On 1.69)
@@ -30,7 +30,6 @@
  * @global object
  */
 function rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext='') {
-
     global $OUTPUT;
 
     static $rsspath = '';
@@ -45,18 +44,22 @@
     $rsspix = $OUTPUT->pix_url('i/rss');
 
     return '<a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a>';
-
 }
 
 /**
  * This function returns the URL for the RSS XML file.
  *
  * @global object
+ * @param int coursid the course id
+ * @param int userid the current user id
+ * @param string modulename the name of the current module. For example "forum"
+ * @param int id For modules, module instance id
  */
 function rss_get_url($courseid, $userid, $modulename, $id) {
     global $CFG;
     require_once($CFG->libdir.'/filelib.php');
-    return get_file_url($courseid.'/'.$userid.'/'.$modulename.'/'.$id.'/rss.xml', null, 'rssfile');
+    $usertoken = rss_get_user_token($userid);
+    return get_file_url($courseid.'/'.$usertoken.'/'.$modulename.'/'.$id.'/rss.xml', null, 'rssfile');
 }
 
 /**
@@ -303,7 +306,7 @@
  * to be sent when a rss is required (file.php)
  * and something goes wrong
  */
-function rss_geterrorxmlfile() {
+function rss_geterrorxmlfile($errortype = 'rsserror') {
     global $CFG;
 
     $return = '';
@@ -317,7 +320,7 @@
         $item->title       = "RSS Error";
         $item->link        = $CFG->wwwroot;
         $item->pubdate     = time();
-        $item->description = get_string("rsserror");
+        $item->description = get_string($errortype);
         $return .= rss_add_items(array($item));
     }
 
@@ -329,6 +332,38 @@
     return $return;
 }
 
+function rss_get_userid_from_user_token($token) {
+    global $DB;
+    $record = $DB->get_record('external_tokens', array('token' => $token, 'tokentype' => EXTERNAL_TOKEN_RSS), 'userid', MUST_EXIST);
+    if ($record) {
+        return $record->userid;
+    }
+    return null;
+}
+
+function rss_get_user_token($userid) {
+    global $DB;
+    $token = null;
+    $record = $DB->get_record('external_tokens', array('userid' => $userid, 'tokentype' => EXTERNAL_TOKEN_RSS), 'token', IGNORE_MISSING);
+    if ($record) {
+        $token =  $record->token;
+    }
+    else {
+        //need to add a new RSS token for this user
+        $newtoken = new object();
+        $newtoken->token = $token = md5(uniqid(rand(),1));
+        $newtoken->tokentype = EXTERNAL_TOKEN_RSS;
+        $newtoken->userid = $userid;
+        $newtoken->externalserviceid = null;//what do we do about an external service id?
+        $newtoken->contextid = get_context_instance(CONTEXT_SYSTEM)->id;
+        $newtoken->creatorid = $userid;
+        $newtoken->timecreated = time();
+
+        $DB->insert_record('external_tokens', $newtoken);
+    }
+    return $token;
+}
+
 // ===== This function are used to write XML tags =========
 // [stronk7]: They are similar to the glossary export and backup generation
 // but I've replicated them here because they have some minor
