1: # The LearningOnline Network with CAPA
2: # Edit Handler for RAT Maps
3: #
4: # $Id: lonratedt.pm,v 1.20 2002/05/16 21:10:05 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # (TeX Content Handler
29: #
30: # 05/29/00,05/30 Gerd Kortemeyer)
31: # 7/1,6/30 Gerd Kortemeyer
32:
33: package Apache::lonratedt;
34:
35: use strict;
36: use Apache::Constants qw(:common);
37: use Apache::lonnet;
38: use Apache::lonratsrv;
39:
40: my @order=();
41: my @resources=();
42:
43:
44: # Mapread read maps into global arrays @links and @resources, determines status
45: # sets @order - pointer to resources in right order
46: # sets @resources - array with the resources with correct idx
47: #
48: sub mapread {
49: my $fn=shift;
50:
51: my @links;
52: undef @links;
53: undef @resources;
54: undef @order;
55:
56: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
57: if ($errtext) { return ($errtext,2); }
58:
59: # -------------------------------------------------------------------- Read map
60: foreach (split(/\<\&\>/,$outtext)) {
61: my ($command,$number,$content)=split(/\<\:\>/,$_);
62: if ($command eq 'objcont') {
63: $resources[$number]=$content;
64: }
65: if ($command eq 'objlinks') {
66: $links[$number]=$content;
67: }
68: }
69: # ------------------------------------------------------- Is this a linear map?
70: my @starters=();
71: my @endings=();
72: undef @starters;
73: undef @endings;
74:
75: foreach (@links) {
76: if (defined($_)) {
77: my ($start,$end,$cond)=split(/\:/,$_);
78: if ((defined($starters[$start])) || (defined($endings[$end]))) {
79: return
80: ('Map has branchings. Use advanced editor.',1);
81: }
82: $starters[$start]=1;
83: $endings[$end]=1;
84: if ($cond) {
85: return
86: ('Map has conditions. Use advanced editor.',1);
87: }
88: }
89:
90: }
91: for (my $i=0; $i<=$#resources; $i++) {
92: if (defined($resources[$i])) {
93: unless (($starters[$i]) || ($endings[$i])) {
94: return
95: ('Map has unconnected resources. Use advanced editor.',1);
96: }
97: }
98: }
99:
100: # -------------------------------------------------- This is a linear map, sort
101:
102: my $startidx=0;
103: my $endidx=0;
104: for (my $i=0; $i<=$#resources; $i++) {
105: if (defined($resources[$i])) {
106: my ($title,$url,$ext,$type)=split(/\:/,$resources[$i]);
107: if ($type eq 'start') { $startidx=$i; }
108: if ($type eq 'finish') { $endidx=$i; }
109: }
110: }
111: my $k=0;
112: my $currentidx=$startidx;
113: $order[$k]=$currentidx;
114: for (my $i=0; $i<=$#resources; $i++) {
115: foreach (@links) {
116: my ($start,$end)=split(/\:/,$_);
117: if ($start==$currentidx) {
118: $currentidx=$end;
119: $k++;
120: $order[$k]=$currentidx;
121: last;
122: }
123: }
124: if ($currentidx==$endidx) { last; }
125: }
126: return $errtext;
127: }
128:
129: # ---------------------------------------------- Read a map as well as possible
130:
131: sub attemptread {
132: my $fn=shift;
133:
134: my @links;
135: undef @links;
136: my @theseres;
137: undef @theseres;
138:
139: my ($outtext,$errtext)=&Apache::lonratsrv::loadmap($fn,'');
140: if ($errtext) { return @theseres }
141:
142: # -------------------------------------------------------------------- Read map
143: foreach (split(/\<\&\>/,$outtext)) {
144: my ($command,$number,$content)=split(/\<\:\>/,$_);
145: if ($command eq 'objcont') {
146: $theseres[$number]=$content;
147: }
148: if ($command eq 'objlinks') {
149: $links[$number]=$content;
150: }
151: }
152:
153: # --------------------------------------------------------------- Sort, sort of
154:
155: my @objsort=();
156: undef @objsort;
157:
158: my @data1=();
159: my @data2=();
160: undef @data1;
161: undef @data2;
162:
163: my $k;
164: my $kj;
165: my $j;
166: my $ij;
167:
168: for ($k=1;$k<=$#theseres;$k++) {
169: if (defined($theseres[$k])) {
170: $objsort[$#objsort+1]=$k;
171: }
172: }
173:
174: for ($k=1;$k<=$#links;$k++) {
175: if (defined($links[$k])) {
176: @data1=split(/\:/,$links[$k]);
177: $kj=-1;
178: for (my $j=0;$j<=$#objsort;$j++) {
179: if ((split(/\:/,$objsort[$j]))[0]==$data1[0]) {
180: $kj=$j;
181: }
182: }
183: if ($kj!=-1) { $objsort[$kj].=':'.$data1[1]; }
184: }
185: }
186: for ($k=0;$k<=$#objsort;$k++) {
187: for ($j=0;$j<=$#objsort;$j++) {
188: if ($k!=$j) {
189: @data1=split(/\:/,$objsort[$k]);
190: @data2=split(/\:/,$objsort[$j]);
191: my $dol=$#data1+1;
192: my $dtl=$#data2+1;
193: if ($dol+$dtl<1000) {
194: for ($kj=1;$kj<$dol;$kj++) {
195: if ($data1[$kj]==$data2[0]) {
196: for ($ij=1;$ij<$dtl;$ij++) {
197: $data1[$#data1+1]=$data2[$ij];
198: }
199: }
200: }
201: for ($kj=1;$kj<$dtl;$kj++) {
202: if ($data2[$kj]==$data1[0]) {
203: for ($ij=1;$ij<$dol;$ij++) {
204: $data2[$#data2+1]=$data1[$ij];
205: }
206: }
207: }
208: $objsort[$k]=join(':',@data1);
209: $objsort[$j]=join(':',@data2);
210: }
211: }
212: }
213: }
214: # ---------------------------------------------------------------- Now sort out
215:
216: @objsort=sort {
217: my @data1=split(/\:/,$a);
218: my @data2=split(/\:/,$b);
219: my $rvalue=0;
220: my $k;
221: for ($k=1;$k<=$#data1;$k++) {
222: if ($data1[$k]==$data2[0]) { $rvalue--; }
223: }
224: for ($k=1;$k<=$#data2;$k++) {
225: if ($data2[$k]==$data1[0]) { $rvalue++; }
226: }
227: if ($rvalue==0) { $rvalue=$#data2-$#data1; }
228: $rvalue;
229: } @objsort;
230:
231: my @outres=();
232: undef @outres;
233:
234: for ($k=0;$k<=$#objsort;$k++) {
235: $outres[$k]=$theseres[(split(/\:/,$objsort[$k]))[0]];
236: }
237: return @outres;
238: }
239:
240: # --------------------------------------------------------- Build up RAT screen
241: sub ratedt {
242: my ($r,$url)=@_;
243: $r->print(<<ENDDOCUMENT);
244:
245: <html>
246: <head>
247: <script language="JavaScript">
248: var flag=0;
249: </script>
250: </head>
251: <frameset rows="1,50,*" border=0>
252: <frame name=server src="$url/loadonly/ratserver" noresize noscroll>
253: <frame name=code src="/adm/rat/code.html">
254: <frame name=mapout src="/adm/rat/map.html">
255: </frameset>
256: </html>
257:
258: ENDDOCUMENT
259: }
260:
261: # ---------------------------------------------------------------- Make buttons
262:
263: sub buttons {
264: my $adv=shift;
265: my $output='<form method=post>';
266: if ($adv==1) {
267: $output.='<input type=submit name=forceadv value="Edit">';
268: } else {
269: unless ($adv==2) {
270: $output.='<input type=submit name=forcesmp value="Simple Edit">';
271: }
272: $output.='<input type=submit name=forceadv value="Advanced Edit">';
273: }
274: return $output.'</form><hr>';
275: }
276:
277: # ----------------------------------------------------------- Paste into target
278: # modifies @order, @resources
279:
280: sub pastetarget {
281: my ($after,@which)=@_;
282: my @insertorder=();
283: foreach (@which) {
284: if (defined($_)) {
285: my ($name,$url)=split(/\=/,$_);
286: my $idx=$#resources+1;
287: $insertorder[$#insertorder+1]=$idx;
288: my $ext='false';
289: if ($url=~/^http\:\/\//) { $ext='true'; }
290: $resources[$idx]=$name.':'.$url.':normal:'.$ext.':res';
291: }
292: }
293: print "insertorder:".join(',',@insertorder).'<br>';
294: print "order:".join(',',@order).'<br>';
295: my @oldorder=splice(@order,$after,$#insertorder+1,@insertorder);
296: print "oldorder:".join(',',@oldorder).'<br>';
297: print "order:".join(',',@order).'<br>';
298: @order=push(@order,@oldorder);
299: print "order:".join(',',@order).'<br>';
300: }
301:
302: # ------------------------------------------------------- Simple edit processor
303:
304: sub smpedt {
305: my ($r,$errtext)=@_;
306: my $buttons=&buttons(2);
307:
308: # ---------------------------------------------------------- Process form input
309:
310: my @importselect=();
311: my @targetselect=();
312: undef @importselect;
313: undef @targetselect;
314: if (defined($ENV{'form.import'})) {
315: if (ref($ENV{'form.import'})) {
316: @importselect=sort($ENV->{'form.import'});
317: } else {
318: @importselect=($ENV{'form.import'});
319: }
320: }
321: if (defined($ENV{'form.target'})) {
322: if (ref($ENV{'form.target'})) {
323: @targetselect=sort($ENV->{'form.target'});
324: } else {
325: @targetselect=($ENV{'form.target'});
326: }
327: }
328: # ============================================================ Process commands
329:
330: my $targetdetail=$ENV{'form.targetdetail'};
331: my $importdetail=$ENV{'form.curimpdetail'};
332:
333: # ---------------------------------------------------- Importing from groupsort
334: if (($ENV{'form.importdetail'}) && (!$ENV{'form.impfortarget'})) {
335:
336: $importdetail='';
337: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
338:
339: my $lastsel;
340:
341: if (defined($importselect[-1])) {
342: $lastsel=$importselect[-1];
343: } else {
344: $lastsel=$#curimport;
345: }
346:
347: for (my $i=0;$i<=$lastsel;$i++) {
348: my ($name,$url)=split(/\=/,$curimport[$i]);
349: if ($url) {
350: $importdetail.='&'.$name.'='.$url;
351: }
352: }
353:
354: $importdetail.='&'.$ENV{'form.importdetail'};
355:
356: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
357: my ($name,$url)=split(/\=/,$curimport[$i]);
358: if ($url) {
359: $importdetail.='&'.$name.'='.$url;
360: }
361: }
362: $importdetail=~s/\&+/\&/g;
363: $importdetail=~s/^\&//;
364:
365: # ------------------------------------------------------------------- Clear all
366: } elsif ($ENV{'form.clear'}) {
367: $importdetail='';
368: # ------------------------------------------------------------ Discard selected
369: } elsif ($ENV{'form.discard'}) {
370: $importdetail='';
371: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
372: foreach (@importselect) {
373: $curimport[$_]='';
374: }
375: for (my $i=0;$i<=$#curimport;$i++) {
376: my ($name,$url)=split(/\=/,$curimport[$i]);
377: if ($url) {
378: $importdetail.='&'.$name.'='.$url;
379: }
380: }
381: # --------------------------------------------------------- Loading another map
382: } elsif ($ENV{'form.loadmap'}) {
383: $importdetail='';
384: my @curimport=split(/\&/,$ENV{'form.curimpdetail'});
385:
386: my $lastsel;
387:
388: if (defined($importselect[-1])) {
389: $lastsel=$importselect[-1];
390: } else {
391: $lastsel=$#curimport;
392: }
393:
394: for (my $i=0;$i<=$lastsel;$i++) {
395: my ($name,$url)=split(/\=/,$curimport[$i]);
396: if ($url) {
397: $importdetail.='&'.$name.'='.$url;
398: }
399: }
400:
401: foreach (
402: &attemptread(&Apache::lonnet::filelocation('',$ENV{'form.importmap'}))) {
403: my ($name,$url)=split(/\:/,$_);
404: if ($url) {
405: $importdetail.='&'.&Apache::lonnet::escape($name).'='.
406: &Apache::lonnet::escape($url);
407: }
408: }
409:
410: for (my $i=$lastsel+1;$i<=$#curimport;$i++) {
411: my ($name,$url)=split(/\=/,$curimport[$i]);
412: if ($url) {
413: $importdetail.='&'.$name.'='.$url;
414: }
415: }
416: $importdetail=~s/\&+/\&/g;
417: $importdetail=~s/^\&//;
418:
419: # ------------------------------------------------ Groupimport/search to target
420: } elsif ($ENV{'form.importdetail'}) {
421: my $lastsel;
422:
423: if (defined($targetselect[-1])) {
424: $lastsel=$targetselect[-1];
425: } else {
426: $lastsel=$#order;
427: }
428: &pastetarget($lastsel,split(/\&/,$ENV{'form.importdata'}));
429: # ------------------------------------------------------------------------- Cut
430: } elsif ($ENV{'form.cut'}) {
431: # ----------------------------------------------------------------------- Paste
432: } elsif ($ENV{'form.paste'}) {
433: # ------------------------------------------------
434: }
435: # ------------------------------------------------------------ Assemble windows
436:
437: my $idx=-1;
438: my $importwindow=join("\n",map {
439: $idx++;
440: if ($_) {
441: my ($name,$url)=split(/\=/,$_);
442: unless ($name) { $name=(split(/\//,$url))[-1]; }
443: unless ($name) { $name='EMPTY'; }
444: '<option value="'.$idx.'">'.&Apache::lonnet::unescape($name).
445: '</option>';
446: }
447: } split(/\&/,$importdetail));
448:
449: $idx=0;
450: my $targetwindow=join("\n",map {
451: my ($name,$url)=split(/\:/,$resources[$_]);
452: unless ($name) { $name=(split(/\//,$url))[-1]; }
453: unless ($name) { $name='EMPTY'; }
454: $targetdetail.='&'.&Apache::lonnet::escape($name).'='.
455: &Apache::lonnet::escape($url);
456: $idx++;
457: '<option value="'.$idx.'">'.$name.'</option>';
458: } @order);
459:
460: # ----------------------------------------------------- Start simple RAT screen
461: $r->print(<<ENDSMPHEAD);
462: <html>
463: <head>
464: <script>
465: var srch;
466: var srchflag=-1; // 1 means currently open
467: // 0 means closed (but has been open)
468: // -1 means never yet opened/defined
469: var srchmode='';
470:
471: var idx;
472: var idxflag=-1; // 1 means currently open
473: // 0 means closed (but has been open)
474: // -1 means never yet opened/defined
475: var idxmode='';
476:
477: // ------------------------------------------------------ Clears indexer window
478: function idxclear() {
479: idx.document.clear();
480: }
481:
482: // ------------------------------------------------------- Clears search window
483: function srchclear() {
484: srch.document.clear();
485: }
486:
487: // ------------------------------------------------------ Closes indexer window
488: function idxclose() {
489: if (idx && !idx.closed) {
490: idxflag=0;
491: idx.close();
492: }
493: }
494:
495: // ------------------------------------------------------- Closes search window
496: function srchclose() {
497: if (srch && !srch.closed) {
498: srchflag=0;
499: srch.close();
500: }
501: }
502:
503: // -------------------------------------------------------- Open indexer window
504: function idxopen(mode) {
505: var options="scrollbars=1,resizable=1,menubar=0";
506: idxmode=mode;
507: idxflag=1;
508: idx=open("/res/?launch=1&mode=simple&catalogmode="+mode,"idxout",options);
509: idx.focus();
510: }
511:
512: // --------------------------------------------------------- Open search window
513: function srchopen(mode) {
514: var options="scrollbars=1,resizable=1,menubar=0";
515: srchmode=mode;
516: srchflag=1;
517: srch=open("/adm/searchcat?launch=1&mode=simple&catalogmode="+mode,"srchout",options);
518: srch.focus();
519: }
520: // ----------------------------------------------------- launch indexer browser
521: function groupsearch() {
522: srchcheck('groupsearch');
523: }
524:
525: function groupimport() {
526: idxcheck('groupimport');
527: }
528: // ------------------------------------------------------- Do srch status check
529: function srchcheck(mode) {
530: if (!srch || srch.closed || srchmode!=mode) {
531: srchopen(mode);
532: }
533: srch.focus();
534: }
535:
536: // -------------------------------------------------------- Do idx status check
537: function idxcheck(mode) {
538: if (!idx || idx.closed || idxmode!=mode) {
539: idxopen(mode);
540: }
541: idx.focus();
542: }
543:
544:
545: var editbrowser;
546: function openbrowser(formname,elementname,only,omit) {
547: var url = '/res/?';
548: if (editbrowser == null) {
549: url += 'launch=1&';
550: }
551: url += 'catalogmode=interactive&';
552: url += 'mode=edit&';
553: url += 'form=' + formname + '&';
554: if (only != null) {
555: url += 'only=' + only + '&';
556: }
557: if (omit != null) {
558: url += 'omit=' + omit + '&';
559: }
560: url += 'element=' + elementname + '';
561: var title = 'Browser';
562: var options = 'scrollbars=1,resizable=1,menubar=0';
563: options += ',width=700,height=600';
564: editbrowser = open(url,title,options,'1');
565: editbrowser.focus();
566: }
567:
568: function openview(entry) {
569: var url=unescape((entry.split('='))[1]);
570: var parts=new Array;
571: parts=url.split(':');
572: url=parts.join(':');
573: if (url) { open(url,'cat'); }
574: }
575:
576: function viewtarget() {
577: openview((document.forms.simpleedit.targetdetail.value.split('&'))
578: [document.forms.simpleedit.target.selectedIndex+1]);
579: }
580:
581: function viewimport() {
582: openview((document.forms.simpleedit.curimpdetail.value.split('&'))
583: [document.forms.simpleedit.import.selectedIndex+1]);
584: }
585:
586: </script>
587: </head>
588: <body bgcolor='#FFFFFF'>
589: $buttons
590: <font color=red>$errtext</font>
591: <form name=simpleedit method=post>
592: <input type=hidden name=forcesmp value=1>
593: <table>
594: <tr><th width="40%">Import</th>
595: <th> </th>
596: <th width="40%">Target</th></tr>
597: <tr><td bgcolor="#FFFFCC">
598: <input type=button onClick="javascript:groupsearch()" value="Group Search">
599: <input type=button onClick="javascript:groupimport();" value="Group Import">
600: after selected
601: <hr>
602: <input type=text size=20 name=importmap>
603: <input type=button
604: onClick="javascript:openbrowser('simpleedit','importmap','sequence,page','')"
605: value="Browse"><input type=submit name=loadmap value="Load Map"><hr>
606: <input type=submit name="discard" value="Discard Selected">
607: <input type=submit name="clear" value="Clear All">
608: <input type=button onClick="javascript:viewimport()" value="View">
609:
610: </td><td> </td><td bgcolor="#FFFFCC">
611:
612: <input type=button onClick=
613: "javascript:impfortarget.value=1;groupsearch()" value="Group Search">
614: <input type=button onClick=
615: "javascript:impfortarget.value=1;groupimport();" value="Group Import">
616: after selected
617: <hr><input type=button onClick="javascript:viewtarget()" value="View">
618: </td></tr>
619:
620: <tr><td bgcolor="#FFFFCC"><select name="import" multiple>
621: $importwindow
622: </select>
623: </td>
624: <td bgcolor="#FFFFAA" align="center">
625: Cut selected<br>
626: <input type=submit name=cut value='<<<'><p>
627: <hr>
628: Paste after selected<br>
629: <input type=submit name=paste value='>>>'>
630: </td>
631: <td bgcolor="#FFFFCC"><select name="target" multiple>
632: $targetwindow
633: </select>
634: </table>
635: <input type=hidden name=importdetail value="">
636: <input type=hidden name=curimpdetail value="$importdetail">
637: <input type=hidden name=targetdetail value="$targetdetail">
638: <input type=hidden name=impfortarget value="0">
639: </form>
640: </body></html>
641: ENDSMPHEAD
642: }
643:
644: # ----------------------------------------------------------------- No such dir
645: sub nodir {
646: my ($r,$dir)=@_;
647: $dir=~s/^\/home\/\w+\/public\_html//;
648: $r->print(<<ENDNODIR);
649: <html>
650: <body bgcolor='#FFFFFF'>
651: <h1>No such directory: $dir</h1>
652: </body>
653: </html>
654: ENDNODIR
655: }
656:
657: # ---------------------------------------------------------------- View Handler
658:
659: sub viewmap {
660: my ($r,$url,$adv,$errtext)=@_;
661: $r->print('<html><body bgcolor="#FFFFFF">'.&buttons($adv));
662: if ($errtext) {
663: $r->print($errtext.'<hr>');
664: }
665: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
666: if (defined($_)) {
667: my ($title,$url)=split(/\:/,$_);
668: $title=~s/\&colon\;/\:/g;
669: $url=~s/\&colon\;/\:/g;
670: unless ($title) { $title=(split(/\//,$url))[-1] };
671: unless ($title) { $title='<i>Empty</i>'; }
672: if ($url) {
673: $r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
674: }
675: $r->print(&Apache::lonratsrv::qtescape($title));
676: if ($url) { $r->print('</a>'); }
677: $r->print('<br>');
678: }
679: }
680: $r->print('</body></html>');
681: }
682:
683: # ================================================================ Main Handler
684:
685: sub handler {
686: my $r=shift;
687: $r->content_type('text/html');
688: $r->send_http_header;
689:
690: return OK if $r->header_only;
691:
692: my $url=$r->uri;
693: my $fn=&Apache::lonnet::filelocation('',$url);
694:
695: my ($dir)=($fn=~/^(.+)\/[^\/]+$/);
696: unless (-e $dir) {
697: &nodir($r,$dir);
698: return OK;
699: }
700:
701: # ------------------------------------------- Determine which tools can be used
702: my $adv=0;
703:
704: unless ($ENV{'form.forcesmp'}) {
705: if ($ENV{'form.forceadv'}) {
706: $adv=1;
707: } elsif (my $fh=Apache::File->new($fn)) {
708: my $allmap=join('',<$fh>);
709: $adv=($allmap=~/\<map[^\>]+mode\s*\=\s*(\'|\")rat/is);
710: }
711: }
712:
713: my $errtext='';
714: my $fatal=0;
715:
716: # -------------------------------------------------------------------- Load map
717: ($errtext,$fatal)=&mapread($fn,$errtext);
718:
719: if ($fatal==1) { $adv=1; }
720:
721: # ----------------------------------- adv==1 now means "graphical MUST be used"
722:
723: if ($ENV{'form.forceadv'}) {
724: &ratedt($r,$url);
725: } elsif ($ENV{'form.forcesmp'}) {
726: &smpedt($r,$errtext);
727: } else {
728: &viewmap($r,$url,$adv,$errtext);
729: }
730: return OK;
731: }
732:
733: 1;
734: __END__
735:
736:
737:
738:
739:
740:
741:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>