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.76 2017/05/30 15:10:57 raeburn 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 :http);
36: use GDBM_File;
37: use Apache::loncommon;
38: use Apache::lonlocal;
39: use Apache::lonnet;
40: use LONCAPA qw(:DEFAULT :match);
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 update_actions_hash {
47: my ($hash) = @_;
48: # be careful in here, there is also a global %hash
49: my $acts=$env{'form.acts'};
50: my @Acts=split(/b/,$acts);
51: my %ahash;
52: my %achash;
53: # some initial hashes for working with data
54: my $ac=0;
55: foreach (@Acts) {
56: my ($state,$ref)=split(/a/);
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') {
64: $hash->{'store_'.$hash->{'pre_'.$key.'_link'}}=
65: $hash->{'pre_'.$key.'_title'};
66: $hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}}=
67: $hash->{'storectr'}+0;
68: $hash->{'storectr'}++;
69: }
70: if ($ahash{$key} eq '0') {
71: if ($hash->{'store_'.$hash->{'pre_'.$key.'_link'}}) {
72: delete($hash->{'store_'.$hash->{'pre_'.$key.'_link'}});
73: delete($hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}});
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:
86: sub readfromdb {
87: my ($r,$resources)=@_;
88:
89: my $diropendb = LONCAPA::tempdir() .
90: "$env{'user.domain'}_$env{'user.name'}_sel_res.db";
91:
92: # ----------------------------- diropendb is now the filename of the db to open
93: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
94: &update_actions_hash(\%hash);
95:
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});
108: }
109:
110: if ($env{'form.oldval'}) {
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);
119: }
120: }
121: # store out new order
122: foreach my $which (0..$#$resources) {
123: my $url = $resources->[$which]{'url'};
124: $hash{'storectr_'.$url} = $which;
125: }
126: } else {
127: $r->print('Unable to tie hash to db file');
128: }
129: untie(%hash);
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: }
141: return OK;
142: }
143:
144: # -------------------------------------------------------------- Read from file
145:
146: sub readfromfile {
147: my ($r,$resources)=@_;
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,$donechk,$allmaps);
156: $allmaps = {};
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; }
162: if ($token->[2]->{'src'} =~ /\.(page|sequence)$/) {
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: }
169: }
170: if ($allmaps->{$token->[2]->{'src'}}) { next; }
171: }
172: } else {
173: if ($token->[2]->{'type'} eq 'zombie') { next; }
174: }
175:
176: my $name=$token->[2]->{'title'};
177: $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
178: my $note;
179: if ($1) {
180: $note = '<br />'.&mt('Removed by ').
181: &Apache::loncommon::plainname($2,$3).', '.
182: &Apache::lonlocal::locallocaltime($1);
183: }
184: $name=~s/\&colon\;/\:/g;
185: push(@{$resources}, {'url' => $token->[2]->{'src'},
186: 'title' => $name,
187: 'note' => $note,
188: 'id' => $token->[2]->{'id'},});
189: }
190: }
191: }
192: }
193: }
194:
195: # ---------------------------------------------------------------- Main Handler
196: sub handler {
197: my $r = shift;
198:
199: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
200: ['acts','mode','readfile','recover']);
201:
202: &Apache::loncommon::content_type($r,'text/html');
203: $r->send_http_header;
204: return OK if $r->header_only;
205:
206: # permissions checking
207: my ($allowed,$canedit,$context,$cid);
208: if ($env{'form.readfile'} eq '') {
209: $allowed = 1;
210: } elsif ($env{'form.readfile'} =~ m{^/uploaded/($match_domain)/($match_courseid)/}) {
211: my ($cdom,$cnum) = ($1,$2);
212: $cid = $cdom.'_'.$cnum;
213: $context = 'course';
214: if ((&Apache::lonnet::allowed('mdc',$cid)) ||
215: (&Apache::lonnet::allowed('cev',$cid))) {
216: $allowed = 1;
217: }
218: } elsif ($env{'form.readfile'} =~ m{^/res/}) {
219: $context = 'res';
220: if ((&Apache::lonnet::allowed('bre',$env{'form.readfile'})) ||
221: (&Apache::lonnet::allowed('bro',$env{'form.readfile'}))) {
222: $allowed = 1;
223: }
224: }
225: if ($allowed) {
226: if ($env{'form.mode'} eq 'rat') {
227: if (&Apache::lonnet::allowed('are',$env{'request.role.domain'})) {
228: $canedit = 1;
229: }
230: } elsif (($env{'form.mode'} eq 'simple') || ($env{'form.mode'} eq '')) {
231: if ($context eq 'course') {
232: if (&Apache::lonnet::allowed('mdc',$cid)) {
233: $canedit = 1;
234: }
235: } elsif (($env{'request.course.id'}) &&
236: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
237: $canedit = 1;
238: } elsif (&Apache::lonnet::allowed('are',$env{'request.role.domain'})) {
239: $canedit = 1;
240: }
241: }
242: }
243:
244: unless ($allowed) {
245: if ($context eq 'course') {
246: if ($env{'request.course.id'} eq $cid) {
247: $env{'user.error.msg'}=
248: "/adm/groupsort::0:1:Course environment gone, reinitialize the course";
249: } else {
250: $env{'user.error.msg'}=
251: "/adm/groupsort:bre:0:0:Cannot view folder contents";
252: }
253: } else {
254: $env{'user.error.msg'}=
255: "/adm/groupsort:bre:0:0:Cannot view map contents";
256: }
257: return HTTP_NOT_ACCEPTABLE;
258: }
259:
260: # finish_import looks different for graphical or "simple" RAT
261: my $finishimport='';
262: my $begincondition='';
263: my $endcondition='';
264: my $noedit;
265: unless ($canedit) {
266: if ($context eq 'course') {
267: $noedit = &js_escape(&mt('You do not have rights to edit the course.'));
268: } else {
269: $noedit = &js_escape(&mt('You do not have rights to edit map contents.'));
270: }
271: }
272: if (($env{'form.readfile'})) {
273: $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
274: $endcondition='}';
275: }
276: if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
277: if ($canedit) {
278: $finishimport=(<<ENDSMP);
279: function finish_import() {
280: opener.document.forms.simpleedit.importdetail.value='';
281: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
282: $begincondition
283: opener.document.forms.simpleedit.importdetail.value+='&'+
284: eval("document.forms.groupsort.title"+num+".value")+'='+
285: eval("document.forms.groupsort.filelink"+num+".value")+'='+
286: eval("document.forms.groupsort.id"+num+".value");
287: $endcondition
288: }
289: opener.document.forms.simpleedit.submit();
290: self.close();
291: }
292: ENDSMP
293: } else {
294: $finishimport=(<<ENDNO);
295: function finish_import() {
296: alert('$noedit');
297: }
298: ENDNO
299: }
300: } else {
301: if ($canedit) {
302: $finishimport=(<<ENDADV);
303: function finish_import() {
304: var linkflag=false;
305: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
306: $begincondition
307: insertRowInLastRow();
308: placeResourceInLastRow(
309: eval("document.forms.groupsort.title"+num+".value"),
310: eval("document.forms.groupsort.filelink"+num+".value"),
311: eval("document.forms.groupsort.id"+num+".value"),
312: linkflag
313: );
314: linkflag=true;
315: $endcondition
316: }
317: opener.editmode=0;
318: opener.notclear=0;
319: opener.linkmode=0;
320: opener.draw();
321: self.close();
322: }
323: ENDADV
324: } else {
325: $finishimport=(<<ENDNONE);
326: function finish_import() {
327: alert('$noedit');
328: }
329: ENDNONE
330: }
331: }
332:
333: # output start of web page
334: my $js = <<END;
335: <script type="text/javascript">
336: function insertRowInLastRow() {
337: opener.insertrow(opener.maxrow);
338: opener.addobj(opener.maxrow,'e&2');
339: }
340: function placeResourceInLastRow (title,url,id,linkflag) {
341: opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
342: opener.escape(url),'false','normal',id);
343: opener.save();
344: if (linkflag) {
345: opener.joinres(opener.linkmode,opener.mostrecent,0);
346: }
347: opener.linkmode=opener.mostrecent;
348: }
349: $finishimport
350: function selectchange(val) {
351: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
352: orderchange(val,newval);
353: }
354: function move(val,newval) {
355: orderchange(val,newval);
356: }
357: function orderchange(val,newval) {
358: document.forms.groupsort.oldval.value=val;
359: document.forms.groupsort.newval.value=newval;
360: document.forms.groupsort.submit();
361: }
362: </script>
363: END
364: # read pertinent machine configuration
365: my $domain = $r->dir_config('lonDefDomain');
366: $iconpath = $r->dir_config('lonIconsURL') . "/";
367:
368: my @resources;
369:
370: if ($env{'form.readfile'}) {
371: &readfromfile($r,\@resources);
372: } else {
373: &readfromdb($r,\@resources);
374: }
375:
376: my $ctr = 0;
377: my $clen = scalar(@resources);
378: my $title = '';
379: if ($env{'form.recover'}) {
380: $title = 'Recover Removed Resources';
381: } else {
382: $title = 'Sort Imported Resources';
383: }
384: my $disabled;
385: unless ($canedit) {
386: $disabled = ' disabled="disabled"';
387: }
388: if (($clen > 1) || ($env{'form.readfile'})) {
389: my %lt=&Apache::lonlocal::texthash(
390: 'fin'=> 'Finalize order of resources',
391: 'ci' => 'Continue Import',
392: 'cs' => 'Continue Search',
393: 'fi' => 'Finish Import',
394: 're' => 'Recover Checked',
395: 'ip' => 'Import Checked',
396: 'ca' => 'Cancel',
397: 'co' => 'Change Order',
398: 'ti' => 'Title',
399: 'pa' => 'Path',
400: 'in' => 'Include'
401: );
402:
403: $r->print(&Apache::loncommon::start_page($title, $js));
404: $r->print('<h1>'.&mt($title).'</h1>');
405:
406: $r->print(<<END);
407: <form method='post' action='/adm/groupsort' name='groupsort'
408: enctype='application/x-www-form-urlencoded'>
409: <input type="hidden" name="fnum" value="$clen" />
410: <input type="hidden" name="oldval" value="" />
411: <input type="hidden" name="newval" value="" />
412: <input type="hidden" name="mode" value="$env{'form.mode'}" />
413: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
414: <input type="hidden" name="recover" value="$env{'form.recover'}" />
415: END
416:
417: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
418: # ---
419:
420: my $buttontext = $lt{'re'};
421: if ($env{'form.recover'}) {
422: $r->print(<<END);
423: <input type="button" name="alter" value="$buttontext"
424: onclick="finish_import()"$disabled />
425: <input type="button" name="alter" value="$lt{'ca'}" onclick="self.close()" />
426: END
427: } else {
428: # --- Continue Buttons
429: my $resurl =
430: &Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
431: $r->print(<<END);
432: <h2>$lt{'fin'}</h2>
433: <div>
434: <input type="button" name="alter" value="$lt{'ci'}"
435: onclick="window.location='$resurl?inhibitmenu=yes&catalogmode=import'" />
436: <input type="button" name="altersearch" value="$lt{'cs'}"
437: onclick="window.location='/adm/searchcat?inhibitmenu=yes&catalogmode=import'" />
438: <input type="button" name="alter" value="$lt{'fi'}"
439: onclick="finish_import()"$disabled />
440: <input type="button" name="alter" value="$lt{'ca'}" onclick="self.close()" />
441: </div>
442: <br />
443: END
444: }
445:
446: # Only display header if content exists
447: if ($clen > 0) {
448: $r->print(&Apache::loncommon::start_data_table()
449: .&Apache::loncommon::start_data_table_header_row());
450: if (($env{'form.readfile'})) {
451: $r->print("<th>$lt{'in'}</th>\n");
452: } else {
453: $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n");
454: }
455: $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
456: $r->print("<th>$lt{'pa'}</th>");
457: $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
458: } else {
459: my $errtxt = '';
460: if ($env{'form.recover'}) {
461: $errtxt = 'There are no resources to recover.';
462: } else {
463: $errtxt = 'There are no resources to import.';
464: }
465: $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
466: }
467: } else {
468: $r->print(&Apache::loncommon::start_page(undef,$js,
469: {'only_body' => 1}));
470: # $r->print('<h1>'.&mt($title).'</h1>');
471: $r->print(<<END);
472: <form method='post' action='/adm/groupsort' name='groupsort'
473: enctype='application/x-www-form-urlencoded'>
474: <input type="hidden" name="fnum" value="$clen" />
475: <input type="hidden" name="oldval" value="" />
476: <input type="hidden" name="newval" value="" />
477: <input type="hidden" name="mode" value="$env{'form.mode'}" />
478: END
479: $r->print(&Apache::loncommon::inhibit_menu_check('input'));
480:
481: }
482: foreach my $resource (@resources) {
483: $ctr++;
484: my $iconname=&Apache::loncommon::icon($resource->{'url'});
485: if (($clen > 1) || ($env{'form.readfile'})) {
486: $r->print(&Apache::loncommon::start_data_table_row()
487: ."<td>");
488: if (($env{'form.readfile'})) {
489: $r->print(&checkbox($ctr-1,$disabled));
490: } else {
491: $r->print(&movers($clen,$ctr));
492: }
493: }
494: $r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
495: $resource->{'id'}));
496: if (($clen > 1) || ($env{'form.readfile'})) {
497: $r->print("</td>");
498: unless (($env{'form.readfile'})) {
499: $r->print("<td>".
500: &select_box($clen,$ctr,$disabled).
501: "</td>");
502: }
503: $r->print("<td>");
504: $r->print("<img src='$iconname' />");
505: $r->print("</td><td>");
506: if (($env{'form.recover'}) &&
507: ($resource->{'url'} =~ m{/uploaded/$match_domain/$match_courseid/supplemental/})) {
508: my $title = &Apache::loncommon::parse_supplemental_title($resource->{'title'});
509: $r->print($title);
510: } else {
511: $r->print($resource->{'title'});
512: }
513: $r->print($resource->{'notes'}."</td><td>\n");
514: $r->print($resource->{'url'}."</td>"
515: .&Apache::loncommon::end_data_table_row()
516: ."\n");
517: }
518: }
519: if (($clen > 1) || ($env{'form.readfile'})) {
520: if ($clen > 0) {
521: $r->print(&Apache::loncommon::end_data_table());
522: }
523: $r->print('</form>');
524: } else {
525: $r->print(<<END);
526: <script type="text/javascript">
527: finish_import();
528: </script>
529: END
530: }
531:
532: $r->print(&Apache::loncommon::end_page());
533:
534: return OK;
535: }
536:
537: # --------------------------------------- Hidden values (returns scalar string)
538: sub hidden {
539: my ($sel,$title,$filelink,$id) = @_;
540: my $string = '<input type="hidden" name="title'.$sel.'" value="'.
541: &escape($title).'" />';
542: $filelink=~s|^/ext/|http://|;
543: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
544: &escape($filelink).'" />';
545: $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
546: return $string;
547: }
548:
549: # --------------------------------------- Moving arrows (returns scalar string)
550: sub movers {
551: my ($total,$sel) = @_;
552: my $dsel = $sel-1;
553: my $usel = $sel+1;
554: $usel = 1 if $usel > $total;
555: $dsel = $total if $dsel < 1;
556: my $string;
557: $string = (<<END);
558: <table border='0' cellspacing='0' cellpadding='0'>
559: <tr><td><a href='javascript:move($sel,$dsel)'>
560: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
561: <tr><td><a href='javascript:move($sel,$usel)'>
562: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
563: </table>
564: END
565: return $string;
566: }
567:
568: # ------------------------------------------ Select box (returns scalar string)
569: sub select_box {
570: my ($total,$sel,$disabled) = @_;
571: my $string;
572: $string = '<select name="alt'.$sel.'"';
573: $string .= " onchange='selectchange($sel)'.$disabled.'>";
574: $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
575: for my $cur (1..$total) {
576: $string .= "<option name='o$cur' value='$cur'";
577: if ($cur == $sel) {
578: $string .= "selected";
579: }
580: $string .= ">$cur</option>";
581: }
582: $string .= "</select>\n";
583: return $string;
584: }
585:
586: # ------------------------------------------------------------------- Checkbox
587:
588: sub checkbox {
589: my ($sel,$disabled) = @_;
590: return "<label><input type='checkbox' name='include$sel'".
591: ($env{"form.include$sel"}?' checked="checked"':'').
592: $disabled.' />'.&mt('Include').'</label>';
593: }
594:
595: 1;
596:
597: __END__
598:
599: =pod
600:
601: =head1 NAME
602:
603: Apache::groupsort.pm
604:
605: =head1 SYNOPSIS
606:
607: Implements a second phase of importing
608: multiple resources into the RAT. Allows for
609: reordering the sequence of resources
610:
611: This is part of the LearningOnline Network with CAPA project
612: described at http://www.lon-capa.org.
613:
614:
615: =head1 NOTABLE SUBROUTINES
616:
617: =over
618:
619: =item
620:
621: =back
622:
623: =cut
624:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>