--- loncom/interface/groupsort.pm	2006/12/05 02:55:51	1.50
+++ loncom/interface/groupsort.pm	2011/07/04 09:24:58	1.68
@@ -2,7 +2,7 @@
 # The LON-CAPA group sort handler
 # Allows for sorting prior to import into RAT.
 #
-# $Id: groupsort.pm,v 1.50 2006/12/05 02:55:51 albertel Exp $
+# $Id: groupsort.pm,v 1.68 2011/07/04 09:24:58 foxr Exp $
 # 
 # Copyright Michigan State University Board of Trustees
 #
@@ -37,106 +37,96 @@ use GDBM_File;
 use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::lonnet;
-use LONCAPA();
+use LONCAPA;
 
 my $iconpath; # variable to be accessible to multiple subroutines
 my %hash; # variable to tie to user specific database
 
 
+sub update_actions_hash {
+    my ($hash) = @_;
+    # be careful in here, there is also a global %hash
+    my $acts=$env{'form.acts'};
+    my @Acts=split(/b/,$acts);
+    my %ahash;
+    my %achash;
+    # some initial hashes for working with data
+    my $ac=0;
+    foreach (@Acts) {
+ 	my ($state,$ref)=split(/a/);
+	$ahash{$ref}=$state;
+	$achash{$ref}=$ac;
+	$ac++;
+    }
+    # sorting through the actions and changing the global database hash
+    foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
+	if ($ahash{$key} eq '1') {
+	    $hash->{'store_'.$hash->{'pre_'.$key.'_link'}}=
+		$hash->{'pre_'.$key.'_title'};
+	    $hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}}=
+		$hash->{'storectr'}+0;
+	    $hash->{'storectr'}++;
+	}
+	if ($ahash{$key} eq '0') {
+	    if ($hash->{'store_'.$hash->{'pre_'.$key.'_link'}}) {
+		delete($hash->{'store_'.$hash->{'pre_'.$key.'_link'}});
+		delete($hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}});
+	    }
+	}
+    }
+    # deleting the previously cached listing
+    foreach my $key (keys(%{ $hash })) {
+	next if ($key !~ /^pre_(\d+)_link/);
+	my $which = $1;
+	delete($hash->{'pre_'.$which.'_title'});
+	delete($hash->{'pre_'.$which.'_link'});
+    }
+}
+
 sub readfromdb {
-    my ($r,$shash,$thash)=@_;
+    my ($r,$resources)=@_;
 
-    my $diropendb = 
-       "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
+    my $diropendb = LONCAPA::tempdir() .
+       "$env{'user.domain'}_$env{'user.name'}_sel_res.db";
 
 # ----------------------------- 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;
-	my %achash;
-	my $ac = 0;
-	foreach (@Acts) {
-	    my ($state,$ref) = split(/a/);
-	    $ahash{$ref} = $state;
-	    $achash{$ref} = $ac;
-	    $ac++;
-	}
-	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
-	    my $key = $_;
-	    if ($ahash{$key} eq '1') {
-		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
-		    $hash{'pre_'.$key.'_title'};
-		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
-		    $hash{'storectr'}+0;
-		$hash{'storectr'}++;
-	    }
-	    if ($ahash{$key} eq '0') {
-		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
-		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
-		}
-	    }
+	&update_actions_hash(\%hash);
+
+	my %temp_resources;
+	foreach my $key (keys(%hash)) {
+	    next if ($key !~ /^store_/);
+	    my ($url) = ($key =~ /^store_(.*)/);
+	    $temp_resources{$hash{'storectr_'.$url}}{'url'}=$url;
+	    $temp_resources{$hash{'storectr_'.$url}}{'title'}=
+		&Apache::lonnet::gettitle($url);
 	}
-	foreach (keys %hash) {
-	    if ($_ =~ /^store_/) {
-		my $key = $_;
-		$key =~ s/^store_//;
-		$$shash{$key} = $hash{'storectr_'.$key};
-		if (&Apache::lonnet::gettitle($key) eq '') {
-		    $$thash{$key} = $hash{'store_'.$key};
-		} else {
-		    $$thash{$key} = &Apache::lonnet::gettitle($key);
-		}
-	    }
+
+	# use the temp, since there might be gaps in the counting
+	foreach my $item (sort {$a <=> $b} (keys(%temp_resources))) {
+	    push(@{ $resources },$temp_resources{$item});
 	}
+
 	if ($env{'form.oldval'}) {
-	    my $newctr = 0;
-	    my %chash;
-	    foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
-		my $key = $_;
-		$newctr++;
-		$$shash{$key} = $newctr;
-		$hash{'storectr_'.$key} = $newctr;
-		$chash{$newctr} = $key;
-	    }
-	    my $oldval = $env{'form.oldval'};
-	    my $newval = $env{'form.newval'};
-	    if ($oldval != $newval) {
-		# when newval==0, then push down and delete
-		if ($newval!=0) {
-		    $$shash{$chash{$oldval}} = $newval;
-		    $hash{'storectr_'.$chash{$oldval}} = $newval;
-		} else {
-		    $$shash{$chash{$oldval}} = $newctr;
-		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
-		}
-		if ($newval==0) { # push down
-		    my $newval2=$newctr;
-		    for my $idx ($oldval..($newval2-1)) {
-			$$shash{$chash{$idx+1}} = $idx;
-			$hash{'storectr_'.$chash{$idx+1}} = $idx;
-		    }
-		    delete $$shash{$chash{$oldval}};
-		    delete $hash{'storectr_'.$chash{$oldval}};
-		    delete $hash{'store_'.$chash{$oldval}};
-		} elsif ($oldval < $newval) { # push down
-		    for my $idx ($oldval..($newval-1)) {
-			$$shash{$chash{$idx+1}} = $idx;
-			$hash{'storectr_'.$chash{$idx+1}} = $idx;
-		    }
-		} elsif ($oldval > $newval) { # push up
-		    for my $idx (reverse($newval..($oldval-1))) {
-			$$shash{$chash{$idx}} = $idx+1;
-			$hash{'storectr_'.$chash{$idx}} = $idx+1;
-		    }
-		}
+	    my $res = splice(@{$resources},$env{'form.oldval'}-1,1);
+	    if ($env{'form.newval'} == 0) {
+		# picked 'discard'
+		my $url =  $res->{'url'};
+		delete($hash{'storectr_'.$url});
+		delete($hash{'store_'.$url});
+	    } else {
+		splice(@{$resources},$env{'form.newval'}-1,0,$res);
 	    }
 	}
+	# store out new order
+	foreach my $which (0..$#$resources) {
+	    my $url =  $resources->[$which]{'url'};
+	    $hash{'storectr_'.$url} = $which;
+	}
     } else {
 	$r->print('Unable to tie hash to db file');
     }
-    untie %hash;
-    return ($shash,$thash);
+    untie(%hash);
 }
 
 
@@ -154,7 +144,7 @@ sub cleanup {
 # -------------------------------------------------------------- Read from file
 
 sub readfromfile {
-    my ($r,$shash,$thash,$nhash)=@_;
+    my ($r,$resources)=@_;
     my $cont=&Apache::lonnet::getfile
 	(&Apache::lonnet::filelocation('',$env{'form.readfile'}));
     if ($cont==-1) {
@@ -163,7 +153,6 @@ sub readfromfile {
     } else {
         my $parser = HTML::TokeParser->new(\$cont);
         my $token;
-	my $n=1;
         while ($token = $parser->get_token) {
 	    if ($token->[0] eq 'S') {
                 if ($token->[1] eq 'resource') {
@@ -173,54 +162,32 @@ sub readfromfile {
 			if ($token->[2]->{'type'} eq 'zombie') { next; }
 		    }
 
-		    my $url=$token->[2]->{'src'};
                     my $name=$token->[2]->{'title'};
 		    $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
+		    my $note;
 		    if ($1) {
-			$$nhash{$url}='<br />'.&mt('Removed by ').
+			$note = '<br />'.&mt('Removed by ').
 			    &Apache::loncommon::plainname($2,$3).', '.
 			    &Apache::lonlocal::locallocaltime($1);
 		    }
 		    $name=~s/\&colon\;/\:/g;
-		    $$thash{$url}=$name;
-                    $$shash{$url}=$n;
-                    $n++;
+		    push(@{$resources}, {'url'   => $token->[2]->{'src'},
+					 'title' => $name,
+					 'note'  => $note,
+				         'id'    => $token->[2]->{'id'},});
 		}
 	    }
 	}
     }
-    return ($shash,$thash);
 }
 
-# --------------------------------------------------------- Read from bookmarks
-
-sub readfrombookmarks {
-    my ($r,$shash,$thash)=@_;
-    my %bookmarks=&Apache::lonnet::dump('bookmarks');
-# the bookmark "hash" is just one entry
-# it's a javascript program code with arguments like ('title','url');
-    my @bookmarks=($bookmarks{'bookmarks'}=~/\((?:\'([^\']+)\'\,\'([^\']+)\'|\"([^\"]+)\"\,\"([^\"]+)\")\)\;/g);
-    my $order=1;
-    for (my $index=0;$index<($#bookmarks+1)/2;$index++) {
-        if ($bookmarks[$index*2+1]) {
-	    $$thash{$bookmarks[$index*2+1]}=$bookmarks[$index*2];
-	    $$thash{$bookmarks[$index*2+1]}=~s/^LON\-CAPA\s+//;
-	    $$shash{$bookmarks[$index*2+1]}=$order;
-	    $order++;
-	}
-    }
-    return ($shash,$thash);
-}
 
 # ---------------------------------------------------------------- Main Handler
 sub handler {
     my $r = shift;
  
    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-			     ['acts','mode','readfile','recover','bookmarks']);
-    # color scheme
-    my $fileclr = '#ffffe6';
-    my $titleclr = '#ddffff';
+			     ['acts','mode','readfile','recover']);
 
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
@@ -230,7 +197,7 @@ sub handler {
     my $finishimport='';
     my $begincondition='';
     my $endcondition='';
-    if (($env{'form.readfile'}) || ($env{'form.bookmarks'}))  {
+    if (($env{'form.readfile'}))  {
         $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
 	$endcondition='}';
     }
@@ -241,8 +208,9 @@ function finish_import() {
     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
 	$begincondition
 	opener.document.forms.simpleedit.importdetail.value+='&'+
-              escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
-	      escape(eval("document.forms.groupsort.filelink"+num+".value"));
+              eval("document.forms.groupsort.title"+num+".value")+'='+
+	      eval("document.forms.groupsort.filelink"+num+".value")+'='+
+	      eval("document.forms.groupsort.id"+num+".value");
 	$endcondition
     }
     opener.document.forms.simpleedit.submit();
@@ -259,6 +227,7 @@ function finish_import() {
 	placeResourceInLastRow(
 	       eval("document.forms.groupsort.title"+num+".value"),
  	       eval("document.forms.groupsort.filelink"+num+".value"),
+ 	       eval("document.forms.groupsort.id"+num+".value"),
 	       linkflag
 	);
         linkflag=true;
@@ -280,9 +249,9 @@ function insertRowInLastRow() {
     opener.insertrow(opener.maxrow);
     opener.addobj(opener.maxrow,'e&2');
 }
-function placeResourceInLastRow (title,url,linkflag) {
+function placeResourceInLastRow (title,url,id,linkflag) {
     opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
-		       opener.escape(url),'false','normal');
+		       opener.escape(url),'false','normal',id);
     opener.save();
     if (linkflag) {
 	opener.joinres(opener.linkmode,opener.mostrecent,0);
@@ -308,36 +277,41 @@ END
     my $domain  = $r->dir_config('lonDefDomain');
     $iconpath = $r->dir_config('lonIconsURL') . "/";
 
-    my %shash; # sort order (key is resource location, value is sort order)
-    my %thash; # title (key is resource location, value is title)
-    my %nhash; # notes (key is resource location);
+    my @resources;
 
     if ($env{'form.readfile'}) {
-	&readfromfile($r,\%shash,\%thash,\%nhash);
-    } elsif ($env{'form.bookmarks'}) {
-	&readfrombookmarks($r,\%shash,\%thash);
+	&readfromfile($r,\@resources);
     } else {
-	&readfromdb($r,\%shash,\%thash);
+	&readfromdb($r,\@resources);
     }
 
     my $ctr = 0;
-    my $clen = scalar(keys %shash);
-    if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+    my $clen = scalar(@resources);
+    my $title = '';
+    if ($env{'form.recover'}) {
+        $title = 'Recover Removed Resources';
+    } else {
+        $title = 'Sort Imported Resources';
+    }
+    if (($clen > 1) || ($env{'form.readfile'})) {
 	my %lt=&Apache::lonlocal::texthash(
 		'fin'=> 'Finalize order of resources',
 		'ci' => 'Continue Import',
 		'cs' => 'Continue Search',
 		'fi' => 'Finish Import',
+		're' => 'Recover Checked',
+		'ip' => 'Import Checked',
 		'ca' => 'Cancel',
 		'co' => 'Change Order',
 		'ti' => 'Title',
 		'pa' => 'Path',
                 'in' => 'Include'
 		);
-	$r->print(&Apache::loncommon::start_page('Sort Imported Resources',
-						 $js));
+
+	$r->print(&Apache::loncommon::start_page($title, $js));
+	$r->print('<h1>'.&mt($title).'</h1>');
+
 	$r->print(<<END);
-<b><font color="#888888">$lt{'fin'}</font></b>
 <form method='post' action='/adm/groupsort' name='groupsort'
       enctype='application/x-www-form-urlencoded'>
 <input type="hidden" name="fnum" value="$clen" />
@@ -345,38 +319,63 @@ END
 <input type="hidden" name="newval" value="" />
 <input type="hidden" name="mode" value="$env{'form.mode'}" />
 <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
-<input type="hidden" name="bookmarks" value="$env{'form.bookmarks'}" />
 <input type="hidden" name="recover" value="$env{'form.recover'}" />
 END
 
+        $r->print(&Apache::loncommon::inhibit_menu_check('input'));
+        # ---
+
+        my $buttontext = $lt{'re'};
+        if ($env{'form.recover'}) {
+	    $r->print(<<END);
+<input type="button" name="alter" value="$buttontext"
+ onClick="finish_import()" />&nbsp;
+<input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
+END
+	} else {
         # --- Continue Buttons
-        my $resurl = 
-	   &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
-        $r->print(<<END);
+	    my $resurl = 
+		&Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
+	    $r->print(<<END);
+<h2>$lt{'fin'}</h2>
+<div>
 <input type="button" name="alter" value="$lt{'ci'}"
- onClick="window.location='$resurl?catalogmode=import'" />&nbsp;
+ onClick="window.location='$resurl?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
 <input type="button" name="altersearch" value="$lt{'cs'}"
- onClick="window.location='/adm/searchcat?catalogmode=import'" />&nbsp;
-END
-        # ---
-
-	$r->print(<<END);
+ onClick="window.location='/adm/searchcat?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
 <input type="button" name="alter" value="$lt{'fi'}"
  onClick="finish_import()" />&nbsp;
 <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
+</div>
+<br />
 END
-        $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
-	$r->print("<table border=0><tr>\n");
-	if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) { 
-	    $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
-	} else { 
-	    $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n"); 
-	}
-	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
-	$r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
+        }
+
+        # Only display header if content exists
+        if ($clen > 0) {
+            $r->print(&Apache::loncommon::start_data_table()
+                     .&Apache::loncommon::start_data_table_header_row());
+            if (($env{'form.readfile'})) { 
+                $r->print("<th>$lt{'in'}</th>\n");
+            } else { 
+                $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n"); 
+            }
+            $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
+            $r->print("<th>$lt{'pa'}</th>");
+            $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
+        } else {
+            my $errtxt = '';
+            if ($env{'form.recover'}) {
+                $errtxt = 'There are no resources to recover.';
+            } else {
+                $errtxt = 'There are no resources to import.';
+            }
+            $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
+        }
     } else {
 	$r->print(&Apache::loncommon::start_page(undef,$js,
 						 {'only_body' => 1}));
+#       $r->print('<h1>'.&mt($title).'</h1>');
 	$r->print(<<END);
 <form method='post' action='/adm/groupsort' name='groupsort'
       enctype='application/x-www-form-urlencoded'>
@@ -385,36 +384,44 @@ END
 <input type="hidden" name="newval" value="" />
 <input type="hidden" name="mode" value="$env{'form.mode'}" />
 END
+        $r->print(&Apache::loncommon::inhibit_menu_check('input'));
+
     }
-    foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
-	my $key=$_;
+    foreach my $resource (@resources) {
 	$ctr++;
-	my $iconname=&Apache::loncommon::icon($key);
-	if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
-	    $r->print("<tr><td bgcolor='$fileclr'>");
-            if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+	my $iconname=&Apache::loncommon::icon($resource->{'url'});
+	if (($clen > 1) || ($env{'form.readfile'})) {
+	    $r->print(&Apache::loncommon::start_data_table_row()
+                     ."<td>");
+            if (($env{'form.readfile'})) {
 		$r->print(&checkbox($ctr-1));
 	    } else {
 		$r->print(&movers($clen,$ctr));
 	    }
 	}
-	$r->print(&hidden($ctr-1,$thash{$key},$key));
-	if (($clen > 1)  || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+	$r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
+			  $resource->{'id'}));
+	if (($clen > 1)  || ($env{'form.readfile'})) {
 	    $r->print("</td>");
-            unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
-		$r->print("<td bgcolor='$fileclr'>".
+            unless (($env{'form.readfile'})) {
+		$r->print("<td>".
 			  &select_box($clen,$ctr).
 			  "</td>");
 	    }
-	    $r->print("<td bgcolor='$fileclr'>");
+	    $r->print("<td>");
 	    $r->print("<img src='$iconname' />");
-	    $r->print("</td><td bgcolor='$fileclr'>");
-	    $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
-	    $r->print("$key</td></tr>\n");
+	    $r->print("</td><td>");
+	    $r->print($resource->{'title'}.$resource->{'notes'}."</td><td>\n");
+	    $r->print($resource->{'url'}."</td>"
+                     .&Apache::loncommon::end_data_table_row()
+                     ."\n");
 	} 
     }
-    if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
-	$r->print("</table></td></tr></table></form>");
+    if (($clen > 1) || ($env{'form.readfile'})) {
+        if ($clen > 0) {
+            $r->print(&Apache::loncommon::end_data_table());
+        }
+        $r->print('</form>');
     } else {
 	$r->print(<<END);
 <script type="text/javascript">
@@ -430,12 +437,13 @@ END
 
 # --------------------------------------- Hidden values (returns scalar string)
 sub hidden {
-    my ($sel,$title,$filelink) = @_;
-    my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
-	'" />';
+    my ($sel,$title,$filelink,$id) = @_;
+    my $string = '<input type="hidden" name="title'.$sel.'" value="'.
+	&escape($title).'" />';
     $filelink=~s|^/ext/|http://|;
     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
-	$filelink.'" />';
+	&escape($filelink).'" />';
+    $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
     return $string;
 }
 
@@ -488,3 +496,30 @@ sub checkbox {
 1;
 
 __END__
+
+=pod
+
+=head1 NAME
+
+Apache::groupsort.pm
+
+=head1 SYNOPSIS
+
+Implements a second phase of importing
+multiple resources into the RAT. Allows for
+reordering the sequence of resources
+
+This is part of the LearningOnline Network with CAPA project
+described at http://www.lon-capa.org.
+
+
+=head1 NOTABLE SUBROUTINES
+
+=over
+
+=item 
+
+=back
+
+=cut
+