--- loncom/interface/groupsort.pm	2001/12/11 03:37:11	1.5
+++ loncom/interface/groupsort.pm	2002/08/16 17:03:23	1.14
@@ -2,7 +2,7 @@
 # The LON-CAPA group sort handler
 # Allows for sorting prior to import into RAT.
 #
-# $Id: groupsort.pm,v 1.5 2001/12/11 03:37:11 harris41 Exp $
+# $Id: groupsort.pm,v 1.14 2002/08/16 17:03:23 matthew Exp $
 # 
 # Copyright Michigan State University Board of Trustees
 #
@@ -28,6 +28,8 @@
 #
 # YEAR=2001
 # 8/7,8/8,10/14,10/15,12/10 Scott Harrison
+# YEAR=2002
+# 1/17 Scott Harrison
 #
 ###
 
@@ -44,7 +46,9 @@ my $iconpath; # variable to be accessibl
 # ---------------------------------------------------------------- Main Handler
 sub handler {
     my $r = shift;
-
+ 
+   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+                                           ['acts','catalogmode','mode']);
     # color scheme
     my $fileclr = '#ffffe6';
     my $titleclr = '#ddffff';
@@ -53,26 +57,23 @@ sub handler {
     $r->send_http_header;
     return OK if $r->header_only;
 
-    # output start of web page
-    $r->print(<<END);
-<html>
-<head>
-<title>The LearningOnline Network With CAPA Group Sorter</title>
-<script language='javascript'>
-function insertRowInLastRow() {
-    opener.insertrow(opener.maxrow);
-    opener.addobj(opener.maxrow,'e&2');
-}
-function placeResourceInLastRow (title,url,linkflag) {
-    opener.newresource(opener.maxrow,2,opener.escape(title),
-		       opener.escape(url),'false','normal');
-    opener.save();
-    opener.mostrecent=opener.obj.length-1;
-    if (linkflag) {
-	opener.joinres(opener.linkmode,opener.mostrecent,0);
+# finish_import looks different for graphical or "simple" RAT
+    my $finishimport='';
+    if ($ENV{'form.mode'} eq 'simple') {
+        $finishimport=(<<ENDSMP);
+function finish_import() {
+    opener.document.forms.simpleedit.importdetail.value='';
+    for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
+	opener.document.forms.simpleedit.importdetail.value+='&'+
+              escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
+	      escape(eval("document.forms.groupsort.filelink"+num+".value"));
     }
-    opener.linkmode=opener.mostrecent;
+    opener.document.forms.simpleedit.submit();
+    self.close();
 }
+ENDSMP
+    } else {
+        $finishimport=(<<ENDADV);
 function finish_import() {
     var linkflag=false;
     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
@@ -87,10 +88,34 @@ function finish_import() {
     opener.editmode=0;
     opener.notclear=0;
     opener.linkmode=0;
-    opener.infoclear();
     opener.draw();
     self.close();
 }
+ENDADV
+    }
+
+# output start of web page
+
+    $r->print(<<END);
+<html>
+<head>
+<title>The LearningOnline Network With CAPA Group Sorter</title>
+<script language='javascript'>
+function insertRowInLastRow() {
+    opener.insertrow(opener.maxrow);
+    opener.addobj(opener.maxrow,'e&2');
+}
+function placeResourceInLastRow (title,url,linkflag) {
+    opener.newresource(opener.maxrow,2,opener.escape(title),
+		       opener.escape(url),'false','normal');
+    opener.save();
+    opener.mostrecent=opener.obj.length-1;
+    if (linkflag) {
+	opener.joinres(opener.linkmode,opener.mostrecent,0);
+    }
+    opener.linkmode=opener.mostrecent;
+}
+$finishimport
 function selectchange(val) {
     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
     orderchange(val,newval);
@@ -114,18 +139,9 @@ END
 
     my %shash; # sort order (key is resource location, value is sort order)
     my %thash; # title (key is resource location, value is title)
-    foreach (split(/&/,$ENV{'QUERY_STRING'})) {
-       my ($name, $value) = split(/=/,$_);
-       $value =~ tr/+/ /;
-       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
-       if ($name eq 'acts') {
-           $ENV{'form.'.$name}=$value;
-       }
-       if ($name eq 'catalogmode') {
-           $ENV{'form.'.$name}=$value;
-       }
-    }
+
     my $diropendb;
+# ------------------------------ which file do we open? Easy if explictly given
     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
 	$diropendb = 
 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
@@ -134,7 +150,50 @@ END
 	$diropendb = 
 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
     }
-    if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+    elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
+	$diropendb = 
+	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
+    }
+# --------------------- not explicitly given, choose the one most recently used
+    else { # choose last accessed
+        my @dbfn;
+        my @dbst;
+
+	$dbfn[0] =
+	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
+        $dbst[0]=-1;
+	if (-e $dbfn[0]) {
+	    $dbst[0]=(stat($dbfn[0]))[9];
+	}
+	$dbfn[1] =
+            "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
+        $dbst[1]=-1;
+	if (-e $dbfn[1]) {
+            $dbst[1]=(stat($dbfn[1]))[9];
+        }
+	$dbfn[2] =
+            "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
+        $dbst[2]=-1;
+	if (-e $dbfn[2]) {
+            $dbst[2]=(stat($dbfn[2]))[9];
+        }
+# Expand here for more modes
+# ....
+
+# Okay, find most recent existing
+
+        my $newest=0;
+        $diropendb='';
+        for (my $i=0; $i<=$#dbfn; $i++) {
+	    if ($dbst[$i]>$newest) {
+		$newest=$dbst[$i];
+                $diropendb=$dbfn[$i];
+            }
+        }
+
+    }
+# ----------------------------- diropendb is now the filename of the db to open
+    if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
 	my $acts = $ENV{'form.acts'};
 	my @Acts = split(/b/,$acts);
 	my %ahash;
@@ -224,17 +283,20 @@ END
     untie %hash;
     my $ctr = 0;
     my $clen = scalar(keys %shash);
-    $r->print('<h2><font color="#888888">The LearningOnline With CAPA '.
-	      'Group Sorter</font></h2>'."\n");
-    $r->print('<b><font color="#888888">Finalize order of resources</font>'.
-	      '</b>'."\n");
-    $r->print("<form method='post' action='/adm/groupsort' name='groupsort' ".
-	      "enctype='application/x-www-form-urlencoded'>");
-    $r->print(<<END);
+   $r->print(<<END);
+<h2><font color="#888888">
+    The LearningOnline Network With CAPA Group Sorter
+    </font></h2>
+<b><font color="#888888">Finalize order of resources</font></b>
+<form method='post' action='/adm/groupsort' name='groupsort'
+      enctype='application/x-www-form-urlencoded'>
 <input type="hidden" name="fnum" value="$clen" />
 <input type="hidden" name="oldval" value="" />
 <input type="hidden" name="newval" value="" />
+<input type="hidden" name="mode" value="$ENV{'form.mode'}" />
 END
+
+# --- Expand here if "GO BACK" button desired
     if ($ENV{'form.catalogmode'} eq 'groupimport') {
 	$r->print(<<END);
 <input type="button" name="alter" value="GO BACK"
@@ -247,6 +309,8 @@ END
  onClick="window.location='/adm/searchcat?catalogmode=groupsearch'" />&nbsp;
 END
 }
+# ---
+
     $r->print(<<END);
 <input type="button" name="alter" value="FINISH IMPORT"
  onClick="finish_import()" />&nbsp;