Diff for /loncom/interface/groupsort.pm between versions 1.56 and 1.57

version 1.56, 2007/07/11 23:53:59 version 1.57, 2007/07/12 00:27:13
Line 82  sub update_actions_hash { Line 82  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 91  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 142  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 151  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 190  sub readfromfile { Line 163  sub readfromfile {
     my $url=$token->[2]->{'src'};      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'   => $url,
                     $$shash{$url}=$n;   'title' => $name,
                     $n++;   'note'  => $note, });
  }   }
     }      }
  }   }
     }      }
     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 322  END Line 294  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 412  END Line 382  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 424  END Line 393  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'}));
  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 435  END Line 404  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'})) {

Removed from v.1.56  
changed lines
  Added in v.1.57


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