--- loncom/interface/groupsort.pm	2008/09/15 13:22:48	1.62
+++ loncom/interface/groupsort.pm	2013/07/15 14:32:44	1.73
@@ -2,7 +2,7 @@
 # The LON-CAPA group sort handler
 # Allows for sorting prior to import into RAT.
 #
-# $Id: groupsort.pm,v 1.62 2008/09/15 13:22:48 bisitz Exp $
+# $Id: groupsort.pm,v 1.73 2013/07/15 14:32:44 bisitz Exp $
 # 
 # Copyright Michigan State University Board of Trustees
 #
@@ -37,7 +37,7 @@ use GDBM_File;
 use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::lonnet;
-use LONCAPA;
+use LONCAPA qw(:DEFAULT :match);
 
 my $iconpath; # variable to be accessible to multiple subroutines
 my %hash; # variable to tie to user specific database
@@ -86,8 +86,8 @@ sub update_actions_hash {
 sub readfromdb {
     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)) {
@@ -152,12 +152,23 @@ sub readfromfile {
 		  &Apache::lonnet::filelocation('',$env{'form.readfile'}));
     } else {
         my $parser = HTML::TokeParser->new(\$cont);
-        my $token;
+        my ($token,$donechk,$allmaps);
+        $allmaps = {};
         while ($token = $parser->get_token) {
 	    if ($token->[0] eq 'S') {
                 if ($token->[1] eq 'resource') {
 		    if ($env{'form.recover'}) {
 			if ($token->[2]->{'type'} ne 'zombie') { next; }
+                        if ($token->[2]->{'src'} =~ /\.(page|sequence)$/) {
+                            if (($env{'request.course.id'}) &&
+                                ($env{'form.readfile'} =~ m{/default(|_\d+)\.(page|sequence)$})) {
+                                unless ($donechk) {
+                                    $allmaps = &Apache::loncommon::allmaps_incourse();
+                                    $donechk = 1;
+                                }
+                            }
+                            if ($allmaps->{$token->[2]->{'src'}}) { next; }
+                        }
 		    } else {
 			if ($token->[2]->{'type'} eq 'zombie') { next; }
 		    }
@@ -181,31 +192,12 @@ sub readfromfile {
     }
 }
 
-# --------------------------------------------------------- Read from bookmarks
-
-sub readfrombookmarks {
-    my ($r,$resources)=@_;
-    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);
-    for (my $index=0;$index<($#bookmarks+1)/2;$index++) {
-        if ($bookmarks[$index*2+1]) {
-	    my $url  = $bookmarks[$index*2+1];
-	    my $name = $bookmarks[$index*2];
-	    $name =~ s/^LON\-CAPA\s+//;
-
-	    push(@{$resources},{'url' => $url, 'title' => $name});
-	}
-    }
-}
-
 # ---------------------------------------------------------------- Main Handler
 sub handler {
     my $r = shift;
  
    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-			     ['acts','mode','readfile','recover','bookmarks']);
+			     ['acts','mode','readfile','recover']);
 
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
@@ -215,7 +207,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='}';
     }
@@ -299,30 +291,35 @@ END
 
     if ($env{'form.readfile'}) {
 	&readfromfile($r,\@resources);
-    } elsif ($env{'form.bookmarks'}) {
-	&readfrombookmarks($r,\@resources);
     } else {
 	&readfromdb($r,\@resources);
     }
 
     my $ctr = 0;
     my $clen = scalar(@resources);
-    if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+    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'
 		);
-	my $title = ($env{'form.recover'}) ? 'Recover Removed Resources'
-                                           : 'Sort Imported Resources';
+
 	$r->print(&Apache::loncommon::start_page($title, $js));
+	$r->print('<h1>'.&mt($title).'</h1>');
 
 	$r->print(<<END);
 <form method='post' action='/adm/groupsort' name='groupsort'
@@ -332,18 +329,18 @@ 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="$lt{'re'}"
- onClick="finish_import()" />&nbsp;
-<input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
+<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
@@ -353,30 +350,42 @@ END
 <h2>$lt{'fin'}</h2>
 <div>
 <input type="button" name="alter" value="$lt{'ci'}"
- onClick="window.location='$resurl?inhibitmenu=yes&amp;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?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
+ 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()" />
+ onclick="finish_import()" />&nbsp;
+<input type="button" name="alter" value="$lt{'ca'}" onclick="self.close()" />
 </div>
 <br />
 END
         }
-        $r->print(&Apache::loncommon::start_data_table()
-                 .&Apache::loncommon::start_data_table_header_row());
-	if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) { 
-	    $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");
+
+        # 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'>
@@ -391,10 +400,10 @@ END
     foreach my $resource (@resources) {
 	$ctr++;
 	my $iconname=&Apache::loncommon::icon($resource->{'url'});
-	if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+	if (($clen > 1) || ($env{'form.readfile'})) {
 	    $r->print(&Apache::loncommon::start_data_table_row()
                      ."<td>");
-            if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+            if (($env{'form.readfile'})) {
 		$r->print(&checkbox($ctr-1));
 	    } else {
 		$r->print(&movers($clen,$ctr));
@@ -402,9 +411,9 @@ END
 	}
 	$r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
 			  $resource->{'id'}));
-	if (($clen > 1)  || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+	if (($clen > 1)  || ($env{'form.readfile'})) {
 	    $r->print("</td>");
-            unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
+            unless (($env{'form.readfile'})) {
 		$r->print("<td>".
 			  &select_box($clen,$ctr).
 			  "</td>");
@@ -412,15 +421,24 @@ END
 	    $r->print("<td>");
 	    $r->print("<img src='$iconname' />");
 	    $r->print("</td><td>");
-	    $r->print($resource->{'title'}.$resource->{'notes'}."</td><td>\n");
+            if (($env{'form.recover'}) &&
+                ($resource->{'url'} =~ m{/uploaded/$match_domain/$match_courseid/supplemental/})) {
+                my $title = &Apache::loncommon::parse_supplemental_title($resource->{'title'});
+                $r->print($title);
+            } else {
+                $r->print($resource->{'title'});
+            }
+            $r->print($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(&Apache::loncommon::end_data_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">
@@ -470,7 +488,7 @@ sub select_box {
     my ($total,$sel) = @_;
     my $string;
     $string = '<select name="alt'.$sel.'"';
-    $string .= " onChange='selectchange($sel)'>";
+    $string .= " onchange='selectchange($sel)'>";
     $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
     for my $cur (1..$total) {
 	$string .= "<option name='o$cur' value='$cur'";
@@ -495,3 +513,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
+