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

1.1       www         1: # The LearningOnline Network
                      2: # RSS Feeder
                      3: #
1.19    ! www         4: # $Id: lonrss.pm,v 1.18 2006/05/10 15:52:11 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: 
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: 
                     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) {
1.19    ! www       215: # Any new feeds or renaming of feeds?
        !           216: 	if ($edit) {
        !           217: # New feed?
        !           218: 	    if ($env{'form.namenewblog'}=~/\w/) {
        !           219: 		&namefeed($env{'form.namenewblog'},$uname,$udom,$env{'form.namenewblog'});
        !           220: 	    }
        !           221: # Old feed that is being renamed?
        !           222: 	    if (($displayfeedname) && ($env{'form.newblogname'}=~/\w/)) {
        !           223: 		if ($env{'form.newblogname'} ne $displayfeedname) {
        !           224: 		    &namefeed($feedname,$uname,$udom,$env{'form.newblogname'});
        !           225: 		    $displayfeedname=&displayfeedname($filename,$uname,$udom);
        !           226: 		}
        !           227: 	    }
        !           228: 	}
1.6       www       229: 	$r->print(&advertisefeeds($uname,$udom,$edit));
                    230:     } 
1.1       www       231:     if ($homeserver eq 'no_host') {
1.5       www       232: 	$r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
1.18      www       233:     } else { # is indeed a user
1.1       www       234: # Course or user?
                    235: 	my $name='';
                    236: 	if ($uname=~/^\d/) {
                    237: 	    my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
                    238: 	    $name=$cenv{'description'};
                    239: 	} else {
                    240: 	    $name=&Apache::loncommon::nickname($uname,$udom);
                    241: 	}
1.19    ! www       242: # Add a new feed
        !           243:         if (($html) && ($edit)) {
        !           244: 	    $r->print('<form method="post">');
        !           245:             $r->print(&mt('Name for New Blog').": <input type='text' size='40' name='namenewblog' />");
        !           246: 	    $r->print('<input type="submit" value="'.&mt('Start a New Blog').'" />');
        !           247: 	    $r->print('</form>');
        !           248: 	}
1.18      www       249:         if ($displayfeedname) { # this is an existing feed
                    250: # Anything to store?
                    251: 	    if ($edit) {
                    252: 		my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
                    253: 		foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
                    254: 		    if ($entry=~/^(\d+)\_status$/) {
                    255: 			my $id=$1;
                    256: 			if ($env{'form.'.$id.'_modified'}) {
                    257: 			    &editentry($id,$uname,$udom,$feedname,
                    258: 				       $env{'form.'.$id.'_title'},
                    259: 				       $env{'form.'.$id.'_description'},
                    260: 				       $env{'form.'.$id.'_url'},
                    261: 				       $env{'form.'.$id.'_status'});
                    262: 			}
                    263: 		    }
                    264: 		}
                    265: 	    } #done storing
                    266: 
                    267: 	    $r->print("\n".
1.19    ! www       268: 		      ($html?'<hr /><h3>':'<title>').
1.18      www       269: 		      &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
                    270: 		      ($html?'</h3>'.($edit?'<form method="post"><br />'.
                    271: 				      &mt('Name of blog/journal').
                    272: 				      ': <input type="text" size="50" name="newblogname" value="'.
                    273: 				      $displayfeedname.'" />':'').'<ul>':'</title>'));
1.1       www       274: # Render private items?
1.18      www       275: 	    my $viewpubliconly=1;
                    276: 	    if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
                    277: 		$viewpubliconly=0;
                    278: 	    }
1.1       www       279: # Get feed items
1.18      www       280: 	    my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
                    281: 	    foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
                    282: 		if ($entry=~/^(\d+)\_status$/) { # is an entry
                    283: 		    my $id=$1;
                    284: 		    if ($edit) {
                    285: 			my %lt=&Apache::lonlocal::texthash('public' => 'public',
                    286: 							   'private' => 'private',
                    287: 							   'hidden' => 'hidden',
                    288: 							   'delete' => 'delete',
                    289: 							   'store' => 'Store changes',
                    290: 							   'title' => 'Title',
                    291: 							   'link' => 'Link',
                    292: 							   'description' => 'Description');
                    293: 			my %status=();
                    294: 			unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
                    295: 			$status{$newsfeed{$id.'_status'}}='checked="checked"';
                    296: 			$r->print(<<ENDEDIT);
1.5       www       297: <li>
1.15      www       298: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
1.6       www       299: &nbsp;&nbsp;
                    300: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
1.5       www       301: &nbsp;&nbsp;
1.6       www       302: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
1.5       www       303: &nbsp;&nbsp;
1.6       www       304: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
1.5       www       305: &nbsp;&nbsp;
1.15      www       306: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
1.5       www       307: <br />
1.18      www       308: $lt{'title'}:
                    309: <input name='$id\_title' type='text' size='60' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
                    310: $lt{'description'}:<br />
1.6       www       311: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
1.18      www       312: $lt{'link'}:
                    313: <input name='$id\_link' type='text' size='60' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
1.6       www       314: <hr /></li>
1.5       www       315: ENDEDIT
1.18      www       316: 		    } else { # not in edit mode, just displaying
                    317: 			if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
                    318: 			if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
                    319: 			$r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
                    320: 				  ($html?"</b><br />\n":"</title>\n<description>").
                    321: 				  $newsfeed{$id.'_description'}.
                    322: 				  ($html?"<br />\n<a href='":"</description>\n<link>").
                    323: 				  "http://".$ENV{'HTTP_HOST'}.
                    324: 				  $newsfeed{$id.'_link'}.
                    325: 				  ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
1.17      www       326: # Enclosure? Get stats
1.18      www       327: 			if ($newsfeed{$id.'_enclosureurl'}) {
                    328: 			    my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'});
                    329: 			    if ($stat[7]) {
1.17      www       330: # Has non-zero length (and exists)
1.18      www       331: 				my $enclosuretype=$newsfeed{$id.'_enclosetype'};
                    332: 				$r->print(($html?"<a href='":"\n<enclosure url='").
                    333: 					  $newsfeed{$id.'_enclosureurl'}."' length='".$stat[7].
                    334: 					  "' type='".$enclosuretype.($html?"'>".&mt('Enclosure')."</a>":"' />"));
                    335: 			    }
                    336: 			}
                    337: 			if ($html) { # is HTML
                    338: 			    $r->print("\n<hr /></li>\n");
                    339: 			} else { # is RSS
                    340: 			    $r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
1.17      www       341: 			}
1.18      www       342: 		    } # end of "in edit mode"
                    343: 		} # end of rendering a real entry
                    344: 	    } # end of loop through all keys
                    345: 	    if ($html) {
                    346: 		$r->print('</ul>');
                    347: 		if ($edit) {
                    348: 		    $r->print('<input type="hidden" name="newid" value="'.$newid.'"/><input type="submit" value="'.&mt('Store Marked Changes').'" />');
1.1       www       349: 		}
                    350: 	    }
1.18      www       351: 	} # was a real display feedname
                    352: 	$r->print(($html?'</form>'.&Apache::loncommon::end_page():'</channel></rss>'."\n"));
                    353:     } # a real user
1.1       www       354:     return OK;
1.18      www       355: } # end handler
1.1       www       356: 1;
                    357: __END__

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