Annotation of loncom/interface/lonrss.pm, revision 1.3

1.1       www         1: # The LearningOnline Network
                      2: # RSS Feeder
                      3: #
1.3     ! albertel    4: # $Id: lonrss.pm,v 1.2 2005/11/18 13:35:30 www Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::lonrss;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::loncommon;
                     34: use Apache::lonnet;
                     35: use Apache::lontexconvert;
                     36: use Apache::lonlocal;
                     37: use Apache::lonhtmlcommon;
                     38: 
                     39: sub filterfeedname {
                     40:     my $filename=shift;
                     41:     $filename=~s/\.rss$//;
                     42:     $filename=~s/\W//g;
                     43:     return $filename;
                     44: }
                     45: 
                     46: sub feedname {
                     47:     return 'nohist_'.&filterfeedname(shift).'_rssfeed';
                     48: }
                     49: 
                     50: sub displayfeedname {
1.2       www        51:     my ($rawname,$uname,$udom)=@_;
                     52:     my $filterfilename=&filterfeedname($rawname);
                     53: # do we have a stored name?
                     54:     my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename],$udom,$uname);
                     55:     if ($stored{$filterfilename}) { return $stored{$filterfilename}; }
                     56: # no, construct a name
                     57:     my $name=$filterfilename; 
                     58:     if ($name=~/^CourseBlog/) {
                     59:         $name=&mt('Course Blog');
                     60: 	if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
                     61: 	    $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
                     62: 	}
                     63:     } else {
                     64: 	$name=~s/\_/ /g;
                     65:     }
                     66:     return $name;
                     67: }
                     68: 
                     69: sub renamefeed {
                     70:     my ($rawname,$uname,$udom,$newname)=@_;
                     71:     return &Apache::lonnet::put('nohist_all_rss_feeds',
                     72: 				{ &filterfeedname($rawname) => $newname },
                     73: 				$udom,$uname);
                     74: }
                     75: 
                     76: sub advertisefeeds {
                     77:     my ($uname,$udom)=@_;
                     78:     my $feeds='';
                     79:     my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
1.3     ! albertel   80:     foreach my $feed (sort(keys(%feednames))) {
        !            81: 	if ($feed!~/^error\:/) {
        !            82: 	    my $url='feed://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
1.2       www        83: 	    $feeds.='<li><a href="'.$url.'">'.
1.3     ! albertel   84: 		$feednames{$feed}.'</a><br /><tt>'.$url.'</tt></li>';
1.2       www        85: 	}
                     86:     }
                     87:     if ($feeds) {
                     88: 	return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
                     89:     } else {
                     90:         return '';
                     91:     }
1.1       www        92: }
                     93: 
1.3     ! albertel   94: {
        !            95:     my $feedcounter;
        !            96:     sub addentry {
        !            97: 	$feedcounter++;
        !            98: 	my $id=time.'00000'.$$.'00000'.$feedcounter;
        !            99: 	return &editentry($id,@_);
        !           100:     }
1.2       www       101: }
                    102: 
                    103: sub editentry {
                    104:     my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
                    105:     my $feedname=&feedname($filename);
                    106:     &Apache::lonnet::put('nohist_all_rss_feeds',
                    107: 			 { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) },
                    108: 			 $udom,$uname);
1.1       www       109:     return &Apache::lonnet::put($feedname,{
                    110: 	$id.'_title' => $title,
                    111: 	$id.'_description' => $description,
                    112: 	$id.'_link' => $url,
                    113: 	$id.'_enclosureurl' => $encurl,
                    114: 	$id.'_enclosurelength' => $enclength,
                    115: 	$id.'_enclosuretype' => $enctype,
                    116: 	$id.'_status' => $status},$udom,$uname);
                    117: }
                    118: 
1.2       www       119: sub changestatus {
                    120:     my ($id,$uname,$udom,$filename,$status)=@_;
                    121:     my $feedname=&feedname($filename);
                    122:     if ($status eq 'deleted') {
                    123: 	return &Apache::lonnet::del($feedname,[$id.'_title',
                    124: 					       $id.'_description',
                    125: 					       $id.'_link',
                    126: 					       $id.'_enclosureurl',
                    127: 					       $id.'_enclosurelength',
                    128: 					       $id.'_enclosuretype',
                    129: 					       $id.'_status'],$udom,$uname);
                    130:     } else {
                    131: 	return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
                    132:     }
                    133: }
                    134: 
1.1       www       135: sub handler {
                    136:     my $r = shift;
1.3     ! albertel  137:     &Apache::loncommon::content_type($r,'application/rss+xml');
1.1       www       138:     $r->send_http_header;
                    139:     return OK if $r->header_only;
                    140: 
                    141:     my (undef,undef,$udom,$uname,$filename)=split(/\//,$r->uri);
                    142:     my $filterfeedname=&filterfeedname($filename);
                    143:     my $feedname=&feedname($filename);
1.2       www       144:     my $displayfeedname=&displayfeedname($filename,$uname,$udom);
1.1       www       145:     $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
                    146: 	      "\n<link>http://".$ENV{'HTTP_HOST'}.'/</link>'.
                    147: 	      "\n<description>".&mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').'</description>');
                    148: # Is this user for real?
                    149:     my $homeserver=&Apache::lonnet::homeserver($uname,$udom); 
                    150:     if ($homeserver eq 'no_host') {
                    151: 	$r->print('<title>'.&mt('No feed available').'</title>');
                    152:     } else {
                    153: # Course or user?
                    154: 	my $name='';
                    155: 	if ($uname=~/^\d/) {
                    156: 	    my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
                    157: 	    $name=$cenv{'description'};
                    158: 	} else {
                    159: 	    $name=&Apache::loncommon::nickname($uname,$udom);
                    160: 	}
                    161:         $r->print("\n<title>".&mt('LON-CAPA RSS Feed "[_1]" for [_2]',$displayfeedname,$name).'</title>');
                    162: # Render private items?
                    163:         my $viewpubliconly=1;
                    164:         if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
                    165: 	    $viewpubliconly=0;
                    166: 	}
                    167: # Get feed items
                    168:         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
1.3     ! albertel  169: 	foreach $entry (sort(keys(%newsfeed))) {
        !           170: 	    if ($entry=~/^(\d+)\_status$/) {
1.1       www       171: 		my $id=$1;
                    172: 		if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
1.2       www       173: 		if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
1.1       www       174: 		$r->print("\n<item>\n<title>".$newsfeed{$id.'_title'}."</title>\n<description>".
1.3     ! albertel  175: 			  $newsfeed{$id.'_description'}."</description>\n<link>"."http://".$ENV{'HTTP_HOST'}.
1.1       www       176: 			  $newsfeed{$id.'_link'}."</link>\n");
                    177: 		if ($newsfeed{$id.'_enclosureurl'}) {
                    178: 		    $r->print("\n<enclosure url='".$newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
                    179: 			      "' type='".$newsfeed{$id.'_enclosuretype'}."' />");
                    180: 		}
1.3     ! albertel  181: 		$r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
1.1       www       182: 	    }
                    183: 	}
                    184:     }
                    185:     $r->print("\n</channel></rss>\n");
                    186:     return OK;
                    187: } 
                    188: 
                    189: 1;
                    190: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>