[LON-CAPA-cvs] cvs: loncom /interface lonsearchcat.pm

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 20 Jun 2002 19:43:50 -0000


This is a MIME encoded message

--matthew1024602230
Content-Type: text/plain

matthew		Thu Jun 20 15:43:50 2002 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Cleanups to &output_results.  
      Moved basic search results HTML header to a new subroutine.
      Moved search progress pop-up window creation to a new subroutine. 
      Replaced "BLOOP" loop with something meaningful.
      Replaced "RLOOP" loop with something meaningful.
      "WLOOP"-ectomy is scheduled for the next commit.
  &make_popwin (code from &output_results) now addresses the progress icons
      using img_$domain_$server instead of a number.  We do not need to use 
      numbers when we have (javascript friendly?) id strings at our disposal.
  &popwin_imgupdate changed to address images with img_$domain_$server instead
      of relying on a number.
  
  
--matthew1024602230
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020620154350.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.124 loncom/interface/lonsearchcat.pm:1.125
--- loncom/interface/lonsearchcat.pm:1.124	Thu Jun 20 10:31:31 2002
+++ loncom/interface/lonsearchcat.pm	Thu Jun 20 15:43:50 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.124 2002/06/20 14:31:31 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.125 2002/06/20 19:43:50 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -524,7 +524,6 @@
 
 sub make_persistent {
     my $persistent='';
-    
     foreach (keys %ENV) {
 	if (/^form\./ && !/submit/) {
 	    my $name=$_;
@@ -532,7 +531,7 @@
 	    $ENV{$key}=~s/\'//g; # do not mess with html field syntax
 	    $name=~s/^form\.//;
 	    $persistent.=<<END;
-<input type='hidden' name='$name' value='$ENV{$key}' />
+<input type="hidden" name="$name" value="$ENV{$key}" />
 END
         }
     }
@@ -1029,104 +1028,16 @@
     my %ENV=%{$envhash};
     my %rhash=%{$replyref};
     my $compiledresult='';
-    my $timeremain=300;
+    my $timeremain=300; # (seconds)
     my $elapsetime=0;
     my $resultflag=0;
     my $tflag=1;
-
+    #
     # make query information persistent to allow for subsequent revision
     my $persistent=&make_persistent();
-
-    # output beginning of search page
-	$r->print(<<BEGINNING);
-<html>
-<head>
-<title>The LearningOnline Network with CAPA</title>
-BEGINNING
-
-    # conditional output of script functions dependent on the mode in
-    # which the search was invoked
-    if ($ENV{'form.catalogmode'} eq 'interactive'){
-	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
-            $r->print(<<SCRIPT) 
-<script type="text/javascript">
-    function select_data(title,url) {
-	changeTitle(title);
-	changeURL(url);
-	self.close();
-    }
-    function changeTitle(val) {
-	if (opener.inf.document.forms.resinfo.elements.t) {
-	    opener.inf.document.forms.resinfo.elements.t.value=val;
-	}
-    }
-    function changeURL(val) {
-	if (opener.inf.document.forms.resinfo.elements.u) {
-	    opener.inf.document.forms.resinfo.elements.u.value=val;
-	}
-    }
-</script>
-SCRIPT
-        } elsif ($ENV{'form.mode'} eq 'edit') {
-            my $form = $ENV{'form.form'};
-            my $element = $ENV{'form.element'};
-            $r->print(<<SCRIPT) 
-<script type="text/javascript">
-function select_data(title,url) {
-    changeURL(url);
-    self.close();
-}
-function changeTitle(val) {
-}
-function changeURL(val) {
-    if (window.opener.document) {
-        window.opener.document.forms["$form"].elements["$element"].value=val;
-    } else {
-	var url = 'forms[\"$form\"].elements[\"$element\"].value';
-        alert("Unable to transfer data to "+url);
-    }
-}
-</script>
-SCRIPT
-        }
-    }
-    $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'groupsearch';
-<script type="text/javascript">
-    function select_data(title,url) {
-//	alert('DEBUG: Should be storing '+title+' and '+url);
-    }
-    function queue(val) {
-	if (eval("document.forms.results.returnvalues["+val+"].checked")) {
-	    document.forms.results.acts.value+='1a'+val+'b';
-	}
-	else {
-	    document.forms.results.acts.value+='0a'+val+'b';
-	}
-    }
-    function select_group() {
-	window.location=
-    "/adm/groupsort?mode=$ENV{'form.mode'}&catalogmode=groupsearch&acts="+
-	    document.forms.results.acts.value;
-    }
-</script>
-SCRIPT
-    $r->print(<<SCRIPT);
-<script type="text/javascript">
-    function displayinfo(val) {
-	popwin.document.forms.popremain.sdetails.value=val;
-    }
-    function openhelp(val) {
-	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
-	     'scrollbars=1,width=400,height=300');
-	openhelpwin.focus();
-    }
-    function abortsearch(val) {
-	popwin.close();
-    }
-</script>
-SCRIPT
+    # spit out the generic header
+    $r->print(&search_results_header());
     $r->rflush();
-
     # begin showing the cataloged results
     $r->print(<<CATALOGBEGIN);
 </head>
@@ -1146,146 +1057,66 @@
 <hr />
 <h3>Search Query</h3>
 CATALOGCONTROLS
+    #
+    # Remind them what they searched for
+    #
     if ($mode eq 'Basic') {
-	$r->print(<<RESULTS);
-<p>
-<b>Basic search:</b> $ENV{'form.basicexp'}
-</p>
-RESULTS
-    }
-    elsif ($mode eq 'Advanced') {
-	$r->print(<<RESULTS);
-<p>
-<b>Advanced search</b>
-$query
-</p>
-RESULTS
+	$r->print('<p><b>Basic search:</b> '.$ENV{'form.basicexp'}.'</p>');
+    } elsif ($mode eq 'Advanced') {
+	$r->print('<p><b>Advanced search</b> '.$query.'</p>');
     }
     $r->print('<h3>Search Results</h3>');
     $r->rflush();
-    my $servernum=(keys %rhash)+0;
-
-    # define server grid (shows status of multiple machines)
-    my $hcinit;
-    my $grid="'<br />'+";
-    $grid.="\n";
-    my $sn=1;
-    for my $sk (sort keys %rhash) {
-	# '<a href="
-	$grid.="'<a href=\"";
-	# javascript:displayinfo('+
-	$grid.="javascript:opener.displayinfo('+";
-	# "'"+'key
-	$grid.="\"'\"+'";
-	$grid.=$sk;
-	my $hc;
-	if ($rhash{$sk} eq 'con_lost') {
-	    $hc="BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR ";
-	}
-	else {
-	    $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
-	    $hcinit.="hc[\"$sk\"]=\"not yet connected...\";";
-	}
-	$grid.=" hitcount=".$hc;
-	$grid.=" domain=".$hostdomains{$sk};
-	$grid.=" IP=".$hostips{$sk};
-	# '+"'"+'">'+
-	$grid.="'+\"'\"+')\">'+";
-	$grid.="\n";
-	$grid.="'<img border=\"0\" name=\"img".$sn."\"".
-	    " src=\"/adm/lonIcons/srvnull.gif\" alt=\"".$sk."\" /></a>'+\n";
-	$grid.="'<br />'+\n" unless $sn%10;
-        $sn++;
-    }
-	    $r->print(<<ENDPOP);
-<script type="text/javascript">
-    popwin=open('','popwin','scrollbars=1,width=400,height=220');
-    popwin.focus();
-    popwin.document.writeln('<'+'html>');
-    popwin.document.writeln('<'+'head>');
-    popwin.document.writeln('<'+'script>');
-    popwin.document.writeln('hc=new Array();$hcinit');
-    popwin.document.writeln('<'+'/script>');
-    popwin.document.writeln('<'+'/head>'+
-        '<'+'body bgcolor="#FFFFFF">'+
-	'<'+'image name="whirly" align="right" src="/adm/lonIcons/'+
-	'lonanim.gif" '+
-	'alt="animated logo" />'+
-	'<'+'h3>Search Results Progress<'+'/h3>'+
-        '<'+'form name="popremain">'+
-        '<'+'tt>'+
-	'<'+'br clear="all"/><i>PLEASE BE PATIENT</i>'+
-	'<'+'br />SCANNING $servernum SERVERS'+
-	'<'+'br clear="all" />Number of record hits found '+
-	'<'+'input type="text" size="10" name="numhits"'+
-	' value="0" />'+
-	'<'+'br clear="all" />Time elapsed '+
-	'<'+'input type="text" size="10" name="elapsetime"'+
-	' value="0" />'+
-	'<'+'br />'+
-	'SERVER GRID (click on any cell for details)'+
-        $grid
-        '<'+'br />'+
-	'Server details '+
-	'<'+'input type="text" size="35" name="sdetails"'+
-	' value="" />'+
-	'<'+'br />'+
-	' <'+'input type="button" name="button"'+
-	' value="close this window" '+
-	' onClick="javascript:opener.abortsearch()" />'+
-	' <'+'input type="button" name="button"'+
-	' value="help" onClick="javascript:opener.openhelp()" />'+
-	'<'+'/tt>'+
-        '<'+'/form>'+
-        '<'+'/body><'+'/html>');
-    popwin.document.close();
-</script>
-ENDPOP
+    #
+    # make the pop-up window for status
+    #
+    $r->print(&make_popwin(%rhash));
     $r->rflush();
-
+    ##
+    ## Prepare for the main loop below
+    ##
     my $servercount=0;
     my $hitcountsum=0;
-    my $bloop=$servernum;
-    my %orkey;
-  BLOOP: while(1) {
-      my $sn=0;
-      last BLOOP unless $bloop;
-      last BLOOP unless $timeremain;
-    RLOOP: foreach my $rkey (sort keys %rhash) {
-	$sn++;
-	next RLOOP if $orkey{$rkey};
+    my $servernum=(keys %rhash);
+    my $serversleft=$servernum;
+    ##
+    ## Run until we run out of time or we run out of servers
+    ##
+    while($serversleft && $timeremain) {
+      ##
+      ## %rhash has servers deleted from it as results come in 
+      ## (within the foreach loop below).
+      ##
+      foreach my $rkey (sort keys %rhash) {
 	$servercount++;
 	$tflag=1;
 	$compiledresult='';
 	my $hostname=$rkey;
 	my $reply=$rhash{$rkey};
 	my @results;
-	
-	my $replyfile='';
-
 	if ($reply eq 'con_lost') {
-	    &popwin_imgupdate($r,$sn,"srvbad.gif");
-	    $bloop--;
-	    $orkey{$rkey}=1;
-	}
-	else {
-	    $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
-	    $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
+	    &popwin_imgupdate($r,$rkey,"srvbad.gif");
+	    $serversleft--;
+            delete $rhash{$rkey};
+	} else {
+            # must do since 'use strict' checks for tainting
+	    $reply=~/^([\.\w]+)$/; 
+	    my $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
 	    $reply=~/(.*?)\_/;
 	    {
 		my $temp=0;
 	      WLOOP: while (1) {
 		  if (-e $replyfile && $tflag) {
-		      &popwin_imgupdate($r,$sn,"srvhalf.gif");
+		      &popwin_imgupdate($r,$rkey,"srvhalf.gif");
 		      &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
 				 '"still transferring..."'.';');
 		      $tflag=0;
 		  }
 		  if (-e "$replyfile.end") {
-		      $bloop--;
-		      $orkey{$rkey}=1;
+		      $serversleft--;
+                      delete $rhash{$rkey};
 		      if (-s $replyfile) {
-			  &popwin_imgupdate($r,$sn,"srvgood.gif");
+			  &popwin_imgupdate($r,$rkey,"srvgood.gif");
 			  my $fh=Apache::File->new($replyfile) or 
 			      ($r->print('ERROR: file '.
 					 $replyfile.' cannot be opened') and
@@ -1299,21 +1130,16 @@
 				    'numhits.value='.$hitcountsum.';');
 		      }
 		      else {
-			  &popwin_imgupdate($r,$sn,"srvempty.gif");
+			  &popwin_imgupdate($r,$rkey,"srvempty.gif");
 			  &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
 		      }
 		      last WLOOP;
-		  }
-		  if ($temp>1) {
-		      sleep 1;
-		      $timeremain--;
-		      $elapsetime++;
-		      last WLOOP;
-		  }
+		  } # end of if ( -e "$replyfile.end")
 		  last WLOOP unless $timeremain;
-		  sleep 1;
+		  sleep 1;    # wait for daemons to write files?
 		  $timeremain--;
 		  $elapsetime++;
+		  last WLOOP if ($temp>1);
 		  &popwin_js($r,"popwin.document.popremain.".
 			       "elapsetime.value=$elapsetime;");
 		  $temp++;
@@ -1321,7 +1147,7 @@
 	    }
 	    &popwin_js($r,'popwin.document.whirly.'.
 		       'src="/adm/lonIcons/lonanimend.gif";');
-	}
+	} # end of if ($reply eq 'con_lost') else statement
 	my $customshow='';
 	my $extrashow='';
 	my @customfields;
@@ -1484,6 +1310,204 @@
 ######################################################################
 ######################################################################
 
+=pod
+
+=item &search_results_header
+
+=cut
+
+######################################################################
+######################################################################
+sub search_results_header {
+    my $result = '';
+    # output beginning of search page
+    $result.=<<BEGINNING;
+<html>
+<head>
+<title>The LearningOnline Network with CAPA</title>
+BEGINNING
+    # conditional output of script functions dependent on the mode in
+    # which the search was invoked
+    if ($ENV{'form.catalogmode'} eq 'interactive'){
+	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
+            $result.=<<SCRIPT;
+<script type="text/javascript">
+    function select_data(title,url) {
+	changeTitle(title);
+	changeURL(url);
+	self.close();
+    }
+    function changeTitle(val) {
+	if (opener.inf.document.forms.resinfo.elements.t) {
+	    opener.inf.document.forms.resinfo.elements.t.value=val;
+	}
+    }
+    function changeURL(val) {
+	if (opener.inf.document.forms.resinfo.elements.u) {
+	    opener.inf.document.forms.resinfo.elements.u.value=val;
+	}
+    }
+</script>
+SCRIPT
+        } elsif ($ENV{'form.mode'} eq 'edit') {
+            my $form = $ENV{'form.form'};
+            my $element = $ENV{'form.element'};
+            $result.=<<SCRIPT;
+<script type="text/javascript">
+function select_data(title,url) {
+    changeURL(url);
+    self.close();
+}
+function changeTitle(val) {
+}
+function changeURL(val) {
+    if (window.opener.document) {
+        window.opener.document.forms["$form"].elements["$element"].value=val;
+    } else {
+	var url = 'forms[\"$form\"].elements[\"$element\"].value';
+        alert("Unable to transfer data to "+url);
+    }
+}
+</script>
+SCRIPT
+        }
+    }
+    $result.=<<SCRIPT if $ENV{'form.catalogmode'} eq 'groupsearch';
+<script type="text/javascript">
+    function select_data(title,url) {
+//	alert('DEBUG: Should be storing '+title+' and '+url);
+    }
+    function queue(val) {
+	if (eval("document.forms.results.returnvalues["+val+"].checked")) {
+	    document.forms.results.acts.value+='1a'+val+'b';
+	}
+	else {
+	    document.forms.results.acts.value+='0a'+val+'b';
+	}
+    }
+    function select_group() {
+	window.location=
+    "/adm/groupsort?mode=$ENV{'form.mode'}&catalogmode=groupsearch&acts="+
+	    document.forms.results.acts.value;
+    }
+</script>
+SCRIPT
+    $result.=<<SCRIPT;
+<script type="text/javascript">
+    function displayinfo(val) {
+	popwin.document.forms.popremain.sdetails.value=val;
+    }
+    function openhelp(val) {
+	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
+	     'scrollbars=1,width=400,height=300');
+	openhelpwin.focus();
+    }
+    function abortsearch(val) {
+	popwin.close();
+    }
+</script>
+SCRIPT
+    return $result;
+}
+
+######################################################################
+######################################################################
+
+=pod
+
+=item &make_popwin()
+
+Returns html with javascript in it to open up the status window.
+
+=cut
+
+######################################################################
+######################################################################
+sub make_popwin {
+    my %rhash = @_;
+    my $servernum=(keys %rhash);
+    my $hcinit;
+    my $grid="'<br />'+\n";
+    # $sn is the server number, used ONLY to make sure we have
+    # rows of 10 each.  No longer used to index images.
+    my $sn=1;
+    foreach my $sk (sort keys %rhash) {
+	# '<a href="
+	$grid.="'<a href=\"";
+	# javascript:displayinfo('+
+	$grid.="javascript:opener.displayinfo('+";
+	# "'"+'key
+	$grid.="\"'\"+'";
+	$grid.=$sk;
+	my $hc;
+	if ($rhash{$sk} eq 'con_lost') {
+	    $hc="BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR ";
+	}
+	else {
+	    $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
+	    $hcinit.="hc[\"$sk\"]=\"not yet connected...\";";
+	}
+	$grid.=" hitcount=".$hc;
+	$grid.=" domain=".$hostdomains{$sk};
+	$grid.=" IP=".$hostips{$sk};
+	# '+"'"+'">'+
+	$grid.="'+\"'\"+')\">'+";
+	$grid.="\n";
+	$grid.="'<img border=\"0\" name=\"img_".$hostdomains{$sk}.'_'.$sk."\"".
+	    " src=\"/adm/lonIcons/srvnull.gif\" alt=\"".$sk."\" /></a>'+\n";
+	$grid.="'<br />'+\n" unless $sn%10;
+        $sn++;
+    }
+    my $result.=<<ENDPOP;
+<script type="text/javascript">
+    popwin=open('','popwin','scrollbars=1,width=400,height=220');
+    popwin.focus();
+    popwin.document.writeln('<'+'html>');
+    popwin.document.writeln('<'+'head>');
+    popwin.document.writeln('<'+'script>');
+    popwin.document.writeln('hc=new Array();$hcinit');
+    popwin.document.writeln('<'+'/script>');
+    popwin.document.writeln('<'+'/head>'+
+        '<'+'body bgcolor="#FFFFFF">'+
+	'<'+'image name="whirly" align="right" src="/adm/lonIcons/'+
+	'lonanim.gif" '+
+	'alt="animated logo" />'+
+	'<'+'h3>Search Results Progress<'+'/h3>'+
+        '<'+'form name="popremain">'+
+        '<'+'tt>'+
+	'<'+'br clear="all"/><i>PLEASE BE PATIENT</i>'+
+	'<'+'br />SCANNING $servernum SERVERS'+
+	'<'+'br clear="all" />Number of record hits found '+
+	'<'+'input type="text" size="10" name="numhits"'+
+	' value="0" />'+
+	'<'+'br clear="all" />Time elapsed '+
+	'<'+'input type="text" size="10" name="elapsetime"'+
+	' value="0" />'+
+	'<'+'br />'+
+	'SERVER GRID (click on any cell for details)'+
+        $grid
+        '<'+'br />'+
+	'Server details '+
+	'<'+'input type="text" size="35" name="sdetails"'+
+	' value="" />'+
+	'<'+'br />'+
+	' <'+'input type="button" name="button"'+
+	' value="close this window" '+
+	' onClick="javascript:opener.abortsearch()" />'+
+	' <'+'input type="button" name="button"'+
+	' value="help" onClick="javascript:opener.openhelp()" />'+
+	'<'+'/tt>'+
+        '<'+'/form>'+
+        '<'+'/body><'+'/html>');
+    popwin.document.close();
+</script>
+ENDPOP
+    return $result;
+}
+
+######################################################################
+######################################################################
+
 =pod 
 
 =item Metadata Viewing Functions
@@ -1827,13 +1851,16 @@
 
 =item &popwin_imgupdate()
 
+Send a given image (and its location) out to the browser.  Takes as 
+input $r, loncapa server id, and an icon URL.
+
 =cut
 
 ######################################################################
 ######################################################################
 sub popwin_imgupdate {
-    my ($r,$imgnum,$icon) = @_;
-    &popwin_js($r,'popwin.document.img'.$imgnum.'.'.
+    my ($r,$server,$icon) = @_;
+    &popwin_js($r,'popwin.document.img_'.$hostdomains{$server}.'_'.$server.'.'.
 	       'src="/adm/lonIcons/'.$icon.'";');
 }    
 

--matthew1024602230--