Annotation of loncom/interface/groupsort.pm, revision 1.68.6.2
1.1 harris41 1: # The LearningOnline Network with CAPA
1.4 harris41 2: # The LON-CAPA group sort handler
3: # Allows for sorting prior to import into RAT.
4: #
1.68.6.2! raeburn 5: # $Id: groupsort.pm,v 1.68.6.1 2012/05/02 19:10:13 raeburn Exp $
1.4 harris41 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.
1.1 harris41 20: #
1.4 harris41 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
1.1 harris41 26: #
1.4 harris41 27: # http://www.lon-capa.org/
1.1 harris41 28: #
1.4 harris41 29: ###
1.1 harris41 30:
31: package Apache::groupsort;
32:
33: use strict;
34:
35: use Apache::Constants qw(:common);
36: use GDBM_File;
1.15 www 37: use Apache::loncommon;
1.23 www 38: use Apache::lonlocal;
1.27 taceyjo1 39: use Apache::lonnet;
1.61 albertel 40: use LONCAPA;
1.1 harris41 41:
1.33 www 42: my $iconpath; # variable to be accessible to multiple subroutines
1.2 harris41 43: my %hash; # variable to tie to user specific database
1.8 www 44:
1.2 harris41 45:
1.55 albertel 46: sub update_actions_hash {
47: my ($hash) = @_;
1.60 banghart 48: # be careful in here, there is also a global %hash
1.55 albertel 49: my $acts=$env{'form.acts'};
50: my @Acts=split(/b/,$acts);
51: my %ahash;
52: my %achash;
1.60 banghart 53: # some initial hashes for working with data
1.55 albertel 54: my $ac=0;
55: foreach (@Acts) {
1.60 banghart 56: my ($state,$ref)=split(/a/);
1.55 albertel 57: $ahash{$ref}=$state;
58: $achash{$ref}=$ac;
59: $ac++;
60: }
61: # sorting through the actions and changing the global database hash
62: foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
63: if ($ahash{$key} eq '1') {
1.60 banghart 64: $hash->{'store_'.$hash->{'pre_'.$key.'_link'}}=
1.55 albertel 65: $hash->{'pre_'.$key.'_title'};
1.60 banghart 66: $hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}}=
1.55 albertel 67: $hash->{'storectr'}+0;
68: $hash->{'storectr'}++;
69: }
70: if ($ahash{$key} eq '0') {
1.60 banghart 71: if ($hash->{'store_'.$hash->{'pre_'.$key.'_link'}}) {
72: delete($hash->{'store_'.$hash->{'pre_'.$key.'_link'}});
73: delete($hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}});
1.55 albertel 74: }
75: }
76: }
77: # deleting the previously cached listing
78: foreach my $key (keys(%{ $hash })) {
79: next if ($key !~ /^pre_(\d+)_link/);
80: my $which = $1;
81: delete($hash->{'pre_'.$which.'_title'});
82: delete($hash->{'pre_'.$which.'_link'});
83: }
84: }
85:
1.33 www 86: sub readfromdb {
1.57 albertel 87: my ($r,$resources)=@_;
1.9 www 88:
1.68 foxr 89: my $diropendb = LONCAPA::tempdir() .
90: "$env{'user.domain'}_$env{'user.name'}_sel_res.db";
1.11 www 91:
92: # ----------------------------- diropendb is now the filename of the db to open
1.13 albertel 93: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.55 albertel 94: &update_actions_hash(\%hash);
95:
1.57 albertel 96: my %temp_resources;
97: foreach my $key (keys(%hash)) {
98: next if ($key !~ /^store_/);
99: my ($url) = ($key =~ /^store_(.*)/);
100: $temp_resources{$hash{'storectr_'.$url}}{'url'}=$url;
101: $temp_resources{$hash{'storectr_'.$url}}{'title'}=
102: &Apache::lonnet::gettitle($url);
103: }
104:
105: # use the temp, since there might be gaps in the counting
106: foreach my $item (sort {$a <=> $b} (keys(%temp_resources))) {
107: push(@{ $resources },$temp_resources{$item});
1.5 harris41 108: }
1.57 albertel 109:
1.31 albertel 110: if ($env{'form.oldval'}) {
1.57 albertel 111: my $res = splice(@{$resources},$env{'form.oldval'}-1,1);
112: if ($env{'form.newval'} == 0) {
113: # picked 'discard'
114: my $url = $res->{'url'};
115: delete($hash{'storectr_'.$url});
116: delete($hash{'store_'.$url});
117: } else {
118: splice(@{$resources},$env{'form.newval'}-1,0,$res);
1.1 harris41 119: }
1.57 albertel 120: }
121: # store out new order
122: foreach my $which (0..$#$resources) {
123: my $url = $resources->[$which]{'url'};
124: $hash{'storectr_'.$url} = $which;
1.1 harris41 125: }
126: } else {
1.34 www 127: $r->print('Unable to tie hash to db file');
1.1 harris41 128: }
1.57 albertel 129: untie(%hash);
1.33 www 130: }
131:
132:
133:
134: sub cleanup {
135: if (tied(%hash)){
136: &Apache::lonnet::logthis('Cleanup groupsort: hash');
137: unless (untie(%hash)) {
138: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
139: }
140: }
1.39 albertel 141: return OK;
1.33 www 142: }
143:
1.34 www 144: # -------------------------------------------------------------- Read from file
145:
146: sub readfromfile {
1.57 albertel 147: my ($r,$resources)=@_;
1.34 www 148: my $cont=&Apache::lonnet::getfile
149: (&Apache::lonnet::filelocation('',$env{'form.readfile'}));
150: if ($cont==-1) {
151: $r->print('Unable to read file: '.
152: &Apache::lonnet::filelocation('',$env{'form.readfile'}));
153: } else {
154: my $parser = HTML::TokeParser->new(\$cont);
155: my $token;
156: while ($token = $parser->get_token) {
157: if ($token->[0] eq 'S') {
158: if ($token->[1] eq 'resource') {
159: if ($env{'form.recover'}) {
160: if ($token->[2]->{'type'} ne 'zombie') { next; }
161: } else {
162: if ($token->[2]->{'type'} eq 'zombie') { next; }
163: }
1.35 www 164:
1.34 www 165: my $name=$token->[2]->{'title'};
1.50 albertel 166: $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
1.57 albertel 167: my $note;
1.34 www 168: if ($1) {
1.57 albertel 169: $note = '<br />'.&mt('Removed by ').
1.34 www 170: &Apache::loncommon::plainname($2,$3).', '.
171: &Apache::lonlocal::locallocaltime($1);
172: }
1.37 www 173: $name=~s/\&colon\;/\:/g;
1.58 albertel 174: push(@{$resources}, {'url' => $token->[2]->{'src'},
1.57 albertel 175: 'title' => $name,
1.58 albertel 176: 'note' => $note,
177: 'id' => $token->[2]->{'id'},});
1.34 www 178: }
179: }
180: }
181: }
182: }
183:
1.44 www 184:
1.33 www 185: # ---------------------------------------------------------------- Main Handler
186: sub handler {
187: my $r = shift;
188:
189: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.68.6.2! raeburn 190: ['acts','mode','readfile','recover']);
1.33 www 191:
192: &Apache::loncommon::content_type($r,'text/html');
193: $r->send_http_header;
194: return OK if $r->header_only;
195:
196: # finish_import looks different for graphical or "simple" RAT
197: my $finishimport='';
1.37 www 198: my $begincondition='';
199: my $endcondition='';
1.68.6.2! raeburn 200: if (($env{'form.readfile'})) {
1.37 www 201: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
202: $endcondition='}';
203: }
1.33 www 204: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
205: $finishimport=(<<ENDSMP);
206: function finish_import() {
207: opener.document.forms.simpleedit.importdetail.value='';
208: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 209: $begincondition
1.33 www 210: opener.document.forms.simpleedit.importdetail.value+='&'+
1.61 albertel 211: eval("document.forms.groupsort.title"+num+".value")+'='+
212: eval("document.forms.groupsort.filelink"+num+".value")+'='+
213: eval("document.forms.groupsort.id"+num+".value");
1.37 www 214: $endcondition
1.33 www 215: }
216: opener.document.forms.simpleedit.submit();
217: self.close();
218: }
219: ENDSMP
220: } else {
221: $finishimport=(<<ENDADV);
222: function finish_import() {
223: var linkflag=false;
224: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 225: $begincondition
1.33 www 226: insertRowInLastRow();
227: placeResourceInLastRow(
228: eval("document.forms.groupsort.title"+num+".value"),
229: eval("document.forms.groupsort.filelink"+num+".value"),
1.59 albertel 230: eval("document.forms.groupsort.id"+num+".value"),
1.33 www 231: linkflag
232: );
233: linkflag=true;
1.37 www 234: $endcondition
1.33 www 235: }
236: opener.editmode=0;
237: opener.notclear=0;
238: opener.linkmode=0;
239: opener.draw();
240: self.close();
241: }
242: ENDADV
243: }
244:
245: # output start of web page
1.40 albertel 246: my $js = <<END;
247: <script type="text/javascript">
1.33 www 248: function insertRowInLastRow() {
249: opener.insertrow(opener.maxrow);
250: opener.addobj(opener.maxrow,'e&2');
251: }
1.59 albertel 252: function placeResourceInLastRow (title,url,id,linkflag) {
1.33 www 253: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
1.59 albertel 254: opener.escape(url),'false','normal',id);
1.33 www 255: opener.save();
256: if (linkflag) {
257: opener.joinres(opener.linkmode,opener.mostrecent,0);
258: }
259: opener.linkmode=opener.mostrecent;
260: }
261: $finishimport
262: function selectchange(val) {
263: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
264: orderchange(val,newval);
265: }
266: function move(val,newval) {
267: orderchange(val,newval);
268: }
269: function orderchange(val,newval) {
270: document.forms.groupsort.oldval.value=val;
271: document.forms.groupsort.newval.value=newval;
272: document.forms.groupsort.submit();
273: }
274: </script>
275: END
276: # read pertinent machine configuration
277: my $domain = $r->dir_config('lonDefDomain');
278: $iconpath = $r->dir_config('lonIconsURL') . "/";
279:
1.57 albertel 280: my @resources;
1.33 www 281:
1.34 www 282: if ($env{'form.readfile'}) {
1.57 albertel 283: &readfromfile($r,\@resources);
1.34 www 284: } else {
1.57 albertel 285: &readfromdb($r,\@resources);
1.34 www 286: }
1.33 www 287:
1.2 harris41 288: my $ctr = 0;
1.57 albertel 289: my $clen = scalar(@resources);
1.66 bisitz 290: my $title = '';
291: if ($env{'form.recover'}) {
292: $title = 'Recover Removed Resources';
293: } else {
294: $title = 'Sort Imported Resources';
295: }
1.68.6.2! raeburn 296: if (($clen > 1) || ($env{'form.readfile'})) {
1.23 www 297: my %lt=&Apache::lonlocal::texthash(
298: 'fin'=> 'Finalize order of resources',
1.42 www 299: 'ci' => 'Continue Import',
300: 'cs' => 'Continue Search',
1.23 www 301: 'fi' => 'Finish Import',
1.51 albertel 302: 're' => 'Recover Checked',
1.64 bisitz 303: 'ip' => 'Import Checked',
1.23 www 304: 'ca' => 'Cancel',
305: 'co' => 'Change Order',
306: 'ti' => 'Title',
1.37 www 307: 'pa' => 'Path',
308: 'in' => 'Include'
1.23 www 309: );
1.64 bisitz 310:
1.56 albertel 311: $r->print(&Apache::loncommon::start_page($title, $js));
1.66 bisitz 312: $r->print('<h1>'.&mt($title).'</h1>');
1.56 albertel 313:
1.22 albertel 314: $r->print(<<END);
1.14 matthew 315: <form method='post' action='/adm/groupsort' name='groupsort'
316: enctype='application/x-www-form-urlencoded'>
1.1 harris41 317: <input type="hidden" name="fnum" value="$clen" />
318: <input type="hidden" name="oldval" value="" />
319: <input type="hidden" name="newval" value="" />
1.31 albertel 320: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36 www 321: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
322: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3 harris41 323: END
1.11 www 324:
1.54 albertel 325: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
1.51 albertel 326: # ---
1.65 bisitz 327:
328: my $buttontext = $lt{'re'};
1.68.6.2! raeburn 329: if ($env{'form.recover'}) {
1.51 albertel 330: $r->print(<<END);
1.65 bisitz 331: <input type="button" name="alter" value="$buttontext"
1.51 albertel 332: onClick="finish_import()" />
333: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
334: END
335: } else {
1.42 www 336: # --- Continue Buttons
1.51 albertel 337: my $resurl =
338: &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
339: $r->print(<<END);
1.62 bisitz 340: <h2>$lt{'fin'}</h2>
341: <div>
1.42 www 342: <input type="button" name="alter" value="$lt{'ci'}"
1.54 albertel 343: onClick="window.location='$resurl?inhibitmenu=yes&catalogmode=import'" />
1.42 www 344: <input type="button" name="altersearch" value="$lt{'cs'}"
1.54 albertel 345: onClick="window.location='/adm/searchcat?inhibitmenu=yes&catalogmode=import'" />
1.23 www 346: <input type="button" name="alter" value="$lt{'fi'}"
1.2 harris41 347: onClick="finish_import()" />
1.23 www 348: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.62 bisitz 349: </div>
350: <br />
1.1 harris41 351: END
1.51 albertel 352: }
1.65 bisitz 353:
354: # Only display header if content exists
355: if ($clen > 0) {
356: $r->print(&Apache::loncommon::start_data_table()
357: .&Apache::loncommon::start_data_table_header_row());
1.67 wenzelju 358: if (($env{'form.readfile'})) {
1.65 bisitz 359: $r->print("<th>$lt{'in'}</th>\n");
360: } else {
361: $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n");
362: }
363: $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
364: $r->print("<th>$lt{'pa'}</th>");
365: $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
366: } else {
1.66 bisitz 367: my $errtxt = '';
368: if ($env{'form.recover'}) {
369: $errtxt = 'There are no resources to recover.';
370: } else {
371: $errtxt = 'There are no resources to import.';
372: }
373: $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
1.65 bisitz 374: }
1.22 albertel 375: } else {
1.40 albertel 376: $r->print(&Apache::loncommon::start_page(undef,$js,
1.41 banghart 377: {'only_body' => 1}));
1.66 bisitz 378: # $r->print('<h1>'.&mt($title).'</h1>');
1.22 albertel 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="" />
1.31 albertel 385: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22 albertel 386: END
1.54 albertel 387: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
388:
1.22 albertel 389: }
1.57 albertel 390: foreach my $resource (@resources) {
1.1 harris41 391: $ctr++;
1.57 albertel 392: my $iconname=&Apache::loncommon::icon($resource->{'url'});
1.68.6.2! raeburn 393: if (($clen > 1) || ($env{'form.readfile'})) {
1.62 bisitz 394: $r->print(&Apache::loncommon::start_data_table_row()
395: ."<td>");
1.68.6.2! raeburn 396: if (($env{'form.readfile'})) {
1.37 www 397: $r->print(&checkbox($ctr-1));
398: } else {
399: $r->print(&movers($clen,$ctr));
400: }
1.22 albertel 401: }
1.58 albertel 402: $r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
403: $resource->{'id'}));
1.68.6.2! raeburn 404: if (($clen > 1) || ($env{'form.readfile'})) {
1.37 www 405: $r->print("</td>");
1.68.6.2! raeburn 406: unless (($env{'form.readfile'})) {
1.62 bisitz 407: $r->print("<td>".
1.37 www 408: &select_box($clen,$ctr).
409: "</td>");
410: }
1.62 bisitz 411: $r->print("<td>");
1.26 albertel 412: $r->print("<img src='$iconname' />");
1.62 bisitz 413: $r->print("</td><td>");
414: $r->print($resource->{'title'}.$resource->{'notes'}."</td><td>\n");
415: $r->print($resource->{'url'}."</td>"
416: .&Apache::loncommon::end_data_table_row()
417: ."\n");
1.22 albertel 418: }
419: }
1.68.6.2! raeburn 420: if (($clen > 1) || ($env{'form.readfile'})) {
1.65 bisitz 421: if ($clen > 0) {
422: $r->print(&Apache::loncommon::end_data_table());
423: }
424: $r->print('</form>');
1.22 albertel 425: } else {
426: $r->print(<<END);
427: <script type="text/javascript">
428: finish_import();
429: </script>
430: END
431: }
1.40 albertel 432:
433: $r->print(&Apache::loncommon::end_page());
1.22 albertel 434:
1.1 harris41 435: return OK;
436: }
437:
1.2 harris41 438: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 439: sub hidden {
1.58 albertel 440: my ($sel,$title,$filelink,$id) = @_;
1.61 albertel 441: my $string = '<input type="hidden" name="title'.$sel.'" value="'.
442: &escape($title).'" />';
1.48 albertel 443: $filelink=~s|^/ext/|http://|;
1.2 harris41 444: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
1.61 albertel 445: &escape($filelink).'" />';
446: $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
1.1 harris41 447: return $string;
448: }
449:
1.2 harris41 450: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 451: sub movers {
1.2 harris41 452: my ($total,$sel) = @_;
453: my $dsel = $sel-1;
454: my $usel = $sel+1;
455: $usel = 1 if $usel > $total;
456: $dsel = $total if $dsel < 1;
1.1 harris41 457: my $string;
1.2 harris41 458: $string = (<<END);
1.1 harris41 459: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 460: <tr><td><a href='javascript:move($sel,$dsel)'>
461: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
462: <tr><td><a href='javascript:move($sel,$usel)'>
463: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 464: </table>
465: END
466: return $string;
467: }
1.2 harris41 468:
469: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 470: sub select_box {
1.2 harris41 471: my ($total,$sel) = @_;
1.1 harris41 472: my $string;
1.2 harris41 473: $string = '<select name="alt'.$sel.'"';
474: $string .= " onChange='selectchange($sel)'>";
1.36 www 475: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1 harris41 476: for my $cur (1..$total) {
1.2 harris41 477: $string .= "<option name='o$cur' value='$cur'";
478: if ($cur == $sel) {
479: $string .= "selected";
1.1 harris41 480: }
1.2 harris41 481: $string .= ">$cur</option>";
1.1 harris41 482: }
1.2 harris41 483: $string .= "</select>\n";
1.1 harris41 484: return $string;
485: }
486:
1.37 www 487: # ------------------------------------------------------------------- Checkbox
488:
489: sub checkbox {
490: my $sel=shift;
491: return "<label><input type='checkbox' name='include$sel'".
492: ($env{"form.include$sel"}?' checked="checked"':'').
493: ' />'.&mt('Include').'</label>';
494: }
495:
1.1 harris41 496: 1;
497:
498: __END__
1.63 jms 499:
500: =pod
501:
502: =head1 NAME
503:
504: Apache::groupsort.pm
505:
506: =head1 SYNOPSIS
507:
508: Implements a second phase of importing
509: multiple resources into the RAT. Allows for
510: reordering the sequence of resources
511:
512: This is part of the LearningOnline Network with CAPA project
513: described at http://www.lon-capa.org.
514:
515:
516: =head1 NOTABLE SUBROUTINES
517:
518: =over
519:
520: =item
521:
522: =back
523:
524: =cut
525:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>