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