Diff for /loncom/interface/groupsort.pm between versions 1.55 and 1.60

version 1.55, 2007/07/11 23:51:19 version 1.60, 2007/07/25 23:10:23
Line 45  my %hash; # variable to tie to user spec Line 45  my %hash; # variable to tie to user spec
   
 sub update_actions_hash {  sub update_actions_hash {
     my ($hash) = @_;      my ($hash) = @_;
       # be careful in here, there is also a global %hash
     my $acts=$env{'form.acts'};      my $acts=$env{'form.acts'};
     my @Acts=split(/b/,$acts);      my @Acts=split(/b/,$acts);
     my %ahash;      my %ahash;
     my %achash;      my %achash;
     my $ac=0;  
     # some initial hashes for working with data      # some initial hashes for working with data
       my $ac=0;
     foreach (@Acts) {      foreach (@Acts) {
  my ($state,$ref)=split(/a/);    my ($state,$ref)=split(/a/);
  $ahash{$ref}=$state;   $ahash{$ref}=$state;
  $achash{$ref}=$ac;   $achash{$ref}=$ac;
  $ac++;   $ac++;
Line 60  sub update_actions_hash { Line 61  sub update_actions_hash {
     # sorting through the actions and changing the global database hash      # sorting through the actions and changing the global database hash
     foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {      foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
  if ($ahash{$key} eq '1') {   if ($ahash{$key} eq '1') {
     $hash->{'store_'.$hash{'pre_'.$key.'_link'}}=      $hash->{'store_'.$hash->{'pre_'.$key.'_link'}}=
  $hash->{'pre_'.$key.'_title'};   $hash->{'pre_'.$key.'_title'};
     $hash->{'storectr_'.$hash{'pre_'.$key.'_link'}}=      $hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}}=
  $hash->{'storectr'}+0;   $hash->{'storectr'}+0;
     $hash->{'storectr'}++;      $hash->{'storectr'}++;
  }   }
  if ($ahash{$key} eq '0') {   if ($ahash{$key} eq '0') {
     if ($hash->{'store_'.$hash{'pre_'.$key.'_link'}}) {      if ($hash->{'store_'.$hash->{'pre_'.$key.'_link'}}) {
  delete($hash->{'store_'.$hash{'pre_'.$key.'_link'}});   delete($hash->{'store_'.$hash->{'pre_'.$key.'_link'}});
    delete($hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}});
     }      }
  }   }
     }      }
Line 82  sub update_actions_hash { Line 84  sub update_actions_hash {
 }  }
   
 sub readfromdb {  sub readfromdb {
     my ($r,$shash,$thash)=@_;      my ($r,$resources)=@_;
   
     my $diropendb =       my $diropendb = 
        "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";         "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
Line 91  sub readfromdb { Line 93  sub readfromdb {
     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {      if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  &update_actions_hash(\%hash);   &update_actions_hash(\%hash);
   
  foreach (keys %hash) {   my %temp_resources;
     if ($_ =~ /^store_/) {   foreach my $key (keys(%hash)) {
  my $key = $_;      next if ($key !~ /^store_/);
  $key =~ s/^store_//;      my ($url) = ($key =~ /^store_(.*)/);
  $$shash{$key} = $hash{'storectr_'.$key};      $temp_resources{$hash{'storectr_'.$url}}{'url'}=$url;
  if (&Apache::lonnet::gettitle($key) eq '') {      $temp_resources{$hash{'storectr_'.$url}}{'title'}=
     $$thash{$key} = $hash{'store_'.$key};   &Apache::lonnet::gettitle($url);
  } 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'}) {   if ($env{'form.oldval'}) {
     my $newctr = 0;      my $res = splice(@{$resources},$env{'form.oldval'}-1,1);
     my %chash;      if ($env{'form.newval'} == 0) {
     foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {   # picked 'discard'
  my $key = $_;   my $url =  $res->{'url'};
  $newctr++;   delete($hash{'storectr_'.$url});
  $$shash{$key} = $newctr;   delete($hash{'store_'.$url});
  $hash{'storectr_'.$key} = $newctr;      } else {
  $chash{$newctr} = $key;   splice(@{$resources},$env{'form.newval'}-1,0,$res);
     }  
     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;  
     }  
  }  
     }      }
  }   }
    # store out new order
    foreach my $which (0..$#$resources) {
       my $url =  $resources->[$which]{'url'};
       $hash{'storectr_'.$url} = $which;
    }
     } else {      } else {
  $r->print('Unable to tie hash to db file');   $r->print('Unable to tie hash to db file');
     }      }
     untie %hash;      untie(%hash);
     return ($shash,$thash);  
 }  }
   
   
Line 168  sub cleanup { Line 144  sub cleanup {
 # -------------------------------------------------------------- Read from file  # -------------------------------------------------------------- Read from file
   
 sub readfromfile {  sub readfromfile {
     my ($r,$shash,$thash,$nhash)=@_;      my ($r,$resources)=@_;
     my $cont=&Apache::lonnet::getfile      my $cont=&Apache::lonnet::getfile
  (&Apache::lonnet::filelocation('',$env{'form.readfile'}));   (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
     if ($cont==-1) {      if ($cont==-1) {
Line 177  sub readfromfile { Line 153  sub readfromfile {
     } else {      } else {
         my $parser = HTML::TokeParser->new(\$cont);          my $parser = HTML::TokeParser->new(\$cont);
         my $token;          my $token;
  my $n=1;  
         while ($token = $parser->get_token) {          while ($token = $parser->get_token) {
     if ($token->[0] eq 'S') {      if ($token->[0] eq 'S') {
                 if ($token->[1] eq 'resource') {                  if ($token->[1] eq 'resource') {
Line 187  sub readfromfile { Line 162  sub readfromfile {
  if ($token->[2]->{'type'} eq 'zombie') { next; }   if ($token->[2]->{'type'} eq 'zombie') { next; }
     }      }
   
     my $url=$token->[2]->{'src'};  
                     my $name=$token->[2]->{'title'};                      my $name=$token->[2]->{'title'};
     $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;      $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
       my $note;
     if ($1) {      if ($1) {
  $$nhash{$url}='<br />'.&mt('Removed by ').   $note = '<br />'.&mt('Removed by ').
     &Apache::loncommon::plainname($2,$3).', '.      &Apache::loncommon::plainname($2,$3).', '.
     &Apache::lonlocal::locallocaltime($1);      &Apache::lonlocal::locallocaltime($1);
     }      }
     $name=~s/\&colon\;/\:/g;      $name=~s/\&colon\;/\:/g;
     $$thash{$url}=$name;      push(@{$resources}, {'url'   => $token->[2]->{'src'},
                     $$shash{$url}=$n;   'title' => $name,
                     $n++;   'note'  => $note,
            'id'    => $token->[2]->{'id'},});
  }   }
     }      }
  }   }
     }      }
     return ($shash,$thash);  
 }  }
   
 # --------------------------------------------------------- Read from bookmarks  # --------------------------------------------------------- Read from bookmarks
   
 sub readfrombookmarks {  sub readfrombookmarks {
     my ($r,$shash,$thash)=@_;      my ($r,$resources)=@_;
     my %bookmarks=&Apache::lonnet::dump('bookmarks');      my %bookmarks=&Apache::lonnet::dump('bookmarks');
 # the bookmark "hash" is just one entry  # the bookmark "hash" is just one entry
 # it's a javascript program code with arguments like ('title','url');  # it's a javascript program code with arguments like ('title','url');
     my @bookmarks=($bookmarks{'bookmarks'}=~/\((?:\'([^\']+)\'\,\'([^\']+)\'|\"([^\"]+)\"\,\"([^\"]+)\")\)\;/g);      my @bookmarks=($bookmarks{'bookmarks'}=~/\((?:\'([^\']+)\'\,\'([^\']+)\'|\"([^\"]+)\"\,\"([^\"]+)\")\)\;/g);
     my $order=1;  
     for (my $index=0;$index<($#bookmarks+1)/2;$index++) {      for (my $index=0;$index<($#bookmarks+1)/2;$index++) {
         if ($bookmarks[$index*2+1]) {          if ($bookmarks[$index*2+1]) {
     $$thash{$bookmarks[$index*2+1]}=$bookmarks[$index*2];      my $url  = $bookmarks[$index*2+1];
     $$thash{$bookmarks[$index*2+1]}=~s/^LON\-CAPA\s+//;      my $name = $bookmarks[$index*2];
     $$shash{$bookmarks[$index*2+1]}=$order;      $name =~ s/^LON\-CAPA\s+//;
     $order++;  
       push(@{$resources},{'url' => $url, 'title' => $name});
  }   }
     }      }
     return ($shash,$thash);  
 }  }
   
 # ---------------------------------------------------------------- Main Handler  # ---------------------------------------------------------------- Main Handler
Line 256  function finish_import() { Line 230  function finish_import() {
  $begincondition   $begincondition
  opener.document.forms.simpleedit.importdetail.value+='&'+   opener.document.forms.simpleedit.importdetail.value+='&'+
               escape(eval("document.forms.groupsort.title"+num+".value"))+'='+                escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
       escape(eval("document.forms.groupsort.filelink"+num+".value"));        escape(eval("document.forms.groupsort.filelink"+num+".value"))+'='+
         escape(eval("document.forms.groupsort.id"+num+".value"));
  $endcondition   $endcondition
     }      }
     opener.document.forms.simpleedit.submit();      opener.document.forms.simpleedit.submit();
Line 273  function finish_import() { Line 248  function finish_import() {
  placeResourceInLastRow(   placeResourceInLastRow(
        eval("document.forms.groupsort.title"+num+".value"),         eval("document.forms.groupsort.title"+num+".value"),
         eval("document.forms.groupsort.filelink"+num+".value"),          eval("document.forms.groupsort.filelink"+num+".value"),
           eval("document.forms.groupsort.id"+num+".value"),
        linkflag         linkflag
  );   );
         linkflag=true;          linkflag=true;
Line 294  function insertRowInLastRow() { Line 270  function insertRowInLastRow() {
     opener.insertrow(opener.maxrow);      opener.insertrow(opener.maxrow);
     opener.addobj(opener.maxrow,'e&2');      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.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
        opener.escape(url),'false','normal');         opener.escape(url),'false','normal',id);
     opener.save();      opener.save();
     if (linkflag) {      if (linkflag) {
  opener.joinres(opener.linkmode,opener.mostrecent,0);   opener.joinres(opener.linkmode,opener.mostrecent,0);
Line 322  END Line 298  END
     my $domain  = $r->dir_config('lonDefDomain');      my $domain  = $r->dir_config('lonDefDomain');
     $iconpath = $r->dir_config('lonIconsURL') . "/";      $iconpath = $r->dir_config('lonIconsURL') . "/";
   
     my %shash; # sort order (key is resource location, value is sort order)      my @resources;
     my %thash; # title (key is resource location, value is title)  
     my %nhash; # notes (key is resource location);  
   
     if ($env{'form.readfile'}) {      if ($env{'form.readfile'}) {
  &readfromfile($r,\%shash,\%thash,\%nhash);   &readfromfile($r,\@resources);
     } elsif ($env{'form.bookmarks'}) {      } elsif ($env{'form.bookmarks'}) {
  &readfrombookmarks($r,\%shash,\%thash);   &readfrombookmarks($r,\@resources);
     } else {      } else {
  &readfromdb($r,\%shash,\%thash);   &readfromdb($r,\@resources);
     }      }
   
     my $ctr = 0;      my $ctr = 0;
     my $clen = scalar(keys %shash);      my $clen = scalar(@resources);
     if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {      if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
  my %lt=&Apache::lonlocal::texthash(   my %lt=&Apache::lonlocal::texthash(
  'fin'=> 'Finalize order of resources',   'fin'=> 'Finalize order of resources',
Line 349  END Line 323  END
  'pa' => 'Path',   'pa' => 'Path',
                 'in' => 'Include'                  'in' => 'Include'
  );   );
  $r->print(&Apache::loncommon::start_page('Sort Imported Resources',   my $title = ($env{'form.recover'}) ? 'Recover Removed Resources'
  $js));                                             : 'Sort Imported Resources';
    $r->print(&Apache::loncommon::start_page($title, $js));
   
  $r->print(<<END);   $r->print(<<END);
 <b><font color="#888888">$lt{'fin'}</font></b>  
 <form method='post' action='/adm/groupsort' name='groupsort'  <form method='post' action='/adm/groupsort' name='groupsort'
       enctype='application/x-www-form-urlencoded'>        enctype='application/x-www-form-urlencoded'>
 <input type="hidden" name="fnum" value="$clen" />  <input type="hidden" name="fnum" value="$clen" />
Line 378  END Line 353  END
     my $resurl =       my $resurl = 
  &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());   &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
     $r->print(<<END);      $r->print(<<END);
   <b><font color="#888888">$lt{'fin'}</font></b>
 <input type="button" name="alter" value="$lt{'ci'}"  <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'}"  <input type="button" name="altersearch" value="$lt{'cs'}"
Line 410  END Line 386  END
         $r->print(&Apache::loncommon::inhibit_menu_check('input'));          $r->print(&Apache::loncommon::inhibit_menu_check('input'));
   
     }      }
     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {      foreach my $resource (@resources) {
  my $key=$_;  
  $ctr++;   $ctr++;
  my $iconname=&Apache::loncommon::icon($key);   my $iconname=&Apache::loncommon::icon($resource->{'url'});
  if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {   if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
     $r->print("<tr><td bgcolor='$fileclr'>");      $r->print("<tr><td bgcolor='$fileclr'>");
             if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {              if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
Line 422  END Line 397  END
  $r->print(&movers($clen,$ctr));   $r->print(&movers($clen,$ctr));
     }      }
  }   }
  $r->print(&hidden($ctr-1,$thash{$key},$key));   $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'}) || ($env{'form.bookmarks'})) {
     $r->print("</td>");      $r->print("</td>");
             unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {              unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
Line 433  END Line 409  END
     $r->print("<td bgcolor='$fileclr'>");      $r->print("<td bgcolor='$fileclr'>");
     $r->print("<img src='$iconname' />");      $r->print("<img src='$iconname' />");
     $r->print("</td><td bgcolor='$fileclr'>");      $r->print("</td><td bgcolor='$fileclr'>");
     $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");      $r->print($resource->{'title'}.$resource->{'notes'}."</td><td bgcolor='$fileclr'>\n");
     $r->print("$key</td></tr>\n");      $r->print($resource->{'url'}."</td></tr>\n");
  }    } 
     }      }
     if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {      if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
Line 454  END Line 430  END
   
 # --------------------------------------- Hidden values (returns scalar string)  # --------------------------------------- Hidden values (returns scalar string)
 sub hidden {  sub hidden {
     my ($sel,$title,$filelink) = @_;      my ($sel,$title,$filelink,$id) = @_;
     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.      my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
  '" />';   '" />';
     $filelink=~s|^/ext/|http://|;      $filelink=~s|^/ext/|http://|;
     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.      $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
  $filelink.'" />';   $filelink.'" />';
       $string .= '<input type="hidden" name="id'.$sel.'" value="'.$id.'" />';
     return $string;      return $string;
 }  }
   

Removed from v.1.55  
changed lines
  Added in v.1.60


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>