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

1.1       www         1: # The LearningOnline Network
                      2: # RSS Feeder
                      3: #
1.17    ! www         4: # $Id: lonrss.pm,v 1.16 2006/04/13 18:12:49 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;
                     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>'.
                     93: 		($edit?'':'<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.5       www       202:     } else {
                    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?
                    211:     if ($edit) {
                    212:         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
                    213: 	foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
                    214: 	    if ($entry=~/^(\d+)\_status$/) {
                    215: 		my $id=$1;
                    216: 		if ($env{'form.'.$id.'_modified'}) {
                    217: 		    &editentry($id,$uname,$udom,$feedname,
                    218: 			       $env{'form.'.$id.'_title'},
                    219: 			       $env{'form.'.$id.'_description'},
                    220: 			       $env{'form.'.$id.'_url'},
                    221: 			       $env{'form.'.$id.'_status'});
                    222: 		}
                    223: 	    }
                    224: 	}
                    225:     }
1.16      albertel  226:     my $newid = &get_new_feed_id();
1.1       www       227: # Is this user for real?
1.6       www       228:     my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
                    229:     if ($html) {
                    230: 	$r->print(&advertisefeeds($uname,$udom,$edit));
                    231:     } 
1.1       www       232:     if ($homeserver eq 'no_host') {
1.5       www       233: 	$r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
1.1       www       234:     } else {
                    235: # Course or user?
                    236: 	my $name='';
                    237: 	if ($uname=~/^\d/) {
                    238: 	    my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
                    239: 	    $name=$cenv{'description'};
                    240: 	} else {
                    241: 	    $name=&Apache::loncommon::nickname($uname,$udom);
                    242: 	}
1.5       www       243:         $r->print("\n".
                    244: 		  ($html?'<h3>':'<title>').
                    245: 		  &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
1.6       www       246: 		  ($html?'</h3>'.($edit?'<form method="post"><br />'.
                    247: 				  &mt('Name of blog/journal').
                    248: 				  ': <input type="text" size="50" name="newblogname" value="'.
                    249: 				  $displayfeedname.'" />':'').'<ul>':'</title>'));
1.1       www       250: # Render private items?
                    251:         my $viewpubliconly=1;
                    252:         if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
                    253: 	    $viewpubliconly=0;
                    254: 	}
                    255: # Get feed items
                    256:         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
1.15      www       257: 	foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
1.3       albertel  258: 	    if ($entry=~/^(\d+)\_status$/) {
1.1       www       259: 		my $id=$1;
1.5       www       260: 		if ($edit) {
                    261: 		    my %lt=&Apache::lonlocal::texthash('public' => 'public',
                    262: 						       'private' => 'private',
                    263: 						       'hidden' => 'hidden',
1.6       www       264: 						       'delete' => 'delete',
                    265: 						       'store' => 'Store changes');
1.5       www       266: 		    my %status=();
1.15      www       267:                     unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
1.5       www       268: 		    $status{$newsfeed{$id.'_status'}}='checked="checked"';
                    269: 		    $r->print(<<ENDEDIT);
                    270: <li>
1.15      www       271: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
1.6       www       272: &nbsp;&nbsp;
                    273: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
1.5       www       274: &nbsp;&nbsp;
1.6       www       275: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
1.5       www       276: &nbsp;&nbsp;
1.6       www       277: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
1.5       www       278: &nbsp;&nbsp;
1.15      www       279: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
1.5       www       280: <br />
1.6       www       281: <input name='$id\_title' type='text' size='80' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
                    282: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
                    283: <input name='$id\_link' type='text' size='80' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
                    284: <hr /></li>
1.5       www       285: ENDEDIT
                    286: 		} else {
                    287: 		    if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
                    288: 		    if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
                    289: 		    $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
                    290: 			      ($html?"</b><br />\n":"</title>\n<description>").
                    291: 			      $newsfeed{$id.'_description'}.
                    292: 			      ($html?"<br />\n<a href='":"</description>\n<link>").
                    293: 			      "http://".$ENV{'HTTP_HOST'}.
                    294: 			      $newsfeed{$id.'_link'}.
                    295: 			      ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
1.17    ! www       296: # Enclosure? Get stats
1.5       www       297: 		    if ($newsfeed{$id.'_enclosureurl'}) {
1.17    ! www       298: 			my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'});
        !           299: 			if ($stat[7]) {
        !           300: # Has non-zero length (and exists)
        !           301: 			    my $enclosuretype=$newsfeed{$id.'_enclosetype'};
        !           302: 			    $r->print(($html?"<a href='":"\n<enclosure url='").
        !           303: 				      $newsfeed{$id.'_enclosureurl'}."' length='".$stat[7].
        !           304: 				      "' type='".$enclosuretype.($html?"'>".&mt('Enclosure')."</a>":"' />"));
        !           305: 			}
1.5       www       306: 		    }
                    307: 		    if ($html) {
                    308: 			$r->print("\n<hr /></li>\n");
                    309: 		    } else {
                    310: 			$r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
                    311: 		    }
1.1       www       312: 		}
                    313: 	    }
                    314: 	}
                    315:     }
1.15      www       316:     $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"));
1.1       www       317:     return OK;
                    318: } 
                    319: 1;
                    320: __END__

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