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

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

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