File:
[LON-CAPA] /
loncom /
interface /
groupsort.pm
Revision
1.38:
download - view:
text,
annotated -
select for diffs
Sat Jun 11 01:06:54 2005 UTC (19 years, 3 months ago) by
www
Branches:
MAIN
CVS tags:
version_2_1_2,
version_2_1_1,
version_2_1_0,
version_2_0_X,
version_2_0_99_1,
version_2_0_2,
version_2_0_1,
version_2_0_0,
version_1_99_3,
version_1_99_2,
version_1_99_1,
version_1_99_0,
HEAD
Bug #3738: being able to recover "zombie" resources (i.e., undelete without
data loss)
1: # The LearningOnline Network with CAPA
2: # The LON-CAPA group sort handler
3: # Allows for sorting prior to import into RAT.
4: #
5: # $Id: groupsort.pm,v 1.38 2005/06/11 01:06:54 www Exp $
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.
20: #
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
26: #
27: # http://www.lon-capa.org/
28: #
29: ###
30:
31: package Apache::groupsort;
32:
33: use strict;
34:
35: use Apache::Constants qw(:common);
36: use GDBM_File;
37: use Apache::loncommon;
38: use Apache::lonlocal;
39: use Apache::lonnet;
40:
41: my $iconpath; # variable to be accessible to multiple subroutines
42: my %hash; # variable to tie to user specific database
43:
44:
45: sub readfromdb {
46: my ($r,$shash,$thash)=@_;
47:
48: my $diropendb;
49: # ------------------------------ which file do we open? Easy if explictly given
50: if ($env{'form.catalogmode'} eq 'groupsearch') {
51: $diropendb =
52: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
53: } elsif ($env{'form.catalogmode'} eq 'groupimport') {
54: $diropendb =
55: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
56: } elsif ($env{'form.catalogmode'} eq 'groupsec') {
57: $diropendb =
58: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
59: } else {
60: # --------------------- not explicitly given, choose the one most recently used
61: my @dbfn;
62: my @dbst;
63:
64: $dbfn[0] =
65: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
66: $dbst[0]=-1;
67: if (-e $dbfn[0]) {
68: $dbst[0]=(stat($dbfn[0]))[9];
69: }
70: $dbfn[1] =
71: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
72: $dbst[1]=-1;
73: if (-e $dbfn[1]) {
74: $dbst[1]=(stat($dbfn[1]))[9];
75: }
76: $dbfn[2] =
77: "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
78: $dbst[2]=-1;
79: if (-e $dbfn[2]) {
80: $dbst[2]=(stat($dbfn[2]))[9];
81: }
82: # Expand here for more modes
83: # ....
84:
85: # Okay, find most recent existing
86:
87: my $newest=0;
88: $diropendb='';
89: for (my $i=0; $i<=$#dbfn; $i++) {
90: if ($dbst[$i]>$newest) {
91: $newest=$dbst[$i];
92: $diropendb=$dbfn[$i];
93: }
94: }
95:
96: }
97: # ----------------------------- diropendb is now the filename of the db to open
98: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
99: my $acts = $env{'form.acts'};
100: my @Acts = split(/b/,$acts);
101: my %ahash;
102: my %achash;
103: my $ac = 0;
104: foreach (@Acts) {
105: my ($state,$ref) = split(/a/);
106: $ahash{$ref} = $state;
107: $achash{$ref} = $ac;
108: $ac++;
109: }
110: foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
111: my $key = $_;
112: if ($ahash{$key} eq '1') {
113: # my $keyz=join("<br />",keys %hash);
114: # print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
115: $hash{'store_'.$hash{'pre_'.$key.'_link'}} =
116: $hash{'pre_'.$key.'_title'};
117: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
118: $hash{'storectr'}+0;
119: $hash{'storectr'}++;
120: }
121: if ($ahash{$key} eq '0') {
122: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
123: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
124: }
125: }
126: }
127: foreach (keys %hash) {
128: if ($_ =~ /^store_/) {
129: my $key = $_;
130: $key =~ s/^store_//;
131: $$shash{$key} = $hash{'storectr_'.$key};
132: if (&Apache::lonnet::gettitle($key) eq '') {
133: $$thash{$key} = $hash{'store_'.$key};
134: } else {
135: $$thash{$key} = &Apache::lonnet::gettitle($key);
136: }
137: }
138: }
139: if ($env{'form.oldval'}) {
140: my $newctr = 0;
141: my %chash;
142: foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
143: my $key = $_;
144: $newctr++;
145: $$shash{$key} = $newctr;
146: $hash{'storectr_'.$key} = $newctr;
147: $chash{$newctr} = $key;
148: }
149: my $oldval = $env{'form.oldval'};
150: my $newval = $env{'form.newval'};
151: if ($oldval != $newval) {
152: # when newval==0, then push down and delete
153: if ($newval!=0) {
154: $$shash{$chash{$oldval}} = $newval;
155: $hash{'storectr_'.$chash{$oldval}} = $newval;
156: } else {
157: $$shash{$chash{$oldval}} = $newctr;
158: $hash{'storectr_'.$chash{$oldval}} = $newctr;
159: }
160: if ($newval==0) { # push down
161: my $newval2=$newctr;
162: for my $idx ($oldval..($newval2-1)) {
163: $$shash{$chash{$idx+1}} = $idx;
164: $hash{'storectr_'.$chash{$idx+1}} = $idx;
165: }
166: delete $$shash{$chash{$oldval}};
167: delete $hash{'storectr_'.$chash{$oldval}};
168: delete $hash{'store_'.$chash{$oldval}};
169: } elsif ($oldval < $newval) { # push down
170: for my $idx ($oldval..($newval-1)) {
171: $$shash{$chash{$idx+1}} = $idx;
172: $hash{'storectr_'.$chash{$idx+1}} = $idx;
173: }
174: } elsif ($oldval > $newval) { # push up
175: for my $idx (reverse($newval..($oldval-1))) {
176: $$shash{$chash{$idx}} = $idx+1;
177: $hash{'storectr_'.$chash{$idx}} = $idx+1;
178: }
179: }
180: }
181: }
182: } else {
183: $r->print('Unable to tie hash to db file');
184: }
185: untie %hash;
186: return ($shash,$thash);
187: }
188:
189:
190:
191: sub cleanup {
192: if (tied(%hash)){
193: &Apache::lonnet::logthis('Cleanup groupsort: hash');
194: unless (untie(%hash)) {
195: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
196: }
197: }
198: }
199:
200: # -------------------------------------------------------------- Read from file
201:
202: sub readfromfile {
203: my ($r,$shash,$thash,$nhash)=@_;
204: my $cont=&Apache::lonnet::getfile
205: (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
206: if ($cont==-1) {
207: $r->print('Unable to read file: '.
208: &Apache::lonnet::filelocation('',$env{'form.readfile'}));
209: } else {
210: my $parser = HTML::TokeParser->new(\$cont);
211: my $token;
212: my $n=1;
213: while ($token = $parser->get_token) {
214: if ($token->[0] eq 'S') {
215: if ($token->[1] eq 'resource') {
216: if ($env{'form.recover'}) {
217: if ($token->[2]->{'type'} ne 'zombie') { next; }
218: } else {
219: if ($token->[2]->{'type'} eq 'zombie') { next; }
220: }
221:
222: my $url=$token->[2]->{'src'};
223: my $name=$token->[2]->{'title'};
224: $name=~s/ \[\((\d+)\,(\w+)\,(\w+)\)\]$//;
225: if ($1) {
226: $$nhash{$url}='<br />'.&mt('Removed by ').
227: &Apache::loncommon::plainname($2,$3).', '.
228: &Apache::lonlocal::locallocaltime($1);
229: }
230: $name=~s/\&colon\;/\:/g;
231: $$thash{$url}=$name;
232: $$shash{$url}=$n;
233: $n++;
234: }
235: }
236: }
237: }
238: return ($shash,$thash);
239: }
240:
241: # ---------------------------------------------------------------- Main Handler
242: sub handler {
243: my $r = shift;
244:
245: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
246: ['acts','catalogmode','mode','readfile','recover']);
247: # color scheme
248: my $fileclr = '#ffffe6';
249: my $titleclr = '#ddffff';
250:
251: &Apache::loncommon::content_type($r,'text/html');
252: $r->send_http_header;
253: return OK if $r->header_only;
254:
255: # finish_import looks different for graphical or "simple" RAT
256: my $finishimport='';
257: my $begincondition='';
258: my $endcondition='';
259: if ($env{'form.readfile'}) {
260: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
261: $endcondition='}';
262: }
263: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
264: $finishimport=(<<ENDSMP);
265: function finish_import() {
266: opener.document.forms.simpleedit.importdetail.value='';
267: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
268: $begincondition
269: opener.document.forms.simpleedit.importdetail.value+='&'+
270: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
271: escape(eval("document.forms.groupsort.filelink"+num+".value"));
272: $endcondition
273: }
274: opener.document.forms.simpleedit.submit();
275: self.close();
276: }
277: ENDSMP
278: } else {
279: $finishimport=(<<ENDADV);
280: function finish_import() {
281: var linkflag=false;
282: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
283: $begincondition
284: insertRowInLastRow();
285: placeResourceInLastRow(
286: eval("document.forms.groupsort.title"+num+".value"),
287: eval("document.forms.groupsort.filelink"+num+".value"),
288: linkflag
289: );
290: linkflag=true;
291: $endcondition
292: }
293: opener.editmode=0;
294: opener.notclear=0;
295: opener.linkmode=0;
296: opener.draw();
297: self.close();
298: }
299: ENDADV
300: }
301:
302: # output start of web page
303: my $html=&Apache::lonxml::xmlbegin();
304: $r->print(<<END);
305: $html
306: <head>
307: <title>The LearningOnline Network With CAPA Group Sorter</title>
308: <script language='javascript'>
309: function insertRowInLastRow() {
310: opener.insertrow(opener.maxrow);
311: opener.addobj(opener.maxrow,'e&2');
312: }
313: function placeResourceInLastRow (title,url,linkflag) {
314: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
315: opener.escape(url),'false','normal');
316: opener.save();
317: if (linkflag) {
318: opener.joinres(opener.linkmode,opener.mostrecent,0);
319: }
320: opener.linkmode=opener.mostrecent;
321: }
322: $finishimport
323: function selectchange(val) {
324: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
325: orderchange(val,newval);
326: }
327: function move(val,newval) {
328: orderchange(val,newval);
329: }
330: function orderchange(val,newval) {
331: document.forms.groupsort.oldval.value=val;
332: document.forms.groupsort.newval.value=newval;
333: document.forms.groupsort.submit();
334: }
335: </script>
336: </head>
337: END
338: # read pertinent machine configuration
339: my $domain = $r->dir_config('lonDefDomain');
340: $iconpath = $r->dir_config('lonIconsURL') . "/";
341:
342: my %shash; # sort order (key is resource location, value is sort order)
343: my %thash; # title (key is resource location, value is title)
344: my %nhash; # notes (key is resource location);
345:
346: if ($env{'form.readfile'}) {
347: &readfromfile($r,\%shash,\%thash,\%nhash);
348: } else {
349: &readfromdb($r,\%shash,\%thash);
350: }
351:
352: my $ctr = 0;
353: my $clen = scalar(keys %shash);
354: if (($clen > 1) || ($env{'form.readfile'})) {
355: my %lt=&Apache::lonlocal::texthash(
356: 'fin'=> 'Finalize order of resources',
357: 'gb' => 'Go Back',
358: 'ns' => 'New Search',
359: 'fi' => 'Finish Import',
360: 'ca' => 'Cancel',
361: 'co' => 'Change Order',
362: 'ti' => 'Title',
363: 'pa' => 'Path',
364: 'in' => 'Include'
365: );
366: $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
367: $r->print(<<END);
368: <b><font color="#888888">$lt{'fin'}</font></b>
369: <form method='post' action='/adm/groupsort' name='groupsort'
370: enctype='application/x-www-form-urlencoded'>
371: <input type="hidden" name="fnum" value="$clen" />
372: <input type="hidden" name="oldval" value="" />
373: <input type="hidden" name="newval" value="" />
374: <input type="hidden" name="mode" value="$env{'form.mode'}" />
375: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
376: <input type="hidden" name="recover" value="$env{'form.recover'}" />
377: END
378:
379: # --- Expand here if "GO BACK" button desired
380: if ($env{'form.catalogmode'} eq 'groupimport') {
381: my $resurl = &Apache::loncommon::lastresurl();
382: $r->print(<<END);
383: <input type="button" name="alter" value="$lt{'gb'}"
384: onClick="window.location='$resurl?catalogmode=groupimport'" />
385: END
386: }
387: if ($env{'form.catalogmode'} eq 'groupsearch') {
388: $r->print(<<END);
389: <input type="button" name="alter" value="$lt{'ns'}"
390: onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />
391: END
392: }
393: # ---
394:
395: $r->print(<<END);
396: <input type="button" name="alter" value="$lt{'fi'}"
397: onClick="finish_import()" />
398: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
399: END
400: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
401: $r->print("<table border=0><tr>\n");
402: if ($env{'form.readfile'}) {
403: $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
404: } else {
405: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
406: }
407: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
408: $r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
409: } else {
410: $r->print(<<END);
411: <body>
412: <form method='post' action='/adm/groupsort' name='groupsort'
413: enctype='application/x-www-form-urlencoded'>
414: <input type="hidden" name="fnum" value="$clen" />
415: <input type="hidden" name="oldval" value="" />
416: <input type="hidden" name="newval" value="" />
417: <input type="hidden" name="mode" value="$env{'form.mode'}" />
418: END
419: }
420: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
421: my $key=$_;
422: $ctr++;
423: my $iconname=&Apache::loncommon::icon($key);
424: if (($clen > 1) || ($env{'form.readfile'})) {
425: $r->print("<tr><td bgcolor='$fileclr'>");
426: if ($env{'form.readfile'}) {
427: $r->print(&checkbox($ctr-1));
428: } else {
429: $r->print(&movers($clen,$ctr));
430: }
431: }
432: $r->print(&hidden($ctr-1,$thash{$key},$key));
433: if (($clen > 1) || ($env{'form.readfile'})) {
434: $r->print("</td>");
435: unless ($env{'form.readfile'}) {
436: $r->print("<td bgcolor='$fileclr'>".
437: &select_box($clen,$ctr).
438: "</td>");
439: }
440: $r->print("<td bgcolor='$fileclr'>");
441: $r->print("<img src='$iconname' />");
442: $r->print("</td><td bgcolor='$fileclr'>");
443: $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
444: $r->print("$key</td></tr>\n");
445: }
446: }
447: if (($clen > 1) || ($env{'form.readfile'})) {
448: $r->print("</table></td></tr></table></form>");
449: } else {
450: $r->print(<<END);
451: <script type="text/javascript">
452: finish_import();
453: </script>
454: END
455: }
456: $r->print(<<END);
457: </body>
458: </html>
459: END
460:
461: return OK;
462: }
463:
464: # --------------------------------------- Hidden values (returns scalar string)
465: sub hidden {
466: my ($sel,$title,$filelink) = @_;
467: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
468: '" />';
469: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
470: $filelink.'" />';
471: return $string;
472: }
473:
474: # --------------------------------------- Moving arrows (returns scalar string)
475: sub movers {
476: my ($total,$sel) = @_;
477: my $dsel = $sel-1;
478: my $usel = $sel+1;
479: $usel = 1 if $usel > $total;
480: $dsel = $total if $dsel < 1;
481: my $string;
482: $string = (<<END);
483: <table border='0' cellspacing='0' cellpadding='0'>
484: <tr><td><a href='javascript:move($sel,$dsel)'>
485: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
486: <tr><td><a href='javascript:move($sel,$usel)'>
487: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
488: </table>
489: END
490: return $string;
491: }
492:
493: # ------------------------------------------ Select box (returns scalar string)
494: sub select_box {
495: my ($total,$sel) = @_;
496: my $string;
497: $string = '<select name="alt'.$sel.'"';
498: $string .= " onChange='selectchange($sel)'>";
499: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
500: for my $cur (1..$total) {
501: $string .= "<option name='o$cur' value='$cur'";
502: if ($cur == $sel) {
503: $string .= "selected";
504: }
505: $string .= ">$cur</option>";
506: }
507: $string .= "</select>\n";
508: return $string;
509: }
510:
511: # ------------------------------------------------------------------- Checkbox
512:
513: sub checkbox {
514: my $sel=shift;
515: return "<label><input type='checkbox' name='include$sel'".
516: ($env{"form.include$sel"}?' checked="checked"':'').
517: ' />'.&mt('Include').'</label>';
518: }
519:
520: 1;
521:
522: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>