Annotation of loncom/interface/groupsort.pm, revision 1.40
1.1 harris41 1: # The LearningOnline Network with CAPA
1.4 harris41 2: # The LON-CAPA group sort handler
3: # Allows for sorting prior to import into RAT.
4: #
1.40 ! albertel 5: # $Id: groupsort.pm,v 1.39 2006/02/07 19:46:30 albertel Exp $
1.4 harris41 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
1.1 harris41 20: #
1.4 harris41 21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
1.1 harris41 26: #
1.4 harris41 27: # http://www.lon-capa.org/
1.1 harris41 28: #
1.4 harris41 29: ###
1.1 harris41 30:
31: package Apache::groupsort;
32:
33: use strict;
34:
35: use Apache::Constants qw(:common);
36: use GDBM_File;
1.15 www 37: use Apache::loncommon;
1.23 www 38: use Apache::lonlocal;
1.27 taceyjo1 39: use Apache::lonnet;
1.1 harris41 40:
1.33 www 41: my $iconpath; # variable to be accessible to multiple subroutines
1.2 harris41 42: my %hash; # variable to tie to user specific database
1.8 www 43:
1.2 harris41 44:
1.33 www 45: sub readfromdb {
46: my ($r,$shash,$thash)=@_;
1.9 www 47:
1.4 harris41 48: my $diropendb;
1.11 www 49: # ------------------------------ which file do we open? Easy if explictly given
1.31 albertel 50: if ($env{'form.catalogmode'} eq 'groupsearch') {
1.4 harris41 51: $diropendb =
1.31 albertel 52: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
1.33 www 53: } elsif ($env{'form.catalogmode'} eq 'groupimport') {
1.4 harris41 54: $diropendb =
1.31 albertel 55: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
1.33 www 56: } elsif ($env{'form.catalogmode'} eq 'groupsec') {
1.11 www 57: $diropendb =
1.31 albertel 58: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
1.33 www 59: } else {
1.11 www 60: # --------------------- not explicitly given, choose the one most recently used
61: my @dbfn;
62: my @dbst;
63:
64: $dbfn[0] =
1.31 albertel 65: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
1.11 www 66: $dbst[0]=-1;
67: if (-e $dbfn[0]) {
68: $dbst[0]=(stat($dbfn[0]))[9];
1.6 harris41 69: }
1.11 www 70: $dbfn[1] =
1.31 albertel 71: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
1.11 www 72: $dbst[1]=-1;
73: if (-e $dbfn[1]) {
74: $dbst[1]=(stat($dbfn[1]))[9];
1.6 harris41 75: }
1.11 www 76: $dbfn[2] =
1.31 albertel 77: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
1.11 www 78: $dbst[2]=-1;
79: if (-e $dbfn[2]) {
80: $dbst[2]=(stat($dbfn[2]))[9];
81: }
82: # Expand here for more modes
83: # ....
84:
85: # Okay, find most recent existing
86:
87: my $newest=0;
1.12 www 88: $diropendb='';
1.11 www 89: for (my $i=0; $i<=$#dbfn; $i++) {
90: if ($dbst[$i]>$newest) {
91: $newest=$dbst[$i];
92: $diropendb=$dbfn[$i];
93: }
1.6 harris41 94: }
1.11 www 95:
1.4 harris41 96: }
1.11 www 97: # ----------------------------- diropendb is now the filename of the db to open
1.13 albertel 98: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.31 albertel 99: my $acts = $env{'form.acts'};
1.2 harris41 100: my @Acts = split(/b/,$acts);
1.1 harris41 101: my %ahash;
102: my %achash;
1.2 harris41 103: my $ac = 0;
1.5 harris41 104: foreach (@Acts) {
1.2 harris41 105: my ($state,$ref) = split(/a/);
106: $ahash{$ref} = $state;
107: $achash{$ref} = $ac;
1.1 harris41 108: $ac++;
1.5 harris41 109: }
110: foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2 harris41 111: my $key = $_;
1.1 harris41 112: if ($ahash{$key} eq '1') {
1.3 harris41 113: # my $keyz=join("<br />",keys %hash);
114: # print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
1.2 harris41 115: $hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1 harris41 116: $hash{'pre_'.$key.'_title'};
1.2 harris41 117: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1 harris41 118: $hash{'storectr'}+0;
119: $hash{'storectr'}++;
120: }
121: if ($ahash{$key} eq '0') {
122: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
123: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
124: }
125: }
1.5 harris41 126: }
127: foreach (keys %hash) {
1.1 harris41 128: if ($_ =~ /^store_/) {
1.2 harris41 129: my $key = $_;
130: $key =~ s/^store_//;
1.33 www 131: $$shash{$key} = $hash{'storectr_'.$key};
1.27 taceyjo1 132: if (&Apache::lonnet::gettitle($key) eq '') {
1.33 www 133: $$thash{$key} = $hash{'store_'.$key};
1.28 albertel 134: } else {
1.33 www 135: $$thash{$key} = &Apache::lonnet::gettitle($key);
1.28 albertel 136: }
1.1 harris41 137: }
1.5 harris41 138: }
1.31 albertel 139: if ($env{'form.oldval'}) {
1.2 harris41 140: my $newctr = 0;
1.1 harris41 141: my %chash;
1.33 www 142: foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
1.2 harris41 143: my $key = $_;
1.1 harris41 144: $newctr++;
1.33 www 145: $$shash{$key} = $newctr;
1.2 harris41 146: $hash{'storectr_'.$key} = $newctr;
147: $chash{$newctr} = $key;
1.5 harris41 148: }
1.31 albertel 149: my $oldval = $env{'form.oldval'};
150: my $newval = $env{'form.newval'};
1.2 harris41 151: if ($oldval != $newval) {
1.3 harris41 152: # when newval==0, then push down and delete
153: if ($newval!=0) {
1.33 www 154: $$shash{$chash{$oldval}} = $newval;
1.3 harris41 155: $hash{'storectr_'.$chash{$oldval}} = $newval;
1.33 www 156: } else {
157: $$shash{$chash{$oldval}} = $newctr;
1.3 harris41 158: $hash{'storectr_'.$chash{$oldval}} = $newctr;
159: }
160: if ($newval==0) { # push down
161: my $newval2=$newctr;
162: for my $idx ($oldval..($newval2-1)) {
1.33 www 163: $$shash{$chash{$idx+1}} = $idx;
1.3 harris41 164: $hash{'storectr_'.$chash{$idx+1}} = $idx;
165: }
1.33 www 166: delete $$shash{$chash{$oldval}};
1.3 harris41 167: delete $hash{'storectr_'.$chash{$oldval}};
168: delete $hash{'store_'.$chash{$oldval}};
1.33 www 169: } elsif ($oldval < $newval) { # push down
1.1 harris41 170: for my $idx ($oldval..($newval-1)) {
1.33 www 171: $$shash{$chash{$idx+1}} = $idx;
1.2 harris41 172: $hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1 harris41 173: }
1.33 www 174: } elsif ($oldval > $newval) { # push up
1.1 harris41 175: for my $idx (reverse($newval..($oldval-1))) {
1.33 www 176: $$shash{$chash{$idx}} = $idx+1;
1.2 harris41 177: $hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1 harris41 178: }
179: }
180: }
181: }
182: } else {
1.34 www 183: $r->print('Unable to tie hash to db file');
1.1 harris41 184: }
185: untie %hash;
1.33 www 186: return ($shash,$thash);
187: }
188:
189:
190:
191: sub cleanup {
192: if (tied(%hash)){
193: &Apache::lonnet::logthis('Cleanup groupsort: hash');
194: unless (untie(%hash)) {
195: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
196: }
197: }
1.39 albertel 198: return OK;
1.33 www 199: }
200:
1.34 www 201: # -------------------------------------------------------------- Read from file
202:
203: sub readfromfile {
1.35 www 204: my ($r,$shash,$thash,$nhash)=@_;
1.34 www 205: my $cont=&Apache::lonnet::getfile
206: (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
207: if ($cont==-1) {
208: $r->print('Unable to read file: '.
209: &Apache::lonnet::filelocation('',$env{'form.readfile'}));
210: } else {
211: my $parser = HTML::TokeParser->new(\$cont);
212: my $token;
1.35 www 213: my $n=1;
1.34 www 214: while ($token = $parser->get_token) {
215: if ($token->[0] eq 'S') {
216: if ($token->[1] eq 'resource') {
217: if ($env{'form.recover'}) {
218: if ($token->[2]->{'type'} ne 'zombie') { next; }
219: } else {
220: if ($token->[2]->{'type'} eq 'zombie') { next; }
221: }
1.35 www 222:
223: my $url=$token->[2]->{'src'};
1.34 www 224: my $name=$token->[2]->{'title'};
225: $name=~s/ \[\((\d+)\,(\w+)\,(\w+)\)\]$//;
226: if ($1) {
1.35 www 227: $$nhash{$url}='<br />'.&mt('Removed by ').
1.34 www 228: &Apache::loncommon::plainname($2,$3).', '.
229: &Apache::lonlocal::locallocaltime($1);
230: }
1.37 www 231: $name=~s/\&colon\;/\:/g;
1.35 www 232: $$thash{$url}=$name;
233: $$shash{$url}=$n;
234: $n++;
1.34 www 235: }
236: }
237: }
238: }
239: return ($shash,$thash);
240: }
241:
1.33 www 242: # ---------------------------------------------------------------- Main Handler
243: sub handler {
244: my $r = shift;
245:
246: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.34 www 247: ['acts','catalogmode','mode','readfile','recover']);
1.33 www 248: # color scheme
249: my $fileclr = '#ffffe6';
250: my $titleclr = '#ddffff';
251:
252: &Apache::loncommon::content_type($r,'text/html');
253: $r->send_http_header;
254: return OK if $r->header_only;
255:
256: # finish_import looks different for graphical or "simple" RAT
257: my $finishimport='';
1.37 www 258: my $begincondition='';
259: my $endcondition='';
260: if ($env{'form.readfile'}) {
261: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
262: $endcondition='}';
263: }
1.33 www 264: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
265: $finishimport=(<<ENDSMP);
266: function finish_import() {
267: opener.document.forms.simpleedit.importdetail.value='';
268: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 269: $begincondition
1.33 www 270: opener.document.forms.simpleedit.importdetail.value+='&'+
271: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
272: escape(eval("document.forms.groupsort.filelink"+num+".value"));
1.37 www 273: $endcondition
1.33 www 274: }
275: opener.document.forms.simpleedit.submit();
276: self.close();
277: }
278: ENDSMP
279: } else {
280: $finishimport=(<<ENDADV);
281: function finish_import() {
282: var linkflag=false;
283: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 284: $begincondition
1.33 www 285: insertRowInLastRow();
286: placeResourceInLastRow(
287: eval("document.forms.groupsort.title"+num+".value"),
288: eval("document.forms.groupsort.filelink"+num+".value"),
289: linkflag
290: );
291: linkflag=true;
1.37 www 292: $endcondition
1.33 www 293: }
294: opener.editmode=0;
295: opener.notclear=0;
296: opener.linkmode=0;
297: opener.draw();
298: self.close();
299: }
300: ENDADV
301: }
302:
303: # output start of web page
1.40 ! albertel 304: my $js = <<END;
! 305: <script type="text/javascript">
1.33 www 306: function insertRowInLastRow() {
307: opener.insertrow(opener.maxrow);
308: opener.addobj(opener.maxrow,'e&2');
309: }
310: function placeResourceInLastRow (title,url,linkflag) {
311: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
312: opener.escape(url),'false','normal');
313: opener.save();
314: if (linkflag) {
315: opener.joinres(opener.linkmode,opener.mostrecent,0);
316: }
317: opener.linkmode=opener.mostrecent;
318: }
319: $finishimport
320: function selectchange(val) {
321: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
322: orderchange(val,newval);
323: }
324: function move(val,newval) {
325: orderchange(val,newval);
326: }
327: function orderchange(val,newval) {
328: document.forms.groupsort.oldval.value=val;
329: document.forms.groupsort.newval.value=newval;
330: document.forms.groupsort.submit();
331: }
332: </script>
333: END
334: # read pertinent machine configuration
335: my $domain = $r->dir_config('lonDefDomain');
336: $iconpath = $r->dir_config('lonIconsURL') . "/";
337:
338: my %shash; # sort order (key is resource location, value is sort order)
339: my %thash; # title (key is resource location, value is title)
1.35 www 340: my %nhash; # notes (key is resource location);
1.33 www 341:
1.34 www 342: if ($env{'form.readfile'}) {
1.35 www 343: &readfromfile($r,\%shash,\%thash,\%nhash);
1.34 www 344: } else {
345: &readfromdb($r,\%shash,\%thash);
346: }
1.33 www 347:
1.2 harris41 348: my $ctr = 0;
349: my $clen = scalar(keys %shash);
1.38 www 350: if (($clen > 1) || ($env{'form.readfile'})) {
1.23 www 351: my %lt=&Apache::lonlocal::texthash(
352: 'fin'=> 'Finalize order of resources',
353: 'gb' => 'Go Back',
354: 'ns' => 'New Search',
355: 'fi' => 'Finish Import',
356: 'ca' => 'Cancel',
357: 'co' => 'Change Order',
358: 'ti' => 'Title',
1.37 www 359: 'pa' => 'Path',
360: 'in' => 'Include'
1.23 www 361: );
1.40 ! albertel 362: $r->print(&Apache::loncommon::start_page('Sort Imported Resources',
! 363: $js));
1.22 albertel 364: $r->print(<<END);
1.23 www 365: <b><font color="#888888">$lt{'fin'}</font></b>
1.14 matthew 366: <form method='post' action='/adm/groupsort' name='groupsort'
367: enctype='application/x-www-form-urlencoded'>
1.1 harris41 368: <input type="hidden" name="fnum" value="$clen" />
369: <input type="hidden" name="oldval" value="" />
370: <input type="hidden" name="newval" value="" />
1.31 albertel 371: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36 www 372: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
373: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3 harris41 374: END
1.11 www 375:
1.22 albertel 376: # --- Expand here if "GO BACK" button desired
1.31 albertel 377: if ($env{'form.catalogmode'} eq 'groupimport') {
1.29 matthew 378: my $resurl = &Apache::loncommon::lastresurl();
1.22 albertel 379: $r->print(<<END);
1.23 www 380: <input type="button" name="alter" value="$lt{'gb'}"
1.29 matthew 381: onClick="window.location='$resurl?catalogmode=groupimport'" />
1.3 harris41 382: END
1.22 albertel 383: }
1.31 albertel 384: if ($env{'form.catalogmode'} eq 'groupsearch') {
1.22 albertel 385: $r->print(<<END);
1.23 www 386: <input type="button" name="alter" value="$lt{'ns'}"
1.16 matthew 387: onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />
1.3 harris41 388: END
1.22 albertel 389: }
390: # ---
1.11 www 391:
1.22 albertel 392: $r->print(<<END);
1.23 www 393: <input type="button" name="alter" value="$lt{'fi'}"
1.2 harris41 394: onClick="finish_import()" />
1.23 www 395: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1 harris41 396: END
1.22 albertel 397: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
398: $r->print("<table border=0><tr>\n");
1.37 www 399: if ($env{'form.readfile'}) {
400: $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
401: } else {
402: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
403: }
1.23 www 404: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
405: $r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22 albertel 406: } else {
1.40 ! albertel 407: $r->print(&Apache::loncommon::start_page(undef,$js,
! 408: {'only_body' => 1});
1.22 albertel 409: $r->print(<<END);
410: <form method='post' action='/adm/groupsort' name='groupsort'
411: enctype='application/x-www-form-urlencoded'>
412: <input type="hidden" name="fnum" value="$clen" />
413: <input type="hidden" name="oldval" value="" />
414: <input type="hidden" name="newval" value="" />
1.31 albertel 415: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22 albertel 416: END
417: }
1.5 harris41 418: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1 harris41 419: my $key=$_;
420: $ctr++;
1.25 albertel 421: my $iconname=&Apache::loncommon::icon($key);
1.38 www 422: if (($clen > 1) || ($env{'form.readfile'})) {
1.22 albertel 423: $r->print("<tr><td bgcolor='$fileclr'>");
1.37 www 424: if ($env{'form.readfile'}) {
425: $r->print(&checkbox($ctr-1));
426: } else {
427: $r->print(&movers($clen,$ctr));
428: }
1.22 albertel 429: }
1.1 harris41 430: $r->print(&hidden($ctr-1,$thash{$key},$key));
1.38 www 431: if (($clen > 1) || ($env{'form.readfile'})) {
1.37 www 432: $r->print("</td>");
433: unless ($env{'form.readfile'}) {
434: $r->print("<td bgcolor='$fileclr'>".
435: &select_box($clen,$ctr).
436: "</td>");
437: }
438: $r->print("<td bgcolor='$fileclr'>");
1.26 albertel 439: $r->print("<img src='$iconname' />");
1.22 albertel 440: $r->print("</td><td bgcolor='$fileclr'>");
1.36 www 441: $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
1.22 albertel 442: $r->print("$key</td></tr>\n");
443: }
444: }
1.38 www 445: if (($clen > 1) || ($env{'form.readfile'})) {
1.22 albertel 446: $r->print("</table></td></tr></table></form>");
447: } else {
448: $r->print(<<END);
449: <script type="text/javascript">
450: finish_import();
451: </script>
452: END
453: }
1.40 ! albertel 454:
! 455: $r->print(&Apache::loncommon::end_page());
1.22 albertel 456:
1.1 harris41 457: return OK;
458: }
459:
1.2 harris41 460: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 461: sub hidden {
1.2 harris41 462: my ($sel,$title,$filelink) = @_;
463: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
464: '" />';
465: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
466: $filelink.'" />';
1.1 harris41 467: return $string;
468: }
469:
1.2 harris41 470: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 471: sub movers {
1.2 harris41 472: my ($total,$sel) = @_;
473: my $dsel = $sel-1;
474: my $usel = $sel+1;
475: $usel = 1 if $usel > $total;
476: $dsel = $total if $dsel < 1;
1.1 harris41 477: my $string;
1.2 harris41 478: $string = (<<END);
1.1 harris41 479: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 480: <tr><td><a href='javascript:move($sel,$dsel)'>
481: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
482: <tr><td><a href='javascript:move($sel,$usel)'>
483: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 484: </table>
485: END
486: return $string;
487: }
1.2 harris41 488:
489: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 490: sub select_box {
1.2 harris41 491: my ($total,$sel) = @_;
1.1 harris41 492: my $string;
1.2 harris41 493: $string = '<select name="alt'.$sel.'"';
494: $string .= " onChange='selectchange($sel)'>";
1.36 www 495: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1 harris41 496: for my $cur (1..$total) {
1.2 harris41 497: $string .= "<option name='o$cur' value='$cur'";
498: if ($cur == $sel) {
499: $string .= "selected";
1.1 harris41 500: }
1.2 harris41 501: $string .= ">$cur</option>";
1.1 harris41 502: }
1.2 harris41 503: $string .= "</select>\n";
1.1 harris41 504: return $string;
505: }
506:
1.37 www 507: # ------------------------------------------------------------------- Checkbox
508:
509: sub checkbox {
510: my $sel=shift;
511: return "<label><input type='checkbox' name='include$sel'".
512: ($env{"form.include$sel"}?' checked="checked"':'').
513: ' />'.&mt('Include').'</label>';
514: }
515:
1.1 harris41 516: 1;
517:
518: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>