Annotation of loncom/interface/lonrss.pm, revision 1.2
1.1 www 1: # The LearningOnline Network
2: # RSS Feeder
3: #
1.2 ! www 4: # $Id: lonrss.pm,v 1.1 2005/11/17 21:40:52 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: my $feedcounter=0;
40:
41: sub filterfeedname {
42: my $filename=shift;
43: $filename=~s/\.rss$//;
44: $filename=~s/\W//g;
45: return $filename;
46: }
47:
48: sub feedname {
49: return 'nohist_'.&filterfeedname(shift).'_rssfeed';
50: }
51:
52: sub displayfeedname {
1.2 ! www 53: my ($rawname,$uname,$udom)=@_;
! 54: my $filterfilename=&filterfeedname($rawname);
! 55: # do we have a stored name?
! 56: my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename],$udom,$uname);
! 57: if ($stored{$filterfilename}) { return $stored{$filterfilename}; }
! 58: # no, construct a name
! 59: my $name=$filterfilename;
! 60: if ($name=~/^CourseBlog/) {
! 61: $name=&mt('Course Blog');
! 62: if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
! 63: $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
! 64: }
! 65: } else {
! 66: $name=~s/\_/ /g;
! 67: }
! 68: return $name;
! 69: }
! 70:
! 71: sub renamefeed {
! 72: my ($rawname,$uname,$udom,$newname)=@_;
! 73: return &Apache::lonnet::put('nohist_all_rss_feeds',
! 74: { &filterfeedname($rawname) => $newname },
! 75: $udom,$uname);
! 76: }
! 77:
! 78: sub advertisefeeds {
! 79: my ($uname,$udom)=@_;
! 80: my $feeds='';
! 81: my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
! 82: foreach (sort keys %feednames) {
! 83: if ($_!~/^error\:/) {
! 84: my $url='feed://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$_.'.rss';
! 85: $feeds.='<li><a href="'.$url.'">'.
! 86: $feednames{$_}.'</a><br /><tt>'.$url.'</tt></li>';
! 87: }
! 88: }
! 89: if ($feeds) {
! 90: return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
! 91: } else {
! 92: return '';
! 93: }
1.1 www 94: }
95:
96: sub addentry {
97: $feedcounter++;
98: my $id=time.'00000'.$$.'00000'.$feedcounter;
1.2 ! www 99: return &editentry($id,@_);
! 100: }
! 101:
! 102: sub editentry {
! 103: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
! 104: my $feedname=&feedname($filename);
! 105: &Apache::lonnet::put('nohist_all_rss_feeds',
! 106: { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) },
! 107: $udom,$uname);
1.1 www 108: return &Apache::lonnet::put($feedname,{
109: $id.'_title' => $title,
110: $id.'_description' => $description,
111: $id.'_link' => $url,
112: $id.'_enclosureurl' => $encurl,
113: $id.'_enclosurelength' => $enclength,
114: $id.'_enclosuretype' => $enctype,
115: $id.'_status' => $status},$udom,$uname);
116: }
117:
1.2 ! www 118: sub changestatus {
! 119: my ($id,$uname,$udom,$filename,$status)=@_;
! 120: my $feedname=&feedname($filename);
! 121: if ($status eq 'deleted') {
! 122: return &Apache::lonnet::del($feedname,[$id.'_title',
! 123: $id.'_description',
! 124: $id.'_link',
! 125: $id.'_enclosureurl',
! 126: $id.'_enclosurelength',
! 127: $id.'_enclosuretype',
! 128: $id.'_status'],$udom,$uname);
! 129: } else {
! 130: return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
! 131: }
! 132: }
! 133:
1.1 www 134: sub handler {
135: my $r = shift;
136: &Apache::loncommon::content_type($r,'text/xml');
137: $r->send_http_header;
138: return OK if $r->header_only;
139:
140: my (undef,undef,$udom,$uname,$filename)=split(/\//,$r->uri);
141: my $filterfeedname=&filterfeedname($filename);
142: my $feedname=&feedname($filename);
1.2 ! www 143: my $displayfeedname=&displayfeedname($filename,$uname,$udom);
1.1 www 144: $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
145: "\n<link>http://".$ENV{'HTTP_HOST'}.'/</link>'.
146: "\n<description>".&mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').'</description>');
147: # Is this user for real?
148: my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
149: if ($homeserver eq 'no_host') {
150: $r->print('<title>'.&mt('No feed available').'</title>');
151: } else {
152: # Course or user?
153: my $name='';
154: if ($uname=~/^\d/) {
155: my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
156: $name=$cenv{'description'};
157: } else {
158: $name=&Apache::loncommon::nickname($uname,$udom);
159: }
160: $r->print("\n<title>".&mt('LON-CAPA RSS Feed "[_1]" for [_2]',$displayfeedname,$name).'</title>');
161: # Render private items?
162: my $viewpubliconly=1;
163: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
164: $viewpubliconly=0;
165: }
166: # Get feed items
167: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
168: foreach (sort keys %newsfeed) {
169: if ($_=~/^(\d+)\_status$/) {
170: my $id=$1;
171: if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
1.2 ! www 172: if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
1.1 www 173: $r->print("\n<item>\n<title>".$newsfeed{$id.'_title'}."</title>\n<description>".
174: $newsfeed{$id.'_description'}."</description>\n<link>".
175: $newsfeed{$id.'_link'}."</link>\n");
176: if ($newsfeed{$id.'_enclosureurl'}) {
177: $r->print("\n<enclosure url='".$newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
178: "' type='".$newsfeed{$id.'_enclosuretype'}."' />");
179: }
180: $r->print("\n<guid>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
181: }
182: }
183: }
184: $r->print("\n</channel></rss>\n");
185: return OK;
186: }
187:
188: 1;
189: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>