--- loncom/interface/lonrss.pm	2005/11/18 13:35:30	1.2
+++ loncom/interface/lonrss.pm	2006/04/22 20:58:32	1.17
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # RSS Feeder
 #
-# $Id: lonrss.pm,v 1.2 2005/11/18 13:35:30 www Exp $
+# $Id: lonrss.pm,v 1.17 2006/04/22 20:58:32 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -36,12 +36,13 @@ use Apache::lontexconvert;
 use Apache::lonlocal;
 use Apache::lonhtmlcommon;
 
-my $feedcounter=0;
 
 sub filterfeedname {
     my $filename=shift;
-    $filename=~s/\.rss$//;
+    $filename=~s/(\_rss\.html|\.rss)$//;
     $filename=~s/\W//g;
+    $filename=~s/\_rssfeed$//;
+    $filename=~s/^nohist\_//;
     return $filename;
 }
 
@@ -76,14 +77,20 @@ sub renamefeed {
 }
 
 sub advertisefeeds {
-    my ($uname,$udom)=@_;
+    my ($uname,$udom,$edit)=@_;
     my $feeds='';
     my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
-    foreach (sort keys %feednames) {
-	if ($_!~/^error\:/) {
-	    my $url='feed://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$_.'.rss';
-	    $feeds.='<li><a href="'.$url.'">'.
-		$feednames{$_}.'</a><br /><tt>'.$url.'</tt></li>';
+    my $mode='public';
+    if ($edit) {
+	$mode='adm';
+    }
+    foreach my $feed (sort(keys(%feednames))) {
+	if ($feed!~/^error\:/) {
+	    my $feedurl='http://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
+	    my $htmlurl='http://'.$ENV{'HTTP_HOST'}.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html';
+	    $feeds.='<li>'.$feednames{$feed}.
+		'<br />'.($edit?&mt('Edit'):'HTML').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a>'.
+		($edit?'':'<br />RSS: <a href="'.$feedurl.'"><tt>'.$feedurl.'</tt></a>').'</li>';
 	}
     }
     if ($feeds) {
@@ -93,14 +100,29 @@ sub advertisefeeds {
     }
 }
 
+sub rss_link {
+    my ($url) = @_;
+    return qq|<link rel="alternate" type="application/rss+xml" title="Course Announcements" href="$url" />|;
+}
+
+{
+    my $feedcounter;
+    sub get_new_feed_id {
+	$feedcounter++;
+	return time().'00000'.$$.'00000'.$feedcounter;
+    }
+}
+
 sub addentry {
-    $feedcounter++;
-    my $id=time.'00000'.$$.'00000'.$feedcounter;
+    my $id=&get_new_feed_id();
     return &editentry($id,@_);
 }
 
 sub editentry {
-    my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
+    my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_;
+    if ($status eq 'deleted') {
+	return &changestatus($id,$uname,$udom,$filename,$status);
+    }
     my $feedname=&feedname($filename);
     &Apache::lonnet::put('nohist_all_rss_feeds',
 			 { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) },
@@ -110,7 +132,6 @@ sub editentry {
 	$id.'_description' => $description,
 	$id.'_link' => $url,
 	$id.'_enclosureurl' => $encurl,
-	$id.'_enclosurelength' => $enclength,
 	$id.'_enclosuretype' => $enctype,
 	$id.'_status' => $status},$udom,$uname);
 }
@@ -123,7 +144,6 @@ sub changestatus {
 					       $id.'_description',
 					       $id.'_link',
 					       $id.'_enclosureurl',
-					       $id.'_enclosurelength',
 					       $id.'_enclosuretype',
 					       $id.'_status'],$udom,$uname);
     } else {
@@ -131,23 +151,86 @@ sub changestatus {
     }
 }
 
+sub changed_js {
+    return <<ENDSCRIPT;
+<script type="text/javascript">
+    function changed(tform,id) {
+        tform.elements[id+"_modified"].checked=true;
+    }
+</script>
+ENDSCRIPT
+}
+
+sub determine_enclosure_types {
+    my ($url)=@_;
+    my ($ending)=($url=~/\.(\w+)$/);
+    return &Apache::loncommon::filemimetype($ending);
+}
+
 sub handler {
-    my $r = shift;
-    &Apache::loncommon::content_type($r,'text/xml');
+    my ($r) = @_;
+
+    my $edit=0;
+    my $html=0;
+    my (undef,$mode,$udom,$uname,$filename)=split(/\//,$r->uri);
+    if (($mode eq 'adm') && ($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
+	$edit=1;
+	$html=1;
+    }
+    if ($filename=~/\.html$/) {
+	$html=1;
+    }
+    if ($html) {
+	&Apache::loncommon::content_type($r,'text/html');
+    } else {
+# Workaround Mozilla/Firefox
+#	&Apache::loncommon::content_type($r,'application/rss+xml');
+	&Apache::loncommon::content_type($r,'text/xml');
+    }
     $r->send_http_header;
     return OK if $r->header_only;
 
-    my (undef,undef,$udom,$uname,$filename)=split(/\//,$r->uri);
     my $filterfeedname=&filterfeedname($filename);
     my $feedname=&feedname($filename);
     my $displayfeedname=&displayfeedname($filename,$uname,$udom);
-    $r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
-	      "\n<link>http://".$ENV{'HTTP_HOST'}.'/</link>'.
-	      "\n<description>".&mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').'</description>');
+    if ($html) {
+	$r->print(&Apache::loncommon::start_page($displayfeedname,undef,
+						 {'domain'         => $udom,
+						  'force_register' =>
+						      $env{'form.register'}}).
+		  &changed_js());
+    } else {
+	$r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
+		  "\n<link>http://".$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.
+		  $filterfeedname.'_rss.html</link>'.
+		  "\n<description>".
+		  &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').
+		  '</description>');
+    }
+# Do we have stuff to store?
+    if ($edit) {
+        my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
+	foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
+	    if ($entry=~/^(\d+)\_status$/) {
+		my $id=$1;
+		if ($env{'form.'.$id.'_modified'}) {
+		    &editentry($id,$uname,$udom,$feedname,
+			       $env{'form.'.$id.'_title'},
+			       $env{'form.'.$id.'_description'},
+			       $env{'form.'.$id.'_url'},
+			       $env{'form.'.$id.'_status'});
+		}
+	    }
+	}
+    }
+    my $newid = &get_new_feed_id();
 # Is this user for real?
-    my $homeserver=&Apache::lonnet::homeserver($uname,$udom); 
+    my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
+    if ($html) {
+	$r->print(&advertisefeeds($uname,$udom,$edit));
+    } 
     if ($homeserver eq 'no_host') {
-	$r->print('<title>'.&mt('No feed available').'</title>');
+	$r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
     } else {
 # Course or user?
 	my $name='';
@@ -157,7 +240,13 @@ sub handler {
 	} else {
 	    $name=&Apache::loncommon::nickname($uname,$udom);
 	}
-        $r->print("\n<title>".&mt('LON-CAPA RSS Feed "[_1]" for [_2]',$displayfeedname,$name).'</title>');
+        $r->print("\n".
+		  ($html?'<h3>':'<title>').
+		  &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
+		  ($html?'</h3>'.($edit?'<form method="post"><br />'.
+				  &mt('Name of blog/journal').
+				  ': <input type="text" size="50" name="newblogname" value="'.
+				  $displayfeedname.'" />':'').'<ul>':'</title>'));
 # Render private items?
         my $viewpubliconly=1;
         if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
@@ -165,25 +254,67 @@ sub handler {
 	}
 # Get feed items
         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
-	foreach (sort keys %newsfeed) {
-	    if ($_=~/^(\d+)\_status$/) {
+	foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
+	    if ($entry=~/^(\d+)\_status$/) {
 		my $id=$1;
-		if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
-		if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
-		$r->print("\n<item>\n<title>".$newsfeed{$id.'_title'}."</title>\n<description>".
-			  $newsfeed{$id.'_description'}."</description>\n<link>".
-			  $newsfeed{$id.'_link'}."</link>\n");
-		if ($newsfeed{$id.'_enclosureurl'}) {
-		    $r->print("\n<enclosure url='".$newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
-			      "' type='".$newsfeed{$id.'_enclosuretype'}."' />");
+		if ($edit) {
+		    my %lt=&Apache::lonlocal::texthash('public' => 'public',
+						       'private' => 'private',
+						       'hidden' => 'hidden',
+						       'delete' => 'delete',
+						       'store' => 'Store changes');
+		    my %status=();
+                    unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
+		    $status{$newsfeed{$id.'_status'}}='checked="checked"';
+		    $r->print(<<ENDEDIT);
+<li>
+<label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
+&nbsp;&nbsp;
+<label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
+&nbsp;&nbsp;
+<label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
+&nbsp;&nbsp;
+<label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
+&nbsp;&nbsp;
+<label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
+<br />
+<input name='$id\_title' type='text' size='80' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
+<textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
+<input name='$id\_link' type='text' size='80' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
+<hr /></li>
+ENDEDIT
+		} else {
+		    if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
+		    if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
+		    $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
+			      ($html?"</b><br />\n":"</title>\n<description>").
+			      $newsfeed{$id.'_description'}.
+			      ($html?"<br />\n<a href='":"</description>\n<link>").
+			      "http://".$ENV{'HTTP_HOST'}.
+			      $newsfeed{$id.'_link'}.
+			      ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
+# Enclosure? Get stats
+		    if ($newsfeed{$id.'_enclosureurl'}) {
+			my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'});
+			if ($stat[7]) {
+# Has non-zero length (and exists)
+			    my $enclosuretype=$newsfeed{$id.'_enclosetype'};
+			    $r->print(($html?"<a href='":"\n<enclosure url='").
+				      $newsfeed{$id.'_enclosureurl'}."' length='".$stat[7].
+				      "' type='".$enclosuretype.($html?"'>".&mt('Enclosure')."</a>":"' />"));
+			}
+		    }
+		    if ($html) {
+			$r->print("\n<hr /></li>\n");
+		    } else {
+			$r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
+		    }
 		}
-		$r->print("\n<guid>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
 	    }
 	}
     }
-    $r->print("\n</channel></rss>\n");
+    $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"));
     return OK;
 } 
-
 1;
 __END__