Annotation of loncom/interface/groupsort.pm, revision 1.71
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.71 ! raeburn 5: # $Id: groupsort.pm,v 1.70 2013/01/08 03:46:07 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.69 raeburn 40: use LONCAPA qw(:DEFAULT :match);
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);
1.71 ! raeburn 155: my ($token,$donechk,$allmaps);
! 156: $allmaps = {};
1.34 www 157: while ($token = $parser->get_token) {
158: if ($token->[0] eq 'S') {
159: if ($token->[1] eq 'resource') {
160: if ($env{'form.recover'}) {
161: if ($token->[2]->{'type'} ne 'zombie') { next; }
1.70 raeburn 162: if ($token->[2]->{'src'} =~ /\.(page|sequence)$/) {
1.71 ! raeburn 163: if (($env{'request.course.id'}) &&
! 164: ($env{'form.readfile'} =~ m{/default(|_\d+)\.(page|sequence)$})) {
! 165: unless ($donechk) {
! 166: $allmaps = &Apache::loncommon::allmaps_incourse();
! 167: $donechk = 1;
! 168: }
1.70 raeburn 169: }
1.71 ! raeburn 170: if ($allmaps->{$token->[2]->{'src'}}) { next; }
1.70 raeburn 171: }
1.34 www 172: } else {
173: if ($token->[2]->{'type'} eq 'zombie') { next; }
174: }
1.35 www 175:
1.34 www 176: my $name=$token->[2]->{'title'};
1.50 albertel 177: $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
1.57 albertel 178: my $note;
1.34 www 179: if ($1) {
1.57 albertel 180: $note = '<br />'.&mt('Removed by ').
1.34 www 181: &Apache::loncommon::plainname($2,$3).', '.
182: &Apache::lonlocal::locallocaltime($1);
183: }
1.37 www 184: $name=~s/\&colon\;/\:/g;
1.58 albertel 185: push(@{$resources}, {'url' => $token->[2]->{'src'},
1.57 albertel 186: 'title' => $name,
1.58 albertel 187: 'note' => $note,
188: 'id' => $token->[2]->{'id'},});
1.34 www 189: }
190: }
191: }
192: }
193: }
194:
1.33 www 195: # ---------------------------------------------------------------- Main Handler
196: sub handler {
197: my $r = shift;
198:
199: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.67 wenzelju 200: ['acts','mode','readfile','recover']);
1.33 www 201:
202: &Apache::loncommon::content_type($r,'text/html');
203: $r->send_http_header;
204: return OK if $r->header_only;
205:
206: # finish_import looks different for graphical or "simple" RAT
207: my $finishimport='';
1.37 www 208: my $begincondition='';
209: my $endcondition='';
1.67 wenzelju 210: if (($env{'form.readfile'})) {
1.37 www 211: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
212: $endcondition='}';
213: }
1.33 www 214: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
215: $finishimport=(<<ENDSMP);
216: function finish_import() {
217: opener.document.forms.simpleedit.importdetail.value='';
218: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 219: $begincondition
1.33 www 220: opener.document.forms.simpleedit.importdetail.value+='&'+
1.61 albertel 221: eval("document.forms.groupsort.title"+num+".value")+'='+
222: eval("document.forms.groupsort.filelink"+num+".value")+'='+
223: eval("document.forms.groupsort.id"+num+".value");
1.37 www 224: $endcondition
1.33 www 225: }
226: opener.document.forms.simpleedit.submit();
227: self.close();
228: }
229: ENDSMP
230: } else {
231: $finishimport=(<<ENDADV);
232: function finish_import() {
233: var linkflag=false;
234: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37 www 235: $begincondition
1.33 www 236: insertRowInLastRow();
237: placeResourceInLastRow(
238: eval("document.forms.groupsort.title"+num+".value"),
239: eval("document.forms.groupsort.filelink"+num+".value"),
1.59 albertel 240: eval("document.forms.groupsort.id"+num+".value"),
1.33 www 241: linkflag
242: );
243: linkflag=true;
1.37 www 244: $endcondition
1.33 www 245: }
246: opener.editmode=0;
247: opener.notclear=0;
248: opener.linkmode=0;
249: opener.draw();
250: self.close();
251: }
252: ENDADV
253: }
254:
255: # output start of web page
1.40 albertel 256: my $js = <<END;
257: <script type="text/javascript">
1.33 www 258: function insertRowInLastRow() {
259: opener.insertrow(opener.maxrow);
260: opener.addobj(opener.maxrow,'e&2');
261: }
1.59 albertel 262: function placeResourceInLastRow (title,url,id,linkflag) {
1.33 www 263: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
1.59 albertel 264: opener.escape(url),'false','normal',id);
1.33 www 265: opener.save();
266: if (linkflag) {
267: opener.joinres(opener.linkmode,opener.mostrecent,0);
268: }
269: opener.linkmode=opener.mostrecent;
270: }
271: $finishimport
272: function selectchange(val) {
273: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
274: orderchange(val,newval);
275: }
276: function move(val,newval) {
277: orderchange(val,newval);
278: }
279: function orderchange(val,newval) {
280: document.forms.groupsort.oldval.value=val;
281: document.forms.groupsort.newval.value=newval;
282: document.forms.groupsort.submit();
283: }
284: </script>
285: END
286: # read pertinent machine configuration
287: my $domain = $r->dir_config('lonDefDomain');
288: $iconpath = $r->dir_config('lonIconsURL') . "/";
289:
1.57 albertel 290: my @resources;
1.33 www 291:
1.34 www 292: if ($env{'form.readfile'}) {
1.57 albertel 293: &readfromfile($r,\@resources);
1.34 www 294: } else {
1.57 albertel 295: &readfromdb($r,\@resources);
1.34 www 296: }
1.33 www 297:
1.2 harris41 298: my $ctr = 0;
1.57 albertel 299: my $clen = scalar(@resources);
1.66 bisitz 300: my $title = '';
301: if ($env{'form.recover'}) {
302: $title = 'Recover Removed Resources';
303: } else {
304: $title = 'Sort Imported Resources';
305: }
1.67 wenzelju 306: if (($clen > 1) || ($env{'form.readfile'})) {
1.23 www 307: my %lt=&Apache::lonlocal::texthash(
308: 'fin'=> 'Finalize order of resources',
1.42 www 309: 'ci' => 'Continue Import',
310: 'cs' => 'Continue Search',
1.23 www 311: 'fi' => 'Finish Import',
1.51 albertel 312: 're' => 'Recover Checked',
1.64 bisitz 313: 'ip' => 'Import Checked',
1.23 www 314: 'ca' => 'Cancel',
315: 'co' => 'Change Order',
316: 'ti' => 'Title',
1.37 www 317: 'pa' => 'Path',
318: 'in' => 'Include'
1.23 www 319: );
1.64 bisitz 320:
1.56 albertel 321: $r->print(&Apache::loncommon::start_page($title, $js));
1.66 bisitz 322: $r->print('<h1>'.&mt($title).'</h1>');
1.56 albertel 323:
1.22 albertel 324: $r->print(<<END);
1.14 matthew 325: <form method='post' action='/adm/groupsort' name='groupsort'
326: enctype='application/x-www-form-urlencoded'>
1.1 harris41 327: <input type="hidden" name="fnum" value="$clen" />
328: <input type="hidden" name="oldval" value="" />
329: <input type="hidden" name="newval" value="" />
1.31 albertel 330: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36 www 331: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
332: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3 harris41 333: END
1.11 www 334:
1.54 albertel 335: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
1.51 albertel 336: # ---
1.65 bisitz 337:
338: my $buttontext = $lt{'re'};
1.67 wenzelju 339: if ($env{'form.recover'}) {
1.51 albertel 340: $r->print(<<END);
1.65 bisitz 341: <input type="button" name="alter" value="$buttontext"
1.51 albertel 342: onClick="finish_import()" />
343: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
344: END
345: } else {
1.42 www 346: # --- Continue Buttons
1.51 albertel 347: my $resurl =
348: &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
349: $r->print(<<END);
1.62 bisitz 350: <h2>$lt{'fin'}</h2>
351: <div>
1.42 www 352: <input type="button" name="alter" value="$lt{'ci'}"
1.54 albertel 353: onClick="window.location='$resurl?inhibitmenu=yes&catalogmode=import'" />
1.42 www 354: <input type="button" name="altersearch" value="$lt{'cs'}"
1.54 albertel 355: onClick="window.location='/adm/searchcat?inhibitmenu=yes&catalogmode=import'" />
1.23 www 356: <input type="button" name="alter" value="$lt{'fi'}"
1.2 harris41 357: onClick="finish_import()" />
1.23 www 358: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.62 bisitz 359: </div>
360: <br />
1.1 harris41 361: END
1.51 albertel 362: }
1.65 bisitz 363:
364: # Only display header if content exists
365: if ($clen > 0) {
366: $r->print(&Apache::loncommon::start_data_table()
367: .&Apache::loncommon::start_data_table_header_row());
1.67 wenzelju 368: if (($env{'form.readfile'})) {
1.65 bisitz 369: $r->print("<th>$lt{'in'}</th>\n");
370: } else {
371: $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n");
372: }
373: $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
374: $r->print("<th>$lt{'pa'}</th>");
375: $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
376: } else {
1.66 bisitz 377: my $errtxt = '';
378: if ($env{'form.recover'}) {
379: $errtxt = 'There are no resources to recover.';
380: } else {
381: $errtxt = 'There are no resources to import.';
382: }
383: $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
1.65 bisitz 384: }
1.22 albertel 385: } else {
1.40 albertel 386: $r->print(&Apache::loncommon::start_page(undef,$js,
1.41 banghart 387: {'only_body' => 1}));
1.66 bisitz 388: # $r->print('<h1>'.&mt($title).'</h1>');
1.22 albertel 389: $r->print(<<END);
390: <form method='post' action='/adm/groupsort' name='groupsort'
391: enctype='application/x-www-form-urlencoded'>
392: <input type="hidden" name="fnum" value="$clen" />
393: <input type="hidden" name="oldval" value="" />
394: <input type="hidden" name="newval" value="" />
1.31 albertel 395: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22 albertel 396: END
1.54 albertel 397: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
398:
1.22 albertel 399: }
1.57 albertel 400: foreach my $resource (@resources) {
1.1 harris41 401: $ctr++;
1.57 albertel 402: my $iconname=&Apache::loncommon::icon($resource->{'url'});
1.67 wenzelju 403: if (($clen > 1) || ($env{'form.readfile'})) {
1.62 bisitz 404: $r->print(&Apache::loncommon::start_data_table_row()
405: ."<td>");
1.67 wenzelju 406: if (($env{'form.readfile'})) {
1.37 www 407: $r->print(&checkbox($ctr-1));
408: } else {
409: $r->print(&movers($clen,$ctr));
410: }
1.22 albertel 411: }
1.58 albertel 412: $r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
413: $resource->{'id'}));
1.67 wenzelju 414: if (($clen > 1) || ($env{'form.readfile'})) {
1.37 www 415: $r->print("</td>");
1.67 wenzelju 416: unless (($env{'form.readfile'})) {
1.62 bisitz 417: $r->print("<td>".
1.37 www 418: &select_box($clen,$ctr).
419: "</td>");
420: }
1.62 bisitz 421: $r->print("<td>");
1.26 albertel 422: $r->print("<img src='$iconname' />");
1.62 bisitz 423: $r->print("</td><td>");
1.69 raeburn 424: if (($env{'form.recover'}) &&
425: ($resource->{'url'} =~ m{/uploaded/$match_domain/$match_courseid/supplemental/})) {
426: my $title = &Apache::loncommon::parse_supplemental_title($resource->{'title'});
427: $r->print($title);
428: } else {
429: $r->print($resource->{'title'});
430: }
431: $r->print($resource->{'notes'}."</td><td>\n");
1.62 bisitz 432: $r->print($resource->{'url'}."</td>"
433: .&Apache::loncommon::end_data_table_row()
434: ."\n");
1.22 albertel 435: }
436: }
1.67 wenzelju 437: if (($clen > 1) || ($env{'form.readfile'})) {
1.65 bisitz 438: if ($clen > 0) {
439: $r->print(&Apache::loncommon::end_data_table());
440: }
441: $r->print('</form>');
1.22 albertel 442: } else {
443: $r->print(<<END);
444: <script type="text/javascript">
445: finish_import();
446: </script>
447: END
448: }
1.40 albertel 449:
450: $r->print(&Apache::loncommon::end_page());
1.22 albertel 451:
1.1 harris41 452: return OK;
453: }
454:
1.2 harris41 455: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 456: sub hidden {
1.58 albertel 457: my ($sel,$title,$filelink,$id) = @_;
1.61 albertel 458: my $string = '<input type="hidden" name="title'.$sel.'" value="'.
459: &escape($title).'" />';
1.48 albertel 460: $filelink=~s|^/ext/|http://|;
1.2 harris41 461: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
1.61 albertel 462: &escape($filelink).'" />';
463: $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
1.1 harris41 464: return $string;
465: }
466:
1.2 harris41 467: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 468: sub movers {
1.2 harris41 469: my ($total,$sel) = @_;
470: my $dsel = $sel-1;
471: my $usel = $sel+1;
472: $usel = 1 if $usel > $total;
473: $dsel = $total if $dsel < 1;
1.1 harris41 474: my $string;
1.2 harris41 475: $string = (<<END);
1.1 harris41 476: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 477: <tr><td><a href='javascript:move($sel,$dsel)'>
478: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
479: <tr><td><a href='javascript:move($sel,$usel)'>
480: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 481: </table>
482: END
483: return $string;
484: }
1.2 harris41 485:
486: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 487: sub select_box {
1.2 harris41 488: my ($total,$sel) = @_;
1.1 harris41 489: my $string;
1.2 harris41 490: $string = '<select name="alt'.$sel.'"';
491: $string .= " onChange='selectchange($sel)'>";
1.36 www 492: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1 harris41 493: for my $cur (1..$total) {
1.2 harris41 494: $string .= "<option name='o$cur' value='$cur'";
495: if ($cur == $sel) {
496: $string .= "selected";
1.1 harris41 497: }
1.2 harris41 498: $string .= ">$cur</option>";
1.1 harris41 499: }
1.2 harris41 500: $string .= "</select>\n";
1.1 harris41 501: return $string;
502: }
503:
1.37 www 504: # ------------------------------------------------------------------- Checkbox
505:
506: sub checkbox {
507: my $sel=shift;
508: return "<label><input type='checkbox' name='include$sel'".
509: ($env{"form.include$sel"}?' checked="checked"':'').
510: ' />'.&mt('Include').'</label>';
511: }
512:
1.1 harris41 513: 1;
514:
515: __END__
1.63 jms 516:
517: =pod
518:
519: =head1 NAME
520:
521: Apache::groupsort.pm
522:
523: =head1 SYNOPSIS
524:
525: Implements a second phase of importing
526: multiple resources into the RAT. Allows for
527: reordering the sequence of resources
528:
529: This is part of the LearningOnline Network with CAPA project
530: described at http://www.lon-capa.org.
531:
532:
533: =head1 NOTABLE SUBROUTINES
534:
535: =over
536:
537: =item
538:
539: =back
540:
541: =cut
542:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>