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.21.2.1 2003/10/08 20:33:34 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: # YEAR=2001
30: # YEAR=2002
31: #
32: ###
33:
34: package Apache::groupsort;
35:
36: use strict;
37:
38: use Apache::Constants qw(:common);
39: use GDBM_File;
40: use Apache::loncommon;
41:
42: my %hash; # variable to tie to user specific database
43: my $iconpath; # variable to be accessible to multiple subroutines
44:
45: sub cleanup {
46: if (tied(%hash)){
47: &Apache::lonnet::logthis('Cleanup groupsort: hash');
48: unless (untie(%hash)) {
49: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
50: }
51: }
52: }
53:
54: # ---------------------------------------------------------------- Main Handler
55: sub handler {
56: my $r = shift;
57:
58: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
59: ['acts','catalogmode','mode']);
60: # color scheme
61: my $fileclr = '#ffffe6';
62: my $titleclr = '#ddffff';
63:
64: $r->content_type('text/html');
65: $r->send_http_header;
66: return OK if $r->header_only;
67:
68: # finish_import looks different for graphical or "simple" RAT
69: my $finishimport='';
70: if ($ENV{'form.mode'} eq 'simple' || $ENV{'form.mode'} eq '') {
71: $finishimport=(<<ENDSMP);
72: function finish_import() {
73: opener.document.forms.simpleedit.importdetail.value='';
74: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
75: opener.document.forms.simpleedit.importdetail.value+='&'+
76: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
77: escape(eval("document.forms.groupsort.filelink"+num+".value"));
78: }
79: opener.document.forms.simpleedit.submit();
80: self.close();
81: }
82: ENDSMP
83: } else {
84: $finishimport=(<<ENDADV);
85: function finish_import() {
86: var linkflag=false;
87: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
88: insertRowInLastRow();
89: placeResourceInLastRow(
90: eval("document.forms.groupsort.title"+num+".value"),
91: eval("document.forms.groupsort.filelink"+num+".value"),
92: linkflag
93: );
94: linkflag=true;
95: }
96: opener.editmode=0;
97: opener.notclear=0;
98: opener.linkmode=0;
99: opener.draw();
100: self.close();
101: }
102: ENDADV
103: }
104:
105: # output start of web page
106:
107: $r->print(<<END);
108: <html>
109: <head>
110: <title>The LearningOnline Network With CAPA Group Sorter</title>
111: <script language='javascript'>
112: function insertRowInLastRow() {
113: opener.insertrow(opener.maxrow);
114: opener.addobj(opener.maxrow,'e&2');
115: }
116: function placeResourceInLastRow (title,url,linkflag) {
117: opener.newresource(opener.maxrow,2,opener.escape(title),
118: opener.escape(url),'false','normal');
119: opener.save();
120: opener.mostrecent=opener.obj.length-1;
121: if (linkflag) {
122: opener.joinres(opener.linkmode,opener.mostrecent,0);
123: }
124: opener.linkmode=opener.mostrecent;
125: }
126: $finishimport
127: function selectchange(val) {
128: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
129: orderchange(val,newval);
130: }
131: function move(val,newval) {
132: orderchange(val,newval);
133: }
134: function orderchange(val,newval) {
135: document.forms.groupsort.oldval.value=val;
136: document.forms.groupsort.newval.value=newval;
137: document.forms.groupsort.submit();
138: }
139: </script>
140: </head>
141: END
142: $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
143: # read pertinent machine configuration
144: my $domain = $r->dir_config('lonDefDomain');
145: $iconpath = $r->dir_config('lonIconsURL') . "/";
146:
147: my %shash; # sort order (key is resource location, value is sort order)
148: my %thash; # title (key is resource location, value is title)
149:
150: my $diropendb;
151: # ------------------------------ which file do we open? Easy if explictly given
152: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
153: $diropendb =
154: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
155: }
156: elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
157: $diropendb =
158: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
159: }
160: elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
161: $diropendb =
162: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_groupsec.db";
163: }
164: # --------------------- not explicitly given, choose the one most recently used
165: else { # choose last accessed
166: my @dbfn;
167: my @dbst;
168:
169: $dbfn[0] =
170: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
171: $dbst[0]=-1;
172: if (-e $dbfn[0]) {
173: $dbst[0]=(stat($dbfn[0]))[9];
174: }
175: $dbfn[1] =
176: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
177: $dbst[1]=-1;
178: if (-e $dbfn[1]) {
179: $dbst[1]=(stat($dbfn[1]))[9];
180: }
181: $dbfn[2] =
182: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_groupsec.db";
183: $dbst[2]=-1;
184: if (-e $dbfn[2]) {
185: $dbst[2]=(stat($dbfn[2]))[9];
186: }
187: # Expand here for more modes
188: # ....
189:
190: # Okay, find most recent existing
191:
192: my $newest=0;
193: $diropendb='';
194: for (my $i=0; $i<=$#dbfn; $i++) {
195: if ($dbst[$i]>$newest) {
196: $newest=$dbst[$i];
197: $diropendb=$dbfn[$i];
198: }
199: }
200:
201: }
202: # ----------------------------- diropendb is now the filename of the db to open
203: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
204: my $acts = $ENV{'form.acts'};
205: my @Acts = split(/b/,$acts);
206: my %ahash;
207: my %achash;
208: my $ac = 0;
209: foreach (@Acts) {
210: my ($state,$ref) = split(/a/);
211: $ahash{$ref} = $state;
212: $achash{$ref} = $ac;
213: $ac++;
214: }
215: foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
216: my $key = $_;
217: if ($ahash{$key} eq '1') {
218: # my $keyz=join("<br />",keys %hash);
219: # print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
220: $hash{'store_'.$hash{'pre_'.$key.'_link'}} =
221: $hash{'pre_'.$key.'_title'};
222: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
223: $hash{'storectr'}+0;
224: $hash{'storectr'}++;
225: }
226: if ($ahash{$key} eq '0') {
227: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
228: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
229: }
230: }
231: }
232: foreach (keys %hash) {
233: if ($_ =~ /^store_/) {
234: my $key = $_;
235: $key =~ s/^store_//;
236: $shash{$key} = $hash{'storectr_'.$key};
237: $thash{$key} = $hash{'store_'.$key};
238: }
239: }
240: if ($ENV{'form.oldval'}) {
241: my $newctr = 0;
242: my %chash;
243: foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
244: my $key = $_;
245: $newctr++;
246: $shash{$key} = $newctr;
247: $hash{'storectr_'.$key} = $newctr;
248: $chash{$newctr} = $key;
249: }
250: my $oldval = $ENV{'form.oldval'};
251: my $newval = $ENV{'form.newval'};
252: if ($oldval != $newval) {
253: # when newval==0, then push down and delete
254: if ($newval!=0) {
255: $shash{$chash{$oldval}} = $newval;
256: $hash{'storectr_'.$chash{$oldval}} = $newval;
257: }
258: else {
259: $shash{$chash{$oldval}} = $newctr;
260: $hash{'storectr_'.$chash{$oldval}} = $newctr;
261: }
262: if ($newval==0) { # push down
263: my $newval2=$newctr;
264: for my $idx ($oldval..($newval2-1)) {
265: $shash{$chash{$idx+1}} = $idx;
266: $hash{'storectr_'.$chash{$idx+1}} = $idx;
267: }
268: delete $shash{$chash{$oldval}};
269: delete $hash{'storectr_'.$chash{$oldval}};
270: delete $hash{'store_'.$chash{$oldval}};
271: }
272: elsif ($oldval < $newval) { # push down
273: for my $idx ($oldval..($newval-1)) {
274: $shash{$chash{$idx+1}} = $idx;
275: $hash{'storectr_'.$chash{$idx+1}} = $idx;
276: }
277: }
278: elsif ($oldval > $newval) { # push up
279: for my $idx (reverse($newval..($oldval-1))) {
280: $shash{$chash{$idx}} = $idx+1;
281: $hash{'storectr_'.$chash{$idx}} = $idx+1;
282: }
283: }
284: }
285: }
286: } else {
287: $r->print('Unable to tie hash to db file</body></html>');
288: return OK;
289: }
290: untie %hash;
291: my $ctr = 0;
292: my $clen = scalar(keys %shash);
293: $r->print(<<END);
294: <b><font color="#888888">Finalize order of resources</font></b>
295: <form method='post' action='/adm/groupsort' name='groupsort'
296: enctype='application/x-www-form-urlencoded'>
297: <input type="hidden" name="fnum" value="$clen" />
298: <input type="hidden" name="oldval" value="" />
299: <input type="hidden" name="newval" value="" />
300: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
301: END
302:
303: # --- Expand here if "GO BACK" button desired
304: if ($ENV{'form.catalogmode'} eq 'groupimport') {
305: $r->print(<<END);
306: <input type="button" name="alter" value="GO BACK"
307: onClick="window.location='/res/?catalogmode=groupimport'" />
308: END
309: }
310: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
311: $r->print(<<END);
312: <input type="button" name="alter" value="New Search"
313: onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />
314: END
315: }
316: # ---
317:
318: $r->print(<<END);
319: <input type="button" name="alter" value="FINISH IMPORT"
320: onClick="finish_import()" />
321: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
322: END
323: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
324: $r->print("<table border=0><tr>\n");
325: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
326: "\n");
327: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
328: $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
329: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
330: my $key=$_;
331: $ctr++;
332: my @file_ext = split(/\./,$key);
333: my $curfext = $file_ext[scalar(@file_ext)-1];
334: my $iconname="unknown.gif";
335: my $embstyle = &Apache::loncommon::fileembstyle($curfext);
336: # The unless conditional that follows is a bit of overkill
337: $iconname = $curfext.".gif" unless
338: (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
339: $r->print("<tr><td bgcolor='$fileclr'>");
340: $r->print(&movers($clen,$ctr));
341: $r->print(&hidden($ctr-1,$thash{$key},$key));
342: $r->print("</td><td bgcolor='$fileclr'>");
343: $r->print(&select_box($clen,$ctr));
344: $r->print("</td><td bgcolor='$fileclr'>");
345: $r->print("<img src='$iconpath$iconname'>");
346: $r->print("</td><td bgcolor='$fileclr'>");
347: $r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
348: $r->print("$key</td></tr>\n");
349: }
350: $r->print("</table></td></tr></table></form>");
351: $r->print(<<END);
352: </body>
353: </html>
354: END
355: return OK;
356: }
357:
358: # --------------------------------------- Hidden values (returns scalar string)
359: sub hidden {
360: my ($sel,$title,$filelink) = @_;
361: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
362: '" />';
363: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
364: $filelink.'" />';
365: return $string;
366: }
367:
368: # --------------------------------------- Moving arrows (returns scalar string)
369: sub movers {
370: my ($total,$sel) = @_;
371: my $dsel = $sel-1;
372: my $usel = $sel+1;
373: $usel = 1 if $usel > $total;
374: $dsel = $total if $dsel < 1;
375: my $string;
376: $string = (<<END);
377: <table border='0' cellspacing='0' cellpadding='0'>
378: <tr><td><a href='javascript:move($sel,$dsel)'>
379: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
380: <tr><td><a href='javascript:move($sel,$usel)'>
381: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
382: </table>
383: END
384: return $string;
385: }
386:
387: # ------------------------------------------ Select box (returns scalar string)
388: sub select_box {
389: my ($total,$sel) = @_;
390: my $string;
391: $string = '<select name="alt'.$sel.'"';
392: $string .= " onChange='selectchange($sel)'>";
393: $string .= "<option name='o0' value='0'>remove</option>";
394: for my $cur (1..$total) {
395: $string .= "<option name='o$cur' value='$cur'";
396: if ($cur == $sel) {
397: $string .= "selected";
398: }
399: $string .= ">$cur</option>";
400: }
401: $string .= "</select>\n";
402: return $string;
403: }
404:
405: 1;
406:
407: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>