Annotation of loncom/interface/groupsort.pm, revision 1.50
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.50 ! albertel 5: # $Id: groupsort.pm,v 1.49 2006/09/12 22:13:05 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',
331: 'ca' => 'Cancel',
332: 'co' => 'Change Order',
333: 'ti' => 'Title',
1.37 www 334: 'pa' => 'Path',
335: 'in' => 'Include'
1.23 www 336: );
1.40 albertel 337: $r->print(&Apache::loncommon::start_page('Sort Imported Resources',
338: $js));
1.22 albertel 339: $r->print(<<END);
1.23 www 340: <b><font color="#888888">$lt{'fin'}</font></b>
1.14 matthew 341: <form method='post' action='/adm/groupsort' name='groupsort'
342: enctype='application/x-www-form-urlencoded'>
1.1 harris41 343: <input type="hidden" name="fnum" value="$clen" />
344: <input type="hidden" name="oldval" value="" />
345: <input type="hidden" name="newval" value="" />
1.31 albertel 346: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36 www 347: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
1.44 www 348: <input type="hidden" name="bookmarks" value="$env{'form.bookmarks'}" />
1.36 www 349: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3 harris41 350: END
1.11 www 351:
1.42 www 352: # --- Continue Buttons
1.49 albertel 353: my $resurl =
354: &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
1.42 www 355: $r->print(<<END);
356: <input type="button" name="alter" value="$lt{'ci'}"
1.43 www 357: onClick="window.location='$resurl?catalogmode=import'" />
1.42 www 358: <input type="button" name="altersearch" value="$lt{'cs'}"
1.43 www 359: onClick="window.location='/adm/searchcat?catalogmode=import'" />
1.3 harris41 360: END
1.22 albertel 361: # ---
1.11 www 362:
1.22 albertel 363: $r->print(<<END);
1.23 www 364: <input type="button" name="alter" value="$lt{'fi'}"
1.2 harris41 365: onClick="finish_import()" />
1.23 www 366: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1 harris41 367: END
1.22 albertel 368: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
369: $r->print("<table border=0><tr>\n");
1.44 www 370: if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 371: $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
372: } else {
373: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
374: }
1.23 www 375: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
376: $r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22 albertel 377: } else {
1.40 albertel 378: $r->print(&Apache::loncommon::start_page(undef,$js,
1.41 banghart 379: {'only_body' => 1}));
1.22 albertel 380: $r->print(<<END);
381: <form method='post' action='/adm/groupsort' name='groupsort'
382: enctype='application/x-www-form-urlencoded'>
383: <input type="hidden" name="fnum" value="$clen" />
384: <input type="hidden" name="oldval" value="" />
385: <input type="hidden" name="newval" value="" />
1.31 albertel 386: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22 albertel 387: END
388: }
1.5 harris41 389: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1 harris41 390: my $key=$_;
391: $ctr++;
1.25 albertel 392: my $iconname=&Apache::loncommon::icon($key);
1.44 www 393: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.22 albertel 394: $r->print("<tr><td bgcolor='$fileclr'>");
1.44 www 395: if (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 396: $r->print(&checkbox($ctr-1));
397: } else {
398: $r->print(&movers($clen,$ctr));
399: }
1.22 albertel 400: }
1.1 harris41 401: $r->print(&hidden($ctr-1,$thash{$key},$key));
1.44 www 402: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 403: $r->print("</td>");
1.44 www 404: unless (($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.37 www 405: $r->print("<td bgcolor='$fileclr'>".
406: &select_box($clen,$ctr).
407: "</td>");
408: }
409: $r->print("<td bgcolor='$fileclr'>");
1.26 albertel 410: $r->print("<img src='$iconname' />");
1.22 albertel 411: $r->print("</td><td bgcolor='$fileclr'>");
1.36 www 412: $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
1.22 albertel 413: $r->print("$key</td></tr>\n");
414: }
415: }
1.44 www 416: if (($clen > 1) || ($env{'form.readfile'}) || ($env{'form.bookmarks'})) {
1.22 albertel 417: $r->print("</table></td></tr></table></form>");
418: } else {
419: $r->print(<<END);
420: <script type="text/javascript">
421: finish_import();
422: </script>
423: END
424: }
1.40 albertel 425:
426: $r->print(&Apache::loncommon::end_page());
1.22 albertel 427:
1.1 harris41 428: return OK;
429: }
430:
1.2 harris41 431: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 432: sub hidden {
1.2 harris41 433: my ($sel,$title,$filelink) = @_;
434: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
435: '" />';
1.48 albertel 436: $filelink=~s|^/ext/|http://|;
1.2 harris41 437: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
438: $filelink.'" />';
1.1 harris41 439: return $string;
440: }
441:
1.2 harris41 442: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 443: sub movers {
1.2 harris41 444: my ($total,$sel) = @_;
445: my $dsel = $sel-1;
446: my $usel = $sel+1;
447: $usel = 1 if $usel > $total;
448: $dsel = $total if $dsel < 1;
1.1 harris41 449: my $string;
1.2 harris41 450: $string = (<<END);
1.1 harris41 451: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 452: <tr><td><a href='javascript:move($sel,$dsel)'>
453: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
454: <tr><td><a href='javascript:move($sel,$usel)'>
455: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 456: </table>
457: END
458: return $string;
459: }
1.2 harris41 460:
461: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 462: sub select_box {
1.2 harris41 463: my ($total,$sel) = @_;
1.1 harris41 464: my $string;
1.2 harris41 465: $string = '<select name="alt'.$sel.'"';
466: $string .= " onChange='selectchange($sel)'>";
1.36 www 467: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1 harris41 468: for my $cur (1..$total) {
1.2 harris41 469: $string .= "<option name='o$cur' value='$cur'";
470: if ($cur == $sel) {
471: $string .= "selected";
1.1 harris41 472: }
1.2 harris41 473: $string .= ">$cur</option>";
1.1 harris41 474: }
1.2 harris41 475: $string .= "</select>\n";
1.1 harris41 476: return $string;
477: }
478:
1.37 www 479: # ------------------------------------------------------------------- Checkbox
480:
481: sub checkbox {
482: my $sel=shift;
483: return "<label><input type='checkbox' name='include$sel'".
484: ($env{"form.include$sel"}?' checked="checked"':'').
485: ' />'.&mt('Include').'</label>';
486: }
487:
1.1 harris41 488: 1;
489:
490: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>