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

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

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