Annotation of loncom/interface/groupsort.pm, revision 1.56
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.56 ! albertel 5: # $Id: groupsort.pm,v 1.55 2007/07/11 23:51:19 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.50 albertel 40: use LONCAPA();
1.1 harris41 41:
1.33 www 42: my $iconpath; # variable to be accessible to multiple subroutines
1.2 harris41 43: my %hash; # variable to tie to user specific database
1.8 www 44:
1.2 harris41 45:
1.55 albertel 46: sub update_actions_hash {
47: my ($hash) = @_;
48: my $acts=$env{'form.acts'};
49: my @Acts=split(/b/,$acts);
50: my %ahash;
51: my %achash;
52: my $ac=0;
53: # some initial hashes for working with data
54: foreach (@Acts) {
55: my ($state,$ref)=split(/a/);
56: $ahash{$ref}=$state;
57: $achash{$ref}=$ac;
58: $ac++;
59: }
60: # sorting through the actions and changing the global database hash
61: foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
62: if ($ahash{$key} eq '1') {
63: $hash->{'store_'.$hash{'pre_'.$key.'_link'}}=
64: $hash->{'pre_'.$key.'_title'};
65: $hash->{'storectr_'.$hash{'pre_'.$key.'_link'}}=
66: $hash->{'storectr'}+0;
67: $hash->{'storectr'}++;
68: }
69: if ($ahash{$key} eq '0') {
70: if ($hash->{'store_'.$hash{'pre_'.$key.'_link'}}) {
71: delete($hash->{'store_'.$hash{'pre_'.$key.'_link'}});
72: }
73: }
74: }
75: # deleting the previously cached listing
76: foreach my $key (keys(%{ $hash })) {
77: next if ($key !~ /^pre_(\d+)_link/);
78: my $which = $1;
79: delete($hash->{'pre_'.$which.'_title'});
80: delete($hash->{'pre_'.$which.'_link'});
81: }
82: }
83:
1.33 www 84: sub readfromdb {
85: my ($r,$shash,$thash)=@_;
1.9 www 86:
1.42 www 87: my $diropendb =
88: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
1.11 www 89:
90: # ----------------------------- diropendb is now the filename of the db to open
1.13 albertel 91: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.55 albertel 92: &update_actions_hash(\%hash);
93:
1.5 harris41 94: foreach (keys %hash) {
1.1 harris41 95: if ($_ =~ /^store_/) {
1.2 harris41 96: my $key = $_;
97: $key =~ s/^store_//;
1.33 www 98: $$shash{$key} = $hash{'storectr_'.$key};
1.27 taceyjo1 99: if (&Apache::lonnet::gettitle($key) eq '') {
1.33 www 100: $$thash{$key} = $hash{'store_'.$key};
1.28 albertel 101: } else {
1.33 www 102: $$thash{$key} = &Apache::lonnet::gettitle($key);
1.28 albertel 103: }
1.1 harris41 104: }
1.5 harris41 105: }
1.31 albertel 106: if ($env{'form.oldval'}) {
1.2 harris41 107: my $newctr = 0;
1.1 harris41 108: my %chash;
1.33 www 109: foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
1.2 harris41 110: my $key = $_;
1.1 harris41 111: $newctr++;
1.33 www 112: $$shash{$key} = $newctr;
1.2 harris41 113: $hash{'storectr_'.$key} = $newctr;
114: $chash{$newctr} = $key;
1.5 harris41 115: }
1.31 albertel 116: my $oldval = $env{'form.oldval'};
117: my $newval = $env{'form.newval'};
1.2 harris41 118: if ($oldval != $newval) {
1.3 harris41 119: # when newval==0, then push down and delete
120: if ($newval!=0) {
1.33 www 121: $$shash{$chash{$oldval}} = $newval;
1.3 harris41 122: $hash{'storectr_'.$chash{$oldval}} = $newval;
1.33 www 123: } else {
124: $$shash{$chash{$oldval}} = $newctr;
1.3 harris41 125: $hash{'storectr_'.$chash{$oldval}} = $newctr;
126: }
127: if ($newval==0) { # push down
128: my $newval2=$newctr;
129: for my $idx ($oldval..($newval2-1)) {
1.33 www 130: $$shash{$chash{$idx+1}} = $idx;
1.3 harris41 131: $hash{'storectr_'.$chash{$idx+1}} = $idx;
132: }
1.33 www 133: delete $$shash{$chash{$oldval}};
1.3 harris41 134: delete $hash{'storectr_'.$chash{$oldval}};
135: delete $hash{'store_'.$chash{$oldval}};
1.33 www 136: } elsif ($oldval < $newval) { # push down
1.1 harris41 137: for my $idx ($oldval..($newval-1)) {
1.33 www 138: $$shash{$chash{$idx+1}} = $idx;
1.2 harris41 139: $hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1 harris41 140: }
1.33 www 141: } elsif ($oldval > $newval) { # push up
1.1 harris41 142: for my $idx (reverse($newval..($oldval-1))) {
1.33 www 143: $$shash{$chash{$idx}} = $idx+1;
1.2 harris41 144: $hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1 harris41 145: }
146: }
147: }
148: }
149: } else {
1.34 www 150: $r->print('Unable to tie hash to db file');
1.1 harris41 151: }
152: untie %hash;
1.33 www 153: return ($shash,$thash);
154: }
155:
156:
157:
158: sub cleanup {
159: if (tied(%hash)){
160: &Apache::lonnet::logthis('Cleanup groupsort: hash');
161: unless (untie(%hash)) {
162: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
163: }
164: }
1.39 albertel 165: return OK;
1.33 www 166: }
167:
1.34 www 168: # -------------------------------------------------------------- Read from file
169:
170: sub readfromfile {
1.35 www 171: my ($r,$shash,$thash,$nhash)=@_;
1.34 www 172: my $cont=&Apache::lonnet::getfile
173: (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
174: if ($cont==-1) {
175: $r->print('Unable to read file: '.
176: &Apache::lonnet::filelocation('',$env{'form.readfile'}));
177: } else {
178: my $parser = HTML::TokeParser->new(\$cont);
179: my $token;
1.35 www 180: my $n=1;
1.34 www 181: while ($token = $parser->get_token) {
182: if ($token->[0] eq 'S') {
183: if ($token->[1] eq 'resource') {
184: if ($env{'form.recover'}) {
185: if ($token->[2]->{'type'} ne 'zombie') { next; }
186: } else {
187: if ($token->[2]->{'type'} eq 'zombie') { next; }
188: }
1.35 www 189:
190: my $url=$token->[2]->{'src'};
1.34 www 191: my $name=$token->[2]->{'title'};
1.50 albertel 192: $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
1.34 www 193: if ($1) {
1.35 www 194: $$nhash{$url}='<br />'.&mt('Removed by ').
1.34 www 195: &Apache::loncommon::plainname($2,$3).', '.
196: &Apache::lonlocal::locallocaltime($1);
197: }
1.37 www 198: $name=~s/\&colon\;/\:/g;
1.35 www 199: $$thash{$url}=$name;
200: $$shash{$url}=$n;
201: $n++;
1.34 www 202: }
203: }
204: }
205: }
206: return ($shash,$thash);
207: }
208:
1.44 www 209: # --------------------------------------------------------- Read from bookmarks
210:
211: sub readfrombookmarks {
212: my ($r,$shash,$thash)=@_;
213: my %bookmarks=&Apache::lonnet::dump('bookmarks');
214: # the bookmark "hash" is just one entry
215: # it's a javascript program code with arguments like ('title','url');
216: my @bookmarks=($bookmarks{'bookmarks'}=~/\((?:\'([^\']+)\'\,\'([^\']+)\'|\"([^\"]+)\"\,\"([^\"]+)\")\)\;/g);
217: my $order=1;
218: for (my $index=0;$index<($#bookmarks+1)/2;$index++) {
219: if ($bookmarks[$index*2+1]) {
220: $$thash{$bookmarks[$index*2+1]}=$bookmarks[$index*2];
221: $$thash{$bookmarks[$index*2+1]}=~s/^LON\-CAPA\s+//;
222: $$shash{$bookmarks[$index*2+1]}=$order;
223: $order++;
224: }
225: }
226: return ($shash,$thash);
227: }
228:
1.33 www 229: # ---------------------------------------------------------------- Main Handler
230: sub handler {
231: my $r = shift;
232:
233: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.44 www 234: ['acts','mode','readfile','recover','bookmarks']);
1.33 www 235: # color scheme
236: my $fileclr = '#ffffe6';
237: my $titleclr = '#ddffff';
238:
239: &Apache::loncommon::content_type($r,'text/html');
240: $r->send_http_header;
241: return OK if $r->header_only;
242:
243: # finish_import looks different for graphical or "simple" RAT
244: my $finishimport='';
1.37 www 245: my $begincondition='';
246: my $endcondition='';
1.44 www 247: if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 248: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
249: $endcondition='}';
250: }
1.33 www 251: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
252: $finishimport=(<<ENDSMP);
253: function finish_import() {
254: opener.document.forms.simpleedit.importdetail.value='';
255: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 256: $begincondition
1.33 www 257: opener.document.forms.simpleedit.importdetail.value+='&'+
258: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
259: escape(eval("document.forms.groupsort.filelink"+num+".value"));
1.37 www 260: $endcondition
1.33 www 261: }
262: opener.document.forms.simpleedit.submit();
263: self.close();
264: }
265: ENDSMP
266: } else {
267: $finishimport=(<<ENDADV);
268: function finish_import() {
269: var linkflag=false;
270: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 271: $begincondition
1.33 www 272: insertRowInLastRow();
273: placeResourceInLastRow(
274: eval("document.forms.groupsort.title"+num+".value"),
275: eval("document.forms.groupsort.filelink"+num+".value"),
276: linkflag
277: );
278: linkflag=true;
1.37 www 279: $endcondition
1.33 www 280: }
281: opener.editmode=0;
282: opener.notclear=0;
283: opener.linkmode=0;
284: opener.draw();
285: self.close();
286: }
287: ENDADV
288: }
289:
290: # output start of web page
1.40 albertel 291: my $js = <<END;
292: <script type="text/javascript">
1.33 www 293: function insertRowInLastRow() {
294: opener.insertrow(opener.maxrow);
295: opener.addobj(opener.maxrow,'e&2');
296: }
297: function placeResourceInLastRow (title,url,linkflag) {
298: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
299: opener.escape(url),'false','normal');
300: opener.save();
301: if (linkflag) {
302: opener.joinres(opener.linkmode,opener.mostrecent,0);
303: }
304: opener.linkmode=opener.mostrecent;
305: }
306: $finishimport
307: function selectchange(val) {
308: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
309: orderchange(val,newval);
310: }
311: function move(val,newval) {
312: orderchange(val,newval);
313: }
314: function orderchange(val,newval) {
315: document.forms.groupsort.oldval.value=val;
316: document.forms.groupsort.newval.value=newval;
317: document.forms.groupsort.submit();
318: }
319: </script>
320: END
321: # read pertinent machine configuration
322: my $domain = $r->dir_config('lonDefDomain');
323: $iconpath = $r->dir_config('lonIconsURL') . "/";
324:
325: my %shash; # sort order (key is resource location, value is sort order)
326: my %thash; # title (key is resource location, value is title)
1.35 www 327: my %nhash; # notes (key is resource location);
1.33 www 328:
1.34 www 329: if ($env{'form.readfile'}) {
1.35 www 330: &readfromfile($r,\%shash,\%thash,\%nhash);
1.44 www 331: } elsif ($env{'form.bookmarks'}) {
332: &readfrombookmarks($r,\%shash,\%thash);
1.34 www 333: } else {
334: &readfromdb($r,\%shash,\%thash);
335: }
1.33 www 336:
1.2 harris41 337: my $ctr = 0;
338: my $clen = scalar(keys %shash);
1.44 www 339: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.23 www 340: my %lt=&Apache::lonlocal::texthash(
341: 'fin'=> 'Finalize order of resources',
1.42 www 342: 'ci' => 'Continue Import',
343: 'cs' => 'Continue Search',
1.23 www 344: 'fi' => 'Finish Import',
1.51 albertel 345: 're' => 'Recover Checked',
1.23 www 346: 'ca' => 'Cancel',
347: 'co' => 'Change Order',
348: 'ti' => 'Title',
1.37 www 349: 'pa' => 'Path',
350: 'in' => 'Include'
1.23 www 351: );
1.56 ! albertel 352: my $title = ($env{'form.recover'}) ? 'Recover Removed Resources'
! 353: : 'Sort Imported Resources';
! 354: $r->print(&Apache::loncommon::start_page($title, $js));
! 355:
1.22 albertel 356: $r->print(<<END);
1.14 matthew 357: <form method='post' action='/adm/groupsort' name='groupsort'
358: enctype='application/x-www-form-urlencoded'>
1.1 harris41 359: <input type="hidden" name="fnum" value="$clen" />
360: <input type="hidden" name="oldval" value="" />
361: <input type="hidden" name="newval" value="" />
1.31 albertel 362: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36 www 363: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
1.44 www 364: <input type="hidden" name="bookmarks" value="$env{'form.bookmarks'}" />
1.36 www 365: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3 harris41 366: END
1.11 www 367:
1.54 albertel 368: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
1.51 albertel 369: # ---
370:
371: if ($env{'form.recover'}) {
372: $r->print(<<END);
373: <input type="button" name="alter" value="$lt{'re'}"
374: onClick="finish_import()" />
375: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
376: END
377: } else {
1.42 www 378: # --- Continue Buttons
1.51 albertel 379: my $resurl =
380: &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
381: $r->print(<<END);
1.56 ! albertel 382: <b><font color="#888888">$lt{'fin'}</font></b>
1.42 www 383: <input type="button" name="alter" value="$lt{'ci'}"
1.54 albertel 384: onClick="window.location='$resurl?inhibitmenu=yes&catalogmode=import'" />
1.42 www 385: <input type="button" name="altersearch" value="$lt{'cs'}"
1.54 albertel 386: onClick="window.location='/adm/searchcat?inhibitmenu=yes&catalogmode=import'" />
1.23 www 387: <input type="button" name="alter" value="$lt{'fi'}"
1.2 harris41 388: onClick="finish_import()" />
1.23 www 389: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1 harris41 390: END
1.51 albertel 391: }
1.22 albertel 392: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
393: $r->print("<table border=0><tr>\n");
1.44 www 394: if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 395: $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
396: } else {
397: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
398: }
1.23 www 399: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
400: $r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22 albertel 401: } else {
1.40 albertel 402: $r->print(&Apache::loncommon::start_page(undef,$js,
1.41 banghart 403: {'only_body' => 1}));
1.22 albertel 404: $r->print(<<END);
405: <form method='post' action='/adm/groupsort' name='groupsort'
406: enctype='application/x-www-form-urlencoded'>
407: <input type="hidden" name="fnum" value="$clen" />
408: <input type="hidden" name="oldval" value="" />
409: <input type="hidden" name="newval" value="" />
1.31 albertel 410: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22 albertel 411: END
1.54 albertel 412: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
413:
1.22 albertel 414: }
1.5 harris41 415: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1 harris41 416: my $key=$_;
417: $ctr++;
1.25 albertel 418: my $iconname=&Apache::loncommon::icon($key);
1.44 www 419: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.22 albertel 420: $r->print("<tr><td bgcolor='$fileclr'>");
1.44 www 421: if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 422: $r->print(&checkbox($ctr-1));
423: } else {
424: $r->print(&movers($clen,$ctr));
425: }
1.22 albertel 426: }
1.1 harris41 427: $r->print(&hidden($ctr-1,$thash{$key},$key));
1.44 www 428: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 429: $r->print("</td>");
1.44 www 430: unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 431: $r->print("<td bgcolor='$fileclr'>".
432: &select_box($clen,$ctr).
433: "</td>");
434: }
435: $r->print("<td bgcolor='$fileclr'>");
1.26 albertel 436: $r->print("<img src='$iconname' />");
1.22 albertel 437: $r->print("</td><td bgcolor='$fileclr'>");
1.36 www 438: $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
1.22 albertel 439: $r->print("$key</td></tr>\n");
440: }
441: }
1.44 www 442: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.22 albertel 443: $r->print("</table></td></tr></table></form>");
444: } else {
445: $r->print(<<END);
446: <script type="text/javascript">
447: finish_import();
448: </script>
449: END
450: }
1.40 albertel 451:
452: $r->print(&Apache::loncommon::end_page());
1.22 albertel 453:
1.1 harris41 454: return OK;
455: }
456:
1.2 harris41 457: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 458: sub hidden {
1.2 harris41 459: my ($sel,$title,$filelink) = @_;
460: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
461: '" />';
1.48 albertel 462: $filelink=~s|^/ext/|http://|;
1.2 harris41 463: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
464: $filelink.'" />';
1.1 harris41 465: return $string;
466: }
467:
1.2 harris41 468: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 469: sub movers {
1.2 harris41 470: my ($total,$sel) = @_;
471: my $dsel = $sel-1;
472: my $usel = $sel+1;
473: $usel = 1 if $usel > $total;
474: $dsel = $total if $dsel < 1;
1.1 harris41 475: my $string;
1.2 harris41 476: $string = (<<END);
1.1 harris41 477: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 478: <tr><td><a href='javascript:move($sel,$dsel)'>
479: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
480: <tr><td><a href='javascript:move($sel,$usel)'>
481: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 482: </table>
483: END
484: return $string;
485: }
1.2 harris41 486:
487: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 488: sub select_box {
1.2 harris41 489: my ($total,$sel) = @_;
1.1 harris41 490: my $string;
1.2 harris41 491: $string = '<select name="alt'.$sel.'"';
492: $string .= " onChange='selectchange($sel)'>";
1.36 www 493: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1 harris41 494: for my $cur (1..$total) {
1.2 harris41 495: $string .= "<option name='o$cur' value='$cur'";
496: if ($cur == $sel) {
497: $string .= "selected";
1.1 harris41 498: }
1.2 harris41 499: $string .= ">$cur</option>";
1.1 harris41 500: }
1.2 harris41 501: $string .= "</select>\n";
1.1 harris41 502: return $string;
503: }
504:
1.37 www 505: # ------------------------------------------------------------------- Checkbox
506:
507: sub checkbox {
508: my $sel=shift;
509: return "<label><input type='checkbox' name='include$sel'".
510: ($env{"form.include$sel"}?' checked="checked"':'').
511: ' />'.&mt('Include').'</label>';
512: }
513:
1.1 harris41 514: 1;
515:
516: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>