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