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

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

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