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

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

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