From: toyomoyo Date: Fri, 17 Mar 2006 07:38:08 +0000 (+0000) Subject: made some changes to allow rss feeds X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=516194d0e84c8c9bdcfd4392e80dac317b1b1678;p=moodle.git made some changes to allow rss feeds --- diff --git a/blog/class.BlogFilter.php b/blog/class.BlogFilter.php index 710f048752..cfd02ab171 100755 --- a/blog/class.BlogFilter.php +++ b/blog/class.BlogFilter.php @@ -191,6 +191,18 @@ class BlogFilter { $tagquerysql = ''; } + // if we have specified an ID + if ($this->postid) { + + if ($post = get_record('post', 'id', $this->postid)) { + $blogEntry = new BlogEntry($post); + $blogEntries[] = $blogEntry; + + $this->filtered_entries = $blogEntries; + return $this->filtered_entries; + } + } + /**************************************** * depending on the type, there are 4 * * different possible sqls * diff --git a/blog/index.php b/blog/index.php index b69b91b184..14940c9291 100755 --- a/blog/index.php +++ b/blog/index.php @@ -24,6 +24,7 @@ $groupid = optional_param('groupid',0,PARAM_INT); $courseid = optional_param('courseid',0,PARAM_INT); $tag = s(urldecode(optional_param('tag', '', PARAM_NOTAGS))); $tagid = optional_param('tagid', 0, PARAM_INT); +$postid = optional_param('postid',0,PARAM_INT); $filtertype = optional_param('filtertype', '', PARAM_ALPHA); $filterselect = optional_param('filterselect', 0, PARAM_INT); @@ -122,29 +123,9 @@ switch ($filtertype) { if ($CFG->bloglevel < BLOG_USER_LEVEL) { error ('Blogs is not enabled'); } - $canview = 0; //bad start - - $usercourses = get_my_courses($filterselect); - foreach ($usercourses as $usercourse) { - /// if viewer and user sharing same non-spg course, then grant permission - if (groupmode($usercourse)!= SEPARATEGROUPS){ - if (isstudent($usercourse->id) || isteacher($usercourse->id)) { - $canview = 1; - } - } else { - /// now we need every group the user is in, and check to see if view is a member - if ($usergroups = user_group($usercourse->id, $filterselect)) { - foreach ($usergroups as $usergroup) { - if (ismember($usergroup->id)) { - $canview = 1; - } - } - } - } - } - if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) { - error ('you can not view this user\'s blogs'); - } + + blog_user_can_view_user_post($filterselect); + /// check to see if the viewer is sharing no_group, visible group course. /// if not , check if the viewer is in any spg group as the user break; @@ -157,7 +138,7 @@ if ($limit == 'none') { $limit = get_user_preferences('blogpagesize',8); } -$blogFilter =& new BlogFilter($userid, '', $limit, $start,$filtertype, $filterselect, $tagid, $tag); +$blogFilter =& new BlogFilter($userid, $postid, $limit, $start,$filtertype, $filterselect, $tagid, $tag); //print_object($blogFilter); //debug include($CFG->dirroot .'/blog/header.php'); diff --git a/blog/lib.php b/blog/lib.php index 4e7edd35cc..42e0d57c50 100755 --- a/blog/lib.php +++ b/blog/lib.php @@ -9,6 +9,7 @@ require_once($CFG->dirroot .'/blog/class.BlogEntry.php'); require_once($CFG->dirroot .'/blog/class.BlogFilter.php'); require_once($CFG->libdir .'/blocklib.php'); require_once($CFG->libdir .'/pagelib.php'); +require_once('rsslib.php'); require_once($CFG->dirroot .'/blog/blogpage.php'); /* blog access level constant declaration */ @@ -166,6 +167,8 @@ function blog_print_html_formatted_entries(&$blogFilter, $filtertype, $filtersel $blogEntries = $blogFilter->get_filtered_entries(); // show page next/previous links if applicable print_paging_bar($blogFilter->get_viewable_entry_count(), $blogpage, $bloglimit, $blogFilter->baseurl, 'blogpage'); + + blog_rss_print_link($filtertype, $filterselect); print ''; if (blog_isLoggedIn()) { @@ -333,4 +336,42 @@ function blog_applicable_publish_states($courseid='') { return $options; } +/// Checks to see if a user can view the blogs of another user. +/// He can do so, if he is admin, in any same non-spg course, +/// or spg group, but same group member +function blog_user_can_view_user_post($targetuserid) { + + $canview = 0; //bad start + + if (isadmin()) { + return true; + } + + $usercourses = get_my_courses($targetuserid); + foreach ($usercourses as $usercourse) { + /// if viewer and user sharing same non-spg course, then grant permission + if (groupmode($usercourse)!= SEPARATEGROUPS){ + if (isstudent($usercourse->id) || isteacher($usercourse->id)) { + $canview = 1; + return $canview; + } + } else { + /// now we need every group the user is in, and check to see if view is a member + if ($usergroups = user_group($usercourse->id, $targetuserid)) { + foreach ($usergroups as $usergroup) { + if (ismember($usergroup->id)) { + $canview = 1; + return $canview; + } + } + } + } + } + + if (!$canview && $CFG->bloglevel < BLOG_SITE_LEVEL) { + error ('you can not view this user\'s blogs'); + } + + return $canview; +} ?> diff --git a/blog/rsslib.php b/blog/rsslib.php new file mode 100755 index 0000000000..f1f328d5d4 --- /dev/null +++ b/blog/rsslib.php @@ -0,0 +1,409 @@ +dirroot.'/lib/rsslib.php'); + + + // This function returns the icon (from theme) with the link to rss/file.php + // needs some hacking to rss/file.php + function blog_rss_print_link($filtertype, $filterselect, $tooltiptext='') { + + global $CFG, $USER; + + static $pixpath = ''; + static $rsspath = ''; + $rsspix = $CFG->pixpath .'/i/rss.gif'; + + if ($CFG->slasharguments) { + $rsspath = $CFG->wwwroot.'/rss/file.php/blogs/'.$filtertype.'/'.$filterselect.'/rss.xml'; + } else { + $rsspath = $CFG->wwwroot.'/rss/file.php?file=/blogs/'.$filtertype.'/'.$filterselect.'/rss.xml'; + } + print '
'; + + } + + // This file adds support to rss feeds generation + // This function is the main entry point to database module + // rss feeds generation. Foreach database with rss enabled + // build one XML rss structure. + function blog_rss_feeds() { + + blog_site_feeds(); //generate site level feeds, last 20 entries? + blog_course_feeds(); //generate all course level feeds, last 20 entries + blog_group_feeds(); //generate all group level feeds, last 20 entries + blog_user_feeds(); //generate all user level feeds, last 20 entries + + } + + /* Rss files for blogs + * 4 different ways to store feeds. + * site - $CFG->dataroot/rss/blogs/site/SITEID.xml + * course - $CFG->dataroot/rss/blogs/course/courseid.xml + * group - $CFG->dataroot/rss/blogs/group/groupid.xml + * user - $CFG->dataroot/rss/blogs/user/userid.xml + */ + function blog_rss_file_name($type, $id) { + global $CFG; + $filename = "$CFG->dataroot/rss/blogs/$type/$id/rss.xml"; + return $filename; + } + + //This function saves to file the rss feed specified in the parameters + function blog_rss_save_file($type, $id, $result) { + global $CFG; + + $status = true; + + if (! $basedir = make_upload_directory ('rss/blogs/'. $type.'/'.$id)) { + //Cannot be created, so error + $status = false; + } + + if ($status) { + $file = blog_rss_file_name($type, $id); + $rss_file = fopen($file, "w"); + if ($rss_file) { + $status = fwrite ($rss_file, $result); + fclose($rss_file); + } else { + $status = false; + } + } + return $status; + } + + + // Only 1 view, site level feeds + function blog_site_feeds() { + + global $CFG; + $status = true; + + //////$CFG->debug = true; + + // Check CFG->enablerssfeeds. + if (empty($CFG->enablerssfeeds)) { + //Some debug... + if ($CFG->debug > 7) { + echo "DISABLED (admin variables)"; + } + } + + // It's working so we start... + else { + // Iterate over all data. + $filename = blog_rss_file_name('site', SITEID); // RSS file + // Get the most recent 20 posts + $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p, + '.$CFG->prefix.'user u + WHERE p.userid = u.id + AND (p.publishstate = \'site\' OR p.publishstate = \'public\') + AND u.deleted = 0 ORDER BY lastmodified DESC LIMIT 0,20'; + + $blogposts = get_records_sql($sql); + + // Now all the rss items. + $items = array(); + + foreach ($blogposts as $blogpost) { + $item = null; + $temp = array(); + array_push($temp, $blogpost); + + $user = get_record('user','id',$blogpost->userid); + $item->author = fullname($user); + $item->title = $blogpost->subject; + $item->pubdate = $blogpost->lastmodified; + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id; + $item->description = format_text($blogpost->summary, $blogpost->format); + array_push($items, $item); + } + + // First all rss feeds common headers. + $header = rss_standard_header(format_string('siteblog',true), + $CFG->wwwroot.'/blog/index.php', + format_string('intro',true)); + + if (!empty($header)) { + $articles = rss_add_items($items); + } + + // Now all rss feeds common footers. + if (!empty($header) && !empty($articles)) { + $footer = rss_standard_footer(); + } + // Now, if everything is ok, concatenate it. + if (!empty($header) && !empty($articles) && !empty($footer)) { + $rss = $header.$articles.$footer; + + //Save the XML contents to file. + $status = blog_rss_save_file('site', SITEID, $rss); + } + else { + $status = false; + } + } + return $status; + } + + + /// Generate the feeds for all courses + function blog_course_feeds() { + + $courses = get_records('course'); + foreach ($courses as $course) { + if ($course->id != SITEID) { + blog_course_feed($course); + } + } + } + + // takes in course object from db + function blog_course_feed($course) { + + global $CFG; + $status = true; + + ////$CFG->debug = true; + + // Check CFG->enablerssfeeds. + if (empty($CFG->enablerssfeeds)) { + //Some debug... + if ($CFG->debug > 7) { + echo "DISABLED (admin variables)"; + } + } + + // It's working so we start... + else { + // Iterate over all data. + $filename = blog_rss_file_name('course', $course->id); // RSS file + // Get the most recent 20 posts + + $sql = '(SELECT p.* FROM '.$CFG->prefix.'post p, ' + .$CFG->prefix.'user_students u + WHERE p.userid = u.userid + AND u.course = '.$course->id.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\')) + + UNION + + (SELECT p.* FROM '.$CFG->prefix.'post p, ' + .$CFG->prefix.'user_teachers u + WHERE p.userid = u.userid + AND u.course = '.$course->id.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\')) ORDER BY lastmodified DESC LIMIT 0,20'; + + $blogposts = get_records_sql($sql); + + // Now all the rss items. + $items = array(); + + foreach ($blogposts as $blogpost) { + $item = null; + $temp = array(); + array_push($temp, $blogpost); + + $user = get_record('user','id',$blogpost->userid); + $item->author = fullname($user); + $item->title = $blogpost->subject; + $item->pubdate = $blogpost->lastmodified; + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id; + $item->description = format_text($blogpost->summary, $blogpost->format); + array_push($items, $item); + } + + // First all rss feeds common headers. + $header = rss_standard_header(format_string('courseblog',true), + $CFG->wwwroot.'/blog/index.php', + format_string('intro',true)); + + if (!empty($header)) { + $articles = rss_add_items($items); + } + + // Now all rss feeds common footers. + if (!empty($header) && !empty($articles)) { + $footer = rss_standard_footer(); + } + // Now, if everything is ok, concatenate it. + if (!empty($header) && !empty($articles) && !empty($footer)) { + $rss = $header.$articles.$footer; + + //Save the XML contents to file. + $status = blog_rss_save_file('course',$course->id, $rss); + } + else { + $status = false; + } + } + return $status; + } + + + function blog_group_feeds() { + + $groups = get_records('groups'); + foreach ($groups as $group) { + blog_group_feed($group); + } + } + + // takes in course object from db + function blog_group_feed($group) { + + global $CFG; + $status = true; + + //$CFG->debug = true; + + // Check CFG->enablerssfeeds. + if (empty($CFG->enablerssfeeds)) { + //Some debug... + if ($CFG->debug > 7) { + echo "DISABLED (admin variables)"; + } + } + + // It's working so we start... + else { + // Iterate over all data. + $filename = blog_rss_file_name('group', $group->id); // RSS file + // Get the most recent 20 posts + + $sql= 'SELECT p.* FROM '.$CFG->prefix.'post p, ' + .$CFG->prefix.'groups_members m + WHERE p.userid = m.userid + AND m.groupid = '.$group->id.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20'; + + + + // Now all the rss items. + $items = array(); + if ($blogposts = get_records_sql($sql)) { + foreach ($blogposts as $blogpost) { + $item = null; + $temp = array(); + array_push($temp, $blogpost); + + $user = get_record('user','id',$blogpost->userid); + $item->author = fullname($user); + $item->title = $blogpost->subject; + $item->pubdate = $blogpost->lastmodified; + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id; + $item->description = format_text($blogpost->summary, $blogpost->format); + array_push($items, $item); + } + } + + // First all rss feeds common headers. + $header = rss_standard_header(format_string('groupblog',true), + $CFG->wwwroot.'/blog/index.php', + format_string('intro',true)); + + if (!empty($header)) { + $articles = rss_add_items($items); + } + + // Now all rss feeds common footers. + if (!empty($header) && !empty($articles)) { + $footer = rss_standard_footer(); + } + // Now, if everything is ok, concatenate it. + if (!empty($header) && !empty($articles) && !empty($footer)) { + $rss = $header.$articles.$footer; + + //Save the XML contents to file. + $status = blog_rss_save_file('group',$group->id, $rss); + } + else { + $status = false; + } + } + return $status; + } + + + function blog_user_feeds() { + + $users = get_records('user'); + foreach ($users as $user) { + blog_user_feed($user); + } + } + + // takes in course object from db + function blog_user_feed($user) { + + global $CFG; + $status = true; + + ////$CFG->debug = true; + + // Check CFG->enablerssfeeds. + if (empty($CFG->enablerssfeeds)) { + //Some debug... + if ($CFG->debug > 7) { + echo "DISABLED (admin variables)"; + } + } + + // It's working so we start... + else { + // Iterate over all data. + $filename = blog_rss_file_name('user', $user->id); // RSS file + // Get the most recent 20 posts + + $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p, ' + .$CFG->prefix.'user u + WHERE p.userid = u.id + AND u.id = '.$user->id.' + AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20'; + + + + // Now all the rss items. + $items = array(); + if ($blogposts = get_records_sql($sql)) { + foreach ($blogposts as $blogpost) { + $item = null; + $temp = array(); + array_push($temp, $blogpost); + + $user = get_record('user','id',$blogpost->userid); + $item->author = fullname($user); + $item->title = $blogpost->subject; + $item->pubdate = $blogpost->lastmodified; + $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id; + $item->description = format_text($blogpost->summary, $blogpost->format); + array_push($items, $item); + } + } + // First all rss feeds common headers. + $header = rss_standard_header(format_string('userblog',true), + $CFG->wwwroot.'/blog/index.php', + format_string('intro',true)); + + if (!empty($header)) { + $articles = rss_add_items($items); + } + + // Now all rss feeds common footers. + if (!empty($header) && !empty($articles)) { + $footer = rss_standard_footer(); + } + // Now, if everything is ok, concatenate it. + if (!empty($header) && !empty($articles) && !empty($footer)) { + $rss = $header.$articles.$footer; + + //Save the XML contents to file. + $status = blog_rss_save_file('user',$user->id, $rss); + } + else { + $status = false; + } + } + return $status; + } +?>