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