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