1: # The LearningOnline Network
2: # RSS Feeder
3: #
4: # $Id: lonrss.pm,v 1.16 2006/04/13 18:12:49 albertel Exp $
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:
40: sub filterfeedname {
41: my $filename=shift;
42: $filename=~s/(\_rss\.html|\.rss)$//;
43: $filename=~s/\W//g;
44: $filename=~s/\_rssfeed$//;
45: $filename=~s/^nohist\_//;
46: return $filename;
47: }
48:
49: sub feedname {
50: return 'nohist_'.&filterfeedname(shift).'_rssfeed';
51: }
52:
53: sub displayfeedname {
54: my ($rawname,$uname,$udom)=@_;
55: my $filterfilename=&filterfeedname($rawname);
56: # do we have a stored name?
57: my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename],$udom,$uname);
58: if ($stored{$filterfilename}) { return $stored{$filterfilename}; }
59: # no, construct a name
60: my $name=$filterfilename;
61: if ($name=~/^CourseBlog/) {
62: $name=&mt('Course Blog');
63: if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
64: $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
65: }
66: } else {
67: $name=~s/\_/ /g;
68: }
69: return $name;
70: }
71:
72: sub renamefeed {
73: my ($rawname,$uname,$udom,$newname)=@_;
74: return &Apache::lonnet::put('nohist_all_rss_feeds',
75: { &filterfeedname($rawname) => $newname },
76: $udom,$uname);
77: }
78:
79: sub advertisefeeds {
80: my ($uname,$udom,$edit)=@_;
81: my $feeds='';
82: my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
83: my $mode='public';
84: if ($edit) {
85: $mode='adm';
86: }
87: foreach my $feed (sort(keys(%feednames))) {
88: if ($feed!~/^error\:/) {
89: my $feedurl='http://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
90: my $htmlurl='http://'.$ENV{'HTTP_HOST'}.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html';
91: $feeds.='<li>'.$feednames{$feed}.
92: '<br />'.($edit?&mt('Edit'):'HTML').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a>'.
93: ($edit?'':'<br />RSS: <a href="'.$feedurl.'"><tt>'.$feedurl.'</tt></a>').'</li>';
94: }
95: }
96: if ($feeds) {
97: return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
98: } else {
99: return '';
100: }
101: }
102:
103: sub rss_link {
104: my ($url) = @_;
105: return qq|<link rel="alternate" type="application/rss+xml" title="Course Announcements" href="$url" />|;
106: }
107:
108: {
109: my $feedcounter;
110: sub get_new_feed_id {
111: $feedcounter++;
112: return time().'00000'.$$.'00000'.$feedcounter;
113: }
114: }
115:
116: sub addentry {
117: my $id=&get_new_feed_id();
118: return &editentry($id,@_);
119: }
120:
121: sub editentry {
122: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
123: if ($status eq 'deleted') {
124: return &changestatus($id,$uname,$udom,$filename,$status);
125: }
126: my $feedname=&feedname($filename);
127: &Apache::lonnet::put('nohist_all_rss_feeds',
128: { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) },
129: $udom,$uname);
130: return &Apache::lonnet::put($feedname,{
131: $id.'_title' => $title,
132: $id.'_description' => $description,
133: $id.'_link' => $url,
134: $id.'_enclosureurl' => $encurl,
135: $id.'_enclosurelength' => $enclength,
136: $id.'_enclosuretype' => $enctype,
137: $id.'_status' => $status},$udom,$uname);
138: }
139:
140: sub changestatus {
141: my ($id,$uname,$udom,$filename,$status)=@_;
142: my $feedname=&feedname($filename);
143: if ($status eq 'deleted') {
144: return &Apache::lonnet::del($feedname,[$id.'_title',
145: $id.'_description',
146: $id.'_link',
147: $id.'_enclosureurl',
148: $id.'_enclosurelength',
149: $id.'_enclosuretype',
150: $id.'_status'],$udom,$uname);
151: } else {
152: return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
153: }
154: }
155:
156: sub changed_js {
157: return <<ENDSCRIPT;
158: <script type="text/javascript">
159: function changed(tform,id) {
160: tform.elements[id+"_modified"].checked=true;
161: }
162: </script>
163: ENDSCRIPT
164: }
165:
166: sub handler {
167: my ($r) = @_;
168:
169: my $edit=0;
170: my $html=0;
171: my (undef,$mode,$udom,$uname,$filename)=split(/\//,$r->uri);
172: if (($mode eq 'adm') && ($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
173: $edit=1;
174: $html=1;
175: }
176: if ($filename=~/\.html$/) {
177: $html=1;
178: }
179: if ($html) {
180: &Apache::loncommon::content_type($r,'text/html');
181: } else {
182: # Workaround Mozilla/Firefox
183: # &Apache::loncommon::content_type($r,'application/rss+xml');
184: &Apache::loncommon::content_type($r,'text/xml');
185: }
186: $r->send_http_header;
187: return OK if $r->header_only;
188:
189: my $filterfeedname=&filterfeedname($filename);
190: my $feedname=&feedname($filename);
191: my $displayfeedname=&displayfeedname($filename,$uname,$udom);
192: if ($html) {
193: $r->print(&Apache::loncommon::start_page($displayfeedname,undef,
194: {'domain' => $udom,
195: 'force_register' =>
196: $env{'form.register'}}).
197: &changed_js());
198: } else {
199: $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
200: "\n<link>http://".$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.
201: $filterfeedname.'_rss.html</link>'.
202: "\n<description>".
203: &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').
204: '</description>');
205: }
206: # Do we have stuff to store?
207: if ($edit) {
208: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
209: foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
210: if ($entry=~/^(\d+)\_status$/) {
211: my $id=$1;
212: if ($env{'form.'.$id.'_modified'}) {
213: &editentry($id,$uname,$udom,$feedname,
214: $env{'form.'.$id.'_title'},
215: $env{'form.'.$id.'_description'},
216: $env{'form.'.$id.'_url'},
217: $env{'form.'.$id.'_status'});
218: }
219: }
220: }
221: }
222: my $newid = &get_new_feed_id();
223: # Is this user for real?
224: my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
225: if ($html) {
226: $r->print(&advertisefeeds($uname,$udom,$edit));
227: }
228: if ($homeserver eq 'no_host') {
229: $r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
230: } else {
231: # Course or user?
232: my $name='';
233: if ($uname=~/^\d/) {
234: my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
235: $name=$cenv{'description'};
236: } else {
237: $name=&Apache::loncommon::nickname($uname,$udom);
238: }
239: $r->print("\n".
240: ($html?'<h3>':'<title>').
241: &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
242: ($html?'</h3>'.($edit?'<form method="post"><br />'.
243: &mt('Name of blog/journal').
244: ': <input type="text" size="50" name="newblogname" value="'.
245: $displayfeedname.'" />':'').'<ul>':'</title>'));
246: # Render private items?
247: my $viewpubliconly=1;
248: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
249: $viewpubliconly=0;
250: }
251: # Get feed items
252: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
253: foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
254: if ($entry=~/^(\d+)\_status$/) {
255: my $id=$1;
256: if ($edit) {
257: my %lt=&Apache::lonlocal::texthash('public' => 'public',
258: 'private' => 'private',
259: 'hidden' => 'hidden',
260: 'delete' => 'delete',
261: 'store' => 'Store changes');
262: my %status=();
263: unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
264: $status{$newsfeed{$id.'_status'}}='checked="checked"';
265: $r->print(<<ENDEDIT);
266: <li>
267: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
268:
269: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
270:
271: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
272:
273: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
274:
275: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
276: <br />
277: <input name='$id\_title' type='text' size='80' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
278: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
279: <input name='$id\_link' type='text' size='80' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
280: <hr /></li>
281: ENDEDIT
282: } else {
283: if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
284: if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
285: $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
286: ($html?"</b><br />\n":"</title>\n<description>").
287: $newsfeed{$id.'_description'}.
288: ($html?"<br />\n<a href='":"</description>\n<link>").
289: "http://".$ENV{'HTTP_HOST'}.
290: $newsfeed{$id.'_link'}.
291: ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
292: if ($newsfeed{$id.'_enclosureurl'}) {
293: $r->print(($html?"<a href='":"\n<enclosure url='").
294: $newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
295: "' type='".$newsfeed{$id.'_enclosuretype'}.($html?"'>".&mt('Enclosure')."</a>":"' />"));
296: }
297: if ($html) {
298: $r->print("\n<hr /></li>\n");
299: } else {
300: $r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
301: }
302: }
303: }
304: }
305: }
306: $r->print("\n".($html?'</ul>'.($edit?'<input type="hidden" name="newid" value="'.$newid.'"/><input type="submit" value="'.&mt('Store Marked Changes').'" /></form>':'').&Apache::loncommon::end_page():'</channel></rss>'."\n"));
307: return OK;
308: }
309: 1;
310: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>