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.39 2006/02/07 19:46:30 albertel 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: return OK;
199: }
200:
201: # -------------------------------------------------------------- Read from file
202:
203: sub readfromfile {
204: my ($r,$shash,$thash,$nhash)=@_;
205: my $cont=&Apache::lonnet::getfile
206: (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
207: if ($cont==-1) {
208: $r->print('Unable to read file: '.
209: &Apache::lonnet::filelocation('',$env{'form.readfile'}));
210: } else {
211: my $parser = HTML::TokeParser->new(\$cont);
212: my $token;
213: my $n=1;
214: while ($token = $parser->get_token) {
215: if ($token->[0] eq 'S') {
216: if ($token->[1] eq 'resource') {
217: if ($env{'form.recover'}) {
218: if ($token->[2]->{'type'} ne 'zombie') { next; }
219: } else {
220: if ($token->[2]->{'type'} eq 'zombie') { next; }
221: }
222:
223: my $url=$token->[2]->{'src'};
224: my $name=$token->[2]->{'title'};
225: $name=~s/ \[\((\d+)\,(\w+)\,(\w+)\)\]$//;
226: if ($1) {
227: $$nhash{$url}='<br />'.&mt('Removed by ').
228: &Apache::loncommon::plainname($2,$3).', '.
229: &Apache::lonlocal::locallocaltime($1);
230: }
231: $name=~s/\&colon\;/\:/g;
232: $$thash{$url}=$name;
233: $$shash{$url}=$n;
234: $n++;
235: }
236: }
237: }
238: }
239: return ($shash,$thash);
240: }
241:
242: # ---------------------------------------------------------------- Main Handler
243: sub handler {
244: my $r = shift;
245:
246: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
247: ['acts','catalogmode','mode','readfile','recover']);
248: # color scheme
249: my $fileclr = '#ffffe6';
250: my $titleclr = '#ddffff';
251:
252: &Apache::loncommon::content_type($r,'text/html');
253: $r->send_http_header;
254: return OK if $r->header_only;
255:
256: # finish_import looks different for graphical or "simple" RAT
257: my $finishimport='';
258: my $begincondition='';
259: my $endcondition='';
260: if ($env{'form.readfile'}) {
261: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
262: $endcondition='}';
263: }
264: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
265: $finishimport=(<<ENDSMP);
266: function finish_import() {
267: opener.document.forms.simpleedit.importdetail.value='';
268: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
269: $begincondition
270: opener.document.forms.simpleedit.importdetail.value+='&'+
271: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
272: escape(eval("document.forms.groupsort.filelink"+num+".value"));
273: $endcondition
274: }
275: opener.document.forms.simpleedit.submit();
276: self.close();
277: }
278: ENDSMP
279: } else {
280: $finishimport=(<<ENDADV);
281: function finish_import() {
282: var linkflag=false;
283: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
284: $begincondition
285: insertRowInLastRow();
286: placeResourceInLastRow(
287: eval("document.forms.groupsort.title"+num+".value"),
288: eval("document.forms.groupsort.filelink"+num+".value"),
289: linkflag
290: );
291: linkflag=true;
292: $endcondition
293: }
294: opener.editmode=0;
295: opener.notclear=0;
296: opener.linkmode=0;
297: opener.draw();
298: self.close();
299: }
300: ENDADV
301: }
302:
303: # output start of web page
304: my $html=&Apache::lonxml::xmlbegin();
305: $r->print(<<END);
306: $html
307: <head>
308: <title>The LearningOnline Network With CAPA Group Sorter</title>
309: <script language='javascript'>
310: function insertRowInLastRow() {
311: opener.insertrow(opener.maxrow);
312: opener.addobj(opener.maxrow,'e&2');
313: }
314: function placeResourceInLastRow (title,url,linkflag) {
315: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
316: opener.escape(url),'false','normal');
317: opener.save();
318: if (linkflag) {
319: opener.joinres(opener.linkmode,opener.mostrecent,0);
320: }
321: opener.linkmode=opener.mostrecent;
322: }
323: $finishimport
324: function selectchange(val) {
325: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
326: orderchange(val,newval);
327: }
328: function move(val,newval) {
329: orderchange(val,newval);
330: }
331: function orderchange(val,newval) {
332: document.forms.groupsort.oldval.value=val;
333: document.forms.groupsort.newval.value=newval;
334: document.forms.groupsort.submit();
335: }
336: </script>
337: </head>
338: END
339: # read pertinent machine configuration
340: my $domain = $r->dir_config('lonDefDomain');
341: $iconpath = $r->dir_config('lonIconsURL') . "/";
342:
343: my %shash; # sort order (key is resource location, value is sort order)
344: my %thash; # title (key is resource location, value is title)
345: my %nhash; # notes (key is resource location);
346:
347: if ($env{'form.readfile'}) {
348: &readfromfile($r,\%shash,\%thash,\%nhash);
349: } else {
350: &readfromdb($r,\%shash,\%thash);
351: }
352:
353: my $ctr = 0;
354: my $clen = scalar(keys %shash);
355: if (($clen > 1) || ($env{'form.readfile'})) {
356: my %lt=&Apache::lonlocal::texthash(
357: 'fin'=> 'Finalize order of resources',
358: 'gb' => 'Go Back',
359: 'ns' => 'New Search',
360: 'fi' => 'Finish Import',
361: 'ca' => 'Cancel',
362: 'co' => 'Change Order',
363: 'ti' => 'Title',
364: 'pa' => 'Path',
365: 'in' => 'Include'
366: );
367: $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
368: $r->print(<<END);
369: <b><font color="#888888">$lt{'fin'}</font></b>
370: <form method='post' action='/adm/groupsort' name='groupsort'
371: enctype='application/x-www-form-urlencoded'>
372: <input type="hidden" name="fnum" value="$clen" />
373: <input type="hidden" name="oldval" value="" />
374: <input type="hidden" name="newval" value="" />
375: <input type="hidden" name="mode" value="$env{'form.mode'}" />
376: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
377: <input type="hidden" name="recover" value="$env{'form.recover'}" />
378: END
379:
380: # --- Expand here if "GO BACK" button desired
381: if ($env{'form.catalogmode'} eq 'groupimport') {
382: my $resurl = &Apache::loncommon::lastresurl();
383: $r->print(<<END);
384: <input type="button" name="alter" value="$lt{'gb'}"
385: onClick="window.location='$resurl?catalogmode=groupimport'" />
386: END
387: }
388: if ($env{'form.catalogmode'} eq 'groupsearch') {
389: $r->print(<<END);
390: <input type="button" name="alter" value="$lt{'ns'}"
391: onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />
392: END
393: }
394: # ---
395:
396: $r->print(<<END);
397: <input type="button" name="alter" value="$lt{'fi'}"
398: onClick="finish_import()" />
399: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
400: END
401: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
402: $r->print("<table border=0><tr>\n");
403: if ($env{'form.readfile'}) {
404: $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
405: } else {
406: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
407: }
408: $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
409: $r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
410: } else {
411: $r->print(<<END);
412: <body>
413: <form method='post' action='/adm/groupsort' name='groupsort'
414: enctype='application/x-www-form-urlencoded'>
415: <input type="hidden" name="fnum" value="$clen" />
416: <input type="hidden" name="oldval" value="" />
417: <input type="hidden" name="newval" value="" />
418: <input type="hidden" name="mode" value="$env{'form.mode'}" />
419: END
420: }
421: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
422: my $key=$_;
423: $ctr++;
424: my $iconname=&Apache::loncommon::icon($key);
425: if (($clen > 1) || ($env{'form.readfile'})) {
426: $r->print("<tr><td bgcolor='$fileclr'>");
427: if ($env{'form.readfile'}) {
428: $r->print(&checkbox($ctr-1));
429: } else {
430: $r->print(&movers($clen,$ctr));
431: }
432: }
433: $r->print(&hidden($ctr-1,$thash{$key},$key));
434: if (($clen > 1) || ($env{'form.readfile'})) {
435: $r->print("</td>");
436: unless ($env{'form.readfile'}) {
437: $r->print("<td bgcolor='$fileclr'>".
438: &select_box($clen,$ctr).
439: "</td>");
440: }
441: $r->print("<td bgcolor='$fileclr'>");
442: $r->print("<img src='$iconname' />");
443: $r->print("</td><td bgcolor='$fileclr'>");
444: $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
445: $r->print("$key</td></tr>\n");
446: }
447: }
448: if (($clen > 1) || ($env{'form.readfile'})) {
449: $r->print("</table></td></tr></table></form>");
450: } else {
451: $r->print(<<END);
452: <script type="text/javascript">
453: finish_import();
454: </script>
455: END
456: }
457: $r->print(<<END);
458: </body>
459: </html>
460: END
461:
462: return OK;
463: }
464:
465: # --------------------------------------- Hidden values (returns scalar string)
466: sub hidden {
467: my ($sel,$title,$filelink) = @_;
468: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
469: '" />';
470: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
471: $filelink.'" />';
472: return $string;
473: }
474:
475: # --------------------------------------- Moving arrows (returns scalar string)
476: sub movers {
477: my ($total,$sel) = @_;
478: my $dsel = $sel-1;
479: my $usel = $sel+1;
480: $usel = 1 if $usel > $total;
481: $dsel = $total if $dsel < 1;
482: my $string;
483: $string = (<<END);
484: <table border='0' cellspacing='0' cellpadding='0'>
485: <tr><td><a href='javascript:move($sel,$dsel)'>
486: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
487: <tr><td><a href='javascript:move($sel,$usel)'>
488: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
489: </table>
490: END
491: return $string;
492: }
493:
494: # ------------------------------------------ Select box (returns scalar string)
495: sub select_box {
496: my ($total,$sel) = @_;
497: my $string;
498: $string = '<select name="alt'.$sel.'"';
499: $string .= " onChange='selectchange($sel)'>";
500: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
501: for my $cur (1..$total) {
502: $string .= "<option name='o$cur' value='$cur'";
503: if ($cur == $sel) {
504: $string .= "selected";
505: }
506: $string .= ">$cur</option>";
507: }
508: $string .= "</select>\n";
509: return $string;
510: }
511:
512: # ------------------------------------------------------------------- Checkbox
513:
514: sub checkbox {
515: my $sel=shift;
516: return "<label><input type='checkbox' name='include$sel'".
517: ($env{"form.include$sel"}?' checked="checked"':'').
518: ' />'.&mt('Include').'</label>';
519: }
520:
521: 1;
522:
523: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>