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

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

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