Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

made some changes to allow rss feeds

  • Loading branch information...
commit 516194d0e84c8c9bdcfd4392e80dac317b1b1678 1 parent 7fe0ce4
toyomoyo authored
12 blog/class.BlogFilter.php
@@ -191,6 +191,18 @@ function fetch_entries($limit=true) {
191 191 $tagquerysql = '';
192 192 }
193 193
  194 + // if we have specified an ID
  195 + if ($this->postid) {
  196 +
  197 + if ($post = get_record('post', 'id', $this->postid)) {
  198 + $blogEntry = new BlogEntry($post);
  199 + $blogEntries[] = $blogEntry;
  200 +
  201 + $this->filtered_entries = $blogEntries;
  202 + return $this->filtered_entries;
  203 + }
  204 + }
  205 +
194 206 /****************************************
195 207 * depending on the type, there are 4 *
196 208 * different possible sqls *
29 blog/index.php
@@ -24,6 +24,7 @@
24 24 $courseid = optional_param('courseid',0,PARAM_INT);
25 25 $tag = s(urldecode(optional_param('tag', '', PARAM_NOTAGS)));
26 26 $tagid = optional_param('tagid', 0, PARAM_INT);
  27 +$postid = optional_param('postid',0,PARAM_INT);
27 28
28 29 $filtertype = optional_param('filtertype', '', PARAM_ALPHA);
29 30 $filterselect = optional_param('filterselect', 0, PARAM_INT);
@@ -122,29 +123,9 @@
122 123 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
123 124 error ('Blogs is not enabled');
124 125 }
125   - $canview = 0; //bad start
126   -
127   - $usercourses = get_my_courses($filterselect);
128   - foreach ($usercourses as $usercourse) {
129   - /// if viewer and user sharing same non-spg course, then grant permission
130   - if (groupmode($usercourse)!= SEPARATEGROUPS){
131   - if (isstudent($usercourse->id) || isteacher($usercourse->id)) {
132   - $canview = 1;
133   - }
134   - } else {
135   - /// now we need every group the user is in, and check to see if view is a member
136   - if ($usergroups = user_group($usercourse->id, $filterselect)) {
137   - foreach ($usergroups as $usergroup) {
138   - if (ismember($usergroup->id)) {
139   - $canview = 1;
140   - }
141   - }
142   - }
143   - }
144   - }
145   - if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) {
146   - error ('you can not view this user\'s blogs');
147   - }
  126 +
  127 + blog_user_can_view_user_post($filterselect);
  128 +
148 129 /// check to see if the viewer is sharing no_group, visible group course.
149 130 /// if not , check if the viewer is in any spg group as the user
150 131 break;
@@ -157,7 +138,7 @@
157 138 $limit = get_user_preferences('blogpagesize',8);
158 139 }
159 140
160   -$blogFilter =& new BlogFilter($userid, '', $limit, $start,$filtertype, $filterselect, $tagid, $tag);
  141 +$blogFilter =& new BlogFilter($userid, $postid, $limit, $start,$filtertype, $filterselect, $tagid, $tag);
161 142 //print_object($blogFilter); //debug
162 143
163 144 include($CFG->dirroot .'/blog/header.php');
41 blog/lib.php
@@ -9,6 +9,7 @@
9 9 require_once($CFG->dirroot .'/blog/class.BlogFilter.php');
10 10 require_once($CFG->libdir .'/blocklib.php');
11 11 require_once($CFG->libdir .'/pagelib.php');
  12 +require_once('rsslib.php');
12 13 require_once($CFG->dirroot .'/blog/blogpage.php');
13 14
14 15 /* blog access level constant declaration */
@@ -166,6 +167,8 @@ function blog_print_html_formatted_entries(&$blogFilter, $filtertype, $filtersel
166 167 $blogEntries = $blogFilter->get_filtered_entries();
167 168 // show page next/previous links if applicable
168 169 print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage');
  170 +
  171 + blog_rss_print_link($filtertype, $filterselect);
169 172 print '</div>';
170 173
171 174 if (blog_isLoggedIn()) {
@@ -333,4 +336,42 @@ function blog_applicable_publish_states($courseid='') {
333 336 return $options;
334 337 }
335 338
  339 +/// Checks to see if a user can view the blogs of another user.
  340 +/// He can do so, if he is admin, in any same non-spg course,
  341 +/// or spg group, but same group member
  342 +function blog_user_can_view_user_post($targetuserid) {
  343 +
  344 + $canview = 0; //bad start
  345 +
  346 + if (isadmin()) {
  347 + return true;
  348 + }
  349 +
  350 + $usercourses = get_my_courses($targetuserid);
  351 + foreach ($usercourses as $usercourse) {
  352 + /// if viewer and user sharing same non-spg course, then grant permission
  353 + if (groupmode($usercourse)!= SEPARATEGROUPS){
  354 + if (isstudent($usercourse->id) || isteacher($usercourse->id)) {
  355 + $canview = 1;
  356 + return $canview;
  357 + }
  358 + } else {
  359 + /// now we need every group the user is in, and check to see if view is a member
  360 + if ($usergroups = user_group($usercourse->id, $targetuserid)) {
  361 + foreach ($usergroups as $usergroup) {
  362 + if (ismember($usergroup->id)) {
  363 + $canview = 1;
  364 + return $canview;
  365 + }
  366 + }
  367 + }
  368 + }
  369 + }
  370 +
  371 + if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) {
  372 + error ('you can not view this user\'s blogs');
  373 + }
  374 +
  375 + return $canview;
  376 +}
336 377 ?>
409 blog/rsslib.php
... ... @@ -0,0 +1,409 @@
  1 +<?php
  2 +
  3 + require_once($CFG->dirroot.'/lib/rsslib.php');
  4 +
  5 +
  6 + // This function returns the icon (from theme) with the link to rss/file.php
  7 + // needs some hacking to rss/file.php
  8 + function blog_rss_print_link($filtertype, $filterselect, $tooltiptext='') {
  9 +
  10 + global $CFG, $USER;
  11 +
  12 + static $pixpath = '';
  13 + static $rsspath = '';
  14 + $rsspix = $CFG->pixpath .'/i/rss.gif';
  15 +
  16 + if ($CFG->slasharguments) {
  17 + $rsspath = $CFG->wwwroot.'/rss/file.php/blogs/'.$filtertype.'/'.$filterselect.'/rss.xml';
  18 + } else {
  19 + $rsspath = $CFG->wwwroot.'/rss/file.php?file=/blogs/'.$filtertype.'/'.$filterselect.'/rss.xml';
  20 + }
  21 + print '<div align="right"><a href="'. $rsspath .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="" /></a></div>';
  22 +
  23 + }
  24 +
  25 + // This file adds support to rss feeds generation
  26 + // This function is the main entry point to database module
  27 + // rss feeds generation. Foreach database with rss enabled
  28 + // build one XML rss structure.
  29 + function blog_rss_feeds() {
  30 +
  31 + blog_site_feeds(); //generate site level feeds, last 20 entries?
  32 + blog_course_feeds(); //generate all course level feeds, last 20 entries
  33 + blog_group_feeds(); //generate all group level feeds, last 20 entries
  34 + blog_user_feeds(); //generate all user level feeds, last 20 entries
  35 +
  36 + }
  37 +
  38 + /* Rss files for blogs
  39 + * 4 different ways to store feeds.
  40 + * site - $CFG->dataroot/rss/blogs/site/SITEID.xml
  41 + * course - $CFG->dataroot/rss/blogs/course/courseid.xml
  42 + * group - $CFG->dataroot/rss/blogs/group/groupid.xml
  43 + * user - $CFG->dataroot/rss/blogs/user/userid.xml
  44 + */
  45 + function blog_rss_file_name($type, $id) {
  46 + global $CFG;
  47 + $filename = "$CFG->dataroot/rss/blogs/$type/$id/rss.xml";
  48 + return $filename;
  49 + }
  50 +
  51 + //This function saves to file the rss feed specified in the parameters
  52 + function blog_rss_save_file($type, $id, $result) {
  53 + global $CFG;
  54 +
  55 + $status = true;
  56 +
  57 + if (! $basedir = make_upload_directory ('rss/blogs/'. $type.'/'.$id)) {
  58 + //Cannot be created, so error
  59 + $status = false;
  60 + }
  61 +
  62 + if ($status) {
  63 + $file = blog_rss_file_name($type, $id);
  64 + $rss_file = fopen($file, "w");
  65 + if ($rss_file) {
  66 + $status = fwrite ($rss_file, $result);
  67 + fclose($rss_file);
  68 + } else {
  69 + $status = false;
  70 + }
  71 + }
  72 + return $status;
  73 + }
  74 +
  75 +
  76 + // Only 1 view, site level feeds
  77 + function blog_site_feeds() {
  78 +
  79 + global $CFG;
  80 + $status = true;
  81 +
  82 + //////$CFG->debug = true;
  83 +
  84 + // Check CFG->enablerssfeeds.
  85 + if (empty($CFG->enablerssfeeds)) {
  86 + //Some debug...
  87 + if ($CFG->debug > 7) {
  88 + echo "DISABLED (admin variables)";
  89 + }
  90 + }
  91 +
  92 + // It's working so we start...
  93 + else {
  94 + // Iterate over all data.
  95 + $filename = blog_rss_file_name('site', SITEID); // RSS file
  96 + // Get the most recent 20 posts
  97 + $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p,
  98 + '.$CFG->prefix.'user u
  99 + WHERE p.userid = u.id
  100 + AND (p.publishstate = \'site\' OR p.publishstate = \'public\')
  101 + AND u.deleted = 0 ORDER BY lastmodified DESC LIMIT 0,20';
  102 +
  103 + $blogposts = get_records_sql($sql);
  104 +
  105 + // Now all the rss items.
  106 + $items = array();
  107 +
  108 + foreach ($blogposts as $blogpost) {
  109 + $item = null;
  110 + $temp = array();
  111 + array_push($temp, $blogpost);
  112 +
  113 + $user = get_record('user','id',$blogpost->userid);
  114 + $item->author = fullname($user);
  115 + $item->title = $blogpost->subject;
  116 + $item->pubdate = $blogpost->lastmodified;
  117 + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
  118 + $item->description = format_text($blogpost->summary, $blogpost->format);
  119 + array_push($items, $item);
  120 + }
  121 +
  122 + // First all rss feeds common headers.
  123 + $header = rss_standard_header(format_string('siteblog',true),
  124 + $CFG->wwwroot.'/blog/index.php',
  125 + format_string('intro',true));
  126 +
  127 + if (!empty($header)) {
  128 + $articles = rss_add_items($items);
  129 + }
  130 +
  131 + // Now all rss feeds common footers.
  132 + if (!empty($header) && !empty($articles)) {
  133 + $footer = rss_standard_footer();
  134 + }
  135 + // Now, if everything is ok, concatenate it.
  136 + if (!empty($header) && !empty($articles) && !empty($footer)) {
  137 + $rss = $header.$articles.$footer;
  138 +
  139 + //Save the XML contents to file.
  140 + $status = blog_rss_save_file('site', SITEID, $rss);
  141 + }
  142 + else {
  143 + $status = false;
  144 + }
  145 + }
  146 + return $status;
  147 + }
  148 +
  149 +
  150 + /// Generate the feeds for all courses
  151 + function blog_course_feeds() {
  152 +
  153 + $courses = get_records('course');
  154 + foreach ($courses as $course) {
  155 + if ($course->id != SITEID) {
  156 + blog_course_feed($course);
  157 + }
  158 + }
  159 + }
  160 +
  161 + // takes in course object from db
  162 + function blog_course_feed($course) {
  163 +
  164 + global $CFG;
  165 + $status = true;
  166 +
  167 + ////$CFG->debug = true;
  168 +
  169 + // Check CFG->enablerssfeeds.
  170 + if (empty($CFG->enablerssfeeds)) {
  171 + //Some debug...
  172 + if ($CFG->debug > 7) {
  173 + echo "DISABLED (admin variables)";
  174 + }
  175 + }
  176 +
  177 + // It's working so we start...
  178 + else {
  179 + // Iterate over all data.
  180 + $filename = blog_rss_file_name('course', $course->id); // RSS file
  181 + // Get the most recent 20 posts
  182 +
  183 + $sql = '(SELECT p.* FROM '.$CFG->prefix.'post p, '
  184 + .$CFG->prefix.'user_students u
  185 + WHERE p.userid = u.userid
  186 + AND u.course = '.$course->id.'
  187 + AND (p.publishstate = \'site\' OR p.publishstate = \'public\'))
  188 +
  189 + UNION
  190 +
  191 + (SELECT p.* FROM '.$CFG->prefix.'post p, '
  192 + .$CFG->prefix.'user_teachers u
  193 + WHERE p.userid = u.userid
  194 + AND u.course = '.$course->id.'
  195 + AND (p.publishstate = \'site\' OR p.publishstate = \'public\')) ORDER BY lastmodified DESC LIMIT 0,20';
  196 +
  197 + $blogposts = get_records_sql($sql);
  198 +
  199 + // Now all the rss items.
  200 + $items = array();
  201 +
  202 + foreach ($blogposts as $blogpost) {
  203 + $item = null;
  204 + $temp = array();
  205 + array_push($temp, $blogpost);
  206 +
  207 + $user = get_record('user','id',$blogpost->userid);
  208 + $item->author = fullname($user);
  209 + $item->title = $blogpost->subject;
  210 + $item->pubdate = $blogpost->lastmodified;
  211 + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
  212 + $item->description = format_text($blogpost->summary, $blogpost->format);
  213 + array_push($items, $item);
  214 + }
  215 +
  216 + // First all rss feeds common headers.
  217 + $header = rss_standard_header(format_string('courseblog',true),
  218 + $CFG->wwwroot.'/blog/index.php',
  219 + format_string('intro',true));
  220 +
  221 + if (!empty($header)) {
  222 + $articles = rss_add_items($items);
  223 + }
  224 +
  225 + // Now all rss feeds common footers.
  226 + if (!empty($header) && !empty($articles)) {
  227 + $footer = rss_standard_footer();
  228 + }
  229 + // Now, if everything is ok, concatenate it.
  230 + if (!empty($header) && !empty($articles) && !empty($footer)) {
  231 + $rss = $header.$articles.$footer;
  232 +
  233 + //Save the XML contents to file.
  234 + $status = blog_rss_save_file('course',$course->id, $rss);
  235 + }
  236 + else {
  237 + $status = false;
  238 + }
  239 + }
  240 + return $status;
  241 + }
  242 +
  243 +
  244 + function blog_group_feeds() {
  245 +
  246 + $groups = get_records('groups');
  247 + foreach ($groups as $group) {
  248 + blog_group_feed($group);
  249 + }
  250 + }
  251 +
  252 + // takes in course object from db
  253 + function blog_group_feed($group) {
  254 +
  255 + global $CFG;
  256 + $status = true;
  257 +
  258 + //$CFG->debug = true;
  259 +
  260 + // Check CFG->enablerssfeeds.
  261 + if (empty($CFG->enablerssfeeds)) {
  262 + //Some debug...
  263 + if ($CFG->debug > 7) {
  264 + echo "DISABLED (admin variables)";
  265 + }
  266 + }
  267 +
  268 + // It's working so we start...
  269 + else {
  270 + // Iterate over all data.
  271 + $filename = blog_rss_file_name('group', $group->id); // RSS file
  272 + // Get the most recent 20 posts
  273 +
  274 + $sql= 'SELECT p.* FROM '.$CFG->prefix.'post p, '
  275 + .$CFG->prefix.'groups_members m
  276 + WHERE p.userid = m.userid
  277 + AND m.groupid = '.$group->id.'
  278 + AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20';
  279 +
  280 +
  281 +
  282 + // Now all the rss items.
  283 + $items = array();
  284 + if ($blogposts = get_records_sql($sql)) {
  285 + foreach ($blogposts as $blogpost) {
  286 + $item = null;
  287 + $temp = array();
  288 + array_push($temp, $blogpost);
  289 +
  290 + $user = get_record('user','id',$blogpost->userid);
  291 + $item->author = fullname($user);
  292 + $item->title = $blogpost->subject;
  293 + $item->pubdate = $blogpost->lastmodified;
  294 + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
  295 + $item->description = format_text($blogpost->summary, $blogpost->format);
  296 + array_push($items, $item);
  297 + }
  298 + }
  299 +
  300 + // First all rss feeds common headers.
  301 + $header = rss_standard_header(format_string('groupblog',true),
  302 + $CFG->wwwroot.'/blog/index.php',
  303 + format_string('intro',true));
  304 +
  305 + if (!empty($header)) {
  306 + $articles = rss_add_items($items);
  307 + }
  308 +
  309 + // Now all rss feeds common footers.
  310 + if (!empty($header) && !empty($articles)) {
  311 + $footer = rss_standard_footer();
  312 + }
  313 + // Now, if everything is ok, concatenate it.
  314 + if (!empty($header) && !empty($articles) && !empty($footer)) {
  315 + $rss = $header.$articles.$footer;
  316 +
  317 + //Save the XML contents to file.
  318 + $status = blog_rss_save_file('group',$group->id, $rss);
  319 + }
  320 + else {
  321 + $status = false;
  322 + }
  323 + }
  324 + return $status;
  325 + }
  326 +
  327 +
  328 + function blog_user_feeds() {
  329 +
  330 + $users = get_records('user');
  331 + foreach ($users as $user) {
  332 + blog_user_feed($user);
  333 + }
  334 + }
  335 +
  336 + // takes in course object from db
  337 + function blog_user_feed($user) {
  338 +
  339 + global $CFG;
  340 + $status = true;
  341 +
  342 + ////$CFG->debug = true;
  343 +
  344 + // Check CFG->enablerssfeeds.
  345 + if (empty($CFG->enablerssfeeds)) {
  346 + //Some debug...
  347 + if ($CFG->debug > 7) {
  348 + echo "DISABLED (admin variables)";
  349 + }
  350 + }
  351 +
  352 + // It's working so we start...
  353 + else {
  354 + // Iterate over all data.
  355 + $filename = blog_rss_file_name('user', $user->id); // RSS file
  356 + // Get the most recent 20 posts
  357 +
  358 + $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p, '
  359 + .$CFG->prefix.'user u
  360 + WHERE p.userid = u.id
  361 + AND u.id = '.$user->id.'
  362 + AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20';
  363 +
  364 +
  365 +
  366 + // Now all the rss items.
  367 + $items = array();
  368 + if ($blogposts = get_records_sql($sql)) {
  369 + foreach ($blogposts as $blogpost) {
  370 + $item = null;
  371 + $temp = array();
  372 + array_push($temp, $blogpost);
  373 +
  374 + $user = get_record('user','id',$blogpost->userid);
  375 + $item->author = fullname($user);
  376 + $item->title = $blogpost->subject;
  377 + $item->pubdate = $blogpost->lastmodified;
  378 + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
  379 + $item->description = format_text($blogpost->summary, $blogpost->format);
  380 + array_push($items, $item);
  381 + }
  382 + }
  383 + // First all rss feeds common headers.
  384 + $header = rss_standard_header(format_string('userblog',true),
  385 + $CFG->wwwroot.'/blog/index.php',
  386 + format_string('intro',true));
  387 +
  388 + if (!empty($header)) {
  389 + $articles = rss_add_items($items);
  390 + }
  391 +
  392 + // Now all rss feeds common footers.
  393 + if (!empty($header) && !empty($articles)) {
  394 + $footer = rss_standard_footer();
  395 + }
  396 + // Now, if everything is ok, concatenate it.
  397 + if (!empty($header) && !empty($articles) && !empty($footer)) {
  398 + $rss = $header.$articles.$footer;
  399 +
  400 + //Save the XML contents to file.
  401 + $status = blog_rss_save_file('user',$user->id, $rss);
  402 + }
  403 + else {
  404 + $status = false;
  405 + }
  406 + }
  407 + return $status;
  408 + }
  409 +?>

0 comments on commit 516194d

Please sign in to comment.
Something went wrong with that request. Please try again.