Annotation of loncom/interface/lonrss.pm, revision 1.26
1.1 www 1: # The LearningOnline Network
2: # RSS Feeder
3: #
1.26 ! albertel 4: # $Id: lonrss.pm,v 1.25 2006/06/03 21:05:04 albertel 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;
1.22 albertel 32: use LONCAPA;
1.1 www 33: use Apache::Constants qw(:common);
34: use Apache::loncommon;
35: use Apache::lonnet;
36: use Apache::lontexconvert;
37: use Apache::lonlocal;
38: use Apache::lonhtmlcommon;
39:
1.15 www 40:
1.1 www 41: sub filterfeedname {
42: my $filename=shift;
1.5 www 43: $filename=~s/(\_rss\.html|\.rss)$//;
1.1 www 44: $filename=~s/\W//g;
1.15 www 45: $filename=~s/\_rssfeed$//;
46: $filename=~s/^nohist\_//;
1.1 www 47: return $filename;
48: }
49:
50: sub feedname {
51: return 'nohist_'.&filterfeedname(shift).'_rssfeed';
52: }
53:
54: sub displayfeedname {
1.2 www 55: my ($rawname,$uname,$udom)=@_;
56: my $filterfilename=&filterfeedname($rawname);
57: # do we have a stored name?
1.20 www 58: my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename,'feed_display_option_'.$filterfilename],$udom,$uname);
59: if ($stored{$filterfilename}) { return ($stored{$filterfilename},$stored{'feed_display_option_'.$filterfilename}); }
1.2 www 60: # no, construct a name
61: my $name=$filterfilename;
62: if ($name=~/^CourseBlog/) {
63: $name=&mt('Course Blog');
64: if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
65: $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
66: }
67: } else {
68: $name=~s/\_/ /g;
69: }
1.20 www 70: return ($name,$stored{'feed_display_option_'.$filterfilename});
1.2 www 71: }
72:
1.19 www 73: sub namefeed {
1.2 www 74: my ($rawname,$uname,$udom,$newname)=@_;
75: return &Apache::lonnet::put('nohist_all_rss_feeds',
76: { &filterfeedname($rawname) => $newname },
77: $udom,$uname);
78: }
79:
1.20 www 80: sub changefeeddisplay {
81: my ($rawname,$uname,$udom,$newstatus)=@_;
82: return &Apache::lonnet::put('nohist_all_rss_feeds',
83: { 'feed_display_option_'.&filterfeedname($rawname) => $newstatus },
84: $udom,$uname);
85: }
86:
1.2 www 87: sub advertisefeeds {
1.6 www 88: my ($uname,$udom,$edit)=@_;
1.2 www 89: my $feeds='';
90: my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
1.6 www 91: my $mode='public';
92: if ($edit) {
93: $mode='adm';
94: }
1.3 albertel 95: foreach my $feed (sort(keys(%feednames))) {
1.24 www 96: if (($feed!~/^error\:/) && ($feed!~/^feed\_display\_option\_/)) {
1.14 www 97: my $feedurl='http://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
1.6 www 98: my $htmlurl='http://'.$ENV{'HTTP_HOST'}.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html';
1.20 www 99: if ($feednames{'feed_display_option_'.$feed} eq 'hidden') {
100: if ($edit) {
101: $feeds.='<li><i>'.$feednames{$feed}.'</i><br />'.&mt('Hidden').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a></li>';
102: }
103: } else {
104: $feeds.='<li><b>'.$feednames{$feed}.
105: '</b><br />'.($edit?&mt('Edit'):'HTML').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a>'.
106: '<br />RSS: <a href="'.$feedurl.'"><tt>'.$feedurl.'</tt></a></li>';
107: }
1.2 www 108: }
109: }
110: if ($feeds) {
111: return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
112: } else {
113: return '';
114: }
1.1 www 115: }
116:
1.12 albertel 117: sub rss_link {
118: my ($url) = @_;
119: return qq|<link rel="alternate" type="application/rss+xml" title="Course Announcements" href="$url" />|;
120: }
121:
1.16 albertel 122: {
123: my $feedcounter;
124: sub get_new_feed_id {
125: $feedcounter++;
126: return time().'00000'.$$.'00000'.$feedcounter;
127: }
128: }
129:
1.15 www 130: sub addentry {
1.16 albertel 131: my $id=&get_new_feed_id();
1.15 www 132: return &editentry($id,@_);
1.2 www 133: }
134:
135: sub editentry {
1.17 www 136: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_;
1.15 www 137: if ($status eq 'deleted') {
138: return &changestatus($id,$uname,$udom,$filename,$status);
139: }
1.2 www 140: my $feedname=&feedname($filename);
141: &Apache::lonnet::put('nohist_all_rss_feeds',
1.24 www 142: { &filterfeedname($filename) =>
143: (&displayfeedname($filename,$uname,$udom))[0] },
1.2 www 144: $udom,$uname);
1.1 www 145: return &Apache::lonnet::put($feedname,{
146: $id.'_title' => $title,
147: $id.'_description' => $description,
148: $id.'_link' => $url,
149: $id.'_enclosureurl' => $encurl,
150: $id.'_enclosuretype' => $enctype,
151: $id.'_status' => $status},$udom,$uname);
152: }
153:
1.2 www 154: sub changestatus {
155: my ($id,$uname,$udom,$filename,$status)=@_;
156: my $feedname=&feedname($filename);
157: if ($status eq 'deleted') {
158: return &Apache::lonnet::del($feedname,[$id.'_title',
159: $id.'_description',
160: $id.'_link',
161: $id.'_enclosureurl',
162: $id.'_enclosuretype',
163: $id.'_status'],$udom,$uname);
164: } else {
165: return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
166: }
167: }
168:
1.8 albertel 169: sub changed_js {
170: return <<ENDSCRIPT;
171: <script type="text/javascript">
172: function changed(tform,id) {
173: tform.elements[id+"_modified"].checked=true;
174: }
175: </script>
1.11 albertel 176: ENDSCRIPT
1.8 albertel 177: }
178:
1.17 www 179: sub determine_enclosure_types {
180: my ($url)=@_;
181: my ($ending)=($url=~/\.(\w+)$/);
182: return &Apache::loncommon::filemimetype($ending);
183: }
184:
1.21 www 185: sub course_blog_link {
186: my ($id,$title,$description,$url,$encurl,$enctype)=@_;
187: if ($env{'request.course.id'}) {
188: return &add_blog_entry_link($id,
189: $env{'course.'.$env{'request.course.id'}.'.num'},
190: $env{'course.'.$env{'request.course.id'}.'.domain'},
191: 'Course_Announcements',
192: $title,$description,$url,'public',$encurl,$enctype,
193: &mt('Add to Course Announcements'));
194: } else {
195: return '';
196: }
197: }
198:
199: sub add_blog_entry_link {
200: my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype,$linktext)=@_;
201: return "<a href='/adm/$udom/$uname/".&filterfeedname($filename).'_rss.html?queryid='.
1.22 albertel 202: &escape($id).
203: '&title='.&escape($title).
204: '&description='.&escape($description).
205: '&url='.&escape($url).
206: '&status='.&escape($status).
207: '&encurl='.&escape($encurl).
208: '&enctype='.&escape($enctype).
1.21 www 209: "'>".$linktext.'</a>';
210:
211: }
212:
1.1 www 213: sub handler {
1.8 albertel 214: my ($r) = @_;
1.5 www 215:
216: my $edit=0;
217: my $html=0;
218: my (undef,$mode,$udom,$uname,$filename)=split(/\//,$r->uri);
219: if (($mode eq 'adm') && ($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
220: $edit=1;
221: $html=1;
222: }
1.21 www 223: if (($mode eq 'adm') && (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
224: $edit=1;
225: $html=1;
226: }
1.5 www 227: if ($filename=~/\.html$/) {
228: $html=1;
229: }
230: if ($html) {
231: &Apache::loncommon::content_type($r,'text/html');
232: } else {
233: # Workaround Mozilla/Firefox
234: # &Apache::loncommon::content_type($r,'application/rss+xml');
235: &Apache::loncommon::content_type($r,'text/xml');
236: }
1.1 www 237: $r->send_http_header;
238: return OK if $r->header_only;
239:
240: my $filterfeedname=&filterfeedname($filename);
241: my $feedname=&feedname($filename);
1.20 www 242: my ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
1.5 www 243: if ($html) {
1.25 albertel 244: my $title = $displayfeedname?$displayfeedname
245: :"Available RSS Feeds and Blogs";
1.26 ! albertel 246: $r->print(&Apache::loncommon::start_page($title,undef,
1.13 albertel 247: {'domain' => $udom,
248: 'force_register' =>
249: $env{'form.register'}}).
1.8 albertel 250: &changed_js());
1.18 www 251: } else { # render RSS
1.5 www 252: $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
253: "\n<link>http://".$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.
254: $filterfeedname.'_rss.html</link>'.
255: "\n<description>".
256: &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').
257: '</description>');
258: }
1.21 www 259: # This will be the entry id for new additions to the blog
1.16 albertel 260: my $newid = &get_new_feed_id();
1.1 www 261: # Is this user for real?
1.6 www 262: my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
263: if ($html) {
1.19 www 264: # Any new feeds or renaming of feeds?
265: if ($edit) {
1.20 www 266: # Hide a feed?
267: if ($env{'form.hidethisblog'}) {
268: &changefeeddisplay($feedname,$uname,$udom,'hidden');
269: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
270: }
271: # Advertise a feed?
272: if ($env{'form.advertisethisblog'}) {
273: &changefeeddisplay($feedname,$uname,$udom,'public');
274: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
275: }
1.19 www 276: # New feed?
277: if ($env{'form.namenewblog'}=~/\w/) {
278: &namefeed($env{'form.namenewblog'},$uname,$udom,$env{'form.namenewblog'});
279: }
280: # Old feed that is being renamed?
281: if (($displayfeedname) && ($env{'form.newblogname'}=~/\w/)) {
282: if ($env{'form.newblogname'} ne $displayfeedname) {
283: &namefeed($feedname,$uname,$udom,$env{'form.newblogname'});
1.20 www 284: ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom);
1.19 www 285: }
286: }
287: }
1.6 www 288: $r->print(&advertisefeeds($uname,$udom,$edit));
289: }
1.1 www 290: if ($homeserver eq 'no_host') {
1.5 www 291: $r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
1.18 www 292: } else { # is indeed a user
1.1 www 293: # Course or user?
294: my $name='';
295: if ($uname=~/^\d/) {
296: my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
297: $name=$cenv{'description'};
298: } else {
299: $name=&Apache::loncommon::nickname($uname,$udom);
300: }
1.19 www 301: # Add a new feed
302: if (($html) && ($edit)) {
303: $r->print('<form method="post">');
1.21 www 304: $r->print(&mt('Name for New Feed').": <input type='text' size='40' name='namenewblog' />");
305: $r->print('<input type="submit" value="'.&mt('Start a New Feed').'" />');
1.19 www 306: $r->print('</form>');
307: }
1.18 www 308: if ($displayfeedname) { # this is an existing feed
309: # Anything to store?
310: if ($edit) {
1.21 www 311: # check if this was called with a query string
312: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['queryid']);
313: if ($env{'form.queryid'}) {
314: # yes, collect the remainder
315: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
316: ['title',
317: 'description',
318: 'url',
319: 'status',
320: 'enclosureurl',
321: 'enclosuretype']);
322: # my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_;
323:
324: &editentry($env{'form.queryid'},
325: $uname,$udom,$filename,
326: $env{'form.title'},
327: $env{'form.description'},
328: $env{'form.url'},
329: $env{'form.status'},
330: $env{'form.encurl'},
331: $env{'form.enctype'}
332: );
333: }
1.18 www 334: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
335: foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
336: if ($entry=~/^(\d+)\_status$/) {
337: my $id=$1;
338: if ($env{'form.'.$id.'_modified'}) {
339: &editentry($id,$uname,$udom,$feedname,
340: $env{'form.'.$id.'_title'},
341: $env{'form.'.$id.'_description'},
342: $env{'form.'.$id.'_url'},
1.21 www 343: $env{'form.'.$id.'_status'},
344: $env{'form.'.$id.'_enclosureurl'},
345: $env{'form.'.$id.'_enclosuretype'},
346: );
1.18 www 347: }
348: }
349: }
350: } #done storing
351:
352: $r->print("\n".
1.19 www 353: ($html?'<hr /><h3>':'<title>').
1.18 www 354: &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
1.20 www 355: ($displayoption eq 'hidden'?' ('.&mt('Hidden').')':'').
1.18 www 356: ($html?'</h3>'.($edit?'<form method="post"><br />'.
1.21 www 357: &mt('Name of this Feed').
1.18 www 358: ': <input type="text" size="50" name="newblogname" value="'.
359: $displayfeedname.'" />':'').'<ul>':'</title>'));
1.1 www 360: # Render private items?
1.18 www 361: my $viewpubliconly=1;
362: if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
363: $viewpubliconly=0;
364: }
1.1 www 365: # Get feed items
1.18 www 366: my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
367: foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
368: if ($entry=~/^(\d+)\_status$/) { # is an entry
369: my $id=$1;
370: if ($edit) {
371: my %lt=&Apache::lonlocal::texthash('public' => 'public',
372: 'private' => 'private',
373: 'hidden' => 'hidden',
374: 'delete' => 'delete',
375: 'store' => 'Store changes',
376: 'title' => 'Title',
377: 'link' => 'Link',
378: 'description' => 'Description');
379: my %status=();
380: unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
381: $status{$newsfeed{$id.'_status'}}='checked="checked"';
382: $r->print(<<ENDEDIT);
1.5 www 383: <li>
1.15 www 384: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
1.6 www 385:
386: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
1.5 www 387:
1.6 www 388: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
1.5 www 389:
1.6 www 390: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
1.5 www 391:
1.15 www 392: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
1.5 www 393: <br />
1.18 www 394: $lt{'title'}:
395: <input name='$id\_title' type='text' size='60' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
396: $lt{'description'}:<br />
1.6 www 397: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
1.18 www 398: $lt{'link'}:
399: <input name='$id\_link' type='text' size='60' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
1.6 www 400: <hr /></li>
1.5 www 401: ENDEDIT
1.18 www 402: } else { # not in edit mode, just displaying
403: if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
404: if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
405: $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
406: ($html?"</b><br />\n":"</title>\n<description>").
407: $newsfeed{$id.'_description'}.
408: ($html?"<br />\n<a href='":"</description>\n<link>").
409: "http://".$ENV{'HTTP_HOST'}.
410: $newsfeed{$id.'_link'}.
411: ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
1.17 www 412: # Enclosure? Get stats
1.18 www 413: if ($newsfeed{$id.'_enclosureurl'}) {
414: my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'});
415: if ($stat[7]) {
1.17 www 416: # Has non-zero length (and exists)
1.18 www 417: my $enclosuretype=$newsfeed{$id.'_enclosetype'};
418: $r->print(($html?"<a href='":"\n<enclosure url='").
419: $newsfeed{$id.'_enclosureurl'}."' length='".$stat[7].
420: "' type='".$enclosuretype.($html?"'>".&mt('Enclosure')."</a>":"' />"));
421: }
422: }
423: if ($html) { # is HTML
424: $r->print("\n<hr /></li>\n");
425: } else { # is RSS
426: $r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
1.17 www 427: }
1.18 www 428: } # end of "in edit mode"
429: } # end of rendering a real entry
430: } # end of loop through all keys
431: if ($html) {
432: $r->print('</ul>');
433: if ($edit) {
1.20 www 434: $r->print('<input type="hidden" name="newid" value="'.$newid.'"/><input type="submit" value="'.&mt('Store Marked Changes').'" />'.
435: ($displayoption eq 'hidden'?'<input type="submit" name="advertisethisblog" value="'.&mt('Advertise this Feed').'" />':
436: '<input type="submit" name="hidethisblog" value="'.&mt('Hide this Feed').'" />'));
1.1 www 437: }
438: }
1.18 www 439: } # was a real display feedname
440: $r->print(($html?'</form>'.&Apache::loncommon::end_page():'</channel></rss>'."\n"));
441: } # a real user
1.1 www 442: return OK;
1.18 www 443: } # end handler
1.1 www 444: 1;
445: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>