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