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