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

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

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